app_list_toast_view.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #ifndef ASH_APP_LIST_VIEWS_APP_LIST_TOAST_VIEW_H_
  5. #define ASH_APP_LIST_VIEWS_APP_LIST_TOAST_VIEW_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/style/pill_button.h"
  9. #include "ui/views/controls/button/button.h"
  10. #include "ui/views/view.h"
  11. namespace views {
  12. class BoxLayout;
  13. class Button;
  14. class Label;
  15. class LabelButton;
  16. class ImageView;
  17. } // namespace views
  18. namespace gfx {
  19. struct VectorIcon;
  20. } // namespace gfx
  21. namespace ash {
  22. class AppListViewDelegate;
  23. // A view specific to AppList that displays an icon, two labels and a button.
  24. // The view can be built with all or some of the elements mentioned before, but
  25. // always will have to include at least a title. The view has a grid-like layout
  26. // with 3 columns where the icon will be on the first column, the button on the
  27. // last column and the title and subtitle will share the middle column, with the
  28. // title over the subtitle.
  29. class ASH_EXPORT AppListToastView : public views::View {
  30. public:
  31. class Builder {
  32. public:
  33. explicit Builder(const std::u16string title);
  34. virtual ~Builder();
  35. std::unique_ptr<AppListToastView> Build();
  36. // Methods for setting vector icons for the toast.
  37. // Vector icons would change appearance with theming by default.
  38. // Nevertheless there might be a case when different icons need to be used
  39. // with dark/light mode (i.e. non-monochromatic icons) and a single icon is
  40. // not enough. For this case, use SetThemingIcons().
  41. Builder& SetIcon(const gfx::VectorIcon* icon);
  42. Builder& SetThemingIcons(const gfx::VectorIcon* dark_icon,
  43. const gfx::VectorIcon* light_icon);
  44. Builder& SetIconSize(int icon_size);
  45. Builder& SetIconBackground(bool has_icon_background);
  46. Builder& SetSubtitle(const std::u16string subtitle);
  47. Builder& SetButton(std::u16string button_text,
  48. views::Button::PressedCallback button_callback);
  49. Builder& SetCloseButton(
  50. views::Button::PressedCallback close_button_callback);
  51. Builder& SetStyleForTabletMode(bool style_for_tablet_mode);
  52. Builder& SetViewDelegate(AppListViewDelegate* delegate);
  53. private:
  54. std::u16string title_;
  55. absl::optional<std::u16string> subtitle_;
  56. absl::optional<std::u16string> button_text_;
  57. const gfx::VectorIcon* icon_ = nullptr;
  58. const gfx::VectorIcon* dark_icon_ = nullptr;
  59. const gfx::VectorIcon* light_icon_ = nullptr;
  60. absl::optional<int> icon_size_;
  61. views::Button::PressedCallback button_callback_;
  62. views::Button::PressedCallback close_button_callback_;
  63. bool style_for_tablet_mode_ = false;
  64. bool has_icon_background_ = false;
  65. AppListViewDelegate* view_delegate_ = nullptr;
  66. };
  67. // Whether `view` is a ToastPillButton.
  68. static bool IsToastButton(views::View* view);
  69. explicit AppListToastView(const std::u16string title);
  70. AppListToastView(const AppListToastView&) = delete;
  71. AppListToastView& operator=(const AppListToastView&) = delete;
  72. ~AppListToastView() override;
  73. // views::View:
  74. gfx::Size GetMaximumSize() const override;
  75. gfx::Size GetMinimumSize() const override;
  76. gfx::Size CalculatePreferredSize() const override;
  77. void OnThemeChanged() override;
  78. void SetButton(std::u16string button_text,
  79. views::Button::PressedCallback button_callback);
  80. void SetCloseButton(views::Button::PressedCallback close_button_callback);
  81. void SetIcon(const gfx::VectorIcon* icon);
  82. void SetThemingIcons(const gfx::VectorIcon* dark_icon,
  83. const gfx::VectorIcon* light_icon);
  84. void SetIconSize(int icon_size);
  85. void SetTitle(const std::u16string title);
  86. void SetSubtitle(const std::u16string subtitle);
  87. void UpdateInteriorMargins(const gfx::Insets& margins);
  88. void SetViewDelegate(AppListViewDelegate* delegate);
  89. // Styles the toast for display in tablet mode launcher UI - for example, adds
  90. // background blur, and sets rounded corners on the toast layer.
  91. void StyleForTabletMode();
  92. // Sets whether the icon for the toast should have a background.
  93. void AddIconBackground();
  94. views::LabelButton* toast_button() const { return toast_button_; }
  95. views::Button* close_button() const { return close_button_; }
  96. private:
  97. class ToastPillButton : public PillButton {
  98. public:
  99. ToastPillButton(AppListViewDelegate* view_delegate,
  100. PressedCallback callback,
  101. const std::u16string& text,
  102. Type type,
  103. const gfx::VectorIcon* icon);
  104. // views::View:
  105. void OnFocus() override;
  106. void OnBlur() override;
  107. AppListViewDelegate* view_delegate_ = nullptr;
  108. };
  109. // Attach the icon to the toast based on theming and available icons.
  110. void UpdateIconImage();
  111. // Creates an ImageView for the icon and inserts it in the toast view.
  112. void CreateIconView();
  113. // Get the available space for `title_label_` width.
  114. int GetExpandedTitleLabelWidth();
  115. // Vector icons to use with dark/light mode.
  116. const gfx::VectorIcon* dark_icon_ = nullptr;
  117. const gfx::VectorIcon* light_icon_ = nullptr;
  118. // Vector icon to use if there are not dark or light mode specific icons.
  119. const gfx::VectorIcon* default_icon_ = nullptr;
  120. absl::optional<int> icon_size_;
  121. // Whether the toast UI should be style for tablet mode app list UI.
  122. bool style_for_tablet_mode_ = false;
  123. // Whether the toast icon should be styled with a background.
  124. bool has_icon_background_ = false;
  125. AppListViewDelegate* view_delegate_ = nullptr;
  126. // Toast icon view.
  127. views::ImageView* icon_ = nullptr;
  128. // Label with the main text for the toast.
  129. views::Label* title_label_ = nullptr;
  130. // Label with the subtext for the toast.
  131. views::Label* subtitle_label_ = nullptr;
  132. // The button for the toast.
  133. ToastPillButton* toast_button_ = nullptr;
  134. // The close button for the toast.
  135. views::Button* close_button_ = nullptr;
  136. // Helper view to layout labels.
  137. views::View* label_container_ = nullptr;
  138. // Layout manager for the view.
  139. views::BoxLayout* layout_manager_ = nullptr;
  140. };
  141. } // namespace ash
  142. #endif // ASH_APP_LIST_VIEWS_APP_LIST_TOAST_VIEW_H_