cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / ash / shelf / shelf_view.cc
blob9f3fe0a05165812de2521df3fe5ff67c16a4a677
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.
5 #include "ash/shelf/shelf_view.h"
7 #include <algorithm>
9 #include "ash/ash_constants.h"
10 #include "ash/ash_switches.h"
11 #include "ash/drag_drop/drag_image_view.h"
12 #include "ash/metrics/user_metrics_recorder.h"
13 #include "ash/root_window_controller.h"
14 #include "ash/scoped_target_root_window.h"
15 #include "ash/shelf/app_list_button.h"
16 #include "ash/shelf/overflow_bubble.h"
17 #include "ash/shelf/overflow_bubble_view.h"
18 #include "ash/shelf/overflow_button.h"
19 #include "ash/shelf/shelf_button.h"
20 #include "ash/shelf/shelf_constants.h"
21 #include "ash/shelf/shelf_delegate.h"
22 #include "ash/shelf/shelf_icon_observer.h"
23 #include "ash/shelf/shelf_item_delegate_manager.h"
24 #include "ash/shelf/shelf_layout_manager.h"
25 #include "ash/shelf/shelf_menu_model.h"
26 #include "ash/shelf/shelf_model.h"
27 #include "ash/shelf/shelf_tooltip_manager.h"
28 #include "ash/shelf/shelf_widget.h"
29 #include "ash/shell.h"
30 #include "ash/wm/coordinate_conversion.h"
31 #include "base/auto_reset.h"
32 #include "base/memory/scoped_ptr.h"
33 #include "base/metrics/histogram.h"
34 #include "grit/ash_strings.h"
35 #include "ui/accessibility/ax_view_state.h"
36 #include "ui/aura/client/screen_position_client.h"
37 #include "ui/aura/window.h"
38 #include "ui/aura/window_event_dispatcher.h"
39 #include "ui/base/l10n/l10n_util.h"
40 #include "ui/base/models/simple_menu_model.h"
41 #include "ui/base/resource/resource_bundle.h"
42 #include "ui/compositor/layer.h"
43 #include "ui/compositor/layer_animator.h"
44 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
45 #include "ui/events/event_utils.h"
46 #include "ui/gfx/canvas.h"
47 #include "ui/gfx/geometry/point.h"
48 #include "ui/views/animation/bounds_animator.h"
49 #include "ui/views/border.h"
50 #include "ui/views/controls/button/image_button.h"
51 #include "ui/views/controls/menu/menu_model_adapter.h"
52 #include "ui/views/controls/menu/menu_runner.h"
53 #include "ui/views/focus/focus_search.h"
54 #include "ui/views/view_model.h"
55 #include "ui/views/view_model_utils.h"
56 #include "ui/views/widget/widget.h"
57 #include "ui/wm/core/coordinate_conversion.h"
59 using gfx::Animation;
60 using views::View;
62 namespace ash {
64 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM = 0;
65 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT = 1;
66 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT = 2;
67 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT = 3;
69 // Default amount content is inset on the left edge.
70 const int kDefaultLeadingInset = 8;
72 // Minimum distance before drag starts.
73 const int kMinimumDragDistance = 8;
75 // Additional spacing for the left and right side of icons.
76 const int kHorizontalIconSpacing = 2;
78 // Inset for items which do not have an icon.
79 const int kHorizontalNoIconInsetSpacing =
80 kHorizontalIconSpacing + kDefaultLeadingInset;
82 // The proportion of the shelf space reserved for non-panel icons. Panels
83 // may flow into this space but will be put into the overflow bubble if there
84 // is contention for the space.
85 const float kReservedNonPanelIconProportion = 0.67f;
87 // This is the command id of the menu item which contains the name of the menu.
88 const int kCommandIdOfMenuName = 0;
90 // The background color of the active item in the list.
91 const SkColor kActiveListItemBackgroundColor = SkColorSetRGB(203 , 219, 241);
93 // The background color of the active & hovered item in the list.
94 const SkColor kFocusedActiveListItemBackgroundColor =
95 SkColorSetRGB(193, 211, 236);
97 // The text color of the caption item in a list.
98 const SkColor kCaptionItemForegroundColor = SK_ColorBLACK;
100 // The maximum allowable length of a menu line of an application menu in pixels.
101 const int kMaximumAppMenuItemLength = 350;
103 // The distance of the cursor from the outer rim of the shelf before it
104 // separates.
105 const int kRipOffDistance = 48;
107 // The rip off drag and drop proxy image should get scaled by this factor.
108 const float kDragAndDropProxyScale = 1.5f;
110 // The opacity represents that this partially disappeared item will get removed.
111 const float kDraggedImageOpacity = 0.5f;
113 namespace {
115 // A class to temporarily disable a given bounds animator.
116 class BoundsAnimatorDisabler {
117 public:
118 explicit BoundsAnimatorDisabler(views::BoundsAnimator* bounds_animator)
119 : old_duration_(bounds_animator->GetAnimationDuration()),
120 bounds_animator_(bounds_animator) {
121 bounds_animator_->SetAnimationDuration(1);
124 ~BoundsAnimatorDisabler() {
125 bounds_animator_->SetAnimationDuration(old_duration_);
128 private:
129 // The previous animation duration.
130 int old_duration_;
131 // The bounds animator which gets used.
132 views::BoundsAnimator* bounds_animator_;
134 DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorDisabler);
137 // The MenuModelAdapter gets slightly changed to adapt the menu appearance to
138 // our requirements.
139 // TODO(bruthig): ShelfMenuModelAdapter does not appear to be used, remove it.
140 class ShelfMenuModelAdapter : public views::MenuModelAdapter {
141 public:
142 explicit ShelfMenuModelAdapter(ShelfMenuModel* menu_model);
144 // views::MenuModelAdapter:
145 const gfx::FontList* GetLabelFontList(int command_id) const override;
146 bool IsCommandEnabled(int id) const override;
147 void GetHorizontalIconMargins(int id,
148 int icon_size,
149 int* left_margin,
150 int* right_margin) const override;
151 bool GetForegroundColor(int command_id,
152 bool is_hovered,
153 SkColor* override_color) const override;
154 bool GetBackgroundColor(int command_id,
155 bool is_hovered,
156 SkColor* override_color) const override;
157 int GetMaxWidthForMenu(views::MenuItemView* menu) override;
158 bool ShouldReserveSpaceForSubmenuIndicator() const override;
160 private:
161 ShelfMenuModel* menu_model_;
163 DISALLOW_COPY_AND_ASSIGN(ShelfMenuModelAdapter);
166 ShelfMenuModelAdapter::ShelfMenuModelAdapter(ShelfMenuModel* menu_model)
167 : MenuModelAdapter(menu_model),
168 menu_model_(menu_model) {
171 const gfx::FontList* ShelfMenuModelAdapter::GetLabelFontList(
172 int command_id) const {
173 if (command_id != kCommandIdOfMenuName)
174 return MenuModelAdapter::GetLabelFontList(command_id);
176 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
177 return &rb->GetFontList(ui::ResourceBundle::BoldFont);
180 bool ShelfMenuModelAdapter::IsCommandEnabled(int id) const {
181 return id != kCommandIdOfMenuName;
184 bool ShelfMenuModelAdapter::GetForegroundColor(int command_id,
185 bool is_hovered,
186 SkColor* override_color) const {
187 if (command_id != kCommandIdOfMenuName)
188 return false;
190 *override_color = kCaptionItemForegroundColor;
191 return true;
194 bool ShelfMenuModelAdapter::GetBackgroundColor(int command_id,
195 bool is_hovered,
196 SkColor* override_color) const {
197 if (!menu_model_->IsCommandActive(command_id))
198 return false;
200 *override_color = is_hovered ? kFocusedActiveListItemBackgroundColor :
201 kActiveListItemBackgroundColor;
202 return true;
205 void ShelfMenuModelAdapter::GetHorizontalIconMargins(int command_id,
206 int icon_size,
207 int* left_margin,
208 int* right_margin) const {
209 *left_margin = kHorizontalIconSpacing;
210 *right_margin = (command_id != kCommandIdOfMenuName) ?
211 kHorizontalIconSpacing : -(icon_size + kHorizontalNoIconInsetSpacing);
214 int ShelfMenuModelAdapter::GetMaxWidthForMenu(views::MenuItemView* menu) {
215 return kMaximumAppMenuItemLength;
218 bool ShelfMenuModelAdapter::ShouldReserveSpaceForSubmenuIndicator() const {
219 return false;
222 // Custom FocusSearch used to navigate the shelf in the order items are in
223 // the ViewModel.
224 class ShelfFocusSearch : public views::FocusSearch {
225 public:
226 explicit ShelfFocusSearch(views::ViewModel* view_model)
227 : FocusSearch(NULL, true, true),
228 view_model_(view_model) {}
229 ~ShelfFocusSearch() override {}
231 // views::FocusSearch overrides:
232 View* FindNextFocusableView(View* starting_view,
233 bool reverse,
234 Direction direction,
235 bool check_starting_view,
236 views::FocusTraversable** focus_traversable,
237 View** focus_traversable_view) override {
238 int index = view_model_->GetIndexOfView(starting_view);
239 if (index == -1)
240 return view_model_->view_at(0);
242 if (reverse) {
243 --index;
244 if (index < 0)
245 index = view_model_->view_size() - 1;
246 } else {
247 ++index;
248 if (index >= view_model_->view_size())
249 index = 0;
251 return view_model_->view_at(index);
254 private:
255 views::ViewModel* view_model_;
257 DISALLOW_COPY_AND_ASSIGN(ShelfFocusSearch);
260 // AnimationDelegate used when inserting a new item. This steadily increases the
261 // opacity of the layer as the animation progress.
262 class FadeInAnimationDelegate : public gfx::AnimationDelegate {
263 public:
264 explicit FadeInAnimationDelegate(views::View* view) : view_(view) {}
265 ~FadeInAnimationDelegate() override {}
267 // AnimationDelegate overrides:
268 void AnimationProgressed(const Animation* animation) override {
269 view_->layer()->SetOpacity(animation->GetCurrentValue());
270 view_->layer()->ScheduleDraw();
272 void AnimationEnded(const Animation* animation) override {
273 view_->layer()->SetOpacity(1.0f);
274 view_->layer()->ScheduleDraw();
276 void AnimationCanceled(const Animation* animation) override {
277 view_->layer()->SetOpacity(1.0f);
278 view_->layer()->ScheduleDraw();
281 private:
282 views::View* view_;
284 DISALLOW_COPY_AND_ASSIGN(FadeInAnimationDelegate);
287 void ReflectItemStatus(const ShelfItem& item, ShelfButton* button) {
288 switch (item.status) {
289 case STATUS_CLOSED:
290 button->ClearState(ShelfButton::STATE_ACTIVE);
291 button->ClearState(ShelfButton::STATE_RUNNING);
292 button->ClearState(ShelfButton::STATE_ATTENTION);
293 break;
294 case STATUS_RUNNING:
295 button->ClearState(ShelfButton::STATE_ACTIVE);
296 button->AddState(ShelfButton::STATE_RUNNING);
297 button->ClearState(ShelfButton::STATE_ATTENTION);
298 break;
299 case STATUS_ACTIVE:
300 button->AddState(ShelfButton::STATE_ACTIVE);
301 button->ClearState(ShelfButton::STATE_RUNNING);
302 button->ClearState(ShelfButton::STATE_ATTENTION);
303 break;
304 case STATUS_ATTENTION:
305 button->ClearState(ShelfButton::STATE_ACTIVE);
306 button->ClearState(ShelfButton::STATE_RUNNING);
307 button->AddState(ShelfButton::STATE_ATTENTION);
308 break;
312 } // namespace
314 // AnimationDelegate used when deleting an item. This steadily decreased the
315 // opacity of the layer as the animation progress.
316 class ShelfView::FadeOutAnimationDelegate : public gfx::AnimationDelegate {
317 public:
318 FadeOutAnimationDelegate(ShelfView* host, views::View* view)
319 : shelf_view_(host),
320 view_(view) {}
321 ~FadeOutAnimationDelegate() override {}
323 // AnimationDelegate overrides:
324 void AnimationProgressed(const Animation* animation) override {
325 view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
326 view_->layer()->ScheduleDraw();
328 void AnimationEnded(const Animation* animation) override {
329 shelf_view_->OnFadeOutAnimationEnded();
331 void AnimationCanceled(const Animation* animation) override {}
333 private:
334 ShelfView* shelf_view_;
335 scoped_ptr<views::View> view_;
337 DISALLOW_COPY_AND_ASSIGN(FadeOutAnimationDelegate);
340 // AnimationDelegate used to trigger fading an element in. When an item is
341 // inserted this delegate is attached to the animation that expands the size of
342 // the item. When done it kicks off another animation to fade the item in.
343 class ShelfView::StartFadeAnimationDelegate : public gfx::AnimationDelegate {
344 public:
345 StartFadeAnimationDelegate(ShelfView* host,
346 views::View* view)
347 : shelf_view_(host),
348 view_(view) {}
349 ~StartFadeAnimationDelegate() override {}
351 // AnimationDelegate overrides:
352 void AnimationEnded(const Animation* animation) override {
353 shelf_view_->FadeIn(view_);
355 void AnimationCanceled(const Animation* animation) override {
356 view_->layer()->SetOpacity(1.0f);
359 private:
360 ShelfView* shelf_view_;
361 views::View* view_;
363 DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate);
366 ShelfView::ShelfView(ShelfModel* model,
367 ShelfDelegate* delegate,
368 ShelfLayoutManager* manager)
369 : model_(model),
370 delegate_(delegate),
371 view_model_(new views::ViewModel),
372 first_visible_index_(0),
373 last_visible_index_(-1),
374 overflow_button_(NULL),
375 owner_overflow_bubble_(NULL),
376 drag_pointer_(NONE),
377 drag_view_(NULL),
378 start_drag_index_(-1),
379 context_menu_id_(0),
380 leading_inset_(kDefaultLeadingInset),
381 cancelling_drag_model_changed_(false),
382 last_hidden_index_(0),
383 closing_event_time_(base::TimeDelta()),
384 got_deleted_(NULL),
385 drag_and_drop_item_pinned_(false),
386 drag_and_drop_shelf_id_(0),
387 drag_replaced_view_(nullptr),
388 dragged_off_shelf_(false),
389 snap_back_from_rip_off_view_(NULL),
390 item_manager_(Shell::GetInstance()->shelf_item_delegate_manager()),
391 layout_manager_(manager),
392 overflow_mode_(false),
393 main_shelf_(NULL),
394 dragged_off_from_overflow_to_shelf_(false),
395 is_repost_event_(false),
396 last_pressed_index_(-1) {
397 DCHECK(model_);
398 bounds_animator_.reset(new views::BoundsAnimator(this));
399 bounds_animator_->AddObserver(this);
400 set_context_menu_controller(this);
401 focus_search_.reset(new ShelfFocusSearch(view_model_.get()));
402 tooltip_.reset(new ShelfTooltipManager(manager, this));
405 ShelfView::~ShelfView() {
406 bounds_animator_->RemoveObserver(this);
407 model_->RemoveObserver(this);
408 // If we are inside the MenuRunner, we need to know if we were getting
409 // deleted while it was running.
410 if (got_deleted_)
411 *got_deleted_ = true;
414 void ShelfView::Init() {
415 model_->AddObserver(this);
417 const ShelfItems& items(model_->items());
418 for (ShelfItems::const_iterator i = items.begin(); i != items.end(); ++i) {
419 views::View* child = CreateViewForItem(*i);
420 child->SetPaintToLayer(true);
421 view_model_->Add(child, static_cast<int>(i - items.begin()));
422 AddChildView(child);
424 overflow_button_ = new OverflowButton(this);
425 overflow_button_->set_context_menu_controller(this);
426 ConfigureChildView(overflow_button_);
427 AddChildView(overflow_button_);
429 // We'll layout when our bounds change.
432 void ShelfView::OnShelfAlignmentChanged() {
433 overflow_button_->OnShelfAlignmentChanged();
434 LayoutToIdealBounds();
435 for (int i = 0; i < view_model_->view_size(); ++i) {
436 if (i >= first_visible_index_ && i <= last_visible_index_)
437 view_model_->view_at(i)->Layout();
439 tooltip_->Close();
440 if (overflow_bubble_)
441 overflow_bubble_->Hide();
444 void ShelfView::SchedulePaintForAllButtons() {
445 for (int i = 0; i < view_model_->view_size(); ++i) {
446 if (i >= first_visible_index_ && i <= last_visible_index_)
447 view_model_->view_at(i)->SchedulePaint();
449 if (overflow_button_ && overflow_button_->visible())
450 overflow_button_->SchedulePaint();
453 gfx::Rect ShelfView::GetIdealBoundsOfItemIcon(ShelfID id) {
454 int index = model_->ItemIndexByID(id);
455 if (index == -1)
456 return gfx::Rect();
457 // Map all items from overflow area to the overflow button. Note that the
458 // section between last_index_hidden_ and model_->FirstPanelIndex() is the
459 // list of invisible panel items. However, these items are currently nowhere
460 // represented and get dropped instead - see (crbug.com/378907). As such there
461 // is no way to address them or place them. We therefore move them over the
462 // overflow button.
463 if (index > last_visible_index_ && index < model_->FirstPanelIndex())
464 index = last_visible_index_ + 1;
465 const gfx::Rect& ideal_bounds(view_model_->ideal_bounds(index));
466 DCHECK_NE(TYPE_APP_LIST, model_->items()[index].type);
467 views::View* view = view_model_->view_at(index);
468 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
469 ShelfButton* button = static_cast<ShelfButton*>(view);
470 gfx::Rect icon_bounds = button->GetIconBounds();
471 return gfx::Rect(GetMirroredXWithWidthInView(
472 ideal_bounds.x() + icon_bounds.x(), icon_bounds.width()),
473 ideal_bounds.y() + icon_bounds.y(),
474 icon_bounds.width(),
475 icon_bounds.height());
478 void ShelfView::UpdatePanelIconPosition(ShelfID id,
479 const gfx::Point& midpoint) {
480 int current_index = model_->ItemIndexByID(id);
481 int first_panel_index = model_->FirstPanelIndex();
482 if (current_index < first_panel_index)
483 return;
485 gfx::Point midpoint_in_view(GetMirroredXInView(midpoint.x()),
486 midpoint.y());
487 int target_index = current_index;
488 while (target_index > first_panel_index &&
489 layout_manager_->PrimaryAxisValue(
490 view_model_->ideal_bounds(target_index).x(),
491 view_model_->ideal_bounds(target_index).y()) >
492 layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
493 midpoint_in_view.y())) {
494 --target_index;
496 while (target_index < view_model_->view_size() - 1 &&
497 layout_manager_->PrimaryAxisValue(
498 view_model_->ideal_bounds(target_index).right(),
499 view_model_->ideal_bounds(target_index).bottom()) <
500 layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
501 midpoint_in_view.y())) {
502 ++target_index;
504 if (current_index != target_index)
505 model_->Move(current_index, target_index);
508 bool ShelfView::IsShowingMenu() const {
509 return (launcher_menu_runner_.get() &&
510 launcher_menu_runner_->IsRunning());
513 bool ShelfView::IsShowingOverflowBubble() const {
514 return overflow_bubble_.get() && overflow_bubble_->IsShowing();
517 views::View* ShelfView::GetAppListButtonView() const {
518 for (int i = 0; i < model_->item_count(); ++i) {
519 if (model_->items()[i].type == TYPE_APP_LIST)
520 return view_model_->view_at(i);
523 NOTREACHED() << "Applist button not found";
524 return NULL;
527 ////////////////////////////////////////////////////////////////////////////////
528 // ShelfView, FocusTraversable implementation:
530 views::FocusSearch* ShelfView::GetFocusSearch() {
531 return focus_search_.get();
534 views::FocusTraversable* ShelfView::GetFocusTraversableParent() {
535 return parent()->GetFocusTraversable();
538 View* ShelfView::GetFocusTraversableParentView() {
539 return this;
542 void ShelfView::CreateDragIconProxy(
543 const gfx::Point& location_in_screen_coordinates,
544 const gfx::ImageSkia& icon,
545 views::View* replaced_view,
546 const gfx::Vector2d& cursor_offset_from_center,
547 float scale_factor) {
548 drag_replaced_view_ = replaced_view;
549 drag_image_.reset(new ash::DragImageView(
550 drag_replaced_view_->GetWidget()->GetNativeWindow()->GetRootWindow(),
551 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE));
552 drag_image_->SetImage(icon);
553 gfx::Size size = drag_image_->GetPreferredSize();
554 size.set_width(size.width() * scale_factor);
555 size.set_height(size.height() * scale_factor);
556 drag_image_offset_ = gfx::Vector2d(size.width() / 2, size.height() / 2) +
557 cursor_offset_from_center;
558 gfx::Rect drag_image_bounds(
559 location_in_screen_coordinates - drag_image_offset_,
560 size);
561 drag_image_->SetBoundsInScreen(drag_image_bounds);
562 drag_image_->SetWidgetVisible(true);
565 void ShelfView::UpdateDragIconProxy(
566 const gfx::Point& location_in_screen_coordinates) {
567 // TODO(jennyz): Investigate why drag_image_ becomes NULL at this point per
568 // crbug.com/34722, while the app list item is still being dragged around.
569 if (drag_image_) {
570 drag_image_->SetScreenPosition(
571 location_in_screen_coordinates - drag_image_offset_);
575 void ShelfView::DestroyDragIconProxy() {
576 drag_image_.reset();
577 drag_image_offset_ = gfx::Vector2d(0, 0);
580 bool ShelfView::StartDrag(const std::string& app_id,
581 const gfx::Point& location_in_screen_coordinates) {
582 // Bail if an operation is already going on - or the cursor is not inside.
583 // This could happen if mouse / touch operations overlap.
584 if (drag_and_drop_shelf_id_ ||
585 !GetBoundsInScreen().Contains(location_in_screen_coordinates))
586 return false;
588 // If the AppsGridView (which was dispatching this event) was opened by our
589 // button, ShelfView dragging operations are locked and we have to unlock.
590 CancelDrag(-1);
591 drag_and_drop_item_pinned_ = false;
592 drag_and_drop_app_id_ = app_id;
593 drag_and_drop_shelf_id_ =
594 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_);
595 // Check if the application is known and pinned - if not, we have to pin it so
596 // that we can re-arrange the shelf order accordingly. Note that items have
597 // to be pinned to give them the same (order) possibilities as a shortcut.
598 // When an item is dragged from overflow to shelf, IsShowingOverflowBubble()
599 // returns true. At this time, we don't need to pin the item.
600 if (!IsShowingOverflowBubble() &&
601 (!drag_and_drop_shelf_id_ ||
602 !delegate_->IsAppPinned(app_id))) {
603 delegate_->PinAppWithID(app_id);
604 drag_and_drop_shelf_id_ =
605 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_);
606 if (!drag_and_drop_shelf_id_)
607 return false;
608 drag_and_drop_item_pinned_ = true;
610 views::View* drag_and_drop_view = view_model_->view_at(
611 model_->ItemIndexByID(drag_and_drop_shelf_id_));
612 DCHECK(drag_and_drop_view);
614 // Since there is already an icon presented by the caller, we hide this item
615 // for now. That has to be done by reducing the size since the visibility will
616 // change once a regrouping animation is performed.
617 pre_drag_and_drop_size_ = drag_and_drop_view->size();
618 drag_and_drop_view->SetSize(gfx::Size());
620 // First we have to center the mouse cursor over the item.
621 gfx::Point pt = drag_and_drop_view->GetBoundsInScreen().CenterPoint();
622 views::View::ConvertPointFromScreen(drag_and_drop_view, &pt);
623 gfx::Point point_in_root = location_in_screen_coordinates;
624 ::wm::ConvertPointFromScreen(
625 ash::wm::GetRootWindowAt(location_in_screen_coordinates), &point_in_root);
626 ui::MouseEvent event(ui::ET_MOUSE_PRESSED, pt, point_in_root,
627 ui::EventTimeForNow(), 0, 0);
628 PointerPressedOnButton(drag_and_drop_view,
629 ShelfButtonHost::DRAG_AND_DROP,
630 event);
632 // Drag the item where it really belongs.
633 Drag(location_in_screen_coordinates);
634 return true;
637 bool ShelfView::Drag(const gfx::Point& location_in_screen_coordinates) {
638 if (!drag_and_drop_shelf_id_ ||
639 !GetBoundsInScreen().Contains(location_in_screen_coordinates))
640 return false;
642 gfx::Point pt = location_in_screen_coordinates;
643 views::View* drag_and_drop_view = view_model_->view_at(
644 model_->ItemIndexByID(drag_and_drop_shelf_id_));
645 ConvertPointFromScreen(drag_and_drop_view, &pt);
646 gfx::Point point_in_root = location_in_screen_coordinates;
647 ::wm::ConvertPointFromScreen(
648 ash::wm::GetRootWindowAt(location_in_screen_coordinates), &point_in_root);
649 ui::MouseEvent event(ui::ET_MOUSE_DRAGGED, pt, point_in_root,
650 ui::EventTimeForNow(), 0, 0);
651 PointerDraggedOnButton(drag_and_drop_view,
652 ShelfButtonHost::DRAG_AND_DROP,
653 event);
654 return true;
657 void ShelfView::EndDrag(bool cancel) {
658 if (!drag_and_drop_shelf_id_)
659 return;
661 views::View* drag_and_drop_view = view_model_->view_at(
662 model_->ItemIndexByID(drag_and_drop_shelf_id_));
663 PointerReleasedOnButton(
664 drag_and_drop_view, ShelfButtonHost::DRAG_AND_DROP, cancel);
666 // Either destroy the temporarily created item - or - make the item visible.
667 if (drag_and_drop_item_pinned_ && cancel) {
668 delegate_->UnpinAppWithID(drag_and_drop_app_id_);
669 } else if (drag_and_drop_view) {
670 if (cancel) {
671 // When a hosted drag gets canceled, the item can remain in the same slot
672 // and it might have moved within the bounds. In that case the item need
673 // to animate back to its correct location.
674 AnimateToIdealBounds();
675 } else {
676 drag_and_drop_view->SetSize(pre_drag_and_drop_size_);
680 drag_and_drop_shelf_id_ = 0;
683 void ShelfView::LayoutToIdealBounds() {
684 if (bounds_animator_->IsAnimating()) {
685 AnimateToIdealBounds();
686 return;
689 IdealBounds ideal_bounds;
690 CalculateIdealBounds(&ideal_bounds);
691 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
692 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
695 void ShelfView::UpdateAllButtonsVisibilityInOverflowMode() {
696 // The overflow button is not shown in overflow mode.
697 overflow_button_->SetVisible(false);
698 DCHECK_LT(last_visible_index_, view_model_->view_size());
699 for (int i = 0; i < view_model_->view_size(); ++i) {
700 bool visible = i >= first_visible_index_ &&
701 i <= last_visible_index_;
702 // To track the dragging of |drag_view_| continuously, its visibility
703 // should be always true regardless of its position.
704 if (dragged_off_from_overflow_to_shelf_ &&
705 view_model_->view_at(i) == drag_view_)
706 view_model_->view_at(i)->SetVisible(true);
707 else
708 view_model_->view_at(i)->SetVisible(visible);
712 void ShelfView::CalculateIdealBounds(IdealBounds* bounds) const {
713 int available_size = layout_manager_->PrimaryAxisValue(width(), height());
714 DCHECK(model_->item_count() == view_model_->view_size());
715 if (!available_size)
716 return;
718 int first_panel_index = model_->FirstPanelIndex();
719 int last_button_index = first_panel_index - 1;
721 int x = 0;
722 int y = 0;
723 int button_size = kShelfButtonSize;
724 int button_spacing = kShelfButtonSpacing;
726 int w = layout_manager_->PrimaryAxisValue(button_size, width());
727 int h = layout_manager_->PrimaryAxisValue(height(), button_size);
728 for (int i = 0; i < view_model_->view_size(); ++i) {
729 if (i < first_visible_index_) {
730 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, 0, 0));
731 continue;
734 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
735 x = layout_manager_->PrimaryAxisValue(x + w + button_spacing, x);
736 y = layout_manager_->PrimaryAxisValue(y, y + h + button_spacing);
739 if (is_overflow_mode()) {
740 const_cast<ShelfView*>(this)->UpdateAllButtonsVisibilityInOverflowMode();
741 return;
744 // Right aligned icons.
745 int end_position = available_size - button_spacing;
746 x = layout_manager_->PrimaryAxisValue(end_position, 0);
747 y = layout_manager_->PrimaryAxisValue(0, end_position);
748 for (int i = view_model_->view_size() - 1;
749 i >= first_panel_index; --i) {
750 x = layout_manager_->PrimaryAxisValue(x - w - button_spacing, x);
751 y = layout_manager_->PrimaryAxisValue(y, y - h - button_spacing);
752 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
753 end_position = layout_manager_->PrimaryAxisValue(x, y);
756 // Icons on the left / top are guaranteed up to kLeftIconProportion of
757 // the available space.
758 int last_icon_position = layout_manager_->PrimaryAxisValue(
759 view_model_->ideal_bounds(last_button_index).right(),
760 view_model_->ideal_bounds(last_button_index).bottom()) + button_size;
761 int reserved_icon_space = available_size * kReservedNonPanelIconProportion;
762 if (last_icon_position < reserved_icon_space)
763 end_position = last_icon_position;
764 else
765 end_position = std::max(end_position, reserved_icon_space);
767 bounds->overflow_bounds.set_size(
768 gfx::Size(layout_manager_->PrimaryAxisValue(w, width()),
769 layout_manager_->PrimaryAxisValue(height(), h)));
771 last_visible_index_ = DetermineLastVisibleIndex(
772 end_position - button_size);
773 last_hidden_index_ = DetermineFirstVisiblePanelIndex(end_position) - 1;
774 bool show_overflow = last_visible_index_ < last_button_index ||
775 last_hidden_index_ >= first_panel_index;
777 // Create Space for the overflow button
778 if (show_overflow) {
779 // The following code makes sure that platform apps icons (aligned to left /
780 // top) are favored over panel apps icons (aligned to right / bottom).
781 if (last_visible_index_ > 0 && last_visible_index_ < last_button_index) {
782 // This condition means that we will take one platform app and replace it
783 // with the overflow button and put the app in the overflow bubble.
784 // This happens when the space needed for platform apps exceeds the
785 // reserved area for non-panel icons,
786 // (i.e. |last_icon_position| > |reserved_icon_space|).
787 --last_visible_index_;
788 } else if (last_hidden_index_ >= first_panel_index &&
789 last_hidden_index_ < view_model_->view_size() - 1) {
790 // This condition means that we will take a panel app icon and replace it
791 // with the overflow button.
792 // This happens when there is still room for platform apps in the reserved
793 // area for non-panel icons,
794 // (i.e. |last_icon_position| < |reserved_icon_space|).
795 ++last_hidden_index_;
799 for (int i = 0; i < view_model_->view_size(); ++i) {
800 bool visible = i <= last_visible_index_ || i > last_hidden_index_;
801 // To receive drag event continuously from |drag_view_| during the dragging
802 // off from the shelf, don't make |drag_view_| invisible. It will be
803 // eventually invisible and removed from the |view_model_| by
804 // FinalizeRipOffDrag().
805 if (dragged_off_shelf_ && view_model_->view_at(i) == drag_view_)
806 continue;
807 view_model_->view_at(i)->SetVisible(visible);
810 overflow_button_->SetVisible(show_overflow);
811 if (show_overflow) {
812 DCHECK_NE(0, view_model_->view_size());
813 if (last_visible_index_ == -1) {
814 x = 0;
815 y = 0;
816 } else {
817 x = layout_manager_->PrimaryAxisValue(
818 view_model_->ideal_bounds(last_visible_index_).right(),
819 view_model_->ideal_bounds(last_visible_index_).x());
820 y = layout_manager_->PrimaryAxisValue(
821 view_model_->ideal_bounds(last_visible_index_).y(),
822 view_model_->ideal_bounds(last_visible_index_).bottom());
824 // Set all hidden panel icon positions to be on the overflow button.
825 for (int i = first_panel_index; i <= last_hidden_index_; ++i)
826 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
828 // Add more space between last visible item and overflow button.
829 // Without this, two buttons look too close compared with other items.
830 x = layout_manager_->PrimaryAxisValue(x + button_spacing, x);
831 y = layout_manager_->PrimaryAxisValue(y, y + button_spacing);
833 bounds->overflow_bounds.set_x(x);
834 bounds->overflow_bounds.set_y(y);
835 if (overflow_bubble_.get() && overflow_bubble_->IsShowing())
836 UpdateOverflowRange(overflow_bubble_->shelf_view());
837 } else {
838 if (overflow_bubble_)
839 overflow_bubble_->Hide();
843 int ShelfView::DetermineLastVisibleIndex(int max_value) const {
844 int index = model_->FirstPanelIndex() - 1;
845 while (index >= 0 &&
846 layout_manager_->PrimaryAxisValue(
847 view_model_->ideal_bounds(index).right(),
848 view_model_->ideal_bounds(index).bottom()) > max_value) {
849 index--;
851 return index;
854 int ShelfView::DetermineFirstVisiblePanelIndex(int min_value) const {
855 int index = model_->FirstPanelIndex();
856 while (index < view_model_->view_size() &&
857 layout_manager_->PrimaryAxisValue(
858 view_model_->ideal_bounds(index).right(),
859 view_model_->ideal_bounds(index).bottom()) < min_value) {
860 ++index;
862 return index;
865 void ShelfView::AddIconObserver(ShelfIconObserver* observer) {
866 observers_.AddObserver(observer);
869 void ShelfView::RemoveIconObserver(ShelfIconObserver* observer) {
870 observers_.RemoveObserver(observer);
873 void ShelfView::AnimateToIdealBounds() {
874 IdealBounds ideal_bounds;
875 CalculateIdealBounds(&ideal_bounds);
876 for (int i = 0; i < view_model_->view_size(); ++i) {
877 View* view = view_model_->view_at(i);
878 bounds_animator_->AnimateViewTo(view, view_model_->ideal_bounds(i));
879 // Now that the item animation starts, we have to make sure that the
880 // padding of the first gets properly transferred to the new first item.
881 if (i && view->border())
882 view->SetBorder(views::Border::NullBorder());
884 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
887 views::View* ShelfView::CreateViewForItem(const ShelfItem& item) {
888 views::View* view = NULL;
889 switch (item.type) {
890 case TYPE_BROWSER_SHORTCUT:
891 case TYPE_APP_SHORTCUT:
892 case TYPE_WINDOWED_APP:
893 case TYPE_PLATFORM_APP:
894 case TYPE_DIALOG:
895 case TYPE_APP_PANEL: {
896 ShelfButton* button = ShelfButton::Create(this, this, layout_manager_);
897 button->SetImage(item.image);
898 ReflectItemStatus(item, button);
899 view = button;
900 break;
903 case TYPE_APP_LIST: {
904 view = new AppListButton(this, this, layout_manager_->shelf_widget());
905 break;
908 default:
909 break;
911 view->set_context_menu_controller(this);
913 DCHECK(view);
914 ConfigureChildView(view);
915 return view;
918 void ShelfView::FadeIn(views::View* view) {
919 view->SetVisible(true);
920 view->layer()->SetOpacity(0);
921 AnimateToIdealBounds();
922 bounds_animator_->SetAnimationDelegate(
923 view,
924 scoped_ptr<gfx::AnimationDelegate>(new FadeInAnimationDelegate(view)));
927 void ShelfView::PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event) {
928 DCHECK(!dragging());
929 DCHECK(drag_view_);
930 drag_pointer_ = pointer;
931 start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
933 if (start_drag_index_== -1) {
934 CancelDrag(-1);
935 return;
938 // If the item is no longer draggable, bail out.
939 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
940 model_->items()[start_drag_index_].id);
941 if (!item_delegate->IsDraggable()) {
942 CancelDrag(-1);
943 return;
946 // Move the view to the front so that it appears on top of other views.
947 ReorderChildView(drag_view_, -1);
948 bounds_animator_->StopAnimatingView(drag_view_);
951 void ShelfView::ContinueDrag(const ui::LocatedEvent& event) {
952 // Due to a syncing operation the application might have been removed.
953 // Bail if it is gone.
954 int current_index = view_model_->GetIndexOfView(drag_view_);
955 DCHECK_NE(-1, current_index);
957 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
958 model_->items()[current_index].id);
959 if (!item_delegate->IsDraggable()) {
960 CancelDrag(-1);
961 return;
964 // If this is not a drag and drop host operation and not the app list item,
965 // check if the item got ripped off the shelf - if it did we are done.
966 if (!drag_and_drop_shelf_id_ &&
967 RemovableByRipOff(current_index) != NOT_REMOVABLE) {
968 if (HandleRipOffDrag(event))
969 return;
970 // The rip off handler could have changed the location of the item.
971 current_index = view_model_->GetIndexOfView(drag_view_);
974 // TODO: I don't think this works correctly with RTL.
975 gfx::Point drag_point(event.location());
976 ConvertPointToTarget(drag_view_, this, &drag_point);
978 // Constrain the location to the range of valid indices for the type.
979 std::pair<int, int> indices(GetDragRange(current_index));
980 int first_drag_index = indices.first;
981 int last_drag_index = indices.second;
982 // If the last index isn't valid, we're overflowing. Constrain to the app list
983 // (which is the last visible item).
984 if (first_drag_index < model_->FirstPanelIndex() &&
985 last_drag_index > last_visible_index_)
986 last_drag_index = last_visible_index_;
987 int x = 0, y = 0;
988 if (layout_manager_->IsHorizontalAlignment()) {
989 x = std::max(view_model_->ideal_bounds(indices.first).x(),
990 drag_point.x() - drag_origin_.x());
991 x = std::min(view_model_->ideal_bounds(last_drag_index).right() -
992 view_model_->ideal_bounds(current_index).width(),
994 if (drag_view_->x() == x)
995 return;
996 drag_view_->SetX(x);
997 } else {
998 y = std::max(view_model_->ideal_bounds(indices.first).y(),
999 drag_point.y() - drag_origin_.y());
1000 y = std::min(view_model_->ideal_bounds(last_drag_index).bottom() -
1001 view_model_->ideal_bounds(current_index).height(),
1003 if (drag_view_->y() == y)
1004 return;
1005 drag_view_->SetY(y);
1008 int target_index =
1009 views::ViewModelUtils::DetermineMoveIndex(
1010 *view_model_, drag_view_,
1011 layout_manager_->IsHorizontalAlignment() ?
1012 views::ViewModelUtils::HORIZONTAL :
1013 views::ViewModelUtils::VERTICAL,
1014 x, y);
1015 target_index =
1016 std::min(indices.second, std::max(target_index, indices.first));
1017 if (target_index == current_index)
1018 return;
1020 // Change the model, the ShelfItemMoved() callback will handle the
1021 // |view_model_| update.
1022 model_->Move(current_index, target_index);
1023 bounds_animator_->StopAnimatingView(drag_view_);
1026 bool ShelfView::HandleRipOffDrag(const ui::LocatedEvent& event) {
1027 int current_index = view_model_->GetIndexOfView(drag_view_);
1028 DCHECK_NE(-1, current_index);
1029 std::string dragged_app_id =
1030 delegate_->GetAppIDForShelfID(model_->items()[current_index].id);
1032 gfx::Point screen_location = event.root_location();
1033 ::wm::ConvertPointToScreen(GetWidget()->GetNativeWindow()->GetRootWindow(),
1034 &screen_location);
1036 // To avoid ugly forwards and backwards flipping we use different constants
1037 // for ripping off / re-inserting the items.
1038 if (dragged_off_shelf_) {
1039 // If the shelf/overflow bubble bounds contains |screen_location| we insert
1040 // the item back into the shelf.
1041 if (GetBoundsForDragInsertInScreen().Contains(screen_location)) {
1042 if (dragged_off_from_overflow_to_shelf_) {
1043 // During the dragging an item from Shelf to Overflow, it can enter here
1044 // directly because both are located very closly.
1045 main_shelf_->EndDrag(true);
1046 // Stops the animation of |drag_view_| and sets its bounds explicitly
1047 // becase ContinueDrag() stops its animation. Without this, unexpected
1048 // bounds will be set.
1049 bounds_animator_->StopAnimatingView(drag_view_);
1050 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1051 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index));
1052 dragged_off_from_overflow_to_shelf_ = false;
1054 // Destroy our proxy view item.
1055 DestroyDragIconProxy();
1056 // Re-insert the item and return simply false since the caller will handle
1057 // the move as in any normal case.
1058 dragged_off_shelf_ = false;
1059 drag_view_->layer()->SetOpacity(1.0f);
1060 // The size of Overflow bubble should be updated immediately when an item
1061 // is re-inserted.
1062 if (is_overflow_mode())
1063 PreferredSizeChanged();
1064 return false;
1065 } else if (is_overflow_mode() &&
1066 main_shelf_->GetBoundsForDragInsertInScreen().Contains(
1067 screen_location)) {
1068 if (!dragged_off_from_overflow_to_shelf_) {
1069 dragged_off_from_overflow_to_shelf_ = true;
1070 drag_image_->SetOpacity(1.0f);
1071 main_shelf_->StartDrag(dragged_app_id, screen_location);
1072 } else {
1073 main_shelf_->Drag(screen_location);
1075 } else if (dragged_off_from_overflow_to_shelf_) {
1076 // Makes the |drag_image_| partially disappear again.
1077 dragged_off_from_overflow_to_shelf_ = false;
1078 drag_image_->SetOpacity(kDraggedImageOpacity);
1079 main_shelf_->EndDrag(true);
1080 bounds_animator_->StopAnimatingView(drag_view_);
1081 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1082 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index));
1084 // Move our proxy view item.
1085 UpdateDragIconProxy(screen_location);
1086 return true;
1088 // Check if we are too far away from the shelf to enter the ripped off state.
1089 // Determine the distance to the shelf.
1090 int delta = CalculateShelfDistance(screen_location);
1091 if (delta > kRipOffDistance) {
1092 // Create a proxy view item which can be moved anywhere.
1093 CreateDragIconProxy(event.root_location(),
1094 drag_view_->GetImage(),
1095 drag_view_,
1096 gfx::Vector2d(0, 0),
1097 kDragAndDropProxyScale);
1098 drag_view_->layer()->SetOpacity(0.0f);
1099 dragged_off_shelf_ = true;
1100 if (RemovableByRipOff(current_index) == REMOVABLE) {
1101 // Move the item to the front of the first panel item and hide it.
1102 // ShelfItemMoved() callback will handle the |view_model_| update and
1103 // call AnimateToIdealBounds().
1104 if (current_index != model_->FirstPanelIndex() - 1) {
1105 model_->Move(current_index, model_->FirstPanelIndex() - 1);
1106 StartFadeInLastVisibleItem();
1107 } else if (is_overflow_mode()) {
1108 // Overflow bubble should be shrunk when an item is ripped off.
1109 PreferredSizeChanged();
1111 // Make the item partially disappear to show that it will get removed if
1112 // dropped.
1113 drag_image_->SetOpacity(kDraggedImageOpacity);
1115 return true;
1117 return false;
1120 void ShelfView::FinalizeRipOffDrag(bool cancel) {
1121 if (!dragged_off_shelf_)
1122 return;
1123 // Make sure we do not come in here again.
1124 dragged_off_shelf_ = false;
1126 // Coming here we should always have a |drag_view_|.
1127 DCHECK(drag_view_);
1128 int current_index = view_model_->GetIndexOfView(drag_view_);
1129 // If the view isn't part of the model anymore (|current_index| == -1), a sync
1130 // operation must have removed it. In that case we shouldn't change the model
1131 // and only delete the proxy image.
1132 if (current_index == -1) {
1133 DestroyDragIconProxy();
1134 return;
1137 // Set to true when the animation should snap back to where it was before.
1138 bool snap_back = false;
1139 // Items which cannot be dragged off will be handled as a cancel.
1140 if (!cancel) {
1141 if (dragged_off_from_overflow_to_shelf_) {
1142 dragged_off_from_overflow_to_shelf_ = false;
1143 main_shelf_->EndDrag(false);
1144 drag_view_->layer()->SetOpacity(1.0f);
1145 } else if (RemovableByRipOff(current_index) != REMOVABLE) {
1146 // Make sure we do not try to remove un-removable items like items which
1147 // were not pinned or have to be always there.
1148 cancel = true;
1149 snap_back = true;
1150 } else {
1151 // Make sure the item stays invisible upon removal.
1152 drag_view_->SetVisible(false);
1153 std::string app_id =
1154 delegate_->GetAppIDForShelfID(model_->items()[current_index].id);
1155 delegate_->UnpinAppWithID(app_id);
1158 if (cancel || snap_back) {
1159 if (dragged_off_from_overflow_to_shelf_) {
1160 dragged_off_from_overflow_to_shelf_ = false;
1161 // Main shelf handles revert of dragged item.
1162 main_shelf_->EndDrag(true);
1163 drag_view_->layer()->SetOpacity(1.0f);
1164 } else if (!cancelling_drag_model_changed_) {
1165 // Only do something if the change did not come through a model change.
1166 gfx::Rect drag_bounds = drag_image_->GetBoundsInScreen();
1167 gfx::Point relative_to = GetBoundsInScreen().origin();
1168 gfx::Rect target(
1169 gfx::PointAtOffsetFromOrigin(drag_bounds.origin()- relative_to),
1170 drag_bounds.size());
1171 drag_view_->SetBoundsRect(target);
1172 // Hide the status from the active item since we snap it back now. Upon
1173 // animation end the flag gets cleared if |snap_back_from_rip_off_view_|
1174 // is set.
1175 snap_back_from_rip_off_view_ = drag_view_;
1176 drag_view_->AddState(ShelfButton::STATE_HIDDEN);
1177 // When a canceling drag model is happening, the view model is diverged
1178 // from the menu model and movements / animations should not be done.
1179 model_->Move(current_index, start_drag_index_);
1180 AnimateToIdealBounds();
1182 drag_view_->layer()->SetOpacity(1.0f);
1184 DestroyDragIconProxy();
1187 ShelfView::RemovableState ShelfView::RemovableByRipOff(int index) const {
1188 DCHECK(index >= 0 && index < model_->item_count());
1189 ShelfItemType type = model_->items()[index].type;
1190 if (type == TYPE_APP_LIST || type == TYPE_DIALOG || !delegate_->CanPin())
1191 return NOT_REMOVABLE;
1193 std::string app_id = delegate_->GetAppIDForShelfID(model_->items()[index].id);
1194 // Note: Only pinned app shortcuts can be removed!
1195 return (type == TYPE_APP_SHORTCUT && delegate_->IsAppPinned(app_id)) ?
1196 REMOVABLE : DRAGGABLE;
1199 bool ShelfView::SameDragType(ShelfItemType typea, ShelfItemType typeb) const {
1200 switch (typea) {
1201 case TYPE_APP_SHORTCUT:
1202 case TYPE_BROWSER_SHORTCUT:
1203 return (typeb == TYPE_APP_SHORTCUT || typeb == TYPE_BROWSER_SHORTCUT);
1204 case TYPE_APP_LIST:
1205 case TYPE_PLATFORM_APP:
1206 case TYPE_WINDOWED_APP:
1207 case TYPE_APP_PANEL:
1208 case TYPE_DIALOG:
1209 return typeb == typea;
1210 case TYPE_UNDEFINED:
1211 NOTREACHED() << "ShelfItemType must be set.";
1212 return false;
1214 NOTREACHED();
1215 return false;
1218 std::pair<int, int> ShelfView::GetDragRange(int index) {
1219 int min_index = -1;
1220 int max_index = -1;
1221 ShelfItemType type = model_->items()[index].type;
1222 for (int i = 0; i < model_->item_count(); ++i) {
1223 if (SameDragType(model_->items()[i].type, type)) {
1224 if (min_index == -1)
1225 min_index = i;
1226 max_index = i;
1229 return std::pair<int, int>(min_index, max_index);
1232 void ShelfView::ConfigureChildView(views::View* view) {
1233 view->SetPaintToLayer(true);
1234 view->layer()->SetFillsBoundsOpaquely(false);
1237 void ShelfView::ToggleOverflowBubble() {
1238 if (IsShowingOverflowBubble()) {
1239 overflow_bubble_->Hide();
1240 return;
1243 if (!overflow_bubble_)
1244 overflow_bubble_.reset(new OverflowBubble());
1246 ShelfView* overflow_view =
1247 new ShelfView(model_, delegate_, layout_manager_);
1248 overflow_view->overflow_mode_ = true;
1249 overflow_view->Init();
1250 overflow_view->set_owner_overflow_bubble(overflow_bubble_.get());
1251 overflow_view->OnShelfAlignmentChanged();
1252 overflow_view->main_shelf_ = this;
1253 UpdateOverflowRange(overflow_view);
1255 overflow_bubble_->Show(overflow_button_, overflow_view);
1257 Shell::GetInstance()->UpdateShelfVisibility();
1260 void ShelfView::OnFadeOutAnimationEnded() {
1261 AnimateToIdealBounds();
1262 StartFadeInLastVisibleItem();
1265 void ShelfView::StartFadeInLastVisibleItem() {
1266 // If overflow button is visible and there is a valid new last item, fading
1267 // the new last item in after sliding animation is finished.
1268 if (overflow_button_->visible() && last_visible_index_ >= 0) {
1269 views::View* last_visible_view = view_model_->view_at(last_visible_index_);
1270 last_visible_view->layer()->SetOpacity(0);
1271 bounds_animator_->SetAnimationDelegate(
1272 last_visible_view,
1273 scoped_ptr<gfx::AnimationDelegate>(
1274 new StartFadeAnimationDelegate(this, last_visible_view)));
1278 void ShelfView::UpdateOverflowRange(ShelfView* overflow_view) const {
1279 const int first_overflow_index = last_visible_index_ + 1;
1280 const int last_overflow_index = last_hidden_index_;
1281 DCHECK_LE(first_overflow_index, last_overflow_index);
1282 DCHECK_LT(last_overflow_index, view_model_->view_size());
1284 overflow_view->first_visible_index_ = first_overflow_index;
1285 overflow_view->last_visible_index_ = last_overflow_index;
1288 bool ShelfView::ShouldHideTooltip(const gfx::Point& cursor_location) {
1289 gfx::Rect active_bounds;
1291 for (int i = 0; i < child_count(); ++i) {
1292 views::View* child = child_at(i);
1293 if (child == overflow_button_)
1294 continue;
1295 if (!ShouldShowTooltipForView(child))
1296 continue;
1298 gfx::Rect child_bounds = child->GetMirroredBounds();
1299 active_bounds.Union(child_bounds);
1302 return !active_bounds.Contains(cursor_location);
1305 gfx::Rect ShelfView::GetVisibleItemsBoundsInScreen() {
1306 gfx::Size preferred_size = GetPreferredSize();
1307 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1308 ConvertPointToScreen(this, &origin);
1309 return gfx::Rect(origin, preferred_size);
1312 gfx::Rect ShelfView::GetBoundsForDragInsertInScreen() {
1313 gfx::Size preferred_size;
1314 if (is_overflow_mode()) {
1315 DCHECK(owner_overflow_bubble_);
1316 gfx::Rect bubble_bounds =
1317 owner_overflow_bubble_->bubble_view()->GetBubbleBounds();
1318 preferred_size = bubble_bounds.size();
1319 } else {
1320 const int last_button_index = view_model_->view_size() - 1;
1321 gfx::Rect last_button_bounds =
1322 view_model_->view_at(last_button_index)->bounds();
1323 if (overflow_button_->visible() &&
1324 model_->GetItemIndexForType(TYPE_APP_PANEL) == -1) {
1325 // When overflow button is visible and shelf has no panel items,
1326 // last_button_bounds should be overflow button's bounds.
1327 last_button_bounds = overflow_button_->bounds();
1330 if (layout_manager_->IsHorizontalAlignment()) {
1331 preferred_size = gfx::Size(last_button_bounds.right() + leading_inset_,
1332 kShelfSize);
1333 } else {
1334 preferred_size = gfx::Size(kShelfSize,
1335 last_button_bounds.bottom() + leading_inset_);
1338 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1340 // In overflow mode, we should use OverflowBubbleView as a source for
1341 // converting |origin| to screen coordinates. When a scroll operation is
1342 // occurred in OverflowBubble, the bounds of ShelfView in OverflowBubble can
1343 // be changed.
1344 if (is_overflow_mode())
1345 ConvertPointToScreen(owner_overflow_bubble_->bubble_view(), &origin);
1346 else
1347 ConvertPointToScreen(this, &origin);
1349 return gfx::Rect(origin, preferred_size);
1352 int ShelfView::CancelDrag(int modified_index) {
1353 FinalizeRipOffDrag(true);
1354 if (!drag_view_)
1355 return modified_index;
1356 bool was_dragging = dragging();
1357 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1358 drag_pointer_ = NONE;
1359 drag_view_ = NULL;
1360 if (drag_view_index == modified_index) {
1361 // The view that was being dragged is being modified. Don't do anything.
1362 return modified_index;
1364 if (!was_dragging)
1365 return modified_index;
1367 // Restore previous position, tracking the position of the modified view.
1368 bool at_end = modified_index == view_model_->view_size();
1369 views::View* modified_view =
1370 (modified_index >= 0 && !at_end) ?
1371 view_model_->view_at(modified_index) : NULL;
1372 model_->Move(drag_view_index, start_drag_index_);
1374 // If the modified view will be at the end of the list, return the new end of
1375 // the list.
1376 if (at_end)
1377 return view_model_->view_size();
1378 return modified_view ? view_model_->GetIndexOfView(modified_view) : -1;
1381 gfx::Size ShelfView::GetPreferredSize() const {
1382 IdealBounds ideal_bounds;
1383 CalculateIdealBounds(&ideal_bounds);
1385 int last_button_index = is_overflow_mode() ?
1386 last_visible_index_ : view_model_->view_size() - 1;
1388 // When an item is dragged off from the overflow bubble, it is moved to last
1389 // position and and changed to invisible. Overflow bubble size should be
1390 // shrunk to fit only for visible items.
1391 // If |dragged_off_from_overflow_to_shelf_| is set, there will be no invisible
1392 // items in the shelf.
1393 if (is_overflow_mode() &&
1394 dragged_off_shelf_ &&
1395 !dragged_off_from_overflow_to_shelf_ &&
1396 RemovableByRipOff(view_model_->GetIndexOfView(drag_view_)) == REMOVABLE)
1397 last_button_index--;
1399 const gfx::Rect last_button_bounds =
1400 last_button_index >= first_visible_index_ ?
1401 view_model_->ideal_bounds(last_button_index) :
1402 gfx::Rect(gfx::Size(kShelfSize, kShelfSize));
1404 if (layout_manager_->IsHorizontalAlignment()) {
1405 return gfx::Size(last_button_bounds.right() + leading_inset_, kShelfSize);
1408 return gfx::Size(kShelfSize,
1409 last_button_bounds.bottom() + leading_inset_);
1412 void ShelfView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1413 // This bounds change is produced by the shelf movement and all content has
1414 // to follow. Using an animation at that time would produce a time lag since
1415 // the animation of the BoundsAnimator has itself a delay before it arrives
1416 // at the required location. As such we tell the animator to go there
1417 // immediately.
1418 BoundsAnimatorDisabler disabler(bounds_animator_.get());
1419 LayoutToIdealBounds();
1420 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1421 OnShelfIconPositionsChanged());
1423 if (IsShowingOverflowBubble())
1424 overflow_bubble_->Hide();
1427 views::FocusTraversable* ShelfView::GetPaneFocusTraversable() {
1428 return this;
1431 void ShelfView::GetAccessibleState(ui::AXViewState* state) {
1432 state->role = ui::AX_ROLE_TOOLBAR;
1433 state->name = l10n_util::GetStringUTF16(IDS_ASH_SHELF_ACCESSIBLE_NAME);
1436 void ShelfView::OnGestureEvent(ui::GestureEvent* event) {
1437 aura::Window* target_window = static_cast<views::View*>(event->target())
1438 ->GetWidget()
1439 ->GetNativeWindow();
1440 if (gesture_handler_.ProcessGestureEvent(*event, target_window))
1441 event->StopPropagation();
1444 void ShelfView::ShelfItemAdded(int model_index) {
1446 base::AutoReset<bool> cancelling_drag(
1447 &cancelling_drag_model_changed_, true);
1448 model_index = CancelDrag(model_index);
1450 views::View* view = CreateViewForItem(model_->items()[model_index]);
1451 AddChildView(view);
1452 // Hide the view, it'll be made visible when the animation is done. Using
1453 // opacity 0 here to avoid messing with CalculateIdealBounds which touches
1454 // the view's visibility.
1455 view->layer()->SetOpacity(0);
1456 view_model_->Add(view, model_index);
1458 // Give the button its ideal bounds. That way if we end up animating the
1459 // button before this animation completes it doesn't appear at some random
1460 // spot (because it was in the middle of animating from 0,0 0x0 to its
1461 // target).
1462 IdealBounds ideal_bounds;
1463 CalculateIdealBounds(&ideal_bounds);
1464 view->SetBoundsRect(view_model_->ideal_bounds(model_index));
1466 // The first animation moves all the views to their target position. |view|
1467 // is hidden, so it visually appears as though we are providing space for
1468 // it. When done we'll fade the view in.
1469 AnimateToIdealBounds();
1470 if (model_index <= last_visible_index_ ||
1471 model_index >= model_->FirstPanelIndex()) {
1472 bounds_animator_->SetAnimationDelegate(
1473 view,
1474 scoped_ptr<gfx::AnimationDelegate>(
1475 new StartFadeAnimationDelegate(this, view)));
1476 } else {
1477 // Undo the hiding if animation does not run.
1478 view->layer()->SetOpacity(1.0f);
1482 void ShelfView::ShelfItemRemoved(int model_index, ShelfID id) {
1483 if (id == context_menu_id_)
1484 launcher_menu_runner_.reset();
1486 base::AutoReset<bool> cancelling_drag(
1487 &cancelling_drag_model_changed_, true);
1488 model_index = CancelDrag(model_index);
1490 views::View* view = view_model_->view_at(model_index);
1491 view_model_->Remove(model_index);
1493 // When the overflow bubble is visible, the overflow range needs to be set
1494 // before CalculateIdealBounds() gets called. Otherwise CalculateIdealBounds()
1495 // could trigger a ShelfItemChanged() by hiding the overflow bubble and
1496 // since the overflow bubble is not yet synced with the ShelfModel this
1497 // could cause a crash.
1498 if (overflow_bubble_ && overflow_bubble_->IsShowing()) {
1499 last_hidden_index_ = std::min(last_hidden_index_,
1500 view_model_->view_size() - 1);
1501 UpdateOverflowRange(overflow_bubble_->shelf_view());
1504 if (view->visible()) {
1505 // The first animation fades out the view. When done we'll animate the rest
1506 // of the views to their target location.
1507 bounds_animator_->AnimateViewTo(view, view->bounds());
1508 bounds_animator_->SetAnimationDelegate(
1509 view,
1510 scoped_ptr<gfx::AnimationDelegate>(
1511 new FadeOutAnimationDelegate(this, view)));
1512 } else {
1513 // We don't need to show a fade out animation for invisible |view|. When an
1514 // item is ripped out from the shelf, its |view| is already invisible.
1515 AnimateToIdealBounds();
1518 // Close the tooltip because it isn't needed any longer and its anchor view
1519 // will be deleted soon.
1520 if (tooltip_->GetCurrentAnchorView() == view)
1521 tooltip_->Close();
1524 void ShelfView::ShelfItemChanged(int model_index, const ShelfItem& old_item) {
1525 const ShelfItem& item(model_->items()[model_index]);
1526 if (old_item.type != item.type) {
1527 // Type changed, swap the views.
1528 model_index = CancelDrag(model_index);
1529 scoped_ptr<views::View> old_view(view_model_->view_at(model_index));
1530 bounds_animator_->StopAnimatingView(old_view.get());
1531 // Removing and re-inserting a view in our view model will strip the ideal
1532 // bounds from the item. To avoid recalculation of everything the bounds
1533 // get remembered and restored after the insertion to the previous value.
1534 gfx::Rect old_ideal_bounds = view_model_->ideal_bounds(model_index);
1535 view_model_->Remove(model_index);
1536 views::View* new_view = CreateViewForItem(item);
1537 AddChildView(new_view);
1538 view_model_->Add(new_view, model_index);
1539 view_model_->set_ideal_bounds(model_index, old_ideal_bounds);
1540 new_view->SetBoundsRect(old_view->bounds());
1541 return;
1544 views::View* view = view_model_->view_at(model_index);
1545 switch (item.type) {
1546 case TYPE_BROWSER_SHORTCUT:
1547 // Fallthrough for the new Shelf since it needs to show the activation
1548 // change as well.
1549 case TYPE_APP_SHORTCUT:
1550 case TYPE_WINDOWED_APP:
1551 case TYPE_PLATFORM_APP:
1552 case TYPE_DIALOG:
1553 case TYPE_APP_PANEL: {
1554 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1555 ShelfButton* button = static_cast<ShelfButton*>(view);
1556 ReflectItemStatus(item, button);
1557 // The browser shortcut is currently not a "real" item and as such the
1558 // the image is bogous as well. We therefore keep the image as is for it.
1559 if (item.type != TYPE_BROWSER_SHORTCUT)
1560 button->SetImage(item.image);
1561 button->SchedulePaint();
1562 break;
1565 default:
1566 break;
1570 void ShelfView::ShelfItemMoved(int start_index, int target_index) {
1571 view_model_->Move(start_index, target_index);
1572 // When cancelling a drag due to a shelf item being added, the currently
1573 // dragged item is moved back to its initial position. AnimateToIdealBounds
1574 // will be called again when the new item is added to the |view_model_| but
1575 // at this time the |view_model_| is inconsistent with the |model_|.
1576 if (!cancelling_drag_model_changed_)
1577 AnimateToIdealBounds();
1580 void ShelfView::ShelfStatusChanged() {
1581 // Nothing to do here.
1584 void ShelfView::PointerPressedOnButton(views::View* view,
1585 Pointer pointer,
1586 const ui::LocatedEvent& event) {
1587 if (drag_view_)
1588 return;
1590 int index = view_model_->GetIndexOfView(view);
1591 if (index == -1)
1592 return;
1594 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1595 model_->items()[index].id);
1596 if (view_model_->view_size() <= 1 || !item_delegate->IsDraggable())
1597 return; // View is being deleted or not draggable, ignore request.
1599 // Only when the repost event occurs on the same shelf item, we should ignore
1600 // the call in ShelfView::ButtonPressed(...).
1601 is_repost_event_ = IsRepostEvent(event) && (last_pressed_index_ == index);
1603 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1604 drag_view_ = static_cast<ShelfButton*>(view);
1605 drag_origin_ = gfx::Point(event.x(), event.y());
1606 UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage",
1607 layout_manager_->SelectValueForShelfAlignment(
1608 SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM,
1609 SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT,
1610 SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT,
1611 -1),
1612 SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT);
1615 void ShelfView::PointerDraggedOnButton(views::View* view,
1616 Pointer pointer,
1617 const ui::LocatedEvent& event) {
1618 // To prepare all drag types (moving an item in the shelf and dragging off),
1619 // we should check the x-axis and y-axis offset.
1620 if (!dragging() && drag_view_ &&
1621 ((std::abs(event.x() - drag_origin_.x()) >= kMinimumDragDistance) ||
1622 (std::abs(event.y() - drag_origin_.y()) >= kMinimumDragDistance))) {
1623 PrepareForDrag(pointer, event);
1625 if (drag_pointer_ == pointer)
1626 ContinueDrag(event);
1629 void ShelfView::PointerReleasedOnButton(views::View* view,
1630 Pointer pointer,
1631 bool canceled) {
1632 // Reset |is_repost_event| to false.
1633 is_repost_event_ = false;
1635 if (canceled) {
1636 CancelDrag(-1);
1637 } else if (drag_pointer_ == pointer) {
1638 FinalizeRipOffDrag(false);
1639 drag_pointer_ = NONE;
1640 AnimateToIdealBounds();
1642 // If the drag pointer is NONE, no drag operation is going on and the
1643 // drag_view can be released.
1644 if (drag_pointer_ == NONE)
1645 drag_view_ = NULL;
1648 void ShelfView::MouseMovedOverButton(views::View* view) {
1649 if (!ShouldShowTooltipForView(view))
1650 return;
1652 if (!tooltip_->IsVisible())
1653 tooltip_->ResetTimer();
1656 void ShelfView::MouseEnteredButton(views::View* view) {
1657 if (!ShouldShowTooltipForView(view))
1658 return;
1660 if (tooltip_->IsVisible()) {
1661 tooltip_->ShowImmediately(view, GetAccessibleName(view));
1662 } else {
1663 tooltip_->ShowDelayed(view, GetAccessibleName(view));
1667 void ShelfView::MouseExitedButton(views::View* view) {
1668 if (!tooltip_->IsVisible())
1669 tooltip_->StopTimer();
1672 base::string16 ShelfView::GetAccessibleName(const views::View* view) {
1673 int view_index = view_model_->GetIndexOfView(view);
1674 // May be -1 while in the process of animating closed.
1675 if (view_index == -1)
1676 return base::string16();
1678 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1679 model_->items()[view_index].id);
1680 return item_delegate->GetTitle();
1683 void ShelfView::ButtonPressed(views::Button* sender, const ui::Event& event) {
1684 // Do not handle mouse release during drag.
1685 if (dragging())
1686 return;
1688 if (sender == overflow_button_) {
1689 ToggleOverflowBubble();
1690 shelf_button_pressed_metric_tracker_.ButtonPressed(
1691 event, sender, ShelfItemDelegate::kNoAction);
1692 return;
1695 int view_index = view_model_->GetIndexOfView(sender);
1696 // May be -1 while in the process of animating closed.
1697 if (view_index == -1)
1698 return;
1700 // If the menu was just closed by the same event as this one, we ignore
1701 // the call and don't open the menu again. See crbug.com/343005 for more
1702 // detail.
1703 if (is_repost_event_)
1704 return;
1706 // Record the index for the last pressed shelf item.
1707 last_pressed_index_ = view_index;
1709 // Don't activate the item twice on double-click. Otherwise the window starts
1710 // animating open due to the first click, then immediately minimizes due to
1711 // the second click. The user most likely intended to open or minimize the
1712 // item once, not do both.
1713 if (event.flags() & ui::EF_IS_DOUBLE_CLICK)
1714 return;
1717 ScopedTargetRootWindow scoped_target(
1718 sender->GetWidget()->GetNativeView()->GetRootWindow());
1719 // Slow down activation animations if shift key is pressed.
1720 scoped_ptr<ui::ScopedAnimationDurationScaleMode> slowing_animations;
1721 if (event.IsShiftDown()) {
1722 slowing_animations.reset(new ui::ScopedAnimationDurationScaleMode(
1723 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION));
1726 // Collect usage statistics before we decide what to do with the click.
1727 switch (model_->items()[view_index].type) {
1728 case TYPE_APP_SHORTCUT:
1729 case TYPE_WINDOWED_APP:
1730 case TYPE_PLATFORM_APP:
1731 case TYPE_BROWSER_SHORTCUT:
1732 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1733 UMA_LAUNCHER_CLICK_ON_APP);
1734 break;
1736 case TYPE_APP_LIST:
1737 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1738 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON);
1739 break;
1741 case TYPE_APP_PANEL:
1742 case TYPE_DIALOG:
1743 break;
1745 case TYPE_UNDEFINED:
1746 NOTREACHED() << "ShelfItemType must be set.";
1747 break;
1750 ShelfItemDelegate::PerformedAction performed_action =
1751 item_manager_->GetShelfItemDelegate(model_->items()[view_index].id)
1752 ->ItemSelected(event);
1754 shelf_button_pressed_metric_tracker_.ButtonPressed(event, sender,
1755 performed_action);
1757 if (performed_action != ShelfItemDelegate::kNewWindowCreated)
1758 ShowListMenuForView(model_->items()[view_index], sender, event);
1762 bool ShelfView::ShowListMenuForView(const ShelfItem& item,
1763 views::View* source,
1764 const ui::Event& event) {
1765 ShelfItemDelegate* item_delegate =
1766 item_manager_->GetShelfItemDelegate(item.id);
1767 scoped_ptr<ui::MenuModel> list_menu_model(
1768 item_delegate->CreateApplicationMenu(event.flags()));
1770 // Make sure we have a menu and it has at least two items in addition to the
1771 // application title and the 3 spacing separators.
1772 if (!list_menu_model.get() || list_menu_model->GetItemCount() <= 5)
1773 return false;
1775 ShowMenu(list_menu_model.get(),
1776 source,
1777 gfx::Point(),
1778 false,
1779 ui::GetMenuSourceTypeForEvent(event));
1780 return true;
1783 void ShelfView::ShowContextMenuForView(views::View* source,
1784 const gfx::Point& point,
1785 ui::MenuSourceType source_type) {
1786 int view_index = view_model_->GetIndexOfView(source);
1787 if (view_index == -1) {
1788 Shell::GetInstance()->ShowContextMenu(point, source_type);
1789 return;
1792 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1793 model_->items()[view_index].id);
1794 context_menu_model_.reset(item_delegate->CreateContextMenu(
1795 source->GetWidget()->GetNativeView()->GetRootWindow()));
1796 if (!context_menu_model_)
1797 return;
1799 base::AutoReset<ShelfID> reseter(
1800 &context_menu_id_,
1801 view_index == -1 ? 0 : model_->items()[view_index].id);
1803 ShowMenu(context_menu_model_.get(),
1804 source,
1805 point,
1806 true,
1807 source_type);
1810 void ShelfView::ShowMenu(ui::MenuModel* menu_model,
1811 views::View* source,
1812 const gfx::Point& click_point,
1813 bool context_menu,
1814 ui::MenuSourceType source_type) {
1815 closing_event_time_ = base::TimeDelta();
1816 launcher_menu_runner_.reset(new views::MenuRunner(
1817 menu_model, context_menu ? views::MenuRunner::CONTEXT_MENU : 0));
1819 ScopedTargetRootWindow scoped_target(
1820 source->GetWidget()->GetNativeView()->GetRootWindow());
1822 // Determine the menu alignment dependent on the shelf.
1823 views::MenuAnchorPosition menu_alignment = views::MENU_ANCHOR_TOPLEFT;
1824 gfx::Rect anchor_point = gfx::Rect(click_point, gfx::Size());
1826 ShelfWidget* shelf = RootWindowController::ForShelf(
1827 GetWidget()->GetNativeView())->shelf();
1828 if (!context_menu) {
1829 // Application lists use a bubble.
1830 ShelfAlignment align = shelf->GetAlignment();
1831 anchor_point = source->GetBoundsInScreen();
1833 // It is possible to invoke the menu while it is sliding into view. To cover
1834 // that case, the screen coordinates are offsetted by the animation delta.
1835 gfx::Vector2d offset =
1836 source->GetWidget()->GetNativeWindow()->bounds().origin() -
1837 source->GetWidget()->GetNativeWindow()->GetTargetBounds().origin();
1838 anchor_point.set_x(anchor_point.x() - offset.x());
1839 anchor_point.set_y(anchor_point.y() - offset.y());
1841 // Shelf items can have an asymmetrical border for spacing reasons.
1842 // Adjust anchor location for this.
1843 if (source->border())
1844 anchor_point.Inset(source->border()->GetInsets());
1846 switch (align) {
1847 case SHELF_ALIGNMENT_BOTTOM:
1848 menu_alignment = views::MENU_ANCHOR_BUBBLE_ABOVE;
1849 break;
1850 case SHELF_ALIGNMENT_LEFT:
1851 menu_alignment = views::MENU_ANCHOR_BUBBLE_RIGHT;
1852 break;
1853 case SHELF_ALIGNMENT_RIGHT:
1854 menu_alignment = views::MENU_ANCHOR_BUBBLE_LEFT;
1855 break;
1856 case SHELF_ALIGNMENT_TOP:
1857 menu_alignment = views::MENU_ANCHOR_BUBBLE_BELOW;
1858 break;
1861 // If this gets deleted while we are in the menu, the shelf will be gone
1862 // as well.
1863 bool got_deleted = false;
1864 got_deleted_ = &got_deleted;
1866 shelf->ForceUndimming(true);
1867 // NOTE: if you convert to HAS_MNEMONICS be sure and update menu building
1868 // code.
1869 if (launcher_menu_runner_->RunMenuAt(source->GetWidget(),
1870 NULL,
1871 anchor_point,
1872 menu_alignment,
1873 source_type) ==
1874 views::MenuRunner::MENU_DELETED) {
1875 if (!got_deleted) {
1876 got_deleted_ = NULL;
1877 shelf->ForceUndimming(false);
1879 return;
1881 got_deleted_ = NULL;
1882 shelf->ForceUndimming(false);
1884 // If it is a context menu and we are showing overflow bubble
1885 // we want to hide overflow bubble.
1886 if (owner_overflow_bubble_)
1887 owner_overflow_bubble_->HideBubbleAndRefreshButton();
1889 // Unpinning an item will reset the |launcher_menu_runner_| before coming
1890 // here.
1891 if (launcher_menu_runner_)
1892 closing_event_time_ = launcher_menu_runner_->closing_event_time();
1893 Shell::GetInstance()->UpdateShelfVisibility();
1896 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) {
1897 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1898 OnShelfIconPositionsChanged());
1899 PreferredSizeChanged();
1902 void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
1903 if (snap_back_from_rip_off_view_ && animator == bounds_animator_) {
1904 if (!animator->IsAnimating(snap_back_from_rip_off_view_)) {
1905 // Coming here the animation of the ShelfButton is finished and the
1906 // previously hidden status can be shown again. Since the button itself
1907 // might have gone away or changed locations we check that the button
1908 // is still in the shelf and show its status again.
1909 for (int index = 0; index < view_model_->view_size(); index++) {
1910 views::View* view = view_model_->view_at(index);
1911 if (view == snap_back_from_rip_off_view_) {
1912 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1913 ShelfButton* button = static_cast<ShelfButton*>(view);
1914 button->ClearState(ShelfButton::STATE_HIDDEN);
1915 break;
1918 snap_back_from_rip_off_view_ = NULL;
1923 bool ShelfView::IsRepostEvent(const ui::Event& event) {
1924 if (closing_event_time_ == base::TimeDelta())
1925 return false;
1927 base::TimeDelta delta =
1928 base::TimeDelta(event.time_stamp() - closing_event_time_);
1929 closing_event_time_ = base::TimeDelta();
1930 // If the current (press down) event is a repost event, the time stamp of
1931 // these two events should be the same.
1932 return (delta.InMilliseconds() == 0);
1935 const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const {
1936 int view_index = view_model_->GetIndexOfView(view);
1937 if (view_index == -1)
1938 return NULL;
1939 return &(model_->items()[view_index]);
1942 bool ShelfView::ShouldShowTooltipForView(const views::View* view) const {
1943 if (view == GetAppListButtonView() &&
1944 Shell::GetInstance()->GetAppListWindow())
1945 return false;
1946 const ShelfItem* item = ShelfItemForView(view);
1947 if (!item)
1948 return true;
1949 ShelfItemDelegate* item_delegate =
1950 item_manager_->GetShelfItemDelegate(item->id);
1951 return item_delegate->ShouldShowTooltip();
1954 int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const {
1955 ShelfWidget* shelf = RootWindowController::ForShelf(
1956 GetWidget()->GetNativeView())->shelf();
1957 ShelfAlignment align = shelf->GetAlignment();
1958 const gfx::Rect bounds = GetBoundsInScreen();
1959 int distance = 0;
1960 switch (align) {
1961 case SHELF_ALIGNMENT_BOTTOM:
1962 distance = bounds.y() - coordinate.y();
1963 break;
1964 case SHELF_ALIGNMENT_LEFT:
1965 distance = coordinate.x() - bounds.right();
1966 break;
1967 case SHELF_ALIGNMENT_RIGHT:
1968 distance = bounds.x() - coordinate.x();
1969 break;
1970 case SHELF_ALIGNMENT_TOP:
1971 distance = coordinate.y() - bounds.bottom();
1972 break;
1974 return distance > 0 ? distance : 0;
1977 } // namespace ash