detailed_view_delegate.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2018 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/tray/detailed_view_delegate.h"
  5. #include "ash/resources/vector_icons/vector_icons.h"
  6. #include "ash/strings/grit/ash_strings.h"
  7. #include "ash/style/ash_color_provider.h"
  8. #include "ash/style/icon_button.h"
  9. #include "ash/system/tray/hover_highlight_view.h"
  10. #include "ash/system/tray/tray_constants.h"
  11. #include "ash/system/tray/tray_popup_utils.h"
  12. #include "ash/system/unified/unified_system_tray_controller.h"
  13. #include "components/vector_icons/vector_icons.h"
  14. #include "ui/base/l10n/l10n_util.h"
  15. #include "ui/gfx/paint_vector_icon.h"
  16. #include "ui/gfx/scoped_canvas.h"
  17. #include "ui/gfx/vector_icon_types.h"
  18. #include "ui/views/border.h"
  19. #include "ui/views/controls/label.h"
  20. #include "ui/views/controls/separator.h"
  21. #include "ui/views/layout/box_layout.h"
  22. namespace ash {
  23. using ContentLayerType = AshColorProvider::ContentLayerType;
  24. namespace {
  25. void ConfigureTitleTriView(TriView* tri_view, TriView::Container container) {
  26. std::unique_ptr<views::BoxLayout> layout;
  27. switch (container) {
  28. case TriView::Container::START:
  29. case TriView::Container::END: {
  30. const int left_padding = container == TriView::Container::START
  31. ? kUnifiedBackButtonLeftPadding
  32. : 0;
  33. layout = std::make_unique<views::BoxLayout>(
  34. views::BoxLayout::Orientation::kHorizontal,
  35. gfx::Insets::TLBR(0, left_padding, 0, 0), kUnifiedTopShortcutSpacing);
  36. layout->set_main_axis_alignment(
  37. views::BoxLayout::MainAxisAlignment::kCenter);
  38. layout->set_cross_axis_alignment(
  39. views::BoxLayout::CrossAxisAlignment::kCenter);
  40. break;
  41. }
  42. case TriView::Container::CENTER:
  43. tri_view->SetFlexForContainer(TriView::Container::CENTER, 1.f);
  44. layout = std::make_unique<views::BoxLayout>(
  45. views::BoxLayout::Orientation::kVertical);
  46. layout->set_main_axis_alignment(
  47. views::BoxLayout::MainAxisAlignment::kCenter);
  48. layout->set_cross_axis_alignment(
  49. views::BoxLayout::CrossAxisAlignment::kStretch);
  50. break;
  51. }
  52. tri_view->SetContainerLayout(container, std::move(layout));
  53. tri_view->SetMinSize(container,
  54. gfx::Size(0, kUnifiedDetailedViewTitleRowHeight));
  55. }
  56. class BackButton : public IconButton {
  57. public:
  58. BackButton(views::Button::PressedCallback callback)
  59. : IconButton(std::move(callback),
  60. IconButton::Type::kSmallFloating,
  61. &kUnifiedMenuExpandIcon,
  62. IDS_ASH_STATUS_TRAY_PREVIOUS_MENU) {}
  63. BackButton(const BackButton&) = delete;
  64. BackButton& operator=(const BackButton&) = delete;
  65. ~BackButton() override = default;
  66. // Use the same icon as CollapseButton with rotation.
  67. void PaintButtonContents(gfx::Canvas* canvas) override {
  68. gfx::ScopedCanvas scoped(canvas);
  69. canvas->Translate(gfx::Vector2d(size().width() / 2, size().height() / 2));
  70. canvas->sk_canvas()->rotate(-90);
  71. gfx::ImageSkia image = GetImageToPaint();
  72. canvas->DrawImageInt(image, -image.width() / 2, -image.height() / 2);
  73. }
  74. };
  75. } // namespace
  76. DetailedViewDelegate::DetailedViewDelegate(
  77. UnifiedSystemTrayController* tray_controller)
  78. : tray_controller_(tray_controller) {}
  79. DetailedViewDelegate::~DetailedViewDelegate() = default;
  80. void DetailedViewDelegate::TransitionToMainView(bool restore_focus) {
  81. tray_controller_->TransitionToMainView(restore_focus);
  82. }
  83. void DetailedViewDelegate::CloseBubble() {
  84. tray_controller_->CloseBubble();
  85. }
  86. absl::optional<SkColor> DetailedViewDelegate::GetBackgroundColor() {
  87. return absl::nullopt;
  88. }
  89. bool DetailedViewDelegate::IsOverflowIndicatorEnabled() const {
  90. return false;
  91. }
  92. TriView* DetailedViewDelegate::CreateTitleRow(int string_id) {
  93. auto* tri_view = new TriView(kUnifiedTopShortcutSpacing);
  94. ConfigureTitleTriView(tri_view, TriView::Container::START);
  95. ConfigureTitleTriView(tri_view, TriView::Container::CENTER);
  96. ConfigureTitleTriView(tri_view, TriView::Container::END);
  97. title_label_ = TrayPopupUtils::CreateDefaultLabel();
  98. title_label_->SetText(l10n_util::GetStringUTF16(string_id));
  99. title_label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
  100. AshColorProvider::ContentLayerType::kTextColorPrimary));
  101. TrayPopupUtils::SetLabelFontList(title_label_,
  102. TrayPopupUtils::FontStyle::kTitle);
  103. tri_view->AddView(TriView::Container::CENTER, title_label_);
  104. tri_view->SetContainerVisible(TriView::Container::END, false);
  105. tri_view->SetBorder(
  106. views::CreateEmptyBorder(kUnifiedDetailedViewTitlePadding));
  107. return tri_view;
  108. }
  109. views::View* DetailedViewDelegate::CreateTitleSeparator() {
  110. title_separator_ = new views::Separator();
  111. title_separator_->SetColorId(ui::kColorAshSystemUIMenuSeparator);
  112. title_separator_->SetBorder(views::CreateEmptyBorder(gfx::Insets::TLBR(
  113. kTitleRowProgressBarHeight - views::Separator::kThickness, 0, 0, 0)));
  114. return title_separator_;
  115. }
  116. void DetailedViewDelegate::ShowStickyHeaderSeparator(views::View* view,
  117. bool show_separator) {
  118. if (show_separator) {
  119. view->SetBorder(views::CreatePaddedBorder(
  120. views::CreateSolidSidedBorder(
  121. gfx::Insets::TLBR(0, 0, kTraySeparatorWidth, 0),
  122. AshColorProvider::Get()->GetContentLayerColor(
  123. ContentLayerType::kSeparatorColor)),
  124. gfx::Insets::TLBR(kMenuSeparatorVerticalPadding, 0,
  125. kMenuSeparatorVerticalPadding - kTraySeparatorWidth,
  126. 0)));
  127. } else {
  128. view->SetBorder(views::CreateEmptyBorder(
  129. gfx::Insets::VH(kMenuSeparatorVerticalPadding, 0)));
  130. }
  131. view->SchedulePaint();
  132. }
  133. HoverHighlightView* DetailedViewDelegate::CreateScrollListItem(
  134. ViewClickListener* listener,
  135. const gfx::VectorIcon& icon,
  136. const std::u16string& text) {
  137. HoverHighlightView* item = new HoverHighlightView(listener);
  138. if (icon.is_empty())
  139. item->AddLabelRow(text);
  140. else
  141. item->AddIconAndLabel(
  142. gfx::CreateVectorIcon(icon,
  143. AshColorProvider::Get()->GetContentLayerColor(
  144. ContentLayerType::kIconColorPrimary)),
  145. text);
  146. return item;
  147. }
  148. views::Button* DetailedViewDelegate::CreateBackButton(
  149. views::Button::PressedCallback callback) {
  150. return new BackButton(std::move(callback));
  151. }
  152. views::Button* DetailedViewDelegate::CreateInfoButton(
  153. views::Button::PressedCallback callback,
  154. int info_accessible_name_id) {
  155. return new IconButton(std::move(callback), IconButton::Type::kSmall,
  156. &kUnifiedMenuInfoIcon, info_accessible_name_id);
  157. }
  158. views::Button* DetailedViewDelegate::CreateSettingsButton(
  159. views::Button::PressedCallback callback,
  160. int setting_accessible_name_id) {
  161. auto* button = new IconButton(std::move(callback), IconButton::Type::kSmall,
  162. &vector_icons::kSettingsOutlineIcon,
  163. setting_accessible_name_id);
  164. if (!TrayPopupUtils::CanOpenWebUISettings())
  165. button->SetEnabled(false);
  166. return button;
  167. }
  168. views::Button* DetailedViewDelegate::CreateHelpButton(
  169. views::Button::PressedCallback callback) {
  170. auto* button =
  171. new IconButton(std::move(callback), IconButton::Type::kSmall,
  172. &vector_icons::kHelpOutlineIcon, IDS_ASH_STATUS_TRAY_HELP);
  173. // Help opens a web page, so treat it like Web UI settings.
  174. if (!TrayPopupUtils::CanOpenWebUISettings())
  175. button->SetEnabled(false);
  176. return button;
  177. }
  178. void DetailedViewDelegate::UpdateColors() {
  179. if (title_label_) {
  180. title_label_->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor(
  181. AshColorProvider::ContentLayerType::kTextColorPrimary));
  182. }
  183. if (title_separator_) {
  184. title_separator_->SetColorId(ui::kColorAshSystemUIMenuSeparator);
  185. }
  186. }
  187. } // namespace ash