feature_pod_button.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifndef ASH_SYSTEM_UNIFIED_FEATURE_POD_BUTTON_H_
  5. #define ASH_SYSTEM_UNIFIED_FEATURE_POD_BUTTON_H_
  6. #include "ash/ash_export.h"
  7. #include "ash/style/icon_button.h"
  8. #include "base/bind.h"
  9. #include "ui/base/metadata/metadata_header_macros.h"
  10. #include "ui/gfx/vector_icon_types.h"
  11. #include "ui/views/view.h"
  12. namespace views {
  13. class ImageView;
  14. class Label;
  15. }
  16. namespace ash {
  17. class FeaturePodControllerBase;
  18. // TODO(crbug/1276545): Remove FeaturePodIconButton after the migration.
  19. // A toggle button with an icon used by feature pods and in other places.
  20. class ASH_EXPORT FeaturePodIconButton : public IconButton {
  21. public:
  22. METADATA_HEADER(FeaturePodIconButton);
  23. FeaturePodIconButton(PressedCallback callback, bool is_togglable);
  24. FeaturePodIconButton(const FeaturePodIconButton&) = delete;
  25. FeaturePodIconButton& operator=(const FeaturePodIconButton&) = delete;
  26. ~FeaturePodIconButton() override;
  27. };
  28. // Button internally used in FeaturePodButton. Should not be used directly.
  29. class ASH_EXPORT FeaturePodLabelButton : public views::Button {
  30. public:
  31. METADATA_HEADER(FeaturePodLabelButton);
  32. explicit FeaturePodLabelButton(PressedCallback callback);
  33. FeaturePodLabelButton(const FeaturePodLabelButton&) = delete;
  34. FeaturePodLabelButton& operator=(const FeaturePodLabelButton&) = delete;
  35. ~FeaturePodLabelButton() override;
  36. // Set the text of label shown below the icon. See FeaturePodButton::SetLabel.
  37. void SetLabel(const std::u16string& label);
  38. const std::u16string& GetLabelText() const;
  39. // Set the text of sub-label shown below the label.
  40. // See FeaturePodButton::SetSubLabel.
  41. void SetSubLabel(const std::u16string& sub_label);
  42. const std::u16string& GetSubLabelText() const;
  43. // Show arrow to indicate that the feature has a detailed view.
  44. // See FeaturePodButton::ShowDetailedViewArrow.
  45. void ShowDetailedViewArrow();
  46. // views::Button:
  47. void Layout() override;
  48. gfx::Size CalculatePreferredSize() const override;
  49. void OnThemeChanged() override;
  50. private:
  51. // Layout |child| in horizontal center with its vertical origin set to |y|.
  52. void LayoutInCenter(views::View* child, int y);
  53. void OnEnabledChanged();
  54. // Owned by views hierarchy.
  55. views::Label* const label_;
  56. views::Label* const sub_label_;
  57. views::ImageView* const detailed_view_arrow_;
  58. base::CallbackListSubscription enabled_changed_subscription_ =
  59. AddEnabledChangedCallback(
  60. base::BindRepeating(&FeaturePodLabelButton::OnEnabledChanged,
  61. base::Unretained(this)));
  62. };
  63. // A button in FeaturePodsView. These buttons are main entry points of features
  64. // in UnifiedSystemTray. Each button has its icon, label, and sub-label placed
  65. // vertically. The button may be togglable and the background color indicates
  66. // the current state. Otherwise, the button is not a toggle button and just
  67. // navigates to the appropriate detailed view.
  68. // See the comment in FeaturePodsView for detail.
  69. class ASH_EXPORT FeaturePodButton : public views::View {
  70. public:
  71. METADATA_HEADER(FeaturePodButton);
  72. explicit FeaturePodButton(FeaturePodControllerBase* controller,
  73. bool is_togglable = true);
  74. FeaturePodButton(const FeaturePodButton&) = delete;
  75. FeaturePodButton& operator=(const FeaturePodButton&) = delete;
  76. ~FeaturePodButton() override;
  77. // Set the vector icon shown in a circle.
  78. void SetVectorIcon(const gfx::VectorIcon& icon);
  79. // Set the text of label shown below the icon.
  80. void SetLabel(const std::u16string& label);
  81. // Set the text of sub-label shown below the label.
  82. void SetSubLabel(const std::u16string& sub_label);
  83. // Set the tooltip text of the icon button.
  84. void SetIconTooltip(const std::u16string& text);
  85. // Set the tooltip text of the label button.
  86. void SetLabelTooltip(const std::u16string& text);
  87. // Convenience method to set both icon and label tooltip texts.
  88. void SetIconAndLabelTooltips(const std::u16string& text);
  89. // Show arrow to indicate that the feature has a detailed view.
  90. void ShowDetailedViewArrow();
  91. // Remove the label button from keyboard focus chain. This is useful when
  92. // the icon button and the label button has the same action.
  93. void DisableLabelButtonFocus();
  94. // Change the toggled state. If toggled, the background color of the circle
  95. // will change. If the button is not togglable, then SetToggled() will do
  96. // nothing and |IsToggled()| will always return false.
  97. void SetToggled(bool toggled);
  98. bool IsToggled() const { return icon_button_->toggled(); }
  99. // Change the expanded state. 0.0 if collapsed, and 1.0 if expanded.
  100. // Otherwise, it shows intermediate state. In the collapsed state, the labels
  101. // are not shown, so the label buttons always fade out as expanded_amount
  102. // decreases. We also need to fade out the icon button when it's not part of
  103. // the buttons visible in the collapsed state. fade_icon_button will be passed
  104. // as true for these cases.
  105. void SetExpandedAmount(double expanded_amount, bool fade_icon_button);
  106. // Get opacity for a given expanded_amount value. Used to fade out
  107. // all label buttons and icon buttons that are hidden in collapsed state
  108. // while collapsing.
  109. double GetOpacityForExpandedAmount(double expanded_amount);
  110. // Only called by the container. Same as SetVisible but doesn't change
  111. // |visible_preferred_| flag.
  112. void SetVisibleByContainer(bool visible);
  113. // views::View:
  114. void SetVisible(bool visible) override;
  115. bool HasFocus() const override;
  116. void RequestFocus() override;
  117. bool visible_preferred() const { return visible_preferred_; }
  118. FeaturePodIconButton* icon_button() const { return icon_button_; }
  119. FeaturePodLabelButton* label_button() const { return label_button_; }
  120. private:
  121. // For unit tests.
  122. friend class BluetoothFeaturePodControllerTest;
  123. friend class NetworkFeaturePodControllerTest;
  124. friend class NightLightFeaturePodControllerTest;
  125. void OnEnabledChanged();
  126. // Owned by views hierarchy.
  127. FeaturePodIconButton* const icon_button_;
  128. FeaturePodLabelButton* const label_button_;
  129. // If true, it is preferred by the FeaturePodController that the view is
  130. // visible. Usually, this should match visible(), but in case that the
  131. // container does not have enough space, it might not match.
  132. // In such case, the preferred visibility is reflected after the container is
  133. // expanded.
  134. bool visible_preferred_ = true;
  135. base::CallbackListSubscription enabled_changed_subscription_ =
  136. AddEnabledChangedCallback(
  137. base::BindRepeating(&FeaturePodButton::OnEnabledChanged,
  138. base::Unretained(this)));
  139. };
  140. } // namespace ash
  141. #endif // ASH_SYSTEM_UNIFIED_FEATURE_POD_BUTTON_H_