[go: nahoru, domu]

blob: 2d1c5fadc2154f80390b0d4fa03f9249131bde6c [file] [log] [blame]
James Cookb0bf8e82017-04-09 17:01:441// Copyright (c) 2012 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
5#include "ash/shelf/shelf_view.h"
6
7#include <algorithm>
8#include <memory>
Jan Wilken Dörried76713f2020-03-31 12:12:119#include <utility>
James Cookb0bf8e82017-04-09 17:01:4410
Matthew Mourgos175a5cc52019-04-18 16:33:0911#include "ash/app_list/app_list_controller_impl.h"
James Cookb0bf8e82017-04-09 17:01:4412#include "ash/drag_drop/drag_image_view.h"
Manu Cornetdcb572e2019-03-27 01:47:0813#include "ash/keyboard/keyboard_util.h"
Darren Shencb2508442019-07-03 21:48:2314#include "ash/keyboard/ui/keyboard_ui_controller.h"
Scott Violet41562d12017-06-26 15:15:4815#include "ash/metrics/user_metrics_recorder.h"
Steven Bennetts93d3e5b12018-05-05 01:14:5016#include "ash/public/cpp/ash_constants.h"
Ahmed Fakhry039882f2020-06-16 21:56:2117#include "ash/public/cpp/ash_features.h"
Xiyuan Xia64d1f0a82020-10-30 04:12:5218#include "ash/public/cpp/metrics_util.h"
msw109806d2017-06-02 20:11:5719#include "ash/public/cpp/shelf_model.h"
Manu Cornetdcb572e2019-03-27 01:47:0820#include "ash/public/cpp/shelf_types.h"
Manu Cornet724e11382018-05-10 22:19:4421#include "ash/public/cpp/window_properties.h"
minchf6b2be32017-05-11 20:54:0122#include "ash/screen_util.h"
Jit Yao Yap9425a1c52020-09-03 05:19:5923#include "ash/session/session_controller_impl.h"
Avery Musbach22d527b2020-04-24 21:00:4124#include "ash/shelf/hotseat_widget.h"
25#include "ash/shelf/scrollable_shelf_view.h"
James Cook840177e2017-05-25 02:20:0126#include "ash/shelf/shelf.h"
Manu Cornete363aec2019-01-13 13:07:0727#include "ash/shelf/shelf_app_button.h"
James Cookb0bf8e82017-04-09 17:01:4428#include "ash/shelf/shelf_application_menu_model.h"
29#include "ash/shelf/shelf_button.h"
Michael Wasserman47bf1782017-08-18 23:17:1030#include "ash/shelf/shelf_context_menu_model.h"
Sammie Quona87fc322017-11-10 17:43:3731#include "ash/shelf/shelf_controller.h"
Alex Newcomerc8357182019-06-26 19:31:0632#include "ash/shelf/shelf_focus_cycler.h"
Alex Newcomer43e6ccefe2019-10-09 21:43:5933#include "ash/shelf/shelf_layout_manager.h"
Alex Newcomer46af4c92018-05-29 22:26:1434#include "ash/shelf/shelf_menu_model_adapter.h"
Andrew Xubbdc1422019-09-12 01:52:4335#include "ash/shelf/shelf_tooltip_manager.h"
James Cookb0bf8e82017-04-09 17:01:4436#include "ash/shelf/shelf_widget.h"
James Cookb0bf8e82017-04-09 17:01:4437#include "ash/shell.h"
38#include "ash/shell_delegate.h"
39#include "ash/strings/grit/ash_strings.h"
Yulun Wub83992692020-10-29 19:08:5240#include "ash/style/ash_color_provider.h"
yilkale9c69e72019-07-12 18:59:2341#include "ash/system/status_area_widget.h"
Ahmed Fakhry1b6ea0b2020-07-09 01:45:2442#include "ash/wm/desks/desks_util.h"
Manu Cornet724e11382018-05-10 22:19:4443#include "ash/wm/mru_window_tracker.h"
Sammie Quonfb3feae2017-07-25 20:13:4244#include "ash/wm/tablet_mode/tablet_mode_controller.h"
James Cook00e65e92019-07-25 03:19:0845#include "ash/wm/window_util.h"
James Cookb0bf8e82017-04-09 17:01:4446#include "base/auto_reset.h"
Sebastien Marchand6d0558fd2019-01-25 16:49:3747#include "base/bind.h"
danakjdb9ae7942020-11-11 16:01:3548#include "base/callback_helpers.h"
Manu Cornet2f804d92019-01-18 03:59:4749#include "base/containers/adapters.h"
Jan Wilken Dörrieb5a41c32020-12-09 18:55:4750#include "base/containers/contains.h"
Andrew Xu40ffcdb2020-04-02 16:20:4451#include "base/metrics/histogram_functions.h"
James Cookb0bf8e82017-04-09 17:01:4452#include "base/metrics/histogram_macros.h"
Peter Kasting8b80a9b2019-09-09 20:27:2353#include "base/numerics/ranges.h"
Alex Newcomer46af4c92018-05-29 22:26:1454#include "base/strings/utf_string_conversions.h"
Avery Musbach5a8e6cf2019-01-15 00:46:5755#include "base/timer/timer.h"
Jit Yao Yap9425a1c52020-09-03 05:19:5956#include "components/account_id/account_id.h"
57#include "components/services/app_service/public/cpp/app_registry_cache_wrapper.h"
58#include "components/services/app_service/public/mojom/types.mojom.h"
James Cookb0bf8e82017-04-09 17:01:4459#include "ui/accessibility/ax_node_data.h"
Henrique Ferreiro1748fd12020-08-04 12:51:4660#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
James Cookb0bf8e82017-04-09 17:01:4461#include "ui/base/l10n/l10n_util.h"
62#include "ui/base/models/simple_menu_model.h"
Alex Newcomer509bf672018-02-08 00:03:3063#include "ui/base/ui_base_features.h"
Xiyuan Xia64d1f0a82020-10-30 04:12:5264#include "ui/compositor/animation_throughput_reporter.h"
James Cookb0bf8e82017-04-09 17:01:4465#include "ui/compositor/layer.h"
Andrew Xuec6ba942020-03-23 18:17:5966#include "ui/compositor/layer_animation_observer.h"
James Cookb0bf8e82017-04-09 17:01:4467#include "ui/compositor/layer_animator.h"
68#include "ui/compositor/scoped_animation_duration_scale_mode.h"
Joel Hockey784a8322020-05-20 21:19:2569#include "ui/display/scoped_display_for_new_windows.h"
James Cookb0bf8e82017-04-09 17:01:4470#include "ui/events/event_utils.h"
71#include "ui/gfx/canvas.h"
72#include "ui/gfx/geometry/point.h"
Manu Cornetf02f084d2019-02-06 02:50:3873#include "ui/strings/grit/ui_strings.h"
James Cookb0bf8e82017-04-09 17:01:4474#include "ui/views/animation/bounds_animator.h"
Peter Kasting816d9b62018-10-20 01:53:4675#include "ui/views/animation/ink_drop.h"
James Cookb0bf8e82017-04-09 17:01:4476#include "ui/views/border.h"
Manu Cornet2f804d92019-01-18 03:59:4777#include "ui/views/controls/button/button.h"
James Cookb0bf8e82017-04-09 17:01:4478#include "ui/views/controls/menu/menu_model_adapter.h"
79#include "ui/views/controls/menu/menu_runner.h"
yilkale9c69e72019-07-12 18:59:2380#include "ui/views/controls/separator.h"
James Cookb0bf8e82017-04-09 17:01:4481#include "ui/views/focus/focus_search.h"
82#include "ui/views/view_model.h"
83#include "ui/views/view_model_utils.h"
84#include "ui/views/widget/widget.h"
85#include "ui/wm/core/coordinate_conversion.h"
86
87using gfx::Animation;
88using views::View;
89
90namespace ash {
91
James Cookb0bf8e82017-04-09 17:01:4492// The distance of the cursor from the outer rim of the shelf before it
93// separates.
Alex Newcomera2f222a2018-03-20 20:08:3594constexpr int kRipOffDistance = 48;
James Cookb0bf8e82017-04-09 17:01:4495
96// The rip off drag and drop proxy image should get scaled by this factor.
Alex Newcomera2f222a2018-03-20 20:08:3597constexpr float kDragAndDropProxyScale = 1.2f;
James Cookb0bf8e82017-04-09 17:01:4498
99// The opacity represents that this partially disappeared item will get removed.
Alex Newcomera2f222a2018-03-20 20:08:35100constexpr float kDraggedImageOpacity = 0.5f;
James Cookb0bf8e82017-04-09 17:01:44101
102namespace {
103
yilkale9c69e72019-07-12 18:59:23104// The dimensions, in pixels, of the separator between pinned and unpinned
105// items.
106constexpr int kSeparatorSize = 20;
107constexpr int kSeparatorThickness = 1;
108
Andrew Xu40ffcdb2020-04-02 16:20:44109constexpr char kShelfIconMoveAnimationHistogram[] =
110 "Ash.ShelfIcon.AnimationSmoothness.Move";
111constexpr char kShelfIconFadeInAnimationHistogram[] =
112 "Ash.ShelfIcon.AnimationSmoothness.FadeIn";
113constexpr char kShelfIconFadeOutAnimationHistogram[] =
114 "Ash.ShelfIcon.AnimationSmoothness.FadeOut";
115
Sammie Quon9b911f2f2017-12-15 02:53:15116// Helper to check if tablet mode is enabled.
117bool IsTabletModeEnabled() {
118 return Shell::Get()->tablet_mode_controller() &&
Mitsuru Oshima9e05edd2019-06-17 19:35:39119 Shell::Get()->tablet_mode_controller()->InTabletMode();
Sammie Quon9b911f2f2017-12-15 02:53:15120}
121
James Cookb0bf8e82017-04-09 17:01:44122// A class to temporarily disable a given bounds animator.
123class BoundsAnimatorDisabler {
124 public:
125 explicit BoundsAnimatorDisabler(views::BoundsAnimator* bounds_animator)
126 : old_duration_(bounds_animator->GetAnimationDuration()),
127 bounds_animator_(bounds_animator) {
Peter Kastingb22bf2342019-09-12 19:39:49128 bounds_animator_->SetAnimationDuration(
129 base::TimeDelta::FromMilliseconds(1));
James Cookb0bf8e82017-04-09 17:01:44130 }
131
132 ~BoundsAnimatorDisabler() {
133 bounds_animator_->SetAnimationDuration(old_duration_);
134 }
135
136 private:
137 // The previous animation duration.
Peter Kastingb22bf2342019-09-12 19:39:49138 base::TimeDelta old_duration_;
James Cookb0bf8e82017-04-09 17:01:44139 // The bounds animator which gets used.
140 views::BoundsAnimator* bounds_animator_;
141
142 DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorDisabler);
143};
144
145// Custom FocusSearch used to navigate the shelf in the order items are in
146// the ViewModel.
147class ShelfFocusSearch : public views::FocusSearch {
148 public:
Sammie Quon4682f12f2018-07-13 17:34:19149 explicit ShelfFocusSearch(ShelfView* shelf_view)
150 : FocusSearch(nullptr, true, true), shelf_view_(shelf_view) {}
Chris Watkinsc24daf62017-11-28 03:43:09151 ~ShelfFocusSearch() override = default;
James Cookb0bf8e82017-04-09 17:01:44152
Sammie Quon9b911f2f2017-12-15 02:53:15153 // views::FocusSearch:
Dominic Mazzoni5d26d2ba2018-04-23 23:50:43154 View* FindNextFocusableView(
155 View* starting_view,
156 FocusSearch::SearchDirection search_direction,
157 FocusSearch::TraversalDirection traversal_direction,
158 FocusSearch::StartingViewPolicy check_starting_view,
Dominic Mazzoni2c41f792018-05-17 20:18:00159 FocusSearch::AnchoredDialogPolicy can_go_into_anchored_dialog,
Dominic Mazzoni5d26d2ba2018-04-23 23:50:43160 views::FocusTraversable** focus_traversable,
161 View** focus_traversable_view) override {
Manu Cornetaef0c112019-08-19 19:35:24162 // Build a list of all the views that we are able to focus.
Manu Cornet65594252019-01-19 11:12:13163 std::vector<views::View*> focusable_views;
James Cookb0bf8e82017-04-09 17:01:44164
Ahmed Fakhryf5a2f522020-06-04 23:16:43165 for (int i : shelf_view_->visible_views_indices())
Manu Corneta4814362019-08-09 22:09:30166 focusable_views.push_back(shelf_view_->view_model()->view_at(i));
Manu Corneta4814362019-08-09 22:09:30167
Manu Cornet65594252019-01-19 11:12:13168 // Where are we starting from?
169 int start_index = 0;
170 for (size_t i = 0; i < focusable_views.size(); ++i) {
171 if (focusable_views[i] == starting_view) {
172 start_index = i;
173 break;
Sammie Quon4682f12f2018-07-13 17:34:19174 }
175 }
Manu Cornet65594252019-01-19 11:12:13176 int new_index =
177 start_index +
178 (search_direction == FocusSearch::SearchDirection::kBackwards ? -1 : 1);
179 // Loop around.
180 if (new_index < 0)
181 new_index = focusable_views.size() - 1;
182 else if (new_index >= static_cast<int>(focusable_views.size()))
183 new_index = 0;
184
Manu Cornet65594252019-01-19 11:12:13185 return focusable_views[new_index];
James Cookb0bf8e82017-04-09 17:01:44186 }
187
188 private:
Sammie Quon4682f12f2018-07-13 17:34:19189 ShelfView* shelf_view_;
James Cookb0bf8e82017-04-09 17:01:44190
191 DISALLOW_COPY_AND_ASSIGN(ShelfFocusSearch);
192};
193
Xiyuan Xia64d1f0a82020-10-30 04:12:52194void ReportMoveAnimationSmoothness(int smoothness) {
195 base::UmaHistogramPercentageObsoleteDoNotUse(kShelfIconMoveAnimationHistogram,
196 smoothness);
197}
Andrew Xu40ffcdb2020-04-02 16:20:44198
Xiyuan Xia64d1f0a82020-10-30 04:12:52199void ReportFadeInAnimationSmoothness(int smoothness) {
200 base::UmaHistogramPercentageObsoleteDoNotUse(
201 kShelfIconFadeInAnimationHistogram, smoothness);
202}
Andrew Xu40ffcdb2020-04-02 16:20:44203
Xiyuan Xia64d1f0a82020-10-30 04:12:52204void ReportFadeOutAnimationSmoothness(int smoothness) {
205 base::UmaHistogramPercentageObsoleteDoNotUse(
206 kShelfIconFadeOutAnimationHistogram, smoothness);
207}
Andrew Xu40ffcdb2020-04-02 16:20:44208
Michael Wasserman47bf1782017-08-18 23:17:10209// Returns the id of the display on which |view| is shown.
Manu Cornet1ed1f6b2019-06-14 17:25:53210int64_t GetDisplayIdForView(const View* view) {
Michael Wasserman47bf1782017-08-18 23:17:10211 aura::Window* window = view->GetWidget()->GetNativeWindow();
212 return display::Screen::GetScreen()->GetDisplayNearestWindow(window).id();
213}
214
Manu Cornete363aec2019-01-13 13:07:07215// Whether |item_view| is a ShelfAppButton and its state is STATE_DRAGGING.
Alex Newcomerd6ccf922017-12-15 22:55:16216bool ShelfButtonIsInDrag(const ShelfItemType item_type,
217 const views::View* item_view) {
218 switch (item_type) {
219 case TYPE_PINNED_APP:
220 case TYPE_BROWSER_SHORTCUT:
221 case TYPE_APP:
Manu Cornete363aec2019-01-13 13:07:07222 return static_cast<const ShelfAppButton*>(item_view)->state() &
223 ShelfAppButton::STATE_DRAGGING;
Alex Newcomerd6ccf922017-12-15 22:55:16224 case TYPE_DIALOG:
Alex Newcomerd6ccf922017-12-15 22:55:16225 case TYPE_UNDEFINED:
226 return false;
227 }
228}
229
Ahmed Fakhry1b6ea0b2020-07-09 01:45:24230// Called back by the shelf item delegates to determine whether an app menu item
231// should be included in the shelf app menu given its corresponding window. This
232// is used to filter out items whose windows are on inactive desks when the per-
233// desk shelf feature is enabled.
234bool ShouldIncludeMenuItem(aura::Window* window) {
235 if (!features::IsPerDeskShelfEnabled())
236 return true;
237 return desks_util::BelongsToActiveDesk(window);
238}
239
Jit Yao Yap9425a1c52020-09-03 05:19:59240// Returns true if the app associated with |app_id| is a Remote App.
241bool IsRemoteApp(const std::string& app_id) {
242 AccountId account_id =
243 Shell::Get()->session_controller()->GetActiveAccountId();
244 apps::AppRegistryCache* cache =
245 apps::AppRegistryCacheWrapper::Get().GetAppRegistryCache(account_id);
246 return cache && cache->GetAppType(app_id) == apps::mojom::AppType::kRemote;
247}
248
Matthew Mourgos1241a8a2021-03-05 18:11:52249// Records the user metric action for whenever a shelf item is pinned or
250// unpinned.
251void RecordPinUnpinUserAction(bool pinned) {
252 Shell::Get()->metrics()->RecordUserMetricsAction(
253 pinned ? UMA_SHELF_ITEM_PINNED : UMA_SHELF_ITEM_UNPINNED);
254}
255
James Cookb0bf8e82017-04-09 17:01:44256} // namespace
257
Andrew Xuec6ba942020-03-23 18:17:59258// ImplicitAnimationObserver used when adding an item.
259class ShelfView::FadeInAnimationDelegate
260 : public ui::ImplicitAnimationObserver {
Andrew Xu5f02ed62020-02-28 18:11:53261 public:
Andrew Xuec6ba942020-03-23 18:17:59262 explicit FadeInAnimationDelegate(ShelfView* shelf_view)
263 : shelf_view_(shelf_view) {}
264 ~FadeInAnimationDelegate() override { StopObservingImplicitAnimations(); }
Andrew Xu5f02ed62020-02-28 18:11:53265
266 private:
Andrew Xuec6ba942020-03-23 18:17:59267 // ui::ImplicitAnimationObserver:
268 void OnImplicitAnimationsCompleted() override {
269 shelf_view_->OnFadeInAnimationEnded();
270 }
Andrew Xu5f02ed62020-02-28 18:11:53271
Andrew Xuec6ba942020-03-23 18:17:59272 ShelfView* shelf_view_ = nullptr;
Andrew Xu5f02ed62020-02-28 18:11:53273};
274
James Cookb0bf8e82017-04-09 17:01:44275// AnimationDelegate used when deleting an item. This steadily decreased the
276// opacity of the layer as the animation progress.
277class ShelfView::FadeOutAnimationDelegate : public gfx::AnimationDelegate {
278 public:
Matthew Mourgos1b5f0272019-11-26 01:58:54279 FadeOutAnimationDelegate(ShelfView* host, std::unique_ptr<views::View> view)
280 : shelf_view_(host), view_(std::move(view)) {}
Chris Watkinsc24daf62017-11-28 03:43:09281 ~FadeOutAnimationDelegate() override = default;
James Cookb0bf8e82017-04-09 17:01:44282
283 // AnimationDelegate overrides:
284 void AnimationProgressed(const Animation* animation) override {
285 view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
James Cookb0bf8e82017-04-09 17:01:44286 }
287 void AnimationEnded(const Animation* animation) override {
Andrew Xuc4c470a32019-12-19 19:19:05288 // Ensures that |view| is not used after destruction.
289 shelf_view_->StopAnimatingViewIfAny(view_.get());
290
Andrew Xuf5adb1a2019-12-04 21:12:02291 // Remove the view which has been faded away.
292 view_.reset();
293
James Cookb0bf8e82017-04-09 17:01:44294 shelf_view_->OnFadeOutAnimationEnded();
295 }
296 void AnimationCanceled(const Animation* animation) override {}
297
298 private:
299 ShelfView* shelf_view_;
300 std::unique_ptr<views::View> view_;
301
302 DISALLOW_COPY_AND_ASSIGN(FadeOutAnimationDelegate);
303};
304
305// AnimationDelegate used to trigger fading an element in. When an item is
306// inserted this delegate is attached to the animation that expands the size of
307// the item. When done it kicks off another animation to fade the item in.
308class ShelfView::StartFadeAnimationDelegate : public gfx::AnimationDelegate {
309 public:
310 StartFadeAnimationDelegate(ShelfView* host, views::View* view)
311 : shelf_view_(host), view_(view) {}
Chris Watkinsc24daf62017-11-28 03:43:09312 ~StartFadeAnimationDelegate() override = default;
James Cookb0bf8e82017-04-09 17:01:44313
314 // AnimationDelegate overrides:
315 void AnimationEnded(const Animation* animation) override {
316 shelf_view_->FadeIn(view_);
317 }
318 void AnimationCanceled(const Animation* animation) override {
319 view_->layer()->SetOpacity(1.0f);
320 }
321
322 private:
323 ShelfView* shelf_view_;
324 views::View* view_;
325
326 DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate);
327};
328
329// static
330const int ShelfView::kMinimumDragDistance = 8;
331
Andrew Xub5033732019-10-18 17:27:36332ShelfView::ShelfView(ShelfModel* model,
333 Shelf* shelf,
Andrew Xu65a43f32019-10-24 23:16:24334 ApplicationDragAndDropHost* drag_and_drop_host,
335 ShelfButtonDelegate* shelf_button_delegate)
James Cookb0bf8e82017-04-09 17:01:44336 : model_(model),
James Cook840177e2017-05-25 02:20:01337 shelf_(shelf),
Aga Wronska69eda432019-05-21 17:13:36338 view_model_(std::make_unique<views::ViewModel>()),
Sammie Quon751d1af12020-03-03 17:08:39339 bounds_animator_(
340 std::make_unique<views::BoundsAnimator>(this,
Sammie Quoncdb927d52020-05-01 19:25:59341 /*use_transforms=*/true)),
Andrew Xub5033732019-10-18 17:27:36342 focus_search_(std::make_unique<ShelfFocusSearch>(this)),
Andrew Xu65a43f32019-10-24 23:16:24343 drag_and_drop_host_(drag_and_drop_host),
344 shelf_button_delegate_(shelf_button_delegate) {
James Cookb0bf8e82017-04-09 17:01:44345 DCHECK(model_);
James Cook840177e2017-05-25 02:20:01346 DCHECK(shelf_);
Alex Newcomer95bd4832018-08-07 22:17:03347 Shell::Get()->tablet_mode_controller()->AddObserver(this);
Manu Cornetf02f084d2019-02-06 02:50:38348 Shell::Get()->AddShellObserver(this);
James Cookb0bf8e82017-04-09 17:01:44349 bounds_animator_->AddObserver(this);
Andrew Xuac425302020-05-28 20:20:21350 bounds_animator_->SetAnimationDuration(
351 ShelfConfig::Get()->shelf_animation_duration());
James Cookb0bf8e82017-04-09 17:01:44352 set_context_menu_controller(this);
Manu Cornetaef0c112019-08-19 19:35:24353 set_allow_deactivate_on_esc(true);
Manu Cornetf02f084d2019-02-06 02:50:38354
355 announcement_view_ = new views::View();
356 AddChildView(announcement_view_);
James Cookb0bf8e82017-04-09 17:01:44357}
358
359ShelfView::~ShelfView() {
Alex Newcomer95bd4832018-08-07 22:17:03360 // Shell destroys the TabletModeController before destroying all root windows.
361 if (Shell::Get()->tablet_mode_controller())
362 Shell::Get()->tablet_mode_controller()->RemoveObserver(this);
Manu Cornetf02f084d2019-02-06 02:50:38363 Shell::Get()->RemoveShellObserver(this);
James Cookb0bf8e82017-04-09 17:01:44364 bounds_animator_->RemoveObserver(this);
365 model_->RemoveObserver(this);
366}
367
Andrew Xuec9de8e2020-05-07 18:24:26368int ShelfView::GetSizeOfAppButtons(int count, int button_size) {
Matthew Mourgos75327562019-09-09 21:23:57369 const int button_spacing = ShelfConfig::Get()->button_spacing();
Andrew Xu62330e72019-08-06 21:18:50370
371 if (count == 0)
Manu Cornet80ad0702020-03-05 22:46:03372 return 0;
Andrew Xu62330e72019-08-06 21:18:50373
Andrew Xuec9de8e2020-05-07 18:24:26374 const int app_size = count * button_size;
Andrew Xu62330e72019-08-06 21:18:50375 int total_padding = button_spacing * (count - 1);
Manu Cornet80ad0702020-03-05 22:46:03376 return app_size + total_padding;
Andrew Xu62330e72019-08-06 21:18:50377}
378
James Cookb0bf8e82017-04-09 17:01:44379void ShelfView::Init() {
yilkale9c69e72019-07-12 18:59:23380 separator_ = new views::Separator();
Yulun Wub83992692020-10-29 19:08:52381 separator_->SetColor(AshColorProvider::Get()->GetContentLayerColor(
382 AshColorProvider::ContentLayerType::kSeparatorColor));
yilkale9c69e72019-07-12 18:59:23383 separator_->SetPreferredHeight(kSeparatorSize);
384 separator_->SetVisible(false);
Mitsuru Oshimace17e152020-03-20 21:28:36385 ConfigureChildView(separator_, ui::LAYER_TEXTURED);
yilkale9c69e72019-07-12 18:59:23386 AddChildView(separator_);
387
Aga Wronskae2eac672019-06-05 23:52:47388 model()->AddObserver(this);
James Cookb0bf8e82017-04-09 17:01:44389
390 const ShelfItems& items(model_->items());
Manu Cornet134ebc52019-06-07 15:09:40391
James Cookb0bf8e82017-04-09 17:01:44392 for (ShelfItems::const_iterator i = items.begin(); i != items.end(); ++i) {
393 views::View* child = CreateViewForItem(*i);
394 child->SetPaintToLayer();
Matthew Mourgos1b5f0272019-11-26 01:58:54395 int index = static_cast<int>(i - items.begin());
396 view_model_->Add(child, index);
397 // Add child view so it has the same ordering as in the |view_model_|.
398 AddChildViewAt(child, index);
James Cookb0bf8e82017-04-09 17:01:44399 }
Manu Cornet91d8f2e2018-08-08 16:34:03400
Andrew Xuec6ba942020-03-23 18:17:59401 fade_in_animation_delegate_ = std::make_unique<FadeInAnimationDelegate>(this);
402
James Cookb0bf8e82017-04-09 17:01:44403 // We'll layout when our bounds change.
404}
405
msw84b8a5f2017-05-05 00:13:36406gfx::Rect ShelfView::GetIdealBoundsOfItemIcon(const ShelfID& id) {
James Cookb0bf8e82017-04-09 17:01:44407 int index = model_->ItemIndexByID(id);
Ahmed Fakhryf5a2f522020-06-04 23:16:43408 if (!base::Contains(visible_views_indices_, index))
James Cookb0bf8e82017-04-09 17:01:44409 return gfx::Rect();
Mike Wassermanbd1952332018-03-09 00:56:34410
James Cookb0bf8e82017-04-09 17:01:44411 const gfx::Rect& ideal_bounds(view_model_->ideal_bounds(index));
Alex Newcomer43e6ccefe2019-10-09 21:43:59412 ShelfAppButton* button = GetShelfAppButton(id);
James Cookb0bf8e82017-04-09 17:01:44413 gfx::Rect icon_bounds = button->GetIconBounds();
414 return gfx::Rect(GetMirroredXWithWidthInView(
415 ideal_bounds.x() + icon_bounds.x(), icon_bounds.width()),
416 ideal_bounds.y() + icon_bounds.y(), icon_bounds.width(),
417 icon_bounds.height());
418}
419
James Cookb0bf8e82017-04-09 17:01:44420bool ShelfView::IsShowingMenu() const {
Alex Newcomer46af4c92018-05-29 22:26:14421 return shelf_menu_model_adapter_ &&
422 shelf_menu_model_adapter_->IsShowingMenu();
James Cookb0bf8e82017-04-09 17:01:44423}
424
Manu Cornet32fc91b2019-01-02 18:47:33425void ShelfView::UpdateVisibleShelfItemBoundsUnion() {
426 visible_shelf_item_bounds_union_.SetRect(0, 0, 0, 0);
Ahmed Fakhryf5a2f522020-06-04 23:16:43427 for (const int i : visible_views_indices_) {
Manu Cornet32fc91b2019-01-02 18:47:33428 const views::View* child = view_model_->view_at(i);
Ahmed Fakhry4fd7fbe2020-06-18 23:03:30429 if (ShouldShowTooltipForChildView(child)) {
430 visible_shelf_item_bounds_union_.Union(
431 GetChildViewTargetMirroredBounds(child));
432 }
James Cookb0bf8e82017-04-09 17:01:44433 }
Manu Cornet32fc91b2019-01-02 18:47:33434}
435
Andrew Xubbdc1422019-09-12 01:52:43436bool ShelfView::ShouldShowTooltipForView(const views::View* view) const {
437 if (!view || !view->parent())
438 return false;
439
440 if (view->parent() == this)
441 return ShouldShowTooltipForChildView(view);
442
Manu Cornet80ad0702020-03-05 22:46:03443 return false;
Andrew Xubbdc1422019-09-12 01:52:43444}
445
Alex Newcomer43e6ccefe2019-10-09 21:43:59446ShelfAppButton* ShelfView::GetShelfAppButton(const ShelfID& id) {
447 const int index = model_->ItemIndexByID(id);
448 if (index < 0)
449 return nullptr;
450
451 views::View* const view = view_model_->view_at(index);
452 DCHECK_EQ(ShelfAppButton::kViewClassName, view->GetClassName());
453 return static_cast<ShelfAppButton*>(view);
454}
455
Andrew Xuc4c470a32019-12-19 19:19:05456void ShelfView::StopAnimatingViewIfAny(views::View* view) {
457 if (bounds_animator_->IsAnimating(view))
458 bounds_animator_->StopAnimatingView(view);
459}
460
Alex Newcomer2204f022020-01-24 20:47:53461bool ShelfView::IsShelfViewHandlingDragAndDrop() const {
462 // If the ShelfView has a drag icon proxy, the drag originated from the
463 // AppsGridView. When the drag originates from the shelf, the
464 // ScrollableShelfView is the ApplicationDragAndDropHost, so ShelfView will
465 // not have a drag proxy.
Allen Bauer35d4e722020-06-16 22:47:56466 return !!drag_image_widget_;
Alex Newcomer2204f022020-01-24 20:47:53467}
468
Andrew Xuec9de8e2020-05-07 18:24:26469int ShelfView::GetButtonSize() const {
470 return ShelfConfig::Get()->GetShelfButtonSize(
Andrew Xu08bc4942020-06-02 16:43:32471 shelf_->hotseat_widget()->target_hotseat_density());
Andrew Xuec9de8e2020-05-07 18:24:26472}
473
474int ShelfView::GetButtonIconSize() const {
475 return ShelfConfig::Get()->GetShelfButtonIconSize(
Andrew Xu08bc4942020-06-02 16:43:32476 shelf_->hotseat_widget()->target_hotseat_density());
Andrew Xuec9de8e2020-05-07 18:24:26477}
478
479int ShelfView::GetShelfItemRippleSize() const {
480 return GetButtonSize() +
481 2 * ShelfConfig::Get()->scrollable_shelf_ripple_padding();
482}
483
Andrew Xud5bc3b6a2020-06-04 23:04:04484void ShelfView::LayoutIfAppIconsOffsetUpdates() {
485 if (app_icons_layout_offset_ != CalculateAppIconsLayoutOffset())
486 LayoutToIdealBounds();
487}
488
Toni Barzic993362ef2020-06-23 01:06:54489ShelfAppButton* ShelfView::GetShelfItemViewWithContextMenu() {
490 if (context_menu_id_.IsNull())
491 return nullptr;
492 const int item_index = model_->ItemIndexByID(context_menu_id_);
493 if (item_index < 0)
494 return nullptr;
495 return static_cast<ShelfAppButton*>(view_model_->view_at(item_index));
496}
497
Matthew Mourgos2cfa7fa82020-10-01 00:37:00498void ShelfView::AnnounceShelfItemNotificationBadge(views::View* button) {
499 announcement_view_->GetViewAccessibility().OverrideName(
500 l10n_util::GetStringFUTF16(IDS_SHELF_ITEM_HAS_NOTIFICATION_BADGE,
501 GetTitleForView(button)));
502 announcement_view_->NotifyAccessibilityEvent(ax::mojom::Event::kAlert,
503 /*send_native_event=*/true);
504}
505
Angus L. M. McLean IV272abd85c2020-11-16 20:07:59506bool ShelfView::LocationInsideVisibleShelfItemBounds(
507 const gfx::Point& location) const {
508 return visible_shelf_item_bounds_union_.Contains(location);
509}
510
Manu Cornet32fc91b2019-01-02 18:47:33511bool ShelfView::ShouldHideTooltip(const gfx::Point& cursor_location) const {
Andrew Xubbdc1422019-09-12 01:52:43512 // There are thin gaps between launcher buttons but the tooltip shouldn't hide
513 // in the gaps, but the tooltip should hide if the mouse moved totally outside
514 // of the buttons area.
Angus L. M. McLean IV272abd85c2020-11-16 20:07:59515 return !LocationInsideVisibleShelfItemBounds(cursor_location);
James Cookb0bf8e82017-04-09 17:01:44516}
517
Andrew Xubbdc1422019-09-12 01:52:43518const std::vector<aura::Window*> ShelfView::GetOpenWindowsForView(
519 views::View* view) {
520 std::vector<aura::Window*> window_list =
521 Shell::Get()->mru_window_tracker()->BuildWindowForCycleList(kActiveDesk);
522 std::vector<aura::Window*> open_windows;
523 const ShelfItem* item = ShelfItemForView(view);
Alex Newcomer45c7e3ea2018-09-24 21:20:32524
Andrew Xubbdc1422019-09-12 01:52:43525 // The concept of a list of open windows doesn't make sense for something
526 // that isn't an app shortcut: return an empty list.
527 if (!item)
528 return open_windows;
529
530 for (auto* window : window_list) {
531 const std::string window_app_id =
532 ShelfID::Deserialize(window->GetProperty(kShelfIDKey)).app_id;
533 if (window_app_id == item->id.app_id) {
534 // TODO: In the very first version we only show one window. Add the proper
535 // UI to show all windows for a given open app.
536 open_windows.push_back(window);
537 }
538 }
539 return open_windows;
James Cookb0bf8e82017-04-09 17:01:44540}
541
Jan Wilken Dörrie85285b02021-03-11 23:38:47542std::u16string ShelfView::GetTitleForView(const views::View* view) const {
Andrew Xubbdc1422019-09-12 01:52:43543 if (view->parent() == this)
544 return GetTitleForChildView(view);
Wei Lifbc8f942019-03-14 18:10:07545
Jan Wilken Dörrie85285b02021-03-11 23:38:47546 return std::u16string();
Andrew Xubbdc1422019-09-12 01:52:43547}
548
549views::View* ShelfView::GetViewForEvent(const ui::Event& event) {
550 if (event.target() == GetWidget()->GetNativeWindow())
551 return this;
552
Andrew Xubbdc1422019-09-12 01:52:43553 return nullptr;
Wei Lifbc8f942019-03-14 18:10:07554}
555
James Cookb0bf8e82017-04-09 17:01:44556gfx::Rect ShelfView::GetVisibleItemsBoundsInScreen() {
557 gfx::Size preferred_size = GetPreferredSize();
558 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
559 ConvertPointToScreen(this, &origin);
560 return gfx::Rect(origin, preferred_size);
561}
562
Manu Cornet2f804d92019-01-18 03:59:47563gfx::Size ShelfView::CalculatePreferredSize() const {
Andrew Xuec9de8e2020-05-07 18:24:26564 const int hotseat_size = shelf_->hotseat_widget()->GetHotseatSize();
Ahmed Fakhryf5a2f522020-06-04 23:16:43565 if (visible_views_indices_.empty()) {
566 // There are no visible shelf items.
Andrew Xuec9de8e2020-05-07 18:24:26567 return shelf_->IsHorizontalAlignment() ? gfx::Size(0, hotseat_size)
568 : gfx::Size(hotseat_size, 0);
Manu Cornet471267152019-06-19 20:38:18569 }
Manu Cornet2f804d92019-01-18 03:59:47570
Manu Cornet2f804d92019-01-18 03:59:47571 const gfx::Rect last_button_bounds =
Ahmed Fakhryf5a2f522020-06-04 23:16:43572 view_model_->ideal_bounds(visible_views_indices_.back());
Manu Cornet2f804d92019-01-18 03:59:47573
574 if (shelf_->IsHorizontalAlignment())
Andrew Xuec9de8e2020-05-07 18:24:26575 return gfx::Size(last_button_bounds.right(), hotseat_size);
Manu Cornet2f804d92019-01-18 03:59:47576
Andrew Xuec9de8e2020-05-07 18:24:26577 return gfx::Size(hotseat_size, last_button_bounds.bottom());
Manu Cornet2f804d92019-01-18 03:59:47578}
579
580void ShelfView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
581 // This bounds change is produced by the shelf movement (rotation, alignment
582 // change, etc.) and all content has to follow. Using an animation at that
583 // time would produce a time lag since the animation of the BoundsAnimator has
584 // itself a delay before it arrives at the required location. As such we tell
585 // the animator to go there immediately. We still want to use an animation
Toni Barzicb3323c752020-01-31 02:08:26586 // when the bounds change is caused by entering or exiting tablet mode, with
587 // an exception of usage within the scrollable shelf. With scrollable shelf
588 // (and hotseat), tablet mode transition causes hotseat bounds changes, so
589 // animating shelf items as well would introduce a lag.
Manu Cornet2f804d92019-01-18 03:59:47590
591 BoundsAnimatorDisabler disabler(bounds_animator_.get());
592
593 LayoutToIdealBounds();
594 shelf_->NotifyShelfIconPositionsChanged();
Manu Cornet2f804d92019-01-18 03:59:47595}
596
Manu Cornetdcb572e2019-03-27 01:47:08597bool ShelfView::OnKeyPressed(const ui::KeyEvent& event) {
598 if (event.IsControlDown() &&
Manu Cornete03199b2019-12-21 05:33:20599 keyboard_util::IsArrowKeyCode(event.key_code())) {
Manu Cornetdcb572e2019-03-27 01:47:08600 bool swap_with_next = (event.key_code() == ui::VKEY_DOWN ||
601 event.key_code() == ui::VKEY_RIGHT);
Alex Newcomer030e7062019-07-02 00:03:57602 SwapButtons(GetFocusManager()->GetFocusedView(), swap_with_next);
Manu Cornetdcb572e2019-03-27 01:47:08603 return true;
604 }
605 return views::View::OnKeyPressed(event);
606}
607
Andrew Xufc758fe2019-08-15 23:12:39608void ShelfView::OnMouseEvent(ui::MouseEvent* event) {
609 gfx::Point location_in_screen(event->location());
610 View::ConvertPointToScreen(this, &location_in_screen);
611
612 switch (event->type()) {
613 case ui::ET_MOUSEWHEEL:
Manu Cornet74c26c72020-03-10 21:21:13614 // The mousewheel event is handled by the ScrollableShelfView.
Andrew Xufc758fe2019-08-15 23:12:39615 break;
616 case ui::ET_MOUSE_PRESSED:
617 if (!event->IsOnlyLeftMouseButton()) {
618 if (event->IsOnlyRightMouseButton()) {
619 ShowContextMenuForViewImpl(this, location_in_screen,
620 ui::MENU_SOURCE_MOUSE);
621 event->SetHandled();
622 }
623 return;
624 }
625
626 FALLTHROUGH;
627 case ui::ET_MOUSE_DRAGGED:
628 case ui::ET_MOUSE_RELEASED:
629 // Convert the event location from current view to screen, since dragging
630 // the shelf by mouse can open the fullscreen app list. Updating the
631 // bounds of the app list during dragging is based on screen coordinate
632 // space.
633 event->set_location(location_in_screen);
634
635 event->SetHandled();
636 shelf_->ProcessMouseEvent(*event->AsMouseEvent());
637 break;
638 default:
639 break;
640 }
641}
642
Manu Cornet2f804d92019-01-18 03:59:47643views::FocusTraversable* ShelfView::GetPaneFocusTraversable() {
Manu Cornet74c26c72020-03-10 21:21:13644 // ScrollableShelfView should handles the focus traversal.
645 return nullptr;
Manu Cornet2f804d92019-01-18 03:59:47646}
647
Andrew Lee4c6dc1942019-05-30 00:18:35648const char* ShelfView::GetClassName() const {
649 return "ShelfView";
650}
651
Yulun Wu8924cebb2021-01-12 20:23:28652void ShelfView::OnThemeChanged() {
653 views::AccessiblePaneView::OnThemeChanged();
654 if (!separator_)
655 return;
656 separator_->SetColor(AshColorProvider::Get()->GetContentLayerColor(
657 AshColorProvider::ContentLayerType::kSeparatorColor));
658}
659
Manu Cornet2f804d92019-01-18 03:59:47660void ShelfView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
661 node_data->role = ax::mojom::Role::kToolbar;
662 node_data->SetName(l10n_util::GetStringUTF8(IDS_ASH_SHELF_ACCESSIBLE_NAME));
663}
664
665View* ShelfView::GetTooltipHandlerForPoint(const gfx::Point& point) {
666 // Similar implementation as views::View, but without going into each
667 // child's subviews.
668 View::Views children = GetChildrenInZOrder();
669 for (auto* child : base::Reversed(children)) {
Allen Bauer1e7e0042019-05-15 15:51:54670 if (!child->GetVisible())
Manu Cornet2f804d92019-01-18 03:59:47671 continue;
672
673 gfx::Point point_in_child_coords(point);
674 ConvertPointToTarget(this, child, &point_in_child_coords);
675 if (child->HitTestPoint(point_in_child_coords) &&
Andrew Xubbdc1422019-09-12 01:52:43676 ShouldShowTooltipForChildView(child)) {
Manu Cornet2f804d92019-01-18 03:59:47677 return child;
678 }
679 }
680 // If none of our children qualifies, just return the shelf view itself.
681 return this;
682}
683
Alex Newcomer030e7062019-07-02 00:03:57684void ShelfView::OnShelfButtonAboutToRequestFocusFromTabTraversal(
685 ShelfButton* button,
686 bool reverse) {
Manu Corneta4814362019-08-09 22:09:30687 if (ShouldFocusOut(reverse, button)) {
Manu Cornet80ad0702020-03-05 22:46:03688 shelf_->shelf_focus_cycler()->FocusOut(reverse, SourceView::kShelfView);
Alex Newcomer030e7062019-07-02 00:03:57689 }
Alex Newcomer030e7062019-07-02 00:03:57690}
691
James Cookb0bf8e82017-04-09 17:01:44692void ShelfView::ButtonPressed(views::Button* sender,
693 const ui::Event& event,
694 views::InkDrop* ink_drop) {
Manu Cornet4c282052019-01-28 16:27:08695 if (!ShouldEventActivateButton(sender, event)) {
696 ink_drop->SnapToHidden();
James Cookb0bf8e82017-04-09 17:01:44697 return;
Manu Cornet4c282052019-01-28 16:27:08698 }
James Cookb0bf8e82017-04-09 17:01:44699
Mike Wasserman8f86c042018-12-05 22:47:41700 // Prevent concurrent requests that may show application or context menus.
Mike Wasserman8f86c042018-12-05 22:47:41701 if (!item_awaiting_response_.IsNull()) {
702 const ShelfItem* item = ShelfItemForView(sender);
703 if (item && item->id != item_awaiting_response_)
704 ink_drop->AnimateToState(views::InkDropState::DEACTIVATED);
705 return;
706 }
707
Blake O'Hareb410bc92018-06-15 02:07:43708 // Ensure the keyboard is hidden and stays hidden (as long as it isn't locked)
Darren Shencb2508442019-07-03 21:48:23709 if (keyboard::KeyboardUIController::Get()->IsEnabled())
710 keyboard::KeyboardUIController::Get()->HideKeyboardExplicitlyBySystem();
Blake O'Hareb410bc92018-06-15 02:07:43711
James Cookb0bf8e82017-04-09 17:01:44712 // Record the index for the last pressed shelf item.
713 last_pressed_index_ = view_model_->GetIndexOfView(sender);
714 DCHECK_LT(-1, last_pressed_index_);
715
Sammie Quone7c1da12020-05-13 23:20:37716 // Place new windows on the same display as the button. Opening windows is
717 // usually an async operation so we wait until window activation changes
718 // (ShelfItemStatusChanged) before destroying the scoped object. Post a task
719 // to destroy the scoped object just in case the window activation event does
720 // not get fired.
mswad3d9552017-05-18 21:23:36721 aura::Window* window = sender->GetWidget()->GetNativeWindow();
Joel Hockey784a8322020-05-20 21:19:25722 scoped_display_for_new_windows_ =
723 std::make_unique<display::ScopedDisplayForNewWindows>(
724 window->GetRootWindow());
Sammie Quone7c1da12020-05-13 23:20:37725 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
726 FROM_HERE,
Joel Hockey784a8322020-05-20 21:19:25727 base::BindOnce(&ShelfView::DestroyScopedDisplay,
Sammie Quone7c1da12020-05-13 23:20:37728 weak_factory_.GetWeakPtr()),
729 base::TimeDelta::FromMilliseconds(100));
James Cookb0bf8e82017-04-09 17:01:44730
Vladislav Kaznacheev19f3c972019-02-12 22:34:33731 // Slow down activation animations if Control key is pressed.
James Cookb0bf8e82017-04-09 17:01:44732 std::unique_ptr<ui::ScopedAnimationDurationScaleMode> slowing_animations;
Vladislav Kaznacheev19f3c972019-02-12 22:34:33733 if (event.IsControlDown()) {
James Cookb0bf8e82017-04-09 17:01:44734 slowing_animations.reset(new ui::ScopedAnimationDurationScaleMode(
735 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION));
736 }
737
738 // Collect usage statistics before we decide what to do with the click.
739 switch (model_->items()[last_pressed_index_].type) {
740 case TYPE_PINNED_APP:
741 case TYPE_BROWSER_SHORTCUT:
742 case TYPE_APP:
Scott Violet41562d12017-06-26 15:15:48743 Shell::Get()->metrics()->RecordUserMetricsAction(
744 UMA_LAUNCHER_CLICK_ON_APP);
James Cookb0bf8e82017-04-09 17:01:44745 break;
746
James Cookb0bf8e82017-04-09 17:01:44747 case TYPE_DIALOG:
748 break;
749
750 case TYPE_UNDEFINED:
751 NOTREACHED() << "ShelfItemType must be set.";
752 break;
753 }
754
Michael Wasserman78b6f3e2017-07-20 19:51:20755 // Run AfterItemSelected directly if the item has no delegate (ie. in tests).
Mike Wasserman2115b162017-09-19 23:42:37756 const ShelfItem& item = model_->items()[last_pressed_index_];
Michael Wasserman78b6f3e2017-07-20 19:51:20757 if (!model_->GetShelfItemDelegate(item.id)) {
758 AfterItemSelected(item, sender, ui::Event::Clone(event), ink_drop,
Mike Wasserman3bfabb192019-05-17 01:08:34759 SHELF_ACTION_NONE, {});
Michael Wasserman78b6f3e2017-07-20 19:51:20760 return;
761 }
762
Mike Wasserman2115b162017-09-19 23:42:37763 // Notify the item of its selection; handle the result in AfterItemSelected.
Mike Wasserman8f86c042018-12-05 22:47:41764 item_awaiting_response_ = item.id;
James Cookb0bf8e82017-04-09 17:01:44765 model_->GetShelfItemDelegate(item.id)->ItemSelected(
Danan S4952ed42019-02-08 23:09:15766 ui::Event::Clone(event), GetDisplayIdForView(this), LAUNCH_FROM_SHELF,
Evan Stadee7bae45a2018-10-03 20:56:51767 base::BindOnce(&ShelfView::AfterItemSelected, weak_factory_.GetWeakPtr(),
Ahmed Fakhry1b6ea0b2020-07-09 01:45:24768 item, sender, ui::Event::Clone(event), ink_drop),
769 base::BindRepeating(&ShouldIncludeMenuItem));
James Cookb0bf8e82017-04-09 17:01:44770}
771
Alex Newcomer030e7062019-07-02 00:03:57772bool ShelfView::IsShowingMenuForView(const views::View* view) const {
773 return IsShowingMenu() &&
774 shelf_menu_model_adapter_->IsShowingMenuForView(*view);
775}
776
James Cookb0bf8e82017-04-09 17:01:44777////////////////////////////////////////////////////////////////////////////////
778// ShelfView, FocusTraversable implementation:
779
780views::FocusSearch* ShelfView::GetFocusSearch() {
781 return focus_search_.get();
782}
783
Manu Cornetaef0c112019-08-19 19:35:24784////////////////////////////////////////////////////////////////////////////////
785// ShelfView, AccessiblePaneView implementation:
786
787views::View* ShelfView::GetDefaultFocusableChild() {
788 return default_last_focusable_child_ ? FindLastFocusableChild()
789 : FindFirstFocusableChild();
790}
791
Andrew Xubd65dcb2019-09-17 01:25:41792void ShelfView::ShowContextMenuForViewImpl(views::View* source,
793 const gfx::Point& point,
794 ui::MenuSourceType source_type) {
795 // Prevent concurrent requests that may show application or context menus.
796 const ShelfItem* item = ShelfItemForView(source);
797 if (!item_awaiting_response_.IsNull()) {
798 if (item && item->id != item_awaiting_response_) {
799 static_cast<views::Button*>(source)->AnimateInkDrop(
800 views::InkDropState::DEACTIVATED, nullptr);
801 }
802 return;
803 }
804 last_pressed_index_ = -1;
805 if (!item || !model_->GetShelfItemDelegate(item->id)) {
806 ShowShelfContextMenu(ShelfID(), point, source, source_type, nullptr);
807 return;
808 }
809
810 item_awaiting_response_ = item->id;
Toni Barzicb66375b92020-01-22 21:12:42811 context_menu_callback_.Reset(base::BindOnce(
812 &ShelfView::ShowShelfContextMenu, weak_factory_.GetWeakPtr(), item->id,
813 point, source, source_type));
814
Andrew Xubd65dcb2019-09-17 01:25:41815 const int64_t display_id = GetDisplayIdForView(this);
816 model_->GetShelfItemDelegate(item->id)->GetContextMenu(
Toni Barzicb66375b92020-01-22 21:12:42817 display_id, context_menu_callback_.callback());
Andrew Xubd65dcb2019-09-17 01:25:41818}
819
Alex Newcomer95bd4832018-08-07 22:17:03820void ShelfView::OnTabletModeStarted() {
821 // Close all menus when tablet mode starts to ensure that the clamshell only
822 // context menu options are not available in tablet mode.
823 if (shelf_menu_model_adapter_)
824 shelf_menu_model_adapter_->Cancel();
825}
826
827void ShelfView::OnTabletModeEnded() {
828 // Close all menus when tablet mode ends so that menu options are kept
829 // consistent with device state.
830 if (shelf_menu_model_adapter_)
831 shelf_menu_model_adapter_->Cancel();
832}
833
Matthew Mourgoscd010542019-09-13 17:58:54834void ShelfView::OnShelfConfigUpdated() {
835 // Ensure the shelf app buttons have an icon which is up to date with the
836 // current ShelfConfig sizing.
837 for (int i = 0; i < view_model_->view_size(); i++) {
838 ShelfAppButton* button =
839 static_cast<ShelfAppButton*>(view_model_->view_at(i));
Matthew Mourgoscd010542019-09-13 17:58:54840 if (!button->IsIconSizeCurrent())
841 ShelfItemChanged(i, model_->items()[i]);
842 }
843}
844
Manu Cornetd9d5b6c2019-07-22 19:01:45845bool ShelfView::ShouldEventActivateButton(View* view, const ui::Event& event) {
846 // This only applies to app buttons.
847 DCHECK_EQ(ShelfAppButton::kViewClassName, view->GetClassName());
848 if (dragging())
849 return false;
850
851 // Ignore if we are already in a pointer event sequence started with a repost
852 // event on the same shelf item. See crbug.com/343005 for more detail.
853 if (is_repost_event_on_same_item_)
854 return false;
855
856 // Don't activate the item twice on double-click. Otherwise the window starts
857 // animating open due to the first click, then immediately minimizes due to
858 // the second click. The user most likely intended to open or minimize the
859 // item once, not do both.
860 if (event.flags() & ui::EF_IS_DOUBLE_CLICK)
861 return false;
862
863 const bool repost = IsRepostEvent(event);
864
865 // Ignore if this is a repost event on the last pressed shelf item.
866 int index = view_model_->GetIndexOfView(view);
867 if (index == -1)
868 return false;
869 return !repost || last_pressed_index_ != index;
870}
871
Jenny Zhangb541f0d2017-09-14 18:04:22872void ShelfView::CreateDragIconProxyByLocationWithNoAnimation(
873 const gfx::Point& origin_in_screen_coordinates,
874 const gfx::ImageSkia& icon,
875 views::View* replaced_view,
Weidong Guo6a3c8d72018-09-18 18:39:23876 float scale_factor,
877 int blur_radius) {
Jenny Zhangb541f0d2017-09-14 18:04:22878 drag_replaced_view_ = replaced_view;
879 aura::Window* root_window =
880 drag_replaced_view_->GetWidget()->GetNativeWindow()->GetRootWindow();
Henrique Ferreiro1748fd12020-08-04 12:51:46881 drag_image_widget_ =
882 DragImageView::Create(root_window, ui::mojom::DragEventSource::kMouse);
Allen Bauer35d4e722020-06-16 22:47:56883 DragImageView* drag_image = GetDragImage();
Weidong Guo6a3c8d72018-09-18 18:39:23884 if (blur_radius > 0)
885 SetDragImageBlur(icon.size(), blur_radius);
Allen Bauer35d4e722020-06-16 22:47:56886 drag_image->SetImage(icon);
887 gfx::Size size = drag_image->GetPreferredSize();
Jenny Zhangb541f0d2017-09-14 18:04:22888 size.set_width(size.width() * scale_factor);
889 size.set_height(size.height() * scale_factor);
890 gfx::Rect drag_image_bounds(origin_in_screen_coordinates, size);
Allen Bauer35d4e722020-06-16 22:47:56891 drag_image->SetBoundsInScreen(drag_image_bounds);
Jenny Zhangb541f0d2017-09-14 18:04:22892
893 // Turn off the default visibility animation.
Allen Bauer35d4e722020-06-16 22:47:56894 drag_image_widget_->SetVisibilityAnimationTransition(
Jenny Zhangb541f0d2017-09-14 18:04:22895 views::Widget::ANIMATE_NONE);
Allen Bauer35d4e722020-06-16 22:47:56896 drag_image->SetWidgetVisible(true);
Manu Cornet74c26c72020-03-10 21:21:13897 // Add a layer in order to ensure the icon properly animates when a drag
898 // starts from AppsGridView and ends in the Shelf.
Allen Bauer35d4e722020-06-16 22:47:56899 drag_image->SetPaintToLayer();
900 drag_image->layer()->SetFillsBoundsOpaquely(false);
Jenny Zhangb541f0d2017-09-14 18:04:22901}
902
James Cookb0bf8e82017-04-09 17:01:44903void ShelfView::UpdateDragIconProxy(
904 const gfx::Point& location_in_screen_coordinates) {
Allen Bauer35d4e722020-06-16 22:47:56905 // TODO(jennyz): Investigate why drag_image_widget_ becomes null at this point
906 // per crbug.com/34722, while the app list item is still being dragged around.
907 if (drag_image_widget_) {
908 GetDragImage()->SetScreenPosition(location_in_screen_coordinates -
909 drag_image_offset_);
James Cookb0bf8e82017-04-09 17:01:44910 }
911}
912
Jenny Zhangb541f0d2017-09-14 18:04:22913void ShelfView::UpdateDragIconProxyByLocation(
914 const gfx::Point& origin_in_screen_coordinates) {
Allen Bauer35d4e722020-06-16 22:47:56915 if (drag_image_widget_)
916 GetDragImage()->SetScreenPosition(origin_in_screen_coordinates);
Jenny Zhangb541f0d2017-09-14 18:04:22917}
918
Alex Newcomer030e7062019-07-02 00:03:57919bool ShelfView::IsDraggedView(const views::View* view) const {
MinChen1e8be7c2017-09-25 17:48:28920 return drag_view_ == view;
921}
922
Manu Cornet60444142019-02-22 17:46:07923views::View* ShelfView::FindFirstFocusableChild() {
Ahmed Fakhryf5a2f522020-06-04 23:16:43924 if (visible_views_indices_.empty())
Manu Cornete11c3c352019-07-31 06:13:41925 return nullptr;
Ahmed Fakhryf5a2f522020-06-04 23:16:43926 return view_model_->view_at(visible_views_indices_.front());
Manu Cornet60444142019-02-22 17:46:07927}
928
929views::View* ShelfView::FindLastFocusableChild() {
Ahmed Fakhryf5a2f522020-06-04 23:16:43930 if (visible_views_indices_.empty())
Manu Cornete11c3c352019-07-31 06:13:41931 return nullptr;
Ahmed Fakhryf5a2f522020-06-04 23:16:43932 return view_model_->view_at(visible_views_indices_.back());
Manu Cornet60444142019-02-22 17:46:07933}
934
Manu Cornetbc9713d2019-02-20 18:52:19935views::View* ShelfView::FindFirstOrLastFocusableChild(bool last) {
Manu Cornet60444142019-02-22 17:46:07936 return last ? FindLastFocusableChild() : FindFirstFocusableChild();
937}
938
Andrew Xu6d4cbc22019-09-13 18:27:31939bool ShelfView::HandleGestureEvent(const ui::GestureEvent* event) {
940 // Avoid changing |event|'s location since |event| may be received by post
941 // event handlers.
942 ui::GestureEvent copy_event(*event);
943
Andrew Xufc758fe2019-08-15 23:12:39944 // Convert the event location from current view to screen, since swiping up on
945 // the shelf can open the fullscreen app list. Updating the bounds of the app
946 // list during dragging is based on screen coordinate space.
Andrew Xu6d4cbc22019-09-13 18:27:31947 gfx::Point location_in_screen(copy_event.location());
Andrew Xufc758fe2019-08-15 23:12:39948 View::ConvertPointToScreen(this, &location_in_screen);
Andrew Xu6d4cbc22019-09-13 18:27:31949 copy_event.set_location(location_in_screen);
Andrew Xufc758fe2019-08-15 23:12:39950
Andrew Xu6d4cbc22019-09-13 18:27:31951 if (shelf_->ProcessGestureEvent(copy_event))
952 return true;
Andrew Xufc758fe2019-08-15 23:12:39953
Andrew Xu6d4cbc22019-09-13 18:27:31954 return false;
Andrew Xufc758fe2019-08-15 23:12:39955}
956
Andrew Xuff86804a2019-09-16 23:38:28957bool ShelfView::ShouldShowTooltipForChildView(
958 const views::View* child_view) const {
959 DCHECK_EQ(this, child_view->parent());
960
Andrew Xuff86804a2019-09-16 23:38:28961 // Don't show a tooltip for a view that's currently being dragged.
962 if (child_view == drag_view_)
963 return false;
964
965 return ShelfItemForView(child_view) && !IsShowingMenuForView(child_view);
966}
967
Aga Wronskaea39c822019-05-24 16:37:35968// static
Mitsuru Oshimace17e152020-03-20 21:28:36969void ShelfView::ConfigureChildView(views::View* view,
970 ui::LayerType layer_type) {
971 view->SetPaintToLayer(layer_type);
Aga Wronskaea39c822019-05-24 16:37:35972 view->layer()->SetFillsBoundsOpaquely(false);
973}
974
yilkale9c69e72019-07-12 18:59:23975void ShelfView::CalculateIdealBounds() {
Ahmed Fakhry039882f2020-06-16 21:56:21976 DCHECK(model()->item_count() == view_model_->view_size());
yilkale9c69e72019-07-12 18:59:23977
Matthew Mourgos75327562019-09-09 21:23:57978 const int button_spacing = ShelfConfig::Get()->button_spacing();
Wen-Chien Wange17da90f2020-08-14 19:36:38979 UpdateSeparatorIndex();
980
Andrew Xuec9de8e2020-05-07 18:24:26981 const int hotseat_size = shelf_->hotseat_widget()->GetHotseatSize();
yilkale9c69e72019-07-12 18:59:23982
yilkale9c69e72019-07-12 18:59:23983 // Don't show the separator if it isn't needed, or would appear after all
984 // visible items.
Wen-Chien Wange17da90f2020-08-14 19:36:38985 separator_->SetVisible(separator_index_ != -1 &&
986 separator_index_ < visible_views_indices_.back());
987 // Set |separator_index_| to -1 if it is not visible.
988 if (!separator_->GetVisible())
989 separator_index_ = -1;
yilkale9c69e72019-07-12 18:59:23990
Andrew Xud5bc3b6a2020-06-04 23:04:04991 app_icons_layout_offset_ = CalculateAppIconsLayoutOffset();
Andrew Xu7ac34cd2019-10-04 17:24:06992 int x = shelf()->PrimaryAxisValue(app_icons_layout_offset_, 0);
993 int y = shelf()->PrimaryAxisValue(0, app_icons_layout_offset_);
Andrew Xufc758fe2019-08-15 23:12:39994
Manu Cornet74c26c72020-03-10 21:21:13995 // The padding is handled in ScrollableShelfView.
yilkale9c69e72019-07-12 18:59:23996
Andrew Xuec9de8e2020-05-07 18:24:26997 const int button_size = GetButtonSize();
Ahmed Fakhry039882f2020-06-16 21:56:21998 for (int i = 0; i < view_model_->view_size(); ++i) {
999 const bool is_visible = view_model_->view_at(i)->GetVisible();
1000 if (!is_visible) {
1001 // Layout hidden views with empty bounds so they don't consume horizontal
Wen-Chien Wange17da90f2020-08-14 19:36:381002 // space. Note that |separator_index_| cannot be the index of a hidden
Ahmed Fakhry039882f2020-06-16 21:56:211003 // view.
Wen-Chien Wange17da90f2020-08-14 19:36:381004 DCHECK_NE(i, separator_index_);
Ahmed Fakhry039882f2020-06-16 21:56:211005 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, 0, 0));
1006 continue;
1007 }
1008
1009 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, button_size, button_size));
yilkale9c69e72019-07-12 18:59:231010
1011 x = shelf()->PrimaryAxisValue(x + button_size + button_spacing, x);
1012 y = shelf()->PrimaryAxisValue(y, y + button_size + button_spacing);
1013
Wen-Chien Wange17da90f2020-08-14 19:36:381014 if (i == separator_index_) {
yilkale9c69e72019-07-12 18:59:231015 // Place the separator halfway between the two icons it separates,
1016 // vertically centered.
1017 int half_space = button_spacing / 2;
Andrew Xuec9de8e2020-05-07 18:24:261018 int secondary_offset = (hotseat_size - kSeparatorSize) / 2;
yilkale9c69e72019-07-12 18:59:231019 x -= shelf()->PrimaryAxisValue(half_space, 0);
1020 y -= shelf()->PrimaryAxisValue(0, half_space);
1021 separator_->SetBounds(
1022 x + shelf()->PrimaryAxisValue(0, secondary_offset),
1023 y + shelf()->PrimaryAxisValue(secondary_offset, 0),
1024 shelf()->PrimaryAxisValue(kSeparatorThickness, kSeparatorSize),
1025 shelf()->PrimaryAxisValue(kSeparatorSize, kSeparatorThickness));
1026 x += shelf()->PrimaryAxisValue(half_space, 0);
1027 y += shelf()->PrimaryAxisValue(0, half_space);
1028 }
1029 }
yilkale9c69e72019-07-12 18:59:231030}
1031
Manu Cornet134ebc52019-06-07 15:09:401032views::View* ShelfView::CreateViewForItem(const ShelfItem& item) {
1033 views::View* view = nullptr;
1034 switch (item.type) {
1035 case TYPE_PINNED_APP:
1036 case TYPE_BROWSER_SHORTCUT:
1037 case TYPE_APP:
1038 case TYPE_DIALOG: {
Andrew Xu65a43f32019-10-24 23:16:241039 ShelfAppButton* button = new ShelfAppButton(
1040 this, shelf_button_delegate_ ? shelf_button_delegate_ : this);
Manu Cornet134ebc52019-06-07 15:09:401041 button->SetImage(item.image);
Matthew Mourgos842068e2021-01-27 19:48:101042 button->SetNotificationBadgeColor(item.notification_badge_color);
Manu Cornet134ebc52019-06-07 15:09:401043 button->ReflectItemStatus(item);
1044 view = button;
1045 break;
1046 }
1047
Manu Cornet134ebc52019-06-07 15:09:401048 case TYPE_UNDEFINED:
1049 return nullptr;
1050 }
1051
1052 view->set_context_menu_controller(this);
1053
Mitsuru Oshimace17e152020-03-20 21:28:361054 ConfigureChildView(view, ui::LAYER_NOT_DRAWN);
Manu Cornet134ebc52019-06-07 15:09:401055 return view;
1056}
1057
yilkale9c69e72019-07-12 18:59:231058int ShelfView::GetAvailableSpaceForAppIcons() const {
Manu Cornetaef0c112019-08-19 19:35:241059 return shelf()->PrimaryAxisValue(width(), height());
yilkale9c69e72019-07-12 18:59:231060}
1061
Wen-Chien Wange17da90f2020-08-14 19:36:381062void ShelfView::UpdateSeparatorIndex() {
Ahmed Fakhry039882f2020-06-16 21:56:211063 // A separator is shown after the last pinned item only if it's followed by a
1064 // visible app item.
Wen-Chien Wange17da90f2020-08-14 19:36:381065 int first_unpinned_index = -1;
1066 int last_pinned_index = -1;
1067
1068 int dragged_item_index = -1;
1069 if (drag_view_)
1070 dragged_item_index = view_model_->GetIndexOfView(drag_view_);
1071
1072 const bool can_drag_view_across_separator =
1073 drag_view_ && CanDragAcrossSeparator(drag_view_);
Ahmed Fakhry039882f2020-06-16 21:56:211074 for (int i = model()->item_count() - 1; i >= 0; --i) {
1075 const auto& item = model()->items()[i];
Wen-Chien Wange17da90f2020-08-14 19:36:381076 if (IsItemPinned(item)) {
1077 // Dragged pinned item may be moved to the unpinned side of the shelf and
1078 // may end up right of an unpinned app. Dismisses the dragged item to
1079 // check the next one.
1080 if (i == dragged_item_index && can_drag_view_across_separator)
1081 continue;
1082
1083 last_pinned_index = i;
1084 break;
1085 }
Ahmed Fakhry039882f2020-06-16 21:56:211086
1087 if (item.type == TYPE_APP && item.is_on_active_desk)
Wen-Chien Wange17da90f2020-08-14 19:36:381088 first_unpinned_index = i;
yilkale9c69e72019-07-12 18:59:231089 }
Ahmed Fakhry039882f2020-06-16 21:56:211090
Wen-Chien Wange17da90f2020-08-14 19:36:381091 // If there is no unpinned item in shelf, return -1 as the separator should be
1092 // hidden.
1093 if (first_unpinned_index == -1) {
1094 separator_index_ = -1;
1095 return;
1096 }
1097
1098 // If the dragged item is between the pinned apps and unpinned apps, move it
1099 // to the pinned app side if it is closer to the pinned section compared to
1100 // its ideal bounds.
1101 if (can_drag_view_across_separator &&
1102 last_pinned_index < dragged_item_index &&
1103 dragged_item_index <= first_unpinned_index &&
1104 drag_view_relative_to_ideal_bounds_ == RelativePosition::kLeft) {
1105 separator_index_ = dragged_item_index;
1106 return;
1107 }
1108
1109 separator_index_ = last_pinned_index;
yilkale9c69e72019-07-12 18:59:231110}
1111
James Cookb0bf8e82017-04-09 17:01:441112void ShelfView::DestroyDragIconProxy() {
Allen Bauer35d4e722020-06-16 22:47:561113 drag_image_widget_.reset();
James Cookb0bf8e82017-04-09 17:01:441114 drag_image_offset_ = gfx::Vector2d(0, 0);
1115}
1116
Allen Bauer35d4e722020-06-16 22:47:561117views::UniqueWidgetPtr
1118ShelfView::RetrieveDragIconProxyAndClearDragProxyState() {
Alex Newcomer2204f022020-01-24 20:47:531119 // TODO(https://crub.com/1045186): Make ScrollableShelfView the only
1120 // ApplicationDragAndDropHost in the view hierarchy, and remove this.
Allen Bauer35d4e722020-06-16 22:47:561121 views::UniqueWidgetPtr temp_drag_image_view = std::move(drag_image_widget_);
Alex Newcomer2204f022020-01-24 20:47:531122 DestroyDragIconProxy();
1123 return temp_drag_image_view;
1124}
1125
Andrew Xubd378052020-06-12 19:41:421126bool ShelfView::ShouldStartDrag(
1127 const std::string& app_id,
1128 const gfx::Point& location_in_screen_coordinates) const {
Jit Yao Yap9425a1c52020-09-03 05:19:591129 // Remote Apps are not pinnable.
1130 if (IsRemoteApp(app_id))
1131 return false;
1132
Andrew Xubd378052020-06-12 19:41:421133 // Do not start drag if an operation is already going on - or the cursor is
1134 // not inside. This could happen if mouse / touch operations overlap.
1135 return (drag_and_drop_shelf_id_.IsNull() && !app_id.empty() &&
1136 GetBoundsInScreen().Contains(location_in_screen_coordinates));
1137}
1138
James Cookb0bf8e82017-04-09 17:01:441139bool ShelfView::StartDrag(const std::string& app_id,
1140 const gfx::Point& location_in_screen_coordinates) {
Andrew Xubd378052020-06-12 19:41:421141 if (!ShouldStartDrag(app_id, location_in_screen_coordinates))
James Cookb0bf8e82017-04-09 17:01:441142 return false;
1143
1144 // If the AppsGridView (which was dispatching this event) was opened by our
1145 // button, ShelfView dragging operations are locked and we have to unlock.
1146 CancelDrag(-1);
1147 drag_and_drop_item_pinned_ = false;
msw2893fe02017-05-10 20:38:111148 drag_and_drop_shelf_id_ = ShelfID(app_id);
1149 // Check if the application is pinned - if not, we have to pin it so
James Cookb0bf8e82017-04-09 17:01:441150 // that we can re-arrange the shelf order accordingly. Note that items have
1151 // to be pinned to give them the same (order) possibilities as a shortcut.
Manu Cornet80ad0702020-03-05 22:46:031152 if (!model_->IsAppPinned(app_id)) {
Manu Cornet5b65a4f2019-11-21 19:11:001153 ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);
msw5138f3d2017-04-20 00:22:071154 model_->PinAppWithID(app_id);
James Cookb0bf8e82017-04-09 17:01:441155 drag_and_drop_item_pinned_ = true;
1156 }
1157 views::View* drag_and_drop_view =
1158 view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_));
1159 DCHECK(drag_and_drop_view);
1160
1161 // Since there is already an icon presented by the caller, we hide this item
1162 // for now. That has to be done by reducing the size since the visibility will
1163 // change once a regrouping animation is performed.
1164 pre_drag_and_drop_size_ = drag_and_drop_view->size();
1165 drag_and_drop_view->SetSize(gfx::Size());
1166
1167 // First we have to center the mouse cursor over the item.
Toni Barzicb79cc112020-01-24 20:56:591168 const gfx::Point start_point_in_screen =
1169 drag_and_drop_view->GetBoundsInScreen().CenterPoint();
1170 gfx::Point pt = start_point_in_screen;
James Cookb0bf8e82017-04-09 17:01:441171 views::View::ConvertPointFromScreen(drag_and_drop_view, &pt);
Toni Barzicb79cc112020-01-24 20:56:591172 gfx::Point point_in_root = start_point_in_screen;
James Cook00e65e92019-07-25 03:19:081173 wm::ConvertPointFromScreen(
1174 window_util::GetRootWindowAt(location_in_screen_coordinates),
1175 &point_in_root);
James Cookb0bf8e82017-04-09 17:01:441176 ui::MouseEvent event(ui::ET_MOUSE_PRESSED, pt, point_in_root,
1177 ui::EventTimeForNow(), 0, 0);
1178 PointerPressedOnButton(drag_and_drop_view, DRAG_AND_DROP, event);
1179
1180 // Drag the item where it really belongs.
1181 Drag(location_in_screen_coordinates);
1182 return true;
1183}
1184
1185bool ShelfView::Drag(const gfx::Point& location_in_screen_coordinates) {
msw84b8a5f2017-05-05 00:13:361186 if (drag_and_drop_shelf_id_.IsNull() ||
James Cookb0bf8e82017-04-09 17:01:441187 !GetBoundsInScreen().Contains(location_in_screen_coordinates))
1188 return false;
1189
1190 gfx::Point pt = location_in_screen_coordinates;
1191 views::View* drag_and_drop_view =
1192 view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_));
1193 ConvertPointFromScreen(drag_and_drop_view, &pt);
varkha3c60fc52017-05-25 16:25:111194 gfx::Point point_in_root = location_in_screen_coordinates;
James Cook00e65e92019-07-25 03:19:081195 wm::ConvertPointFromScreen(
1196 window_util::GetRootWindowAt(location_in_screen_coordinates),
1197 &point_in_root);
James Cookb0bf8e82017-04-09 17:01:441198 ui::MouseEvent event(ui::ET_MOUSE_DRAGGED, pt, point_in_root,
1199 ui::EventTimeForNow(), 0, 0);
1200 PointerDraggedOnButton(drag_and_drop_view, DRAG_AND_DROP, event);
1201 return true;
1202}
1203
1204void ShelfView::EndDrag(bool cancel) {
Avery Musbach5a8e6cf2019-01-15 00:46:571205 drag_scroll_dir_ = 0;
1206 scrolling_timer_.Stop();
1207 speed_up_drag_scrolling_.Stop();
1208
msw84b8a5f2017-05-05 00:13:361209 if (drag_and_drop_shelf_id_.IsNull())
James Cookb0bf8e82017-04-09 17:01:441210 return;
1211
1212 views::View* drag_and_drop_view =
1213 view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_));
1214 PointerReleasedOnButton(drag_and_drop_view, DRAG_AND_DROP, cancel);
1215
1216 // Either destroy the temporarily created item - or - make the item visible.
1217 if (drag_and_drop_item_pinned_ && cancel) {
Manu Cornet5b65a4f2019-11-21 19:11:001218 ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);
msw2893fe02017-05-10 20:38:111219 model_->UnpinAppWithID(drag_and_drop_shelf_id_.app_id);
James Cookb0bf8e82017-04-09 17:01:441220 } else if (drag_and_drop_view) {
Andrew Xuaeb60602019-11-07 22:21:071221 std::unique_ptr<gfx::AnimationDelegate> animation_delegate;
1222
Manu Cornet74c26c72020-03-10 21:21:131223 // Resets the dragged view's opacity at the end of drag. Otherwise, if
1224 // the app is already pinned on shelf before drag starts, the dragged view
1225 // will be invisible when drag ends.
1226 animation_delegate =
1227 std::make_unique<StartFadeAnimationDelegate>(this, drag_and_drop_view);
Andrew Xuaeb60602019-11-07 22:21:071228
James Cookb0bf8e82017-04-09 17:01:441229 if (cancel) {
1230 // When a hosted drag gets canceled, the item can remain in the same slot
1231 // and it might have moved within the bounds. In that case the item need
1232 // to animate back to its correct location.
1233 AnimateToIdealBounds();
Andrew Xuaeb60602019-11-07 22:21:071234 bounds_animator_->SetAnimationDelegate(drag_and_drop_view,
1235 std::move(animation_delegate));
James Cookb0bf8e82017-04-09 17:01:441236 } else {
1237 drag_and_drop_view->SetSize(pre_drag_and_drop_size_);
1238 }
1239 }
1240
msw84b8a5f2017-05-05 00:13:361241 drag_and_drop_shelf_id_ = ShelfID();
James Cookb0bf8e82017-04-09 17:01:441242}
1243
Alex Newcomer030e7062019-07-02 00:03:571244void ShelfView::SwapButtons(views::View* button_to_swap, bool with_next) {
Manu Cornetdcb572e2019-03-27 01:47:081245 if (!button_to_swap)
1246 return;
Manu Cornetdcb572e2019-03-27 01:47:081247
1248 // Find the index of the button to swap in the view model.
1249 int src_index = -1;
1250 for (int i = 0; i < view_model_->view_size(); ++i) {
1251 View* view = view_model_->view_at(i);
1252 if (view == button_to_swap) {
1253 src_index = i;
1254 break;
1255 }
1256 }
1257
Manu Cornetdcb572e2019-03-27 01:47:081258 // Swapping items in the model is sufficient, everything will then be
1259 // reflected in the views.
Manu Cornet36f91b772020-03-18 15:14:051260 if (model_->Swap(src_index, with_next)) {
Manu Cornetec887392020-01-10 20:55:011261 AnimateToIdealBounds();
Manu Cornet36f91b772020-03-18 15:14:051262 const ShelfItem src_item = model_->items()[src_index];
1263 const ShelfItem dst_item =
1264 model_->items()[src_index + (with_next ? 1 : -1)];
1265 AnnounceSwapEvent(src_item, dst_item);
1266 }
Manu Cornetdcb572e2019-03-27 01:47:081267}
1268
James Cookb0bf8e82017-04-09 17:01:441269void ShelfView::PointerPressedOnButton(views::View* view,
1270 Pointer pointer,
1271 const ui::LocatedEvent& event) {
1272 if (drag_view_)
1273 return;
1274
MinChen580196e12017-11-07 21:07:551275 if (IsShowingMenu())
Alex Newcomer46af4c92018-05-29 22:26:141276 shelf_menu_model_adapter_->Cancel();
MinChen580196e12017-11-07 21:07:551277
James Cookb0bf8e82017-04-09 17:01:441278 int index = view_model_->GetIndexOfView(view);
Manu Cornet471267152019-06-19 20:38:181279 if (index == -1 || view_model_->view_size() < 1)
James Cookb0bf8e82017-04-09 17:01:441280 return; // View is being deleted, ignore request.
1281
James Cookb0bf8e82017-04-09 17:01:441282 // Only when the repost event occurs on the same shelf item, we should ignore
1283 // the call in ShelfView::ButtonPressed(...).
1284 is_repost_event_on_same_item_ =
1285 IsRepostEvent(event) && (last_pressed_index_ == index);
1286
Manu Cornete363aec2019-01-13 13:07:071287 CHECK_EQ(ShelfAppButton::kViewClassName, view->GetClassName());
1288 drag_view_ = static_cast<ShelfAppButton*>(view);
James Cookb0bf8e82017-04-09 17:01:441289 drag_origin_ = gfx::Point(event.x(), event.y());
1290 UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage",
Wei-Yin Chen (陳威尹)3330991f2017-07-27 17:25:571291 static_cast<ShelfAlignmentUmaEnumValue>(
1292 shelf_->SelectValueForShelfAlignment(
1293 SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM,
1294 SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT,
1295 SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT)),
James Cookb0bf8e82017-04-09 17:01:441296 SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT);
1297}
1298
1299void ShelfView::PointerDraggedOnButton(views::View* view,
1300 Pointer pointer,
1301 const ui::LocatedEvent& event) {
minch46d21232017-05-03 19:12:251302 if (CanPrepareForDrag(pointer, event))
James Cookb0bf8e82017-04-09 17:01:441303 PrepareForDrag(pointer, event);
minch46d21232017-05-03 19:12:251304
James Cookb0bf8e82017-04-09 17:01:441305 if (drag_pointer_ == pointer)
1306 ContinueDrag(event);
1307}
1308
1309void ShelfView::PointerReleasedOnButton(views::View* view,
1310 Pointer pointer,
1311 bool canceled) {
Avery Musbach5a8e6cf2019-01-15 00:46:571312 drag_scroll_dir_ = 0;
1313 scrolling_timer_.Stop();
1314 speed_up_drag_scrolling_.Stop();
1315
James Cookb0bf8e82017-04-09 17:01:441316 is_repost_event_on_same_item_ = false;
1317
1318 if (canceled) {
1319 CancelDrag(-1);
1320 } else if (drag_pointer_ == pointer) {
1321 FinalizeRipOffDrag(false);
1322 drag_pointer_ = NONE;
Wen-Chien Wange17da90f2020-08-14 19:36:381323
1324 // Check if the pin status of |drag_view_| should be changed when
1325 // |drag_view_| is dragged over the separator. Do nothing if |drag_view_| is
1326 // already handled in FinalizedRipOffDrag.
1327 if (drag_view_) {
1328 if (ShouldUpdateDraggedViewPinStatus(view_model_->GetIndexOfView(view))) {
1329 const std::string drag_app_id = ShelfItemForView(drag_view_)->id.app_id;
1330 ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);
1331 if (model_->IsAppPinned(drag_app_id)) {
1332 model_->UnpinAppWithID(drag_app_id);
1333 } else {
1334 model_->PinAppWithID(drag_app_id);
1335 }
1336 }
1337 }
James Cookb0bf8e82017-04-09 17:01:441338 AnimateToIdealBounds();
1339 }
Andrew Xub5033732019-10-18 17:27:361340
1341 if (drag_pointer_ != NONE)
1342 return;
1343
Manu Cornet74c26c72020-03-10 21:21:131344 drag_and_drop_host_->DestroyDragIconProxy();
Andrew Xub5033732019-10-18 17:27:361345
James Cookb0bf8e82017-04-09 17:01:441346 // If the drag pointer is NONE, no drag operation is going on and the
Wen-Chien Wange17da90f2020-08-14 19:36:381347 // |drag_view_| can be released.
Andrew Xub5033732019-10-18 17:27:361348 drag_view_ = nullptr;
Wen-Chien Wange17da90f2020-08-14 19:36:381349 drag_view_relative_to_ideal_bounds_ = RelativePosition::kNotAvailable;
James Cookb0bf8e82017-04-09 17:01:441350}
1351
1352void ShelfView::LayoutToIdealBounds() {
1353 if (bounds_animator_->IsAnimating()) {
1354 AnimateToIdealBounds();
1355 return;
1356 }
1357
Manu Cornet28b8eb082019-03-20 16:22:581358 CalculateIdealBounds();
James Cookb0bf8e82017-04-09 17:01:441359 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
Manu Cornet32fc91b2019-01-02 18:47:331360 UpdateVisibleShelfItemBoundsUnion();
James Cookb0bf8e82017-04-09 17:01:441361}
1362
Manu Cornet91d8f2e2018-08-08 16:34:031363bool ShelfView::IsItemPinned(const ShelfItem& item) const {
James Cook49d30522020-04-22 13:45:371364 return IsPinnedShelfItemType(item.type);
Manu Cornet91d8f2e2018-08-08 16:34:031365}
1366
Ahmed Fakhry039882f2020-06-16 21:56:211367bool ShelfView::IsItemVisible(const ShelfItem& item) const {
1368 return IsItemPinned(item) || item.is_on_active_desk;
1369}
1370
Sammie Quon8f7d4c12018-08-08 18:27:461371void ShelfView::OnTabletModeChanged() {
Manu Cornet74c26c72020-03-10 21:21:131372 // The layout change will happen as part of shelf config update.
Sammie Quon8f7d4c12018-08-08 18:27:461373}
1374
James Cookb0bf8e82017-04-09 17:01:441375void ShelfView::AnimateToIdealBounds() {
Manu Cornet28b8eb082019-03-20 16:22:581376 CalculateIdealBounds();
Xiyuan Xia64d1f0a82020-10-30 04:12:521377
1378 move_animation_tracker_.emplace(
1379 GetWidget()->GetCompositor()->RequestNewThroughputTracker());
1380 move_animation_tracker_->Start(metrics_util::ForSmoothness(
1381 base::BindRepeating(&ReportMoveAnimationSmoothness)));
Manu Cornet5fd92392019-06-14 17:35:301382
Manu Cornet471267152019-06-19 20:38:181383 for (int i = 0; i < view_model_->view_size(); ++i) {
James Cookb0bf8e82017-04-09 17:01:441384 View* view = view_model_->view_at(i);
1385 bounds_animator_->AnimateViewTo(view, view_model_->ideal_bounds(i));
1386 // Now that the item animation starts, we have to make sure that the
1387 // padding of the first gets properly transferred to the new first item.
Manu Cornet5fd92392019-06-14 17:35:301388 if (view->border())
James Cookb0bf8e82017-04-09 17:01:441389 view->SetBorder(views::NullBorder());
1390 }
Manu Cornet32fc91b2019-01-02 18:47:331391 UpdateVisibleShelfItemBoundsUnion();
James Cookb0bf8e82017-04-09 17:01:441392}
1393
James Cookb0bf8e82017-04-09 17:01:441394void ShelfView::FadeIn(views::View* view) {
1395 view->SetVisible(true);
1396 view->layer()->SetOpacity(0);
Andrew Xuec6ba942020-03-23 18:17:591397
1398 ui::ScopedLayerAnimationSettings fade_in_animation_settings(
1399 view->layer()->GetAnimator());
1400 fade_in_animation_settings.SetTweenType(gfx::Tween::EASE_OUT);
1401 fade_in_animation_settings.SetPreemptionStrategy(
1402 ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
1403 fade_in_animation_settings.AddObserver(fade_in_animation_delegate_.get());
Xiyuan Xia64d1f0a82020-10-30 04:12:521404
1405 ui::AnimationThroughputReporter reporter(
1406 fade_in_animation_settings.GetAnimator(),
1407 metrics_util::ForSmoothness(
1408 base::BindRepeating(&ReportFadeInAnimationSmoothness)));
1409
Andrew Xuec6ba942020-03-23 18:17:591410 view->layer()->SetOpacity(1.f);
James Cookb0bf8e82017-04-09 17:01:441411}
1412
1413void ShelfView::PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event) {
1414 DCHECK(!dragging());
1415 DCHECK(drag_view_);
1416 drag_pointer_ = pointer;
1417 start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
Avery Musbach5a8e6cf2019-01-15 00:46:571418 drag_scroll_dir_ = 0;
James Cookb0bf8e82017-04-09 17:01:441419
1420 if (start_drag_index_ == -1) {
1421 CancelDrag(-1);
1422 return;
1423 }
1424
Toni Barzicb66375b92020-01-22 21:12:421425 // Cancel in-flight request for app item context menu model (made when app
1426 // context menu is requested), to prevent the pending callback from showing
1427 // a context menu just after drag starts.
1428 if (!context_menu_callback_.IsCancelled()) {
1429 context_menu_callback_.Cancel();
1430 item_awaiting_response_ = ShelfID();
1431 }
1432
James Cookb0bf8e82017-04-09 17:01:441433 // Move the view to the front so that it appears on top of other views.
1434 ReorderChildView(drag_view_, -1);
1435 bounds_animator_->StopAnimatingView(drag_view_);
1436
1437 drag_view_->OnDragStarted(&event);
Andrew Xub5033732019-10-18 17:27:361438
Manu Cornet74c26c72020-03-10 21:21:131439 drag_view_->layer()->SetOpacity(0.0f);
1440 drag_and_drop_host_->CreateDragIconProxyByLocationWithNoAnimation(
1441 event.root_location(), drag_view_->GetImage(), drag_view_,
1442 /*scale_factor=*/1.0f, /*blur_radius=*/0);
James Cookb0bf8e82017-04-09 17:01:441443}
1444
1445void ShelfView::ContinueDrag(const ui::LocatedEvent& event) {
1446 DCHECK(dragging());
1447 DCHECK(drag_view_);
Avery Musbach5a8e6cf2019-01-15 00:46:571448 DCHECK_NE(-1, view_model_->GetIndexOfView(drag_view_));
James Cookb0bf8e82017-04-09 17:01:441449
Avery Musbach709f5212020-04-23 18:21:581450 const bool dragged_off_shelf_before = dragged_off_shelf_;
1451
Avery Musbache63496622020-04-22 21:00:591452 // Handle rip off functionality if this is not a drag and drop host operation
1453 // and not the app list item.
msw84b8a5f2017-05-05 00:13:361454 if (drag_and_drop_shelf_id_.IsNull() &&
Avery Musbach5a8e6cf2019-01-15 00:46:571455 RemovableByRipOff(view_model_->GetIndexOfView(drag_view_)) !=
Avery Musbache63496622020-04-22 21:00:591456 NOT_REMOVABLE) {
1457 HandleRipOffDrag(event);
1458 // Check if the item got ripped off the shelf - if it did we are done.
1459 if (dragged_off_shelf_) {
1460 drag_scroll_dir_ = 0;
1461 scrolling_timer_.Stop();
1462 speed_up_drag_scrolling_.Stop();
Avery Musbach709f5212020-04-23 18:21:581463 if (!dragged_off_shelf_before)
1464 model_->OnItemRippedOff();
Avery Musbache63496622020-04-22 21:00:591465 return;
1466 }
Avery Musbach5a8e6cf2019-01-15 00:46:571467 }
1468
Andrew Xu8d0825a2020-01-09 22:59:531469 // Calculates the drag point in screen before MoveDragViewTo is called.
1470 gfx::Point drag_point_in_screen(event.location());
1471 ConvertPointToScreen(drag_view_, &drag_point_in_screen);
1472
James Cookb0bf8e82017-04-09 17:01:441473 gfx::Point drag_point(event.location());
1474 ConvertPointToTarget(drag_view_, this, &drag_point);
Avery Musbach5a8e6cf2019-01-15 00:46:571475 MoveDragViewTo(shelf_->PrimaryAxisValue(drag_point.x() - drag_origin_.x(),
1476 drag_point.y() - drag_origin_.y()));
Manu Cornet74c26c72020-03-10 21:21:131477 drag_and_drop_host_->UpdateDragIconProxy(drag_point_in_screen -
1478 drag_origin_.OffsetFromOrigin());
Avery Musbach709f5212020-04-23 18:21:581479 if (dragged_off_shelf_before)
1480 model_->OnItemReturnedFromRipOff(view_model_->GetIndexOfView(drag_view_));
Avery Musbach5a8e6cf2019-01-15 00:46:571481}
James Cookb0bf8e82017-04-09 17:01:441482
Avery Musbach5a8e6cf2019-01-15 00:46:571483void ShelfView::MoveDragViewTo(int primary_axis_coordinate) {
Wen-Chien Wange17da90f2020-08-14 19:36:381484 const int current_item_index = view_model_->GetIndexOfView(drag_view_);
1485 const std::pair<int, int> indices(GetDragRange(current_item_index));
James Cook840177e2017-05-25 02:20:011486 if (shelf_->IsHorizontalAlignment()) {
Avery Musbach5a8e6cf2019-01-15 00:46:571487 int x = GetMirroredXWithWidthInView(primary_axis_coordinate,
1488 drag_view_->width());
1489 x = std::max(view_model_->ideal_bounds(indices.first).x(), x);
1490 x = std::min(view_model_->ideal_bounds(indices.second).right() -
Wen-Chien Wange17da90f2020-08-14 19:36:381491 view_model_->ideal_bounds(current_item_index).width(),
James Cookb0bf8e82017-04-09 17:01:441492 x);
Wen-Chien Wange17da90f2020-08-14 19:36:381493 if (drag_view_->x() != x)
1494 drag_view_->SetX(x);
James Cookb0bf8e82017-04-09 17:01:441495 } else {
Avery Musbach5a8e6cf2019-01-15 00:46:571496 int y = std::max(view_model_->ideal_bounds(indices.first).y(),
1497 primary_axis_coordinate);
1498 y = std::min(view_model_->ideal_bounds(indices.second).bottom() -
Wen-Chien Wange17da90f2020-08-14 19:36:381499 view_model_->ideal_bounds(current_item_index).height(),
James Cookb0bf8e82017-04-09 17:01:441500 y);
Wen-Chien Wange17da90f2020-08-14 19:36:381501 if (drag_view_->y() != y)
1502 drag_view_->SetY(y);
James Cookb0bf8e82017-04-09 17:01:441503 }
1504
1505 int target_index = views::ViewModelUtils::DetermineMoveIndex(
Hwanseung Lee92031472019-03-29 00:06:441506 *view_model_, drag_view_, shelf_->IsHorizontalAlignment(),
Avery Musbach5a8e6cf2019-01-15 00:46:571507 drag_view_->x(), drag_view_->y());
James Cookb0bf8e82017-04-09 17:01:441508 target_index =
Peter Kasting8b80a9b2019-09-09 20:27:231509 base::ClampToRange(target_index, indices.first, indices.second);
James Cookb0bf8e82017-04-09 17:01:441510
Wen-Chien Wange17da90f2020-08-14 19:36:381511 // Check the relative position of |drag_view_| and its ideal bounds if it can
1512 // be dragged across the separator to pin or unpin.
1513 if (CanDragAcrossSeparator(drag_view_)) {
1514 // Compare the center points of |drag_view_| and its ideal bounds to
1515 // determine whether the separator should be moved to the left or right by
1516 // using |drag_view_relative_to_ideal_bounds_|. The actual position will
1517 // be updated in CalculateIdealBounds.
1518 gfx::Point drag_view_center = drag_view_->bounds().CenterPoint();
1519 int drag_view_position =
1520 shelf()->PrimaryAxisValue(drag_view_center.x(), drag_view_center.y());
1521 gfx::Point ideal_bound_center =
1522 view_model_->ideal_bounds(target_index).CenterPoint();
1523 int ideal_bound_position = shelf()->PrimaryAxisValue(
1524 ideal_bound_center.x(), ideal_bound_center.y());
James Cookb0bf8e82017-04-09 17:01:441525
Wen-Chien Wange17da90f2020-08-14 19:36:381526 drag_view_relative_to_ideal_bounds_ =
1527 drag_view_position < ideal_bound_position ? RelativePosition::kLeft
1528 : RelativePosition::kRight;
1529 if (target_index == current_item_index) {
1530 AnimateToIdealBounds();
1531 NotifyAccessibilityEvent(ax::mojom::Event::kChildrenChanged,
1532 true /* send_native_event */);
1533 }
1534 }
1535
1536 if (target_index == current_item_index)
1537 return;
1538 // Change the model if the dragged item index is changed, the ShelfItemMoved()
1539 // callback will handle the |view_model_| update.
1540 model_->Move(current_item_index, target_index);
James Cookb0bf8e82017-04-09 17:01:441541 bounds_animator_->StopAnimatingView(drag_view_);
1542}
1543
Toni Barzicb6a993e2020-01-27 17:59:181544void ShelfView::CreateDragIconProxy(
1545 const gfx::Point& location_in_screen_coordinates,
1546 const gfx::ImageSkia& icon,
1547 views::View* replaced_view,
1548 const gfx::Vector2d& cursor_offset_from_center,
1549 float scale_factor,
1550 bool animate_visibility) {
1551 drag_replaced_view_ = replaced_view;
1552 aura::Window* root_window =
1553 drag_replaced_view_->GetWidget()->GetNativeWindow()->GetRootWindow();
Henrique Ferreiro1748fd12020-08-04 12:51:461554 drag_image_widget_ =
1555 DragImageView::Create(root_window, ui::mojom::DragEventSource::kMouse);
Allen Bauer35d4e722020-06-16 22:47:561556 DragImageView* drag_image = GetDragImage();
1557 drag_image->SetImage(icon);
1558 gfx::Size size = drag_image->GetPreferredSize();
Toni Barzicb6a993e2020-01-27 17:59:181559 size.set_width(std::round(size.width() * scale_factor));
1560 size.set_height(std::round(size.height() * scale_factor));
1561 drag_image_offset_ = gfx::Vector2d(size.width() / 2, size.height() / 2) +
1562 cursor_offset_from_center;
1563 gfx::Rect drag_image_bounds(
1564 location_in_screen_coordinates - drag_image_offset_, size);
Allen Bauer35d4e722020-06-16 22:47:561565 drag_image->SetBoundsInScreen(drag_image_bounds);
Toni Barzicb6a993e2020-01-27 17:59:181566 if (!animate_visibility) {
Allen Bauer35d4e722020-06-16 22:47:561567 drag_image_widget_->SetVisibilityAnimationTransition(
Toni Barzicb6a993e2020-01-27 17:59:181568 views::Widget::ANIMATE_NONE);
1569 }
Allen Bauer35d4e722020-06-16 22:47:561570 drag_image->SetWidgetVisible(true);
Toni Barzicb6a993e2020-01-27 17:59:181571}
1572
Avery Musbache63496622020-04-22 21:00:591573void ShelfView::HandleRipOffDrag(const ui::LocatedEvent& event) {
James Cookb0bf8e82017-04-09 17:01:441574 int current_index = view_model_->GetIndexOfView(drag_view_);
1575 DCHECK_NE(-1, current_index);
msw2893fe02017-05-10 20:38:111576 std::string dragged_app_id = model_->items()[current_index].id.app_id;
James Cookb0bf8e82017-04-09 17:01:441577
mswca7526ef2017-05-31 00:52:481578 gfx::Point screen_location = event.root_location();
1579 ::wm::ConvertPointToScreen(GetWidget()->GetNativeWindow()->GetRootWindow(),
1580 &screen_location);
James Cookb0bf8e82017-04-09 17:01:441581
1582 // To avoid ugly forwards and backwards flipping we use different constants
1583 // for ripping off / re-inserting the items.
1584 if (dragged_off_shelf_) {
1585 // If the shelf/overflow bubble bounds contains |screen_location| we insert
1586 // the item back into the shelf.
1587 if (GetBoundsForDragInsertInScreen().Contains(screen_location)) {
Manu Cornet74c26c72020-03-10 21:21:131588 drag_and_drop_host_->CreateDragIconProxyByLocationWithNoAnimation(
Allen Bauer35d4e722020-06-16 22:47:561589 event.root_location(), drag_view_->GetImage(), GetDragImage(),
Manu Cornet74c26c72020-03-10 21:21:131590 /*scale_factor=*/1.0f, /*blur_radius=*/0);
Toni Barzicb6a993e2020-01-27 17:59:181591
James Cookb0bf8e82017-04-09 17:01:441592 // Destroy our proxy view item.
1593 DestroyDragIconProxy();
1594 // Re-insert the item and return simply false since the caller will handle
1595 // the move as in any normal case.
1596 dragged_off_shelf_ = false;
Andrew Xub5033732019-10-18 17:27:361597
Avery Musbache63496622020-04-22 21:00:591598 return;
James Cookb0bf8e82017-04-09 17:01:441599 }
1600 // Move our proxy view item.
1601 UpdateDragIconProxy(screen_location);
Avery Musbache63496622020-04-22 21:00:591602 return;
James Cookb0bf8e82017-04-09 17:01:441603 }
sammiequonbbd450e32017-06-06 22:16:161604
1605 // Mark the item as dragged off the shelf if the drag distance exceeds
Manu Cornet80ad0702020-03-05 22:46:031606 // |kRipOffDistance|.
James Cookb0bf8e82017-04-09 17:01:441607 int delta = CalculateShelfDistance(screen_location);
sammiequonbbd450e32017-06-06 22:16:161608 bool dragged_off_shelf = delta > kRipOffDistance;
sammiequonbbd450e32017-06-06 22:16:161609
1610 if (dragged_off_shelf) {
Manu Cornet74c26c72020-03-10 21:21:131611 // Replaces a proxy icon provided by drag_and_drop_host_ - keep cursor
1612 // position consistent with the host provided icon, and disable
1613 // visibility animations (to prevent the proxy icon from lingering on
1614 // when replaced with the icon provided by the host).
Toni Barzicb6a993e2020-01-27 17:59:181615 const gfx::Point center = drag_view_->GetLocalBounds().CenterPoint();
Manu Cornet74c26c72020-03-10 21:21:131616 const gfx::Vector2d cursor_offset_from_center = drag_origin_ - center;
James Cookb0bf8e82017-04-09 17:01:441617 // Create a proxy view item which can be moved anywhere.
1618 CreateDragIconProxy(event.root_location(), drag_view_->GetImage(),
Toni Barzicb6a993e2020-01-27 17:59:181619 drag_view_, cursor_offset_from_center,
Manu Cornet74c26c72020-03-10 21:21:131620 kDragAndDropProxyScale, /*animate_visibility=*/false);
Andrew Xub5033732019-10-18 17:27:361621
Andrew Xu8d0825a2020-01-09 22:59:531622 dragged_off_shelf_ = true;
1623
Manu Cornet74c26c72020-03-10 21:21:131624 drag_and_drop_host_->DestroyDragIconProxy();
Andrew Xub5033732019-10-18 17:27:361625
James Cookb0bf8e82017-04-09 17:01:441626 if (RemovableByRipOff(current_index) == REMOVABLE) {
Manu Cornetb17e07862018-08-01 18:28:491627 // Move the item to the back and hide it. ShelfItemMoved() callback will
1628 // handle the |view_model_| update and call AnimateToIdealBounds().
Avery Musbach5edfac72020-04-22 16:13:071629 if (current_index != model_->item_count() - 1)
Manu Cornetb17e07862018-08-01 18:28:491630 model_->Move(current_index, model_->item_count() - 1);
James Cookb0bf8e82017-04-09 17:01:441631 // Make the item partially disappear to show that it will get removed if
1632 // dropped.
Allen Bauer35d4e722020-06-16 22:47:561633 GetDragImage()->SetOpacity(kDraggedImageOpacity);
James Cookb0bf8e82017-04-09 17:01:441634 }
James Cookb0bf8e82017-04-09 17:01:441635 }
James Cookb0bf8e82017-04-09 17:01:441636}
1637
1638void ShelfView::FinalizeRipOffDrag(bool cancel) {
1639 if (!dragged_off_shelf_)
1640 return;
1641 // Make sure we do not come in here again.
1642 dragged_off_shelf_ = false;
1643
1644 // Coming here we should always have a |drag_view_|.
1645 DCHECK(drag_view_);
1646 int current_index = view_model_->GetIndexOfView(drag_view_);
1647 // If the view isn't part of the model anymore (|current_index| == -1), a sync
1648 // operation must have removed it. In that case we shouldn't change the model
1649 // and only delete the proxy image.
1650 if (current_index == -1) {
1651 DestroyDragIconProxy();
1652 return;
1653 }
1654
1655 // Set to true when the animation should snap back to where it was before.
1656 bool snap_back = false;
1657 // Items which cannot be dragged off will be handled as a cancel.
1658 if (!cancel) {
Manu Cornet80ad0702020-03-05 22:46:031659 if (RemovableByRipOff(current_index) != REMOVABLE) {
James Cookb0bf8e82017-04-09 17:01:441660 // Make sure we do not try to remove un-removable items like items which
1661 // were not pinned or have to be always there.
1662 cancel = true;
1663 snap_back = true;
1664 } else {
1665 // Make sure the item stays invisible upon removal.
1666 drag_view_->SetVisible(false);
Manu Cornet5b65a4f2019-11-21 19:11:001667 ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);
msw2893fe02017-05-10 20:38:111668 model_->UnpinAppWithID(model_->items()[current_index].id.app_id);
James Cookb0bf8e82017-04-09 17:01:441669 }
1670 }
1671 if (cancel || snap_back) {
Manu Cornet80ad0702020-03-05 22:46:031672 if (!cancelling_drag_model_changed_) {
James Cookb0bf8e82017-04-09 17:01:441673 // Only do something if the change did not come through a model change.
Allen Bauer35d4e722020-06-16 22:47:561674 gfx::Rect drag_bounds = GetDragImage()->GetBoundsInScreen();
James Cookb0bf8e82017-04-09 17:01:441675 gfx::Point relative_to = GetBoundsInScreen().origin();
1676 gfx::Rect target(
1677 gfx::PointAtOffsetFromOrigin(drag_bounds.origin() - relative_to),
1678 drag_bounds.size());
1679 drag_view_->SetBoundsRect(target);
1680 // Hide the status from the active item since we snap it back now. Upon
1681 // animation end the flag gets cleared if |snap_back_from_rip_off_view_|
1682 // is set.
1683 snap_back_from_rip_off_view_ = drag_view_;
Manu Cornete363aec2019-01-13 13:07:071684 drag_view_->AddState(ShelfAppButton::STATE_HIDDEN);
James Cookb0bf8e82017-04-09 17:01:441685 // When a canceling drag model is happening, the view model is diverged
1686 // from the menu model and movements / animations should not be done.
1687 model_->Move(current_index, start_drag_index_);
1688 AnimateToIdealBounds();
1689 }
1690 drag_view_->layer()->SetOpacity(1.0f);
nancylingwang0c9842472020-08-21 15:50:241691 model_->OnItemReturnedFromRipOff(model_->item_count() - 1);
James Cookb0bf8e82017-04-09 17:01:441692 }
1693 DestroyDragIconProxy();
1694}
1695
1696ShelfView::RemovableState ShelfView::RemovableByRipOff(int index) const {
1697 DCHECK(index >= 0 && index < model_->item_count());
1698 ShelfItemType type = model_->items()[index].type;
Manu Cornetff12bb12019-06-20 16:52:061699 if (type == TYPE_DIALOG)
James Cookb0bf8e82017-04-09 17:01:441700 return NOT_REMOVABLE;
1701
1702 if (model_->items()[index].pinned_by_policy)
1703 return NOT_REMOVABLE;
1704
1705 // Note: Only pinned app shortcuts can be removed!
msw2893fe02017-05-10 20:38:111706 const std::string& app_id = model_->items()[index].id.app_id;
msw5138f3d2017-04-20 00:22:071707 return (type == TYPE_PINNED_APP && model_->IsAppPinned(app_id)) ? REMOVABLE
1708 : DRAGGABLE;
James Cookb0bf8e82017-04-09 17:01:441709}
1710
1711bool ShelfView::SameDragType(ShelfItemType typea, ShelfItemType typeb) const {
James Cook49d30522020-04-22 13:45:371712 if (IsPinnedShelfItemType(typea) && IsPinnedShelfItemType(typeb))
1713 return true;
1714 if (typea == TYPE_UNDEFINED || typeb == TYPE_UNDEFINED) {
1715 NOTREACHED() << "ShelfItemType must be set.";
1716 return false;
James Cookb0bf8e82017-04-09 17:01:441717 }
James Cook49d30522020-04-22 13:45:371718 // Running app or dialog.
1719 return typea == typeb;
James Cookb0bf8e82017-04-09 17:01:441720}
1721
Alex Newcomerc8357182019-06-26 19:31:061722bool ShelfView::ShouldFocusOut(bool reverse, views::View* button) {
1723 // The logic here seems backwards, but is actually correct. For instance if
1724 // the ShelfView's internal focus cycling logic attemmpts to focus the first
Manu Corneta4814362019-08-09 22:09:301725 // child after hitting Tab, we intercept that and instead, advance through
1726 // to the status area.
Alex Newcomerc8357182019-06-26 19:31:061727 return (reverse && button == FindLastFocusableChild()) ||
1728 (!reverse && button == FindFirstFocusableChild());
1729}
1730
James Cookb0bf8e82017-04-09 17:01:441731std::pair<int, int> ShelfView::GetDragRange(int index) {
Ahmed Fakhryf5a2f522020-06-04 23:16:431732 DCHECK(base::Contains(visible_views_indices_, index));
Wen-Chien Wange17da90f2020-08-14 19:36:381733 const ShelfItem& dragged_item = model_->items()[index];
1734
1735 // If |drag_view_| is allowed to be dragged across the separator, return the
1736 // first and the last index of the |visible_views_indices_|.
1737 if (CanDragAcrossSeparator(drag_view_)) {
1738 return std::make_pair(visible_views_indices_[0],
1739 visible_views_indices_.back());
1740 }
Ahmed Fakhryf5a2f522020-06-04 23:16:431741
1742 int first = -1;
1743 int last = -1;
1744 for (int i : visible_views_indices_) {
Wen-Chien Wange17da90f2020-08-14 19:36:381745 if (SameDragType(model_->items()[i].type, dragged_item.type)) {
Ahmed Fakhryf5a2f522020-06-04 23:16:431746 if (first == -1)
1747 first = i;
1748 last = i;
1749 } else if (first != -1) {
1750 break;
1751 }
1752 }
1753 DCHECK_NE(first, -1);
1754 DCHECK_NE(last, -1);
1755
1756 // TODO(afakhry): Consider changing this when taking into account inactive
1757 // desks.
1758 return std::make_pair(first, last);
James Cookb0bf8e82017-04-09 17:01:441759}
1760
Wen-Chien Wange17da90f2020-08-14 19:36:381761bool ShelfView::ShouldUpdateDraggedViewPinStatus(int dragged_view_index) {
1762 if (!features::IsDragUnpinnedAppToPinEnabled())
1763 return false;
1764
1765 DCHECK(base::Contains(visible_views_indices_, dragged_view_index));
1766 bool is_moved_item_pinned =
1767 IsPinnedShelfItemType(model_->items()[dragged_view_index].type);
1768 if (separator_index_ == -1) {
1769 // If |separator_index_| equals to -1, all the apps in shelf are expected to
1770 // have the same pinned status.
1771 for (auto index : visible_views_indices_) {
1772 if (index != dragged_view_index) {
1773 // Return true if the pin status of the moved item is different from
1774 // others.
1775 return is_moved_item_pinned !=
1776 IsPinnedShelfItemType(model_->items()[index].type);
1777 }
1778 }
1779 return false;
1780 }
1781 // If the separator is shown, check whether the pin status of dragged item
1782 // matches the pin status implied by the dragged view position relative to the
1783 // separator.
1784 bool should_pinned_by_position = dragged_view_index <= separator_index_;
1785 return should_pinned_by_position != is_moved_item_pinned;
1786}
1787
1788bool ShelfView::CanDragAcrossSeparator(views::View* drag_view) const {
1789 if (!features::IsDragUnpinnedAppToPinEnabled())
1790 return false;
1791
1792 DCHECK(drag_view);
1793 // The dragged item is not allowed to be unpinned if |drag_view| is pinned by
1794 // policy, dragged from app list, or its item type is TYPE_BROWSER_SHORTCUT.
1795 // Therefore, the |drag_view| can not be dragged across the separator.
1796 bool can_change_pin_state =
1797 ShelfItemForView(drag_view)->type == TYPE_PINNED_APP ||
1798 ShelfItemForView(drag_view)->type == TYPE_APP;
1799 // Note that |drag_and_drop_shelf_id_| is set only when the current drag view
1800 // is from app list, which can not be dragged to the unpinned app side.
1801 return !ShelfItemForView(drag_view)->pinned_by_policy &&
1802 drag_and_drop_shelf_id_ == ShelfID() && can_change_pin_state;
1803}
1804
Andrew Xu5f02ed62020-02-28 18:11:531805void ShelfView::OnFadeInAnimationEnded() {
1806 // Call PreferredSizeChanged() to notify container to re-layout at the end
1807 // of fade-in animation.
Manu Cornet74c26c72020-03-10 21:21:131808 PreferredSizeChanged();
Andrew Xu5f02ed62020-02-28 18:11:531809}
1810
James Cookb0bf8e82017-04-09 17:01:441811void ShelfView::OnFadeOutAnimationEnded() {
Xiyuan Xia64d1f0a82020-10-30 04:12:521812 if (fade_out_animation_tracker_) {
1813 fade_out_animation_tracker_->Stop();
1814 fade_out_animation_tracker_.reset();
1815 }
1816
Andrew Xu7ac34cd2019-10-04 17:24:061817 // Call PreferredSizeChanged() to notify container to re-layout at the end
1818 // of removal animation.
Manu Cornet74c26c72020-03-10 21:21:131819 PreferredSizeChanged();
Andrew Xu7ac34cd2019-10-04 17:24:061820
James Cookb0bf8e82017-04-09 17:01:441821 AnimateToIdealBounds();
James Cookb0bf8e82017-04-09 17:01:441822}
1823
Alex Newcomer9c9cfa02018-05-31 17:29:121824gfx::Rect ShelfView::GetMenuAnchorRect(const views::View& source,
Alex Newcomercce253a2018-06-22 22:49:511825 const gfx::Point& location,
1826 bool context_menu) const {
Alex Newcomer1ef59e12018-11-27 03:00:101827 // Application menus for items are anchored on the icon bounds.
1828 if (ShelfItemForView(&source) || !context_menu)
1829 return source.GetBoundsInScreen();
1830
Manu Cornet80ad0702020-03-05 22:46:031831 const gfx::Rect shelf_bounds_in_screen = GetBoundsInScreen();
Alex Newcomera2f222a2018-03-20 20:08:351832 gfx::Point origin;
1833 switch (shelf_->alignment()) {
Manu Cornet44feda12019-12-03 21:02:481834 case ShelfAlignment::kBottom:
1835 case ShelfAlignment::kBottomLocked:
Alex Newcomercce253a2018-06-22 22:49:511836 origin = gfx::Point(location.x(), shelf_bounds_in_screen.y());
Alex Newcomera2f222a2018-03-20 20:08:351837 break;
Manu Cornet44feda12019-12-03 21:02:481838 case ShelfAlignment::kLeft:
Alex Newcomercce253a2018-06-22 22:49:511839 origin = gfx::Point(shelf_bounds_in_screen.right(), location.y());
Alex Newcomera2f222a2018-03-20 20:08:351840 break;
Manu Cornet44feda12019-12-03 21:02:481841 case ShelfAlignment::kRight:
Alex Newcomercce253a2018-06-22 22:49:511842 origin = gfx::Point(shelf_bounds_in_screen.x(), location.y());
Alex Newcomera2f222a2018-03-20 20:08:351843 break;
1844 }
Alex Newcomer1ef59e12018-11-27 03:00:101845 return gfx::Rect(origin, gfx::Size());
Alex Newcomera2f222a2018-03-20 20:08:351846}
1847
Manu Cornetf02f084d2019-02-06 02:50:381848void ShelfView::AnnounceShelfAlignment() {
Jan Wilken Dörrie85285b02021-03-11 23:38:471849 std::u16string announcement;
Manu Cornetf02f084d2019-02-06 02:50:381850 switch (shelf_->alignment()) {
Manu Cornet44feda12019-12-03 21:02:481851 case ShelfAlignment::kBottom:
1852 case ShelfAlignment::kBottomLocked:
Manu Cornetf02f084d2019-02-06 02:50:381853 announcement = l10n_util::GetStringUTF16(IDS_SHELF_ALIGNMENT_BOTTOM);
1854 break;
Manu Cornet44feda12019-12-03 21:02:481855 case ShelfAlignment::kLeft:
Manu Cornetf02f084d2019-02-06 02:50:381856 announcement = l10n_util::GetStringUTF16(IDS_SHELF_ALIGNMENT_LEFT);
1857 break;
Manu Cornet44feda12019-12-03 21:02:481858 case ShelfAlignment::kRight:
Manu Cornetf02f084d2019-02-06 02:50:381859 announcement = l10n_util::GetStringUTF16(IDS_SHELF_ALIGNMENT_RIGHT);
1860 break;
1861 }
1862 announcement_view_->GetViewAccessibility().OverrideName(announcement);
Manu Cornete793566a2019-11-21 19:14:031863 announcement_view_->NotifyAccessibilityEvent(ax::mojom::Event::kAlert,
1864 /*send_native_event=*/true);
Manu Cornetf02f084d2019-02-06 02:50:381865}
1866
1867void ShelfView::AnnounceShelfAutohideBehavior() {
Jan Wilken Dörrie85285b02021-03-11 23:38:471868 std::u16string announcement;
Manu Cornetf02f084d2019-02-06 02:50:381869 switch (shelf_->auto_hide_behavior()) {
Manu Cornetd5766412019-12-17 03:25:191870 case ShelfAutoHideBehavior::kAlways:
Manu Cornetf02f084d2019-02-06 02:50:381871 announcement = l10n_util::GetStringUTF16(IDS_SHELF_STATE_AUTO_HIDE);
1872 break;
Manu Cornetd5766412019-12-17 03:25:191873 case ShelfAutoHideBehavior::kNever:
Manu Cornetf02f084d2019-02-06 02:50:381874 announcement = l10n_util::GetStringUTF16(IDS_SHELF_STATE_ALWAYS_SHOWN);
1875 break;
Manu Cornetd5766412019-12-17 03:25:191876 case ShelfAutoHideBehavior::kAlwaysHidden:
Manu Cornetf02f084d2019-02-06 02:50:381877 announcement = l10n_util::GetStringUTF16(IDS_SHELF_STATE_ALWAYS_HIDDEN);
1878 break;
1879 }
1880 announcement_view_->GetViewAccessibility().OverrideName(announcement);
Manu Cornete793566a2019-11-21 19:14:031881 announcement_view_->NotifyAccessibilityEvent(ax::mojom::Event::kAlert,
1882 /*send_native_event=*/true);
1883}
1884
1885void ShelfView::AnnouncePinUnpinEvent(const ShelfItem& item, bool pinned) {
Jan Wilken Dörrie85285b02021-03-11 23:38:471886 std::u16string item_title =
Manu Cornete793566a2019-11-21 19:14:031887 item.title.empty()
1888 ? l10n_util::GetStringUTF16(IDS_SHELF_ITEM_GENERIC_NAME)
1889 : item.title;
Jan Wilken Dörrie85285b02021-03-11 23:38:471890 std::u16string announcement = l10n_util::GetStringFUTF16(
Manu Cornete793566a2019-11-21 19:14:031891 pinned ? IDS_SHELF_ITEM_WAS_PINNED : IDS_SHELF_ITEM_WAS_UNPINNED,
1892 item_title);
1893 announcement_view_->GetViewAccessibility().OverrideName(announcement);
1894 announcement_view_->NotifyAccessibilityEvent(ax::mojom::Event::kAlert,
1895 /*send_native_event=*/true);
Manu Cornetf02f084d2019-02-06 02:50:381896}
1897
Manu Cornet36f91b772020-03-18 15:14:051898void ShelfView::AnnounceSwapEvent(const ShelfItem& first_item,
1899 const ShelfItem& second_item) {
Jan Wilken Dörrie85285b02021-03-11 23:38:471900 std::u16string first_item_title =
Manu Cornet36f91b772020-03-18 15:14:051901 first_item.title.empty()
1902 ? l10n_util::GetStringUTF16(IDS_SHELF_ITEM_GENERIC_NAME)
1903 : first_item.title;
Jan Wilken Dörrie85285b02021-03-11 23:38:471904 std::u16string second_item_title =
Manu Cornet36f91b772020-03-18 15:14:051905 second_item.title.empty()
1906 ? l10n_util::GetStringUTF16(IDS_SHELF_ITEM_GENERIC_NAME)
1907 : second_item.title;
Jan Wilken Dörrie85285b02021-03-11 23:38:471908 std::u16string announcement = l10n_util::GetStringFUTF16(
Manu Cornet36f91b772020-03-18 15:14:051909 IDS_SHELF_ITEMS_WERE_SWAPPED, first_item_title, second_item_title);
1910 announcement_view_->GetViewAccessibility().OverrideName(announcement);
1911 announcement_view_->NotifyAccessibilityEvent(ax::mojom::Event::kAlert,
1912 /*send_native_event=*/true);
1913}
1914
James Cookb0bf8e82017-04-09 17:01:441915gfx::Rect ShelfView::GetBoundsForDragInsertInScreen() {
Avery Musbach22d527b2020-04-24 21:00:411916 const ScrollableShelfView* scrollable_shelf_view =
1917 shelf_->hotseat_widget()->scrollable_shelf_view();
1918 gfx::Rect bounds = scrollable_shelf_view->visible_space();
1919 views::View::ConvertRectToScreen(scrollable_shelf_view, &bounds);
1920 return bounds;
James Cookb0bf8e82017-04-09 17:01:441921}
1922
1923int ShelfView::CancelDrag(int modified_index) {
Avery Musbach5a8e6cf2019-01-15 00:46:571924 drag_scroll_dir_ = 0;
1925 scrolling_timer_.Stop();
1926 speed_up_drag_scrolling_.Stop();
1927
James Cookb0bf8e82017-04-09 17:01:441928 FinalizeRipOffDrag(true);
1929 if (!drag_view_)
1930 return modified_index;
1931 bool was_dragging = dragging();
1932 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1933 drag_pointer_ = NONE;
1934 drag_view_ = nullptr;
1935 if (drag_view_index == modified_index) {
1936 // The view that was being dragged is being modified. Don't do anything.
1937 return modified_index;
1938 }
1939 if (!was_dragging)
1940 return modified_index;
1941
1942 // Restore previous position, tracking the position of the modified view.
1943 bool at_end = modified_index == view_model_->view_size();
1944 views::View* modified_view = (modified_index >= 0 && !at_end)
1945 ? view_model_->view_at(modified_index)
1946 : nullptr;
1947 model_->Move(drag_view_index, start_drag_index_);
1948
1949 // If the modified view will be at the end of the list, return the new end of
1950 // the list.
1951 if (at_end)
1952 return view_model_->view_size();
1953 return modified_view ? view_model_->GetIndexOfView(modified_view) : -1;
1954}
1955
James Cookb0bf8e82017-04-09 17:01:441956void ShelfView::OnGestureEvent(ui::GestureEvent* event) {
Manu Cornet80ad0702020-03-05 22:46:031957 if (!ShouldHandleGestures(*event))
Andrew Xu6d4cbc22019-09-13 18:27:311958 return;
1959
1960 if (HandleGestureEvent(event))
1961 event->StopPropagation();
James Cookb0bf8e82017-04-09 17:01:441962}
1963
1964void ShelfView::ShelfItemAdded(int model_index) {
1965 {
1966 base::AutoReset<bool> cancelling_drag(&cancelling_drag_model_changed_,
1967 true);
1968 model_index = CancelDrag(model_index);
1969 }
Manu Cornet4a908de2018-04-09 04:32:271970 const ShelfItem& item(model_->items()[model_index]);
1971 views::View* view = CreateViewForItem(item);
James Cookb0bf8e82017-04-09 17:01:441972 // Hide the view, it'll be made visible when the animation is done. Using
1973 // opacity 0 here to avoid messing with CalculateIdealBounds which touches
1974 // the view's visibility.
1975 view->layer()->SetOpacity(0);
1976 view_model_->Add(view, model_index);
1977
Matthew Mourgos1b5f0272019-11-26 01:58:541978 // Add child view so it has the same ordering as in the |view_model_|.
Ahmed Fakhry039882f2020-06-16 21:56:211979 // Note: No need to call UpdateShelfItemViewsVisibility() here directly, since
1980 // it will be called by ScrollableShelfView::ViewHierarchyChanged() as a
1981 // result of the below call.
Matthew Mourgos1b5f0272019-11-26 01:58:541982 AddChildViewAt(view, model_index);
1983
James Cookb0bf8e82017-04-09 17:01:441984 // Give the button its ideal bounds. That way if we end up animating the
1985 // button before this animation completes it doesn't appear at some random
1986 // spot (because it was in the middle of animating from 0,0 0x0 to its
1987 // target).
Manu Cornet28b8eb082019-03-20 16:22:581988 CalculateIdealBounds();
James Cookb0bf8e82017-04-09 17:01:441989 view->SetBoundsRect(view_model_->ideal_bounds(model_index));
1990
Toni Barzic0ad32002020-06-23 06:20:251991 if (model_->is_current_mutation_user_triggered() &&
1992 drag_and_drop_shelf_id_ != item.id) {
Andrew Xu8582a1e22019-12-10 02:21:181993 view->ScrollViewToVisible();
Toni Barzic0ad32002020-06-23 06:20:251994 }
Andrew Xu8582a1e22019-12-10 02:21:181995
Manu Cornete0bf3712018-12-18 18:41:361996 // The first animation moves all the views to their target position. |view|
1997 // is hidden, so it visually appears as though we are providing space for
1998 // it. When done we'll fade the view in.
1999 AnimateToIdealBounds();
Ahmed Fakhryf5a2f522020-06-04 23:16:432000 DCHECK_LE(model_index, visible_views_indices_.back());
2001 bounds_animator_->SetAnimationDelegate(
2002 view, std::unique_ptr<gfx::AnimationDelegate>(
2003 new StartFadeAnimationDelegate(this, view)));
Manu Cornete793566a2019-11-21 19:14:032004
2005 if (model_->is_current_mutation_user_triggered() &&
2006 item.type == TYPE_PINNED_APP) {
2007 AnnouncePinUnpinEvent(item, /*pinned=*/true);
Matthew Mourgos1241a8a2021-03-05 18:11:522008 RecordPinUnpinUserAction(/*pinned=*/true);
Manu Cornete793566a2019-11-21 19:14:032009 }
James Cookb0bf8e82017-04-09 17:01:442010}
2011
2012void ShelfView::ShelfItemRemoved(int model_index, const ShelfItem& old_item) {
Alex Newcomer46af4c92018-05-29 22:26:142013 if (old_item.id == context_menu_id_ && shelf_menu_model_adapter_)
2014 shelf_menu_model_adapter_->Cancel();
khmel3e5c2a12017-10-16 18:07:082015
Andrew Xube1e23b2019-12-16 22:09:062016 // If std::move is not called on |view|, |view| will be deleted once out of
2017 // scope.
Matthew Mourgos1b5f0272019-11-26 01:58:542018 std::unique_ptr<views::View> view(view_model_->view_at(model_index));
khmel3e5c2a12017-10-16 18:07:082019 view_model_->Remove(model_index);
2020
James Cookb0bf8e82017-04-09 17:01:442021 {
2022 base::AutoReset<bool> cancelling_drag(&cancelling_drag_model_changed_,
2023 true);
khmel3e5c2a12017-10-16 18:07:082024 CancelDrag(-1);
James Cookb0bf8e82017-04-09 17:01:442025 }
James Cookb0bf8e82017-04-09 17:01:442026
Matthew Mourgos1b5f0272019-11-26 01:58:542027 if (view.get() == shelf_->tooltip()->GetCurrentAnchorView())
2028 shelf_->tooltip()->Close();
2029
Toni Barzic8c2b7e902020-01-29 00:26:482030 if (view->GetVisible() && view->layer()->opacity() > 0.0f) {
Ahmed Fakhry039882f2020-06-16 21:56:212031 UpdateShelfItemViewsVisibility();
Andrew Xu47aa4072019-12-22 04:35:202032
Xiyuan Xia64d1f0a82020-10-30 04:12:522033 // There could be multiple fade out animations running. Only start
2034 // tracking for the first one.
2035 if (!fade_out_animation_tracker_) {
2036 fade_out_animation_tracker_.emplace(
2037 GetWidget()->GetCompositor()->RequestNewThroughputTracker());
2038 fade_out_animation_tracker_->Start(metrics_util::ForSmoothness(
2039 base::BindRepeating(&ReportFadeOutAnimationSmoothness)));
2040 }
2041
James Cookb0bf8e82017-04-09 17:01:442042 // The first animation fades out the view. When done we'll animate the rest
2043 // of the views to their target location.
Matthew Mourgos1b5f0272019-11-26 01:58:542044 bounds_animator_->AnimateViewTo(view.get(), view->bounds());
James Cookb0bf8e82017-04-09 17:01:442045 bounds_animator_->SetAnimationDelegate(
Matthew Mourgos1b5f0272019-11-26 01:58:542046 view.get(), std::unique_ptr<gfx::AnimationDelegate>(
2047 new FadeOutAnimationDelegate(this, std::move(view))));
James Cookb0bf8e82017-04-09 17:01:442048 } else {
Andrew Xuc4c470a32019-12-19 19:19:052049 // Ensures that |view| is not used after destruction.
2050 StopAnimatingViewIfAny(view.get());
2051
2052 // Removes |view| to trigger ViewHierarchyChanged function in the parent
2053 // view if any.
2054 view.reset();
2055
Andrew Xue9e025c2019-10-10 17:38:012056 // If there is no fade out animation, notify the parent view of the
2057 // changed size before bounds animations start.
Manu Cornet74c26c72020-03-10 21:21:132058 PreferredSizeChanged();
Andrew Xue9e025c2019-10-10 17:38:012059
James Cookb0bf8e82017-04-09 17:01:442060 // We don't need to show a fade out animation for invisible |view|. When an
2061 // item is ripped out from the shelf, its |view| is already invisible.
2062 AnimateToIdealBounds();
2063 }
2064
Manu Cornete793566a2019-11-21 19:14:032065 if (model_->is_current_mutation_user_triggered() &&
2066 old_item.type == TYPE_PINNED_APP) {
2067 AnnouncePinUnpinEvent(old_item, /*pinned=*/false);
Matthew Mourgos1241a8a2021-03-05 18:11:522068 RecordPinUnpinUserAction(/*pinned=*/false);
Manu Cornete793566a2019-11-21 19:14:032069 }
James Cookb0bf8e82017-04-09 17:01:442070}
2071
2072void ShelfView::ShelfItemChanged(int model_index, const ShelfItem& old_item) {
msw5138f3d2017-04-20 00:22:072073 // Bail if the view and shelf sizes do not match. ShelfItemChanged may be
2074 // called here before ShelfItemAdded, due to ChromeLauncherController's
2075 // item initialization, which calls SetItem during ShelfItemAdded.
2076 if (static_cast<int>(model_->items().size()) != view_model_->view_size())
2077 return;
2078
Ahmed Fakhry039882f2020-06-16 21:56:212079 const ShelfItem& item = model_->items()[model_index];
2080
2081 // If there's a change in the item's active desk, perform the update at the
2082 // end of this function in order to guarantee that both |model_| and
2083 // |view_model_| are consistent if there are other changes in the item.
2084 base::ScopedClosureRunner run_at_scope_exit;
2085 if (old_item.is_on_active_desk != item.is_on_active_desk) {
2086 run_at_scope_exit.ReplaceClosure(base::BindOnce(
2087 &ShelfView::ShelfItemsUpdatedForDeskChange, base::Unretained(this)));
2088 }
2089
James Cookb0bf8e82017-04-09 17:01:442090 if (old_item.type != item.type) {
2091 // Type changed, swap the views.
2092 model_index = CancelDrag(model_index);
2093 std::unique_ptr<views::View> old_view(view_model_->view_at(model_index));
2094 bounds_animator_->StopAnimatingView(old_view.get());
2095 // Removing and re-inserting a view in our view model will strip the ideal
2096 // bounds from the item. To avoid recalculation of everything the bounds
2097 // get remembered and restored after the insertion to the previous value.
2098 gfx::Rect old_ideal_bounds = view_model_->ideal_bounds(model_index);
2099 view_model_->Remove(model_index);
2100 views::View* new_view = CreateViewForItem(item);
Ahmed Fakhry4fd7fbe2020-06-18 23:03:302101 // The view must be added to the |view_model_| before it's added as a child
2102 // so that the model is consistent when UpdateShelfItemViewsVisibility() is
2103 // called as a result the hierarchy changes caused by AddChildView(). See
2104 // ScrollableShelfView::ViewHierarchyChanged().
James Cookb0bf8e82017-04-09 17:01:442105 view_model_->Add(new_view, model_index);
Ahmed Fakhry4fd7fbe2020-06-18 23:03:302106 AddChildView(new_view);
James Cookb0bf8e82017-04-09 17:01:442107 view_model_->set_ideal_bounds(model_index, old_ideal_bounds);
Manu Cornet4a908de2018-04-09 04:32:272108
Sammie Quoncdb927d52020-05-01 19:25:592109 bounds_animator_->StopAnimatingView(new_view);
James Cookb0bf8e82017-04-09 17:01:442110 new_view->SetBoundsRect(old_view->bounds());
Manu Cornet80ad0702020-03-05 22:46:032111 bounds_animator_->AnimateViewTo(new_view, old_ideal_bounds);
Manu Cornet06a752d2018-08-24 17:10:582112
2113 // If an item is being pinned or unpinned, show the new status of the
2114 // shelf immediately so that the separator gets drawn as needed.
Manu Cornete793566a2019-11-21 19:14:032115 if (old_item.type == TYPE_PINNED_APP || item.type == TYPE_PINNED_APP) {
Matthew Mourgos1241a8a2021-03-05 18:11:522116 if (model_->is_current_mutation_user_triggered()) {
Manu Cornete793566a2019-11-21 19:14:032117 AnnouncePinUnpinEvent(old_item, item.type == TYPE_PINNED_APP);
Matthew Mourgos1241a8a2021-03-05 18:11:522118 RecordPinUnpinUserAction(item.type == TYPE_PINNED_APP);
2119 }
Manu Cornet06a752d2018-08-24 17:10:582120 AnimateToIdealBounds();
Manu Cornete793566a2019-11-21 19:14:032121 }
James Cookb0bf8e82017-04-09 17:01:442122 return;
2123 }
2124
2125 views::View* view = view_model_->view_at(model_index);
2126 switch (item.type) {
James Cookb0bf8e82017-04-09 17:01:442127 case TYPE_PINNED_APP:
2128 case TYPE_BROWSER_SHORTCUT:
2129 case TYPE_APP:
2130 case TYPE_DIALOG: {
Manu Cornete363aec2019-01-13 13:07:072131 CHECK_EQ(ShelfAppButton::kViewClassName, view->GetClassName());
2132 ShelfAppButton* button = static_cast<ShelfAppButton*>(view);
2133 button->ReflectItemStatus(item);
James Cookb0bf8e82017-04-09 17:01:442134 button->SetImage(item.image);
Matthew Mourgos842068e2021-01-27 19:48:102135 button->SetNotificationBadgeColor(item.notification_badge_color);
James Cookb0bf8e82017-04-09 17:01:442136 button->SchedulePaint();
2137 break;
2138 }
James Cook49d30522020-04-22 13:45:372139 case TYPE_UNDEFINED:
James Cookb0bf8e82017-04-09 17:01:442140 break;
2141 }
2142}
2143
Ahmed Fakhry039882f2020-06-16 21:56:212144void ShelfView::ShelfItemsUpdatedForDeskChange() {
2145 DCHECK(features::IsPerDeskShelfEnabled());
2146
2147 // The order here matters, since switching/removing desks, or moving windows
2148 // between desks will affect shelf items' visibility, we need to update the
2149 // visibility of the views first before we layout.
2150 UpdateShelfItemViewsVisibility();
2151 // Signal to the parent ScrollableShelfView so that it can recenter the items
2152 // after their visibility have been updated (via
2153 // `UpdateAvailableSpaceAndScroll()`).
2154 PreferredSizeChanged();
2155 LayoutToIdealBounds();
2156}
2157
James Cookb0bf8e82017-04-09 17:01:442158void ShelfView::ShelfItemMoved(int start_index, int target_index) {
2159 view_model_->Move(start_index, target_index);
Matthew Mourgos1b5f0272019-11-26 01:58:542160
2161 // Reorder the child view to be in the same order as in the |view_model_|.
2162 ReorderChildView(view_model_->view_at(target_index), target_index);
2163 NotifyAccessibilityEvent(ax::mojom::Event::kChildrenChanged,
2164 true /* send_native_event */);
2165
James Cookb0bf8e82017-04-09 17:01:442166 // When cancelling a drag due to a shelf item being added, the currently
2167 // dragged item is moved back to its initial position. AnimateToIdealBounds
2168 // will be called again when the new item is added to the |view_model_| but
2169 // at this time the |view_model_| is inconsistent with the |model_|.
2170 if (!cancelling_drag_model_changed_)
2171 AnimateToIdealBounds();
2172}
2173
msw19b30c2c2017-06-01 03:21:402174void ShelfView::ShelfItemDelegateChanged(const ShelfID& id,
khmel1240ee82017-10-05 23:48:182175 ShelfItemDelegate* old_delegate,
Vladislav Kaznacheev4f25e9582019-04-26 18:53:442176 ShelfItemDelegate* delegate) {
2177 if (id == context_menu_id_ && shelf_menu_model_adapter_)
2178 shelf_menu_model_adapter_->Cancel();
2179}
msw19b30c2c2017-06-01 03:21:402180
Manu Cornet389f2902018-08-23 00:43:552181void ShelfView::ShelfItemStatusChanged(const ShelfID& id) {
Joel Hockey784a8322020-05-20 21:19:252182 scoped_display_for_new_windows_.reset();
Sammie Quone7c1da12020-05-13 23:20:372183
Manu Cornet389f2902018-08-23 00:43:552184 int index = model_->ItemIndexByID(id);
2185 if (index < 0)
2186 return;
2187
2188 const ShelfItem item = model_->items()[index];
Alex Newcomer43e6ccefe2019-10-09 21:43:592189 ShelfAppButton* button = GetShelfAppButton(id);
Manu Cornete363aec2019-01-13 13:07:072190 button->ReflectItemStatus(item);
Manu Cornet389f2902018-08-23 00:43:552191 button->SchedulePaint();
2192}
2193
Avery Musbach709f5212020-04-23 18:21:582194void ShelfView::ShelfItemRippedOff() {
2195 // On the display where the drag started, there is nothing to do.
2196 if (dragging())
2197 return;
2198 // When a dragged item has been ripped off the shelf, it is moved to the end.
2199 // Now we need to hide it.
2200 view_model_->view_at(model_->item_count() - 1)->layer()->SetOpacity(0.f);
2201}
2202
2203void ShelfView::ShelfItemReturnedFromRipOff(int index) {
2204 // On the display where the drag started, there is nothing to do.
2205 if (dragging())
2206 return;
2207 // Show the item and prevent it from animating into place from the position
2208 // where it was sitting with zero opacity.
2209 views::View* view = view_model_->view_at(index);
Avery Musbach1dfc2952020-05-07 20:44:092210 const gfx::Rect bounds = bounds_animator_->GetTargetBounds(view);
Avery Musbach709f5212020-04-23 18:21:582211 bounds_animator_->StopAnimatingView(view);
Avery Musbach1dfc2952020-05-07 20:44:092212 view->SetBoundsRect(bounds);
Avery Musbach709f5212020-04-23 18:21:582213 view->layer()->SetOpacity(1.f);
2214}
2215
Sammie Quonf6b30f92020-01-07 00:35:132216void ShelfView::OnShelfAlignmentChanged(aura::Window* root_window,
2217 ShelfAlignment old_alignment) {
Manu Cornetf02f084d2019-02-06 02:50:382218 LayoutToIdealBounds();
Ahmed Fakhryf5a2f522020-06-04 23:16:432219 for (const auto& visible_index : visible_views_indices_)
2220 view_model_->view_at(visible_index)->Layout();
Manu Cornetf02f084d2019-02-06 02:50:382221
Manu Cornetf02f084d2019-02-06 02:50:382222 AnnounceShelfAlignment();
2223}
2224
2225void ShelfView::OnShelfAutoHideBehaviorChanged(aura::Window* root_window) {
2226 AnnounceShelfAutohideBehavior();
2227}
2228
Mike Wasserman3bfabb192019-05-17 01:08:342229void ShelfView::AfterItemSelected(const ShelfItem& item,
2230 views::Button* sender,
2231 std::unique_ptr<ui::Event> event,
2232 views::InkDrop* ink_drop,
2233 ShelfAction action,
2234 ShelfItemDelegate::AppMenuItems menu_items) {
Mike Wasserman8f86c042018-12-05 22:47:412235 item_awaiting_response_ = ShelfID();
James Cookb0bf8e82017-04-09 17:01:442236 shelf_button_pressed_metric_tracker_.ButtonPressed(*event, sender, action);
2237
Matthew Mourgos175a5cc52019-04-18 16:33:092238 // Record AppList metric for any action considered an app launch.
Michael Giuffrida8a551bc2019-08-14 14:28:392239 if (action == SHELF_ACTION_NEW_WINDOW_CREATED ||
2240 action == SHELF_ACTION_WINDOW_ACTIVATED) {
Ana Salazarc0ab0462020-04-01 19:12:592241 Shell::Get()->app_list_controller()->RecordShelfAppLaunched();
Matthew Mourgos175a5cc52019-04-18 16:33:092242 }
2243
James Cookb0bf8e82017-04-09 17:01:442244 // The app list handles its own ink drop effect state changes.
Avery Musbach62773662019-01-15 00:16:562245 if (action == SHELF_ACTION_APP_LIST_DISMISSED) {
2246 ink_drop->SnapToActivated();
2247 ink_drop->AnimateToState(views::InkDropState::HIDDEN);
Toni Barzicb66375b92020-01-22 21:12:422248 } else if (action != SHELF_ACTION_APP_LIST_SHOWN && !dragging()) {
2249 if (action != SHELF_ACTION_NEW_WINDOW_CREATED && menu_items.size() > 1 &&
2250 !dragging()) {
2251 // Show the app menu with 2 or more items, if no window was created. The
2252 // menu is not shown in case item drag started while the selection request
2253 // was in progress.
James Cookb0bf8e82017-04-09 17:01:442254 ink_drop->AnimateToState(views::InkDropState::ACTIVATED);
2255 context_menu_id_ = item.id;
Mitsuru Oshima04b54d02017-10-09 14:22:452256 ShowMenu(std::make_unique<ShelfApplicationMenuModel>(
Mike Wasserman3bfabb192019-05-17 01:08:342257 item.title, std::move(menu_items),
James Cookb0bf8e82017-04-09 17:01:442258 model_->GetShelfItemDelegate(item.id)),
Min Chen2be72dc2018-12-03 23:37:192259 sender, gfx::Point(), /*context_menu=*/false,
Alex Newcomer32d2fdd2018-03-29 23:19:042260 ui::GetMenuSourceTypeForEvent(*event));
Min Chen2be72dc2018-12-03 23:37:192261 shelf_->UpdateVisibilityState();
James Cookb0bf8e82017-04-09 17:01:442262 } else {
2263 ink_drop->AnimateToState(views::InkDropState::ACTION_TRIGGERED);
2264 }
2265 }
Alex Newcomer43e6ccefe2019-10-09 21:43:592266 shelf_->shelf_layout_manager()->OnShelfItemSelected(action);
James Cookb0bf8e82017-04-09 17:01:442267}
2268
Mike Wasserman3bfabb192019-05-17 01:08:342269void ShelfView::ShowShelfContextMenu(
Michael Wasserman47bf1782017-08-18 23:17:102270 const ShelfID& shelf_id,
2271 const gfx::Point& point,
MinChen63a7bcc2017-09-20 18:51:002272 views::View* source,
Michael Wasserman47bf1782017-08-18 23:17:102273 ui::MenuSourceType source_type,
Mike Wasserman3bfabb192019-05-17 01:08:342274 std::unique_ptr<ui::SimpleMenuModel> model) {
Michael Wasserman47bf1782017-08-18 23:17:102275 context_menu_id_ = shelf_id;
Mike Wasserman3bfabb192019-05-17 01:08:342276 if (!model) {
2277 const int64_t display_id = GetDisplayIdForView(this);
2278 model = std::make_unique<ShelfContextMenuModel>(nullptr, display_id);
2279 }
2280 ShowMenu(std::move(model), source, point, /*context_menu=*/true, source_type);
Michael Wasserman47bf1782017-08-18 23:17:102281}
2282
Alex Newcomer46af4c92018-05-29 22:26:142283void ShelfView::ShowMenu(std::unique_ptr<ui::SimpleMenuModel> menu_model,
James Cookb0bf8e82017-04-09 17:01:442284 views::View* source,
2285 const gfx::Point& click_point,
2286 bool context_menu,
Alex Newcomer32d2fdd2018-03-29 23:19:042287 ui::MenuSourceType source_type) {
Mike Wasserman8f86c042018-12-05 22:47:412288 // Delayed callbacks to show context and application menus may conflict; hide
2289 // the old menu before showing a new menu in that case.
2290 if (IsShowingMenu())
2291 shelf_menu_model_adapter_->Cancel();
2292
2293 item_awaiting_response_ = ShelfID();
Alex Newcomera2f222a2018-03-20 20:08:352294 if (menu_model->GetItemCount() == 0)
2295 return;
Alex Newcomer32d2fdd2018-03-29 23:19:042296 menu_owner_ = source;
James Cookb0bf8e82017-04-09 17:01:442297
2298 closing_event_time_ = base::TimeTicks();
Alex Newcomer46af4c92018-05-29 22:26:142299
2300 // NOTE: If you convert to HAS_MNEMONICS be sure to update menu building code.
Alex Newcomer1ef59e12018-11-27 03:00:102301 int run_types = views::MenuRunner::USE_TOUCHABLE_LAYOUT;
Alex Newcomerac50b1f2018-05-25 23:37:072302 if (context_menu) {
minchf6b2be32017-05-11 20:54:012303 run_types |=
2304 views::MenuRunner::CONTEXT_MENU | views::MenuRunner::FIXED_ANCHOR;
Alex Newcomerac50b1f2018-05-25 23:37:072305 }
Alex Newcomer46af4c92018-05-29 22:26:142306
Alex Newcomerac50b1f2018-05-25 23:37:072307 const ShelfItem* item = ShelfItemForView(source);
Alex Newcomera2f222a2018-03-20 20:08:352308 // Only selected shelf items with context menu opened can be dragged.
Alex Newcomerd6ccf922017-12-15 22:55:162309 if (context_menu && item && ShelfButtonIsInDrag(item->type, source) &&
MinChen2a4edb32017-10-12 22:24:232310 source_type == ui::MenuSourceType::MENU_SOURCE_TOUCH) {
MinChen63a7bcc2017-09-20 18:51:002311 run_types |= views::MenuRunner::SEND_GESTURE_EVENTS_TO_OWNER;
2312 }
2313
Alex Newcomer46af4c92018-05-29 22:26:142314 shelf_menu_model_adapter_ = std::make_unique<ShelfMenuModelAdapter>(
2315 item ? item->id.app_id : std::string(), std::move(menu_model), source,
2316 source_type,
Matthew Mourgos3850cda2019-04-26 21:21:412317 base::BindOnce(&ShelfView::OnMenuClosed, base::Unretained(this), source),
Ahmed Fakhry1b6ea0b2020-07-09 01:45:242318 IsTabletModeEnabled(),
2319 /*for_application_menu_items*/ !context_menu);
Alex Newcomercce253a2018-06-22 22:49:512320 shelf_menu_model_adapter_->Run(
2321 GetMenuAnchorRect(*source, click_point, context_menu),
Hwanseung Lee26c56f12019-03-26 21:10:452322 shelf_->IsHorizontalAlignment() ? views::MenuAnchorPosition::kBubbleAbove
2323 : views::MenuAnchorPosition::kBubbleLeft,
Alex Newcomer1ef59e12018-11-27 03:00:102324 run_types);
Andrew Xu75f51e22020-05-19 23:31:122325
2326 if (!context_menu_shown_callback_.is_null())
2327 context_menu_shown_callback_.Run();
James Cookb0bf8e82017-04-09 17:01:442328}
2329
Alex Newcomer32d2fdd2018-03-29 23:19:042330void ShelfView::OnMenuClosed(views::View* source) {
2331 menu_owner_ = nullptr;
msw84b8a5f2017-05-05 00:13:362332 context_menu_id_ = ShelfID();
James Cookb0bf8e82017-04-09 17:01:442333
Alex Newcomer46af4c92018-05-29 22:26:142334 closing_event_time_ = shelf_menu_model_adapter_->GetClosingEventTime();
James Cookb0bf8e82017-04-09 17:01:442335
Alex Newcomer32d2fdd2018-03-29 23:19:042336 const ShelfItem* item = ShelfItemForView(source);
Manu Cornetff12bb12019-06-20 16:52:062337 if (item)
Manu Cornete363aec2019-01-13 13:07:072338 static_cast<ShelfAppButton*>(source)->OnMenuClosed();
James Cookb0bf8e82017-04-09 17:01:442339
Alex Newcomer46af4c92018-05-29 22:26:142340 shelf_menu_model_adapter_.reset();
James Cookb0bf8e82017-04-09 17:01:442341
Min Chen8292d612018-11-20 23:11:092342 const bool is_in_drag = item && ShelfButtonIsInDrag(item->type, source);
2343 // Update the shelf visibility since auto-hide or alignment might have
2344 // changes, but don't update if shelf item is being dragged. Since shelf
2345 // should be kept as visible during shelf item drag even menu is closed.
2346 if (!is_in_drag)
2347 shelf_->UpdateVisibilityState();
James Cookb0bf8e82017-04-09 17:01:442348}
2349
2350void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) {
James Cook840177e2017-05-25 02:20:012351 shelf_->NotifyShelfIconPositionsChanged();
Andrew Xu7ac34cd2019-10-04 17:24:062352
2353 // Do not call PreferredSizeChanged() so that container does not re-layout
2354 // during the bounds animation.
James Cookb0bf8e82017-04-09 17:01:442355}
2356
2357void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
Sammie Quona87fc322017-11-10 17:43:372358 shelf_->set_is_tablet_mode_animation_running(false);
Sammie Quonbbc88092017-08-01 15:06:112359
Xiyuan Xia64d1f0a82020-10-30 04:12:522360 if (move_animation_tracker_) {
2361 move_animation_tracker_->Stop();
2362 move_animation_tracker_.reset();
2363 }
2364
James Cookb0bf8e82017-04-09 17:01:442365 if (snap_back_from_rip_off_view_ && animator == bounds_animator_.get()) {
2366 if (!animator->IsAnimating(snap_back_from_rip_off_view_)) {
Manu Cornete363aec2019-01-13 13:07:072367 // Coming here the animation of the ShelfAppButton is finished and the
James Cookb0bf8e82017-04-09 17:01:442368 // previously hidden status can be shown again. Since the button itself
2369 // might have gone away or changed locations we check that the button
2370 // is still in the shelf and show its status again.
sangwoofdbca1f2020-01-03 03:25:072371 const auto& entries = view_model_->entries();
2372 const auto iter = std::find_if(
2373 entries.begin(), entries.end(), [this](const auto& entry) {
2374 return entry.view == snap_back_from_rip_off_view_;
2375 });
2376 if (iter != entries.end())
2377 snap_back_from_rip_off_view_->ClearState(ShelfAppButton::STATE_HIDDEN);
2378
James Cookb0bf8e82017-04-09 17:01:442379 snap_back_from_rip_off_view_ = nullptr;
2380 }
2381 }
2382}
2383
2384bool ShelfView::IsRepostEvent(const ui::Event& event) {
2385 if (closing_event_time_.is_null())
2386 return false;
2387
2388 // If the current (press down) event is a repost event, the time stamp of
2389 // these two events should be the same.
2390 return closing_event_time_ == event.time_stamp();
2391}
2392
2393const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const {
2394 const int view_index = view_model_->GetIndexOfView(view);
2395 return (view_index < 0) ? nullptr : &(model_->items()[view_index]);
2396}
2397
2398int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const {
2399 const gfx::Rect bounds = GetBoundsInScreen();
James Cook840177e2017-05-25 02:20:012400 int distance = shelf_->SelectValueForShelfAlignment(
James Cookb0bf8e82017-04-09 17:01:442401 bounds.y() - coordinate.y(), coordinate.x() - bounds.right(),
2402 bounds.x() - coordinate.x());
2403 return distance > 0 ? distance : 0;
2404}
2405
minch46d21232017-05-03 19:12:252406bool ShelfView::CanPrepareForDrag(Pointer pointer,
2407 const ui::LocatedEvent& event) {
2408 // Bail if dragging has already begun, or if no item has been pressed.
2409 if (dragging() || !drag_view_)
2410 return false;
2411
2412 // Dragging only begins once the pointer has travelled a minimum distance.
2413 if ((std::abs(event.x() - drag_origin_.x()) < kMinimumDragDistance) &&
2414 (std::abs(event.y() - drag_origin_.y()) < kMinimumDragDistance)) {
2415 return false;
2416 }
2417
minch46d21232017-05-03 19:12:252418 return true;
2419}
2420
Weidong Guo6a3c8d72018-09-18 18:39:232421void ShelfView::SetDragImageBlur(const gfx::Size& size, int blur_radius) {
Allen Bauer35d4e722020-06-16 22:47:562422 DragImageView* drag_image = GetDragImage();
2423 drag_image->SetPaintToLayer();
2424 drag_image->layer()->SetFillsBoundsOpaquely(false);
Jun Mukaid49590e2019-06-28 18:02:522425 const uint32_t radius = std::round(size.width() / 2.f);
Allen Bauer35d4e722020-06-16 22:47:562426 drag_image->layer()->SetRoundedCornerRadius({radius, radius, radius, radius});
2427 drag_image->layer()->SetBackgroundBlur(blur_radius);
Weidong Guo6a3c8d72018-09-18 18:39:232428}
2429
Andrew Xu4ac14692019-08-10 02:07:282430bool ShelfView::ShouldHandleGestures(const ui::GestureEvent& event) const {
2431 if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN) {
2432 float x_offset = event.details().scroll_x_hint();
2433 float y_offset = event.details().scroll_y_hint();
2434 if (!shelf_->IsHorizontalAlignment())
2435 std::swap(x_offset, y_offset);
2436
2437 return std::abs(x_offset) < std::abs(y_offset);
2438 }
2439
2440 return true;
2441}
2442
Jan Wilken Dörrie85285b02021-03-11 23:38:472443std::u16string ShelfView::GetTitleForChildView(const views::View* view) const {
Andrew Xubbdc1422019-09-12 01:52:432444 const ShelfItem* item = ShelfItemForView(view);
Jan Wilken Dörrie85285b02021-03-11 23:38:472445 return item ? item->title : std::u16string();
Andrew Xubbdc1422019-09-12 01:52:432446}
2447
Ahmed Fakhry039882f2020-06-16 21:56:212448void ShelfView::UpdateShelfItemViewsVisibility() {
Ahmed Fakhryf5a2f522020-06-04 23:16:432449 visible_views_indices_.clear();
2450 for (int i = 0; i < view_model_->view_size(); ++i) {
Ahmed Fakhry039882f2020-06-16 21:56:212451 View* view = view_model_->view_at(i);
2452 // To receive drag event continuously from |drag_view_| during the dragging
2453 // off from the shelf, don't make |drag_view_| invisible. It will be
2454 // eventually invisible and removed from the |view_model_| by
2455 // FinalizeRipOffDrag().
2456 const bool has_to_show = dragged_off_shelf_ && view == drag_view();
2457 const bool is_visible = has_to_show || IsItemVisible(model()->items()[i]);
2458 view->SetVisible(is_visible);
2459
2460 if (is_visible)
2461 visible_views_indices_.push_back(i);
Ahmed Fakhryf5a2f522020-06-04 23:16:432462 }
Andrew Xufcd04ed2019-09-23 20:33:252463}
2464
Joel Hockey784a8322020-05-20 21:19:252465void ShelfView::DestroyScopedDisplay() {
2466 scoped_display_for_new_windows_.reset();
Sammie Quone7c1da12020-05-13 23:20:372467}
2468
Andrew Xud5bc3b6a2020-06-04 23:04:042469int ShelfView::CalculateAppIconsLayoutOffset() const {
2470 const ScrollableShelfView* scrollable_shelf_view =
2471 shelf_->hotseat_widget()->scrollable_shelf_view();
Andrew Xu2dab9e612020-06-16 19:41:552472 const gfx::Insets& edge_padding_insets =
2473 scrollable_shelf_view->edge_padding_insets();
Andrew Xud5bc3b6a2020-06-04 23:04:042474
Andrew Xud763f7e02020-06-23 21:33:232475 return shelf_->IsHorizontalAlignment() ? edge_padding_insets.left()
Andrew Xu2dab9e612020-06-16 19:41:552476 : edge_padding_insets.top();
Andrew Xud5bc3b6a2020-06-04 23:04:042477}
2478
Allen Bauer35d4e722020-06-16 22:47:562479DragImageView* ShelfView::GetDragImage() {
2480 return static_cast<DragImageView*>(drag_image_widget_->GetContentsView());
2481}
2482
Ahmed Fakhry4fd7fbe2020-06-18 23:03:302483gfx::Rect ShelfView::GetChildViewTargetMirroredBounds(
2484 const views::View* child) const {
2485 DCHECK_EQ(this, child->parent());
2486 return GetMirroredRect(bounds_animator_->GetTargetBounds(child));
2487}
2488
James Cookb0bf8e82017-04-09 17:01:442489} // namespace ash