OLD | NEW |
| (Empty) |
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/common/shelf/shelf_view.h" | |
6 | |
7 #include <algorithm> | |
8 #include <memory> | |
9 | |
10 #include "ash/common/ash_constants.h" | |
11 #include "ash/common/drag_drop/drag_image_view.h" | |
12 #include "ash/common/scoped_root_window_for_new_windows.h" | |
13 #include "ash/common/shelf/app_list_button.h" | |
14 #include "ash/common/shelf/overflow_bubble.h" | |
15 #include "ash/common/shelf/overflow_bubble_view.h" | |
16 #include "ash/common/shelf/overflow_button.h" | |
17 #include "ash/common/shelf/shelf_application_menu_model.h" | |
18 #include "ash/common/shelf/shelf_button.h" | |
19 #include "ash/common/shelf/shelf_constants.h" | |
20 #include "ash/common/shelf/shelf_delegate.h" | |
21 #include "ash/common/shelf/shelf_model.h" | |
22 #include "ash/common/shelf/shelf_widget.h" | |
23 #include "ash/common/shelf/wm_shelf.h" | |
24 #include "ash/common/shell_delegate.h" | |
25 #include "ash/common/wm/root_window_finder.h" | |
26 #include "ash/common/wm_shell.h" | |
27 #include "ash/common/wm_window.h" | |
28 #include "ash/strings/grit/ash_strings.h" | |
29 #include "base/auto_reset.h" | |
30 #include "base/memory/ptr_util.h" | |
31 #include "base/metrics/histogram_macros.h" | |
32 #include "ui/accessibility/ax_node_data.h" | |
33 #include "ui/base/l10n/l10n_util.h" | |
34 #include "ui/base/models/simple_menu_model.h" | |
35 #include "ui/compositor/layer.h" | |
36 #include "ui/compositor/layer_animator.h" | |
37 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | |
38 #include "ui/events/event_utils.h" | |
39 #include "ui/gfx/canvas.h" | |
40 #include "ui/gfx/geometry/point.h" | |
41 #include "ui/views/animation/bounds_animator.h" | |
42 #include "ui/views/border.h" | |
43 #include "ui/views/controls/button/image_button.h" | |
44 #include "ui/views/controls/menu/menu_model_adapter.h" | |
45 #include "ui/views/controls/menu/menu_runner.h" | |
46 #include "ui/views/focus/focus_search.h" | |
47 #include "ui/views/view_model.h" | |
48 #include "ui/views/view_model_utils.h" | |
49 #include "ui/views/widget/widget.h" | |
50 #include "ui/wm/core/coordinate_conversion.h" | |
51 | |
52 using gfx::Animation; | |
53 using views::View; | |
54 | |
55 namespace ash { | |
56 | |
57 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM = 0; | |
58 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT = 1; | |
59 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT = 2; | |
60 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT = 3; | |
61 | |
62 // Default amount content is inset on the left edge. | |
63 const int kDefaultLeadingInset = 8; | |
64 | |
65 // The proportion of the shelf space reserved for non-panel icons. Panels | |
66 // may flow into this space but will be put into the overflow bubble if there | |
67 // is contention for the space. | |
68 const float kReservedNonPanelIconProportion = 0.67f; | |
69 | |
70 // The distance of the cursor from the outer rim of the shelf before it | |
71 // separates. | |
72 const int kRipOffDistance = 48; | |
73 | |
74 // The rip off drag and drop proxy image should get scaled by this factor. | |
75 const float kDragAndDropProxyScale = 1.5f; | |
76 | |
77 // The opacity represents that this partially disappeared item will get removed. | |
78 const float kDraggedImageOpacity = 0.5f; | |
79 | |
80 namespace { | |
81 | |
82 // A class to temporarily disable a given bounds animator. | |
83 class BoundsAnimatorDisabler { | |
84 public: | |
85 explicit BoundsAnimatorDisabler(views::BoundsAnimator* bounds_animator) | |
86 : old_duration_(bounds_animator->GetAnimationDuration()), | |
87 bounds_animator_(bounds_animator) { | |
88 bounds_animator_->SetAnimationDuration(1); | |
89 } | |
90 | |
91 ~BoundsAnimatorDisabler() { | |
92 bounds_animator_->SetAnimationDuration(old_duration_); | |
93 } | |
94 | |
95 private: | |
96 // The previous animation duration. | |
97 int old_duration_; | |
98 // The bounds animator which gets used. | |
99 views::BoundsAnimator* bounds_animator_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorDisabler); | |
102 }; | |
103 | |
104 // Custom FocusSearch used to navigate the shelf in the order items are in | |
105 // the ViewModel. | |
106 class ShelfFocusSearch : public views::FocusSearch { | |
107 public: | |
108 explicit ShelfFocusSearch(views::ViewModel* view_model) | |
109 : FocusSearch(nullptr, true, true), view_model_(view_model) {} | |
110 ~ShelfFocusSearch() override {} | |
111 | |
112 // views::FocusSearch overrides: | |
113 View* FindNextFocusableView(View* starting_view, | |
114 bool reverse, | |
115 Direction direction, | |
116 bool check_starting_view, | |
117 views::FocusTraversable** focus_traversable, | |
118 View** focus_traversable_view) override { | |
119 int index = view_model_->GetIndexOfView(starting_view); | |
120 if (index == -1) | |
121 return view_model_->view_at(0); | |
122 | |
123 if (reverse) { | |
124 --index; | |
125 if (index < 0) | |
126 index = view_model_->view_size() - 1; | |
127 } else { | |
128 ++index; | |
129 if (index >= view_model_->view_size()) | |
130 index = 0; | |
131 } | |
132 return view_model_->view_at(index); | |
133 } | |
134 | |
135 private: | |
136 views::ViewModel* view_model_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(ShelfFocusSearch); | |
139 }; | |
140 | |
141 // AnimationDelegate used when inserting a new item. This steadily increases the | |
142 // opacity of the layer as the animation progress. | |
143 class FadeInAnimationDelegate : public gfx::AnimationDelegate { | |
144 public: | |
145 explicit FadeInAnimationDelegate(views::View* view) : view_(view) {} | |
146 ~FadeInAnimationDelegate() override {} | |
147 | |
148 // AnimationDelegate overrides: | |
149 void AnimationProgressed(const Animation* animation) override { | |
150 view_->layer()->SetOpacity(animation->GetCurrentValue()); | |
151 view_->layer()->ScheduleDraw(); | |
152 } | |
153 void AnimationEnded(const Animation* animation) override { | |
154 view_->layer()->SetOpacity(1.0f); | |
155 view_->layer()->ScheduleDraw(); | |
156 } | |
157 void AnimationCanceled(const Animation* animation) override { | |
158 view_->layer()->SetOpacity(1.0f); | |
159 view_->layer()->ScheduleDraw(); | |
160 } | |
161 | |
162 private: | |
163 views::View* view_; | |
164 | |
165 DISALLOW_COPY_AND_ASSIGN(FadeInAnimationDelegate); | |
166 }; | |
167 | |
168 void ReflectItemStatus(const ShelfItem& item, ShelfButton* button) { | |
169 switch (item.status) { | |
170 case STATUS_CLOSED: | |
171 button->ClearState(ShelfButton::STATE_ACTIVE); | |
172 button->ClearState(ShelfButton::STATE_RUNNING); | |
173 button->ClearState(ShelfButton::STATE_ATTENTION); | |
174 break; | |
175 case STATUS_RUNNING: | |
176 button->ClearState(ShelfButton::STATE_ACTIVE); | |
177 button->AddState(ShelfButton::STATE_RUNNING); | |
178 button->ClearState(ShelfButton::STATE_ATTENTION); | |
179 break; | |
180 case STATUS_ACTIVE: | |
181 button->AddState(ShelfButton::STATE_ACTIVE); | |
182 button->ClearState(ShelfButton::STATE_RUNNING); | |
183 button->ClearState(ShelfButton::STATE_ATTENTION); | |
184 break; | |
185 case STATUS_ATTENTION: | |
186 button->ClearState(ShelfButton::STATE_ACTIVE); | |
187 button->ClearState(ShelfButton::STATE_RUNNING); | |
188 button->AddState(ShelfButton::STATE_ATTENTION); | |
189 break; | |
190 } | |
191 } | |
192 | |
193 } // namespace | |
194 | |
195 // AnimationDelegate used when deleting an item. This steadily decreased the | |
196 // opacity of the layer as the animation progress. | |
197 class ShelfView::FadeOutAnimationDelegate : public gfx::AnimationDelegate { | |
198 public: | |
199 FadeOutAnimationDelegate(ShelfView* host, views::View* view) | |
200 : shelf_view_(host), view_(view) {} | |
201 ~FadeOutAnimationDelegate() override {} | |
202 | |
203 // AnimationDelegate overrides: | |
204 void AnimationProgressed(const Animation* animation) override { | |
205 view_->layer()->SetOpacity(1 - animation->GetCurrentValue()); | |
206 view_->layer()->ScheduleDraw(); | |
207 } | |
208 void AnimationEnded(const Animation* animation) override { | |
209 shelf_view_->OnFadeOutAnimationEnded(); | |
210 } | |
211 void AnimationCanceled(const Animation* animation) override {} | |
212 | |
213 private: | |
214 ShelfView* shelf_view_; | |
215 std::unique_ptr<views::View> view_; | |
216 | |
217 DISALLOW_COPY_AND_ASSIGN(FadeOutAnimationDelegate); | |
218 }; | |
219 | |
220 // AnimationDelegate used to trigger fading an element in. When an item is | |
221 // inserted this delegate is attached to the animation that expands the size of | |
222 // the item. When done it kicks off another animation to fade the item in. | |
223 class ShelfView::StartFadeAnimationDelegate : public gfx::AnimationDelegate { | |
224 public: | |
225 StartFadeAnimationDelegate(ShelfView* host, views::View* view) | |
226 : shelf_view_(host), view_(view) {} | |
227 ~StartFadeAnimationDelegate() override {} | |
228 | |
229 // AnimationDelegate overrides: | |
230 void AnimationEnded(const Animation* animation) override { | |
231 shelf_view_->FadeIn(view_); | |
232 } | |
233 void AnimationCanceled(const Animation* animation) override { | |
234 view_->layer()->SetOpacity(1.0f); | |
235 } | |
236 | |
237 private: | |
238 ShelfView* shelf_view_; | |
239 views::View* view_; | |
240 | |
241 DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate); | |
242 }; | |
243 | |
244 // static | |
245 const int ShelfView::kMinimumDragDistance = 8; | |
246 | |
247 ShelfView::ShelfView(ShelfModel* model, | |
248 ShelfDelegate* delegate, | |
249 WmShelf* wm_shelf, | |
250 ShelfWidget* shelf_widget) | |
251 : model_(model), | |
252 delegate_(delegate), | |
253 wm_shelf_(wm_shelf), | |
254 shelf_widget_(shelf_widget), | |
255 view_model_(new views::ViewModel), | |
256 first_visible_index_(0), | |
257 last_visible_index_(-1), | |
258 overflow_button_(nullptr), | |
259 owner_overflow_bubble_(nullptr), | |
260 tooltip_(this), | |
261 drag_pointer_(NONE), | |
262 drag_view_(nullptr), | |
263 start_drag_index_(-1), | |
264 context_menu_id_(0), | |
265 leading_inset_(kDefaultLeadingInset), | |
266 cancelling_drag_model_changed_(false), | |
267 last_hidden_index_(0), | |
268 closing_event_time_(base::TimeTicks()), | |
269 drag_and_drop_item_pinned_(false), | |
270 drag_and_drop_shelf_id_(0), | |
271 drag_replaced_view_(nullptr), | |
272 dragged_off_shelf_(false), | |
273 snap_back_from_rip_off_view_(nullptr), | |
274 overflow_mode_(false), | |
275 main_shelf_(nullptr), | |
276 dragged_off_from_overflow_to_shelf_(false), | |
277 is_repost_event_on_same_item_(false), | |
278 last_pressed_index_(-1) { | |
279 DCHECK(model_); | |
280 DCHECK(delegate_); | |
281 DCHECK(wm_shelf_); | |
282 DCHECK(shelf_widget_); | |
283 bounds_animator_.reset(new views::BoundsAnimator(this)); | |
284 bounds_animator_->AddObserver(this); | |
285 set_context_menu_controller(this); | |
286 focus_search_.reset(new ShelfFocusSearch(view_model_.get())); | |
287 } | |
288 | |
289 ShelfView::~ShelfView() { | |
290 bounds_animator_->RemoveObserver(this); | |
291 model_->RemoveObserver(this); | |
292 } | |
293 | |
294 void ShelfView::Init() { | |
295 model_->AddObserver(this); | |
296 | |
297 const ShelfItems& items(model_->items()); | |
298 for (ShelfItems::const_iterator i = items.begin(); i != items.end(); ++i) { | |
299 views::View* child = CreateViewForItem(*i); | |
300 child->SetPaintToLayer(); | |
301 view_model_->Add(child, static_cast<int>(i - items.begin())); | |
302 AddChildView(child); | |
303 } | |
304 overflow_button_ = new OverflowButton(this, wm_shelf_); | |
305 overflow_button_->set_context_menu_controller(this); | |
306 ConfigureChildView(overflow_button_); | |
307 AddChildView(overflow_button_); | |
308 | |
309 // We'll layout when our bounds change. | |
310 } | |
311 | |
312 void ShelfView::OnShelfAlignmentChanged() { | |
313 overflow_button_->OnShelfAlignmentChanged(); | |
314 LayoutToIdealBounds(); | |
315 for (int i = 0; i < view_model_->view_size(); ++i) { | |
316 if (i >= first_visible_index_ && i <= last_visible_index_) | |
317 view_model_->view_at(i)->Layout(); | |
318 } | |
319 tooltip_.Close(); | |
320 if (overflow_bubble_) | |
321 overflow_bubble_->Hide(); | |
322 // For crbug.com/587931, because AppListButton layout logic is in OnPaint. | |
323 AppListButton* app_list_button = GetAppListButton(); | |
324 if (app_list_button) | |
325 app_list_button->SchedulePaint(); | |
326 } | |
327 | |
328 gfx::Rect ShelfView::GetIdealBoundsOfItemIcon(ShelfID id) { | |
329 int index = model_->ItemIndexByID(id); | |
330 if (index == -1) | |
331 return gfx::Rect(); | |
332 // Map all items from overflow area to the overflow button. Note that the | |
333 // section between last_index_hidden_ and model_->FirstPanelIndex() is the | |
334 // list of invisible panel items. However, these items are currently nowhere | |
335 // represented and get dropped instead - see (crbug.com/378907). As such there | |
336 // is no way to address them or place them. We therefore move them over the | |
337 // overflow button. | |
338 if (index > last_visible_index_ && index < model_->FirstPanelIndex()) | |
339 index = last_visible_index_ + 1; | |
340 const gfx::Rect& ideal_bounds(view_model_->ideal_bounds(index)); | |
341 DCHECK_NE(TYPE_APP_LIST, model_->items()[index].type); | |
342 views::View* view = view_model_->view_at(index); | |
343 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName()); | |
344 ShelfButton* button = static_cast<ShelfButton*>(view); | |
345 gfx::Rect icon_bounds = button->GetIconBounds(); | |
346 return gfx::Rect(GetMirroredXWithWidthInView( | |
347 ideal_bounds.x() + icon_bounds.x(), icon_bounds.width()), | |
348 ideal_bounds.y() + icon_bounds.y(), icon_bounds.width(), | |
349 icon_bounds.height()); | |
350 } | |
351 | |
352 void ShelfView::UpdatePanelIconPosition(ShelfID id, | |
353 const gfx::Point& midpoint) { | |
354 int current_index = model_->ItemIndexByID(id); | |
355 int first_panel_index = model_->FirstPanelIndex(); | |
356 if (current_index < first_panel_index) | |
357 return; | |
358 | |
359 gfx::Point midpoint_in_view(GetMirroredXInView(midpoint.x()), midpoint.y()); | |
360 int target_index = current_index; | |
361 while ( | |
362 target_index > first_panel_index && | |
363 wm_shelf_->PrimaryAxisValue(view_model_->ideal_bounds(target_index).x(), | |
364 view_model_->ideal_bounds(target_index).y()) > | |
365 wm_shelf_->PrimaryAxisValue(midpoint_in_view.x(), | |
366 midpoint_in_view.y())) { | |
367 --target_index; | |
368 } | |
369 while (target_index < view_model_->view_size() - 1 && | |
370 wm_shelf_->PrimaryAxisValue( | |
371 view_model_->ideal_bounds(target_index).right(), | |
372 view_model_->ideal_bounds(target_index).bottom()) < | |
373 wm_shelf_->PrimaryAxisValue(midpoint_in_view.x(), | |
374 midpoint_in_view.y())) { | |
375 ++target_index; | |
376 } | |
377 if (current_index != target_index) | |
378 model_->Move(current_index, target_index); | |
379 } | |
380 | |
381 bool ShelfView::IsShowingMenu() const { | |
382 return launcher_menu_runner_.get() && launcher_menu_runner_->IsRunning(); | |
383 } | |
384 | |
385 bool ShelfView::IsShowingOverflowBubble() const { | |
386 return overflow_bubble_.get() && overflow_bubble_->IsShowing(); | |
387 } | |
388 | |
389 AppListButton* ShelfView::GetAppListButton() const { | |
390 for (int i = 0; i < model_->item_count(); ++i) { | |
391 if (model_->items()[i].type == TYPE_APP_LIST) { | |
392 views::View* view = view_model_->view_at(i); | |
393 CHECK_EQ(AppListButton::kViewClassName, view->GetClassName()); | |
394 return static_cast<AppListButton*>(view); | |
395 } | |
396 } | |
397 | |
398 NOTREACHED() << "Applist button not found"; | |
399 return nullptr; | |
400 } | |
401 | |
402 bool ShelfView::ShouldHideTooltip(const gfx::Point& cursor_location) const { | |
403 gfx::Rect tooltip_bounds; | |
404 for (int i = 0; i < child_count(); ++i) { | |
405 const views::View* child = child_at(i); | |
406 if (child != overflow_button_ && ShouldShowTooltipForView(child)) | |
407 tooltip_bounds.Union(child->GetMirroredBounds()); | |
408 } | |
409 return !tooltip_bounds.Contains(cursor_location); | |
410 } | |
411 | |
412 bool ShelfView::ShouldShowTooltipForView(const views::View* view) const { | |
413 // TODO(msw): Push this app list state into ShelfItem::shows_tooltip. | |
414 if (view == GetAppListButton() && GetAppListButton()->is_showing_app_list()) | |
415 return false; | |
416 const ShelfItem* item = ShelfItemForView(view); | |
417 return item && item->shows_tooltip; | |
418 } | |
419 | |
420 base::string16 ShelfView::GetTitleForView(const views::View* view) const { | |
421 const ShelfItem* item = ShelfItemForView(view); | |
422 return item ? item->title : base::string16(); | |
423 } | |
424 | |
425 gfx::Rect ShelfView::GetVisibleItemsBoundsInScreen() { | |
426 gfx::Size preferred_size = GetPreferredSize(); | |
427 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0); | |
428 ConvertPointToScreen(this, &origin); | |
429 return gfx::Rect(origin, preferred_size); | |
430 } | |
431 | |
432 void ShelfView::ButtonPressed(views::Button* sender, | |
433 const ui::Event& event, | |
434 views::InkDrop* ink_drop) { | |
435 if (sender == overflow_button_) { | |
436 ToggleOverflowBubble(); | |
437 shelf_button_pressed_metric_tracker_.ButtonPressed(event, sender, | |
438 SHELF_ACTION_NONE); | |
439 return; | |
440 } | |
441 | |
442 // None of the checks in ShouldEventActivateButton() affects overflow button. | |
443 // So, it is safe to be checked after handling overflow button. | |
444 if (!ShouldEventActivateButton(sender, event)) | |
445 return; | |
446 | |
447 // Record the index for the last pressed shelf item. | |
448 last_pressed_index_ = view_model_->GetIndexOfView(sender); | |
449 DCHECK_LT(-1, last_pressed_index_); | |
450 | |
451 // Place new windows on the same display as the button. | |
452 WmWindow* window = WmWindow::Get(sender->GetWidget()->GetNativeWindow()); | |
453 scoped_root_window_for_new_windows_.reset( | |
454 new ScopedRootWindowForNewWindows(window->GetRootWindow())); | |
455 | |
456 // Slow down activation animations if shift key is pressed. | |
457 std::unique_ptr<ui::ScopedAnimationDurationScaleMode> slowing_animations; | |
458 if (event.IsShiftDown()) { | |
459 slowing_animations.reset(new ui::ScopedAnimationDurationScaleMode( | |
460 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION)); | |
461 } | |
462 | |
463 // Collect usage statistics before we decide what to do with the click. | |
464 switch (model_->items()[last_pressed_index_].type) { | |
465 case TYPE_APP_SHORTCUT: | |
466 case TYPE_BROWSER_SHORTCUT: | |
467 case TYPE_APP: | |
468 WmShell::Get()->RecordUserMetricsAction(UMA_LAUNCHER_CLICK_ON_APP); | |
469 break; | |
470 | |
471 case TYPE_APP_LIST: | |
472 WmShell::Get()->RecordUserMetricsAction( | |
473 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON); | |
474 break; | |
475 | |
476 case TYPE_APP_PANEL: | |
477 case TYPE_DIALOG: | |
478 break; | |
479 | |
480 case TYPE_UNDEFINED: | |
481 NOTREACHED() << "ShelfItemType must be set."; | |
482 break; | |
483 } | |
484 | |
485 const int64_t display_id = window->GetDisplayNearestWindow().id(); | |
486 ShelfAction performed_action = | |
487 model_->GetShelfItemDelegate(model_->items()[last_pressed_index_].id) | |
488 ->ItemSelected(event.type(), event.flags(), display_id, | |
489 LAUNCH_FROM_UNKNOWN); | |
490 | |
491 shelf_button_pressed_metric_tracker_.ButtonPressed(event, sender, | |
492 performed_action); | |
493 | |
494 // For the app list menu no TRIGGERED ink drop effect is needed and it | |
495 // handles its own ACTIVATED/DEACTIVATED states. | |
496 if (performed_action == SHELF_ACTION_NEW_WINDOW_CREATED || | |
497 (performed_action != SHELF_ACTION_APP_LIST_SHOWN && | |
498 !ShowListMenuForView(model_->items()[last_pressed_index_], sender, event, | |
499 ink_drop))) { | |
500 ink_drop->AnimateToState(views::InkDropState::ACTION_TRIGGERED); | |
501 } | |
502 // Allow the menu to clear |scoped_root_window_for_new_windows_| during | |
503 // OnMenuClosed. | |
504 if (!IsShowingMenu()) | |
505 scoped_root_window_for_new_windows_.reset(); | |
506 } | |
507 | |
508 //////////////////////////////////////////////////////////////////////////////// | |
509 // ShelfView, FocusTraversable implementation: | |
510 | |
511 views::FocusSearch* ShelfView::GetFocusSearch() { | |
512 return focus_search_.get(); | |
513 } | |
514 | |
515 views::FocusTraversable* ShelfView::GetFocusTraversableParent() { | |
516 return parent()->GetFocusTraversable(); | |
517 } | |
518 | |
519 View* ShelfView::GetFocusTraversableParentView() { | |
520 return this; | |
521 } | |
522 | |
523 void ShelfView::CreateDragIconProxy( | |
524 const gfx::Point& location_in_screen_coordinates, | |
525 const gfx::ImageSkia& icon, | |
526 views::View* replaced_view, | |
527 const gfx::Vector2d& cursor_offset_from_center, | |
528 float scale_factor) { | |
529 drag_replaced_view_ = replaced_view; | |
530 WmWindow* root_window = | |
531 WmWindow::Get(drag_replaced_view_->GetWidget()->GetNativeWindow()) | |
532 ->GetRootWindow(); | |
533 drag_image_.reset(new DragImageView( | |
534 root_window, ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE)); | |
535 drag_image_->SetImage(icon); | |
536 gfx::Size size = drag_image_->GetPreferredSize(); | |
537 size.set_width(size.width() * scale_factor); | |
538 size.set_height(size.height() * scale_factor); | |
539 drag_image_offset_ = gfx::Vector2d(size.width() / 2, size.height() / 2) + | |
540 cursor_offset_from_center; | |
541 gfx::Rect drag_image_bounds( | |
542 location_in_screen_coordinates - drag_image_offset_, size); | |
543 drag_image_->SetBoundsInScreen(drag_image_bounds); | |
544 drag_image_->SetWidgetVisible(true); | |
545 } | |
546 | |
547 void ShelfView::UpdateDragIconProxy( | |
548 const gfx::Point& location_in_screen_coordinates) { | |
549 // TODO(jennyz): Investigate why drag_image_ becomes null at this point per | |
550 // crbug.com/34722, while the app list item is still being dragged around. | |
551 if (drag_image_) { | |
552 drag_image_->SetScreenPosition(location_in_screen_coordinates - | |
553 drag_image_offset_); | |
554 } | |
555 } | |
556 | |
557 void ShelfView::DestroyDragIconProxy() { | |
558 drag_image_.reset(); | |
559 drag_image_offset_ = gfx::Vector2d(0, 0); | |
560 } | |
561 | |
562 bool ShelfView::StartDrag(const std::string& app_id, | |
563 const gfx::Point& location_in_screen_coordinates) { | |
564 // Bail if an operation is already going on - or the cursor is not inside. | |
565 // This could happen if mouse / touch operations overlap. | |
566 if (drag_and_drop_shelf_id_ || | |
567 !GetBoundsInScreen().Contains(location_in_screen_coordinates)) | |
568 return false; | |
569 | |
570 // If the AppsGridView (which was dispatching this event) was opened by our | |
571 // button, ShelfView dragging operations are locked and we have to unlock. | |
572 CancelDrag(-1); | |
573 drag_and_drop_item_pinned_ = false; | |
574 drag_and_drop_app_id_ = app_id; | |
575 drag_and_drop_shelf_id_ = | |
576 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_); | |
577 // Check if the application is known and pinned - if not, we have to pin it so | |
578 // that we can re-arrange the shelf order accordingly. Note that items have | |
579 // to be pinned to give them the same (order) possibilities as a shortcut. | |
580 // When an item is dragged from overflow to shelf, IsShowingOverflowBubble() | |
581 // returns true. At this time, we don't need to pin the item. | |
582 if (!IsShowingOverflowBubble() && | |
583 (!drag_and_drop_shelf_id_ || !delegate_->IsAppPinned(app_id))) { | |
584 delegate_->PinAppWithID(app_id); | |
585 drag_and_drop_shelf_id_ = | |
586 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_); | |
587 if (!drag_and_drop_shelf_id_) | |
588 return false; | |
589 drag_and_drop_item_pinned_ = true; | |
590 } | |
591 views::View* drag_and_drop_view = | |
592 view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_)); | |
593 DCHECK(drag_and_drop_view); | |
594 | |
595 // Since there is already an icon presented by the caller, we hide this item | |
596 // for now. That has to be done by reducing the size since the visibility will | |
597 // change once a regrouping animation is performed. | |
598 pre_drag_and_drop_size_ = drag_and_drop_view->size(); | |
599 drag_and_drop_view->SetSize(gfx::Size()); | |
600 | |
601 // First we have to center the mouse cursor over the item. | |
602 gfx::Point pt = drag_and_drop_view->GetBoundsInScreen().CenterPoint(); | |
603 views::View::ConvertPointFromScreen(drag_and_drop_view, &pt); | |
604 gfx::Point point_in_root = | |
605 wm::GetRootWindowAt(location_in_screen_coordinates) | |
606 ->ConvertPointFromScreen(location_in_screen_coordinates); | |
607 ui::MouseEvent event(ui::ET_MOUSE_PRESSED, pt, point_in_root, | |
608 ui::EventTimeForNow(), 0, 0); | |
609 PointerPressedOnButton(drag_and_drop_view, DRAG_AND_DROP, event); | |
610 | |
611 // Drag the item where it really belongs. | |
612 Drag(location_in_screen_coordinates); | |
613 return true; | |
614 } | |
615 | |
616 bool ShelfView::Drag(const gfx::Point& location_in_screen_coordinates) { | |
617 if (!drag_and_drop_shelf_id_ || | |
618 !GetBoundsInScreen().Contains(location_in_screen_coordinates)) | |
619 return false; | |
620 | |
621 gfx::Point pt = location_in_screen_coordinates; | |
622 views::View* drag_and_drop_view = | |
623 view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_)); | |
624 ConvertPointFromScreen(drag_and_drop_view, &pt); | |
625 gfx::Point point_in_root = | |
626 wm::GetRootWindowAt(location_in_screen_coordinates) | |
627 ->ConvertPointFromScreen(location_in_screen_coordinates); | |
628 ui::MouseEvent event(ui::ET_MOUSE_DRAGGED, pt, point_in_root, | |
629 ui::EventTimeForNow(), 0, 0); | |
630 PointerDraggedOnButton(drag_and_drop_view, DRAG_AND_DROP, event); | |
631 return true; | |
632 } | |
633 | |
634 void ShelfView::EndDrag(bool cancel) { | |
635 if (!drag_and_drop_shelf_id_) | |
636 return; | |
637 | |
638 views::View* drag_and_drop_view = | |
639 view_model_->view_at(model_->ItemIndexByID(drag_and_drop_shelf_id_)); | |
640 PointerReleasedOnButton(drag_and_drop_view, DRAG_AND_DROP, cancel); | |
641 | |
642 // Either destroy the temporarily created item - or - make the item visible. | |
643 if (drag_and_drop_item_pinned_ && cancel) { | |
644 delegate_->UnpinAppWithID(drag_and_drop_app_id_); | |
645 } else if (drag_and_drop_view) { | |
646 if (cancel) { | |
647 // When a hosted drag gets canceled, the item can remain in the same slot | |
648 // and it might have moved within the bounds. In that case the item need | |
649 // to animate back to its correct location. | |
650 AnimateToIdealBounds(); | |
651 } else { | |
652 drag_and_drop_view->SetSize(pre_drag_and_drop_size_); | |
653 } | |
654 } | |
655 | |
656 drag_and_drop_shelf_id_ = 0; | |
657 } | |
658 | |
659 bool ShelfView::ShouldEventActivateButton(View* view, const ui::Event& event) { | |
660 if (dragging()) | |
661 return false; | |
662 | |
663 // Ignore if we are already in a pointer event sequence started with a repost | |
664 // event on the same shelf item. See crbug.com/343005 for more detail. | |
665 if (is_repost_event_on_same_item_) | |
666 return false; | |
667 | |
668 // Don't activate the item twice on double-click. Otherwise the window starts | |
669 // animating open due to the first click, then immediately minimizes due to | |
670 // the second click. The user most likely intended to open or minimize the | |
671 // item once, not do both. | |
672 if (event.flags() & ui::EF_IS_DOUBLE_CLICK) | |
673 return false; | |
674 | |
675 // Ignore if this is a repost event on the last pressed shelf item. | |
676 int index = view_model_->GetIndexOfView(view); | |
677 if (index == -1) | |
678 return false; | |
679 return !IsRepostEvent(event) || last_pressed_index_ != index; | |
680 } | |
681 | |
682 void ShelfView::PointerPressedOnButton(views::View* view, | |
683 Pointer pointer, | |
684 const ui::LocatedEvent& event) { | |
685 if (drag_view_) | |
686 return; | |
687 | |
688 int index = view_model_->GetIndexOfView(view); | |
689 if (index == -1 || view_model_->view_size() <= 1) | |
690 return; // View is being deleted, ignore request. | |
691 | |
692 if (view == GetAppListButton()) | |
693 return; // View is not draggable, ignore request. | |
694 | |
695 // Only when the repost event occurs on the same shelf item, we should ignore | |
696 // the call in ShelfView::ButtonPressed(...). | |
697 is_repost_event_on_same_item_ = | |
698 IsRepostEvent(event) && (last_pressed_index_ == index); | |
699 | |
700 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName()); | |
701 drag_view_ = static_cast<ShelfButton*>(view); | |
702 drag_origin_ = gfx::Point(event.x(), event.y()); | |
703 UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage", | |
704 wm_shelf_->SelectValueForShelfAlignment( | |
705 SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM, | |
706 SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT, | |
707 SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT), | |
708 SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT); | |
709 } | |
710 | |
711 void ShelfView::PointerDraggedOnButton(views::View* view, | |
712 Pointer pointer, | |
713 const ui::LocatedEvent& event) { | |
714 // To prepare all drag types (moving an item in the shelf and dragging off), | |
715 // we should check the x-axis and y-axis offset. | |
716 if (!dragging() && drag_view_ && | |
717 ((std::abs(event.x() - drag_origin_.x()) >= kMinimumDragDistance) || | |
718 (std::abs(event.y() - drag_origin_.y()) >= kMinimumDragDistance))) { | |
719 PrepareForDrag(pointer, event); | |
720 } | |
721 if (drag_pointer_ == pointer) | |
722 ContinueDrag(event); | |
723 } | |
724 | |
725 void ShelfView::PointerReleasedOnButton(views::View* view, | |
726 Pointer pointer, | |
727 bool canceled) { | |
728 is_repost_event_on_same_item_ = false; | |
729 | |
730 if (canceled) { | |
731 CancelDrag(-1); | |
732 } else if (drag_pointer_ == pointer) { | |
733 FinalizeRipOffDrag(false); | |
734 drag_pointer_ = NONE; | |
735 AnimateToIdealBounds(); | |
736 } | |
737 // If the drag pointer is NONE, no drag operation is going on and the | |
738 // drag_view can be released. | |
739 if (drag_pointer_ == NONE) | |
740 drag_view_ = nullptr; | |
741 } | |
742 | |
743 void ShelfView::LayoutToIdealBounds() { | |
744 if (bounds_animator_->IsAnimating()) { | |
745 AnimateToIdealBounds(); | |
746 return; | |
747 } | |
748 | |
749 IdealBounds ideal_bounds; | |
750 CalculateIdealBounds(&ideal_bounds); | |
751 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_); | |
752 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds); | |
753 } | |
754 | |
755 void ShelfView::UpdateShelfItemBackground(SkColor color) { | |
756 GetAppListButton()->UpdateShelfItemBackground(color); | |
757 overflow_button_->UpdateShelfItemBackground(color); | |
758 } | |
759 | |
760 void ShelfView::UpdateAllButtonsVisibilityInOverflowMode() { | |
761 // The overflow button is not shown in overflow mode. | |
762 overflow_button_->SetVisible(false); | |
763 DCHECK_LT(last_visible_index_, view_model_->view_size()); | |
764 for (int i = 0; i < view_model_->view_size(); ++i) { | |
765 bool visible = i >= first_visible_index_ && i <= last_visible_index_; | |
766 // To track the dragging of |drag_view_| continuously, its visibility | |
767 // should be always true regardless of its position. | |
768 if (dragged_off_from_overflow_to_shelf_ && | |
769 view_model_->view_at(i) == drag_view_) | |
770 view_model_->view_at(i)->SetVisible(true); | |
771 else | |
772 view_model_->view_at(i)->SetVisible(visible); | |
773 } | |
774 } | |
775 | |
776 void ShelfView::CalculateIdealBounds(IdealBounds* bounds) const { | |
777 int available_size = wm_shelf_->PrimaryAxisValue(width(), height()); | |
778 DCHECK(model_->item_count() == view_model_->view_size()); | |
779 if (!available_size) | |
780 return; | |
781 | |
782 int first_panel_index = model_->FirstPanelIndex(); | |
783 int last_button_index = first_panel_index - 1; | |
784 | |
785 int x = 0; | |
786 int y = 0; | |
787 | |
788 int w = wm_shelf_->PrimaryAxisValue(kShelfButtonSize, width()); | |
789 int h = wm_shelf_->PrimaryAxisValue(height(), kShelfButtonSize); | |
790 for (int i = 0; i < view_model_->view_size(); ++i) { | |
791 if (i < first_visible_index_) { | |
792 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, 0, 0)); | |
793 continue; | |
794 } | |
795 | |
796 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h)); | |
797 x = wm_shelf_->PrimaryAxisValue(x + w + kShelfButtonSpacing, x); | |
798 y = wm_shelf_->PrimaryAxisValue(y, y + h + kShelfButtonSpacing); | |
799 } | |
800 | |
801 if (is_overflow_mode()) { | |
802 const_cast<ShelfView*>(this)->UpdateAllButtonsVisibilityInOverflowMode(); | |
803 return; | |
804 } | |
805 | |
806 // Right aligned icons. | |
807 int end_position = available_size; | |
808 x = wm_shelf_->PrimaryAxisValue(end_position, 0); | |
809 y = wm_shelf_->PrimaryAxisValue(0, end_position); | |
810 for (int i = view_model_->view_size() - 1; i >= first_panel_index; --i) { | |
811 x = wm_shelf_->PrimaryAxisValue(x - w - kShelfButtonSpacing, x); | |
812 y = wm_shelf_->PrimaryAxisValue(y, y - h - kShelfButtonSpacing); | |
813 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h)); | |
814 end_position = wm_shelf_->PrimaryAxisValue(x, y); | |
815 } | |
816 | |
817 // Icons on the left / top are guaranteed up to kLeftIconProportion of | |
818 // the available space. | |
819 int last_icon_position = | |
820 wm_shelf_->PrimaryAxisValue( | |
821 view_model_->ideal_bounds(last_button_index).right(), | |
822 view_model_->ideal_bounds(last_button_index).bottom()) + | |
823 kShelfButtonSpacing; | |
824 int reserved_icon_space = available_size * kReservedNonPanelIconProportion; | |
825 if (last_icon_position < reserved_icon_space) | |
826 end_position = last_icon_position; | |
827 else | |
828 end_position = std::max(end_position, reserved_icon_space); | |
829 | |
830 bounds->overflow_bounds.set_size( | |
831 gfx::Size(wm_shelf_->PrimaryAxisValue(w, width()), | |
832 wm_shelf_->PrimaryAxisValue(height(), h))); | |
833 | |
834 last_visible_index_ = | |
835 DetermineLastVisibleIndex(end_position - kShelfButtonSpacing); | |
836 last_hidden_index_ = DetermineFirstVisiblePanelIndex(end_position) - 1; | |
837 bool show_overflow = last_visible_index_ < last_button_index || | |
838 last_hidden_index_ >= first_panel_index; | |
839 | |
840 // Create Space for the overflow button | |
841 if (show_overflow) { | |
842 // The following code makes sure that platform apps icons (aligned to left / | |
843 // top) are favored over panel apps icons (aligned to right / bottom). | |
844 if (last_visible_index_ > 0 && last_visible_index_ < last_button_index) { | |
845 // This condition means that we will take one platform app and replace it | |
846 // with the overflow button and put the app in the overflow bubble. | |
847 // This happens when the space needed for platform apps exceeds the | |
848 // reserved area for non-panel icons, | |
849 // (i.e. |last_icon_position| > |reserved_icon_space|). | |
850 --last_visible_index_; | |
851 } else if (last_hidden_index_ >= first_panel_index && | |
852 last_hidden_index_ < view_model_->view_size() - 1) { | |
853 // This condition means that we will take a panel app icon and replace it | |
854 // with the overflow button. | |
855 // This happens when there is still room for platform apps in the reserved | |
856 // area for non-panel icons, | |
857 // (i.e. |last_icon_position| < |reserved_icon_space|). | |
858 ++last_hidden_index_; | |
859 } | |
860 } | |
861 | |
862 for (int i = 0; i < view_model_->view_size(); ++i) { | |
863 bool visible = i <= last_visible_index_ || i > last_hidden_index_; | |
864 // To receive drag event continuously from |drag_view_| during the dragging | |
865 // off from the shelf, don't make |drag_view_| invisible. It will be | |
866 // eventually invisible and removed from the |view_model_| by | |
867 // FinalizeRipOffDrag(). | |
868 if (dragged_off_shelf_ && view_model_->view_at(i) == drag_view_) | |
869 continue; | |
870 view_model_->view_at(i)->SetVisible(visible); | |
871 } | |
872 | |
873 overflow_button_->SetVisible(show_overflow); | |
874 if (show_overflow) { | |
875 DCHECK_NE(0, view_model_->view_size()); | |
876 if (last_visible_index_ == -1) { | |
877 x = 0; | |
878 y = 0; | |
879 } else { | |
880 x = wm_shelf_->PrimaryAxisValue( | |
881 view_model_->ideal_bounds(last_visible_index_).right(), | |
882 view_model_->ideal_bounds(last_visible_index_).x()); | |
883 y = wm_shelf_->PrimaryAxisValue( | |
884 view_model_->ideal_bounds(last_visible_index_).y(), | |
885 view_model_->ideal_bounds(last_visible_index_).bottom()); | |
886 } | |
887 | |
888 if (last_visible_index_ >= 0) { | |
889 // Add more space between last visible item and overflow button. | |
890 // Without this, two buttons look too close compared with other items. | |
891 x = wm_shelf_->PrimaryAxisValue(x + kShelfButtonSpacing, x); | |
892 y = wm_shelf_->PrimaryAxisValue(y, y + kShelfButtonSpacing); | |
893 } | |
894 | |
895 // Set all hidden panel icon positions to be on the overflow button. | |
896 for (int i = first_panel_index; i <= last_hidden_index_; ++i) | |
897 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h)); | |
898 | |
899 bounds->overflow_bounds.set_x(x); | |
900 bounds->overflow_bounds.set_y(y); | |
901 if (overflow_bubble_.get() && overflow_bubble_->IsShowing()) | |
902 UpdateOverflowRange(overflow_bubble_->shelf_view()); | |
903 } else { | |
904 if (overflow_bubble_) | |
905 overflow_bubble_->Hide(); | |
906 } | |
907 } | |
908 | |
909 int ShelfView::DetermineLastVisibleIndex(int max_value) const { | |
910 int index = model_->FirstPanelIndex() - 1; | |
911 while (index >= 0 && | |
912 wm_shelf_->PrimaryAxisValue( | |
913 view_model_->ideal_bounds(index).right(), | |
914 view_model_->ideal_bounds(index).bottom()) > max_value) { | |
915 index--; | |
916 } | |
917 return index; | |
918 } | |
919 | |
920 int ShelfView::DetermineFirstVisiblePanelIndex(int min_value) const { | |
921 int index = model_->FirstPanelIndex(); | |
922 while (index < view_model_->view_size() && | |
923 wm_shelf_->PrimaryAxisValue(view_model_->ideal_bounds(index).x(), | |
924 view_model_->ideal_bounds(index).y()) < | |
925 min_value) { | |
926 ++index; | |
927 } | |
928 return index; | |
929 } | |
930 | |
931 void ShelfView::AnimateToIdealBounds() { | |
932 IdealBounds ideal_bounds; | |
933 CalculateIdealBounds(&ideal_bounds); | |
934 for (int i = 0; i < view_model_->view_size(); ++i) { | |
935 View* view = view_model_->view_at(i); | |
936 bounds_animator_->AnimateViewTo(view, view_model_->ideal_bounds(i)); | |
937 // Now that the item animation starts, we have to make sure that the | |
938 // padding of the first gets properly transferred to the new first item. | |
939 if (i && view->border()) | |
940 view->SetBorder(views::NullBorder()); | |
941 } | |
942 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds); | |
943 } | |
944 | |
945 views::View* ShelfView::CreateViewForItem(const ShelfItem& item) { | |
946 views::View* view = nullptr; | |
947 switch (item.type) { | |
948 case TYPE_APP_PANEL: | |
949 case TYPE_APP_SHORTCUT: | |
950 case TYPE_BROWSER_SHORTCUT: | |
951 case TYPE_APP: | |
952 case TYPE_DIALOG: { | |
953 ShelfButton* button = new ShelfButton(this, this); | |
954 button->SetImage(item.image); | |
955 ReflectItemStatus(item, button); | |
956 view = button; | |
957 break; | |
958 } | |
959 | |
960 case TYPE_APP_LIST: { | |
961 view = new AppListButton(this, this, wm_shelf_); | |
962 break; | |
963 } | |
964 | |
965 case TYPE_UNDEFINED: | |
966 return nullptr; | |
967 } | |
968 | |
969 view->set_context_menu_controller(this); | |
970 ConfigureChildView(view); | |
971 return view; | |
972 } | |
973 | |
974 void ShelfView::FadeIn(views::View* view) { | |
975 view->SetVisible(true); | |
976 view->layer()->SetOpacity(0); | |
977 AnimateToIdealBounds(); | |
978 bounds_animator_->SetAnimationDelegate( | |
979 view, std::unique_ptr<gfx::AnimationDelegate>( | |
980 new FadeInAnimationDelegate(view))); | |
981 } | |
982 | |
983 void ShelfView::PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event) { | |
984 DCHECK(!dragging()); | |
985 DCHECK(drag_view_); | |
986 drag_pointer_ = pointer; | |
987 start_drag_index_ = view_model_->GetIndexOfView(drag_view_); | |
988 | |
989 if (start_drag_index_ == -1) { | |
990 CancelDrag(-1); | |
991 return; | |
992 } | |
993 | |
994 // Move the view to the front so that it appears on top of other views. | |
995 ReorderChildView(drag_view_, -1); | |
996 bounds_animator_->StopAnimatingView(drag_view_); | |
997 | |
998 drag_view_->OnDragStarted(&event); | |
999 } | |
1000 | |
1001 void ShelfView::ContinueDrag(const ui::LocatedEvent& event) { | |
1002 DCHECK(dragging()); | |
1003 DCHECK(drag_view_); | |
1004 // Due to a syncing operation the application might have been removed. | |
1005 // Bail if it is gone. | |
1006 int current_index = view_model_->GetIndexOfView(drag_view_); | |
1007 DCHECK_NE(-1, current_index); | |
1008 | |
1009 // If this is not a drag and drop host operation and not the app list item, | |
1010 // check if the item got ripped off the shelf - if it did we are done. | |
1011 if (!drag_and_drop_shelf_id_ && | |
1012 RemovableByRipOff(current_index) != NOT_REMOVABLE) { | |
1013 if (HandleRipOffDrag(event)) | |
1014 return; | |
1015 // The rip off handler could have changed the location of the item. | |
1016 current_index = view_model_->GetIndexOfView(drag_view_); | |
1017 } | |
1018 | |
1019 // TODO: I don't think this works correctly with RTL. | |
1020 gfx::Point drag_point(event.location()); | |
1021 ConvertPointToTarget(drag_view_, this, &drag_point); | |
1022 | |
1023 // Constrain the location to the range of valid indices for the type. | |
1024 std::pair<int, int> indices(GetDragRange(current_index)); | |
1025 int first_drag_index = indices.first; | |
1026 int last_drag_index = indices.second; | |
1027 // If the last index isn't valid, we're overflowing. Constrain to the app list | |
1028 // (which is the last visible item). | |
1029 if (first_drag_index < model_->FirstPanelIndex() && | |
1030 last_drag_index > last_visible_index_) | |
1031 last_drag_index = last_visible_index_; | |
1032 int x = 0, y = 0; | |
1033 if (wm_shelf_->IsHorizontalAlignment()) { | |
1034 x = std::max(view_model_->ideal_bounds(indices.first).x(), | |
1035 drag_point.x() - drag_origin_.x()); | |
1036 x = std::min(view_model_->ideal_bounds(last_drag_index).right() - | |
1037 view_model_->ideal_bounds(current_index).width(), | |
1038 x); | |
1039 if (drag_view_->x() == x) | |
1040 return; | |
1041 drag_view_->SetX(x); | |
1042 } else { | |
1043 y = std::max(view_model_->ideal_bounds(indices.first).y(), | |
1044 drag_point.y() - drag_origin_.y()); | |
1045 y = std::min(view_model_->ideal_bounds(last_drag_index).bottom() - | |
1046 view_model_->ideal_bounds(current_index).height(), | |
1047 y); | |
1048 if (drag_view_->y() == y) | |
1049 return; | |
1050 drag_view_->SetY(y); | |
1051 } | |
1052 | |
1053 int target_index = views::ViewModelUtils::DetermineMoveIndex( | |
1054 *view_model_, drag_view_, | |
1055 wm_shelf_->IsHorizontalAlignment() ? views::ViewModelUtils::HORIZONTAL | |
1056 : views::ViewModelUtils::VERTICAL, | |
1057 x, y); | |
1058 target_index = | |
1059 std::min(indices.second, std::max(target_index, indices.first)); | |
1060 | |
1061 // The app list button is always first, and it is the only non-draggable item. | |
1062 int first_draggable_item = model_->GetItemIndexForType(TYPE_APP_LIST) + 1; | |
1063 DCHECK_EQ(1, first_draggable_item); | |
1064 target_index = std::max(target_index, first_draggable_item); | |
1065 DCHECK_LT(target_index, model_->item_count()); | |
1066 | |
1067 if (target_index == current_index) | |
1068 return; | |
1069 | |
1070 // Change the model, the ShelfItemMoved() callback will handle the | |
1071 // |view_model_| update. | |
1072 model_->Move(current_index, target_index); | |
1073 bounds_animator_->StopAnimatingView(drag_view_); | |
1074 } | |
1075 | |
1076 bool ShelfView::HandleRipOffDrag(const ui::LocatedEvent& event) { | |
1077 int current_index = view_model_->GetIndexOfView(drag_view_); | |
1078 DCHECK_NE(-1, current_index); | |
1079 std::string dragged_app_id = | |
1080 delegate_->GetAppIDForShelfID(model_->items()[current_index].id); | |
1081 | |
1082 gfx::Point screen_location = | |
1083 WmWindow::Get(GetWidget()->GetNativeWindow()) | |
1084 ->GetRootWindow() | |
1085 ->ConvertPointToScreen(event.root_location()); | |
1086 | |
1087 // To avoid ugly forwards and backwards flipping we use different constants | |
1088 // for ripping off / re-inserting the items. | |
1089 if (dragged_off_shelf_) { | |
1090 // If the shelf/overflow bubble bounds contains |screen_location| we insert | |
1091 // the item back into the shelf. | |
1092 if (GetBoundsForDragInsertInScreen().Contains(screen_location)) { | |
1093 if (dragged_off_from_overflow_to_shelf_) { | |
1094 // During the dragging an item from Shelf to Overflow, it can enter here | |
1095 // directly because both are located very closly. | |
1096 main_shelf_->EndDrag(true); | |
1097 // Stops the animation of |drag_view_| and sets its bounds explicitly | |
1098 // becase ContinueDrag() stops its animation. Without this, unexpected | |
1099 // bounds will be set. | |
1100 bounds_animator_->StopAnimatingView(drag_view_); | |
1101 int drag_view_index = view_model_->GetIndexOfView(drag_view_); | |
1102 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index)); | |
1103 dragged_off_from_overflow_to_shelf_ = false; | |
1104 } | |
1105 // Destroy our proxy view item. | |
1106 DestroyDragIconProxy(); | |
1107 // Re-insert the item and return simply false since the caller will handle | |
1108 // the move as in any normal case. | |
1109 dragged_off_shelf_ = false; | |
1110 drag_view_->layer()->SetOpacity(1.0f); | |
1111 // The size of Overflow bubble should be updated immediately when an item | |
1112 // is re-inserted. | |
1113 if (is_overflow_mode()) | |
1114 PreferredSizeChanged(); | |
1115 return false; | |
1116 } else if (is_overflow_mode() && | |
1117 main_shelf_->GetBoundsForDragInsertInScreen().Contains( | |
1118 screen_location)) { | |
1119 if (!dragged_off_from_overflow_to_shelf_) { | |
1120 dragged_off_from_overflow_to_shelf_ = true; | |
1121 drag_image_->SetOpacity(1.0f); | |
1122 main_shelf_->StartDrag(dragged_app_id, screen_location); | |
1123 } else { | |
1124 main_shelf_->Drag(screen_location); | |
1125 } | |
1126 } else if (dragged_off_from_overflow_to_shelf_) { | |
1127 // Makes the |drag_image_| partially disappear again. | |
1128 dragged_off_from_overflow_to_shelf_ = false; | |
1129 drag_image_->SetOpacity(kDraggedImageOpacity); | |
1130 main_shelf_->EndDrag(true); | |
1131 bounds_animator_->StopAnimatingView(drag_view_); | |
1132 int drag_view_index = view_model_->GetIndexOfView(drag_view_); | |
1133 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index)); | |
1134 } | |
1135 // Move our proxy view item. | |
1136 UpdateDragIconProxy(screen_location); | |
1137 return true; | |
1138 } | |
1139 // Check if we are too far away from the shelf to enter the ripped off state. | |
1140 // Determine the distance to the shelf. | |
1141 int delta = CalculateShelfDistance(screen_location); | |
1142 if (delta > kRipOffDistance) { | |
1143 // Create a proxy view item which can be moved anywhere. | |
1144 CreateDragIconProxy(event.root_location(), drag_view_->GetImage(), | |
1145 drag_view_, gfx::Vector2d(0, 0), | |
1146 kDragAndDropProxyScale); | |
1147 drag_view_->layer()->SetOpacity(0.0f); | |
1148 dragged_off_shelf_ = true; | |
1149 if (RemovableByRipOff(current_index) == REMOVABLE) { | |
1150 // Move the item to the front of the first panel item and hide it. | |
1151 // ShelfItemMoved() callback will handle the |view_model_| update and | |
1152 // call AnimateToIdealBounds(). | |
1153 if (current_index != model_->FirstPanelIndex() - 1) { | |
1154 model_->Move(current_index, model_->FirstPanelIndex() - 1); | |
1155 StartFadeInLastVisibleItem(); | |
1156 } else if (is_overflow_mode()) { | |
1157 // Overflow bubble should be shrunk when an item is ripped off. | |
1158 PreferredSizeChanged(); | |
1159 } | |
1160 // Make the item partially disappear to show that it will get removed if | |
1161 // dropped. | |
1162 drag_image_->SetOpacity(kDraggedImageOpacity); | |
1163 } | |
1164 return true; | |
1165 } | |
1166 return false; | |
1167 } | |
1168 | |
1169 void ShelfView::FinalizeRipOffDrag(bool cancel) { | |
1170 if (!dragged_off_shelf_) | |
1171 return; | |
1172 // Make sure we do not come in here again. | |
1173 dragged_off_shelf_ = false; | |
1174 | |
1175 // Coming here we should always have a |drag_view_|. | |
1176 DCHECK(drag_view_); | |
1177 int current_index = view_model_->GetIndexOfView(drag_view_); | |
1178 // If the view isn't part of the model anymore (|current_index| == -1), a sync | |
1179 // operation must have removed it. In that case we shouldn't change the model | |
1180 // and only delete the proxy image. | |
1181 if (current_index == -1) { | |
1182 DestroyDragIconProxy(); | |
1183 return; | |
1184 } | |
1185 | |
1186 // Set to true when the animation should snap back to where it was before. | |
1187 bool snap_back = false; | |
1188 // Items which cannot be dragged off will be handled as a cancel. | |
1189 if (!cancel) { | |
1190 if (dragged_off_from_overflow_to_shelf_) { | |
1191 dragged_off_from_overflow_to_shelf_ = false; | |
1192 main_shelf_->EndDrag(false); | |
1193 drag_view_->layer()->SetOpacity(1.0f); | |
1194 } else if (RemovableByRipOff(current_index) != REMOVABLE) { | |
1195 // Make sure we do not try to remove un-removable items like items which | |
1196 // were not pinned or have to be always there. | |
1197 cancel = true; | |
1198 snap_back = true; | |
1199 } else { | |
1200 // Make sure the item stays invisible upon removal. | |
1201 drag_view_->SetVisible(false); | |
1202 std::string app_id = | |
1203 delegate_->GetAppIDForShelfID(model_->items()[current_index].id); | |
1204 delegate_->UnpinAppWithID(app_id); | |
1205 } | |
1206 } | |
1207 if (cancel || snap_back) { | |
1208 if (dragged_off_from_overflow_to_shelf_) { | |
1209 dragged_off_from_overflow_to_shelf_ = false; | |
1210 // Main shelf handles revert of dragged item. | |
1211 main_shelf_->EndDrag(true); | |
1212 drag_view_->layer()->SetOpacity(1.0f); | |
1213 } else if (!cancelling_drag_model_changed_) { | |
1214 // Only do something if the change did not come through a model change. | |
1215 gfx::Rect drag_bounds = drag_image_->GetBoundsInScreen(); | |
1216 gfx::Point relative_to = GetBoundsInScreen().origin(); | |
1217 gfx::Rect target( | |
1218 gfx::PointAtOffsetFromOrigin(drag_bounds.origin() - relative_to), | |
1219 drag_bounds.size()); | |
1220 drag_view_->SetBoundsRect(target); | |
1221 // Hide the status from the active item since we snap it back now. Upon | |
1222 // animation end the flag gets cleared if |snap_back_from_rip_off_view_| | |
1223 // is set. | |
1224 snap_back_from_rip_off_view_ = drag_view_; | |
1225 drag_view_->AddState(ShelfButton::STATE_HIDDEN); | |
1226 // When a canceling drag model is happening, the view model is diverged | |
1227 // from the menu model and movements / animations should not be done. | |
1228 model_->Move(current_index, start_drag_index_); | |
1229 AnimateToIdealBounds(); | |
1230 } | |
1231 drag_view_->layer()->SetOpacity(1.0f); | |
1232 } | |
1233 DestroyDragIconProxy(); | |
1234 } | |
1235 | |
1236 ShelfView::RemovableState ShelfView::RemovableByRipOff(int index) const { | |
1237 DCHECK(index >= 0 && index < model_->item_count()); | |
1238 ShelfItemType type = model_->items()[index].type; | |
1239 if (type == TYPE_APP_LIST || type == TYPE_DIALOG) | |
1240 return NOT_REMOVABLE; | |
1241 | |
1242 if (model_->items()[index].pinned_by_policy) | |
1243 return NOT_REMOVABLE; | |
1244 | |
1245 // Note: Only pinned app shortcuts can be removed! | |
1246 std::string app_id = delegate_->GetAppIDForShelfID(model_->items()[index].id); | |
1247 return (type == TYPE_APP_SHORTCUT && delegate_->IsAppPinned(app_id)) | |
1248 ? REMOVABLE | |
1249 : DRAGGABLE; | |
1250 } | |
1251 | |
1252 bool ShelfView::SameDragType(ShelfItemType typea, ShelfItemType typeb) const { | |
1253 switch (typea) { | |
1254 case TYPE_APP_SHORTCUT: | |
1255 case TYPE_BROWSER_SHORTCUT: | |
1256 return (typeb == TYPE_APP_SHORTCUT || typeb == TYPE_BROWSER_SHORTCUT); | |
1257 case TYPE_APP_PANEL: | |
1258 case TYPE_APP_LIST: | |
1259 case TYPE_APP: | |
1260 case TYPE_DIALOG: | |
1261 return typeb == typea; | |
1262 case TYPE_UNDEFINED: | |
1263 NOTREACHED() << "ShelfItemType must be set."; | |
1264 return false; | |
1265 } | |
1266 NOTREACHED(); | |
1267 return false; | |
1268 } | |
1269 | |
1270 std::pair<int, int> ShelfView::GetDragRange(int index) { | |
1271 int min_index = -1; | |
1272 int max_index = -1; | |
1273 ShelfItemType type = model_->items()[index].type; | |
1274 for (int i = 0; i < model_->item_count(); ++i) { | |
1275 if (SameDragType(model_->items()[i].type, type)) { | |
1276 if (min_index == -1) | |
1277 min_index = i; | |
1278 max_index = i; | |
1279 } | |
1280 } | |
1281 return std::pair<int, int>(min_index, max_index); | |
1282 } | |
1283 | |
1284 void ShelfView::ConfigureChildView(views::View* view) { | |
1285 view->SetPaintToLayer(); | |
1286 view->layer()->SetFillsBoundsOpaquely(false); | |
1287 } | |
1288 | |
1289 void ShelfView::ToggleOverflowBubble() { | |
1290 if (IsShowingOverflowBubble()) { | |
1291 overflow_bubble_->Hide(); | |
1292 return; | |
1293 } | |
1294 | |
1295 if (!overflow_bubble_) | |
1296 overflow_bubble_.reset(new OverflowBubble(wm_shelf_)); | |
1297 | |
1298 ShelfView* overflow_view = | |
1299 new ShelfView(model_, delegate_, wm_shelf_, shelf_widget_); | |
1300 overflow_view->overflow_mode_ = true; | |
1301 overflow_view->Init(); | |
1302 overflow_view->set_owner_overflow_bubble(overflow_bubble_.get()); | |
1303 overflow_view->OnShelfAlignmentChanged(); | |
1304 overflow_view->main_shelf_ = this; | |
1305 UpdateOverflowRange(overflow_view); | |
1306 | |
1307 overflow_bubble_->Show(overflow_button_, overflow_view); | |
1308 | |
1309 wm_shelf_->UpdateVisibilityState(); | |
1310 } | |
1311 | |
1312 void ShelfView::OnFadeOutAnimationEnded() { | |
1313 AnimateToIdealBounds(); | |
1314 StartFadeInLastVisibleItem(); | |
1315 } | |
1316 | |
1317 void ShelfView::StartFadeInLastVisibleItem() { | |
1318 // If overflow button is visible and there is a valid new last item, fading | |
1319 // the new last item in after sliding animation is finished. | |
1320 if (overflow_button_->visible() && last_visible_index_ >= 0) { | |
1321 views::View* last_visible_view = view_model_->view_at(last_visible_index_); | |
1322 last_visible_view->layer()->SetOpacity(0); | |
1323 bounds_animator_->SetAnimationDelegate( | |
1324 last_visible_view, | |
1325 std::unique_ptr<gfx::AnimationDelegate>( | |
1326 new StartFadeAnimationDelegate(this, last_visible_view))); | |
1327 } | |
1328 } | |
1329 | |
1330 void ShelfView::UpdateOverflowRange(ShelfView* overflow_view) const { | |
1331 const int first_overflow_index = last_visible_index_ + 1; | |
1332 const int last_overflow_index = last_hidden_index_; | |
1333 DCHECK_LE(first_overflow_index, last_overflow_index); | |
1334 DCHECK_LT(last_overflow_index, view_model_->view_size()); | |
1335 | |
1336 overflow_view->first_visible_index_ = first_overflow_index; | |
1337 overflow_view->last_visible_index_ = last_overflow_index; | |
1338 } | |
1339 | |
1340 gfx::Rect ShelfView::GetBoundsForDragInsertInScreen() { | |
1341 gfx::Size preferred_size; | |
1342 if (is_overflow_mode()) { | |
1343 DCHECK(owner_overflow_bubble_); | |
1344 gfx::Rect bubble_bounds = | |
1345 owner_overflow_bubble_->bubble_view()->GetBubbleBounds(); | |
1346 preferred_size = bubble_bounds.size(); | |
1347 } else { | |
1348 const int last_button_index = view_model_->view_size() - 1; | |
1349 gfx::Rect last_button_bounds = | |
1350 view_model_->view_at(last_button_index)->bounds(); | |
1351 if (overflow_button_->visible() && | |
1352 model_->GetItemIndexForType(TYPE_APP_PANEL) == -1) { | |
1353 // When overflow button is visible and shelf has no panel items, | |
1354 // last_button_bounds should be overflow button's bounds. | |
1355 last_button_bounds = overflow_button_->bounds(); | |
1356 } | |
1357 | |
1358 if (wm_shelf_->IsHorizontalAlignment()) { | |
1359 preferred_size = gfx::Size(last_button_bounds.right() + leading_inset_, | |
1360 GetShelfConstant(SHELF_SIZE)); | |
1361 } else { | |
1362 preferred_size = gfx::Size(GetShelfConstant(SHELF_SIZE), | |
1363 last_button_bounds.bottom() + leading_inset_); | |
1364 } | |
1365 } | |
1366 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0); | |
1367 | |
1368 // In overflow mode, we should use OverflowBubbleView as a source for | |
1369 // converting |origin| to screen coordinates. When a scroll operation is | |
1370 // occurred in OverflowBubble, the bounds of ShelfView in OverflowBubble can | |
1371 // be changed. | |
1372 if (is_overflow_mode()) | |
1373 ConvertPointToScreen(owner_overflow_bubble_->bubble_view(), &origin); | |
1374 else | |
1375 ConvertPointToScreen(this, &origin); | |
1376 | |
1377 return gfx::Rect(origin, preferred_size); | |
1378 } | |
1379 | |
1380 int ShelfView::CancelDrag(int modified_index) { | |
1381 FinalizeRipOffDrag(true); | |
1382 if (!drag_view_) | |
1383 return modified_index; | |
1384 bool was_dragging = dragging(); | |
1385 int drag_view_index = view_model_->GetIndexOfView(drag_view_); | |
1386 drag_pointer_ = NONE; | |
1387 drag_view_ = nullptr; | |
1388 if (drag_view_index == modified_index) { | |
1389 // The view that was being dragged is being modified. Don't do anything. | |
1390 return modified_index; | |
1391 } | |
1392 if (!was_dragging) | |
1393 return modified_index; | |
1394 | |
1395 // Restore previous position, tracking the position of the modified view. | |
1396 bool at_end = modified_index == view_model_->view_size(); | |
1397 views::View* modified_view = (modified_index >= 0 && !at_end) | |
1398 ? view_model_->view_at(modified_index) | |
1399 : nullptr; | |
1400 model_->Move(drag_view_index, start_drag_index_); | |
1401 | |
1402 // If the modified view will be at the end of the list, return the new end of | |
1403 // the list. | |
1404 if (at_end) | |
1405 return view_model_->view_size(); | |
1406 return modified_view ? view_model_->GetIndexOfView(modified_view) : -1; | |
1407 } | |
1408 | |
1409 gfx::Size ShelfView::GetPreferredSize() const { | |
1410 IdealBounds ideal_bounds; | |
1411 CalculateIdealBounds(&ideal_bounds); | |
1412 const int shelf_size = GetShelfConstant(SHELF_SIZE); | |
1413 | |
1414 int last_button_index = last_visible_index_; | |
1415 if (!is_overflow_mode()) { | |
1416 if (last_hidden_index_ < view_model_->view_size() - 1) | |
1417 last_button_index = view_model_->view_size() - 1; | |
1418 else if (overflow_button_ && overflow_button_->visible()) | |
1419 last_button_index++; | |
1420 } | |
1421 | |
1422 // When an item is dragged off from the overflow bubble, it is moved to last | |
1423 // position and and changed to invisible. Overflow bubble size should be | |
1424 // shrunk to fit only for visible items. | |
1425 // If |dragged_off_from_overflow_to_shelf_| is set, there will be no invisible | |
1426 // items in the shelf. | |
1427 if (is_overflow_mode() && dragged_off_shelf_ && | |
1428 !dragged_off_from_overflow_to_shelf_ && | |
1429 RemovableByRipOff(view_model_->GetIndexOfView(drag_view_)) == REMOVABLE) | |
1430 last_button_index--; | |
1431 | |
1432 const gfx::Rect last_button_bounds = | |
1433 last_button_index >= first_visible_index_ | |
1434 ? view_model_->ideal_bounds(last_button_index) | |
1435 : gfx::Rect(gfx::Size(shelf_size, shelf_size)); | |
1436 | |
1437 if (wm_shelf_->IsHorizontalAlignment()) | |
1438 return gfx::Size(last_button_bounds.right() + leading_inset_, shelf_size); | |
1439 | |
1440 return gfx::Size(shelf_size, last_button_bounds.bottom() + leading_inset_); | |
1441 } | |
1442 | |
1443 void ShelfView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
1444 // This bounds change is produced by the shelf movement and all content has | |
1445 // to follow. Using an animation at that time would produce a time lag since | |
1446 // the animation of the BoundsAnimator has itself a delay before it arrives | |
1447 // at the required location. As such we tell the animator to go there | |
1448 // immediately. | |
1449 BoundsAnimatorDisabler disabler(bounds_animator_.get()); | |
1450 LayoutToIdealBounds(); | |
1451 wm_shelf_->NotifyShelfIconPositionsChanged(); | |
1452 | |
1453 if (IsShowingOverflowBubble()) | |
1454 overflow_bubble_->Hide(); | |
1455 } | |
1456 | |
1457 views::FocusTraversable* ShelfView::GetPaneFocusTraversable() { | |
1458 return this; | |
1459 } | |
1460 | |
1461 void ShelfView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | |
1462 node_data->role = ui::AX_ROLE_TOOLBAR; | |
1463 node_data->SetName(l10n_util::GetStringUTF8(IDS_ASH_SHELF_ACCESSIBLE_NAME)); | |
1464 } | |
1465 | |
1466 void ShelfView::ViewHierarchyChanged( | |
1467 const ViewHierarchyChangedDetails& details) { | |
1468 if (details.is_add && details.child == this) | |
1469 tooltip_.Init(); | |
1470 } | |
1471 | |
1472 void ShelfView::OnGestureEvent(ui::GestureEvent* event) { | |
1473 if (wm_shelf_->ProcessGestureEvent(*event)) | |
1474 event->StopPropagation(); | |
1475 } | |
1476 | |
1477 void ShelfView::ShelfItemAdded(int model_index) { | |
1478 { | |
1479 base::AutoReset<bool> cancelling_drag(&cancelling_drag_model_changed_, | |
1480 true); | |
1481 model_index = CancelDrag(model_index); | |
1482 } | |
1483 views::View* view = CreateViewForItem(model_->items()[model_index]); | |
1484 AddChildView(view); | |
1485 // Hide the view, it'll be made visible when the animation is done. Using | |
1486 // opacity 0 here to avoid messing with CalculateIdealBounds which touches | |
1487 // the view's visibility. | |
1488 view->layer()->SetOpacity(0); | |
1489 view_model_->Add(view, model_index); | |
1490 | |
1491 // Give the button its ideal bounds. That way if we end up animating the | |
1492 // button before this animation completes it doesn't appear at some random | |
1493 // spot (because it was in the middle of animating from 0,0 0x0 to its | |
1494 // target). | |
1495 IdealBounds ideal_bounds; | |
1496 CalculateIdealBounds(&ideal_bounds); | |
1497 view->SetBoundsRect(view_model_->ideal_bounds(model_index)); | |
1498 | |
1499 // The first animation moves all the views to their target position. |view| | |
1500 // is hidden, so it visually appears as though we are providing space for | |
1501 // it. When done we'll fade the view in. | |
1502 AnimateToIdealBounds(); | |
1503 if (model_index <= last_visible_index_ || | |
1504 model_index >= model_->FirstPanelIndex()) { | |
1505 bounds_animator_->SetAnimationDelegate( | |
1506 view, std::unique_ptr<gfx::AnimationDelegate>( | |
1507 new StartFadeAnimationDelegate(this, view))); | |
1508 } else { | |
1509 // Undo the hiding if animation does not run. | |
1510 view->layer()->SetOpacity(1.0f); | |
1511 } | |
1512 } | |
1513 | |
1514 void ShelfView::ShelfItemRemoved(int model_index, ShelfID id) { | |
1515 if (id == context_menu_id_) | |
1516 launcher_menu_runner_->Cancel(); | |
1517 { | |
1518 base::AutoReset<bool> cancelling_drag(&cancelling_drag_model_changed_, | |
1519 true); | |
1520 model_index = CancelDrag(model_index); | |
1521 } | |
1522 views::View* view = view_model_->view_at(model_index); | |
1523 view_model_->Remove(model_index); | |
1524 | |
1525 // When the overflow bubble is visible, the overflow range needs to be set | |
1526 // before CalculateIdealBounds() gets called. Otherwise CalculateIdealBounds() | |
1527 // could trigger a ShelfItemChanged() by hiding the overflow bubble and | |
1528 // since the overflow bubble is not yet synced with the ShelfModel this | |
1529 // could cause a crash. | |
1530 if (overflow_bubble_ && overflow_bubble_->IsShowing()) { | |
1531 last_hidden_index_ = | |
1532 std::min(last_hidden_index_, view_model_->view_size() - 1); | |
1533 UpdateOverflowRange(overflow_bubble_->shelf_view()); | |
1534 } | |
1535 | |
1536 if (view->visible()) { | |
1537 // The first animation fades out the view. When done we'll animate the rest | |
1538 // of the views to their target location. | |
1539 bounds_animator_->AnimateViewTo(view, view->bounds()); | |
1540 bounds_animator_->SetAnimationDelegate( | |
1541 view, std::unique_ptr<gfx::AnimationDelegate>( | |
1542 new FadeOutAnimationDelegate(this, view))); | |
1543 } else { | |
1544 // We don't need to show a fade out animation for invisible |view|. When an | |
1545 // item is ripped out from the shelf, its |view| is already invisible. | |
1546 AnimateToIdealBounds(); | |
1547 } | |
1548 | |
1549 if (view == tooltip_.GetCurrentAnchorView()) | |
1550 tooltip_.Close(); | |
1551 } | |
1552 | |
1553 void ShelfView::ShelfItemChanged(int model_index, const ShelfItem& old_item) { | |
1554 const ShelfItem& item(model_->items()[model_index]); | |
1555 if (old_item.type != item.type) { | |
1556 // Type changed, swap the views. | |
1557 model_index = CancelDrag(model_index); | |
1558 std::unique_ptr<views::View> old_view(view_model_->view_at(model_index)); | |
1559 bounds_animator_->StopAnimatingView(old_view.get()); | |
1560 // Removing and re-inserting a view in our view model will strip the ideal | |
1561 // bounds from the item. To avoid recalculation of everything the bounds | |
1562 // get remembered and restored after the insertion to the previous value. | |
1563 gfx::Rect old_ideal_bounds = view_model_->ideal_bounds(model_index); | |
1564 view_model_->Remove(model_index); | |
1565 views::View* new_view = CreateViewForItem(item); | |
1566 AddChildView(new_view); | |
1567 view_model_->Add(new_view, model_index); | |
1568 view_model_->set_ideal_bounds(model_index, old_ideal_bounds); | |
1569 new_view->SetBoundsRect(old_view->bounds()); | |
1570 if (overflow_button_ && overflow_button_->visible()) | |
1571 AnimateToIdealBounds(); | |
1572 else | |
1573 bounds_animator_->AnimateViewTo(new_view, old_ideal_bounds); | |
1574 return; | |
1575 } | |
1576 | |
1577 views::View* view = view_model_->view_at(model_index); | |
1578 switch (item.type) { | |
1579 case TYPE_APP_PANEL: | |
1580 case TYPE_APP_SHORTCUT: | |
1581 case TYPE_BROWSER_SHORTCUT: | |
1582 case TYPE_APP: | |
1583 case TYPE_DIALOG: { | |
1584 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName()); | |
1585 ShelfButton* button = static_cast<ShelfButton*>(view); | |
1586 ReflectItemStatus(item, button); | |
1587 button->SetImage(item.image); | |
1588 button->SchedulePaint(); | |
1589 break; | |
1590 } | |
1591 | |
1592 default: | |
1593 break; | |
1594 } | |
1595 } | |
1596 | |
1597 void ShelfView::ShelfItemMoved(int start_index, int target_index) { | |
1598 view_model_->Move(start_index, target_index); | |
1599 // When cancelling a drag due to a shelf item being added, the currently | |
1600 // dragged item is moved back to its initial position. AnimateToIdealBounds | |
1601 // will be called again when the new item is added to the |view_model_| but | |
1602 // at this time the |view_model_| is inconsistent with the |model_|. | |
1603 if (!cancelling_drag_model_changed_) | |
1604 AnimateToIdealBounds(); | |
1605 } | |
1606 | |
1607 void ShelfView::OnSetShelfItemDelegate(ShelfID id, | |
1608 ShelfItemDelegate* item_delegate) {} | |
1609 | |
1610 bool ShelfView::ShowListMenuForView(const ShelfItem& item, | |
1611 views::View* source, | |
1612 const ui::Event& event, | |
1613 views::InkDrop* ink_drop) { | |
1614 ShelfItemDelegate* item_delegate = model_->GetShelfItemDelegate(item.id); | |
1615 ShelfAppMenuItemList items = item_delegate->GetAppMenuItems(event.flags()); | |
1616 | |
1617 // The application list menu should only show for two or more items; return | |
1618 // false here to ensure that other behavior is triggered (eg. activating or | |
1619 // minimizing a single associated window, or launching a pinned shelf item). | |
1620 if (items.size() < 2) | |
1621 return false; | |
1622 | |
1623 ink_drop->AnimateToState(views::InkDropState::ACTIVATED); | |
1624 context_menu_id_ = item.id; | |
1625 ShowMenu(base::MakeUnique<ShelfApplicationMenuModel>( | |
1626 item.title, std::move(items), item_delegate), | |
1627 source, gfx::Point(), false, ui::GetMenuSourceTypeForEvent(event), | |
1628 ink_drop); | |
1629 return true; | |
1630 } | |
1631 | |
1632 void ShelfView::ShowContextMenuForView(views::View* source, | |
1633 const gfx::Point& point, | |
1634 ui::MenuSourceType source_type) { | |
1635 last_pressed_index_ = -1; | |
1636 | |
1637 const ShelfItem* item = ShelfItemForView(source); | |
1638 if (!item) { | |
1639 WmShell::Get()->ShowContextMenu(point, source_type); | |
1640 return; | |
1641 } | |
1642 | |
1643 std::unique_ptr<ui::MenuModel> context_menu_model( | |
1644 WmShell::Get()->delegate()->CreateContextMenu(wm_shelf_, item)); | |
1645 if (!context_menu_model) | |
1646 return; | |
1647 | |
1648 context_menu_id_ = item ? item->id : 0; | |
1649 ShowMenu(std::move(context_menu_model), source, point, true, source_type, | |
1650 nullptr); | |
1651 } | |
1652 | |
1653 void ShelfView::ShowMenu(std::unique_ptr<ui::MenuModel> menu_model, | |
1654 views::View* source, | |
1655 const gfx::Point& click_point, | |
1656 bool context_menu, | |
1657 ui::MenuSourceType source_type, | |
1658 views::InkDrop* ink_drop) { | |
1659 menu_model_ = std::move(menu_model); | |
1660 menu_model_adapter_.reset(new views::MenuModelAdapter( | |
1661 menu_model_.get(), | |
1662 base::Bind(&ShelfView::OnMenuClosed, base::Unretained(this), ink_drop))); | |
1663 | |
1664 closing_event_time_ = base::TimeTicks(); | |
1665 int run_types = views::MenuRunner::ASYNC; | |
1666 if (context_menu) | |
1667 run_types |= views::MenuRunner::CONTEXT_MENU; | |
1668 launcher_menu_runner_.reset( | |
1669 new views::MenuRunner(menu_model_adapter_->CreateMenu(), run_types)); | |
1670 | |
1671 // Place new windows on the same display as the button that spawned the menu. | |
1672 WmWindow* window = WmWindow::Get(source->GetWidget()->GetNativeWindow()); | |
1673 scoped_root_window_for_new_windows_.reset( | |
1674 new ScopedRootWindowForNewWindows(window->GetRootWindow())); | |
1675 | |
1676 views::MenuAnchorPosition menu_alignment = views::MENU_ANCHOR_TOPLEFT; | |
1677 gfx::Rect anchor = gfx::Rect(click_point, gfx::Size()); | |
1678 | |
1679 if (!context_menu) { | |
1680 // Application lists use a bubble. | |
1681 // It is possible to invoke the menu while it is sliding into view. To cover | |
1682 // that case, the screen coordinates are offsetted by the animation delta. | |
1683 anchor = source->GetBoundsInScreen() + (window->GetTargetBounds().origin() - | |
1684 window->GetBounds().origin()); | |
1685 | |
1686 // Adjust the anchor location for shelf items with asymmetrical borders. | |
1687 if (source->border()) | |
1688 anchor.Inset(source->border()->GetInsets()); | |
1689 | |
1690 // Determine the menu alignment dependent on the shelf. | |
1691 switch (wm_shelf_->GetAlignment()) { | |
1692 case SHELF_ALIGNMENT_BOTTOM: | |
1693 case SHELF_ALIGNMENT_BOTTOM_LOCKED: | |
1694 menu_alignment = views::MENU_ANCHOR_BUBBLE_ABOVE; | |
1695 break; | |
1696 case SHELF_ALIGNMENT_LEFT: | |
1697 menu_alignment = views::MENU_ANCHOR_BUBBLE_RIGHT; | |
1698 break; | |
1699 case SHELF_ALIGNMENT_RIGHT: | |
1700 menu_alignment = views::MENU_ANCHOR_BUBBLE_LEFT; | |
1701 break; | |
1702 } | |
1703 } | |
1704 | |
1705 // NOTE: if you convert to HAS_MNEMONICS be sure to update menu building code. | |
1706 launcher_menu_runner_->RunMenuAt(source->GetWidget(), nullptr, anchor, | |
1707 menu_alignment, source_type); | |
1708 } | |
1709 | |
1710 void ShelfView::OnMenuClosed(views::InkDrop* ink_drop) { | |
1711 context_menu_id_ = 0; | |
1712 | |
1713 // Hide the hide overflow bubble after showing a context menu for its items. | |
1714 if (owner_overflow_bubble_) | |
1715 owner_overflow_bubble_->Hide(); | |
1716 | |
1717 closing_event_time_ = launcher_menu_runner_->closing_event_time(); | |
1718 | |
1719 if (ink_drop) | |
1720 ink_drop->AnimateToState(views::InkDropState::DEACTIVATED); | |
1721 | |
1722 launcher_menu_runner_.reset(); | |
1723 menu_model_adapter_.reset(); | |
1724 menu_model_.reset(); | |
1725 scoped_root_window_for_new_windows_.reset(); | |
1726 | |
1727 // Auto-hide or alignment might have changed, but only for this shelf. | |
1728 wm_shelf_->UpdateVisibilityState(); | |
1729 } | |
1730 | |
1731 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) { | |
1732 wm_shelf_->NotifyShelfIconPositionsChanged(); | |
1733 PreferredSizeChanged(); | |
1734 } | |
1735 | |
1736 void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) { | |
1737 if (snap_back_from_rip_off_view_ && animator == bounds_animator_.get()) { | |
1738 if (!animator->IsAnimating(snap_back_from_rip_off_view_)) { | |
1739 // Coming here the animation of the ShelfButton is finished and the | |
1740 // previously hidden status can be shown again. Since the button itself | |
1741 // might have gone away or changed locations we check that the button | |
1742 // is still in the shelf and show its status again. | |
1743 for (int index = 0; index < view_model_->view_size(); index++) { | |
1744 views::View* view = view_model_->view_at(index); | |
1745 if (view == snap_back_from_rip_off_view_) { | |
1746 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName()); | |
1747 ShelfButton* button = static_cast<ShelfButton*>(view); | |
1748 button->ClearState(ShelfButton::STATE_HIDDEN); | |
1749 break; | |
1750 } | |
1751 } | |
1752 snap_back_from_rip_off_view_ = nullptr; | |
1753 } | |
1754 } | |
1755 } | |
1756 | |
1757 bool ShelfView::IsRepostEvent(const ui::Event& event) { | |
1758 if (closing_event_time_.is_null()) | |
1759 return false; | |
1760 | |
1761 // If the current (press down) event is a repost event, the time stamp of | |
1762 // these two events should be the same. | |
1763 return closing_event_time_ == event.time_stamp(); | |
1764 } | |
1765 | |
1766 const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const { | |
1767 const int view_index = view_model_->GetIndexOfView(view); | |
1768 return (view_index < 0) ? nullptr : &(model_->items()[view_index]); | |
1769 } | |
1770 | |
1771 int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const { | |
1772 const gfx::Rect bounds = GetBoundsInScreen(); | |
1773 int distance = wm_shelf_->SelectValueForShelfAlignment( | |
1774 bounds.y() - coordinate.y(), coordinate.x() - bounds.right(), | |
1775 bounds.x() - coordinate.x()); | |
1776 return distance > 0 ? distance : 0; | |
1777 } | |
1778 | |
1779 } // namespace ash | |
OLD | NEW |