privacy_screen_toast_view.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2020 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/privacy_screen/privacy_screen_toast_view.h"
  5. #include "ash/constants/ash_features.h"
  6. #include "ash/resources/vector_icons/vector_icons.h"
  7. #include "ash/strings/grit/ash_strings.h"
  8. #include "ash/style/ash_color_provider.h"
  9. #include "ash/system/privacy_screen/privacy_screen_toast_controller.h"
  10. #include "ash/system/tray/tray_constants.h"
  11. #include "ash/system/unified/feature_pod_button.h"
  12. #include "ui/base/l10n/l10n_util.h"
  13. #include "ui/compositor/layer.h"
  14. #include "ui/gfx/paint_vector_icon.h"
  15. #include "ui/views/controls/button/button.h"
  16. #include "ui/views/controls/image_view.h"
  17. #include "ui/views/controls/label.h"
  18. #include "ui/views/layout/box_layout.h"
  19. namespace ash {
  20. namespace {
  21. void ConfigureLabel(views::Label* label, SkColor color, int font_size) {
  22. label->SetAutoColorReadabilityEnabled(false);
  23. label->SetSubpixelRenderingEnabled(false);
  24. label->SetEnabledColor(color);
  25. gfx::Font default_font;
  26. gfx::Font label_font =
  27. default_font.Derive(font_size - default_font.GetFontSize(),
  28. gfx::Font::NORMAL, gfx::Font::Weight::NORMAL);
  29. gfx::FontList font_list(label_font);
  30. label->SetFontList(font_list);
  31. }
  32. } // namespace
  33. // View shown if the privacy screen setting is enterprise managed.
  34. class PrivacyScreenToastManagedView : public views::View {
  35. public:
  36. PrivacyScreenToastManagedView() {
  37. SetLayoutManager(std::make_unique<views::BoxLayout>(
  38. views::BoxLayout::Orientation::kHorizontal, gfx::Insets(),
  39. kUnifiedManagedDeviceSpacing));
  40. views::Label* label = new views::Label();
  41. views::ImageView* icon = new views::ImageView();
  42. const AshColorProvider* color_provider = AshColorProvider::Get();
  43. const SkColor label_color = color_provider->GetContentLayerColor(
  44. AshColorProvider::ContentLayerType::kTextColorSecondary);
  45. ConfigureLabel(label, label_color, kPrivacyScreenToastSubLabelFontSize);
  46. label->SetText(l10n_util::GetStringUTF16(
  47. IDS_ASH_STATUS_TRAY_PRIVACY_SCREEN_ENTERPRISE_MANAGED));
  48. icon->SetPreferredSize(
  49. gfx::Size(kUnifiedSystemInfoHeight, kUnifiedSystemInfoHeight));
  50. const SkColor icon_color = color_provider->GetContentLayerColor(
  51. AshColorProvider::ContentLayerType::kTextColorSecondary);
  52. icon->SetImage(gfx::CreateVectorIcon(kSystemTrayManagedIcon, icon_color));
  53. AddChildView(label);
  54. AddChildView(icon);
  55. }
  56. ~PrivacyScreenToastManagedView() override = default;
  57. };
  58. // View containing the various labels in the toast.
  59. class PrivacyScreenToastLabelView : public views::View {
  60. public:
  61. PrivacyScreenToastLabelView() {
  62. auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
  63. views::BoxLayout::Orientation::kVertical, gfx::Insets(), 0));
  64. layout->set_cross_axis_alignment(
  65. views::BoxLayout::CrossAxisAlignment::kStart);
  66. label_ = new views::Label();
  67. managed_view_ = new PrivacyScreenToastManagedView();
  68. AddChildView(label_);
  69. AddChildView(managed_view_);
  70. const AshColorProvider* color_provider = AshColorProvider::Get();
  71. const SkColor primary_text_color = color_provider->GetContentLayerColor(
  72. AshColorProvider::ContentLayerType::kTextColorPrimary);
  73. ConfigureLabel(label_, primary_text_color,
  74. kPrivacyScreenToastMainLabelFontSize);
  75. }
  76. ~PrivacyScreenToastLabelView() override = default;
  77. void SetPrivacyScreenEnabled(bool enabled, bool managed) {
  78. if (enabled) {
  79. label_->SetText(l10n_util::GetStringUTF16(
  80. IDS_ASH_STATUS_TRAY_PRIVACY_SCREEN_ON_STATE));
  81. } else {
  82. label_->SetText(l10n_util::GetStringUTF16(
  83. IDS_ASH_STATUS_TRAY_PRIVACY_SCREEN_OFF_STATE));
  84. }
  85. managed_view_->SetVisible(managed);
  86. }
  87. private:
  88. views::Label* label_;
  89. PrivacyScreenToastManagedView* managed_view_;
  90. };
  91. PrivacyScreenToastView::PrivacyScreenToastView(
  92. PrivacyScreenToastController* controller,
  93. views::Button::PressedCallback callback)
  94. : controller_(controller) {
  95. auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
  96. views::BoxLayout::Orientation::kHorizontal, kPrivacyScreenToastInsets,
  97. kPrivacyScreenToastSpacing));
  98. layout->set_cross_axis_alignment(
  99. views::BoxLayout::CrossAxisAlignment::kCenter);
  100. button_ =
  101. new FeaturePodIconButton(std::move(callback), /*is_togglable=*/true);
  102. button_->SetVectorIcon(kPrivacyScreenIcon);
  103. button_->SetToggled(false);
  104. button_->AddObserver(this);
  105. AddChildView(button_);
  106. label_ = new PrivacyScreenToastLabelView();
  107. AddChildView(label_);
  108. // In dark light mode, we switch TrayBubbleView to use a textured layer
  109. // instead of solid color layer, so no need to create an extra layer here.
  110. if (!features::IsDarkLightModeEnabled()) {
  111. SetPaintToLayer();
  112. layer()->SetFillsBoundsOpaquely(false);
  113. }
  114. }
  115. PrivacyScreenToastView::~PrivacyScreenToastView() {
  116. button_->RemoveObserver(this);
  117. }
  118. void PrivacyScreenToastView::SetPrivacyScreenEnabled(bool enabled,
  119. bool managed) {
  120. is_enabled_ = enabled;
  121. is_managed_ = managed;
  122. button_->SetToggled(enabled);
  123. label_->SetPrivacyScreenEnabled(enabled, managed);
  124. std::u16string state = l10n_util::GetStringUTF16(
  125. is_enabled_ ? IDS_ASH_STATUS_TRAY_PRIVACY_SCREEN_ON_STATE
  126. : IDS_ASH_STATUS_TRAY_PRIVACY_SCREEN_OFF_STATE);
  127. button_->SetTooltipText(l10n_util::GetStringFUTF16(
  128. IDS_ASH_STATUS_TRAY_PRIVACY_SCREEN_TOOLTIP, state));
  129. Layout();
  130. }
  131. std::u16string PrivacyScreenToastView::GetAccessibleName() {
  132. std::u16string enabled_state = l10n_util::GetStringUTF16(
  133. is_enabled_ ? IDS_ASH_STATUS_TRAY_PRIVACY_SCREEN_ON_STATE
  134. : IDS_ASH_STATUS_TRAY_PRIVACY_SCREEN_OFF_STATE);
  135. std::u16string managed_state =
  136. is_managed_ ? l10n_util::GetStringUTF16(
  137. IDS_ASH_STATUS_TRAY_PRIVACY_SCREEN_ENTERPRISE_MANAGED)
  138. : std::u16string();
  139. return l10n_util::GetStringFUTF16(
  140. IDS_ASH_STATUS_TRAY_PRIVACY_SCREEN_TOAST_ACCESSIBILITY_TEXT,
  141. enabled_state, managed_state);
  142. }
  143. bool PrivacyScreenToastView::IsButtonFocused() const {
  144. return button_->HasFocus();
  145. }
  146. void PrivacyScreenToastView::OnViewFocused(views::View* observed_view) {
  147. DCHECK(observed_view == button_);
  148. controller_->StopAutocloseTimer();
  149. }
  150. void PrivacyScreenToastView::OnViewBlurred(views::View* observed_view) {
  151. DCHECK(observed_view == button_);
  152. controller_->StartAutoCloseTimer();
  153. }
  154. } // namespace ash