[go: nahoru, domu]

blob: 403905029f913397514885167ba408c30c66a108 [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"
David Yeung1f2e3e22023-11-15 17:29:4722#include "ui/base/metadata/metadata_header_macros.h"
Yuki Awano2090b462021-06-08 01:33:0723#include "ui/gfx/geometry/rect.h"
Matthew Mourgos1f286322023-04-20 18:16:5024#include "ui/gfx/geometry/transform.h"
Ahmed Mehfooz6509620f2019-05-21 20:35:1225#include "ui/views/view_targeter_delegate.h"
xiaohuicfb5c54fc2017-06-28 17:32:0226
Wen-Chien Wang3667df202021-09-30 20:08:2327namespace views {
Wen-Chien Wang51a5d604d2022-05-10 18:18:0128class AnimationBuilder;
Wen-Chien Wang3667df202021-09-30 20:08:2329class CircleLayerDelegate;
Matthew Mourgosd4772822023-04-05 18:35:0330class ImageButton;
Wen-Chien Wang51a5d604d2022-05-10 18:18:0131class Label;
Wen-Chien Wang3667df202021-09-30 20:08:2332} // namespace views
33
Wen-Chien Wang617c38a2021-10-07 10:23:5034namespace ui {
Wen-Chien Wang51a5d604d2022-05-10 18:18:0135class LayerOwner;
Wen-Chien Wang617c38a2021-10-07 10:23:5036}
37
James Cookb0bf8e82017-04-09 17:01:4438namespace ash {
Sammie Quon9b911f2f2017-12-15 02:53:1539
Wen-Chien Wang51a5d604d2022-05-10 18:18:0140class Shelf;
Alex Newcomer030e7062019-07-02 00:03:5741class ShelfButtonDelegate;
Yuki Awano2090b462021-06-08 01:33:0742class ShelfNavigationWidget;
Matthew Mourgosd4772822023-04-05 18:35:0343class Shell;
James Cookb0bf8e82017-04-09 17:01:4444
Michael Giuffrida01adeb072019-03-21 22:05:3045// Button used for the AppList icon on the shelf. It opens the app list (in
46// clamshell mode) or home screen (in tablet mode). Because the clamshell-mode
47// app list appears like a dismissable overlay, the button is highlighted while
48// the app list is open in clamshell mode.
49//
50// If Assistant is enabled, the button is filled in; long-pressing it will
51// launch Assistant.
Manu Cornetf953d492019-06-27 05:56:5152class ASH_EXPORT HomeButton : public ShelfControlButton,
Manu Cornet86aef982019-07-18 21:31:1053 public ShelfButtonDelegate,
Matthew Mourgosd4772822023-04-05 18:35:0354 public views::ViewTargeterDelegate,
55 public ShellObserver,
Toni Barzic705b8aa2023-05-24 01:50:1056 public ShelfConfig::Observer,
Matthew Mourgosd4772822023-04-05 18:35:0357 public AppListModelProvider::Observer,
58 public QuickAppAccessModel::Observer {
David Yeung1f2e3e22023-11-15 17:29:4759 METADATA_HEADER(HomeButton, ShelfControlButton)
60
James Cookb0bf8e82017-04-09 17:01:4461 public:
Yuki Awano2090b462021-06-08 01:33:0762 class ScopedNoClipRect {
63 public:
64 explicit ScopedNoClipRect(ShelfNavigationWidget* shelf_navigation_widget);
65 ScopedNoClipRect(const ScopedNoClipRect&) = delete;
66 ScopedNoClipRect& operator=(const ScopedNoClipRect&) = delete;
67 ~ScopedNoClipRect();
68
69 private:
Bartek Nowierskideb75842023-12-27 02:32:4270 const raw_ptr<ShelfNavigationWidget> shelf_navigation_widget_;
Yuki Awano2090b462021-06-08 01:33:0771 const gfx::Rect clip_rect_;
72 };
73
Wen-Chien Wang617c38a2021-10-07 10:23:5074 // An observer that can be used to track the nudge animation state. Currently
75 // used in testing.
76 class NudgeAnimationObserver : public base::CheckedObserver {
77 public:
78 NudgeAnimationObserver() = default;
79 NudgeAnimationObserver(const NudgeAnimationObserver&) = delete;
80 NudgeAnimationObserver& operator=(const NudgeAnimationObserver&) = delete;
81 ~NudgeAnimationObserver() override = default;
82
83 // Called when the nudge animation is started/ended.
84 virtual void NudgeAnimationStarted(HomeButton* home_button) = 0;
85 virtual void NudgeAnimationEnded(HomeButton* home_button) = 0;
Wen-Chien Wang51a5d604d2022-05-10 18:18:0186
87 // Called when the nudge label is animated to fully shown.
88 virtual void NudgeLabelShown(HomeButton* home_button) = 0;
Wen-Chien Wang617c38a2021-10-07 10:23:5089 };
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;
Peter Kasting1e3a1e992024-02-01 18:10:41100 void Layout(PassKey) override;
Wen-Chien Wang51a5d604d2022-05-10 18:18:01101
Manu Cornete363aec2019-01-13 13:07:07102 // views::Button:
James Cookb0bf8e82017-04-09 17:01:44103 void OnGestureEvent(ui::GestureEvent* event) override;
Jan Wilken Dörrie85285b02021-03-11 23:38:47104 std::u16string GetTooltipText(const gfx::Point& p) const override;
James Cookb0bf8e82017-04-09 17:01:44105
Manu Cornet86aef982019-07-18 21:31:10106 // ShelfButtonDelegate:
107 void OnShelfButtonAboutToRequestFocusFromTabTraversal(ShelfButton* button,
108 bool reverse) override;
109 void ButtonPressed(views::Button* sender,
110 const ui::Event& event,
111 views::InkDrop* ink_drop) override;
Manu Cornet86aef982019-07-18 21:31:10112
Toni Barzic705b8aa2023-05-24 01:50:10113 // ShelfConfig::Observer:
114 void OnShelfConfigUpdated() override;
115
Michael Giuffrida01adeb072019-03-21 22:05:30116 // Called when the availability of a long-press gesture may have changed, e.g.
117 // when Assistant becomes enabled.
Yue Libcdb6aa2019-10-02 17:50:57118 void OnAssistantAvailabilityChanged();
Michael Giuffrida01adeb072019-03-21 22:05:30119
120 // True if the app list is shown for the display containing this button.
121 bool IsShowingAppList() const;
122
Toni Barzic71ebb6fd2020-05-29 17:16:53123 // Called when a locale change is detected. Updates the button tooltip and
124 // accessible name.
125 void HandleLocaleChange();
Manu Cornet1ed1f6b2019-06-14 17:25:53126
Alex Newcomer030e7062019-07-02 00:03:57127 // Returns the display which contains this view.
128 int64_t GetDisplayId() const;
129
Yuki Awano2090b462021-06-08 01:33:07130 // Clip rect of this view's widget will be removed during the life time of the
131 // returned ScopedNoClipRect.
Daniel Chengb28e2af62022-01-13 23:56:21132 [[nodiscard]] std::unique_ptr<ScopedNoClipRect> CreateScopedNoClipRect();
Yuki Awano2090b462021-06-08 01:33:07133
Wen-Chien Wang51a5d604d2022-05-10 18:18:01134 // Checks if the `nudge_label_` can be shown for the launcher nudge.
135 // NOTE: This must be called after `CreateNudgeLabel()`, where the
136 // `nudge_label_` is created. This is because whether the nudge can be shown
137 // depends on nudge_label_'s preferred size.
138 bool CanShowNudgeLabel() const;
139
Wen-Chien Wang617c38a2021-10-07 10:23:50140 // Starts the launcher nudge animation.
141 void StartNudgeAnimation();
142
Toni Barzic2e5547382023-05-26 21:53:24143 // Sets the button's "toggled" state - the button is toggled when the bubble
144 // launcher is shown.
145 void SetToggled(bool toggled);
146
Wen-Chien Wang617c38a2021-10-07 10:23:50147 void AddNudgeAnimationObserverForTest(NudgeAnimationObserver* observer);
148 void RemoveNudgeAnimationObserverForTest(NudgeAnimationObserver* observer);
149
Matthew Mourgosd4772822023-04-05 18:35:03150 views::View* expandable_container_for_test() const {
151 return expandable_container_;
152 }
153
154 views::Label* nudge_label_for_test() const { return nudge_label_; }
155
156 views::ImageButton* quick_app_button_for_test() const {
157 return quick_app_button_;
158 }
Wen-Chien Wang51a5d604d2022-05-10 18:18:01159
James Cookb0bf8e82017-04-09 17:01:44160 protected:
Manu Cornete363aec2019-01-13 13:07:07161 // views::Button:
Yulun Wu8924cebb2021-01-12 20:23:28162 void OnThemeChanged() override;
James Cookb0bf8e82017-04-09 17:01:44163
164 private:
Toni Barzic705b8aa2023-05-24 01:50:10165 class ButtonImageView;
166
Wen-Chien Wang51a5d604d2022-05-10 18:18:01167 // Creates `nudge_label_` for launcher nudge.
168 void CreateNudgeLabel();
169
Matthew Mourgosd4772822023-04-05 18:35:03170 // Creates the `expandable_container_` which holds either the `nudge_label_`
171 // or the `quick_app_button_`.
172 void CreateExpandableContainer();
173
174 // Creates the `quick_app_button_` to be shown next to the home button.
175 void CreateQuickAppButton();
176
177 // Called when the quick app button is pressed.
178 void QuickAppButtonPressed();
179
Wen-Chien Wang51a5d604d2022-05-10 18:18:01180 // Animation functions for launcher nudge.
181 void AnimateNudgeRipple(views::AnimationBuilder& builder);
182 void AnimateNudgeBounce(views::AnimationBuilder& builder);
183 void AnimateNudgeLabelSlideIn(views::AnimationBuilder& builder);
184 void AnimateNudgeLabelSlideOut();
185 void AnimateNudgeLabelFadeOut();
186
187 // Callbacks for the nudge animation.
188 void OnNudgeAnimationStarted();
189 void OnNudgeAnimationEnded();
190 void OnLabelSlideInAnimationEnded();
191 void OnLabelFadeOutAnimationEnded();
192
193 // Removes the nudge label from the view hierarchy.
194 void RemoveNudgeLabel();
195
Matthew Mourgosbe9f28272023-04-13 17:45:45196 // Removes the quick app button from the view hierarchy.
197 void RemoveQuickAppButton();
198
Ahmed Mehfooz6509620f2019-05-21 20:35:12199 // views::ViewTargeterDelegate:
200 bool DoesIntersectRect(const views::View* target,
201 const gfx::Rect& rect) const override;
202
Matthew Mourgosd4772822023-04-05 18:35:03203 // ShellObserver:
204 void OnShellDestroying() override;
205
206 // AppListModelProvider::Observer:
207 void OnActiveAppListModelsChanged(AppListModel* model,
208 SearchModel* search_model) override;
209
210 // QuickAppAccessModel::Observer:
Matthew Mourgosbe9f28272023-04-13 17:45:45211 void OnQuickAppShouldShowChanged(bool quick_app_shown) override;
212 void OnQuickAppIconChanged() override;
Matthew Mourgosd4772822023-04-05 18:35:03213
Matthew Mourgos1f286322023-04-20 18:16:50214 // Create and animate in the quick app button from behind the home button.
215 void AnimateQuickAppButtonIn();
216
217 // Animate out the quick app button, deleting the quick app button when
218 // completed.
219 void AnimateQuickAppButtonOut();
220
221 // Callback for the quick app button slide out animation.
222 void OnQuickAppButtonSlideOutDone();
223
224 // Returns a transform which will translate the child of the
225 // `expandable_container` to be placed behind the home button.
226 gfx::Transform GetTransformForContainerChildBehindHomeButton();
227
228 // Returns a clip rect which will clip the `expandable_container` to the
229 // bounds of the home button.
230 gfx::Rect GetExpandableContainerClipRectToHomeButton();
231
Toni Barzic2e5547382023-05-26 21:53:24232 const bool jelly_enabled_;
233
Matthew Mourgosd4772822023-04-05 18:35:03234 base::ScopedObservation<QuickAppAccessModel, QuickAppAccessModel::Observer>
235 quick_app_model_observation_{this};
236
237 base::ScopedObservation<Shell, ShellObserver> shell_observation_{this};
238
239 base::ScopedObservation<AppListModelProvider, AppListModelProvider::Observer>
240 app_list_model_observation_{this};
241
Bartek Nowierskideb75842023-12-27 02:32:42242 const raw_ptr<Shelf> shelf_;
Wen-Chien Wang3667df202021-09-30 20:08:23243
Toni Barzic705b8aa2023-05-24 01:50:10244 // The view that paints the home button content. In its own view to ensure
245 // the background is stacked above `expandable_container_`.
Bartek Nowierskideb75842023-12-27 02:32:42246 raw_ptr<ButtonImageView> button_image_view_ = nullptr;
Toni Barzic705b8aa2023-05-24 01:50:10247
248 // The container of `nudge_label_` or `quick_app_button_`. This is also
249 // responsible for painting the background of the contents. This container can
250 // expand visually by animation.
Jan Keitelae0e7e02024-02-07 22:09:06251 raw_ptr<views::View> expandable_container_ = nullptr;
Toni Barzic705b8aa2023-05-24 01:50:10252
253 // The app button which is shown next to the home button. Only shown when
254 // set by SetQuickApp().
Jan Keitelae0e7e02024-02-07 22:09:06255 raw_ptr<views::ImageButton> quick_app_button_ = nullptr;
Toni Barzic705b8aa2023-05-24 01:50:10256
Michael Giuffrida01adeb072019-03-21 22:05:30257 // The controller used to determine the button's behavior.
Manu Cornetf953d492019-06-27 05:56:51258 HomeButtonController controller_;
Wen-Chien Wang3667df202021-09-30 20:08:23259
Jan Keitelae0e7e02024-02-07 22:09:06260 // The delegate used by |nudge_ripple_layer_|. Only exists during the
261 // nudge animation.
262 std::unique_ptr<views::CircleLayerDelegate> ripple_layer_delegate_;
263
Wen-Chien Wang3667df202021-09-30 20:08:23264 // The ripple layer in the launcher nudge animation. Only exists during the
265 // nudge animation.
Wen-Chien Wang51a5d604d2022-05-10 18:18:01266 ui::LayerOwner nudge_ripple_layer_;
267
268 // The label view and for launcher nudge animation.
Jan Keitelae0e7e02024-02-07 22:09:06269 raw_ptr<views::Label> nudge_label_ = nullptr;
Wen-Chien Wang51a5d604d2022-05-10 18:18:01270
Wen-Chien Wang51a5d604d2022-05-10 18:18:01271 // The timer that counts down to hide the nudge_label_ from showing state.
272 base::OneShotTimer label_nudge_timer_;
Wen-Chien Wang3667df202021-09-30 20:08:23273
Wen-Chien Wang3667df202021-09-30 20:08:23274 std::unique_ptr<ScopedNoClipRect> scoped_no_clip_rect_;
275
Wen-Chien Wang617c38a2021-10-07 10:23:50276 base::ObserverList<NudgeAnimationObserver> observers_;
277
Wen-Chien Wang3667df202021-09-30 20:08:23278 base::WeakPtrFactory<HomeButton> weak_ptr_factory_{this};
James Cookb0bf8e82017-04-09 17:01:44279};
280
281} // namespace ash
282
Manu Cornetf953d492019-06-27 05:56:51283#endif // ASH_SHELF_HOME_BUTTON_H_