1 // 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>
9 #include <utility>
10 
11 #include "ash/app_list/app_list_controller_impl.h"
12 #include "ash/drag_drop/drag_image_view.h"
13 #include "ash/keyboard/keyboard_util.h"
14 #include "ash/keyboard/ui/keyboard_ui_controller.h"
15 #include "ash/metrics/user_metrics_recorder.h"
16 #include "ash/public/cpp/ash_constants.h"
17 #include "ash/public/cpp/ash_features.h"
18 #include "ash/public/cpp/metrics_util.h"
19 #include "ash/public/cpp/shelf_model.h"
20 #include "ash/public/cpp/shelf_types.h"
21 #include "ash/public/cpp/window_properties.h"
22 #include "ash/screen_util.h"
23 #include "ash/session/session_controller_impl.h"
24 #include "ash/shelf/hotseat_widget.h"
25 #include "ash/shelf/scrollable_shelf_view.h"
26 #include "ash/shelf/shelf.h"
27 #include "ash/shelf/shelf_app_button.h"
28 #include "ash/shelf/shelf_application_menu_model.h"
29 #include "ash/shelf/shelf_button.h"
30 #include "ash/shelf/shelf_context_menu_model.h"
31 #include "ash/shelf/shelf_controller.h"
32 #include "ash/shelf/shelf_focus_cycler.h"
33 #include "ash/shelf/shelf_layout_manager.h"
34 #include "ash/shelf/shelf_menu_model_adapter.h"
35 #include "ash/shelf/shelf_tooltip_manager.h"
36 #include "ash/shelf/shelf_widget.h"
37 #include "ash/shell.h"
38 #include "ash/shell_delegate.h"
39 #include "ash/strings/grit/ash_strings.h"
40 #include "ash/style/ash_color_provider.h"
41 #include "ash/system/status_area_widget.h"
42 #include "ash/wm/desks/desks_util.h"
43 #include "ash/wm/mru_window_tracker.h"
44 #include "ash/wm/tablet_mode/tablet_mode_controller.h"
45 #include "ash/wm/window_util.h"
46 #include "base/auto_reset.h"
47 #include "base/bind.h"
48 #include "base/callback_helpers.h"
49 #include "base/containers/adapters.h"
50 #include "base/metrics/histogram_functions.h"
51 #include "base/metrics/histogram_macros.h"
52 #include "base/numerics/ranges.h"
53 #include "base/stl_util.h"
54 #include "base/strings/utf_string_conversions.h"
55 #include "base/timer/timer.h"
56 #include "chromeos/constants/chromeos_switches.h"
57 #include "components/account_id/account_id.h"
58 #include "components/services/app_service/public/cpp/app_registry_cache_wrapper.h"
59 #include "components/services/app_service/public/mojom/types.mojom.h"
60 #include "ui/accessibility/ax_node_data.h"
61 #include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
62 #include "ui/base/l10n/l10n_util.h"
63 #include "ui/base/models/simple_menu_model.h"
64 #include "ui/base/ui_base_features.h"
65 #include "ui/compositor/animation_throughput_reporter.h"
66 #include "ui/compositor/layer.h"
67 #include "ui/compositor/layer_animation_observer.h"
68 #include "ui/compositor/layer_animator.h"
69 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
70 #include "ui/display/scoped_display_for_new_windows.h"
71 #include "ui/events/event_utils.h"
72 #include "ui/gfx/canvas.h"
73 #include "ui/gfx/geometry/point.h"
74 #include "ui/strings/grit/ui_strings.h"
75 #include "ui/views/animation/bounds_animator.h"
76 #include "ui/views/animation/ink_drop.h"
77 #include "ui/views/border.h"
78 #include "ui/views/controls/button/button.h"
79 #include "ui/views/controls/menu/menu_model_adapter.h"
80 #include "ui/views/controls/menu/menu_runner.h"
81 #include "ui/views/controls/separator.h"
82 #include "ui/views/focus/focus_search.h"
83 #include "ui/views/view_model.h"
84 #include "ui/views/view_model_utils.h"
85 #include "ui/views/widget/widget.h"
86 #include "ui/wm/core/coordinate_conversion.h"
87 
88 using gfx::Animation;
89 using views::View;
90 
91 namespace ash {
92 
93 // The distance of the cursor from the outer rim of the shelf before it
94 // separates.
95 constexpr int kRipOffDistance = 48;
96 
97 // The rip off drag and drop proxy image should get scaled by this factor.
98 constexpr float kDragAndDropProxyScale = 1.2f;
99 
100 // The opacity represents that this partially disappeared item will get removed.
101 constexpr float kDraggedImageOpacity = 0.5f;
102 
103 namespace {
104 
105 // The dimensions, in pixels, of the separator between pinned and unpinned
106 // items.
107 constexpr int kSeparatorSize = 20;
108 constexpr int kSeparatorThickness = 1;
109 
110 constexpr char kShelfIconMoveAnimationHistogram[] =
111     "Ash.ShelfIcon.AnimationSmoothness.Move";
112 constexpr char kShelfIconFadeInAnimationHistogram[] =
113     "Ash.ShelfIcon.AnimationSmoothness.FadeIn";
114 constexpr char kShelfIconFadeOutAnimationHistogram[] =
115     "Ash.ShelfIcon.AnimationSmoothness.FadeOut";
116 
117 // Helper to check if tablet mode is enabled.
IsTabletModeEnabled()118 bool IsTabletModeEnabled() {
119   return Shell::Get()->tablet_mode_controller() &&
120          Shell::Get()->tablet_mode_controller()->InTabletMode();
121 }
122 
123 // A class to temporarily disable a given bounds animator.
124 class BoundsAnimatorDisabler {
125  public:
BoundsAnimatorDisabler(views::BoundsAnimator * bounds_animator)126   explicit BoundsAnimatorDisabler(views::BoundsAnimator* bounds_animator)
127       : old_duration_(bounds_animator->GetAnimationDuration()),
128         bounds_animator_(bounds_animator) {
129     bounds_animator_->SetAnimationDuration(
130         base::TimeDelta::FromMilliseconds(1));
131   }
132 
~BoundsAnimatorDisabler()133   ~BoundsAnimatorDisabler() {
134     bounds_animator_->SetAnimationDuration(old_duration_);
135   }
136 
137  private:
138   // The previous animation duration.
139   base::TimeDelta old_duration_;
140   // The bounds animator which gets used.
141   views::BoundsAnimator* bounds_animator_;
142 
143   DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorDisabler);
144 };
145 
146 // Custom FocusSearch used to navigate the shelf in the order items are in
147 // the ViewModel.
148 class ShelfFocusSearch : public views::FocusSearch {
149  public:
ShelfFocusSearch(ShelfView * shelf_view)150   explicit ShelfFocusSearch(ShelfView* shelf_view)
151       : FocusSearch(nullptr, true, true), shelf_view_(shelf_view) {}
152   ~ShelfFocusSearch() override = default;
153 
154   // views::FocusSearch:
FindNextFocusableView(View * starting_view,FocusSearch::SearchDirection search_direction,FocusSearch::TraversalDirection traversal_direction,FocusSearch::StartingViewPolicy check_starting_view,FocusSearch::AnchoredDialogPolicy can_go_into_anchored_dialog,views::FocusTraversable ** focus_traversable,View ** focus_traversable_view)155   View* FindNextFocusableView(
156       View* starting_view,
157       FocusSearch::SearchDirection search_direction,
158       FocusSearch::TraversalDirection traversal_direction,
159       FocusSearch::StartingViewPolicy check_starting_view,
160       FocusSearch::AnchoredDialogPolicy can_go_into_anchored_dialog,
161       views::FocusTraversable** focus_traversable,
162       View** focus_traversable_view) override {
163     // Build a list of all the views that we are able to focus.
164     std::vector<views::View*> focusable_views;
165 
166     for (int i : shelf_view_->visible_views_indices())
167       focusable_views.push_back(shelf_view_->view_model()->view_at(i));
168 
169     // Where are we starting from?
170     int start_index = 0;
171     for (size_t i = 0; i < focusable_views.size(); ++i) {
172       if (focusable_views[i] == starting_view) {
173         start_index = i;
174         break;
175       }
176     }
177     int new_index =
178         start_index +
179         (search_direction == FocusSearch::SearchDirection::kBackwards ? -1 : 1);
180     // Loop around.
181     if (new_index < 0)
182       new_index = focusable_views.size() - 1;
183     else if (new_index >= static_cast<int>(focusable_views.size()))
184       new_index = 0;
185 
186     return focusable_views[new_index];
187   }
188 
189  private:
190   ShelfView* shelf_view_;
191 
192   DISALLOW_COPY_AND_ASSIGN(ShelfFocusSearch);
193 };
194 
ReportMoveAnimationSmoothness(int smoothness)195 void ReportMoveAnimationSmoothness(int smoothness) {
196   base::UmaHistogramPercentageObsoleteDoNotUse(kShelfIconMoveAnimationHistogram,
197                                                smoothness);
198 }
199 
ReportFadeInAnimationSmoothness(int smoothness)200 void ReportFadeInAnimationSmoothness(int smoothness) {
201   base::UmaHistogramPercentageObsoleteDoNotUse(
202       kShelfIconFadeInAnimationHistogram, smoothness);
203 }
204 
ReportFadeOutAnimationSmoothness(int smoothness)205 void ReportFadeOutAnimationSmoothness(int smoothness) {
206   base::UmaHistogramPercentageObsoleteDoNotUse(
207       kShelfIconFadeOutAnimationHistogram, smoothness);
208 }
209 
210 // Returns the id of the display on which |view| is shown.
GetDisplayIdForView(const View * view)211 int64_t GetDisplayIdForView(const View* view) {
212   aura::Window* window = view->GetWidget()->GetNativeWindow();
213   return display::Screen::GetScreen()->GetDisplayNearestWindow(window).id();
214 }
215 
216 // Whether |item_view| is a ShelfAppButton and its state is STATE_DRAGGING.
ShelfButtonIsInDrag(const ShelfItemType item_type,const views::View * item_view)217 bool ShelfButtonIsInDrag(const ShelfItemType item_type,
218                          const views::View* item_view) {
219   switch (item_type) {
220     case TYPE_PINNED_APP:
221     case TYPE_BROWSER_SHORTCUT:
222     case TYPE_APP:
223       return static_cast<const ShelfAppButton*>(item_view)->state() &
224              ShelfAppButton::STATE_DRAGGING;
225     case TYPE_DIALOG:
226     case TYPE_UNDEFINED:
227       return false;
228   }
229 }
230 
231 // Called back by the shelf item delegates to determine whether an app menu item
232 // should be included in the shelf app menu given its corresponding window. This
233 // is used to filter out items whose windows are on inactive desks when the per-
234 // desk shelf feature is enabled.
ShouldIncludeMenuItem(aura::Window * window)235 bool ShouldIncludeMenuItem(aura::Window* window) {
236   if (!features::IsPerDeskShelfEnabled())
237     return true;
238   return desks_util::BelongsToActiveDesk(window);
239 }
240 
241 // Returns true if the app associated with |app_id| is a Remote App.
IsRemoteApp(const std::string & app_id)242 bool IsRemoteApp(const std::string& app_id) {
243   AccountId account_id =
244       Shell::Get()->session_controller()->GetActiveAccountId();
245   apps::AppRegistryCache* cache =
246       apps::AppRegistryCacheWrapper::Get().GetAppRegistryCache(account_id);
247   return cache && cache->GetAppType(app_id) == apps::mojom::AppType::kRemote;
248 }
249 
250 }  // namespace
251 
252 // ImplicitAnimationObserver used when adding an item.
253 class ShelfView::FadeInAnimationDelegate
254     : public ui::ImplicitAnimationObserver {
255  public:
FadeInAnimationDelegate(ShelfView * shelf_view)256   explicit FadeInAnimationDelegate(ShelfView* shelf_view)
257       : shelf_view_(shelf_view) {}
~FadeInAnimationDelegate()258   ~FadeInAnimationDelegate() override { StopObservingImplicitAnimations(); }
259 
260  private:
261   // ui::ImplicitAnimationObserver:
OnImplicitAnimationsCompleted()262   void OnImplicitAnimationsCompleted() override {
263     shelf_view_->OnFadeInAnimationEnded();
264   }
265 
266   ShelfView* shelf_view_ = nullptr;
267 };
268 
269 // AnimationDelegate used when deleting an item. This steadily decreased the
270 // opacity of the layer as the animation progress.
271 class ShelfView::FadeOutAnimationDelegate : public gfx::AnimationDelegate {
272  public:
FadeOutAnimationDelegate(ShelfView * host,std::unique_ptr<views::View> view)273   FadeOutAnimationDelegate(ShelfView* host, std::unique_ptr<views::View> view)
274       : shelf_view_(host), view_(std::move(view)) {}
275   ~FadeOutAnimationDelegate() override = default;
276 
277   // AnimationDelegate overrides:
AnimationProgressed(const Animation * animation)278   void AnimationProgressed(const Animation* animation) override {
279     view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
280   }
AnimationEnded(const Animation * animation)281   void AnimationEnded(const Animation* animation) override {
282     // Ensures that |view| is not used after destruction.
283     shelf_view_->StopAnimatingViewIfAny(view_.get());
284 
285     // Remove the view which has been faded away.
286     view_.reset();
287 
288     shelf_view_->OnFadeOutAnimationEnded();
289   }
AnimationCanceled(const Animation * animation)290   void AnimationCanceled(const Animation* animation) override {}
291 
292  private:
293   ShelfView* shelf_view_;
294   std::unique_ptr<views::View> view_;
295 
296   DISALLOW_COPY_AND_ASSIGN(FadeOutAnimationDelegate);
297 };
298 
299 // AnimationDelegate used to trigger fading an element in. When an item is
300 // inserted this delegate is attached to the animation that expands the size of
301 // the item.  When done it kicks off another animation to fade the item in.
302 class ShelfView::StartFadeAnimationDelegate : public gfx::AnimationDelegate {
303  public:
StartFadeAnimationDelegate(ShelfView * host,views::View * view)304   StartFadeAnimationDelegate(ShelfView* host, views::View* view)
305       : shelf_view_(host), view_(view) {}
306   ~StartFadeAnimationDelegate() override = default;
307 
308   // AnimationDelegate overrides:
AnimationEnded(const Animation * animation)309   void AnimationEnded(const Animation* animation) override {
310     shelf_view_->FadeIn(view_);
311   }
AnimationCanceled(const Animation * animation)312   void AnimationCanceled(const Animation* animation) override {
313     view_->layer()->SetOpacity(1.0f);
314   }
315 
316  private:
317   ShelfView* shelf_view_;
318   views::View* view_;
319 
320   DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate);
321 };
322 
323 // static
324 const int ShelfView::kMinimumDragDistance = 8;
325 
ShelfView(ShelfModel * model,Shelf * shelf,ApplicationDragAndDropHost * drag_and_drop_host,ShelfButtonDelegate * shelf_button_delegate)326 ShelfView::ShelfView(ShelfModel* model,
327                      Shelf* shelf,
328                      ApplicationDragAndDropHost* drag_and_drop_host,
329                      ShelfButtonDelegate* shelf_button_delegate)
330     : model_(model),
331       shelf_(shelf),
332       view_model_(std::make_unique<views::ViewModel>()),
333       bounds_animator_(
334           std::make_unique<views::BoundsAnimator>(this,
335                                                   /*use_transforms=*/true)),
336       focus_search_(std::make_unique<ShelfFocusSearch>(this)),
337       drag_and_drop_host_(drag_and_drop_host),
338       shelf_button_delegate_(shelf_button_delegate) {
339   DCHECK(model_);
340   DCHECK(shelf_);
341   Shell::Get()->tablet_mode_controller()->AddObserver(this);
342   Shell::Get()->AddShellObserver(this);
343   bounds_animator_->AddObserver(this);
344   bounds_animator_->SetAnimationDuration(
345       ShelfConfig::Get()->shelf_animation_duration());
346   set_context_menu_controller(this);
347   set_allow_deactivate_on_esc(true);
348 
349   announcement_view_ = new views::View();
350   AddChildView(announcement_view_);
351 }
352 
~ShelfView()353 ShelfView::~ShelfView() {
354   // Shell destroys the TabletModeController before destroying all root windows.
355   if (Shell::Get()->tablet_mode_controller())
356     Shell::Get()->tablet_mode_controller()->RemoveObserver(this);
357   Shell::Get()->RemoveShellObserver(this);
358   bounds_animator_->RemoveObserver(this);
359   model_->RemoveObserver(this);
360 }
361 
GetSizeOfAppButtons(int count,int button_size)362 int ShelfView::GetSizeOfAppButtons(int count, int button_size) {
363   const int button_spacing = ShelfConfig::Get()->button_spacing();
364 
365   if (count == 0)
366     return 0;
367 
368   const int app_size = count * button_size;
369   int total_padding = button_spacing * (count - 1);
370   return app_size + total_padding;
371 }
372 
Init()373 void ShelfView::Init() {
374   separator_ = new views::Separator();
375   separator_->SetColor(AshColorProvider::Get()->GetContentLayerColor(
376       AshColorProvider::ContentLayerType::kSeparatorColor));
377   separator_->SetPreferredHeight(kSeparatorSize);
378   separator_->SetVisible(false);
379   ConfigureChildView(separator_, ui::LAYER_TEXTURED);
380   AddChildView(separator_);
381 
382   model()->AddObserver(this);
383 
384   const ShelfItems& items(model_->items());
385 
386   for (ShelfItems::const_iterator i = items.begin(); i != items.end(); ++i) {
387     views::View* child = CreateViewForItem(*i);
388     child->SetPaintToLayer();
389     int index = static_cast<int>(i - items.begin());
390     view_model_->Add(child, index);
391     // Add child view so it has the same ordering as in the |view_model_|.
392     AddChildViewAt(child, index);
393   }
394 
395   fade_in_animation_delegate_ = std::make_unique<FadeInAnimationDelegate>(this);
396 
397   // We'll layout when our bounds change.
398 }
399 
GetIdealBoundsOfItemIcon(const ShelfID & id)400 gfx::Rect ShelfView::GetIdealBoundsOfItemIcon(const ShelfID& id) {
401   int index = model_->ItemIndexByID(id);
402   if (!base::Contains(visible_views_indices_, index))
403     return gfx::Rect();
404 
405   const gfx::Rect& ideal_bounds(view_model_->ideal_bounds(index));
406   ShelfAppButton* button = GetShelfAppButton(id);
407   gfx::Rect icon_bounds = button->GetIconBounds();
408   return gfx::Rect(GetMirroredXWithWidthInView(
409                        ideal_bounds.x() + icon_bounds.x(), icon_bounds.width()),
410                    ideal_bounds.y() + icon_bounds.y(), icon_bounds.width(),
411                    icon_bounds.height());
412 }
413 
IsShowingMenu() const414 bool ShelfView::IsShowingMenu() const {
415   return shelf_menu_model_adapter_ &&
416          shelf_menu_model_adapter_->IsShowingMenu();
417 }
418 
UpdateVisibleShelfItemBoundsUnion()419 void ShelfView::UpdateVisibleShelfItemBoundsUnion() {
420   visible_shelf_item_bounds_union_.SetRect(0, 0, 0, 0);
421   for (const int i : visible_views_indices_) {
422     const views::View* child = view_model_->view_at(i);
423     if (ShouldShowTooltipForChildView(child)) {
424       visible_shelf_item_bounds_union_.Union(
425           GetChildViewTargetMirroredBounds(child));
426     }
427   }
428 }
429 
ShouldShowTooltipForView(const views::View * view) const430 bool ShelfView::ShouldShowTooltipForView(const views::View* view) const {
431   if (!view || !view->parent())
432     return false;
433 
434   if (view->parent() == this)
435     return ShouldShowTooltipForChildView(view);
436 
437   return false;
438 }
439 
GetShelfAppButton(const ShelfID & id)440 ShelfAppButton* ShelfView::GetShelfAppButton(const ShelfID& id) {
441   const int index = model_->ItemIndexByID(id);
442   if (index < 0)
443     return nullptr;
444 
445   views::View* const view = view_model_->view_at(index);
446   DCHECK_EQ(ShelfAppButton::kViewClassName, view->GetClassName());
447   return static_cast<ShelfAppButton*>(view);
448 }
449 
StopAnimatingViewIfAny(views::View * view)450 void ShelfView::StopAnimatingViewIfAny(views::View* view) {
451   if (bounds_animator_->IsAnimating(view))
452     bounds_animator_->StopAnimatingView(view);
453 }
454 
IsShelfViewHandlingDragAndDrop() const455 bool ShelfView::IsShelfViewHandlingDragAndDrop() const {
456   // If the ShelfView has a drag icon proxy, the drag originated from the
457   // AppsGridView. When the drag originates from the shelf, the
458   // ScrollableShelfView is the ApplicationDragAndDropHost, so ShelfView will
459   // not have a drag proxy.
460   return !!drag_image_widget_;
461 }
462 
GetButtonSize() const463 int ShelfView::GetButtonSize() const {
464   return ShelfConfig::Get()->GetShelfButtonSize(
465       shelf_->hotseat_widget()->target_hotseat_density());
466 }
467 
GetButtonIconSize() const468 int ShelfView::GetButtonIconSize() const {
469   return ShelfConfig::Get()->GetShelfButtonIconSize(
470       shelf_->hotseat_widget()->target_hotseat_density());
471 }
472 
GetShelfItemRippleSize() const473 int ShelfView::GetShelfItemRippleSize() const {
474   return GetButtonSize() +
475          2 * ShelfConfig::Get()->scrollable_shelf_ripple_padding();
476 }
477 
LayoutIfAppIconsOffsetUpdates()478 void ShelfView::LayoutIfAppIconsOffsetUpdates() {
479   if (app_icons_layout_offset_ != CalculateAppIconsLayoutOffset())
480     LayoutToIdealBounds();
481 }
482 
GetShelfItemViewWithContextMenu()483 ShelfAppButton* ShelfView::GetShelfItemViewWithContextMenu() {
484   if (context_menu_id_.IsNull())
485     return nullptr;
486   const int item_index = model_->ItemIndexByID(context_menu_id_);
487   if (item_index < 0)
488     return nullptr;
489   return static_cast<ShelfAppButton*>(view_model_->view_at(item_index));
490 }
491 
AnnounceShelfItemNotificationBadge(views::View * button)492 void ShelfView::AnnounceShelfItemNotificationBadge(views::View* button) {
493   announcement_view_->GetViewAccessibility().OverrideName(
494       l10n_util::GetStringFUTF16(IDS_SHELF_ITEM_HAS_NOTIFICATION_BADGE,
495                                  GetTitleForView(button)));
496   announcement_view_->NotifyAccessibilityEvent(ax::mojom::Event::kAlert,
497                                                /*send_native_event=*/true);
498 }
499 
ShouldHideTooltip(const gfx::Point & cursor_location) const500 bool ShelfView::ShouldHideTooltip(const gfx::Point& cursor_location) const {
501   // There are thin gaps between launcher buttons but the tooltip shouldn't hide
502   // in the gaps, but the tooltip should hide if the mouse moved totally outside
503   // of the buttons area.
504 
505   return !visible_shelf_item_bounds_union_.Contains(cursor_location);
506 }
507 
GetOpenWindowsForView(views::View * view)508 const std::vector<aura::Window*> ShelfView::GetOpenWindowsForView(
509     views::View* view) {
510   std::vector<aura::Window*> window_list =
511       Shell::Get()->mru_window_tracker()->BuildWindowForCycleList(kActiveDesk);
512   std::vector<aura::Window*> open_windows;
513   const ShelfItem* item = ShelfItemForView(view);
514 
515   // The concept of a list of open windows doesn't make sense for something
516   // that isn't an app shortcut: return an empty list.
517   if (!item)
518     return open_windows;
519 
520   for (auto* window : window_list) {
521     const std::string window_app_id =
522         ShelfID::Deserialize(window->GetProperty(kShelfIDKey)).app_id;
523     if (window_app_id == item->id.app_id) {
524       // TODO: In the very first version we only show one window. Add the proper
525       // UI to show all windows for a given open app.
526       open_windows.push_back(window);
527     }
528   }
529   return open_windows;
530 }
531 
GetTitleForView(const views::View * view) const532 base::string16 ShelfView::GetTitleForView(const views::View* view) const {
533   if (view->parent() == this)
534     return GetTitleForChildView(view);
535 
536   return base::string16();
537 }
538 
GetViewForEvent(const ui::Event & event)539 views::View* ShelfView::GetViewForEvent(const ui::Event& event) {
540   if (event.target() == GetWidget()->GetNativeWindow())
541     return this;
542 
543   return nullptr;
544 }
545 
GetVisibleItemsBoundsInScreen()546 gfx::Rect ShelfView::GetVisibleItemsBoundsInScreen() {
547   gfx::Size preferred_size = GetPreferredSize();
548   gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
549   ConvertPointToScreen(this, &origin);
550   return gfx::Rect(origin, preferred_size);
551 }
552 
CalculatePreferredSize() const553 gfx::Size ShelfView::CalculatePreferredSize() const {
554   const int hotseat_size = shelf_->hotseat_widget()->GetHotseatSize();
555   if (visible_views_indices_.empty()) {
556     // There are no visible shelf items.
557     return shelf_->IsHorizontalAlignment() ? gfx::Size(0, hotseat_size)
558                                            : gfx::Size(hotseat_size, 0);
559   }
560 
561   const gfx::Rect last_button_bounds =
562       view_model_->ideal_bounds(visible_views_indices_.back());
563 
564   if (shelf_->IsHorizontalAlignment())
565     return gfx::Size(last_button_bounds.right(), hotseat_size);
566 
567   return gfx::Size(hotseat_size, last_button_bounds.bottom());
568 }
569 
OnBoundsChanged(const gfx::Rect & previous_bounds)570 void ShelfView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
571   // This bounds change is produced by the shelf movement (rotation, alignment
572   // change, etc.) and all content has to follow. Using an animation at that
573   // time would produce a time lag since the animation of the BoundsAnimator has
574   // itself a delay before it arrives at the required location. As such we tell
575   // the animator to go there immediately. We still want to use an animation
576   // when the bounds change is caused by entering or exiting tablet mode, with
577   // an exception of usage within the scrollable shelf. With scrollable shelf
578   // (and hotseat), tablet mode transition causes hotseat bounds changes, so
579   // animating shelf items as well would introduce a lag.
580 
581   BoundsAnimatorDisabler disabler(bounds_animator_.get());
582 
583   LayoutToIdealBounds();
584   shelf_->NotifyShelfIconPositionsChanged();
585 }
586 
OnKeyPressed(const ui::KeyEvent & event)587 bool ShelfView::OnKeyPressed(const ui::KeyEvent& event) {
588   if (event.IsControlDown() &&
589       keyboard_util::IsArrowKeyCode(event.key_code())) {
590     bool swap_with_next = (event.key_code() == ui::VKEY_DOWN ||
591                            event.key_code() == ui::VKEY_RIGHT);
592     SwapButtons(GetFocusManager()->GetFocusedView(), swap_with_next);
593     return true;
594   }
595   return views::View::OnKeyPressed(event);
596 }
597 
OnMouseEvent(ui::MouseEvent * event)598 void ShelfView::OnMouseEvent(ui::MouseEvent* event) {
599   gfx::Point location_in_screen(event->location());
600   View::ConvertPointToScreen(this, &location_in_screen);
601 
602   switch (event->type()) {
603     case ui::ET_MOUSEWHEEL:
604       // The mousewheel event is handled by the ScrollableShelfView.
605       break;
606     case ui::ET_MOUSE_PRESSED:
607       if (!event->IsOnlyLeftMouseButton()) {
608         if (event->IsOnlyRightMouseButton()) {
609           ShowContextMenuForViewImpl(this, location_in_screen,
610                                      ui::MENU_SOURCE_MOUSE);
611           event->SetHandled();
612         }
613         return;
614       }
615 
616       FALLTHROUGH;
617     case ui::ET_MOUSE_DRAGGED:
618     case ui::ET_MOUSE_RELEASED:
619       // Convert the event location from current view to screen, since dragging
620       // the shelf by mouse can open the fullscreen app list. Updating the
621       // bounds of the app list during dragging is based on screen coordinate
622       // space.
623       event->set_location(location_in_screen);
624 
625       event->SetHandled();
626       shelf_->ProcessMouseEvent(*event->AsMouseEvent());
627       break;
628     default:
629       break;
630   }
631 }
632 
GetPaneFocusTraversable()633 views::FocusTraversable* ShelfView::GetPaneFocusTraversable() {
634   // ScrollableShelfView should handles the focus traversal.
635   return nullptr;
636 }
637 
GetClassName() const638 const char* ShelfView::GetClassName() const {
639   return "ShelfView";
640 }
641 
GetAccessibleNodeData(ui::AXNodeData * node_data)642 void ShelfView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
643   node_data->role = ax::mojom::Role::kToolbar;
644   node_data->SetName(l10n_util::GetStringUTF8(IDS_ASH_SHELF_ACCESSIBLE_NAME));
645 }
646 
GetTooltipHandlerForPoint(const gfx::Point & point)647 View* ShelfView::GetTooltipHandlerForPoint(const gfx::Point& point) {
648   // Similar implementation as views::View, but without going into each
649   // child's subviews.
650   View::Views children = GetChildrenInZOrder();
651   for (auto* child : base::Reversed(children)) {
652     if (!child->GetVisible())
653       continue;
654 
655     gfx::Point point_in_child_coords(point);
656     ConvertPointToTarget(this, child, &point_in_child_coords);
657     if (child->HitTestPoint(point_in_child_coords) &&
658         ShouldShowTooltipForChildView(child)) {
659       return child;
660     }
661   }
662   // If none of our children qualifies, just return the shelf view itself.
663   return this;
664 }
665 
OnShelfButtonAboutToRequestFocusFromTabTraversal(ShelfButton * button,bool reverse)666 void ShelfView::OnShelfButtonAboutToRequestFocusFromTabTraversal(
667     ShelfButton* button,
668     bool reverse) {
669   if (ShouldFocusOut(reverse, button)) {
670     shelf_->shelf_focus_cycler()->FocusOut(reverse, SourceView::kShelfView);
671   }
672 }
673 
ButtonPressed(views::Button * sender,const ui::Event & event,views::InkDrop * ink_drop)674 void ShelfView::ButtonPressed(views::Button* sender,
675                               const ui::Event& event,
676                               views::InkDrop* ink_drop) {
677   if (!ShouldEventActivateButton(sender, event)) {
678     ink_drop->SnapToHidden();
679     return;
680   }
681 
682   // Prevent concurrent requests that may show application or context menus.
683   if (!item_awaiting_response_.IsNull()) {
684     const ShelfItem* item = ShelfItemForView(sender);
685     if (item && item->id != item_awaiting_response_)
686       ink_drop->AnimateToState(views::InkDropState::DEACTIVATED);
687     return;
688   }
689 
690   // Ensure the keyboard is hidden and stays hidden (as long as it isn't locked)
691   if (keyboard::KeyboardUIController::Get()->IsEnabled())
692     keyboard::KeyboardUIController::Get()->HideKeyboardExplicitlyBySystem();
693 
694   // Record the index for the last pressed shelf item.
695   last_pressed_index_ = view_model_->GetIndexOfView(sender);
696   DCHECK_LT(-1, last_pressed_index_);
697 
698   // Place new windows on the same display as the button. Opening windows is
699   // usually an async operation so we wait until window activation changes
700   // (ShelfItemStatusChanged) before destroying the scoped object. Post a task
701   // to destroy the scoped object just in case the window activation event does
702   // not get fired.
703   aura::Window* window = sender->GetWidget()->GetNativeWindow();
704   scoped_display_for_new_windows_ =
705       std::make_unique<display::ScopedDisplayForNewWindows>(
706           window->GetRootWindow());
707   base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
708       FROM_HERE,
709       base::BindOnce(&ShelfView::DestroyScopedDisplay,
710                      weak_factory_.GetWeakPtr()),
711       base::TimeDelta::FromMilliseconds(100));
712 
713   // Slow down activation animations if Control key is pressed.
714   std::unique_ptr<ui::ScopedAnimationDurationScaleMode> slowing_animations;
715   if (event.IsControlDown()) {
716     slowing_animations.reset(new ui::ScopedAnimationDurationScaleMode(
717         ui::ScopedAnimationDurationScaleMode::SLOW_DURATION));
718   }
719 
720   // Collect usage statistics before we decide what to do with the click.
721   switch (model_->items()[last_pressed_index_].type) {
722     case TYPE_PINNED_APP:
723     case TYPE_BROWSER_SHORTCUT:
724     case TYPE_APP:
725       Shell::Get()->metrics()->RecordUserMetricsAction(
726           UMA_LAUNCHER_CLICK_ON_APP);
727       break;
728 
729     case TYPE_DIALOG:
730       break;
731 
732     case TYPE_UNDEFINED:
733       NOTREACHED() << "ShelfItemType must be set.";
734       break;
735   }
736 
737   // Run AfterItemSelected directly if the item has no delegate (ie. in tests).
738   const ShelfItem& item = model_->items()[last_pressed_index_];
739   if (!model_->GetShelfItemDelegate(item.id)) {
740     AfterItemSelected(item, sender, ui::Event::Clone(event), ink_drop,
741                       SHELF_ACTION_NONE, {});
742     return;
743   }
744 
745   // Notify the item of its selection; handle the result in AfterItemSelected.
746   item_awaiting_response_ = item.id;
747   model_->GetShelfItemDelegate(item.id)->ItemSelected(
748       ui::Event::Clone(event), GetDisplayIdForView(this), LAUNCH_FROM_SHELF,
749       base::BindOnce(&ShelfView::AfterItemSelected, weak_factory_.GetWeakPtr(),
750                      item, sender, ui::Event::Clone(event), ink_drop),
751       base::BindRepeating(&ShouldIncludeMenuItem));
752 }
753 
IsShowingMenuForView(const views::View * view) const754 bool ShelfView::IsShowingMenuForView(const views::View* view) const {
755   return IsShowingMenu() &&
756          shelf_menu_model_adapter_->IsShowingMenuForView(*view);
757 }
758 
759 ////////////////////////////////////////////////////////////////////////////////
760 // ShelfView, FocusTraversable implementation:
761 
GetFocusSearch()762 views::FocusSearch* ShelfView::GetFocusSearch() {
763   return focus_search_.get();
764 }
765 
766 ////////////////////////////////////////////////////////////////////////////////
767 // ShelfView, AccessiblePaneView implementation:
768 
GetDefaultFocusableChild()769 views::View* ShelfView::GetDefaultFocusableChild() {
770   return default_last_focusable_child_ ? FindLastFocusableChild()
771                                        : FindFirstFocusableChild();
772 }
773 
ShowContextMenuForViewImpl(views::View * source,const gfx::Point & point,ui::MenuSourceType source_type)774 void ShelfView::ShowContextMenuForViewImpl(views::View* source,
775                                            const gfx::Point& point,
776                                            ui::MenuSourceType source_type) {
777   // Prevent concurrent requests that may show application or context menus.
778   const ShelfItem* item = ShelfItemForView(source);
779   if (!item_awaiting_response_.IsNull()) {
780     if (item && item->id != item_awaiting_response_) {
781       static_cast<views::Button*>(source)->AnimateInkDrop(
782           views::InkDropState::DEACTIVATED, nullptr);
783     }
784     return;
785   }
786   last_pressed_index_ = -1;
787   if (!item || !model_->GetShelfItemDelegate(item->id)) {
788     ShowShelfContextMenu(ShelfID(), point, source, source_type, nullptr);
789     return;
790   }
791 
792   item_awaiting_response_ = item->id;
793   context_menu_callback_.Reset(base::BindOnce(
794       &ShelfView::ShowShelfContextMenu, weak_factory_.GetWeakPtr(), item->id,
795       point, source, source_type));
796 
797   const int64_t display_id = GetDisplayIdForView(this);
798   model_->GetShelfItemDelegate(item->id)->GetContextMenu(
799       display_id, context_menu_callback_.callback());
800 }
801 
OnTabletModeStarted()802 void ShelfView::OnTabletModeStarted() {
803   // Close all menus when tablet mode starts to ensure that the clamshell only
804   // context menu options are not available in tablet mode.
805   if (shelf_menu_model_adapter_)
806     shelf_menu_model_adapter_->Cancel();
807 }
808 
OnTabletModeEnded()809 void ShelfView::OnTabletModeEnded() {
810   // Close all menus when tablet mode ends so that menu options are kept
811   // consistent with device state.
812   if (shelf_menu_model_adapter_)
813     shelf_menu_model_adapter_->Cancel();
814 }
815 
OnShelfConfigUpdated()816 void ShelfView::OnShelfConfigUpdated() {
817   // Ensure the shelf app buttons have an icon which is up to date with the
818   // current ShelfConfig sizing.
819   for (int i = 0; i < view_model_->view_size(); i++) {
820     ShelfAppButton* button =
821         static_cast<ShelfAppButton*>(view_model_->view_at(i));
822     if (!button->IsIconSizeCurrent())
823       ShelfItemChanged(i, model_->items()[i]);
824   }
825 }
826 
ShouldEventActivateButton(View * view,const ui::Event & event)827 bool ShelfView::ShouldEventActivateButton(View* view, const ui::Event& event) {
828   // This only applies to app buttons.
829   DCHECK_EQ(ShelfAppButton::kViewClassName, view->GetClassName());
830   if (dragging())
831     return false;
832 
833   // Ignore if we are already in a pointer event sequence started with a repost
834   // event on the same shelf item. See crbug.com/343005 for more detail.
835   if (is_repost_event_on_same_item_)
836     return false;
837 
838   // Don't activate the item twice on double-click. Otherwise the window starts
839   // animating open due to the first click, then immediately minimizes due to
840   // the second click. The user most likely intended to open or minimize the
841   // item once, not do both.
842   if (event.flags() & ui::EF_IS_DOUBLE_CLICK)
843     return false;
844 
845   const bool repost = IsRepostEvent(event);
846 
847   // Ignore if this is a repost event on the last pressed shelf item.
848   int index = view_model_->GetIndexOfView(view);
849   if (index == -1)
850     return false;
851   return !repost || last_pressed_index_ != index;
852 }
853 
CreateDragIconProxyByLocationWithNoAnimation(const gfx::Point & origin_in_screen_coordinates,const gfx::ImageSkia & icon,views::View * replaced_view,float scale_factor,int blur_radius)854 void ShelfView::CreateDragIconProxyByLocationWithNoAnimation(
855     const gfx::Point& origin_in_screen_coordinates,
856     const gfx::ImageSkia& icon,
857     views::View* replaced_view,
858     float scale_factor,
859     int blur_radius) {
860   drag_replaced_view_ = replaced_view;
861   aura::Window* root_window =
862       drag_replaced_view_->GetWidget()->GetNativeWindow()->GetRootWindow();
863   drag_image_widget_ =
864       DragImageView::Create(root_window, ui::mojom::DragEventSource::kMouse);
865   DragImageView* drag_image = GetDragImage();
866   if (blur_radius > 0)
867     SetDragImageBlur(icon.size(), blur_radius);
868   drag_image->SetImage(icon);
869   gfx::Size size = drag_image->GetPreferredSize();
870   size.set_width(size.width() * scale_factor);
871   size.set_height(size.height() * scale_factor);
872   gfx::Rect drag_image_bounds(origin_in_screen_coordinates, size);
873   drag_image->SetBoundsInScreen(drag_image_bounds);
874 
875   // Turn off the default visibility animation.
876   drag_image_widget_->SetVisibilityAnimationTransition(
877       views::Widget::ANIMATE_NONE);
878   drag_image->SetWidgetVisible(true);
879   // Add a layer in order to ensure the icon properly animates when a drag
880   // starts from AppsGridView and ends in the Shelf.
881   drag_image->SetPaintToLayer();
882   drag_image->layer()->SetFillsBoundsOpaquely(false);
883 }
884 
UpdateDragIconProxy(const gfx::Point & location_in_screen_coordinates)885 void ShelfView::UpdateDragIconProxy(
886     const gfx::Point& location_in_screen_coordinates) {
887   // TODO(jennyz): Investigate why drag_image_widget_ becomes null at this point
888   // per crbug.com/34722, while the app list item is still being dragged around.
889   if (drag_image_widget_) {
890     GetDragImage()->SetScreenPosition(location_in_screen_coordinates -
891                                       drag_image_offset_);
892   }
893 }
894 
UpdateDragIconProxyByLocation(const gfx::Point & origin_in_screen_coordinates)895 void ShelfView::UpdateDragIconProxyByLocation(
896     const gfx::Point& origin_in_screen_coordinates) {
897   if (drag_image_widget_)
898     GetDragImage()->SetScreenPosition(origin_in_screen_coordinates);
899 }
900 
IsDraggedView(const views::View * view) const901 bool ShelfView::IsDraggedView(const views::View* view) const {
902   return drag_view_ == view;
903 }
904 
FindFirstFocusableChild()905 views::View* ShelfView::FindFirstFocusableChild() {
906   if (visible_views_indices_.empty())
907     return nullptr;
908   return view_model_->view_at(visible_views_indices_.front());
909 }
910 
FindLastFocusableChild()911 views::View* ShelfView::FindLastFocusableChild() {
912   if (visible_views_indices_.empty())
913     return nullptr;
914   return view_model_->view_at(visible_views_indices_.back());
915 }
916 
FindFirstOrLastFocusableChild(bool last)917 views::View* ShelfView::FindFirstOrLastFocusableChild(bool last) {
918   return last ? FindLastFocusableChild() : FindFirstFocusableChild();
919 }
920 
HandleGestureEvent(const ui::GestureEvent * event)921 bool ShelfView::HandleGestureEvent(const ui::GestureEvent* event) {
922   // Avoid changing |event|'s location since |event| may be received by post
923   // event handlers.
924   ui::GestureEvent copy_event(*event);
925 
926   // Convert the event location from current view to screen, since swiping up on
927   // the shelf can open the fullscreen app list. Updating the bounds of the app
928   // list during dragging is based on screen coordinate space.
929   gfx::Point location_in_screen(copy_event.location());
930   View::ConvertPointToScreen(this, &location_in_screen);
931   copy_event.set_location(location_in_screen);
932 
933   if (shelf_->ProcessGestureEvent(copy_event))
934     return true;
935 
936   return false;
937 }
938 
ShouldShowTooltipForChildView(const views::View * child_view) const939 bool ShelfView::ShouldShowTooltipForChildView(
940     const views::View* child_view) const {
941   DCHECK_EQ(this, child_view->parent());
942 
943   // Don't show a tooltip for a view that's currently being dragged.
944   if (child_view == drag_view_)
945     return false;
946 
947   return ShelfItemForView(child_view) && !IsShowingMenuForView(child_view);
948 }
949 
950 // static
ConfigureChildView(views::View * view,ui::LayerType layer_type)951 void ShelfView::ConfigureChildView(views::View* view,
952                                    ui::LayerType layer_type) {
953   view->SetPaintToLayer(layer_type);
954   view->layer()->SetFillsBoundsOpaquely(false);
955 }
956 
CalculateIdealBounds()957 void ShelfView::CalculateIdealBounds() {
958   DCHECK(model()->item_count() == view_model_->view_size());
959 
960   const int button_spacing = ShelfConfig::Get()->button_spacing();
961   UpdateSeparatorIndex();
962 
963   const int hotseat_size = shelf_->hotseat_widget()->GetHotseatSize();
964 
965   // Don't show the separator if it isn't needed, or would appear after all
966   // visible items.
967   separator_->SetVisible(separator_index_ != -1 &&
968                          separator_index_ < visible_views_indices_.back());
969   // Set |separator_index_| to -1 if it is not visible.
970   if (!separator_->GetVisible())
971     separator_index_ = -1;
972 
973   app_icons_layout_offset_ = CalculateAppIconsLayoutOffset();
974   int x = shelf()->PrimaryAxisValue(app_icons_layout_offset_, 0);
975   int y = shelf()->PrimaryAxisValue(0, app_icons_layout_offset_);
976 
977   // The padding is handled in ScrollableShelfView.
978 
979   const int button_size = GetButtonSize();
980   for (int i = 0; i < view_model_->view_size(); ++i) {
981     const bool is_visible = view_model_->view_at(i)->GetVisible();
982     if (!is_visible) {
983       // Layout hidden views with empty bounds so they don't consume horizontal
984       // space. Note that |separator_index_| cannot be the index of a hidden
985       // view.
986       DCHECK_NE(i, separator_index_);
987       view_model_->set_ideal_bounds(i, gfx::Rect(x, y, 0, 0));
988       continue;
989     }
990 
991     view_model_->set_ideal_bounds(i, gfx::Rect(x, y, button_size, button_size));
992 
993     x = shelf()->PrimaryAxisValue(x + button_size + button_spacing, x);
994     y = shelf()->PrimaryAxisValue(y, y + button_size + button_spacing);
995 
996     if (i == separator_index_) {
997       // Place the separator halfway between the two icons it separates,
998       // vertically centered.
999       int half_space = button_spacing / 2;
1000       int secondary_offset = (hotseat_size - kSeparatorSize) / 2;
1001       x -= shelf()->PrimaryAxisValue(half_space, 0);
1002       y -= shelf()->PrimaryAxisValue(0, half_space);
1003       separator_->SetBounds(
1004           x + shelf()->PrimaryAxisValue(0, secondary_offset),
1005           y + shelf()->PrimaryAxisValue(secondary_offset, 0),
1006           shelf()->PrimaryAxisValue(kSeparatorThickness, kSeparatorSize),
1007           shelf()->PrimaryAxisValue(kSeparatorSize, kSeparatorThickness));
1008       x += shelf()->PrimaryAxisValue(half_space, 0);
1009       y += shelf()->PrimaryAxisValue(0, half_space);
1010     }
1011   }
1012 }
1013 
CreateViewForItem(const ShelfItem & item)1014 views::View* ShelfView::CreateViewForItem(const ShelfItem& item) {
1015   views::View* view = nullptr;
1016   switch (item.type) {
1017     case TYPE_PINNED_APP:
1018     case TYPE_BROWSER_SHORTCUT:
1019     case TYPE_APP:
1020     case TYPE_DIALOG: {
1021       ShelfAppButton* button = new ShelfAppButton(
1022           this, shelf_button_delegate_ ? shelf_button_delegate_ : this);
1023       button->SetImage(item.image);
1024       button->ReflectItemStatus(item);
1025       view = button;
1026       break;
1027     }
1028 
1029     case TYPE_UNDEFINED:
1030       return nullptr;
1031   }
1032 
1033   view->set_context_menu_controller(this);
1034 
1035   ConfigureChildView(view, ui::LAYER_NOT_DRAWN);
1036   return view;
1037 }
1038 
GetAvailableSpaceForAppIcons() const1039 int ShelfView::GetAvailableSpaceForAppIcons() const {
1040   return shelf()->PrimaryAxisValue(width(), height());
1041 }
1042 
UpdateSeparatorIndex()1043 void ShelfView::UpdateSeparatorIndex() {
1044   // A separator is shown after the last pinned item only if it's followed by a
1045   // visible app item.
1046   int first_unpinned_index = -1;
1047   int last_pinned_index = -1;
1048 
1049   int dragged_item_index = -1;
1050   if (drag_view_)
1051     dragged_item_index = view_model_->GetIndexOfView(drag_view_);
1052 
1053   const bool can_drag_view_across_separator =
1054       drag_view_ && CanDragAcrossSeparator(drag_view_);
1055   for (int i = model()->item_count() - 1; i >= 0; --i) {
1056     const auto& item = model()->items()[i];
1057     if (IsItemPinned(item)) {
1058       // Dragged pinned item may be moved to the unpinned side of the shelf and
1059       // may end up right of an unpinned app. Dismisses the dragged item to
1060       // check the next one.
1061       if (i == dragged_item_index && can_drag_view_across_separator)
1062         continue;
1063 
1064       last_pinned_index = i;
1065       break;
1066     }
1067 
1068     if (item.type == TYPE_APP && item.is_on_active_desk)
1069       first_unpinned_index = i;
1070   }
1071 
1072   // If there is no unpinned item in shelf, return -1 as the separator should be
1073   // hidden.
1074   if (first_unpinned_index == -1) {
1075     separator_index_ = -1;
1076     return;
1077   }
1078 
1079   // If the dragged item is between the pinned apps and unpinned apps, move it
1080   // to the pinned app side if it is closer to the pinned section compared to
1081   // its ideal bounds.
1082   if (can_drag_view_across_separator &&
1083       last_pinned_index < dragged_item_index &&
1084       dragged_item_index <= first_unpinned_index &&
1085       drag_view_relative_to_ideal_bounds_ == RelativePosition::kLeft) {
1086     separator_index_ = dragged_item_index;
1087     return;
1088   }
1089 
1090   separator_index_ = last_pinned_index;
1091 }
1092 
DestroyDragIconProxy()1093 void ShelfView::DestroyDragIconProxy() {
1094   drag_image_widget_.reset();
1095   drag_image_offset_ = gfx::Vector2d(0, 0);
1096 }
1097 
1098 views::UniqueWidgetPtr
RetrieveDragIconProxyAndClearDragProxyState()1099 ShelfView::RetrieveDragIconProxyAndClearDragProxyState() {
1100   // TODO(https://crub.com/1045186): Make ScrollableShelfView the only
1101   // ApplicationDragAndDropHost in the view hierarchy, and remove this.
1102   views::UniqueWidgetPtr temp_drag_image_view = std::move(drag_image_widget_);
1103   DestroyDragIconProxy();
1104   return temp_drag_image_view;
1105 }
1106 
ShouldStartDrag(const std::string & app_id,const gfx::Point & location_in_screen_coordinates) const1107 bool ShelfView::ShouldStartDrag(
1108     const std::string& app_id,
1109     const gfx::Point& location_in_screen_coordinates) const {
1110   // Remote Apps are not pinnable.
1111   if (IsRemoteApp(app_id))
1112     return false;
1113 
1114   // Do not start drag if an operation is already going on - or the cursor is
1115   // not inside. This could happen if mouse / touch operations overlap.
1116   return (drag_and_drop_shelf_id_.IsNull() && !app_id.empty() &&
1117           GetBoundsInScreen().Contains(location_in_screen_coordinates));
1118 }
1119 
StartDrag(const std::string & app_id,const gfx::Point & location_in_screen_coordinates)1120 bool ShelfView::StartDrag(const std::string& app_id,
1121                           const gfx::Point& location_in_screen_coordinates) {
1122   if (!ShouldStartDrag(app_id, location_in_screen_coordinates))
1123     return false;
1124 
1125   // If the AppsGridView (which was dispatching this event) was opened by our
1126   // button, ShelfView dragging operations are locked and we have to unlock.
1127   CancelDrag(-1);
1128   drag_and_drop_item_pinned_ = false;
1129   drag_and_drop_shelf_id_ = ShelfID(app_id);
1130   // Check if the application is pinned - if not, we have to pin it so
1131   // that we can re-arrange the shelf order accordingly. Note that items have
1132   // to be pinned to give them the same (order) possibilities as a shortcut.
1133   if (!model_->IsAppPinned(app_id)) {
1134     ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);
1135     model_->PinAppWithID(app_id);
1136     drag_and_drop_item_pinned_ = true;
1137   }
1138   views::View* drag_and_drop_view =
1139       view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_));
1140   DCHECK(drag_and_drop_view);
1141 
1142   // Since there is already an icon presented by the caller, we hide this item
1143   // for now. That has to be done by reducing the size since the visibility will
1144   // change once a regrouping animation is performed.
1145   pre_drag_and_drop_size_ = drag_and_drop_view->size();
1146   drag_and_drop_view->SetSize(gfx::Size());
1147 
1148   // First we have to center the mouse cursor over the item.
1149   const gfx::Point start_point_in_screen =
1150       drag_and_drop_view->GetBoundsInScreen().CenterPoint();
1151   gfx::Point pt = start_point_in_screen;
1152   views::View::ConvertPointFromScreen(drag_and_drop_view, &pt);
1153   gfx::Point point_in_root = start_point_in_screen;
1154   wm::ConvertPointFromScreen(
1155       window_util::GetRootWindowAt(location_in_screen_coordinates),
1156       &point_in_root);
1157   ui::MouseEvent event(ui::ET_MOUSE_PRESSED, pt, point_in_root,
1158                        ui::EventTimeForNow(), 0, 0);
1159   PointerPressedOnButton(drag_and_drop_view, DRAG_AND_DROP, event);
1160 
1161   // Drag the item where it really belongs.
1162   Drag(location_in_screen_coordinates);
1163   return true;
1164 }
1165 
Drag(const gfx::Point & location_in_screen_coordinates)1166 bool ShelfView::Drag(const gfx::Point& location_in_screen_coordinates) {
1167   if (drag_and_drop_shelf_id_.IsNull() ||
1168       !GetBoundsInScreen().Contains(location_in_screen_coordinates))
1169     return false;
1170 
1171   gfx::Point pt = location_in_screen_coordinates;
1172   views::View* drag_and_drop_view =
1173       view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_));
1174   ConvertPointFromScreen(drag_and_drop_view, &pt);
1175   gfx::Point point_in_root = location_in_screen_coordinates;
1176   wm::ConvertPointFromScreen(
1177       window_util::GetRootWindowAt(location_in_screen_coordinates),
1178       &point_in_root);
1179   ui::MouseEvent event(ui::ET_MOUSE_DRAGGED, pt, point_in_root,
1180                        ui::EventTimeForNow(), 0, 0);
1181   PointerDraggedOnButton(drag_and_drop_view, DRAG_AND_DROP, event);
1182   return true;
1183 }
1184 
EndDrag(bool cancel)1185 void ShelfView::EndDrag(bool cancel) {
1186   drag_scroll_dir_ = 0;
1187   scrolling_timer_.Stop();
1188   speed_up_drag_scrolling_.Stop();
1189 
1190   if (drag_and_drop_shelf_id_.IsNull())
1191     return;
1192 
1193   views::View* drag_and_drop_view =
1194       view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_));
1195   PointerReleasedOnButton(drag_and_drop_view, DRAG_AND_DROP, cancel);
1196 
1197   // Either destroy the temporarily created item - or - make the item visible.
1198   if (drag_and_drop_item_pinned_ && cancel) {
1199     ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);
1200     model_->UnpinAppWithID(drag_and_drop_shelf_id_.app_id);
1201   } else if (drag_and_drop_view) {
1202     std::unique_ptr<gfx::AnimationDelegate> animation_delegate;
1203 
1204     // Resets the dragged view's opacity at the end of drag. Otherwise, if
1205     // the app is already pinned on shelf before drag starts, the dragged view
1206     // will be invisible when drag ends.
1207     animation_delegate =
1208         std::make_unique<StartFadeAnimationDelegate>(this, drag_and_drop_view);
1209 
1210     if (cancel) {
1211       // When a hosted drag gets canceled, the item can remain in the same slot
1212       // and it might have moved within the bounds. In that case the item need
1213       // to animate back to its correct location.
1214       AnimateToIdealBounds();
1215       bounds_animator_->SetAnimationDelegate(drag_and_drop_view,
1216                                              std::move(animation_delegate));
1217     } else {
1218       drag_and_drop_view->SetSize(pre_drag_and_drop_size_);
1219     }
1220   }
1221 
1222   drag_and_drop_shelf_id_ = ShelfID();
1223 }
1224 
SwapButtons(views::View * button_to_swap,bool with_next)1225 void ShelfView::SwapButtons(views::View* button_to_swap, bool with_next) {
1226   if (!button_to_swap)
1227     return;
1228 
1229   // Find the index of the button to swap in the view model.
1230   int src_index = -1;
1231   for (int i = 0; i < view_model_->view_size(); ++i) {
1232     View* view = view_model_->view_at(i);
1233     if (view == button_to_swap) {
1234       src_index = i;
1235       break;
1236     }
1237   }
1238 
1239   // Swapping items in the model is sufficient, everything will then be
1240   // reflected in the views.
1241   if (model_->Swap(src_index, with_next)) {
1242     AnimateToIdealBounds();
1243     const ShelfItem src_item = model_->items()[src_index];
1244     const ShelfItem dst_item =
1245         model_->items()[src_index + (with_next ? 1 : -1)];
1246     AnnounceSwapEvent(src_item, dst_item);
1247   }
1248 }
1249 
PointerPressedOnButton(views::View * view,Pointer pointer,const ui::LocatedEvent & event)1250 void ShelfView::PointerPressedOnButton(views::View* view,
1251                                        Pointer pointer,
1252                                        const ui::LocatedEvent& event) {
1253   if (drag_view_)
1254     return;
1255 
1256   if (IsShowingMenu())
1257     shelf_menu_model_adapter_->Cancel();
1258 
1259   int index = view_model_->GetIndexOfView(view);
1260   if (index == -1 || view_model_->view_size() < 1)
1261     return;  // View is being deleted, ignore request.
1262 
1263   // Only when the repost event occurs on the same shelf item, we should ignore
1264   // the call in ShelfView::ButtonPressed(...).
1265   is_repost_event_on_same_item_ =
1266       IsRepostEvent(event) && (last_pressed_index_ == index);
1267 
1268   CHECK_EQ(ShelfAppButton::kViewClassName, view->GetClassName());
1269   drag_view_ = static_cast<ShelfAppButton*>(view);
1270   drag_origin_ = gfx::Point(event.x(), event.y());
1271   UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage",
1272                             static_cast<ShelfAlignmentUmaEnumValue>(
1273                                 shelf_->SelectValueForShelfAlignment(
1274                                     SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM,
1275                                     SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT,
1276                                     SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT)),
1277                             SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT);
1278 }
1279 
PointerDraggedOnButton(views::View * view,Pointer pointer,const ui::LocatedEvent & event)1280 void ShelfView::PointerDraggedOnButton(views::View* view,
1281                                        Pointer pointer,
1282                                        const ui::LocatedEvent& event) {
1283   if (CanPrepareForDrag(pointer, event))
1284     PrepareForDrag(pointer, event);
1285 
1286   if (drag_pointer_ == pointer)
1287     ContinueDrag(event);
1288 }
1289 
PointerReleasedOnButton(views::View * view,Pointer pointer,bool canceled)1290 void ShelfView::PointerReleasedOnButton(views::View* view,
1291                                         Pointer pointer,
1292                                         bool canceled) {
1293   drag_scroll_dir_ = 0;
1294   scrolling_timer_.Stop();
1295   speed_up_drag_scrolling_.Stop();
1296 
1297   is_repost_event_on_same_item_ = false;
1298 
1299   if (canceled) {
1300     CancelDrag(-1);
1301   } else if (drag_pointer_ == pointer) {
1302     FinalizeRipOffDrag(false);
1303     drag_pointer_ = NONE;
1304 
1305     // Check if the pin status of |drag_view_| should be changed when
1306     // |drag_view_| is dragged over the separator. Do nothing if |drag_view_| is
1307     // already handled in FinalizedRipOffDrag.
1308     if (drag_view_) {
1309       if (ShouldUpdateDraggedViewPinStatus(view_model_->GetIndexOfView(view))) {
1310         const std::string drag_app_id = ShelfItemForView(drag_view_)->id.app_id;
1311         ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);
1312         if (model_->IsAppPinned(drag_app_id)) {
1313           model_->UnpinAppWithID(drag_app_id);
1314         } else {
1315           model_->PinAppWithID(drag_app_id);
1316         }
1317       }
1318     }
1319     AnimateToIdealBounds();
1320   }
1321 
1322   if (drag_pointer_ != NONE)
1323     return;
1324 
1325   drag_and_drop_host_->DestroyDragIconProxy();
1326 
1327   // If the drag pointer is NONE, no drag operation is going on and the
1328   // |drag_view_| can be released.
1329   drag_view_ = nullptr;
1330   drag_view_relative_to_ideal_bounds_ = RelativePosition::kNotAvailable;
1331 }
1332 
LayoutToIdealBounds()1333 void ShelfView::LayoutToIdealBounds() {
1334   if (bounds_animator_->IsAnimating()) {
1335     AnimateToIdealBounds();
1336     return;
1337   }
1338 
1339   CalculateIdealBounds();
1340   views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
1341   UpdateVisibleShelfItemBoundsUnion();
1342 }
1343 
IsItemPinned(const ShelfItem & item) const1344 bool ShelfView::IsItemPinned(const ShelfItem& item) const {
1345   return IsPinnedShelfItemType(item.type);
1346 }
1347 
IsItemVisible(const ShelfItem & item) const1348 bool ShelfView::IsItemVisible(const ShelfItem& item) const {
1349   return IsItemPinned(item) || item.is_on_active_desk;
1350 }
1351 
OnTabletModeChanged()1352 void ShelfView::OnTabletModeChanged() {
1353   // The layout change will happen as part of shelf config update.
1354 }
1355 
AnimateToIdealBounds()1356 void ShelfView::AnimateToIdealBounds() {
1357   CalculateIdealBounds();
1358 
1359   move_animation_tracker_.emplace(
1360       GetWidget()->GetCompositor()->RequestNewThroughputTracker());
1361   move_animation_tracker_->Start(metrics_util::ForSmoothness(
1362       base::BindRepeating(&ReportMoveAnimationSmoothness)));
1363 
1364   for (int i = 0; i < view_model_->view_size(); ++i) {
1365     View* view = view_model_->view_at(i);
1366     bounds_animator_->AnimateViewTo(view, view_model_->ideal_bounds(i));
1367     // Now that the item animation starts, we have to make sure that the
1368     // padding of the first gets properly transferred to the new first item.
1369     if (view->border())
1370       view->SetBorder(views::NullBorder());
1371   }
1372   UpdateVisibleShelfItemBoundsUnion();
1373 }
1374 
FadeIn(views::View * view)1375 void ShelfView::FadeIn(views::View* view) {
1376   view->SetVisible(true);
1377   view->layer()->SetOpacity(0);
1378 
1379   ui::ScopedLayerAnimationSettings fade_in_animation_settings(
1380       view->layer()->GetAnimator());
1381   fade_in_animation_settings.SetTweenType(gfx::Tween::EASE_OUT);
1382   fade_in_animation_settings.SetPreemptionStrategy(
1383       ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
1384   fade_in_animation_settings.AddObserver(fade_in_animation_delegate_.get());
1385 
1386   ui::AnimationThroughputReporter reporter(
1387       fade_in_animation_settings.GetAnimator(),
1388       metrics_util::ForSmoothness(
1389           base::BindRepeating(&ReportFadeInAnimationSmoothness)));
1390 
1391   view->layer()->SetOpacity(1.f);
1392 }
1393 
PrepareForDrag(Pointer pointer,const ui::LocatedEvent & event)1394 void ShelfView::PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event) {
1395   DCHECK(!dragging());
1396   DCHECK(drag_view_);
1397   drag_pointer_ = pointer;
1398   start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
1399   drag_scroll_dir_ = 0;
1400 
1401   if (start_drag_index_ == -1) {
1402     CancelDrag(-1);
1403     return;
1404   }
1405 
1406   // Cancel in-flight request for app item context menu model (made when app
1407   // context menu is requested), to prevent the pending callback from showing
1408   // a context menu just after drag starts.
1409   if (!context_menu_callback_.IsCancelled()) {
1410     context_menu_callback_.Cancel();
1411     item_awaiting_response_ = ShelfID();
1412   }
1413 
1414   // Move the view to the front so that it appears on top of other views.
1415   ReorderChildView(drag_view_, -1);
1416   bounds_animator_->StopAnimatingView(drag_view_);
1417 
1418   drag_view_->OnDragStarted(&event);
1419 
1420   drag_view_->layer()->SetOpacity(0.0f);
1421   drag_and_drop_host_->CreateDragIconProxyByLocationWithNoAnimation(
1422       event.root_location(), drag_view_->GetImage(), drag_view_,
1423       /*scale_factor=*/1.0f, /*blur_radius=*/0);
1424 }
1425 
ContinueDrag(const ui::LocatedEvent & event)1426 void ShelfView::ContinueDrag(const ui::LocatedEvent& event) {
1427   DCHECK(dragging());
1428   DCHECK(drag_view_);
1429   DCHECK_NE(-1, view_model_->GetIndexOfView(drag_view_));
1430 
1431   const bool dragged_off_shelf_before = dragged_off_shelf_;
1432 
1433   // Handle rip off functionality if this is not a drag and drop host operation
1434   // and not the app list item.
1435   if (drag_and_drop_shelf_id_.IsNull() &&
1436       RemovableByRipOff(view_model_->GetIndexOfView(drag_view_)) !=
1437           NOT_REMOVABLE) {
1438     HandleRipOffDrag(event);
1439     // Check if the item got ripped off the shelf - if it did we are done.
1440     if (dragged_off_shelf_) {
1441       drag_scroll_dir_ = 0;
1442       scrolling_timer_.Stop();
1443       speed_up_drag_scrolling_.Stop();
1444       if (!dragged_off_shelf_before)
1445         model_->OnItemRippedOff();
1446       return;
1447     }
1448   }
1449 
1450   // Calculates the drag point in screen before MoveDragViewTo is called.
1451   gfx::Point drag_point_in_screen(event.location());
1452   ConvertPointToScreen(drag_view_, &drag_point_in_screen);
1453 
1454   gfx::Point drag_point(event.location());
1455   ConvertPointToTarget(drag_view_, this, &drag_point);
1456   MoveDragViewTo(shelf_->PrimaryAxisValue(drag_point.x() - drag_origin_.x(),
1457                                           drag_point.y() - drag_origin_.y()));
1458   drag_and_drop_host_->UpdateDragIconProxy(drag_point_in_screen -
1459                                            drag_origin_.OffsetFromOrigin());
1460   if (dragged_off_shelf_before)
1461     model_->OnItemReturnedFromRipOff(view_model_->GetIndexOfView(drag_view_));
1462 }
1463 
MoveDragViewTo(int primary_axis_coordinate)1464 void ShelfView::MoveDragViewTo(int primary_axis_coordinate) {
1465   const int current_item_index = view_model_->GetIndexOfView(drag_view_);
1466   const std::pair<int, int> indices(GetDragRange(current_item_index));
1467   if (shelf_->IsHorizontalAlignment()) {
1468     int x = GetMirroredXWithWidthInView(primary_axis_coordinate,
1469                                         drag_view_->width());
1470     x = std::max(view_model_->ideal_bounds(indices.first).x(), x);
1471     x = std::min(view_model_->ideal_bounds(indices.second).right() -
1472                      view_model_->ideal_bounds(current_item_index).width(),
1473                  x);
1474     if (drag_view_->x() != x)
1475       drag_view_->SetX(x);
1476   } else {
1477     int y = std::max(view_model_->ideal_bounds(indices.first).y(),
1478                      primary_axis_coordinate);
1479     y = std::min(view_model_->ideal_bounds(indices.second).bottom() -
1480                      view_model_->ideal_bounds(current_item_index).height(),
1481                  y);
1482     if (drag_view_->y() != y)
1483       drag_view_->SetY(y);
1484   }
1485 
1486   int target_index = views::ViewModelUtils::DetermineMoveIndex(
1487       *view_model_, drag_view_, shelf_->IsHorizontalAlignment(),
1488       drag_view_->x(), drag_view_->y());
1489   target_index =
1490       base::ClampToRange(target_index, indices.first, indices.second);
1491 
1492   // Check the relative position of |drag_view_| and its ideal bounds if it can
1493   // be dragged across the separator to pin or unpin.
1494   if (CanDragAcrossSeparator(drag_view_)) {
1495     // Compare the center points of |drag_view_| and its ideal bounds to
1496     // determine whether the separator should be moved to the left or right by
1497     // using |drag_view_relative_to_ideal_bounds_|. The actual position will
1498     // be updated in CalculateIdealBounds.
1499     gfx::Point drag_view_center = drag_view_->bounds().CenterPoint();
1500     int drag_view_position =
1501         shelf()->PrimaryAxisValue(drag_view_center.x(), drag_view_center.y());
1502     gfx::Point ideal_bound_center =
1503         view_model_->ideal_bounds(target_index).CenterPoint();
1504     int ideal_bound_position = shelf()->PrimaryAxisValue(
1505         ideal_bound_center.x(), ideal_bound_center.y());
1506 
1507     drag_view_relative_to_ideal_bounds_ =
1508         drag_view_position < ideal_bound_position ? RelativePosition::kLeft
1509                                                   : RelativePosition::kRight;
1510     if (target_index == current_item_index) {
1511       AnimateToIdealBounds();
1512       NotifyAccessibilityEvent(ax::mojom::Event::kChildrenChanged,
1513                                true /* send_native_event */);
1514     }
1515   }
1516 
1517   if (target_index == current_item_index)
1518     return;
1519   // Change the model if the dragged item index is changed, the ShelfItemMoved()
1520   // callback will handle the |view_model_| update.
1521   model_->Move(current_item_index, target_index);
1522   bounds_animator_->StopAnimatingView(drag_view_);
1523 }
1524 
CreateDragIconProxy(const gfx::Point & location_in_screen_coordinates,const gfx::ImageSkia & icon,views::View * replaced_view,const gfx::Vector2d & cursor_offset_from_center,float scale_factor,bool animate_visibility)1525 void ShelfView::CreateDragIconProxy(
1526     const gfx::Point& location_in_screen_coordinates,
1527     const gfx::ImageSkia& icon,
1528     views::View* replaced_view,
1529     const gfx::Vector2d& cursor_offset_from_center,
1530     float scale_factor,
1531     bool animate_visibility) {
1532   drag_replaced_view_ = replaced_view;
1533   aura::Window* root_window =
1534       drag_replaced_view_->GetWidget()->GetNativeWindow()->GetRootWindow();
1535   drag_image_widget_ =
1536       DragImageView::Create(root_window, ui::mojom::DragEventSource::kMouse);
1537   DragImageView* drag_image = GetDragImage();
1538   drag_image->SetImage(icon);
1539   gfx::Size size = drag_image->GetPreferredSize();
1540   size.set_width(std::round(size.width() * scale_factor));
1541   size.set_height(std::round(size.height() * scale_factor));
1542   drag_image_offset_ = gfx::Vector2d(size.width() / 2, size.height() / 2) +
1543                        cursor_offset_from_center;
1544   gfx::Rect drag_image_bounds(
1545       location_in_screen_coordinates - drag_image_offset_, size);
1546   drag_image->SetBoundsInScreen(drag_image_bounds);
1547   if (!animate_visibility) {
1548     drag_image_widget_->SetVisibilityAnimationTransition(
1549         views::Widget::ANIMATE_NONE);
1550   }
1551   drag_image->SetWidgetVisible(true);
1552 }
1553 
HandleRipOffDrag(const ui::LocatedEvent & event)1554 void ShelfView::HandleRipOffDrag(const ui::LocatedEvent& event) {
1555   int current_index = view_model_->GetIndexOfView(drag_view_);
1556   DCHECK_NE(-1, current_index);
1557   std::string dragged_app_id = model_->items()[current_index].id.app_id;
1558 
1559   gfx::Point screen_location = event.root_location();
1560   ::wm::ConvertPointToScreen(GetWidget()->GetNativeWindow()->GetRootWindow(),
1561                              &screen_location);
1562 
1563   // To avoid ugly forwards and backwards flipping we use different constants
1564   // for ripping off / re-inserting the items.
1565   if (dragged_off_shelf_) {
1566     // If the shelf/overflow bubble bounds contains |screen_location| we insert
1567     // the item back into the shelf.
1568     if (GetBoundsForDragInsertInScreen().Contains(screen_location)) {
1569       drag_and_drop_host_->CreateDragIconProxyByLocationWithNoAnimation(
1570           event.root_location(), drag_view_->GetImage(), GetDragImage(),
1571           /*scale_factor=*/1.0f, /*blur_radius=*/0);
1572 
1573       // Destroy our proxy view item.
1574       DestroyDragIconProxy();
1575       // Re-insert the item and return simply false since the caller will handle
1576       // the move as in any normal case.
1577       dragged_off_shelf_ = false;
1578 
1579       return;
1580     }
1581     // Move our proxy view item.
1582     UpdateDragIconProxy(screen_location);
1583     return;
1584   }
1585 
1586   // Mark the item as dragged off the shelf if the drag distance exceeds
1587   // |kRipOffDistance|.
1588   int delta = CalculateShelfDistance(screen_location);
1589   bool dragged_off_shelf = delta > kRipOffDistance;
1590 
1591   if (dragged_off_shelf) {
1592     // Replaces a proxy icon provided by drag_and_drop_host_ - keep cursor
1593     // position consistent with the host provided icon, and disable
1594     // visibility animations (to prevent the proxy icon from lingering on
1595     // when replaced with the icon provided by the host).
1596     const gfx::Point center = drag_view_->GetLocalBounds().CenterPoint();
1597     const gfx::Vector2d cursor_offset_from_center = drag_origin_ - center;
1598     // Create a proxy view item which can be moved anywhere.
1599     CreateDragIconProxy(event.root_location(), drag_view_->GetImage(),
1600                         drag_view_, cursor_offset_from_center,
1601                         kDragAndDropProxyScale, /*animate_visibility=*/false);
1602 
1603     dragged_off_shelf_ = true;
1604 
1605     drag_and_drop_host_->DestroyDragIconProxy();
1606 
1607     if (RemovableByRipOff(current_index) == REMOVABLE) {
1608       // Move the item to the back and hide it. ShelfItemMoved() callback will
1609       // handle the |view_model_| update and call AnimateToIdealBounds().
1610       if (current_index != model_->item_count() - 1)
1611         model_->Move(current_index, model_->item_count() - 1);
1612       // Make the item partially disappear to show that it will get removed if
1613       // dropped.
1614       GetDragImage()->SetOpacity(kDraggedImageOpacity);
1615     }
1616   }
1617 }
1618 
FinalizeRipOffDrag(bool cancel)1619 void ShelfView::FinalizeRipOffDrag(bool cancel) {
1620   if (!dragged_off_shelf_)
1621     return;
1622   // Make sure we do not come in here again.
1623   dragged_off_shelf_ = false;
1624 
1625   // Coming here we should always have a |drag_view_|.
1626   DCHECK(drag_view_);
1627   int current_index = view_model_->GetIndexOfView(drag_view_);
1628   // If the view isn't part of the model anymore (|current_index| == -1), a sync
1629   // operation must have removed it. In that case we shouldn't change the model
1630   // and only delete the proxy image.
1631   if (current_index == -1) {
1632     DestroyDragIconProxy();
1633     return;
1634   }
1635 
1636   // Set to true when the animation should snap back to where it was before.
1637   bool snap_back = false;
1638   // Items which cannot be dragged off will be handled as a cancel.
1639   if (!cancel) {
1640     if (RemovableByRipOff(current_index) != REMOVABLE) {
1641       // Make sure we do not try to remove un-removable items like items which
1642       // were not pinned or have to be always there.
1643       cancel = true;
1644       snap_back = true;
1645     } else {
1646       // Make sure the item stays invisible upon removal.
1647       drag_view_->SetVisible(false);
1648       ShelfModel::ScopedUserTriggeredMutation user_triggered(model_);
1649       model_->UnpinAppWithID(model_->items()[current_index].id.app_id);
1650     }
1651   }
1652   if (cancel || snap_back) {
1653     if (!cancelling_drag_model_changed_) {
1654       // Only do something if the change did not come through a model change.
1655       gfx::Rect drag_bounds = GetDragImage()->GetBoundsInScreen();
1656       gfx::Point relative_to = GetBoundsInScreen().origin();
1657       gfx::Rect target(
1658           gfx::PointAtOffsetFromOrigin(drag_bounds.origin() - relative_to),
1659           drag_bounds.size());
1660       drag_view_->SetBoundsRect(target);
1661       // Hide the status from the active item since we snap it back now. Upon
1662       // animation end the flag gets cleared if |snap_back_from_rip_off_view_|
1663       // is set.
1664       snap_back_from_rip_off_view_ = drag_view_;
1665       drag_view_->AddState(ShelfAppButton::STATE_HIDDEN);
1666       // When a canceling drag model is happening, the view model is diverged
1667       // from the menu model and movements / animations should not be done.
1668       model_->Move(current_index, start_drag_index_);
1669       AnimateToIdealBounds();
1670     }
1671     drag_view_->layer()->SetOpacity(1.0f);
1672     model_->OnItemReturnedFromRipOff(model_->item_count() - 1);
1673   }
1674   DestroyDragIconProxy();
1675 }
1676 
RemovableByRipOff(int index) const1677 ShelfView::RemovableState ShelfView::RemovableByRipOff(int index) const {
1678   DCHECK(index >= 0 && index < model_->item_count());
1679   ShelfItemType type = model_->items()[index].type;
1680   if (type == TYPE_DIALOG)
1681     return NOT_REMOVABLE;
1682 
1683   if (model_->items()[index].pinned_by_policy)
1684     return NOT_REMOVABLE;
1685 
1686   // Note: Only pinned app shortcuts can be removed!
1687   const std::string& app_id = model_->items()[index].id.app_id;
1688   return (type == TYPE_PINNED_APP && model_->IsAppPinned(app_id)) ? REMOVABLE
1689                                                                   : DRAGGABLE;
1690 }
1691 
SameDragType(ShelfItemType typea,ShelfItemType typeb) const1692 bool ShelfView::SameDragType(ShelfItemType typea, ShelfItemType typeb) const {
1693   if (IsPinnedShelfItemType(typea) && IsPinnedShelfItemType(typeb))
1694     return true;
1695   if (typea == TYPE_UNDEFINED || typeb == TYPE_UNDEFINED) {
1696     NOTREACHED() << "ShelfItemType must be set.";
1697     return false;
1698   }
1699   // Running app or dialog.
1700   return typea == typeb;
1701 }
1702 
ShouldFocusOut(bool reverse,views::View * button)1703 bool ShelfView::ShouldFocusOut(bool reverse, views::View* button) {
1704   // The logic here seems backwards, but is actually correct. For instance if
1705   // the ShelfView's internal focus cycling logic attemmpts to focus the first
1706   // child after hitting Tab, we intercept that and instead, advance through
1707   // to the status area.
1708   return (reverse && button == FindLastFocusableChild()) ||
1709          (!reverse && button == FindFirstFocusableChild());
1710 }
1711 
GetDragRange(int index)1712 std::pair<int, int> ShelfView::GetDragRange(int index) {
1713   DCHECK(base::Contains(visible_views_indices_, index));
1714   const ShelfItem& dragged_item = model_->items()[index];
1715 
1716   // If |drag_view_| is allowed to be dragged across the separator, return the
1717   // first and the last index of the |visible_views_indices_|.
1718   if (CanDragAcrossSeparator(drag_view_)) {
1719     return std::make_pair(visible_views_indices_[0],
1720                           visible_views_indices_.back());
1721   }
1722 
1723   int first = -1;
1724   int last = -1;
1725   for (int i : visible_views_indices_) {
1726     if (SameDragType(model_->items()[i].type, dragged_item.type)) {
1727       if (first == -1)
1728         first = i;
1729       last = i;
1730     } else if (first != -1) {
1731       break;
1732     }
1733   }
1734   DCHECK_NE(first, -1);
1735   DCHECK_NE(last, -1);
1736 
1737   // TODO(afakhry): Consider changing this when taking into account inactive
1738   // desks.
1739   return std::make_pair(first, last);
1740 }
1741 
ShouldUpdateDraggedViewPinStatus(int dragged_view_index)1742 bool ShelfView::ShouldUpdateDraggedViewPinStatus(int dragged_view_index) {
1743   if (!features::IsDragUnpinnedAppToPinEnabled())
1744     return false;
1745 
1746   DCHECK(base::Contains(visible_views_indices_, dragged_view_index));
1747   bool is_moved_item_pinned =
1748       IsPinnedShelfItemType(model_->items()[dragged_view_index].type);
1749   if (separator_index_ == -1) {
1750     // If |separator_index_| equals to -1, all the apps in shelf are expected to
1751     // have the same pinned status.
1752     for (auto index : visible_views_indices_) {
1753       if (index != dragged_view_index) {
1754         // Return true if the pin status of the moved item is different from
1755         // others.
1756         return is_moved_item_pinned !=
1757                IsPinnedShelfItemType(model_->items()[index].type);
1758       }
1759     }
1760     return false;
1761   }
1762   // If the separator is shown, check whether the pin status of dragged item
1763   // matches the pin status implied by the dragged view position relative to the
1764   // separator.
1765   bool should_pinned_by_position = dragged_view_index <= separator_index_;
1766   return should_pinned_by_position != is_moved_item_pinned;
1767 }
1768 
CanDragAcrossSeparator(views::View * drag_view) const1769 bool ShelfView::CanDragAcrossSeparator(views::View* drag_view) const {
1770   if (!features::IsDragUnpinnedAppToPinEnabled())
1771     return false;
1772 
1773   DCHECK(drag_view);
1774   // The dragged item is not allowed to be unpinned if |drag_view| is pinned by
1775   // policy, dragged from app list, or its item type is TYPE_BROWSER_SHORTCUT.
1776   // Therefore, the |drag_view| can not be dragged across the separator.
1777   bool can_change_pin_state =
1778       ShelfItemForView(drag_view)->type == TYPE_PINNED_APP ||
1779       ShelfItemForView(drag_view)->type == TYPE_APP;
1780   // Note that |drag_and_drop_shelf_id_| is set only when the current drag view
1781   // is from app list, which can not be dragged to the unpinned app side.
1782   return !ShelfItemForView(drag_view)->pinned_by_policy &&
1783          drag_and_drop_shelf_id_ == ShelfID() && can_change_pin_state;
1784 }
1785 
OnFadeInAnimationEnded()1786 void ShelfView::OnFadeInAnimationEnded() {
1787   // Call PreferredSizeChanged() to notify container to re-layout at the end
1788   // of fade-in animation.
1789   PreferredSizeChanged();
1790 }
1791 
OnFadeOutAnimationEnded()1792 void ShelfView::OnFadeOutAnimationEnded() {
1793   if (fade_out_animation_tracker_) {
1794     fade_out_animation_tracker_->Stop();
1795     fade_out_animation_tracker_.reset();
1796   }
1797 
1798   // Call PreferredSizeChanged() to notify container to re-layout at the end
1799   // of removal animation.
1800   PreferredSizeChanged();
1801 
1802   AnimateToIdealBounds();
1803 }
1804 
GetMenuAnchorRect(const views::View & source,const gfx::Point & location,bool context_menu) const1805 gfx::Rect ShelfView::GetMenuAnchorRect(const views::View& source,
1806                                        const gfx::Point& location,
1807                                        bool context_menu) const {
1808   // Application menus for items are anchored on the icon bounds.
1809   if (ShelfItemForView(&source) || !context_menu)
1810     return source.GetBoundsInScreen();
1811 
1812   const gfx::Rect shelf_bounds_in_screen = GetBoundsInScreen();
1813   gfx::Point origin;
1814   switch (shelf_->alignment()) {
1815     case ShelfAlignment::kBottom:
1816     case ShelfAlignment::kBottomLocked:
1817       origin = gfx::Point(location.x(), shelf_bounds_in_screen.y());
1818       break;
1819     case ShelfAlignment::kLeft:
1820       origin = gfx::Point(shelf_bounds_in_screen.right(), location.y());
1821       break;
1822     case ShelfAlignment::kRight:
1823       origin = gfx::Point(shelf_bounds_in_screen.x(), location.y());
1824       break;
1825   }
1826   return gfx::Rect(origin, gfx::Size());
1827 }
1828 
AnnounceShelfAlignment()1829 void ShelfView::AnnounceShelfAlignment() {
1830   base::string16 announcement;
1831   switch (shelf_->alignment()) {
1832     case ShelfAlignment::kBottom:
1833     case ShelfAlignment::kBottomLocked:
1834       announcement = l10n_util::GetStringUTF16(IDS_SHELF_ALIGNMENT_BOTTOM);
1835       break;
1836     case ShelfAlignment::kLeft:
1837       announcement = l10n_util::GetStringUTF16(IDS_SHELF_ALIGNMENT_LEFT);
1838       break;
1839     case ShelfAlignment::kRight:
1840       announcement = l10n_util::GetStringUTF16(IDS_SHELF_ALIGNMENT_RIGHT);
1841       break;
1842   }
1843   announcement_view_->GetViewAccessibility().OverrideName(announcement);
1844   announcement_view_->NotifyAccessibilityEvent(ax::mojom::Event::kAlert,
1845                                                /*send_native_event=*/true);
1846 }
1847 
AnnounceShelfAutohideBehavior()1848 void ShelfView::AnnounceShelfAutohideBehavior() {
1849   base::string16 announcement;
1850   switch (shelf_->auto_hide_behavior()) {
1851     case ShelfAutoHideBehavior::kAlways:
1852       announcement = l10n_util::GetStringUTF16(IDS_SHELF_STATE_AUTO_HIDE);
1853       break;
1854     case ShelfAutoHideBehavior::kNever:
1855       announcement = l10n_util::GetStringUTF16(IDS_SHELF_STATE_ALWAYS_SHOWN);
1856       break;
1857     case ShelfAutoHideBehavior::kAlwaysHidden:
1858       announcement = l10n_util::GetStringUTF16(IDS_SHELF_STATE_ALWAYS_HIDDEN);
1859       break;
1860   }
1861   announcement_view_->GetViewAccessibility().OverrideName(announcement);
1862   announcement_view_->NotifyAccessibilityEvent(ax::mojom::Event::kAlert,
1863                                                /*send_native_event=*/true);
1864 }
1865 
AnnouncePinUnpinEvent(const ShelfItem & item,bool pinned)1866 void ShelfView::AnnouncePinUnpinEvent(const ShelfItem& item, bool pinned) {
1867   base::string16 item_title =
1868       item.title.empty()
1869           ? l10n_util::GetStringUTF16(IDS_SHELF_ITEM_GENERIC_NAME)
1870           : item.title;
1871   base::string16 announcement = l10n_util::GetStringFUTF16(
1872       pinned ? IDS_SHELF_ITEM_WAS_PINNED : IDS_SHELF_ITEM_WAS_UNPINNED,
1873       item_title);
1874   announcement_view_->GetViewAccessibility().OverrideName(announcement);
1875   announcement_view_->NotifyAccessibilityEvent(ax::mojom::Event::kAlert,
1876                                                /*send_native_event=*/true);
1877 }
1878 
AnnounceSwapEvent(const ShelfItem & first_item,const ShelfItem & second_item)1879 void ShelfView::AnnounceSwapEvent(const ShelfItem& first_item,
1880                                   const ShelfItem& second_item) {
1881   base::string16 first_item_title =
1882       first_item.title.empty()
1883           ? l10n_util::GetStringUTF16(IDS_SHELF_ITEM_GENERIC_NAME)
1884           : first_item.title;
1885   base::string16 second_item_title =
1886       second_item.title.empty()
1887           ? l10n_util::GetStringUTF16(IDS_SHELF_ITEM_GENERIC_NAME)
1888           : second_item.title;
1889   base::string16 announcement = l10n_util::GetStringFUTF16(
1890       IDS_SHELF_ITEMS_WERE_SWAPPED, first_item_title, second_item_title);
1891   announcement_view_->GetViewAccessibility().OverrideName(announcement);
1892   announcement_view_->NotifyAccessibilityEvent(ax::mojom::Event::kAlert,
1893                                                /*send_native_event=*/true);
1894 }
1895 
GetBoundsForDragInsertInScreen()1896 gfx::Rect ShelfView::GetBoundsForDragInsertInScreen() {
1897   const ScrollableShelfView* scrollable_shelf_view =
1898       shelf_->hotseat_widget()->scrollable_shelf_view();
1899   gfx::Rect bounds = scrollable_shelf_view->visible_space();
1900   views::View::ConvertRectToScreen(scrollable_shelf_view, &bounds);
1901   return bounds;
1902 }
1903 
CancelDrag(int modified_index)1904 int ShelfView::CancelDrag(int modified_index) {
1905   drag_scroll_dir_ = 0;
1906   scrolling_timer_.Stop();
1907   speed_up_drag_scrolling_.Stop();
1908 
1909   FinalizeRipOffDrag(true);
1910   if (!drag_view_)
1911     return modified_index;
1912   bool was_dragging = dragging();
1913   int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1914   drag_pointer_ = NONE;
1915   drag_view_ = nullptr;
1916   if (drag_view_index == modified_index) {
1917     // The view that was being dragged is being modified. Don't do anything.
1918     return modified_index;
1919   }
1920   if (!was_dragging)
1921     return modified_index;
1922 
1923   // Restore previous position, tracking the position of the modified view.
1924   bool at_end = modified_index == view_model_->view_size();
1925   views::View* modified_view = (modified_index >= 0 && !at_end)
1926                                    ? view_model_->view_at(modified_index)
1927                                    : nullptr;
1928   model_->Move(drag_view_index, start_drag_index_);
1929 
1930   // If the modified view will be at the end of the list, return the new end of
1931   // the list.
1932   if (at_end)
1933     return view_model_->view_size();
1934   return modified_view ? view_model_->GetIndexOfView(modified_view) : -1;
1935 }
1936 
OnGestureEvent(ui::GestureEvent * event)1937 void ShelfView::OnGestureEvent(ui::GestureEvent* event) {
1938   if (!ShouldHandleGestures(*event))
1939     return;
1940 
1941   if (HandleGestureEvent(event))
1942     event->StopPropagation();
1943 }
1944 
ShelfItemAdded(int model_index)1945 void ShelfView::ShelfItemAdded(int model_index) {
1946   {
1947     base::AutoReset<bool> cancelling_drag(&cancelling_drag_model_changed_,
1948                                           true);
1949     model_index = CancelDrag(model_index);
1950   }
1951   const ShelfItem& item(model_->items()[model_index]);
1952   views::View* view = CreateViewForItem(item);
1953   // Hide the view, it'll be made visible when the animation is done. Using
1954   // opacity 0 here to avoid messing with CalculateIdealBounds which touches
1955   // the view's visibility.
1956   view->layer()->SetOpacity(0);
1957   view_model_->Add(view, model_index);
1958 
1959   // Add child view so it has the same ordering as in the |view_model_|.
1960   // Note: No need to call UpdateShelfItemViewsVisibility() here directly, since
1961   // it will be called by ScrollableShelfView::ViewHierarchyChanged() as a
1962   // result of the below call.
1963   AddChildViewAt(view, model_index);
1964 
1965   // Give the button its ideal bounds. That way if we end up animating the
1966   // button before this animation completes it doesn't appear at some random
1967   // spot (because it was in the middle of animating from 0,0 0x0 to its
1968   // target).
1969   CalculateIdealBounds();
1970   view->SetBoundsRect(view_model_->ideal_bounds(model_index));
1971 
1972   if (model_->is_current_mutation_user_triggered() &&
1973       drag_and_drop_shelf_id_ != item.id) {
1974     view->ScrollViewToVisible();
1975   }
1976 
1977   // The first animation moves all the views to their target position. |view|
1978   // is hidden, so it visually appears as though we are providing space for
1979   // it. When done we'll fade the view in.
1980   AnimateToIdealBounds();
1981   DCHECK_LE(model_index, visible_views_indices_.back());
1982   bounds_animator_->SetAnimationDelegate(
1983       view, std::unique_ptr<gfx::AnimationDelegate>(
1984                 new StartFadeAnimationDelegate(this, view)));
1985 
1986   if (model_->is_current_mutation_user_triggered() &&
1987       item.type == TYPE_PINNED_APP) {
1988     AnnouncePinUnpinEvent(item, /*pinned=*/true);
1989   }
1990 }
1991 
ShelfItemRemoved(int model_index,const ShelfItem & old_item)1992 void ShelfView::ShelfItemRemoved(int model_index, const ShelfItem& old_item) {
1993   if (old_item.id == context_menu_id_ && shelf_menu_model_adapter_)
1994     shelf_menu_model_adapter_->Cancel();
1995 
1996   // If std::move is not called on |view|, |view| will be deleted once out of
1997   // scope.
1998   std::unique_ptr<views::View> view(view_model_->view_at(model_index));
1999   view_model_->Remove(model_index);
2000 
2001   {
2002     base::AutoReset<bool> cancelling_drag(&cancelling_drag_model_changed_,
2003                                           true);
2004     CancelDrag(-1);
2005   }
2006 
2007   if (view.get() == shelf_->tooltip()->GetCurrentAnchorView())
2008     shelf_->tooltip()->Close();
2009 
2010   if (view->GetVisible() && view->layer()->opacity() > 0.0f) {
2011     UpdateShelfItemViewsVisibility();
2012 
2013     // There could be multiple fade out animations running. Only start
2014     // tracking for the first one.
2015     if (!fade_out_animation_tracker_) {
2016       fade_out_animation_tracker_.emplace(
2017           GetWidget()->GetCompositor()->RequestNewThroughputTracker());
2018       fade_out_animation_tracker_->Start(metrics_util::ForSmoothness(
2019           base::BindRepeating(&ReportFadeOutAnimationSmoothness)));
2020     }
2021 
2022     // The first animation fades out the view. When done we'll animate the rest
2023     // of the views to their target location.
2024     bounds_animator_->AnimateViewTo(view.get(), view->bounds());
2025     bounds_animator_->SetAnimationDelegate(
2026         view.get(), std::unique_ptr<gfx::AnimationDelegate>(
2027                         new FadeOutAnimationDelegate(this, std::move(view))));
2028   } else {
2029     // Ensures that |view| is not used after destruction.
2030     StopAnimatingViewIfAny(view.get());
2031 
2032     // Removes |view| to trigger ViewHierarchyChanged function in the parent
2033     // view if any.
2034     view.reset();
2035 
2036     // If there is no fade out animation, notify the parent view of the
2037     // changed size before bounds animations start.
2038     PreferredSizeChanged();
2039 
2040     // We don't need to show a fade out animation for invisible |view|. When an
2041     // item is ripped out from the shelf, its |view| is already invisible.
2042     AnimateToIdealBounds();
2043   }
2044 
2045   if (model_->is_current_mutation_user_triggered() &&
2046       old_item.type == TYPE_PINNED_APP) {
2047     AnnouncePinUnpinEvent(old_item, /*pinned=*/false);
2048   }
2049 }
2050 
ShelfItemChanged(int model_index,const ShelfItem & old_item)2051 void ShelfView::ShelfItemChanged(int model_index, const ShelfItem& old_item) {
2052   // Bail if the view and shelf sizes do not match. ShelfItemChanged may be
2053   // called here before ShelfItemAdded, due to ChromeLauncherController's
2054   // item initialization, which calls SetItem during ShelfItemAdded.
2055   if (static_cast<int>(model_->items().size()) != view_model_->view_size())
2056     return;
2057 
2058   const ShelfItem& item = model_->items()[model_index];
2059 
2060   // If there's a change in the item's active desk, perform the update at the
2061   // end of this function in order to guarantee that both |model_| and
2062   // |view_model_| are consistent if there are other changes in the item.
2063   base::ScopedClosureRunner run_at_scope_exit;
2064   if (old_item.is_on_active_desk != item.is_on_active_desk) {
2065     run_at_scope_exit.ReplaceClosure(base::BindOnce(
2066         &ShelfView::ShelfItemsUpdatedForDeskChange, base::Unretained(this)));
2067   }
2068 
2069   if (old_item.type != item.type) {
2070     // Type changed, swap the views.
2071     model_index = CancelDrag(model_index);
2072     std::unique_ptr<views::View> old_view(view_model_->view_at(model_index));
2073     bounds_animator_->StopAnimatingView(old_view.get());
2074     // Removing and re-inserting a view in our view model will strip the ideal
2075     // bounds from the item. To avoid recalculation of everything the bounds
2076     // get remembered and restored after the insertion to the previous value.
2077     gfx::Rect old_ideal_bounds = view_model_->ideal_bounds(model_index);
2078     view_model_->Remove(model_index);
2079     views::View* new_view = CreateViewForItem(item);
2080     // The view must be added to the |view_model_| before it's added as a child
2081     // so that the model is consistent when UpdateShelfItemViewsVisibility() is
2082     // called as a result the hierarchy changes caused by AddChildView(). See
2083     // ScrollableShelfView::ViewHierarchyChanged().
2084     view_model_->Add(new_view, model_index);
2085     AddChildView(new_view);
2086     view_model_->set_ideal_bounds(model_index, old_ideal_bounds);
2087 
2088     bounds_animator_->StopAnimatingView(new_view);
2089     new_view->SetBoundsRect(old_view->bounds());
2090     bounds_animator_->AnimateViewTo(new_view, old_ideal_bounds);
2091 
2092     // If an item is being pinned or unpinned, show the new status of the
2093     // shelf immediately so that the separator gets drawn as needed.
2094     if (old_item.type == TYPE_PINNED_APP || item.type == TYPE_PINNED_APP) {
2095       if (model_->is_current_mutation_user_triggered())
2096         AnnouncePinUnpinEvent(old_item, item.type == TYPE_PINNED_APP);
2097       AnimateToIdealBounds();
2098     }
2099     return;
2100   }
2101 
2102   views::View* view = view_model_->view_at(model_index);
2103   switch (item.type) {
2104     case TYPE_PINNED_APP:
2105     case TYPE_BROWSER_SHORTCUT:
2106     case TYPE_APP:
2107     case TYPE_DIALOG: {
2108       CHECK_EQ(ShelfAppButton::kViewClassName, view->GetClassName());
2109       ShelfAppButton* button = static_cast<ShelfAppButton*>(view);
2110       button->ReflectItemStatus(item);
2111       button->SetImage(item.image);
2112       button->SchedulePaint();
2113       break;
2114     }
2115     case TYPE_UNDEFINED:
2116       break;
2117   }
2118 }
2119 
ShelfItemsUpdatedForDeskChange()2120 void ShelfView::ShelfItemsUpdatedForDeskChange() {
2121   DCHECK(features::IsPerDeskShelfEnabled());
2122 
2123   // The order here matters, since switching/removing desks, or moving windows
2124   // between desks will affect shelf items' visibility, we need to update the
2125   // visibility of the views first before we layout.
2126   UpdateShelfItemViewsVisibility();
2127   // Signal to the parent ScrollableShelfView so that it can recenter the items
2128   // after their visibility have been updated (via
2129   // `UpdateAvailableSpaceAndScroll()`).
2130   PreferredSizeChanged();
2131   LayoutToIdealBounds();
2132 }
2133 
ShelfItemMoved(int start_index,int target_index)2134 void ShelfView::ShelfItemMoved(int start_index, int target_index) {
2135   view_model_->Move(start_index, target_index);
2136 
2137   // Reorder the child view to be in the same order as in the |view_model_|.
2138   ReorderChildView(view_model_->view_at(target_index), target_index);
2139   NotifyAccessibilityEvent(ax::mojom::Event::kChildrenChanged,
2140                            true /* send_native_event */);
2141 
2142   // When cancelling a drag due to a shelf item being added, the currently
2143   // dragged item is moved back to its initial position. AnimateToIdealBounds
2144   // will be called again when the new item is added to the |view_model_| but
2145   // at this time the |view_model_| is inconsistent with the |model_|.
2146   if (!cancelling_drag_model_changed_)
2147     AnimateToIdealBounds();
2148 }
2149 
ShelfItemDelegateChanged(const ShelfID & id,ShelfItemDelegate * old_delegate,ShelfItemDelegate * delegate)2150 void ShelfView::ShelfItemDelegateChanged(const ShelfID& id,
2151                                          ShelfItemDelegate* old_delegate,
2152                                          ShelfItemDelegate* delegate) {
2153   if (id == context_menu_id_ && shelf_menu_model_adapter_)
2154     shelf_menu_model_adapter_->Cancel();
2155 }
2156 
ShelfItemStatusChanged(const ShelfID & id)2157 void ShelfView::ShelfItemStatusChanged(const ShelfID& id) {
2158   scoped_display_for_new_windows_.reset();
2159 
2160   int index = model_->ItemIndexByID(id);
2161   if (index < 0)
2162     return;
2163 
2164   const ShelfItem item = model_->items()[index];
2165   ShelfAppButton* button = GetShelfAppButton(id);
2166   button->ReflectItemStatus(item);
2167   button->SchedulePaint();
2168 }
2169 
ShelfItemRippedOff()2170 void ShelfView::ShelfItemRippedOff() {
2171   // On the display where the drag started, there is nothing to do.
2172   if (dragging())
2173     return;
2174   // When a dragged item has been ripped off the shelf, it is moved to the end.
2175   // Now we need to hide it.
2176   view_model_->view_at(model_->item_count() - 1)->layer()->SetOpacity(0.f);
2177 }
2178 
ShelfItemReturnedFromRipOff(int index)2179 void ShelfView::ShelfItemReturnedFromRipOff(int index) {
2180   // On the display where the drag started, there is nothing to do.
2181   if (dragging())
2182     return;
2183   // Show the item and prevent it from animating into place from the position
2184   // where it was sitting with zero opacity.
2185   views::View* view = view_model_->view_at(index);
2186   const gfx::Rect bounds = bounds_animator_->GetTargetBounds(view);
2187   bounds_animator_->StopAnimatingView(view);
2188   view->SetBoundsRect(bounds);
2189   view->layer()->SetOpacity(1.f);
2190 }
2191 
OnShelfAlignmentChanged(aura::Window * root_window,ShelfAlignment old_alignment)2192 void ShelfView::OnShelfAlignmentChanged(aura::Window* root_window,
2193                                         ShelfAlignment old_alignment) {
2194   LayoutToIdealBounds();
2195   for (const auto& visible_index : visible_views_indices_)
2196     view_model_->view_at(visible_index)->Layout();
2197 
2198   AnnounceShelfAlignment();
2199 }
2200 
OnShelfAutoHideBehaviorChanged(aura::Window * root_window)2201 void ShelfView::OnShelfAutoHideBehaviorChanged(aura::Window* root_window) {
2202   AnnounceShelfAutohideBehavior();
2203 }
2204 
AfterItemSelected(const ShelfItem & item,views::Button * sender,std::unique_ptr<ui::Event> event,views::InkDrop * ink_drop,ShelfAction action,ShelfItemDelegate::AppMenuItems menu_items)2205 void ShelfView::AfterItemSelected(const ShelfItem& item,
2206                                   views::Button* sender,
2207                                   std::unique_ptr<ui::Event> event,
2208                                   views::InkDrop* ink_drop,
2209                                   ShelfAction action,
2210                                   ShelfItemDelegate::AppMenuItems menu_items) {
2211   item_awaiting_response_ = ShelfID();
2212   shelf_button_pressed_metric_tracker_.ButtonPressed(*event, sender, action);
2213 
2214   // Record AppList metric for any action considered an app launch.
2215   if (action == SHELF_ACTION_NEW_WINDOW_CREATED ||
2216       action == SHELF_ACTION_WINDOW_ACTIVATED) {
2217     Shell::Get()->app_list_controller()->RecordShelfAppLaunched();
2218   }
2219 
2220   // The app list handles its own ink drop effect state changes.
2221   if (action == SHELF_ACTION_APP_LIST_DISMISSED) {
2222     ink_drop->SnapToActivated();
2223     ink_drop->AnimateToState(views::InkDropState::HIDDEN);
2224   } else if (action != SHELF_ACTION_APP_LIST_SHOWN && !dragging()) {
2225     if (action != SHELF_ACTION_NEW_WINDOW_CREATED && menu_items.size() > 1 &&
2226         !dragging()) {
2227       // Show the app menu with 2 or more items, if no window was created. The
2228       // menu is not shown in case item drag started while the selection request
2229       // was in progress.
2230       ink_drop->AnimateToState(views::InkDropState::ACTIVATED);
2231       context_menu_id_ = item.id;
2232       ShowMenu(std::make_unique<ShelfApplicationMenuModel>(
2233                    item.title, std::move(menu_items),
2234                    model_->GetShelfItemDelegate(item.id)),
2235                sender, gfx::Point(), /*context_menu=*/false,
2236                ui::GetMenuSourceTypeForEvent(*event));
2237       shelf_->UpdateVisibilityState();
2238     } else {
2239       ink_drop->AnimateToState(views::InkDropState::ACTION_TRIGGERED);
2240     }
2241   }
2242   shelf_->shelf_layout_manager()->OnShelfItemSelected(action);
2243 }
2244 
ShowShelfContextMenu(const ShelfID & shelf_id,const gfx::Point & point,views::View * source,ui::MenuSourceType source_type,std::unique_ptr<ui::SimpleMenuModel> model)2245 void ShelfView::ShowShelfContextMenu(
2246     const ShelfID& shelf_id,
2247     const gfx::Point& point,
2248     views::View* source,
2249     ui::MenuSourceType source_type,
2250     std::unique_ptr<ui::SimpleMenuModel> model) {
2251   context_menu_id_ = shelf_id;
2252   if (!model) {
2253     const int64_t display_id = GetDisplayIdForView(this);
2254     model = std::make_unique<ShelfContextMenuModel>(nullptr, display_id);
2255   }
2256   ShowMenu(std::move(model), source, point, /*context_menu=*/true, source_type);
2257 }
2258 
ShowMenu(std::unique_ptr<ui::SimpleMenuModel> menu_model,views::View * source,const gfx::Point & click_point,bool context_menu,ui::MenuSourceType source_type)2259 void ShelfView::ShowMenu(std::unique_ptr<ui::SimpleMenuModel> menu_model,
2260                          views::View* source,
2261                          const gfx::Point& click_point,
2262                          bool context_menu,
2263                          ui::MenuSourceType source_type) {
2264   // Delayed callbacks to show context and application menus may conflict; hide
2265   // the old menu before showing a new menu in that case.
2266   if (IsShowingMenu())
2267     shelf_menu_model_adapter_->Cancel();
2268 
2269   item_awaiting_response_ = ShelfID();
2270   if (menu_model->GetItemCount() == 0)
2271     return;
2272   menu_owner_ = source;
2273 
2274   closing_event_time_ = base::TimeTicks();
2275 
2276   // NOTE: If you convert to HAS_MNEMONICS be sure to update menu building code.
2277   int run_types = views::MenuRunner::USE_TOUCHABLE_LAYOUT;
2278   if (context_menu) {
2279     run_types |=
2280         views::MenuRunner::CONTEXT_MENU | views::MenuRunner::FIXED_ANCHOR;
2281   }
2282 
2283   const ShelfItem* item = ShelfItemForView(source);
2284   // Only selected shelf items with context menu opened can be dragged.
2285   if (context_menu && item && ShelfButtonIsInDrag(item->type, source) &&
2286       source_type == ui::MenuSourceType::MENU_SOURCE_TOUCH) {
2287     run_types |= views::MenuRunner::SEND_GESTURE_EVENTS_TO_OWNER;
2288   }
2289 
2290   shelf_menu_model_adapter_ = std::make_unique<ShelfMenuModelAdapter>(
2291       item ? item->id.app_id : std::string(), std::move(menu_model), source,
2292       source_type,
2293       base::BindOnce(&ShelfView::OnMenuClosed, base::Unretained(this), source),
2294       IsTabletModeEnabled(),
2295       /*for_application_menu_items*/ !context_menu);
2296   shelf_menu_model_adapter_->Run(
2297       GetMenuAnchorRect(*source, click_point, context_menu),
2298       shelf_->IsHorizontalAlignment() ? views::MenuAnchorPosition::kBubbleAbove
2299                                       : views::MenuAnchorPosition::kBubbleLeft,
2300       run_types);
2301 
2302   if (!context_menu_shown_callback_.is_null())
2303     context_menu_shown_callback_.Run();
2304 }
2305 
OnMenuClosed(views::View * source)2306 void ShelfView::OnMenuClosed(views::View* source) {
2307   menu_owner_ = nullptr;
2308   context_menu_id_ = ShelfID();
2309 
2310   closing_event_time_ = shelf_menu_model_adapter_->GetClosingEventTime();
2311 
2312   const ShelfItem* item = ShelfItemForView(source);
2313   if (item)
2314     static_cast<ShelfAppButton*>(source)->OnMenuClosed();
2315 
2316   shelf_menu_model_adapter_.reset();
2317 
2318   const bool is_in_drag = item && ShelfButtonIsInDrag(item->type, source);
2319   // Update the shelf visibility since auto-hide or alignment might have
2320   // changes, but don't update if shelf item is being dragged. Since shelf
2321   // should be kept as visible during shelf item drag even menu is closed.
2322   if (!is_in_drag)
2323     shelf_->UpdateVisibilityState();
2324 }
2325 
OnBoundsAnimatorProgressed(views::BoundsAnimator * animator)2326 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) {
2327   shelf_->NotifyShelfIconPositionsChanged();
2328 
2329   // Do not call PreferredSizeChanged() so that container does not re-layout
2330   // during the bounds animation.
2331 }
2332 
OnBoundsAnimatorDone(views::BoundsAnimator * animator)2333 void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
2334   shelf_->set_is_tablet_mode_animation_running(false);
2335 
2336   if (move_animation_tracker_) {
2337     move_animation_tracker_->Stop();
2338     move_animation_tracker_.reset();
2339   }
2340 
2341   if (snap_back_from_rip_off_view_ && animator == bounds_animator_.get()) {
2342     if (!animator->IsAnimating(snap_back_from_rip_off_view_)) {
2343       // Coming here the animation of the ShelfAppButton is finished and the
2344       // previously hidden status can be shown again. Since the button itself
2345       // might have gone away or changed locations we check that the button
2346       // is still in the shelf and show its status again.
2347       const auto& entries = view_model_->entries();
2348       const auto iter = std::find_if(
2349           entries.begin(), entries.end(), [this](const auto& entry) {
2350             return entry.view == snap_back_from_rip_off_view_;
2351           });
2352       if (iter != entries.end())
2353         snap_back_from_rip_off_view_->ClearState(ShelfAppButton::STATE_HIDDEN);
2354 
2355       snap_back_from_rip_off_view_ = nullptr;
2356     }
2357   }
2358 }
2359 
IsRepostEvent(const ui::Event & event)2360 bool ShelfView::IsRepostEvent(const ui::Event& event) {
2361   if (closing_event_time_.is_null())
2362     return false;
2363 
2364   // If the current (press down) event is a repost event, the time stamp of
2365   // these two events should be the same.
2366   return closing_event_time_ == event.time_stamp();
2367 }
2368 
ShelfItemForView(const views::View * view) const2369 const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const {
2370   const int view_index = view_model_->GetIndexOfView(view);
2371   return (view_index < 0) ? nullptr : &(model_->items()[view_index]);
2372 }
2373 
CalculateShelfDistance(const gfx::Point & coordinate) const2374 int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const {
2375   const gfx::Rect bounds = GetBoundsInScreen();
2376   int distance = shelf_->SelectValueForShelfAlignment(
2377       bounds.y() - coordinate.y(), coordinate.x() - bounds.right(),
2378       bounds.x() - coordinate.x());
2379   return distance > 0 ? distance : 0;
2380 }
2381 
CanPrepareForDrag(Pointer pointer,const ui::LocatedEvent & event)2382 bool ShelfView::CanPrepareForDrag(Pointer pointer,
2383                                   const ui::LocatedEvent& event) {
2384   // Bail if dragging has already begun, or if no item has been pressed.
2385   if (dragging() || !drag_view_)
2386     return false;
2387 
2388   // Dragging only begins once the pointer has travelled a minimum distance.
2389   if ((std::abs(event.x() - drag_origin_.x()) < kMinimumDragDistance) &&
2390       (std::abs(event.y() - drag_origin_.y()) < kMinimumDragDistance)) {
2391     return false;
2392   }
2393 
2394   return true;
2395 }
2396 
SetDragImageBlur(const gfx::Size & size,int blur_radius)2397 void ShelfView::SetDragImageBlur(const gfx::Size& size, int blur_radius) {
2398   DragImageView* drag_image = GetDragImage();
2399   drag_image->SetPaintToLayer();
2400   drag_image->layer()->SetFillsBoundsOpaquely(false);
2401   const uint32_t radius = std::round(size.width() / 2.f);
2402   drag_image->layer()->SetRoundedCornerRadius({radius, radius, radius, radius});
2403   drag_image->layer()->SetBackgroundBlur(blur_radius);
2404 }
2405 
ShouldHandleGestures(const ui::GestureEvent & event) const2406 bool ShelfView::ShouldHandleGestures(const ui::GestureEvent& event) const {
2407   if (event.type() == ui::ET_GESTURE_SCROLL_BEGIN) {
2408     float x_offset = event.details().scroll_x_hint();
2409     float y_offset = event.details().scroll_y_hint();
2410     if (!shelf_->IsHorizontalAlignment())
2411       std::swap(x_offset, y_offset);
2412 
2413     return std::abs(x_offset) < std::abs(y_offset);
2414   }
2415 
2416   return true;
2417 }
2418 
GetTitleForChildView(const views::View * view) const2419 base::string16 ShelfView::GetTitleForChildView(const views::View* view) const {
2420   const ShelfItem* item = ShelfItemForView(view);
2421   return item ? item->title : base::string16();
2422 }
2423 
UpdateShelfItemViewsVisibility()2424 void ShelfView::UpdateShelfItemViewsVisibility() {
2425   visible_views_indices_.clear();
2426   for (int i = 0; i < view_model_->view_size(); ++i) {
2427     View* view = view_model_->view_at(i);
2428     // To receive drag event continuously from |drag_view_| during the dragging
2429     // off from the shelf, don't make |drag_view_| invisible. It will be
2430     // eventually invisible and removed from the |view_model_| by
2431     // FinalizeRipOffDrag().
2432     const bool has_to_show = dragged_off_shelf_ && view == drag_view();
2433     const bool is_visible = has_to_show || IsItemVisible(model()->items()[i]);
2434     view->SetVisible(is_visible);
2435 
2436     if (is_visible)
2437       visible_views_indices_.push_back(i);
2438   }
2439 }
2440 
DestroyScopedDisplay()2441 void ShelfView::DestroyScopedDisplay() {
2442   scoped_display_for_new_windows_.reset();
2443 }
2444 
CalculateAppIconsLayoutOffset() const2445 int ShelfView::CalculateAppIconsLayoutOffset() const {
2446   const ScrollableShelfView* scrollable_shelf_view =
2447       shelf_->hotseat_widget()->scrollable_shelf_view();
2448   const gfx::Insets& edge_padding_insets =
2449       scrollable_shelf_view->edge_padding_insets();
2450 
2451   return shelf_->IsHorizontalAlignment() ? edge_padding_insets.left()
2452                                          : edge_padding_insets.top();
2453 }
2454 
GetDragImage()2455 DragImageView* ShelfView::GetDragImage() {
2456   return static_cast<DragImageView*>(drag_image_widget_->GetContentsView());
2457 }
2458 
GetChildViewTargetMirroredBounds(const views::View * child) const2459 gfx::Rect ShelfView::GetChildViewTargetMirroredBounds(
2460     const views::View* child) const {
2461   DCHECK_EQ(this, child->parent());
2462   return GetMirroredRect(bounds_animator_->GetTargetBounds(child));
2463 }
2464 
2465 }  // namespace ash
2466