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