sub_feature_opt_in_view.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/system/phonehub/sub_feature_opt_in_view.h"
  5. #include <string>
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/style/ash_color_provider.h"
  8. #include "ash/style/pill_button.h"
  9. #include "ash/system/phonehub/phone_hub_view_ids.h"
  10. #include "base/strings/utf_string_conversions.h"
  11. #include "ui/base/l10n/l10n_util.h"
  12. #include "ui/chromeos/devicetype_utils.h"
  13. #include "ui/compositor/layer.h"
  14. #include "ui/gfx/geometry/insets.h"
  15. #include "ui/views/border.h"
  16. #include "ui/views/controls/label.h"
  17. #include "ui/views/layout/box_layout_view.h"
  18. #include "ui/views/layout/flex_layout.h"
  19. #include "ui/views/view_class_properties.h"
  20. #include "url/gurl.h"
  21. namespace ash {
  22. namespace {
  23. // Appearance.
  24. constexpr int kButtonSpacingDip = 8;
  25. constexpr int kBorderThicknessDip = 1;
  26. constexpr int kBorderCornerRadiusDip = 8;
  27. constexpr auto kTextLabelBorderInsets = gfx::Insets::TLBR(12, 16, 12, 16);
  28. constexpr auto kButtonContainerBorderInsets = gfx::Insets::TLBR(0, 0, 12, 16);
  29. constexpr int kTextLabelLineHeightDip = 20;
  30. // Typography.
  31. constexpr int kLabelTextFontSizeDip = 14;
  32. } // namespace
  33. SubFeatureOptInView::SubFeatureOptInView(PhoneHubViewID view_id,
  34. int description_string_id,
  35. int set_up_button_string_id)
  36. : view_id_(view_id),
  37. description_string_id_(description_string_id),
  38. set_up_button_string_id_(set_up_button_string_id) {
  39. SetID(view_id_);
  40. InitLayout();
  41. }
  42. SubFeatureOptInView::~SubFeatureOptInView() = default;
  43. void SubFeatureOptInView::RefreshDescription(int description_string_id) {
  44. description_string_id_ = description_string_id;
  45. text_label_->SetText(l10n_util::GetStringFUTF16(description_string_id_,
  46. ui::GetChromeOSDeviceName()));
  47. }
  48. void SubFeatureOptInView::InitLayout() {
  49. // The dark light mode, we switch TrayBubbleView to use a textured layer
  50. // instead of solid color layer, so no need to create an extra layer here.
  51. if (!features::IsDarkLightModeEnabled()) {
  52. SetPaintToLayer();
  53. layer()->SetFillsBoundsOpaquely(false);
  54. }
  55. const SkColor border_color = AshColorProvider::Get()->GetContentLayerColor(
  56. AshColorProvider::ContentLayerType::kSeparatorColor);
  57. SetBorder(views::CreateRoundedRectBorder(
  58. kBorderThicknessDip, kBorderCornerRadiusDip, border_color));
  59. auto* layout = SetLayoutManager(std::make_unique<views::FlexLayout>());
  60. layout->SetOrientation(views::LayoutOrientation::kVertical);
  61. layout->SetMainAxisAlignment(views::LayoutAlignment::kCenter);
  62. // Set up layout row for the text label.
  63. text_label_ = AddChildView(std::make_unique<views::Label>());
  64. text_label_->SetProperty(views::kCrossAxisAlignmentKey,
  65. views::LayoutAlignment::kCenter);
  66. text_label_->SetProperty(views::kMarginsKey, kTextLabelBorderInsets);
  67. text_label_->SetProperty(
  68. views::kFlexBehaviorKey,
  69. views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum,
  70. views::MaximumFlexSizeRule::kUnbounded, true));
  71. auto text_color = AshColorProvider::Get()->GetContentLayerColor(
  72. AshColorProvider::ContentLayerType::kTextColorPrimary);
  73. text_label_->SetEnabledColor(text_color);
  74. text_label_->SetAutoColorReadabilityEnabled(false);
  75. auto default_font = text_label_->font_list();
  76. text_label_->SetFontList(default_font
  77. .DeriveWithSizeDelta(kLabelTextFontSizeDip -
  78. default_font.GetFontSize())
  79. .DeriveWithWeight(gfx::Font::Weight::MEDIUM));
  80. text_label_->SetLineHeight(kTextLabelLineHeightDip);
  81. text_label_->SetMultiLine(/*multi_line=*/true);
  82. text_label_->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
  83. text_label_->SetText(l10n_util::GetStringFUTF16(description_string_id_,
  84. ui::GetChromeOSDeviceName()));
  85. // Set up layout row for the buttons.
  86. auto* button_container =
  87. AddChildView(std::make_unique<views::BoxLayoutView>());
  88. button_container->SetProperty(views::kCrossAxisAlignmentKey,
  89. views::LayoutAlignment::kEnd);
  90. button_container->SetBetweenChildSpacing(kButtonSpacingDip);
  91. button_container->SetBorder(
  92. views::CreateEmptyBorder(kButtonContainerBorderInsets));
  93. dismiss_button_ = button_container->AddChildView(std::make_unique<PillButton>(
  94. base::BindRepeating(&SubFeatureOptInView::DismissButtonPressed,
  95. base::Unretained(this)),
  96. l10n_util::GetStringUTF16(
  97. IDS_ASH_PHONE_HUB_SUB_FEATURE_OPT_IN_DISMISS_BUTTON),
  98. PillButton::Type::kIconlessFloating, /*icon=*/nullptr));
  99. dismiss_button_->SetID(kSubFeatureOptInDismissButton);
  100. set_up_button_ = button_container->AddChildView(std::make_unique<PillButton>(
  101. base::BindRepeating(&SubFeatureOptInView::SetUpButtonPressed,
  102. base::Unretained(this)),
  103. l10n_util::GetStringUTF16(set_up_button_string_id_),
  104. PillButton::Type::kIconless, /*icon=*/nullptr));
  105. set_up_button_->SetID(kSubFeatureOptInConfirmButton);
  106. }
  107. } // namespace ash