[go: nahoru, domu]

blob: ec39393b8943298ecd8ecc674b9bf1d28be7b1a9 [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
Toni Barzic2e5547382023-05-26 21:53:24144 // Sets the button's "toggled" state - the button is toggled when the bubble
145 // launcher is shown.
146 void SetToggled(bool toggled);
147
Wen-Chien Wang617c38a2021-10-07 10:23:50148 void AddNudgeAnimationObserverForTest(NudgeAnimationObserver* observer);
149 void RemoveNudgeAnimationObserverForTest(NudgeAnimationObserver* observer);
150
Matthew Mourgosd4772822023-04-05 18:35:03151 views::View* expandable_container_for_test() const {
152 return expandable_container_;
153 }
154
155 views::Label* nudge_label_for_test() const { return nudge_label_; }
156
157 views::ImageButton* quick_app_button_for_test() const {
158 return quick_app_button_;
159 }
Wen-Chien Wang51a5d604d2022-05-10 18:18:01160
James Cookb0bf8e82017-04-09 17:01:44161 protected:
Manu Cornete363aec2019-01-13 13:07:07162 // views::Button:
Yulun Wu8924cebb2021-01-12 20:23:28163 void OnThemeChanged() override;
James Cookb0bf8e82017-04-09 17:01:44164
165 private:
Toni Barzic705b8aa2023-05-24 01:50:10166 class ButtonImageView;
167
Wen-Chien Wang51a5d604d2022-05-10 18:18:01168 // Creates `nudge_label_` for launcher nudge.
169 void CreateNudgeLabel();
170
Matthew Mourgosd4772822023-04-05 18:35:03171 // Creates the `expandable_container_` which holds either the `nudge_label_`
172 // or the `quick_app_button_`.
173 void CreateExpandableContainer();
174
175 // Creates the `quick_app_button_` to be shown next to the home button.
176 void CreateQuickAppButton();
177
178 // Called when the quick app button is pressed.
179 void QuickAppButtonPressed();
180
Wen-Chien Wang51a5d604d2022-05-10 18:18:01181 // Animation functions for launcher nudge.
182 void AnimateNudgeRipple(views::AnimationBuilder& builder);
183 void AnimateNudgeBounce(views::AnimationBuilder& builder);
184 void AnimateNudgeLabelSlideIn(views::AnimationBuilder& builder);
185 void AnimateNudgeLabelSlideOut();
186 void AnimateNudgeLabelFadeOut();
187
188 // Callbacks for the nudge animation.
189 void OnNudgeAnimationStarted();
190 void OnNudgeAnimationEnded();
191 void OnLabelSlideInAnimationEnded();
192 void OnLabelFadeOutAnimationEnded();
193
194 // Removes the nudge label from the view hierarchy.
195 void RemoveNudgeLabel();
196
Matthew Mourgosbe9f28272023-04-13 17:45:45197 // Removes the quick app button from the view hierarchy.
198 void RemoveQuickAppButton();
199
Ahmed Mehfooz6509620f2019-05-21 20:35:12200 // views::ViewTargeterDelegate:
201 bool DoesIntersectRect(const views::View* target,
202 const gfx::Rect& rect) const override;
203
Matthew Mourgosd4772822023-04-05 18:35:03204 // ShellObserver:
205 void OnShellDestroying() override;
206
207 // AppListModelProvider::Observer:
208 void OnActiveAppListModelsChanged(AppListModel* model,
209 SearchModel* search_model) override;
210
211 // QuickAppAccessModel::Observer:
Matthew Mourgosbe9f28272023-04-13 17:45:45212 void OnQuickAppShouldShowChanged(bool quick_app_shown) override;
213 void OnQuickAppIconChanged() override;
Matthew Mourgosd4772822023-04-05 18:35:03214
Matthew Mourgos1f286322023-04-20 18:16:50215 // Create and animate in the quick app button from behind the home button.
216 void AnimateQuickAppButtonIn();
217
218 // Animate out the quick app button, deleting the quick app button when
219 // completed.
220 void AnimateQuickAppButtonOut();
221
222 // Callback for the quick app button slide out animation.
223 void OnQuickAppButtonSlideOutDone();
224
225 // Returns a transform which will translate the child of the
226 // `expandable_container` to be placed behind the home button.
227 gfx::Transform GetTransformForContainerChildBehindHomeButton();
228
229 // Returns a clip rect which will clip the `expandable_container` to the
230 // bounds of the home button.
231 gfx::Rect GetExpandableContainerClipRectToHomeButton();
232
Toni Barzic2e5547382023-05-26 21:53:24233 const bool jelly_enabled_;
234
Matthew Mourgosd4772822023-04-05 18:35:03235 base::ScopedObservation<QuickAppAccessModel, QuickAppAccessModel::Observer>
236 quick_app_model_observation_{this};
237
238 base::ScopedObservation<Shell, ShellObserver> shell_observation_{this};
239
240 base::ScopedObservation<AppListModelProvider, AppListModelProvider::Observer>
241 app_list_model_observation_{this};
242
Arthur Sonzogni834e018f2023-04-22 10:20:02243 const raw_ptr<Shelf, ExperimentalAsh> shelf_;
Wen-Chien Wang3667df202021-09-30 20:08:23244
Toni Barzic705b8aa2023-05-24 01:50:10245 // The view that paints the home button content. In its own view to ensure
246 // the background is stacked above `expandable_container_`.
247 raw_ptr<ButtonImageView, ExperimentalAsh> button_image_view_ = nullptr;
248
249 // The container of `nudge_label_` or `quick_app_button_`. This is also
250 // responsible for painting the background of the contents. This container can
251 // expand visually by animation.
252 raw_ptr<views::View, ExperimentalAsh> expandable_container_ = nullptr;
253
254 // The app button which is shown next to the home button. Only shown when
255 // set by SetQuickApp().
256 raw_ptr<views::ImageButton, ExperimentalAsh> quick_app_button_ = nullptr;
257
Michael Giuffrida01adeb072019-03-21 22:05:30258 // The controller used to determine the button's behavior.
Manu Cornetf953d492019-06-27 05:56:51259 HomeButtonController controller_;
Wen-Chien Wang3667df202021-09-30 20:08:23260
261 // The ripple layer in the launcher nudge animation. Only exists during the
262 // nudge animation.
Wen-Chien Wang51a5d604d2022-05-10 18:18:01263 ui::LayerOwner nudge_ripple_layer_;
264
265 // The label view and for launcher nudge animation.
Arthur Sonzogni834e018f2023-04-22 10:20:02266 raw_ptr<views::Label, ExperimentalAsh> nudge_label_ = nullptr;
Wen-Chien Wang51a5d604d2022-05-10 18:18:01267
Wen-Chien Wang51a5d604d2022-05-10 18:18:01268 // The timer that counts down to hide the nudge_label_ from showing state.
269 base::OneShotTimer label_nudge_timer_;
Wen-Chien Wang3667df202021-09-30 20:08:23270
271 // The delegate used by |nudge_ripple_layer_|. Only exists during the
272 // nudge animation.
273 std::unique_ptr<views::CircleLayerDelegate> ripple_layer_delegate_;
274
275 std::unique_ptr<ScopedNoClipRect> scoped_no_clip_rect_;
276
Wen-Chien Wang617c38a2021-10-07 10:23:50277 base::ObserverList<NudgeAnimationObserver> observers_;
278
Wen-Chien Wang3667df202021-09-30 20:08:23279 base::WeakPtrFactory<HomeButton> weak_ptr_factory_{this};
James Cookb0bf8e82017-04-09 17:01:44280};
281
282} // namespace ash
283
Manu Cornetf953d492019-06-27 05:56:51284#endif // ASH_SHELF_HOME_BUTTON_H_