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"
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/alternate_app_list_button.h"
16 #include "ash/shelf/app_list_button.h"
17 #include "ash/shelf/overflow_bubble.h"
18 #include "ash/shelf/overflow_bubble_view.h"
19 #include "ash/shelf/overflow_button.h"
20 #include "ash/shelf/shelf_button.h"
21 #include "ash/shelf/shelf_constants.h"
22 #include "ash/shelf/shelf_delegate.h"
23 #include "ash/shelf/shelf_icon_observer.h"
24 #include "ash/shelf/shelf_item_delegate.h"
25 #include "ash/shelf/shelf_item_delegate_manager.h"
26 #include "ash/shelf/shelf_layout_manager.h"
27 #include "ash/shelf/shelf_menu_model.h"
28 #include "ash/shelf/shelf_model.h"
29 #include "ash/shelf/shelf_tooltip_manager.h"
30 #include "ash/shelf/shelf_widget.h"
31 #include "ash/shell.h"
32 #include "ash/wm/coordinate_conversion.h"
33 #include "base/auto_reset.h"
34 #include "base/memory/scoped_ptr.h"
35 #include "base/metrics/histogram.h"
36 #include "grit/ash_resources.h"
37 #include "grit/ash_strings.h"
38 #include "ui/aura/client/screen_position_client.h"
39 #include "ui/aura/root_window.h"
40 #include "ui/aura/window.h"
41 #include "ui/base/accessibility/accessible_view_state.h"
42 #include "ui/base/l10n/l10n_util.h"
43 #include "ui/base/models/simple_menu_model.h"
44 #include "ui/base/resource/resource_bundle.h"
45 #include "ui/compositor/layer.h"
46 #include "ui/compositor/layer_animator.h"
47 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
48 #include "ui/gfx/canvas.h"
49 #include "ui/gfx/point.h"
50 #include "ui/views/animation/bounds_animator.h"
51 #include "ui/views/border.h"
52 #include "ui/views/controls/button/image_button.h"
53 #include "ui/views/controls/menu/menu_model_adapter.h"
54 #include "ui/views/controls/menu/menu_runner.h"
55 #include "ui/views/focus/focus_search.h"
56 #include "ui/views/view_model.h"
57 #include "ui/views/view_model_utils.h"
58 #include "ui/views/widget/widget.h"
66 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM
= 0;
67 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT
= 1;
68 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT
= 2;
69 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT
= 3;
71 // Default amount content is inset on the left edge.
72 const int kDefaultLeadingInset
= 8;
74 // Minimum distance before drag starts.
75 const int kMinimumDragDistance
= 8;
77 // Size between the buttons.
78 const int kButtonSpacing
= 4;
79 const int kAlternateButtonSpacing
= 10;
81 // Size allocated to for each button.
82 const int kButtonSize
= 44;
84 // Additional spacing for the left and right side of icons.
85 const int kHorizontalIconSpacing
= 2;
87 // Inset for items which do not have an icon.
88 const int kHorizontalNoIconInsetSpacing
=
89 kHorizontalIconSpacing
+ kDefaultLeadingInset
;
91 // The proportion of the shelf space reserved for non-panel icons. Panels
92 // may flow into this space but will be put into the overflow bubble if there
93 // is contention for the space.
94 const float kReservedNonPanelIconProportion
= 0.67f
;
96 // This is the command id of the menu item which contains the name of the menu.
97 const int kCommandIdOfMenuName
= 0;
99 // The background color of the active item in the list.
100 const SkColor kActiveListItemBackgroundColor
= SkColorSetRGB(203 , 219, 241);
102 // The background color of the active & hovered item in the list.
103 const SkColor kFocusedActiveListItemBackgroundColor
=
104 SkColorSetRGB(193, 211, 236);
106 // The text color of the caption item in a list.
107 const SkColor kCaptionItemForegroundColor
= SK_ColorBLACK
;
109 // The maximum allowable length of a menu line of an application menu in pixels.
110 const int kMaximumAppMenuItemLength
= 350;
112 // The distance of the cursor from the outer rim of the shelf before it
114 const int kRipOffDistance
= 48;
116 // The rip off drag and drop proxy image should get scaled by this factor.
117 const float kDragAndDropProxyScale
= 1.5f
;
119 // The opacity represents that this partially disappeared item will get removed.
120 const float kDraggedImageOpacity
= 0.5f
;
124 // A class to temporarily disable a given bounds animator.
125 class BoundsAnimatorDisabler
{
127 BoundsAnimatorDisabler(views::BoundsAnimator
* bounds_animator
)
128 : old_duration_(bounds_animator
->GetAnimationDuration()),
129 bounds_animator_(bounds_animator
) {
130 bounds_animator_
->SetAnimationDuration(1);
133 ~BoundsAnimatorDisabler() {
134 bounds_animator_
->SetAnimationDuration(old_duration_
);
138 // The previous animation duration.
140 // The bounds animator which gets used.
141 views::BoundsAnimator
* bounds_animator_
;
143 DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorDisabler
);
146 // The MenuModelAdapter gets slightly changed to adapt the menu appearance to
148 class ShelfMenuModelAdapter
: public views::MenuModelAdapter
{
150 explicit ShelfMenuModelAdapter(ShelfMenuModel
* menu_model
);
152 // views::MenuModelAdapter:
153 virtual const gfx::FontList
* GetLabelFontList(int command_id
) const OVERRIDE
;
154 virtual bool IsCommandEnabled(int id
) const OVERRIDE
;
155 virtual void GetHorizontalIconMargins(int id
,
158 int* right_margin
) const OVERRIDE
;
159 virtual bool GetForegroundColor(int command_id
,
161 SkColor
* override_color
) const OVERRIDE
;
162 virtual bool GetBackgroundColor(int command_id
,
164 SkColor
* override_color
) const OVERRIDE
;
165 virtual int GetMaxWidthForMenu(views::MenuItemView
* menu
) OVERRIDE
;
166 virtual bool ShouldReserveSpaceForSubmenuIndicator() const OVERRIDE
;
169 ShelfMenuModel
* menu_model_
;
171 DISALLOW_COPY_AND_ASSIGN(ShelfMenuModelAdapter
);
174 ShelfMenuModelAdapter::ShelfMenuModelAdapter(ShelfMenuModel
* menu_model
)
175 : MenuModelAdapter(menu_model
),
176 menu_model_(menu_model
) {
179 const gfx::FontList
* ShelfMenuModelAdapter::GetLabelFontList(
180 int command_id
) const {
181 if (command_id
!= kCommandIdOfMenuName
)
182 return MenuModelAdapter::GetLabelFontList(command_id
);
184 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
185 return &rb
.GetFontList(ui::ResourceBundle::BoldFont
);
188 bool ShelfMenuModelAdapter::IsCommandEnabled(int id
) const {
189 return id
!= kCommandIdOfMenuName
;
192 bool ShelfMenuModelAdapter::GetForegroundColor(int command_id
,
194 SkColor
* override_color
) const {
195 if (command_id
!= kCommandIdOfMenuName
)
198 *override_color
= kCaptionItemForegroundColor
;
202 bool ShelfMenuModelAdapter::GetBackgroundColor(int command_id
,
204 SkColor
* override_color
) const {
205 if (!menu_model_
->IsCommandActive(command_id
))
208 *override_color
= is_hovered
? kFocusedActiveListItemBackgroundColor
:
209 kActiveListItemBackgroundColor
;
213 void ShelfMenuModelAdapter::GetHorizontalIconMargins(int command_id
,
216 int* right_margin
) const {
217 *left_margin
= kHorizontalIconSpacing
;
218 *right_margin
= (command_id
!= kCommandIdOfMenuName
) ?
219 kHorizontalIconSpacing
: -(icon_size
+ kHorizontalNoIconInsetSpacing
);
222 int ShelfMenuModelAdapter::GetMaxWidthForMenu(views::MenuItemView
* menu
) {
223 return kMaximumAppMenuItemLength
;
226 bool ShelfMenuModelAdapter::ShouldReserveSpaceForSubmenuIndicator() const {
230 // Custom FocusSearch used to navigate the shelf in the order items are in
232 class ShelfFocusSearch
: public views::FocusSearch
{
234 explicit ShelfFocusSearch(views::ViewModel
* view_model
)
235 : FocusSearch(NULL
, true, true),
236 view_model_(view_model
) {}
237 virtual ~ShelfFocusSearch() {}
239 // views::FocusSearch overrides:
240 virtual View
* FindNextFocusableView(
244 bool check_starting_view
,
245 views::FocusTraversable
** focus_traversable
,
246 View
** focus_traversable_view
) OVERRIDE
{
247 int index
= view_model_
->GetIndexOfView(starting_view
);
249 return view_model_
->view_at(0);
254 index
= view_model_
->view_size() - 1;
257 if (index
>= view_model_
->view_size())
260 return view_model_
->view_at(index
);
264 views::ViewModel
* view_model_
;
266 DISALLOW_COPY_AND_ASSIGN(ShelfFocusSearch
);
269 // AnimationDelegate used when inserting a new item. This steadily increases the
270 // opacity of the layer as the animation progress.
271 class FadeInAnimationDelegate
272 : public views::BoundsAnimator::OwnedAnimationDelegate
{
274 explicit FadeInAnimationDelegate(views::View
* view
) : view_(view
) {}
275 virtual ~FadeInAnimationDelegate() {}
277 // AnimationDelegate overrides:
278 virtual void AnimationProgressed(const Animation
* animation
) OVERRIDE
{
279 view_
->layer()->SetOpacity(animation
->GetCurrentValue());
280 view_
->layer()->ScheduleDraw();
282 virtual void AnimationEnded(const Animation
* animation
) OVERRIDE
{
283 view_
->layer()->SetOpacity(1.0f
);
284 view_
->layer()->ScheduleDraw();
286 virtual void AnimationCanceled(const Animation
* animation
) OVERRIDE
{
287 view_
->layer()->SetOpacity(1.0f
);
288 view_
->layer()->ScheduleDraw();
294 DISALLOW_COPY_AND_ASSIGN(FadeInAnimationDelegate
);
297 void ReflectItemStatus(const ShelfItem
& item
, ShelfButton
* button
) {
298 switch (item
.status
) {
300 button
->ClearState(ShelfButton::STATE_ACTIVE
);
301 button
->ClearState(ShelfButton::STATE_RUNNING
);
302 button
->ClearState(ShelfButton::STATE_ATTENTION
);
305 button
->ClearState(ShelfButton::STATE_ACTIVE
);
306 button
->AddState(ShelfButton::STATE_RUNNING
);
307 button
->ClearState(ShelfButton::STATE_ATTENTION
);
310 button
->AddState(ShelfButton::STATE_ACTIVE
);
311 button
->ClearState(ShelfButton::STATE_RUNNING
);
312 button
->ClearState(ShelfButton::STATE_ATTENTION
);
314 case STATUS_ATTENTION
:
315 button
->ClearState(ShelfButton::STATE_ACTIVE
);
316 button
->ClearState(ShelfButton::STATE_RUNNING
);
317 button
->AddState(ShelfButton::STATE_ATTENTION
);
324 // AnimationDelegate used when deleting an item. This steadily decreased the
325 // opacity of the layer as the animation progress.
326 class ShelfView::FadeOutAnimationDelegate
327 : public views::BoundsAnimator::OwnedAnimationDelegate
{
329 FadeOutAnimationDelegate(ShelfView
* host
, views::View
* view
)
332 virtual ~FadeOutAnimationDelegate() {}
334 // AnimationDelegate overrides:
335 virtual void AnimationProgressed(const Animation
* animation
) OVERRIDE
{
336 view_
->layer()->SetOpacity(1 - animation
->GetCurrentValue());
337 view_
->layer()->ScheduleDraw();
339 virtual void AnimationEnded(const Animation
* animation
) OVERRIDE
{
340 shelf_view_
->OnFadeOutAnimationEnded();
342 virtual void AnimationCanceled(const Animation
* animation
) OVERRIDE
{
346 ShelfView
* shelf_view_
;
347 scoped_ptr
<views::View
> view_
;
349 DISALLOW_COPY_AND_ASSIGN(FadeOutAnimationDelegate
);
352 // AnimationDelegate used to trigger fading an element in. When an item is
353 // inserted this delegate is attached to the animation that expands the size of
354 // the item. When done it kicks off another animation to fade the item in.
355 class ShelfView::StartFadeAnimationDelegate
356 : public views::BoundsAnimator::OwnedAnimationDelegate
{
358 StartFadeAnimationDelegate(ShelfView
* host
,
362 virtual ~StartFadeAnimationDelegate() {}
364 // AnimationDelegate overrides:
365 virtual void AnimationEnded(const Animation
* animation
) OVERRIDE
{
366 shelf_view_
->FadeIn(view_
);
368 virtual void AnimationCanceled(const Animation
* animation
) OVERRIDE
{
369 view_
->layer()->SetOpacity(1.0f
);
373 ShelfView
* shelf_view_
;
376 DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate
);
379 ShelfView::ShelfView(ShelfModel
* model
,
380 ShelfDelegate
* delegate
,
381 ShelfLayoutManager
* manager
)
384 view_model_(new views::ViewModel
),
385 first_visible_index_(0),
386 last_visible_index_(-1),
387 overflow_button_(NULL
),
388 owner_overflow_bubble_(NULL
),
392 start_drag_index_(-1),
394 leading_inset_(kDefaultLeadingInset
),
395 cancelling_drag_model_changed_(false),
396 last_hidden_index_(0),
397 closing_event_time_(base::TimeDelta()),
399 drag_and_drop_item_pinned_(false),
400 drag_and_drop_shelf_id_(0),
401 dragged_off_shelf_(false),
402 snap_back_from_rip_off_view_(NULL
),
403 item_manager_(Shell::GetInstance()->shelf_item_delegate_manager()),
404 layout_manager_(manager
),
405 overflow_mode_(false),
407 dragged_off_from_overflow_to_shelf_(false) {
409 bounds_animator_
.reset(new views::BoundsAnimator(this));
410 bounds_animator_
->AddObserver(this);
411 set_context_menu_controller(this);
412 focus_search_
.reset(new ShelfFocusSearch(view_model_
.get()));
413 tooltip_
.reset(new ShelfTooltipManager(manager
, this));
416 ShelfView::~ShelfView() {
417 bounds_animator_
->RemoveObserver(this);
418 model_
->RemoveObserver(this);
419 // If we are inside the MenuRunner, we need to know if we were getting
420 // deleted while it was running.
422 *got_deleted_
= true;
425 void ShelfView::Init() {
426 model_
->AddObserver(this);
428 const ShelfItems
& items(model_
->items());
429 for (ShelfItems::const_iterator i
= items
.begin(); i
!= items
.end(); ++i
) {
430 views::View
* child
= CreateViewForItem(*i
);
431 child
->SetPaintToLayer(true);
432 view_model_
->Add(child
, static_cast<int>(i
- items
.begin()));
435 ShelfStatusChanged();
436 overflow_button_
= new OverflowButton(this);
437 overflow_button_
->set_context_menu_controller(this);
438 ConfigureChildView(overflow_button_
);
439 AddChildView(overflow_button_
);
440 UpdateFirstButtonPadding();
442 // We'll layout when our bounds change.
445 void ShelfView::OnShelfAlignmentChanged() {
446 UpdateFirstButtonPadding();
447 overflow_button_
->OnShelfAlignmentChanged();
448 LayoutToIdealBounds();
449 for (int i
=0; i
< view_model_
->view_size(); ++i
) {
450 // TODO: remove when AppIcon is a Shelf Button.
451 if (TYPE_APP_LIST
== model_
->items()[i
].type
&&
452 !ash::switches::UseAlternateShelfLayout()) {
453 static_cast<AppListButton
*>(view_model_
->view_at(i
))->SetImageAlignment(
454 layout_manager_
->SelectValueForShelfAlignment(
455 views::ImageButton::ALIGN_CENTER
,
456 views::ImageButton::ALIGN_LEFT
,
457 views::ImageButton::ALIGN_RIGHT
,
458 views::ImageButton::ALIGN_CENTER
),
459 layout_manager_
->SelectValueForShelfAlignment(
460 views::ImageButton::ALIGN_TOP
,
461 views::ImageButton::ALIGN_MIDDLE
,
462 views::ImageButton::ALIGN_MIDDLE
,
463 views::ImageButton::ALIGN_BOTTOM
));
465 if (i
>= first_visible_index_
&& i
<= last_visible_index_
)
466 view_model_
->view_at(i
)->Layout();
469 if (overflow_bubble_
)
470 overflow_bubble_
->Hide();
473 void ShelfView::SchedulePaintForAllButtons() {
474 for (int i
= 0; i
< view_model_
->view_size(); ++i
) {
475 if (i
>= first_visible_index_
&& i
<= last_visible_index_
)
476 view_model_
->view_at(i
)->SchedulePaint();
478 if (overflow_button_
&& overflow_button_
->visible())
479 overflow_button_
->SchedulePaint();
482 gfx::Rect
ShelfView::GetIdealBoundsOfItemIcon(ShelfID id
) {
483 int index
= model_
->ItemIndexByID(id
);
484 if (index
== -1 || (index
> last_visible_index_
&&
485 index
< model_
->FirstPanelIndex()))
487 const gfx::Rect
& ideal_bounds(view_model_
->ideal_bounds(index
));
488 DCHECK_NE(TYPE_APP_LIST
, model_
->items()[index
].type
);
489 ShelfButton
* button
= static_cast<ShelfButton
*>(view_model_
->view_at(index
));
490 gfx::Rect icon_bounds
= button
->GetIconBounds();
491 return gfx::Rect(GetMirroredXWithWidthInView(
492 ideal_bounds
.x() + icon_bounds
.x(), icon_bounds
.width()),
493 ideal_bounds
.y() + icon_bounds
.y(),
495 icon_bounds
.height());
498 void ShelfView::UpdatePanelIconPosition(ShelfID id
,
499 const gfx::Point
& midpoint
) {
500 int current_index
= model_
->ItemIndexByID(id
);
501 int first_panel_index
= model_
->FirstPanelIndex();
502 if (current_index
< first_panel_index
)
505 gfx::Point
midpoint_in_view(GetMirroredXInView(midpoint
.x()),
507 int target_index
= current_index
;
508 while (target_index
> first_panel_index
&&
509 layout_manager_
->PrimaryAxisValue(
510 view_model_
->ideal_bounds(target_index
).x(),
511 view_model_
->ideal_bounds(target_index
).y()) >
512 layout_manager_
->PrimaryAxisValue(midpoint_in_view
.x(),
513 midpoint_in_view
.y())) {
516 while (target_index
< view_model_
->view_size() - 1 &&
517 layout_manager_
->PrimaryAxisValue(
518 view_model_
->ideal_bounds(target_index
).right(),
519 view_model_
->ideal_bounds(target_index
).bottom()) <
520 layout_manager_
->PrimaryAxisValue(midpoint_in_view
.x(),
521 midpoint_in_view
.y())) {
524 if (current_index
!= target_index
)
525 model_
->Move(current_index
, target_index
);
528 bool ShelfView::IsShowingMenu() const {
529 return (launcher_menu_runner_
.get() &&
530 launcher_menu_runner_
->IsRunning());
533 bool ShelfView::IsShowingOverflowBubble() const {
534 return overflow_bubble_
.get() && overflow_bubble_
->IsShowing();
537 views::View
* ShelfView::GetAppListButtonView() const {
538 for (int i
= 0; i
< model_
->item_count(); ++i
) {
539 if (model_
->items()[i
].type
== TYPE_APP_LIST
)
540 return view_model_
->view_at(i
);
543 NOTREACHED() << "Applist button not found";
547 ////////////////////////////////////////////////////////////////////////////////
548 // ShelfView, FocusTraversable implementation:
550 views::FocusSearch
* ShelfView::GetFocusSearch() {
551 return focus_search_
.get();
554 views::FocusTraversable
* ShelfView::GetFocusTraversableParent() {
555 return parent()->GetFocusTraversable();
558 View
* ShelfView::GetFocusTraversableParentView() {
562 void ShelfView::CreateDragIconProxy(
563 const gfx::Point
& location_in_screen_coordinates
,
564 const gfx::ImageSkia
& icon
,
565 views::View
* replaced_view
,
566 const gfx::Vector2d
& cursor_offset_from_center
,
567 float scale_factor
) {
568 drag_replaced_view_
= replaced_view
;
569 drag_image_
.reset(new ash::internal::DragImageView(
570 drag_replaced_view_
->GetWidget()->GetNativeWindow()->GetRootWindow(),
571 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE
));
572 drag_image_
->SetImage(icon
);
573 gfx::Size size
= drag_image_
->GetPreferredSize();
574 size
.set_width(size
.width() * scale_factor
);
575 size
.set_height(size
.height() * scale_factor
);
576 drag_image_offset_
= gfx::Vector2d(size
.width() / 2, size
.height() / 2) +
577 cursor_offset_from_center
;
578 gfx::Rect
drag_image_bounds(
579 location_in_screen_coordinates
- drag_image_offset_
,
581 drag_image_
->SetBoundsInScreen(drag_image_bounds
);
582 drag_image_
->SetWidgetVisible(true);
585 void ShelfView::UpdateDragIconProxy(
586 const gfx::Point
& location_in_screen_coordinates
) {
587 drag_image_
->SetScreenPosition(
588 location_in_screen_coordinates
- drag_image_offset_
);
591 void ShelfView::DestroyDragIconProxy() {
593 drag_image_offset_
= gfx::Vector2d(0, 0);
596 bool ShelfView::StartDrag(const std::string
& app_id
,
597 const gfx::Point
& location_in_screen_coordinates
) {
598 // Bail if an operation is already going on - or the cursor is not inside.
599 // This could happen if mouse / touch operations overlap.
600 if (drag_and_drop_shelf_id_
||
601 !GetBoundsInScreen().Contains(location_in_screen_coordinates
))
604 // If the AppsGridView (which was dispatching this event) was opened by our
605 // button, ShelfView dragging operations are locked and we have to unlock.
607 drag_and_drop_item_pinned_
= false;
608 drag_and_drop_app_id_
= app_id
;
609 drag_and_drop_shelf_id_
=
610 delegate_
->GetShelfIDForAppID(drag_and_drop_app_id_
);
611 // Check if the application is known and pinned - if not, we have to pin it so
612 // that we can re-arrange the shelf order accordingly. Note that items have
613 // to be pinned to give them the same (order) possibilities as a shortcut.
614 // When an item is dragged from overflow to shelf, IsShowingOverflowBubble()
615 // returns true. At this time, we don't need to pin the item.
616 if (!IsShowingOverflowBubble() &&
617 (!drag_and_drop_shelf_id_
||
618 !delegate_
->IsAppPinned(app_id
))) {
619 delegate_
->PinAppWithID(app_id
);
620 drag_and_drop_shelf_id_
=
621 delegate_
->GetShelfIDForAppID(drag_and_drop_app_id_
);
622 if (!drag_and_drop_shelf_id_
)
624 drag_and_drop_item_pinned_
= true;
626 views::View
* drag_and_drop_view
= view_model_
->view_at(
627 model_
->ItemIndexByID(drag_and_drop_shelf_id_
));
628 DCHECK(drag_and_drop_view
);
630 // Since there is already an icon presented by the caller, we hide this item
631 // for now. That has to be done by reducing the size since the visibility will
632 // change once a regrouping animation is performed.
633 pre_drag_and_drop_size_
= drag_and_drop_view
->size();
634 drag_and_drop_view
->SetSize(gfx::Size());
636 // First we have to center the mouse cursor over the item.
637 gfx::Point pt
= drag_and_drop_view
->GetBoundsInScreen().CenterPoint();
638 views::View::ConvertPointFromScreen(drag_and_drop_view
, &pt
);
639 gfx::Point point_in_root
= location_in_screen_coordinates
;
640 ash::wm::ConvertPointFromScreen(
641 ash::wm::GetRootWindowAt(location_in_screen_coordinates
),
643 ui::MouseEvent
event(ui::ET_MOUSE_PRESSED
, pt
, point_in_root
, 0, 0);
644 PointerPressedOnButton(drag_and_drop_view
,
645 ShelfButtonHost::DRAG_AND_DROP
,
648 // Drag the item where it really belongs.
649 Drag(location_in_screen_coordinates
);
653 bool ShelfView::Drag(const gfx::Point
& location_in_screen_coordinates
) {
654 if (!drag_and_drop_shelf_id_
||
655 !GetBoundsInScreen().Contains(location_in_screen_coordinates
))
658 gfx::Point pt
= location_in_screen_coordinates
;
659 views::View
* drag_and_drop_view
= view_model_
->view_at(
660 model_
->ItemIndexByID(drag_and_drop_shelf_id_
));
661 ConvertPointFromScreen(drag_and_drop_view
, &pt
);
662 gfx::Point point_in_root
= location_in_screen_coordinates
;
663 ash::wm::ConvertPointFromScreen(
664 ash::wm::GetRootWindowAt(location_in_screen_coordinates
),
666 ui::MouseEvent
event(ui::ET_MOUSE_DRAGGED
, pt
, point_in_root
, 0, 0);
667 PointerDraggedOnButton(drag_and_drop_view
,
668 ShelfButtonHost::DRAG_AND_DROP
,
673 void ShelfView::EndDrag(bool cancel
) {
674 if (!drag_and_drop_shelf_id_
)
677 views::View
* drag_and_drop_view
= view_model_
->view_at(
678 model_
->ItemIndexByID(drag_and_drop_shelf_id_
));
679 PointerReleasedOnButton(
680 drag_and_drop_view
, ShelfButtonHost::DRAG_AND_DROP
, cancel
);
682 // Either destroy the temporarily created item - or - make the item visible.
683 if (drag_and_drop_item_pinned_
&& cancel
)
684 delegate_
->UnpinAppWithID(drag_and_drop_app_id_
);
685 else if (drag_and_drop_view
) {
687 // When a hosted drag gets canceled, the item can remain in the same slot
688 // and it might have moved within the bounds. In that case the item need
689 // to animate back to its correct location.
690 AnimateToIdealBounds();
692 drag_and_drop_view
->SetSize(pre_drag_and_drop_size_
);
696 drag_and_drop_shelf_id_
= 0;
699 void ShelfView::LayoutToIdealBounds() {
700 if (bounds_animator_
->IsAnimating()) {
701 AnimateToIdealBounds();
705 IdealBounds ideal_bounds
;
706 CalculateIdealBounds(&ideal_bounds
);
707 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_
);
708 overflow_button_
->SetBoundsRect(ideal_bounds
.overflow_bounds
);
711 void ShelfView::UpdateAllButtonsVisibilityInOverflowMode() {
712 // The overflow button is not shown in overflow mode.
713 overflow_button_
->SetVisible(false);
714 int last_button_index
= model_
->FirstPanelIndex() - 1;
715 DCHECK_LT(last_visible_index_
, view_model_
->view_size());
716 for (int i
= 0; i
< view_model_
->view_size(); ++i
) {
717 bool visible
= i
>= first_visible_index_
&&
718 i
<= last_visible_index_
;
719 if (!ash::switches::UseAlternateShelfLayout())
720 visible
&= i
!= last_button_index
;
722 // To track the dragging of |drag_view_| continuously, its visibility
723 // should be always true regardless of its position.
724 if (dragged_off_from_overflow_to_shelf_
&&
725 view_model_
->view_at(i
) == drag_view_
)
726 view_model_
->view_at(i
)->SetVisible(true);
728 view_model_
->view_at(i
)->SetVisible(visible
);
732 void ShelfView::CalculateIdealBounds(IdealBounds
* bounds
) {
733 int available_size
= layout_manager_
->PrimaryAxisValue(width(), height());
734 DCHECK(model_
->item_count() == view_model_
->view_size());
738 int first_panel_index
= model_
->FirstPanelIndex();
739 int last_button_index
= first_panel_index
- 1;
741 // Initial x,y values account both leading_inset in primary
742 // coordinate and secondary coordinate based on the dynamic edge of the
743 // shelf (eg top edge on bottom-aligned shelf).
744 int inset
= ash::switches::UseAlternateShelfLayout() ? 0 : leading_inset_
;
745 int x
= layout_manager_
->SelectValueForShelfAlignment(inset
, 0, 0, inset
);
746 int y
= layout_manager_
->SelectValueForShelfAlignment(0, inset
, inset
, 0);
748 int button_size
= GetButtonSize();
749 int button_spacing
= GetButtonSpacing();
751 int w
= layout_manager_
->PrimaryAxisValue(button_size
, width());
752 int h
= layout_manager_
->PrimaryAxisValue(height(), button_size
);
753 for (int i
= 0; i
< view_model_
->view_size(); ++i
) {
754 if (i
< first_visible_index_
) {
755 view_model_
->set_ideal_bounds(i
, gfx::Rect(x
, y
, 0, 0));
759 view_model_
->set_ideal_bounds(i
, gfx::Rect(x
, y
, w
, h
));
760 if (i
!= last_button_index
) {
761 x
= layout_manager_
->PrimaryAxisValue(x
+ w
+ button_spacing
, x
);
762 y
= layout_manager_
->PrimaryAxisValue(y
, y
+ h
+ button_spacing
);
766 if (is_overflow_mode()) {
767 UpdateAllButtonsVisibilityInOverflowMode();
771 // To address Fitt's law, we make the first shelf button include the
772 // leading inset (if there is one).
773 if (!ash::switches::UseAlternateShelfLayout()) {
774 if (view_model_
->view_size() > 0) {
775 view_model_
->set_ideal_bounds(0, gfx::Rect(gfx::Size(
776 layout_manager_
->PrimaryAxisValue(inset
+ w
, w
),
777 layout_manager_
->PrimaryAxisValue(h
, inset
+ h
))));
781 // Right aligned icons.
782 int end_position
= available_size
- button_spacing
;
783 x
= layout_manager_
->PrimaryAxisValue(end_position
, 0);
784 y
= layout_manager_
->PrimaryAxisValue(0, end_position
);
785 for (int i
= view_model_
->view_size() - 1;
786 i
>= first_panel_index
; --i
) {
787 x
= layout_manager_
->PrimaryAxisValue(x
- w
- button_spacing
, x
);
788 y
= layout_manager_
->PrimaryAxisValue(y
, y
- h
- button_spacing
);
789 view_model_
->set_ideal_bounds(i
, gfx::Rect(x
, y
, w
, h
));
790 end_position
= layout_manager_
->PrimaryAxisValue(x
, y
);
793 // Icons on the left / top are guaranteed up to kLeftIconProportion of
794 // the available space.
795 int last_icon_position
= layout_manager_
->PrimaryAxisValue(
796 view_model_
->ideal_bounds(last_button_index
).right(),
797 view_model_
->ideal_bounds(last_button_index
).bottom())
798 + button_size
+ inset
;
799 if (!ash::switches::UseAlternateShelfLayout())
800 last_icon_position
+= button_size
;
801 int reserved_icon_space
= available_size
* kReservedNonPanelIconProportion
;
802 if (last_icon_position
< reserved_icon_space
)
803 end_position
= last_icon_position
;
805 end_position
= std::max(end_position
, reserved_icon_space
);
807 bounds
->overflow_bounds
.set_size(
808 gfx::Size(layout_manager_
->PrimaryAxisValue(w
, width()),
809 layout_manager_
->PrimaryAxisValue(height(), h
)));
811 if (ash::switches::UseAlternateShelfLayout()) {
812 last_visible_index_
= DetermineLastVisibleIndex(
813 end_position
- button_size
);
815 last_visible_index_
= DetermineLastVisibleIndex(
816 end_position
- inset
- 2 * button_size
);
818 last_hidden_index_
= DetermineFirstVisiblePanelIndex(end_position
) - 1;
820 ((ash::switches::UseAlternateShelfLayout() ? 0 : 1) +
821 last_visible_index_
< last_button_index
||
822 last_hidden_index_
>= first_panel_index
);
824 // Create Space for the overflow button
825 if (show_overflow
&& ash::switches::UseAlternateShelfLayout() &&
826 last_visible_index_
> 0 && last_visible_index_
< last_button_index
)
827 --last_visible_index_
;
828 for (int i
= 0; i
< view_model_
->view_size(); ++i
) {
829 bool visible
= i
<= last_visible_index_
|| i
> last_hidden_index_
;
830 // Always show the app list.
831 if (!ash::switches::UseAlternateShelfLayout())
832 visible
|= (i
== last_button_index
);
833 // To receive drag event continously from |drag_view_| during the dragging
834 // off from the shelf, don't make |drag_view_| invisible. It will be
835 // eventually invisible and removed from the |view_model_| by
836 // FinalizeRipOffDrag().
837 if (dragged_off_shelf_
&& view_model_
->view_at(i
) == drag_view_
)
839 view_model_
->view_at(i
)->SetVisible(visible
);
842 overflow_button_
->SetVisible(show_overflow
);
844 DCHECK_NE(0, view_model_
->view_size());
845 if (last_visible_index_
== -1) {
846 x
= layout_manager_
->SelectValueForShelfAlignment(inset
, 0, 0, inset
);
847 y
= layout_manager_
->SelectValueForShelfAlignment(0, inset
, inset
, 0);
848 } else if (last_visible_index_
== last_button_index
849 && !ash::switches::UseAlternateShelfLayout()) {
850 x
= view_model_
->ideal_bounds(last_visible_index_
).x();
851 y
= view_model_
->ideal_bounds(last_visible_index_
).y();
853 x
= layout_manager_
->PrimaryAxisValue(
854 view_model_
->ideal_bounds(last_visible_index_
).right(),
855 view_model_
->ideal_bounds(last_visible_index_
).x());
856 y
= layout_manager_
->PrimaryAxisValue(
857 view_model_
->ideal_bounds(last_visible_index_
).y(),
858 view_model_
->ideal_bounds(last_visible_index_
).bottom());
860 // Set all hidden panel icon positions to be on the overflow button.
861 for (int i
= first_panel_index
; i
<= last_hidden_index_
; ++i
)
862 view_model_
->set_ideal_bounds(i
, gfx::Rect(x
, y
, w
, h
));
864 // Add more space between last visible item and overflow button.
865 // Without this, two buttons look too close compared with other items.
866 if (ash::switches::UseAlternateShelfLayout()) {
867 x
= layout_manager_
->PrimaryAxisValue(x
+ button_spacing
, x
);
868 y
= layout_manager_
->PrimaryAxisValue(y
, y
+ button_spacing
);
871 bounds
->overflow_bounds
.set_x(x
);
872 bounds
->overflow_bounds
.set_y(y
);
873 if (!ash::switches::UseAlternateShelfLayout()) {
874 // Position app list after overflow button.
875 gfx::Rect app_list_bounds
= view_model_
->ideal_bounds(last_button_index
);
877 x
= layout_manager_
->PrimaryAxisValue(x
+ w
+ button_spacing
, x
);
878 y
= layout_manager_
->PrimaryAxisValue(y
, y
+ h
+ button_spacing
);
879 app_list_bounds
.set_x(x
);
880 app_list_bounds
.set_y(y
);
881 view_model_
->set_ideal_bounds(last_button_index
, app_list_bounds
);
883 if (overflow_bubble_
.get() && overflow_bubble_
->IsShowing())
884 UpdateOverflowRange(overflow_bubble_
->shelf_view());
886 if (overflow_bubble_
)
887 overflow_bubble_
->Hide();
891 int ShelfView::DetermineLastVisibleIndex(int max_value
) const {
892 int index
= model_
->FirstPanelIndex() - 1;
894 layout_manager_
->PrimaryAxisValue(
895 view_model_
->ideal_bounds(index
).right(),
896 view_model_
->ideal_bounds(index
).bottom()) > max_value
) {
902 int ShelfView::DetermineFirstVisiblePanelIndex(int min_value
) const {
903 int index
= model_
->FirstPanelIndex();
904 while (index
< view_model_
->view_size() &&
905 layout_manager_
->PrimaryAxisValue(
906 view_model_
->ideal_bounds(index
).right(),
907 view_model_
->ideal_bounds(index
).bottom()) < min_value
) {
913 void ShelfView::AddIconObserver(ShelfIconObserver
* observer
) {
914 observers_
.AddObserver(observer
);
917 void ShelfView::RemoveIconObserver(ShelfIconObserver
* observer
) {
918 observers_
.RemoveObserver(observer
);
921 void ShelfView::AnimateToIdealBounds() {
922 IdealBounds ideal_bounds
;
923 CalculateIdealBounds(&ideal_bounds
);
924 for (int i
= 0; i
< view_model_
->view_size(); ++i
) {
925 View
* view
= view_model_
->view_at(i
);
926 bounds_animator_
->AnimateViewTo(view
, view_model_
->ideal_bounds(i
));
927 // Now that the item animation starts, we have to make sure that the
928 // padding of the first gets properly transferred to the new first item.
929 if (i
&& view
->border())
930 view
->SetBorder(views::Border::NullBorder());
931 else if (!i
&& !view
->border())
932 UpdateFirstButtonPadding();
934 overflow_button_
->SetBoundsRect(ideal_bounds
.overflow_bounds
);
937 views::View
* ShelfView::CreateViewForItem(const ShelfItem
& item
) {
938 views::View
* view
= NULL
;
940 case TYPE_BROWSER_SHORTCUT
:
941 case TYPE_APP_SHORTCUT
:
942 case TYPE_WINDOWED_APP
:
943 case TYPE_PLATFORM_APP
:
945 case TYPE_APP_PANEL
: {
946 ShelfButton
* button
= ShelfButton::Create(this, this, layout_manager_
);
947 button
->SetImage(item
.image
);
948 ReflectItemStatus(item
, button
);
953 case TYPE_APP_LIST
: {
954 if (ash::switches::UseAlternateShelfLayout()) {
955 view
= new AlternateAppListButton(this,
957 layout_manager_
->shelf_widget());
959 // TODO(dave): turn this into a ShelfButton too.
960 AppListButton
* button
= new AppListButton(this, this);
961 button
->SetImageAlignment(
962 layout_manager_
->SelectValueForShelfAlignment(
963 views::ImageButton::ALIGN_CENTER
,
964 views::ImageButton::ALIGN_LEFT
,
965 views::ImageButton::ALIGN_RIGHT
,
966 views::ImageButton::ALIGN_CENTER
),
967 layout_manager_
->SelectValueForShelfAlignment(
968 views::ImageButton::ALIGN_TOP
,
969 views::ImageButton::ALIGN_MIDDLE
,
970 views::ImageButton::ALIGN_MIDDLE
,
971 views::ImageButton::ALIGN_BOTTOM
));
980 view
->set_context_menu_controller(this);
983 ConfigureChildView(view
);
987 void ShelfView::FadeIn(views::View
* view
) {
988 view
->SetVisible(true);
989 view
->layer()->SetOpacity(0);
990 AnimateToIdealBounds();
991 bounds_animator_
->SetAnimationDelegate(
992 view
, new FadeInAnimationDelegate(view
), true);
995 void ShelfView::PrepareForDrag(Pointer pointer
, const ui::LocatedEvent
& event
) {
998 drag_pointer_
= pointer
;
999 start_drag_index_
= view_model_
->GetIndexOfView(drag_view_
);
1001 if (start_drag_index_
== -1) {
1006 // If the item is no longer draggable, bail out.
1007 ShelfItemDelegate
* item_delegate
= item_manager_
->GetShelfItemDelegate(
1008 model_
->items()[start_drag_index_
].id
);
1009 if (!item_delegate
->IsDraggable()) {
1014 // Move the view to the front so that it appears on top of other views.
1015 ReorderChildView(drag_view_
, -1);
1016 bounds_animator_
->StopAnimatingView(drag_view_
);
1019 void ShelfView::ContinueDrag(const ui::LocatedEvent
& event
) {
1020 // Due to a syncing operation the application might have been removed.
1021 // Bail if it is gone.
1022 int current_index
= view_model_
->GetIndexOfView(drag_view_
);
1023 DCHECK_NE(-1, current_index
);
1025 ShelfItemDelegate
* item_delegate
= item_manager_
->GetShelfItemDelegate(
1026 model_
->items()[current_index
].id
);
1027 if (!item_delegate
->IsDraggable()) {
1032 // If this is not a drag and drop host operation and not the app list item,
1033 // check if the item got ripped off the shelf - if it did we are done.
1034 if (!drag_and_drop_shelf_id_
&& ash::switches::UseDragOffShelf() &&
1035 RemovableByRipOff(current_index
) != NOT_REMOVABLE
) {
1036 if (HandleRipOffDrag(event
))
1038 // The rip off handler could have changed the location of the item.
1039 current_index
= view_model_
->GetIndexOfView(drag_view_
);
1042 // TODO: I don't think this works correctly with RTL.
1043 gfx::Point
drag_point(event
.location());
1044 ConvertPointToTarget(drag_view_
, this, &drag_point
);
1046 // Constrain the location to the range of valid indices for the type.
1047 std::pair
<int, int> indices(GetDragRange(current_index
));
1048 int first_drag_index
= indices
.first
;
1049 int last_drag_index
= indices
.second
;
1050 // If the last index isn't valid, we're overflowing. Constrain to the app list
1051 // (which is the last visible item).
1052 if (first_drag_index
< model_
->FirstPanelIndex() &&
1053 last_drag_index
> last_visible_index_
)
1054 last_drag_index
= last_visible_index_
;
1056 if (layout_manager_
->IsHorizontalAlignment()) {
1057 x
= std::max(view_model_
->ideal_bounds(indices
.first
).x(),
1058 drag_point
.x() - drag_offset_
);
1059 x
= std::min(view_model_
->ideal_bounds(last_drag_index
).right() -
1060 view_model_
->ideal_bounds(current_index
).width(),
1062 if (drag_view_
->x() == x
)
1064 drag_view_
->SetX(x
);
1066 y
= std::max(view_model_
->ideal_bounds(indices
.first
).y(),
1067 drag_point
.y() - drag_offset_
);
1068 y
= std::min(view_model_
->ideal_bounds(last_drag_index
).bottom() -
1069 view_model_
->ideal_bounds(current_index
).height(),
1071 if (drag_view_
->y() == y
)
1073 drag_view_
->SetY(y
);
1077 views::ViewModelUtils::DetermineMoveIndex(
1078 *view_model_
, drag_view_
,
1079 layout_manager_
->IsHorizontalAlignment() ?
1080 views::ViewModelUtils::HORIZONTAL
:
1081 views::ViewModelUtils::VERTICAL
,
1084 std::min(indices
.second
, std::max(target_index
, indices
.first
));
1085 if (target_index
== current_index
)
1088 // Change the model, the ShelfItemMoved() callback will handle the
1089 // |view_model_| update.
1090 model_
->Move(current_index
, target_index
);
1091 bounds_animator_
->StopAnimatingView(drag_view_
);
1094 bool ShelfView::HandleRipOffDrag(const ui::LocatedEvent
& event
) {
1095 int current_index
= view_model_
->GetIndexOfView(drag_view_
);
1096 DCHECK_NE(-1, current_index
);
1097 std::string dragged_app_id
=
1098 delegate_
->GetAppIDForShelfID(model_
->items()[current_index
].id
);
1100 gfx::Point screen_location
= event
.root_location();
1101 ash::wm::ConvertPointToScreen(GetWidget()->GetNativeWindow()->GetRootWindow(),
1104 // To avoid ugly forwards and backwards flipping we use different constants
1105 // for ripping off / re-inserting the items.
1106 if (dragged_off_shelf_
) {
1107 // If the shelf/overflow bubble bounds contains |screen_location| we insert
1108 // the item back into the shelf.
1109 if (GetBoundsForDragInsertInScreen().Contains(screen_location
)) {
1110 if (dragged_off_from_overflow_to_shelf_
) {
1111 // During the dragging an item from Shelf to Overflow, it can enter here
1112 // directly because both are located very closly.
1113 main_shelf_
->EndDrag(true);
1114 // Stops the animation of |drag_view_| and sets its bounds explicitly
1115 // becase ContinueDrag() stops its animation. Without this, unexpected
1116 // bounds will be set.
1117 bounds_animator_
->StopAnimatingView(drag_view_
);
1118 int drag_view_index
= view_model_
->GetIndexOfView(drag_view_
);
1119 drag_view_
->SetBoundsRect(view_model_
->ideal_bounds(drag_view_index
));
1120 dragged_off_from_overflow_to_shelf_
= false;
1122 // Destroy our proxy view item.
1123 DestroyDragIconProxy();
1124 // Re-insert the item and return simply false since the caller will handle
1125 // the move as in any normal case.
1126 dragged_off_shelf_
= false;
1127 drag_view_
->layer()->SetOpacity(1.0f
);
1128 // The size of Overflow bubble should be updated immediately when an item
1130 if (is_overflow_mode())
1131 PreferredSizeChanged();
1133 } else if (is_overflow_mode() &&
1134 main_shelf_
->GetBoundsForDragInsertInScreen().Contains(
1136 if (!dragged_off_from_overflow_to_shelf_
) {
1137 dragged_off_from_overflow_to_shelf_
= true;
1138 drag_image_
->SetOpacity(1.0f
);
1139 main_shelf_
->StartDrag(dragged_app_id
, screen_location
);
1141 main_shelf_
->Drag(screen_location
);
1143 } else if (dragged_off_from_overflow_to_shelf_
) {
1144 // Makes the |drag_image_| partially disappear again.
1145 dragged_off_from_overflow_to_shelf_
= false;
1146 drag_image_
->SetOpacity(kDraggedImageOpacity
);
1147 main_shelf_
->EndDrag(true);
1148 bounds_animator_
->StopAnimatingView(drag_view_
);
1149 int drag_view_index
= view_model_
->GetIndexOfView(drag_view_
);
1150 drag_view_
->SetBoundsRect(view_model_
->ideal_bounds(drag_view_index
));
1152 // Move our proxy view item.
1153 UpdateDragIconProxy(screen_location
);
1156 // Check if we are too far away from the shelf to enter the ripped off state.
1157 // Determine the distance to the shelf.
1158 int delta
= CalculateShelfDistance(screen_location
);
1159 if (delta
> kRipOffDistance
) {
1160 // Create a proxy view item which can be moved anywhere.
1161 ShelfButton
* button
= static_cast<ShelfButton
*>(drag_view_
);
1162 CreateDragIconProxy(event
.root_location(),
1165 gfx::Vector2d(0, 0),
1166 kDragAndDropProxyScale
);
1167 drag_view_
->layer()->SetOpacity(0.0f
);
1168 dragged_off_shelf_
= true;
1169 if (RemovableByRipOff(current_index
) == REMOVABLE
) {
1170 // Move the item to the front of the first panel item and hide it.
1171 // ShelfItemMoved() callback will handle the |view_model_| update and
1172 // call AnimateToIdealBounds().
1173 if (current_index
!= model_
->FirstPanelIndex() - 1) {
1174 model_
->Move(current_index
, model_
->FirstPanelIndex() - 1);
1175 StartFadeInLastVisibleItem();
1176 } else if (is_overflow_mode()) {
1177 // Overflow bubble should be shrunk when an item is ripped off.
1178 PreferredSizeChanged();
1180 // Make the item partially disappear to show that it will get removed if
1182 drag_image_
->SetOpacity(kDraggedImageOpacity
);
1189 void ShelfView::FinalizeRipOffDrag(bool cancel
) {
1190 if (!dragged_off_shelf_
)
1192 // Make sure we do not come in here again.
1193 dragged_off_shelf_
= false;
1195 // Coming here we should always have a |drag_view_|.
1197 int current_index
= view_model_
->GetIndexOfView(drag_view_
);
1198 // If the view isn't part of the model anymore (|current_index| == -1), a sync
1199 // operation must have removed it. In that case we shouldn't change the model
1200 // and only delete the proxy image.
1201 if (current_index
== -1) {
1202 DestroyDragIconProxy();
1206 // Set to true when the animation should snap back to where it was before.
1207 bool snap_back
= false;
1208 // Items which cannot be dragged off will be handled as a cancel.
1210 if (dragged_off_from_overflow_to_shelf_
) {
1211 dragged_off_from_overflow_to_shelf_
= false;
1212 main_shelf_
->EndDrag(false);
1213 drag_view_
->layer()->SetOpacity(1.0f
);
1214 } else if (RemovableByRipOff(current_index
) != REMOVABLE
) {
1215 // Make sure we do not try to remove un-removable items like items which
1216 // were not pinned or have to be always there.
1220 // Make sure the item stays invisible upon removal.
1221 drag_view_
->SetVisible(false);
1222 std::string app_id
=
1223 delegate_
->GetAppIDForShelfID(model_
->items()[current_index
].id
);
1224 delegate_
->UnpinAppWithID(app_id
);
1227 if (cancel
|| snap_back
) {
1228 if (dragged_off_from_overflow_to_shelf_
) {
1229 dragged_off_from_overflow_to_shelf_
= false;
1230 // Main shelf handles revert of dragged item.
1231 main_shelf_
->EndDrag(true);
1232 drag_view_
->layer()->SetOpacity(1.0f
);
1233 } else if (!cancelling_drag_model_changed_
) {
1234 // Only do something if the change did not come through a model change.
1235 gfx::Rect drag_bounds
= drag_image_
->GetBoundsInScreen();
1236 gfx::Point relative_to
= GetBoundsInScreen().origin();
1238 gfx::PointAtOffsetFromOrigin(drag_bounds
.origin()- relative_to
),
1239 drag_bounds
.size());
1240 drag_view_
->SetBoundsRect(target
);
1241 // Hide the status from the active item since we snap it back now. Upon
1242 // animation end the flag gets cleared if |snap_back_from_rip_off_view_|
1244 snap_back_from_rip_off_view_
= drag_view_
;
1245 ShelfButton
* button
= static_cast<ShelfButton
*>(drag_view_
);
1246 button
->AddState(ShelfButton::STATE_HIDDEN
);
1247 // When a canceling drag model is happening, the view model is diverged
1248 // from the menu model and movements / animations should not be done.
1249 model_
->Move(current_index
, start_drag_index_
);
1250 AnimateToIdealBounds();
1252 drag_view_
->layer()->SetOpacity(1.0f
);
1254 DestroyDragIconProxy();
1257 ShelfView::RemovableState
ShelfView::RemovableByRipOff(int index
) {
1258 DCHECK(index
>= 0 && index
< model_
->item_count());
1259 ShelfItemType type
= model_
->items()[index
].type
;
1260 if (type
== TYPE_APP_LIST
|| type
== TYPE_DIALOG
|| !delegate_
->CanPin())
1261 return NOT_REMOVABLE
;
1263 std::string app_id
= delegate_
->GetAppIDForShelfID(model_
->items()[index
].id
);
1264 // Note: Only pinned app shortcuts can be removed!
1265 return (type
== TYPE_APP_SHORTCUT
&& delegate_
->IsAppPinned(app_id
)) ?
1266 REMOVABLE
: DRAGGABLE
;
1269 bool ShelfView::SameDragType(ShelfItemType typea
, ShelfItemType typeb
) const {
1271 case TYPE_APP_SHORTCUT
:
1272 case TYPE_BROWSER_SHORTCUT
:
1273 return (typeb
== TYPE_APP_SHORTCUT
|| typeb
== TYPE_BROWSER_SHORTCUT
);
1275 case TYPE_PLATFORM_APP
:
1276 case TYPE_WINDOWED_APP
:
1277 case TYPE_APP_PANEL
:
1279 return typeb
== typea
;
1280 case TYPE_UNDEFINED
:
1281 NOTREACHED() << "ShelfItemType must be set.";
1288 std::pair
<int, int> ShelfView::GetDragRange(int index
) {
1291 ShelfItemType type
= model_
->items()[index
].type
;
1292 for (int i
= 0; i
< model_
->item_count(); ++i
) {
1293 if (SameDragType(model_
->items()[i
].type
, type
)) {
1294 if (min_index
== -1)
1299 return std::pair
<int, int>(min_index
, max_index
);
1302 void ShelfView::ConfigureChildView(views::View
* view
) {
1303 view
->SetPaintToLayer(true);
1304 view
->layer()->SetFillsBoundsOpaquely(false);
1307 void ShelfView::ToggleOverflowBubble() {
1308 if (IsShowingOverflowBubble()) {
1309 overflow_bubble_
->Hide();
1313 if (!overflow_bubble_
)
1314 overflow_bubble_
.reset(new OverflowBubble());
1316 ShelfView
* overflow_view
=
1317 new ShelfView(model_
, delegate_
, layout_manager_
);
1318 overflow_view
->overflow_mode_
= true;
1319 overflow_view
->Init();
1320 overflow_view
->set_owner_overflow_bubble(overflow_bubble_
.get());
1321 overflow_view
->OnShelfAlignmentChanged();
1322 overflow_view
->main_shelf_
= this;
1323 UpdateOverflowRange(overflow_view
);
1325 overflow_bubble_
->Show(overflow_button_
, overflow_view
);
1327 Shell::GetInstance()->UpdateShelfVisibility();
1330 void ShelfView::UpdateFirstButtonPadding() {
1331 if (ash::switches::UseAlternateShelfLayout())
1334 // Creates an empty border for first shelf button to make included leading
1335 // inset act as the button's padding. This is only needed on button creation
1336 // and when shelf alignment changes.
1337 if (view_model_
->view_size() > 0) {
1338 view_model_
->view_at(0)->SetBorder(views::Border::CreateEmptyBorder(
1339 layout_manager_
->PrimaryAxisValue(0, leading_inset_
),
1340 layout_manager_
->PrimaryAxisValue(leading_inset_
, 0),
1346 void ShelfView::OnFadeOutAnimationEnded() {
1347 AnimateToIdealBounds();
1348 StartFadeInLastVisibleItem();
1351 void ShelfView::StartFadeInLastVisibleItem() {
1352 // If overflow button is visible and there is a valid new last item, fading
1353 // the new last item in after sliding animation is finished.
1354 if (overflow_button_
->visible() && last_visible_index_
>= 0) {
1355 views::View
* last_visible_view
= view_model_
->view_at(last_visible_index_
);
1356 last_visible_view
->layer()->SetOpacity(0);
1357 bounds_animator_
->SetAnimationDelegate(
1359 new ShelfView::StartFadeAnimationDelegate(this, last_visible_view
),
1364 void ShelfView::UpdateOverflowRange(ShelfView
* overflow_view
) {
1365 const int first_overflow_index
= last_visible_index_
+ 1;
1366 const int last_overflow_index
= last_hidden_index_
;
1367 DCHECK_LE(first_overflow_index
, last_overflow_index
);
1368 DCHECK_LT(last_overflow_index
, view_model_
->view_size());
1370 overflow_view
->first_visible_index_
= first_overflow_index
;
1371 overflow_view
->last_visible_index_
= last_overflow_index
;
1374 int ShelfView::GetButtonSize() const {
1375 return ash::switches::UseAlternateShelfLayout() ?
1376 kButtonSize
: kShelfPreferredSize
;
1379 int ShelfView::GetButtonSpacing() const {
1380 return ash::switches::UseAlternateShelfLayout() ?
1381 kAlternateButtonSpacing
: kButtonSpacing
;
1384 bool ShelfView::ShouldHideTooltip(const gfx::Point
& cursor_location
) {
1385 gfx::Rect active_bounds
;
1387 for (int i
= 0; i
< child_count(); ++i
) {
1388 views::View
* child
= child_at(i
);
1389 if (child
== overflow_button_
)
1391 if (!ShouldShowTooltipForView(child
))
1394 gfx::Rect child_bounds
= child
->GetMirroredBounds();
1395 active_bounds
.Union(child_bounds
);
1398 return !active_bounds
.Contains(cursor_location
);
1401 gfx::Rect
ShelfView::GetVisibleItemsBoundsInScreen() {
1402 gfx::Size preferred_size
= GetPreferredSize();
1403 gfx::Point
origin(GetMirroredXWithWidthInView(0, preferred_size
.width()), 0);
1404 ConvertPointToScreen(this, &origin
);
1405 return gfx::Rect(origin
, preferred_size
);
1408 gfx::Rect
ShelfView::GetBoundsForDragInsertInScreen() {
1409 gfx::Size preferred_size
;
1410 if (is_overflow_mode()) {
1411 DCHECK(owner_overflow_bubble_
);
1412 gfx::Rect bubble_bounds
=
1413 owner_overflow_bubble_
->bubble_view()->GetBubbleBounds();
1414 preferred_size
= bubble_bounds
.size();
1416 const int preferred_shelf_size
= layout_manager_
->GetPreferredShelfSize();
1418 const int last_button_index
= view_model_
->view_size() - 1;
1419 gfx::Rect last_button_bounds
=
1420 view_model_
->view_at(last_button_index
)->bounds();
1421 if (overflow_button_
->visible() &&
1422 model_
->GetItemIndexForType(TYPE_APP_PANEL
) == -1) {
1423 // When overflow button is visible and shelf has no panel items,
1424 // last_button_bounds should be overflow button's bounds.
1425 last_button_bounds
= overflow_button_
->bounds();
1428 if (layout_manager_
->IsHorizontalAlignment()) {
1429 preferred_size
= gfx::Size(last_button_bounds
.right() + leading_inset_
,
1430 preferred_shelf_size
);
1432 preferred_size
= gfx::Size(preferred_shelf_size
,
1433 last_button_bounds
.bottom() + leading_inset_
);
1436 gfx::Point
origin(GetMirroredXWithWidthInView(0, preferred_size
.width()), 0);
1438 // In overflow mode, we should use OverflowBubbleView as a source for
1439 // converting |origin| to screen coordinates. When a scroll operation is
1440 // occurred in OverflowBubble, the bounds of ShelfView in OverflowBubble can
1442 if (is_overflow_mode())
1443 ConvertPointToScreen(owner_overflow_bubble_
->bubble_view(), &origin
);
1445 ConvertPointToScreen(this, &origin
);
1447 return gfx::Rect(origin
, preferred_size
);
1450 int ShelfView::CancelDrag(int modified_index
) {
1451 FinalizeRipOffDrag(true);
1453 return modified_index
;
1454 bool was_dragging
= dragging();
1455 int drag_view_index
= view_model_
->GetIndexOfView(drag_view_
);
1456 drag_pointer_
= NONE
;
1458 if (drag_view_index
== modified_index
) {
1459 // The view that was being dragged is being modified. Don't do anything.
1460 return modified_index
;
1463 return modified_index
;
1465 // Restore previous position, tracking the position of the modified view.
1466 bool at_end
= modified_index
== view_model_
->view_size();
1467 views::View
* modified_view
=
1468 (modified_index
>= 0 && !at_end
) ?
1469 view_model_
->view_at(modified_index
) : NULL
;
1470 model_
->Move(drag_view_index
, start_drag_index_
);
1472 // If the modified view will be at the end of the list, return the new end of
1475 return view_model_
->view_size();
1476 return modified_view
? view_model_
->GetIndexOfView(modified_view
) : -1;
1479 gfx::Size
ShelfView::GetPreferredSize() {
1480 IdealBounds ideal_bounds
;
1481 CalculateIdealBounds(&ideal_bounds
);
1483 const int preferred_size
= layout_manager_
->GetPreferredShelfSize();
1485 int last_button_index
= is_overflow_mode() ?
1486 last_visible_index_
: view_model_
->view_size() - 1;
1488 // When an item is dragged off from the overflow bubble, it is moved to last
1489 // position and and changed to invisible. Overflow bubble size should be
1490 // shrunk to fit only for visible items.
1491 // If |dragged_off_from_overflow_to_shelf_| is set, there will be no invisible
1492 // items in the shelf.
1493 if (is_overflow_mode() &&
1494 dragged_off_shelf_
&&
1495 !dragged_off_from_overflow_to_shelf_
&&
1496 RemovableByRipOff(view_model_
->GetIndexOfView(drag_view_
)) == REMOVABLE
)
1497 last_button_index
--;
1499 const gfx::Rect last_button_bounds
=
1500 last_button_index
>= first_visible_index_
?
1501 view_model_
->ideal_bounds(last_button_index
) :
1502 gfx::Rect(gfx::Size(preferred_size
, preferred_size
));
1504 if (layout_manager_
->IsHorizontalAlignment()) {
1505 return gfx::Size(last_button_bounds
.right() + leading_inset_
,
1509 return gfx::Size(preferred_size
,
1510 last_button_bounds
.bottom() + leading_inset_
);
1513 void ShelfView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
1514 // This bounds change is produced by the shelf movement and all content has
1515 // to follow. Using an animation at that time would produce a time lag since
1516 // the animation of the BoundsAnimator has itself a delay before it arrives
1517 // at the required location. As such we tell the animator to go there
1519 BoundsAnimatorDisabler
disabler(bounds_animator_
.get());
1520 LayoutToIdealBounds();
1521 FOR_EACH_OBSERVER(ShelfIconObserver
, observers_
,
1522 OnShelfIconPositionsChanged());
1524 if (IsShowingOverflowBubble())
1525 overflow_bubble_
->Hide();
1528 views::FocusTraversable
* ShelfView::GetPaneFocusTraversable() {
1532 void ShelfView::GetAccessibleState(ui::AccessibleViewState
* state
) {
1533 state
->role
= ui::AccessibilityTypes::ROLE_TOOLBAR
;
1534 state
->name
= l10n_util::GetStringUTF16(IDS_ASH_SHELF_ACCESSIBLE_NAME
);
1537 void ShelfView::OnGestureEvent(ui::GestureEvent
* event
) {
1538 if (gesture_handler_
.ProcessGestureEvent(*event
))
1539 event
->StopPropagation();
1542 void ShelfView::ShelfItemAdded(int model_index
) {
1544 base::AutoReset
<bool> cancelling_drag(
1545 &cancelling_drag_model_changed_
, true);
1546 model_index
= CancelDrag(model_index
);
1548 views::View
* view
= CreateViewForItem(model_
->items()[model_index
]);
1550 // Hide the view, it'll be made visible when the animation is done. Using
1551 // opacity 0 here to avoid messing with CalculateIdealBounds which touches
1552 // the view's visibility.
1553 view
->layer()->SetOpacity(0);
1554 view_model_
->Add(view
, model_index
);
1556 // Give the button its ideal bounds. That way if we end up animating the
1557 // button before this animation completes it doesn't appear at some random
1558 // spot (because it was in the middle of animating from 0,0 0x0 to its
1560 IdealBounds ideal_bounds
;
1561 CalculateIdealBounds(&ideal_bounds
);
1562 view
->SetBoundsRect(view_model_
->ideal_bounds(model_index
));
1564 // The first animation moves all the views to their target position. |view|
1565 // is hidden, so it visually appears as though we are providing space for
1566 // it. When done we'll fade the view in.
1567 AnimateToIdealBounds();
1568 if (model_index
<= last_visible_index_
||
1569 model_index
>= model_
->FirstPanelIndex()) {
1570 bounds_animator_
->SetAnimationDelegate(
1571 view
, new StartFadeAnimationDelegate(this, view
), true);
1573 // Undo the hiding if animation does not run.
1574 view
->layer()->SetOpacity(1.0f
);
1578 void ShelfView::ShelfItemRemoved(int model_index
, ShelfID id
) {
1579 if (id
== context_menu_id_
)
1580 launcher_menu_runner_
.reset();
1582 base::AutoReset
<bool> cancelling_drag(
1583 &cancelling_drag_model_changed_
, true);
1584 model_index
= CancelDrag(model_index
);
1586 views::View
* view
= view_model_
->view_at(model_index
);
1587 view_model_
->Remove(model_index
);
1589 // When the overflow bubble is visible, the overflow range needs to be set
1590 // before CalculateIdealBounds() gets called. Otherwise CalculateIdealBounds()
1591 // could trigger a ShelfItemChanged() by hiding the overflow bubble and
1592 // since the overflow bubble is not yet synced with the ShelfModel this
1593 // could cause a crash.
1594 if (overflow_bubble_
&& overflow_bubble_
->IsShowing()) {
1595 last_hidden_index_
= std::min(last_hidden_index_
,
1596 view_model_
->view_size() - 1);
1597 UpdateOverflowRange(overflow_bubble_
->shelf_view());
1600 if (view
->visible()) {
1601 // The first animation fades out the view. When done we'll animate the rest
1602 // of the views to their target location.
1603 bounds_animator_
->AnimateViewTo(view
, view
->bounds());
1604 bounds_animator_
->SetAnimationDelegate(
1605 view
, new FadeOutAnimationDelegate(this, view
), true);
1607 // We don't need to show a fade out animation for invisible |view|. When an
1608 // item is ripped out from the shelf, its |view| is already invisible.
1609 AnimateToIdealBounds();
1612 // Close the tooltip because it isn't needed any longer and its anchor view
1613 // will be deleted soon.
1614 if (tooltip_
->GetCurrentAnchorView() == view
)
1618 void ShelfView::ShelfItemChanged(int model_index
, const ShelfItem
& old_item
) {
1619 const ShelfItem
& item(model_
->items()[model_index
]);
1620 if (old_item
.type
!= item
.type
) {
1621 // Type changed, swap the views.
1622 model_index
= CancelDrag(model_index
);
1623 scoped_ptr
<views::View
> old_view(view_model_
->view_at(model_index
));
1624 bounds_animator_
->StopAnimatingView(old_view
.get());
1625 // Removing and re-inserting a view in our view model will strip the ideal
1626 // bounds from the item. To avoid recalculation of everything the bounds
1627 // get remembered and restored after the insertion to the previous value.
1628 gfx::Rect old_ideal_bounds
= view_model_
->ideal_bounds(model_index
);
1629 view_model_
->Remove(model_index
);
1630 views::View
* new_view
= CreateViewForItem(item
);
1631 AddChildView(new_view
);
1632 view_model_
->Add(new_view
, model_index
);
1633 view_model_
->set_ideal_bounds(model_index
, old_ideal_bounds
);
1634 new_view
->SetBoundsRect(old_view
->bounds());
1638 views::View
* view
= view_model_
->view_at(model_index
);
1639 switch (item
.type
) {
1640 case TYPE_BROWSER_SHORTCUT
:
1641 // Fallthrough for the new Shelf since it needs to show the activation
1643 case TYPE_APP_SHORTCUT
:
1644 case TYPE_WINDOWED_APP
:
1645 case TYPE_PLATFORM_APP
:
1647 case TYPE_APP_PANEL
: {
1648 ShelfButton
* button
= static_cast<ShelfButton
*>(view
);
1649 ReflectItemStatus(item
, button
);
1650 // The browser shortcut is currently not a "real" item and as such the
1651 // the image is bogous as well. We therefore keep the image as is for it.
1652 if (item
.type
!= TYPE_BROWSER_SHORTCUT
)
1653 button
->SetImage(item
.image
);
1654 button
->SchedulePaint();
1663 void ShelfView::ShelfItemMoved(int start_index
, int target_index
) {
1664 view_model_
->Move(start_index
, target_index
);
1665 // When cancelling a drag due to a shelf item being added, the currently
1666 // dragged item is moved back to its initial position. AnimateToIdealBounds
1667 // will be called again when the new item is added to the |view_model_| but
1668 // at this time the |view_model_| is inconsistent with the |model_|.
1669 if (!cancelling_drag_model_changed_
)
1670 AnimateToIdealBounds();
1673 void ShelfView::ShelfStatusChanged() {
1674 if (ash::switches::UseAlternateShelfLayout())
1676 AppListButton
* app_list_button
=
1677 static_cast<AppListButton
*>(GetAppListButtonView());
1678 if (model_
->status() == ShelfModel::STATUS_LOADING
)
1679 app_list_button
->StartLoadingAnimation();
1681 app_list_button
->StopLoadingAnimation();
1684 void ShelfView::PointerPressedOnButton(views::View
* view
,
1686 const ui::LocatedEvent
& event
) {
1690 int index
= view_model_
->GetIndexOfView(view
);
1694 ShelfItemDelegate
* item_delegate
= item_manager_
->GetShelfItemDelegate(
1695 model_
->items()[index
].id
);
1696 if (view_model_
->view_size() <= 1 || !item_delegate
->IsDraggable())
1697 return; // View is being deleted or not draggable, ignore request.
1700 drag_offset_
= layout_manager_
->PrimaryAxisValue(event
.x(), event
.y());
1701 UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage",
1702 layout_manager_
->SelectValueForShelfAlignment(
1703 SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM
,
1704 SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT
,
1705 SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT
,
1707 SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT
);
1710 void ShelfView::PointerDraggedOnButton(views::View
* view
,
1712 const ui::LocatedEvent
& event
) {
1713 // To prepare all drag types (moving an item in the shelf and dragging off),
1714 // we should check the x-axis and y-axis offset.
1715 if (!dragging() && drag_view_
&&
1716 ((abs(event
.x() - drag_offset_
) >= kMinimumDragDistance
) ||
1717 (abs(event
.y() - drag_offset_
) >= kMinimumDragDistance
))) {
1718 PrepareForDrag(pointer
, event
);
1720 if (drag_pointer_
== pointer
)
1721 ContinueDrag(event
);
1724 void ShelfView::PointerReleasedOnButton(views::View
* view
,
1729 } else if (drag_pointer_
== pointer
) {
1730 FinalizeRipOffDrag(false);
1731 drag_pointer_
= NONE
;
1732 AnimateToIdealBounds();
1734 // If the drag pointer is NONE, no drag operation is going on and the
1735 // drag_view can be released.
1736 if (drag_pointer_
== NONE
)
1740 void ShelfView::MouseMovedOverButton(views::View
* view
) {
1741 if (!ShouldShowTooltipForView(view
))
1744 if (!tooltip_
->IsVisible())
1745 tooltip_
->ResetTimer();
1748 void ShelfView::MouseEnteredButton(views::View
* view
) {
1749 if (!ShouldShowTooltipForView(view
))
1752 if (tooltip_
->IsVisible()) {
1753 tooltip_
->ShowImmediately(view
, GetAccessibleName(view
));
1755 tooltip_
->ShowDelayed(view
, GetAccessibleName(view
));
1759 void ShelfView::MouseExitedButton(views::View
* view
) {
1760 if (!tooltip_
->IsVisible())
1761 tooltip_
->StopTimer();
1764 base::string16
ShelfView::GetAccessibleName(const views::View
* view
) {
1765 int view_index
= view_model_
->GetIndexOfView(view
);
1766 // May be -1 while in the process of animating closed.
1767 if (view_index
== -1)
1768 return base::string16();
1770 ShelfItemDelegate
* item_delegate
= item_manager_
->GetShelfItemDelegate(
1771 model_
->items()[view_index
].id
);
1772 return item_delegate
->GetTitle();
1775 void ShelfView::ButtonPressed(views::Button
* sender
, const ui::Event
& event
) {
1776 // Do not handle mouse release during drag.
1780 if (sender
== overflow_button_
) {
1781 ToggleOverflowBubble();
1785 int view_index
= view_model_
->GetIndexOfView(sender
);
1786 // May be -1 while in the process of animating closed.
1787 if (view_index
== -1)
1790 // If the previous menu was closed by the same event as this one, we ignore
1792 if (!IsUsableEvent(event
))
1796 ScopedTargetRootWindow
scoped_target(
1797 sender
->GetWidget()->GetNativeView()->GetRootWindow());
1798 // Slow down activation animations if shift key is pressed.
1799 scoped_ptr
<ui::ScopedAnimationDurationScaleMode
> slowing_animations
;
1800 if (event
.IsShiftDown()) {
1801 slowing_animations
.reset(new ui::ScopedAnimationDurationScaleMode(
1802 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION
));
1805 // Collect usage statistics before we decide what to do with the click.
1806 switch (model_
->items()[view_index
].type
) {
1807 case TYPE_APP_SHORTCUT
:
1808 case TYPE_WINDOWED_APP
:
1809 case TYPE_PLATFORM_APP
:
1810 case TYPE_BROWSER_SHORTCUT
:
1811 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1812 UMA_LAUNCHER_CLICK_ON_APP
);
1816 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1817 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON
);
1820 case TYPE_APP_PANEL
:
1824 case TYPE_UNDEFINED
:
1825 NOTREACHED() << "ShelfItemType must be set.";
1829 ShelfItemDelegate
* item_delegate
=
1830 item_manager_
->GetShelfItemDelegate(model_
->items()[view_index
].id
);
1831 if (!item_delegate
->ItemSelected(event
))
1832 ShowListMenuForView(model_
->items()[view_index
], sender
, event
);
1836 bool ShelfView::ShowListMenuForView(const ShelfItem
& item
,
1837 views::View
* source
,
1838 const ui::Event
& event
) {
1839 scoped_ptr
<ShelfMenuModel
> menu_model
;
1840 ShelfItemDelegate
* item_delegate
=
1841 item_manager_
->GetShelfItemDelegate(item
.id
);
1842 menu_model
.reset(item_delegate
->CreateApplicationMenu(event
.flags()));
1844 // Make sure we have a menu and it has at least two items in addition to the
1845 // application title and the 3 spacing separators.
1846 if (!menu_model
.get() || menu_model
->GetItemCount() <= 5)
1849 ShowMenu(scoped_ptr
<views::MenuModelAdapter
>(
1850 new ShelfMenuModelAdapter(menu_model
.get())),
1854 ui::GetMenuSourceTypeForEvent(event
));
1858 void ShelfView::ShowContextMenuForView(views::View
* source
,
1859 const gfx::Point
& point
,
1860 ui::MenuSourceType source_type
) {
1861 int view_index
= view_model_
->GetIndexOfView(source
);
1862 if (view_index
== -1) {
1863 Shell::GetInstance()->ShowContextMenu(point
, source_type
);
1867 scoped_ptr
<ui::MenuModel
> menu_model
;
1868 ShelfItemDelegate
* item_delegate
= item_manager_
->GetShelfItemDelegate(
1869 model_
->items()[view_index
].id
);
1870 menu_model
.reset(item_delegate
->CreateContextMenu(
1871 source
->GetWidget()->GetNativeView()->GetRootWindow()));
1875 base::AutoReset
<ShelfID
> reseter(
1877 view_index
== -1 ? 0 : model_
->items()[view_index
].id
);
1879 ShowMenu(scoped_ptr
<views::MenuModelAdapter
>(
1880 new views::MenuModelAdapter(menu_model
.get())),
1887 void ShelfView::ShowMenu(scoped_ptr
<views::MenuModelAdapter
> menu_model_adapter
,
1888 views::View
* source
,
1889 const gfx::Point
& click_point
,
1891 ui::MenuSourceType source_type
) {
1892 closing_event_time_
= base::TimeDelta();
1893 launcher_menu_runner_
.reset(
1894 new views::MenuRunner(menu_model_adapter
->CreateMenu()));
1896 ScopedTargetRootWindow
scoped_target(
1897 source
->GetWidget()->GetNativeView()->GetRootWindow());
1899 // Determine the menu alignment dependent on the shelf.
1900 views::MenuItemView::AnchorPosition menu_alignment
=
1901 views::MenuItemView::TOPLEFT
;
1902 gfx::Rect anchor_point
= gfx::Rect(click_point
, gfx::Size());
1904 ShelfWidget
* shelf
= RootWindowController::ForShelf(
1905 GetWidget()->GetNativeView())->shelf();
1906 if (!context_menu
) {
1907 // Application lists use a bubble.
1908 ShelfAlignment align
= shelf
->GetAlignment();
1909 anchor_point
= source
->GetBoundsInScreen();
1911 // It is possible to invoke the menu while it is sliding into view. To cover
1912 // that case, the screen coordinates are offsetted by the animation delta.
1913 gfx::Vector2d offset
=
1914 source
->GetWidget()->GetNativeWindow()->bounds().origin() -
1915 source
->GetWidget()->GetNativeWindow()->GetTargetBounds().origin();
1916 anchor_point
.set_x(anchor_point
.x() - offset
.x());
1917 anchor_point
.set_y(anchor_point
.y() - offset
.y());
1919 // Shelf items can have an asymmetrical border for spacing reasons.
1920 // Adjust anchor location for this.
1921 if (source
->border())
1922 anchor_point
.Inset(source
->border()->GetInsets());
1925 case SHELF_ALIGNMENT_BOTTOM
:
1926 menu_alignment
= views::MenuItemView::BUBBLE_ABOVE
;
1928 case SHELF_ALIGNMENT_LEFT
:
1929 menu_alignment
= views::MenuItemView::BUBBLE_RIGHT
;
1931 case SHELF_ALIGNMENT_RIGHT
:
1932 menu_alignment
= views::MenuItemView::BUBBLE_LEFT
;
1934 case SHELF_ALIGNMENT_TOP
:
1935 menu_alignment
= views::MenuItemView::BUBBLE_BELOW
;
1939 // If this gets deleted while we are in the menu, the shelf will be gone
1941 bool got_deleted
= false;
1942 got_deleted_
= &got_deleted
;
1944 shelf
->ForceUndimming(true);
1945 // NOTE: if you convert to HAS_MNEMONICS be sure and update menu building
1947 if (launcher_menu_runner_
->RunMenuAt(
1948 source
->GetWidget(),
1953 context_menu
? views::MenuRunner::CONTEXT_MENU
: 0) ==
1954 views::MenuRunner::MENU_DELETED
) {
1956 got_deleted_
= NULL
;
1957 shelf
->ForceUndimming(false);
1961 got_deleted_
= NULL
;
1962 shelf
->ForceUndimming(false);
1964 // If it is a context menu and we are showing overflow bubble
1965 // we want to hide overflow bubble.
1966 if (owner_overflow_bubble_
)
1967 owner_overflow_bubble_
->HideBubbleAndRefreshButton();
1969 // Unpinning an item will reset the |launcher_menu_runner_| before coming
1971 if (launcher_menu_runner_
)
1972 closing_event_time_
= launcher_menu_runner_
->closing_event_time();
1973 Shell::GetInstance()->UpdateShelfVisibility();
1976 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator
* animator
) {
1977 FOR_EACH_OBSERVER(ShelfIconObserver
, observers_
,
1978 OnShelfIconPositionsChanged());
1979 PreferredSizeChanged();
1982 void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator
* animator
) {
1983 if (snap_back_from_rip_off_view_
&& animator
== bounds_animator_
) {
1984 if (!animator
->IsAnimating(snap_back_from_rip_off_view_
)) {
1985 // Coming here the animation of the ShelfButton is finished and the
1986 // previously hidden status can be shown again. Since the button itself
1987 // might have gone away or changed locations we check that the button
1988 // is still in the shelf and show its status again.
1989 for (int index
= 0; index
< view_model_
->view_size(); index
++) {
1990 views::View
* view
= view_model_
->view_at(index
);
1991 if (view
== snap_back_from_rip_off_view_
) {
1992 ShelfButton
* button
= static_cast<ShelfButton
*>(view
);
1993 button
->ClearState(ShelfButton::STATE_HIDDEN
);
1997 snap_back_from_rip_off_view_
= NULL
;
2002 bool ShelfView::IsUsableEvent(const ui::Event
& event
) {
2003 if (closing_event_time_
== base::TimeDelta())
2006 base::TimeDelta delta
=
2007 base::TimeDelta(event
.time_stamp() - closing_event_time_
);
2008 closing_event_time_
= base::TimeDelta();
2009 // TODO(skuhne): This time seems excessive, but it appears that the reposting
2010 // takes that long. Need to come up with a better way of doing this.
2011 return (delta
.InMilliseconds() < 0 || delta
.InMilliseconds() > 130);
2014 const ShelfItem
* ShelfView::ShelfItemForView(const views::View
* view
) const {
2015 int view_index
= view_model_
->GetIndexOfView(view
);
2016 if (view_index
== -1)
2018 return &(model_
->items()[view_index
]);
2021 bool ShelfView::ShouldShowTooltipForView(const views::View
* view
) const {
2022 if (view
== GetAppListButtonView() &&
2023 Shell::GetInstance()->GetAppListWindow())
2025 const ShelfItem
* item
= ShelfItemForView(view
);
2028 ShelfItemDelegate
* item_delegate
=
2029 item_manager_
->GetShelfItemDelegate(item
->id
);
2030 return item_delegate
->ShouldShowTooltip();
2033 int ShelfView::CalculateShelfDistance(const gfx::Point
& coordinate
) const {
2034 ShelfWidget
* shelf
= RootWindowController::ForShelf(
2035 GetWidget()->GetNativeView())->shelf();
2036 ShelfAlignment align
= shelf
->GetAlignment();
2037 const gfx::Rect bounds
= GetBoundsInScreen();
2040 case SHELF_ALIGNMENT_BOTTOM
:
2041 distance
= bounds
.y() - coordinate
.y();
2043 case SHELF_ALIGNMENT_LEFT
:
2044 distance
= coordinate
.x() - bounds
.right();
2046 case SHELF_ALIGNMENT_RIGHT
:
2047 distance
= bounds
.x() - coordinate
.x();
2049 case SHELF_ALIGNMENT_TOP
:
2050 distance
= coordinate
.y() - bounds
.bottom();
2053 return distance
> 0 ? distance
: 0;
2056 } // namespace internal