[go: nahoru, domu]

blob: 789ebf1c7353287f6dfc6d1797782f8965f47ec4 [file] [log] [blame]
James Cookb0bf8e82017-04-09 17:01:441// Copyright 2014 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
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"
James Cookb0bf8e82017-04-09 17:01:4411#include "ash/ash_export.h"
Manu Cornetf953d492019-06-27 05:56:5112#include "ash/shelf/home_button_controller.h"
Manu Cornet86aef982019-07-18 21:31:1013#include "ash/shelf/shelf_button_delegate.h"
Manu Cornet40f908122018-11-08 23:15:2514#include "ash/shelf/shelf_control_button.h"
Yuki Awano2090b462021-06-08 01:33:0715#include "ui/gfx/geometry/rect.h"
Ahmed Mehfooz6509620f2019-05-21 20:35:1216#include "ui/views/view_targeter_delegate.h"
xiaohuicfb5c54fc2017-06-28 17:32:0217
Wen-Chien Wang3667df202021-09-30 20:08:2318namespace views {
Wen-Chien Wang51a5d604d2022-05-10 18:18:0119class AnimationBuilder;
Wen-Chien Wang3667df202021-09-30 20:08:2320class CircleLayerDelegate;
Wen-Chien Wang51a5d604d2022-05-10 18:18:0121class Label;
Wen-Chien Wang3667df202021-09-30 20:08:2322} // namespace views
23
Wen-Chien Wang617c38a2021-10-07 10:23:5024namespace ui {
Wen-Chien Wang51a5d604d2022-05-10 18:18:0125class LayerOwner;
Wen-Chien Wang617c38a2021-10-07 10:23:5026}
27
James Cookb0bf8e82017-04-09 17:01:4428namespace ash {
Sammie Quon9b911f2f2017-12-15 02:53:1529
Wen-Chien Wang51a5d604d2022-05-10 18:18:0130class Shelf;
Alex Newcomer030e7062019-07-02 00:03:5731class ShelfButtonDelegate;
Yuki Awano2090b462021-06-08 01:33:0732class ShelfNavigationWidget;
James Cookb0bf8e82017-04-09 17:01:4433
Michael Giuffrida01adeb072019-03-21 22:05:3034// Button used for the AppList icon on the shelf. It opens the app list (in
35// clamshell mode) or home screen (in tablet mode). Because the clamshell-mode
36// app list appears like a dismissable overlay, the button is highlighted while
37// the app list is open in clamshell mode.
38//
39// If Assistant is enabled, the button is filled in; long-pressing it will
40// launch Assistant.
Manu Cornetf953d492019-06-27 05:56:5141class ASH_EXPORT HomeButton : public ShelfControlButton,
Manu Cornet86aef982019-07-18 21:31:1042 public ShelfButtonDelegate,
Manu Cornetf953d492019-06-27 05:56:5143 public views::ViewTargeterDelegate {
James Cookb0bf8e82017-04-09 17:01:4444 public:
Yuki Awano2090b462021-06-08 01:33:0745 class ScopedNoClipRect {
46 public:
47 explicit ScopedNoClipRect(ShelfNavigationWidget* shelf_navigation_widget);
48 ScopedNoClipRect(const ScopedNoClipRect&) = delete;
49 ScopedNoClipRect& operator=(const ScopedNoClipRect&) = delete;
50 ~ScopedNoClipRect();
51
52 private:
53 ShelfNavigationWidget* const shelf_navigation_widget_;
54 const gfx::Rect clip_rect_;
55 };
56
Wen-Chien Wang617c38a2021-10-07 10:23:5057 // An observer that can be used to track the nudge animation state. Currently
58 // used in testing.
59 class NudgeAnimationObserver : public base::CheckedObserver {
60 public:
61 NudgeAnimationObserver() = default;
62 NudgeAnimationObserver(const NudgeAnimationObserver&) = delete;
63 NudgeAnimationObserver& operator=(const NudgeAnimationObserver&) = delete;
64 ~NudgeAnimationObserver() override = default;
65
66 // Called when the nudge animation is started/ended.
67 virtual void NudgeAnimationStarted(HomeButton* home_button) = 0;
68 virtual void NudgeAnimationEnded(HomeButton* home_button) = 0;
Wen-Chien Wang51a5d604d2022-05-10 18:18:0169
70 // Called when the nudge label is animated to fully shown.
71 virtual void NudgeLabelShown(HomeButton* home_button) = 0;
Wen-Chien Wang617c38a2021-10-07 10:23:5072 };
73
Manu Cornetc0066332019-02-20 19:26:0374 static const char kViewClassName[];
75
Manu Cornet86aef982019-07-18 21:31:1076 explicit HomeButton(Shelf* shelf);
Peter Boström5fc1d312021-09-24 02:32:1577
78 HomeButton(const HomeButton&) = delete;
79 HomeButton& operator=(const HomeButton&) = delete;
80
Manu Cornetf953d492019-06-27 05:56:5181 ~HomeButton() override;
James Cookb0bf8e82017-04-09 17:01:4482
Wen-Chien Wang51a5d604d2022-05-10 18:18:0183 // views::View:
84 gfx::Size CalculatePreferredSize() const override;
85 void Layout() override;
86
Manu Cornete363aec2019-01-13 13:07:0787 // views::Button:
James Cookb0bf8e82017-04-09 17:01:4488 void OnGestureEvent(ui::GestureEvent* event) override;
Manu Cornetc0066332019-02-20 19:26:0389 const char* GetClassName() const override;
Jan Wilken Dörrie85285b02021-03-11 23:38:4790 std::u16string GetTooltipText(const gfx::Point& p) const override;
James Cookb0bf8e82017-04-09 17:01:4491
Manu Cornet86aef982019-07-18 21:31:1092 // ShelfButtonDelegate:
93 void OnShelfButtonAboutToRequestFocusFromTabTraversal(ShelfButton* button,
94 bool reverse) override;
95 void ButtonPressed(views::Button* sender,
96 const ui::Event& event,
97 views::InkDrop* ink_drop) override;
Manu Cornet86aef982019-07-18 21:31:1098
Michael Giuffrida01adeb072019-03-21 22:05:3099 // Called when the availability of a long-press gesture may have changed, e.g.
100 // when Assistant becomes enabled.
Yue Libcdb6aa2019-10-02 17:50:57101 void OnAssistantAvailabilityChanged();
Michael Giuffrida01adeb072019-03-21 22:05:30102
103 // True if the app list is shown for the display containing this button.
104 bool IsShowingAppList() const;
105
Toni Barzic71ebb6fd2020-05-29 17:16:53106 // Called when a locale change is detected. Updates the button tooltip and
107 // accessible name.
108 void HandleLocaleChange();
Manu Cornet1ed1f6b2019-06-14 17:25:53109
Alex Newcomer030e7062019-07-02 00:03:57110 // Returns the display which contains this view.
111 int64_t GetDisplayId() const;
112
Yuki Awano2090b462021-06-08 01:33:07113 // Clip rect of this view's widget will be removed during the life time of the
114 // returned ScopedNoClipRect.
Daniel Chengb28e2af62022-01-13 23:56:21115 [[nodiscard]] std::unique_ptr<ScopedNoClipRect> CreateScopedNoClipRect();
Yuki Awano2090b462021-06-08 01:33:07116
Wen-Chien Wang51a5d604d2022-05-10 18:18:01117 // Checks if the `nudge_label_` can be shown for the launcher nudge.
118 // NOTE: This must be called after `CreateNudgeLabel()`, where the
119 // `nudge_label_` is created. This is because whether the nudge can be shown
120 // depends on nudge_label_'s preferred size.
121 bool CanShowNudgeLabel() const;
122
Wen-Chien Wang617c38a2021-10-07 10:23:50123 // Starts the launcher nudge animation.
124 void StartNudgeAnimation();
125
126 void AddNudgeAnimationObserverForTest(NudgeAnimationObserver* observer);
127 void RemoveNudgeAnimationObserverForTest(NudgeAnimationObserver* observer);
128
Wen-Chien Wang51a5d604d2022-05-10 18:18:01129 views::View* label_container_for_test() const { return label_container_; }
130
James Cookb0bf8e82017-04-09 17:01:44131 protected:
Manu Cornete363aec2019-01-13 13:07:07132 // views::Button:
Evan Stadebad2bc62017-05-30 21:53:24133 void PaintButtonContents(gfx::Canvas* canvas) override;
Yulun Wu8924cebb2021-01-12 20:23:28134 void OnThemeChanged() override;
James Cookb0bf8e82017-04-09 17:01:44135
136 private:
Wen-Chien Wang51a5d604d2022-05-10 18:18:01137 // Creates `nudge_label_` for launcher nudge.
138 void CreateNudgeLabel();
139
140 // Animation functions for launcher nudge.
141 void AnimateNudgeRipple(views::AnimationBuilder& builder);
142 void AnimateNudgeBounce(views::AnimationBuilder& builder);
143 void AnimateNudgeLabelSlideIn(views::AnimationBuilder& builder);
144 void AnimateNudgeLabelSlideOut();
145 void AnimateNudgeLabelFadeOut();
146
147 // Callbacks for the nudge animation.
148 void OnNudgeAnimationStarted();
149 void OnNudgeAnimationEnded();
150 void OnLabelSlideInAnimationEnded();
151 void OnLabelFadeOutAnimationEnded();
152
153 // Removes the nudge label from the view hierarchy.
154 void RemoveNudgeLabel();
155
Ahmed Mehfooz6509620f2019-05-21 20:35:12156 // views::ViewTargeterDelegate:
157 bool DoesIntersectRect(const views::View* target,
158 const gfx::Rect& rect) const override;
159
Wen-Chien Wang51a5d604d2022-05-10 18:18:01160 Shelf* const shelf_;
Wen-Chien Wang3667df202021-09-30 20:08:23161
Michael Giuffrida01adeb072019-03-21 22:05:30162 // The controller used to determine the button's behavior.
Manu Cornetf953d492019-06-27 05:56:51163 HomeButtonController controller_;
Wen-Chien Wang3667df202021-09-30 20:08:23164
165 // The ripple layer in the launcher nudge animation. Only exists during the
166 // nudge animation.
Wen-Chien Wang51a5d604d2022-05-10 18:18:01167 ui::LayerOwner nudge_ripple_layer_;
168
169 // The label view and for launcher nudge animation.
170 views::Label* nudge_label_ = nullptr;
171
172 // The container of `nudge_label_`. This is also responsible for painting the
173 // background of the label.
174 views::View* label_container_ = nullptr;
175
176 // The timer that counts down to hide the nudge_label_ from showing state.
177 base::OneShotTimer label_nudge_timer_;
Wen-Chien Wang3667df202021-09-30 20:08:23178
179 // The delegate used by |nudge_ripple_layer_|. Only exists during the
180 // nudge animation.
181 std::unique_ptr<views::CircleLayerDelegate> ripple_layer_delegate_;
182
183 std::unique_ptr<ScopedNoClipRect> scoped_no_clip_rect_;
184
Wen-Chien Wang617c38a2021-10-07 10:23:50185 base::ObserverList<NudgeAnimationObserver> observers_;
186
Wen-Chien Wang3667df202021-09-30 20:08:23187 base::WeakPtrFactory<HomeButton> weak_ptr_factory_{this};
James Cookb0bf8e82017-04-09 17:01:44188};
189
190} // namespace ash
191
Manu Cornetf953d492019-06-27 05:56:51192#endif // ASH_SHELF_HOME_BUTTON_H_