[go: nahoru, domu]

blob: a672c4ee8c43ab348b78cfa650b8275e105090fb [file] [log] [blame]
Avi Drissman3a215d1e2022-09-07 19:43:091// Copyright 2014 The Chromium Authors
James Cookb0bf8e82017-04-09 17:01:442// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Manu Cornetf953d492019-06-27 05:56:515#ifndef ASH_SHELF_HOME_BUTTON_H_
6#define ASH_SHELF_HOME_BUTTON_H_
James Cookb0bf8e82017-04-09 17:01:447
8#include <memory>
9
Manu Cornet1ed1f6b2019-06-14 17:25:5310#include "ash/app_list/app_list_metrics.h"
Matthew Mourgosd4772822023-04-05 18:35:0311#include "ash/app_list/app_list_model_provider.h"
12#include "ash/app_list/quick_app_access_model.h"
James Cookb0bf8e82017-04-09 17:01:4413#include "ash/ash_export.h"
Matthew Mourgosd4772822023-04-05 18:35:0314#include "ash/public/cpp/app_list/app_list_controller_observer.h"
Toni Barzic705b8aa2023-05-24 01:50:1015#include "ash/public/cpp/shelf_config.h"
Manu Cornetf953d492019-06-27 05:56:5116#include "ash/shelf/home_button_controller.h"
Manu Cornet86aef982019-07-18 21:31:1017#include "ash/shelf/shelf_button_delegate.h"
Manu Cornet40f908122018-11-08 23:15:2518#include "ash/shelf/shelf_control_button.h"
Matthew Mourgosd4772822023-04-05 18:35:0319#include "ash/shell_observer.h"
Arthur Sonzogni834e018f2023-04-22 10:20:0220#include "base/memory/raw_ptr.h"
Matthew Mourgosd4772822023-04-05 18:35:0321#include "base/scoped_observation.h"
Yuki Awano2090b462021-06-08 01:33:0722#include "ui/gfx/geometry/rect.h"
Matthew Mourgos1f286322023-04-20 18:16:5023#include "ui/gfx/geometry/transform.h"
Ahmed Mehfooz6509620f2019-05-21 20:35:1224#include "ui/views/view_targeter_delegate.h"
xiaohuicfb5c54fc2017-06-28 17:32:0225
Wen-Chien Wang3667df202021-09-30 20:08:2326namespace views {
Wen-Chien Wang51a5d604d2022-05-10 18:18:0127class AnimationBuilder;
Wen-Chien Wang3667df202021-09-30 20:08:2328class CircleLayerDelegate;
Matthew Mourgosd4772822023-04-05 18:35:0329class ImageButton;
Wen-Chien Wang51a5d604d2022-05-10 18:18:0130class Label;
Wen-Chien Wang3667df202021-09-30 20:08:2331} // namespace views
32
Wen-Chien Wang617c38a2021-10-07 10:23:5033namespace ui {
Wen-Chien Wang51a5d604d2022-05-10 18:18:0134class LayerOwner;
Wen-Chien Wang617c38a2021-10-07 10:23:5035}
36
James Cookb0bf8e82017-04-09 17:01:4437namespace ash {
Sammie Quon9b911f2f2017-12-15 02:53:1538
Wen-Chien Wang51a5d604d2022-05-10 18:18:0139class Shelf;
Alex Newcomer030e7062019-07-02 00:03:5740class ShelfButtonDelegate;
Yuki Awano2090b462021-06-08 01:33:0741class ShelfNavigationWidget;
Matthew Mourgosd4772822023-04-05 18:35:0342class Shell;
James Cookb0bf8e82017-04-09 17:01:4443
Michael Giuffrida01adeb072019-03-21 22:05:3044// Button used for the AppList icon on the shelf. It opens the app list (in
45// clamshell mode) or home screen (in tablet mode). Because the clamshell-mode
46// app list appears like a dismissable overlay, the button is highlighted while
47// the app list is open in clamshell mode.
48//
49// If Assistant is enabled, the button is filled in; long-pressing it will
50// launch Assistant.
Manu Cornetf953d492019-06-27 05:56:5151class ASH_EXPORT HomeButton : public ShelfControlButton,
Manu Cornet86aef982019-07-18 21:31:1052 public ShelfButtonDelegate,
Matthew Mourgosd4772822023-04-05 18:35:0353 public views::ViewTargeterDelegate,
54 public ShellObserver,
Toni Barzic705b8aa2023-05-24 01:50:1055 public ShelfConfig::Observer,
Matthew Mourgosd4772822023-04-05 18:35:0356 public AppListModelProvider::Observer,
57 public QuickAppAccessModel::Observer {
James Cookb0bf8e82017-04-09 17:01:4458 public:
Yuki Awano2090b462021-06-08 01:33:0759 class ScopedNoClipRect {
60 public:
61 explicit ScopedNoClipRect(ShelfNavigationWidget* shelf_navigation_widget);
62 ScopedNoClipRect(const ScopedNoClipRect&) = delete;
63 ScopedNoClipRect& operator=(const ScopedNoClipRect&) = delete;
64 ~ScopedNoClipRect();
65
66 private:
Arthur Sonzogni834e018f2023-04-22 10:20:0267 const raw_ptr<ShelfNavigationWidget, ExperimentalAsh>
68 shelf_navigation_widget_;
Yuki Awano2090b462021-06-08 01:33:0769 const gfx::Rect clip_rect_;
70 };
71
Wen-Chien Wang617c38a2021-10-07 10:23:5072 // An observer that can be used to track the nudge animation state. Currently
73 // used in testing.
74 class NudgeAnimationObserver : public base::CheckedObserver {
75 public:
76 NudgeAnimationObserver() = default;
77 NudgeAnimationObserver(const NudgeAnimationObserver&) = delete;
78 NudgeAnimationObserver& operator=(const NudgeAnimationObserver&) = delete;
79 ~NudgeAnimationObserver() override = default;
80
81 // Called when the nudge animation is started/ended.
82 virtual void NudgeAnimationStarted(HomeButton* home_button) = 0;
83 virtual void NudgeAnimationEnded(HomeButton* home_button) = 0;
Wen-Chien Wang51a5d604d2022-05-10 18:18:0184
85 // Called when the nudge label is animated to fully shown.
86 virtual void NudgeLabelShown(HomeButton* home_button) = 0;
Wen-Chien Wang617c38a2021-10-07 10:23:5087 };
88
Manu Cornetc0066332019-02-20 19:26:0389 static const char kViewClassName[];
90
Manu Cornet86aef982019-07-18 21:31:1091 explicit HomeButton(Shelf* shelf);
Peter Boström5fc1d312021-09-24 02:32:1592
93 HomeButton(const HomeButton&) = delete;
94 HomeButton& operator=(const HomeButton&) = delete;
95
Manu Cornetf953d492019-06-27 05:56:5196 ~HomeButton() override;
James Cookb0bf8e82017-04-09 17:01:4497
Wen-Chien Wang51a5d604d2022-05-10 18:18:0198 // views::View:
99 gfx::Size CalculatePreferredSize() const override;
100 void Layout() override;
101
Manu Cornete363aec2019-01-13 13:07:07102 // views::Button:
James Cookb0bf8e82017-04-09 17:01:44103 void OnGestureEvent(ui::GestureEvent* event) override;
Manu Cornetc0066332019-02-20 19:26:03104 const char* GetClassName() const override;
Jan Wilken Dörrie85285b02021-03-11 23:38:47105 std::u16string GetTooltipText(const gfx::Point& p) const override;
James Cookb0bf8e82017-04-09 17:01:44106
Manu Cornet86aef982019-07-18 21:31:10107 // ShelfButtonDelegate:
108 void OnShelfButtonAboutToRequestFocusFromTabTraversal(ShelfButton* button,
109 bool reverse) override;
110 void ButtonPressed(views::Button* sender,
111 const ui::Event& event,
112 views::InkDrop* ink_drop) override;
Manu Cornet86aef982019-07-18 21:31:10113
Toni Barzic705b8aa2023-05-24 01:50:10114 // ShelfConfig::Observer:
115 void OnShelfConfigUpdated() override;
116
Michael Giuffrida01adeb072019-03-21 22:05:30117 // Called when the availability of a long-press gesture may have changed, e.g.
118 // when Assistant becomes enabled.
Yue Libcdb6aa2019-10-02 17:50:57119 void OnAssistantAvailabilityChanged();
Michael Giuffrida01adeb072019-03-21 22:05:30120
121 // True if the app list is shown for the display containing this button.
122 bool IsShowingAppList() const;
123
Toni Barzic71ebb6fd2020-05-29 17:16:53124 // Called when a locale change is detected. Updates the button tooltip and
125 // accessible name.
126 void HandleLocaleChange();
Manu Cornet1ed1f6b2019-06-14 17:25:53127
Alex Newcomer030e7062019-07-02 00:03:57128 // Returns the display which contains this view.
129 int64_t GetDisplayId() const;
130
Yuki Awano2090b462021-06-08 01:33:07131 // Clip rect of this view's widget will be removed during the life time of the
132 // returned ScopedNoClipRect.
Daniel Chengb28e2af62022-01-13 23:56:21133 [[nodiscard]] std::unique_ptr<ScopedNoClipRect> CreateScopedNoClipRect();
Yuki Awano2090b462021-06-08 01:33:07134
Wen-Chien Wang51a5d604d2022-05-10 18:18:01135 // Checks if the `nudge_label_` can be shown for the launcher nudge.
136 // NOTE: This must be called after `CreateNudgeLabel()`, where the
137 // `nudge_label_` is created. This is because whether the nudge can be shown
138 // depends on nudge_label_'s preferred size.
139 bool CanShowNudgeLabel() const;
140
Wen-Chien Wang617c38a2021-10-07 10:23:50141 // Starts the launcher nudge animation.
142 void StartNudgeAnimation();
143
144 void AddNudgeAnimationObserverForTest(NudgeAnimationObserver* observer);
145 void RemoveNudgeAnimationObserverForTest(NudgeAnimationObserver* observer);
146
Matthew Mourgosd4772822023-04-05 18:35:03147 views::View* expandable_container_for_test() const {
148 return expandable_container_;
149 }
150
151 views::Label* nudge_label_for_test() const { return nudge_label_; }
152
153 views::ImageButton* quick_app_button_for_test() const {
154 return quick_app_button_;
155 }
Wen-Chien Wang51a5d604d2022-05-10 18:18:01156
James Cookb0bf8e82017-04-09 17:01:44157 protected:
Manu Cornete363aec2019-01-13 13:07:07158 // views::Button:
Yulun Wu8924cebb2021-01-12 20:23:28159 void OnThemeChanged() override;
James Cookb0bf8e82017-04-09 17:01:44160
161 private:
Toni Barzic705b8aa2023-05-24 01:50:10162 class ButtonImageView;
163
Wen-Chien Wang51a5d604d2022-05-10 18:18:01164 // Creates `nudge_label_` for launcher nudge.
165 void CreateNudgeLabel();
166
Matthew Mourgosd4772822023-04-05 18:35:03167 // Creates the `expandable_container_` which holds either the `nudge_label_`
168 // or the `quick_app_button_`.
169 void CreateExpandableContainer();
170
171 // Creates the `quick_app_button_` to be shown next to the home button.
172 void CreateQuickAppButton();
173
174 // Called when the quick app button is pressed.
175 void QuickAppButtonPressed();
176
Wen-Chien Wang51a5d604d2022-05-10 18:18:01177 // Animation functions for launcher nudge.
178 void AnimateNudgeRipple(views::AnimationBuilder& builder);
179 void AnimateNudgeBounce(views::AnimationBuilder& builder);
180 void AnimateNudgeLabelSlideIn(views::AnimationBuilder& builder);
181 void AnimateNudgeLabelSlideOut();
182 void AnimateNudgeLabelFadeOut();
183
184 // Callbacks for the nudge animation.
185 void OnNudgeAnimationStarted();
186 void OnNudgeAnimationEnded();
187 void OnLabelSlideInAnimationEnded();
188 void OnLabelFadeOutAnimationEnded();
189
190 // Removes the nudge label from the view hierarchy.
191 void RemoveNudgeLabel();
192
Matthew Mourgosbe9f28272023-04-13 17:45:45193 // Removes the quick app button from the view hierarchy.
194 void RemoveQuickAppButton();
195
Ahmed Mehfooz6509620f2019-05-21 20:35:12196 // views::ViewTargeterDelegate:
197 bool DoesIntersectRect(const views::View* target,
198 const gfx::Rect& rect) const override;
199
Matthew Mourgosd4772822023-04-05 18:35:03200 // ShellObserver:
201 void OnShellDestroying() override;
202
203 // AppListModelProvider::Observer:
204 void OnActiveAppListModelsChanged(AppListModel* model,
205 SearchModel* search_model) override;
206
207 // QuickAppAccessModel::Observer:
Matthew Mourgosbe9f28272023-04-13 17:45:45208 void OnQuickAppShouldShowChanged(bool quick_app_shown) override;
209 void OnQuickAppIconChanged() override;
Matthew Mourgosd4772822023-04-05 18:35:03210
Matthew Mourgos1f286322023-04-20 18:16:50211 // Create and animate in the quick app button from behind the home button.
212 void AnimateQuickAppButtonIn();
213
214 // Animate out the quick app button, deleting the quick app button when
215 // completed.
216 void AnimateQuickAppButtonOut();
217
218 // Callback for the quick app button slide out animation.
219 void OnQuickAppButtonSlideOutDone();
220
221 // Returns a transform which will translate the child of the
222 // `expandable_container` to be placed behind the home button.
223 gfx::Transform GetTransformForContainerChildBehindHomeButton();
224
225 // Returns a clip rect which will clip the `expandable_container` to the
226 // bounds of the home button.
227 gfx::Rect GetExpandableContainerClipRectToHomeButton();
228
Matthew Mourgosd4772822023-04-05 18:35:03229 base::ScopedObservation<QuickAppAccessModel, QuickAppAccessModel::Observer>
230 quick_app_model_observation_{this};
231
232 base::ScopedObservation<Shell, ShellObserver> shell_observation_{this};
233
234 base::ScopedObservation<AppListModelProvider, AppListModelProvider::Observer>
235 app_list_model_observation_{this};
236
Arthur Sonzogni834e018f2023-04-22 10:20:02237 const raw_ptr<Shelf, ExperimentalAsh> shelf_;
Wen-Chien Wang3667df202021-09-30 20:08:23238
Toni Barzic705b8aa2023-05-24 01:50:10239 // The view that paints the home button content. In its own view to ensure
240 // the background is stacked above `expandable_container_`.
241 raw_ptr<ButtonImageView, ExperimentalAsh> button_image_view_ = nullptr;
242
243 // The container of `nudge_label_` or `quick_app_button_`. This is also
244 // responsible for painting the background of the contents. This container can
245 // expand visually by animation.
246 raw_ptr<views::View, ExperimentalAsh> expandable_container_ = nullptr;
247
248 // The app button which is shown next to the home button. Only shown when
249 // set by SetQuickApp().
250 raw_ptr<views::ImageButton, ExperimentalAsh> quick_app_button_ = nullptr;
251
Michael Giuffrida01adeb072019-03-21 22:05:30252 // The controller used to determine the button's behavior.
Manu Cornetf953d492019-06-27 05:56:51253 HomeButtonController controller_;
Wen-Chien Wang3667df202021-09-30 20:08:23254
255 // The ripple layer in the launcher nudge animation. Only exists during the
256 // nudge animation.
Wen-Chien Wang51a5d604d2022-05-10 18:18:01257 ui::LayerOwner nudge_ripple_layer_;
258
259 // The label view and for launcher nudge animation.
Arthur Sonzogni834e018f2023-04-22 10:20:02260 raw_ptr<views::Label, ExperimentalAsh> nudge_label_ = nullptr;
Wen-Chien Wang51a5d604d2022-05-10 18:18:01261
Wen-Chien Wang51a5d604d2022-05-10 18:18:01262 // The timer that counts down to hide the nudge_label_ from showing state.
263 base::OneShotTimer label_nudge_timer_;
Wen-Chien Wang3667df202021-09-30 20:08:23264
265 // The delegate used by |nudge_ripple_layer_|. Only exists during the
266 // nudge animation.
267 std::unique_ptr<views::CircleLayerDelegate> ripple_layer_delegate_;
268
269 std::unique_ptr<ScopedNoClipRect> scoped_no_clip_rect_;
270
Wen-Chien Wang617c38a2021-10-07 10:23:50271 base::ObserverList<NudgeAnimationObserver> observers_;
272
Wen-Chien Wang3667df202021-09-30 20:08:23273 base::WeakPtrFactory<HomeButton> weak_ptr_factory_{this};
James Cookb0bf8e82017-04-09 17:01:44274};
275
276} // namespace ash
277
Manu Cornetf953d492019-06-27 05:56:51278#endif // ASH_SHELF_HOME_BUTTON_H_