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/accessibility/ax_view_state.h"
39 #include "ui/aura/client/screen_position_client.h"
40 #include "ui/aura/window.h"
41 #include "ui/aura/window_event_dispatcher.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"
65 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM
= 0;
66 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT
= 1;
67 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT
= 2;
68 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT
= 3;
70 // Default amount content is inset on the left edge.
71 const int kDefaultLeadingInset
= 8;
73 // Minimum distance before drag starts.
74 const int kMinimumDragDistance
= 8;
76 // Size between the buttons.
77 const int kButtonSpacing
= 4;
78 const int kAlternateButtonSpacing
= 10;
80 // Size allocated to for each button.
81 const int kButtonSize
= 44;
83 // Additional spacing for the left and right side of icons.
84 const int kHorizontalIconSpacing
= 2;
86 // Inset for items which do not have an icon.
87 const int kHorizontalNoIconInsetSpacing
=
88 kHorizontalIconSpacing
+ kDefaultLeadingInset
;
90 // The proportion of the shelf space reserved for non-panel icons. Panels
91 // may flow into this space but will be put into the overflow bubble if there
92 // is contention for the space.
93 const float kReservedNonPanelIconProportion
= 0.67f
;
95 // This is the command id of the menu item which contains the name of the menu.
96 const int kCommandIdOfMenuName
= 0;
98 // The background color of the active item in the list.
99 const SkColor kActiveListItemBackgroundColor
= SkColorSetRGB(203 , 219, 241);
101 // The background color of the active & hovered item in the list.
102 const SkColor kFocusedActiveListItemBackgroundColor
=
103 SkColorSetRGB(193, 211, 236);
105 // The text color of the caption item in a list.
106 const SkColor kCaptionItemForegroundColor
= SK_ColorBLACK
;
108 // The maximum allowable length of a menu line of an application menu in pixels.
109 const int kMaximumAppMenuItemLength
= 350;
111 // The distance of the cursor from the outer rim of the shelf before it
113 const int kRipOffDistance
= 48;
115 // The rip off drag and drop proxy image should get scaled by this factor.
116 const float kDragAndDropProxyScale
= 1.5f
;
118 // The opacity represents that this partially disappeared item will get removed.
119 const float kDraggedImageOpacity
= 0.5f
;
123 // A class to temporarily disable a given bounds animator.
124 class BoundsAnimatorDisabler
{
126 BoundsAnimatorDisabler(views::BoundsAnimator
* bounds_animator
)
127 : old_duration_(bounds_animator
->GetAnimationDuration()),
128 bounds_animator_(bounds_animator
) {
129 bounds_animator_
->SetAnimationDuration(1);
132 ~BoundsAnimatorDisabler() {
133 bounds_animator_
->SetAnimationDuration(old_duration_
);
137 // The previous animation duration.
139 // The bounds animator which gets used.
140 views::BoundsAnimator
* bounds_animator_
;
142 DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorDisabler
);
145 // The MenuModelAdapter gets slightly changed to adapt the menu appearance to
147 class ShelfMenuModelAdapter
: public views::MenuModelAdapter
{
149 explicit ShelfMenuModelAdapter(ShelfMenuModel
* menu_model
);
151 // views::MenuModelAdapter:
152 virtual const gfx::FontList
* GetLabelFontList(int command_id
) const OVERRIDE
;
153 virtual bool IsCommandEnabled(int id
) const OVERRIDE
;
154 virtual void GetHorizontalIconMargins(int id
,
157 int* right_margin
) const OVERRIDE
;
158 virtual bool GetForegroundColor(int command_id
,
160 SkColor
* override_color
) const OVERRIDE
;
161 virtual bool GetBackgroundColor(int command_id
,
163 SkColor
* override_color
) const OVERRIDE
;
164 virtual int GetMaxWidthForMenu(views::MenuItemView
* menu
) OVERRIDE
;
165 virtual bool ShouldReserveSpaceForSubmenuIndicator() const OVERRIDE
;
168 ShelfMenuModel
* menu_model_
;
170 DISALLOW_COPY_AND_ASSIGN(ShelfMenuModelAdapter
);
173 ShelfMenuModelAdapter::ShelfMenuModelAdapter(ShelfMenuModel
* menu_model
)
174 : MenuModelAdapter(menu_model
),
175 menu_model_(menu_model
) {
178 const gfx::FontList
* ShelfMenuModelAdapter::GetLabelFontList(
179 int command_id
) const {
180 if (command_id
!= kCommandIdOfMenuName
)
181 return MenuModelAdapter::GetLabelFontList(command_id
);
183 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
184 return &rb
.GetFontList(ui::ResourceBundle::BoldFont
);
187 bool ShelfMenuModelAdapter::IsCommandEnabled(int id
) const {
188 return id
!= kCommandIdOfMenuName
;
191 bool ShelfMenuModelAdapter::GetForegroundColor(int command_id
,
193 SkColor
* override_color
) const {
194 if (command_id
!= kCommandIdOfMenuName
)
197 *override_color
= kCaptionItemForegroundColor
;
201 bool ShelfMenuModelAdapter::GetBackgroundColor(int command_id
,
203 SkColor
* override_color
) const {
204 if (!menu_model_
->IsCommandActive(command_id
))
207 *override_color
= is_hovered
? kFocusedActiveListItemBackgroundColor
:
208 kActiveListItemBackgroundColor
;
212 void ShelfMenuModelAdapter::GetHorizontalIconMargins(int command_id
,
215 int* right_margin
) const {
216 *left_margin
= kHorizontalIconSpacing
;
217 *right_margin
= (command_id
!= kCommandIdOfMenuName
) ?
218 kHorizontalIconSpacing
: -(icon_size
+ kHorizontalNoIconInsetSpacing
);
221 int ShelfMenuModelAdapter::GetMaxWidthForMenu(views::MenuItemView
* menu
) {
222 return kMaximumAppMenuItemLength
;
225 bool ShelfMenuModelAdapter::ShouldReserveSpaceForSubmenuIndicator() const {
229 // Custom FocusSearch used to navigate the shelf in the order items are in
231 class ShelfFocusSearch
: public views::FocusSearch
{
233 explicit ShelfFocusSearch(views::ViewModel
* view_model
)
234 : FocusSearch(NULL
, true, true),
235 view_model_(view_model
) {}
236 virtual ~ShelfFocusSearch() {}
238 // views::FocusSearch overrides:
239 virtual View
* FindNextFocusableView(
243 bool check_starting_view
,
244 views::FocusTraversable
** focus_traversable
,
245 View
** focus_traversable_view
) OVERRIDE
{
246 int index
= view_model_
->GetIndexOfView(starting_view
);
248 return view_model_
->view_at(0);
253 index
= view_model_
->view_size() - 1;
256 if (index
>= view_model_
->view_size())
259 return view_model_
->view_at(index
);
263 views::ViewModel
* view_model_
;
265 DISALLOW_COPY_AND_ASSIGN(ShelfFocusSearch
);
268 // AnimationDelegate used when inserting a new item. This steadily increases the
269 // opacity of the layer as the animation progress.
270 class FadeInAnimationDelegate
271 : public views::BoundsAnimator::OwnedAnimationDelegate
{
273 explicit FadeInAnimationDelegate(views::View
* view
) : view_(view
) {}
274 virtual ~FadeInAnimationDelegate() {}
276 // AnimationDelegate overrides:
277 virtual void AnimationProgressed(const Animation
* animation
) OVERRIDE
{
278 view_
->layer()->SetOpacity(animation
->GetCurrentValue());
279 view_
->layer()->ScheduleDraw();
281 virtual void AnimationEnded(const Animation
* animation
) OVERRIDE
{
282 view_
->layer()->SetOpacity(1.0f
);
283 view_
->layer()->ScheduleDraw();
285 virtual void AnimationCanceled(const Animation
* animation
) OVERRIDE
{
286 view_
->layer()->SetOpacity(1.0f
);
287 view_
->layer()->ScheduleDraw();
293 DISALLOW_COPY_AND_ASSIGN(FadeInAnimationDelegate
);
296 void ReflectItemStatus(const ShelfItem
& item
, ShelfButton
* button
) {
297 switch (item
.status
) {
299 button
->ClearState(ShelfButton::STATE_ACTIVE
);
300 button
->ClearState(ShelfButton::STATE_RUNNING
);
301 button
->ClearState(ShelfButton::STATE_ATTENTION
);
304 button
->ClearState(ShelfButton::STATE_ACTIVE
);
305 button
->AddState(ShelfButton::STATE_RUNNING
);
306 button
->ClearState(ShelfButton::STATE_ATTENTION
);
309 button
->AddState(ShelfButton::STATE_ACTIVE
);
310 button
->ClearState(ShelfButton::STATE_RUNNING
);
311 button
->ClearState(ShelfButton::STATE_ATTENTION
);
313 case STATUS_ATTENTION
:
314 button
->ClearState(ShelfButton::STATE_ACTIVE
);
315 button
->ClearState(ShelfButton::STATE_RUNNING
);
316 button
->AddState(ShelfButton::STATE_ATTENTION
);
323 // AnimationDelegate used when deleting an item. This steadily decreased the
324 // opacity of the layer as the animation progress.
325 class ShelfView::FadeOutAnimationDelegate
326 : public views::BoundsAnimator::OwnedAnimationDelegate
{
328 FadeOutAnimationDelegate(ShelfView
* host
, views::View
* view
)
331 virtual ~FadeOutAnimationDelegate() {}
333 // AnimationDelegate overrides:
334 virtual void AnimationProgressed(const Animation
* animation
) OVERRIDE
{
335 view_
->layer()->SetOpacity(1 - animation
->GetCurrentValue());
336 view_
->layer()->ScheduleDraw();
338 virtual void AnimationEnded(const Animation
* animation
) OVERRIDE
{
339 shelf_view_
->OnFadeOutAnimationEnded();
341 virtual void AnimationCanceled(const Animation
* animation
) OVERRIDE
{
345 ShelfView
* shelf_view_
;
346 scoped_ptr
<views::View
> view_
;
348 DISALLOW_COPY_AND_ASSIGN(FadeOutAnimationDelegate
);
351 // AnimationDelegate used to trigger fading an element in. When an item is
352 // inserted this delegate is attached to the animation that expands the size of
353 // the item. When done it kicks off another animation to fade the item in.
354 class ShelfView::StartFadeAnimationDelegate
355 : public views::BoundsAnimator::OwnedAnimationDelegate
{
357 StartFadeAnimationDelegate(ShelfView
* host
,
361 virtual ~StartFadeAnimationDelegate() {}
363 // AnimationDelegate overrides:
364 virtual void AnimationEnded(const Animation
* animation
) OVERRIDE
{
365 shelf_view_
->FadeIn(view_
);
367 virtual void AnimationCanceled(const Animation
* animation
) OVERRIDE
{
368 view_
->layer()->SetOpacity(1.0f
);
372 ShelfView
* shelf_view_
;
375 DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate
);
378 ShelfView::ShelfView(ShelfModel
* model
,
379 ShelfDelegate
* delegate
,
380 ShelfLayoutManager
* manager
)
383 view_model_(new views::ViewModel
),
384 first_visible_index_(0),
385 last_visible_index_(-1),
386 overflow_button_(NULL
),
387 owner_overflow_bubble_(NULL
),
391 start_drag_index_(-1),
393 leading_inset_(kDefaultLeadingInset
),
394 cancelling_drag_model_changed_(false),
395 last_hidden_index_(0),
396 closing_event_time_(base::TimeDelta()),
398 drag_and_drop_item_pinned_(false),
399 drag_and_drop_shelf_id_(0),
400 dragged_off_shelf_(false),
401 snap_back_from_rip_off_view_(NULL
),
402 item_manager_(Shell::GetInstance()->shelf_item_delegate_manager()),
403 layout_manager_(manager
),
404 overflow_mode_(false),
406 dragged_off_from_overflow_to_shelf_(false) {
408 bounds_animator_
.reset(new views::BoundsAnimator(this));
409 bounds_animator_
->AddObserver(this);
410 set_context_menu_controller(this);
411 focus_search_
.reset(new ShelfFocusSearch(view_model_
.get()));
412 tooltip_
.reset(new ShelfTooltipManager(manager
, this));
415 ShelfView::~ShelfView() {
416 bounds_animator_
->RemoveObserver(this);
417 model_
->RemoveObserver(this);
418 // If we are inside the MenuRunner, we need to know if we were getting
419 // deleted while it was running.
421 *got_deleted_
= true;
424 void ShelfView::Init() {
425 model_
->AddObserver(this);
427 const ShelfItems
& items(model_
->items());
428 for (ShelfItems::const_iterator i
= items
.begin(); i
!= items
.end(); ++i
) {
429 views::View
* child
= CreateViewForItem(*i
);
430 child
->SetPaintToLayer(true);
431 view_model_
->Add(child
, static_cast<int>(i
- items
.begin()));
434 ShelfStatusChanged();
435 overflow_button_
= new OverflowButton(this);
436 overflow_button_
->set_context_menu_controller(this);
437 ConfigureChildView(overflow_button_
);
438 AddChildView(overflow_button_
);
439 UpdateFirstButtonPadding();
441 // We'll layout when our bounds change.
444 void ShelfView::OnShelfAlignmentChanged() {
445 UpdateFirstButtonPadding();
446 overflow_button_
->OnShelfAlignmentChanged();
447 LayoutToIdealBounds();
448 for (int i
=0; i
< view_model_
->view_size(); ++i
) {
449 // TODO: remove when AppIcon is a Shelf Button.
450 if (TYPE_APP_LIST
== model_
->items()[i
].type
&&
451 !ash::switches::UseAlternateShelfLayout()) {
452 static_cast<AppListButton
*>(view_model_
->view_at(i
))->SetImageAlignment(
453 layout_manager_
->SelectValueForShelfAlignment(
454 views::ImageButton::ALIGN_CENTER
,
455 views::ImageButton::ALIGN_LEFT
,
456 views::ImageButton::ALIGN_RIGHT
,
457 views::ImageButton::ALIGN_CENTER
),
458 layout_manager_
->SelectValueForShelfAlignment(
459 views::ImageButton::ALIGN_TOP
,
460 views::ImageButton::ALIGN_MIDDLE
,
461 views::ImageButton::ALIGN_MIDDLE
,
462 views::ImageButton::ALIGN_BOTTOM
));
464 if (i
>= first_visible_index_
&& i
<= last_visible_index_
)
465 view_model_
->view_at(i
)->Layout();
468 if (overflow_bubble_
)
469 overflow_bubble_
->Hide();
472 void ShelfView::SchedulePaintForAllButtons() {
473 for (int i
= 0; i
< view_model_
->view_size(); ++i
) {
474 if (i
>= first_visible_index_
&& i
<= last_visible_index_
)
475 view_model_
->view_at(i
)->SchedulePaint();
477 if (overflow_button_
&& overflow_button_
->visible())
478 overflow_button_
->SchedulePaint();
481 gfx::Rect
ShelfView::GetIdealBoundsOfItemIcon(ShelfID id
) {
482 int index
= model_
->ItemIndexByID(id
);
483 if (index
== -1 || (index
> last_visible_index_
&&
484 index
< model_
->FirstPanelIndex()))
486 const gfx::Rect
& ideal_bounds(view_model_
->ideal_bounds(index
));
487 DCHECK_NE(TYPE_APP_LIST
, model_
->items()[index
].type
);
488 ShelfButton
* button
= static_cast<ShelfButton
*>(view_model_
->view_at(index
));
489 gfx::Rect icon_bounds
= button
->GetIconBounds();
490 return gfx::Rect(GetMirroredXWithWidthInView(
491 ideal_bounds
.x() + icon_bounds
.x(), icon_bounds
.width()),
492 ideal_bounds
.y() + icon_bounds
.y(),
494 icon_bounds
.height());
497 void ShelfView::UpdatePanelIconPosition(ShelfID id
,
498 const gfx::Point
& midpoint
) {
499 int current_index
= model_
->ItemIndexByID(id
);
500 int first_panel_index
= model_
->FirstPanelIndex();
501 if (current_index
< first_panel_index
)
504 gfx::Point
midpoint_in_view(GetMirroredXInView(midpoint
.x()),
506 int target_index
= current_index
;
507 while (target_index
> first_panel_index
&&
508 layout_manager_
->PrimaryAxisValue(
509 view_model_
->ideal_bounds(target_index
).x(),
510 view_model_
->ideal_bounds(target_index
).y()) >
511 layout_manager_
->PrimaryAxisValue(midpoint_in_view
.x(),
512 midpoint_in_view
.y())) {
515 while (target_index
< view_model_
->view_size() - 1 &&
516 layout_manager_
->PrimaryAxisValue(
517 view_model_
->ideal_bounds(target_index
).right(),
518 view_model_
->ideal_bounds(target_index
).bottom()) <
519 layout_manager_
->PrimaryAxisValue(midpoint_in_view
.x(),
520 midpoint_in_view
.y())) {
523 if (current_index
!= target_index
)
524 model_
->Move(current_index
, target_index
);
527 bool ShelfView::IsShowingMenu() const {
528 return (launcher_menu_runner_
.get() &&
529 launcher_menu_runner_
->IsRunning());
532 bool ShelfView::IsShowingOverflowBubble() const {
533 return overflow_bubble_
.get() && overflow_bubble_
->IsShowing();
536 views::View
* ShelfView::GetAppListButtonView() const {
537 for (int i
= 0; i
< model_
->item_count(); ++i
) {
538 if (model_
->items()[i
].type
== TYPE_APP_LIST
)
539 return view_model_
->view_at(i
);
542 NOTREACHED() << "Applist button not found";
546 ////////////////////////////////////////////////////////////////////////////////
547 // ShelfView, FocusTraversable implementation:
549 views::FocusSearch
* ShelfView::GetFocusSearch() {
550 return focus_search_
.get();
553 views::FocusTraversable
* ShelfView::GetFocusTraversableParent() {
554 return parent()->GetFocusTraversable();
557 View
* ShelfView::GetFocusTraversableParentView() {
561 void ShelfView::CreateDragIconProxy(
562 const gfx::Point
& location_in_screen_coordinates
,
563 const gfx::ImageSkia
& icon
,
564 views::View
* replaced_view
,
565 const gfx::Vector2d
& cursor_offset_from_center
,
566 float scale_factor
) {
567 drag_replaced_view_
= replaced_view
;
568 drag_image_
.reset(new ash::DragImageView(
569 drag_replaced_view_
->GetWidget()->GetNativeWindow()->GetRootWindow(),
570 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE
));
571 drag_image_
->SetImage(icon
);
572 gfx::Size size
= drag_image_
->GetPreferredSize();
573 size
.set_width(size
.width() * scale_factor
);
574 size
.set_height(size
.height() * scale_factor
);
575 drag_image_offset_
= gfx::Vector2d(size
.width() / 2, size
.height() / 2) +
576 cursor_offset_from_center
;
577 gfx::Rect
drag_image_bounds(
578 location_in_screen_coordinates
- drag_image_offset_
,
580 drag_image_
->SetBoundsInScreen(drag_image_bounds
);
581 drag_image_
->SetWidgetVisible(true);
584 void ShelfView::UpdateDragIconProxy(
585 const gfx::Point
& location_in_screen_coordinates
) {
586 // TODO(jennyz): Investigate why drag_image_ becomes NULL at this point per
587 // crbug.com/34722, while the app list item is still being dragged around.
589 drag_image_
->SetScreenPosition(
590 location_in_screen_coordinates
- drag_image_offset_
);
594 void ShelfView::DestroyDragIconProxy() {
596 drag_image_offset_
= gfx::Vector2d(0, 0);
599 bool ShelfView::StartDrag(const std::string
& app_id
,
600 const gfx::Point
& location_in_screen_coordinates
) {
601 // Bail if an operation is already going on - or the cursor is not inside.
602 // This could happen if mouse / touch operations overlap.
603 if (drag_and_drop_shelf_id_
||
604 !GetBoundsInScreen().Contains(location_in_screen_coordinates
))
607 // If the AppsGridView (which was dispatching this event) was opened by our
608 // button, ShelfView dragging operations are locked and we have to unlock.
610 drag_and_drop_item_pinned_
= false;
611 drag_and_drop_app_id_
= app_id
;
612 drag_and_drop_shelf_id_
=
613 delegate_
->GetShelfIDForAppID(drag_and_drop_app_id_
);
614 // Check if the application is known and pinned - if not, we have to pin it so
615 // that we can re-arrange the shelf order accordingly. Note that items have
616 // to be pinned to give them the same (order) possibilities as a shortcut.
617 // When an item is dragged from overflow to shelf, IsShowingOverflowBubble()
618 // returns true. At this time, we don't need to pin the item.
619 if (!IsShowingOverflowBubble() &&
620 (!drag_and_drop_shelf_id_
||
621 !delegate_
->IsAppPinned(app_id
))) {
622 delegate_
->PinAppWithID(app_id
);
623 drag_and_drop_shelf_id_
=
624 delegate_
->GetShelfIDForAppID(drag_and_drop_app_id_
);
625 if (!drag_and_drop_shelf_id_
)
627 drag_and_drop_item_pinned_
= true;
629 views::View
* drag_and_drop_view
= view_model_
->view_at(
630 model_
->ItemIndexByID(drag_and_drop_shelf_id_
));
631 DCHECK(drag_and_drop_view
);
633 // Since there is already an icon presented by the caller, we hide this item
634 // for now. That has to be done by reducing the size since the visibility will
635 // change once a regrouping animation is performed.
636 pre_drag_and_drop_size_
= drag_and_drop_view
->size();
637 drag_and_drop_view
->SetSize(gfx::Size());
639 // First we have to center the mouse cursor over the item.
640 gfx::Point pt
= drag_and_drop_view
->GetBoundsInScreen().CenterPoint();
641 views::View::ConvertPointFromScreen(drag_and_drop_view
, &pt
);
642 gfx::Point point_in_root
= location_in_screen_coordinates
;
643 ash::wm::ConvertPointFromScreen(
644 ash::wm::GetRootWindowAt(location_in_screen_coordinates
),
646 ui::MouseEvent
event(ui::ET_MOUSE_PRESSED
, pt
, point_in_root
, 0, 0);
647 PointerPressedOnButton(drag_and_drop_view
,
648 ShelfButtonHost::DRAG_AND_DROP
,
651 // Drag the item where it really belongs.
652 Drag(location_in_screen_coordinates
);
656 bool ShelfView::Drag(const gfx::Point
& location_in_screen_coordinates
) {
657 if (!drag_and_drop_shelf_id_
||
658 !GetBoundsInScreen().Contains(location_in_screen_coordinates
))
661 gfx::Point pt
= location_in_screen_coordinates
;
662 views::View
* drag_and_drop_view
= view_model_
->view_at(
663 model_
->ItemIndexByID(drag_and_drop_shelf_id_
));
664 ConvertPointFromScreen(drag_and_drop_view
, &pt
);
665 gfx::Point point_in_root
= location_in_screen_coordinates
;
666 ash::wm::ConvertPointFromScreen(
667 ash::wm::GetRootWindowAt(location_in_screen_coordinates
),
669 ui::MouseEvent
event(ui::ET_MOUSE_DRAGGED
, pt
, point_in_root
, 0, 0);
670 PointerDraggedOnButton(drag_and_drop_view
,
671 ShelfButtonHost::DRAG_AND_DROP
,
676 void ShelfView::EndDrag(bool cancel
) {
677 if (!drag_and_drop_shelf_id_
)
680 views::View
* drag_and_drop_view
= view_model_
->view_at(
681 model_
->ItemIndexByID(drag_and_drop_shelf_id_
));
682 PointerReleasedOnButton(
683 drag_and_drop_view
, ShelfButtonHost::DRAG_AND_DROP
, cancel
);
685 // Either destroy the temporarily created item - or - make the item visible.
686 if (drag_and_drop_item_pinned_
&& cancel
)
687 delegate_
->UnpinAppWithID(drag_and_drop_app_id_
);
688 else if (drag_and_drop_view
) {
690 // When a hosted drag gets canceled, the item can remain in the same slot
691 // and it might have moved within the bounds. In that case the item need
692 // to animate back to its correct location.
693 AnimateToIdealBounds();
695 drag_and_drop_view
->SetSize(pre_drag_and_drop_size_
);
699 drag_and_drop_shelf_id_
= 0;
702 void ShelfView::LayoutToIdealBounds() {
703 if (bounds_animator_
->IsAnimating()) {
704 AnimateToIdealBounds();
708 IdealBounds ideal_bounds
;
709 CalculateIdealBounds(&ideal_bounds
);
710 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_
);
711 overflow_button_
->SetBoundsRect(ideal_bounds
.overflow_bounds
);
714 void ShelfView::UpdateAllButtonsVisibilityInOverflowMode() {
715 // The overflow button is not shown in overflow mode.
716 overflow_button_
->SetVisible(false);
717 int last_button_index
= model_
->FirstPanelIndex() - 1;
718 DCHECK_LT(last_visible_index_
, view_model_
->view_size());
719 for (int i
= 0; i
< view_model_
->view_size(); ++i
) {
720 bool visible
= i
>= first_visible_index_
&&
721 i
<= last_visible_index_
;
722 if (!ash::switches::UseAlternateShelfLayout())
723 visible
&= i
!= last_button_index
;
725 // To track the dragging of |drag_view_| continuously, its visibility
726 // should be always true regardless of its position.
727 if (dragged_off_from_overflow_to_shelf_
&&
728 view_model_
->view_at(i
) == drag_view_
)
729 view_model_
->view_at(i
)->SetVisible(true);
731 view_model_
->view_at(i
)->SetVisible(visible
);
735 void ShelfView::CalculateIdealBounds(IdealBounds
* bounds
) {
736 int available_size
= layout_manager_
->PrimaryAxisValue(width(), height());
737 DCHECK(model_
->item_count() == view_model_
->view_size());
741 int first_panel_index
= model_
->FirstPanelIndex();
742 int last_button_index
= first_panel_index
- 1;
744 // Initial x,y values account both leading_inset in primary
745 // coordinate and secondary coordinate based on the dynamic edge of the
746 // shelf (eg top edge on bottom-aligned shelf).
747 int inset
= ash::switches::UseAlternateShelfLayout() ? 0 : leading_inset_
;
748 int x
= layout_manager_
->SelectValueForShelfAlignment(inset
, 0, 0, inset
);
749 int y
= layout_manager_
->SelectValueForShelfAlignment(0, inset
, inset
, 0);
751 int button_size
= GetButtonSize();
752 int button_spacing
= GetButtonSpacing();
754 int w
= layout_manager_
->PrimaryAxisValue(button_size
, width());
755 int h
= layout_manager_
->PrimaryAxisValue(height(), button_size
);
756 for (int i
= 0; i
< view_model_
->view_size(); ++i
) {
757 if (i
< first_visible_index_
) {
758 view_model_
->set_ideal_bounds(i
, gfx::Rect(x
, y
, 0, 0));
762 view_model_
->set_ideal_bounds(i
, gfx::Rect(x
, y
, w
, h
));
763 if (i
!= last_button_index
) {
764 x
= layout_manager_
->PrimaryAxisValue(x
+ w
+ button_spacing
, x
);
765 y
= layout_manager_
->PrimaryAxisValue(y
, y
+ h
+ button_spacing
);
769 if (is_overflow_mode()) {
770 UpdateAllButtonsVisibilityInOverflowMode();
774 // To address Fitt's law, we make the first shelf button include the
775 // leading inset (if there is one).
776 if (!ash::switches::UseAlternateShelfLayout()) {
777 if (view_model_
->view_size() > 0) {
778 view_model_
->set_ideal_bounds(0, gfx::Rect(gfx::Size(
779 layout_manager_
->PrimaryAxisValue(inset
+ w
, w
),
780 layout_manager_
->PrimaryAxisValue(h
, inset
+ h
))));
784 // Right aligned icons.
785 int end_position
= available_size
- button_spacing
;
786 x
= layout_manager_
->PrimaryAxisValue(end_position
, 0);
787 y
= layout_manager_
->PrimaryAxisValue(0, end_position
);
788 for (int i
= view_model_
->view_size() - 1;
789 i
>= first_panel_index
; --i
) {
790 x
= layout_manager_
->PrimaryAxisValue(x
- w
- button_spacing
, x
);
791 y
= layout_manager_
->PrimaryAxisValue(y
, y
- h
- button_spacing
);
792 view_model_
->set_ideal_bounds(i
, gfx::Rect(x
, y
, w
, h
));
793 end_position
= layout_manager_
->PrimaryAxisValue(x
, y
);
796 // Icons on the left / top are guaranteed up to kLeftIconProportion of
797 // the available space.
798 int last_icon_position
= layout_manager_
->PrimaryAxisValue(
799 view_model_
->ideal_bounds(last_button_index
).right(),
800 view_model_
->ideal_bounds(last_button_index
).bottom())
801 + button_size
+ inset
;
802 if (!ash::switches::UseAlternateShelfLayout())
803 last_icon_position
+= button_size
;
804 int reserved_icon_space
= available_size
* kReservedNonPanelIconProportion
;
805 if (last_icon_position
< reserved_icon_space
)
806 end_position
= last_icon_position
;
808 end_position
= std::max(end_position
, reserved_icon_space
);
810 bounds
->overflow_bounds
.set_size(
811 gfx::Size(layout_manager_
->PrimaryAxisValue(w
, width()),
812 layout_manager_
->PrimaryAxisValue(height(), h
)));
814 if (ash::switches::UseAlternateShelfLayout()) {
815 last_visible_index_
= DetermineLastVisibleIndex(
816 end_position
- button_size
);
818 last_visible_index_
= DetermineLastVisibleIndex(
819 end_position
- inset
- 2 * button_size
);
821 last_hidden_index_
= DetermineFirstVisiblePanelIndex(end_position
) - 1;
823 ((ash::switches::UseAlternateShelfLayout() ? 0 : 1) +
824 last_visible_index_
< last_button_index
||
825 last_hidden_index_
>= first_panel_index
);
827 // Create Space for the overflow button
828 if (show_overflow
&& ash::switches::UseAlternateShelfLayout() &&
829 last_visible_index_
> 0 && last_visible_index_
< last_button_index
)
830 --last_visible_index_
;
831 for (int i
= 0; i
< view_model_
->view_size(); ++i
) {
832 bool visible
= i
<= last_visible_index_
|| i
> last_hidden_index_
;
833 // Always show the app list.
834 if (!ash::switches::UseAlternateShelfLayout())
835 visible
|= (i
== last_button_index
);
836 // To receive drag event continously from |drag_view_| during the dragging
837 // off from the shelf, don't make |drag_view_| invisible. It will be
838 // eventually invisible and removed from the |view_model_| by
839 // FinalizeRipOffDrag().
840 if (dragged_off_shelf_
&& view_model_
->view_at(i
) == drag_view_
)
842 view_model_
->view_at(i
)->SetVisible(visible
);
845 overflow_button_
->SetVisible(show_overflow
);
847 DCHECK_NE(0, view_model_
->view_size());
848 if (last_visible_index_
== -1) {
849 x
= layout_manager_
->SelectValueForShelfAlignment(inset
, 0, 0, inset
);
850 y
= layout_manager_
->SelectValueForShelfAlignment(0, inset
, inset
, 0);
851 } else if (last_visible_index_
== last_button_index
852 && !ash::switches::UseAlternateShelfLayout()) {
853 x
= view_model_
->ideal_bounds(last_visible_index_
).x();
854 y
= view_model_
->ideal_bounds(last_visible_index_
).y();
856 x
= layout_manager_
->PrimaryAxisValue(
857 view_model_
->ideal_bounds(last_visible_index_
).right(),
858 view_model_
->ideal_bounds(last_visible_index_
).x());
859 y
= layout_manager_
->PrimaryAxisValue(
860 view_model_
->ideal_bounds(last_visible_index_
).y(),
861 view_model_
->ideal_bounds(last_visible_index_
).bottom());
863 // Set all hidden panel icon positions to be on the overflow button.
864 for (int i
= first_panel_index
; i
<= last_hidden_index_
; ++i
)
865 view_model_
->set_ideal_bounds(i
, gfx::Rect(x
, y
, w
, h
));
867 // Add more space between last visible item and overflow button.
868 // Without this, two buttons look too close compared with other items.
869 if (ash::switches::UseAlternateShelfLayout()) {
870 x
= layout_manager_
->PrimaryAxisValue(x
+ button_spacing
, x
);
871 y
= layout_manager_
->PrimaryAxisValue(y
, y
+ button_spacing
);
874 bounds
->overflow_bounds
.set_x(x
);
875 bounds
->overflow_bounds
.set_y(y
);
876 if (!ash::switches::UseAlternateShelfLayout()) {
877 // Position app list after overflow button.
878 gfx::Rect app_list_bounds
= view_model_
->ideal_bounds(last_button_index
);
880 x
= layout_manager_
->PrimaryAxisValue(x
+ w
+ button_spacing
, x
);
881 y
= layout_manager_
->PrimaryAxisValue(y
, y
+ h
+ button_spacing
);
882 app_list_bounds
.set_x(x
);
883 app_list_bounds
.set_y(y
);
884 view_model_
->set_ideal_bounds(last_button_index
, app_list_bounds
);
886 if (overflow_bubble_
.get() && overflow_bubble_
->IsShowing())
887 UpdateOverflowRange(overflow_bubble_
->shelf_view());
889 if (overflow_bubble_
)
890 overflow_bubble_
->Hide();
894 int ShelfView::DetermineLastVisibleIndex(int max_value
) const {
895 int index
= model_
->FirstPanelIndex() - 1;
897 layout_manager_
->PrimaryAxisValue(
898 view_model_
->ideal_bounds(index
).right(),
899 view_model_
->ideal_bounds(index
).bottom()) > max_value
) {
905 int ShelfView::DetermineFirstVisiblePanelIndex(int min_value
) const {
906 int index
= model_
->FirstPanelIndex();
907 while (index
< view_model_
->view_size() &&
908 layout_manager_
->PrimaryAxisValue(
909 view_model_
->ideal_bounds(index
).right(),
910 view_model_
->ideal_bounds(index
).bottom()) < min_value
) {
916 void ShelfView::AddIconObserver(ShelfIconObserver
* observer
) {
917 observers_
.AddObserver(observer
);
920 void ShelfView::RemoveIconObserver(ShelfIconObserver
* observer
) {
921 observers_
.RemoveObserver(observer
);
924 void ShelfView::AnimateToIdealBounds() {
925 IdealBounds ideal_bounds
;
926 CalculateIdealBounds(&ideal_bounds
);
927 for (int i
= 0; i
< view_model_
->view_size(); ++i
) {
928 View
* view
= view_model_
->view_at(i
);
929 bounds_animator_
->AnimateViewTo(view
, view_model_
->ideal_bounds(i
));
930 // Now that the item animation starts, we have to make sure that the
931 // padding of the first gets properly transferred to the new first item.
932 if (i
&& view
->border())
933 view
->SetBorder(views::Border::NullBorder());
934 else if (!i
&& !view
->border())
935 UpdateFirstButtonPadding();
937 overflow_button_
->SetBoundsRect(ideal_bounds
.overflow_bounds
);
940 views::View
* ShelfView::CreateViewForItem(const ShelfItem
& item
) {
941 views::View
* view
= NULL
;
943 case TYPE_BROWSER_SHORTCUT
:
944 case TYPE_APP_SHORTCUT
:
945 case TYPE_WINDOWED_APP
:
946 case TYPE_PLATFORM_APP
:
948 case TYPE_APP_PANEL
: {
949 ShelfButton
* button
= ShelfButton::Create(this, this, layout_manager_
);
950 button
->SetImage(item
.image
);
951 ReflectItemStatus(item
, button
);
956 case TYPE_APP_LIST
: {
957 if (ash::switches::UseAlternateShelfLayout()) {
958 view
= new AlternateAppListButton(this,
960 layout_manager_
->shelf_widget());
962 // TODO(dave): turn this into a ShelfButton too.
963 AppListButton
* button
= new AppListButton(this, this);
964 button
->SetImageAlignment(
965 layout_manager_
->SelectValueForShelfAlignment(
966 views::ImageButton::ALIGN_CENTER
,
967 views::ImageButton::ALIGN_LEFT
,
968 views::ImageButton::ALIGN_RIGHT
,
969 views::ImageButton::ALIGN_CENTER
),
970 layout_manager_
->SelectValueForShelfAlignment(
971 views::ImageButton::ALIGN_TOP
,
972 views::ImageButton::ALIGN_MIDDLE
,
973 views::ImageButton::ALIGN_MIDDLE
,
974 views::ImageButton::ALIGN_BOTTOM
));
983 view
->set_context_menu_controller(this);
986 ConfigureChildView(view
);
990 void ShelfView::FadeIn(views::View
* view
) {
991 view
->SetVisible(true);
992 view
->layer()->SetOpacity(0);
993 AnimateToIdealBounds();
994 bounds_animator_
->SetAnimationDelegate(
995 view
, new FadeInAnimationDelegate(view
), true);
998 void ShelfView::PrepareForDrag(Pointer pointer
, const ui::LocatedEvent
& event
) {
1001 drag_pointer_
= pointer
;
1002 start_drag_index_
= view_model_
->GetIndexOfView(drag_view_
);
1004 if (start_drag_index_
== -1) {
1009 // If the item is no longer draggable, bail out.
1010 ShelfItemDelegate
* item_delegate
= item_manager_
->GetShelfItemDelegate(
1011 model_
->items()[start_drag_index_
].id
);
1012 if (!item_delegate
->IsDraggable()) {
1017 // Move the view to the front so that it appears on top of other views.
1018 ReorderChildView(drag_view_
, -1);
1019 bounds_animator_
->StopAnimatingView(drag_view_
);
1022 void ShelfView::ContinueDrag(const ui::LocatedEvent
& event
) {
1023 // Due to a syncing operation the application might have been removed.
1024 // Bail if it is gone.
1025 int current_index
= view_model_
->GetIndexOfView(drag_view_
);
1026 DCHECK_NE(-1, current_index
);
1028 ShelfItemDelegate
* item_delegate
= item_manager_
->GetShelfItemDelegate(
1029 model_
->items()[current_index
].id
);
1030 if (!item_delegate
->IsDraggable()) {
1035 // If this is not a drag and drop host operation and not the app list item,
1036 // check if the item got ripped off the shelf - if it did we are done.
1037 if (!drag_and_drop_shelf_id_
&&
1038 RemovableByRipOff(current_index
) != NOT_REMOVABLE
) {
1039 if (HandleRipOffDrag(event
))
1041 // The rip off handler could have changed the location of the item.
1042 current_index
= view_model_
->GetIndexOfView(drag_view_
);
1045 // TODO: I don't think this works correctly with RTL.
1046 gfx::Point
drag_point(event
.location());
1047 ConvertPointToTarget(drag_view_
, this, &drag_point
);
1049 // Constrain the location to the range of valid indices for the type.
1050 std::pair
<int, int> indices(GetDragRange(current_index
));
1051 int first_drag_index
= indices
.first
;
1052 int last_drag_index
= indices
.second
;
1053 // If the last index isn't valid, we're overflowing. Constrain to the app list
1054 // (which is the last visible item).
1055 if (first_drag_index
< model_
->FirstPanelIndex() &&
1056 last_drag_index
> last_visible_index_
)
1057 last_drag_index
= last_visible_index_
;
1059 if (layout_manager_
->IsHorizontalAlignment()) {
1060 x
= std::max(view_model_
->ideal_bounds(indices
.first
).x(),
1061 drag_point
.x() - drag_offset_
);
1062 x
= std::min(view_model_
->ideal_bounds(last_drag_index
).right() -
1063 view_model_
->ideal_bounds(current_index
).width(),
1065 if (drag_view_
->x() == x
)
1067 drag_view_
->SetX(x
);
1069 y
= std::max(view_model_
->ideal_bounds(indices
.first
).y(),
1070 drag_point
.y() - drag_offset_
);
1071 y
= std::min(view_model_
->ideal_bounds(last_drag_index
).bottom() -
1072 view_model_
->ideal_bounds(current_index
).height(),
1074 if (drag_view_
->y() == y
)
1076 drag_view_
->SetY(y
);
1080 views::ViewModelUtils::DetermineMoveIndex(
1081 *view_model_
, drag_view_
,
1082 layout_manager_
->IsHorizontalAlignment() ?
1083 views::ViewModelUtils::HORIZONTAL
:
1084 views::ViewModelUtils::VERTICAL
,
1087 std::min(indices
.second
, std::max(target_index
, indices
.first
));
1088 if (target_index
== current_index
)
1091 // Change the model, the ShelfItemMoved() callback will handle the
1092 // |view_model_| update.
1093 model_
->Move(current_index
, target_index
);
1094 bounds_animator_
->StopAnimatingView(drag_view_
);
1097 bool ShelfView::HandleRipOffDrag(const ui::LocatedEvent
& event
) {
1098 int current_index
= view_model_
->GetIndexOfView(drag_view_
);
1099 DCHECK_NE(-1, current_index
);
1100 std::string dragged_app_id
=
1101 delegate_
->GetAppIDForShelfID(model_
->items()[current_index
].id
);
1103 gfx::Point screen_location
= event
.root_location();
1104 ash::wm::ConvertPointToScreen(GetWidget()->GetNativeWindow()->GetRootWindow(),
1107 // To avoid ugly forwards and backwards flipping we use different constants
1108 // for ripping off / re-inserting the items.
1109 if (dragged_off_shelf_
) {
1110 // If the shelf/overflow bubble bounds contains |screen_location| we insert
1111 // the item back into the shelf.
1112 if (GetBoundsForDragInsertInScreen().Contains(screen_location
)) {
1113 if (dragged_off_from_overflow_to_shelf_
) {
1114 // During the dragging an item from Shelf to Overflow, it can enter here
1115 // directly because both are located very closly.
1116 main_shelf_
->EndDrag(true);
1117 // Stops the animation of |drag_view_| and sets its bounds explicitly
1118 // becase ContinueDrag() stops its animation. Without this, unexpected
1119 // bounds will be set.
1120 bounds_animator_
->StopAnimatingView(drag_view_
);
1121 int drag_view_index
= view_model_
->GetIndexOfView(drag_view_
);
1122 drag_view_
->SetBoundsRect(view_model_
->ideal_bounds(drag_view_index
));
1123 dragged_off_from_overflow_to_shelf_
= false;
1125 // Destroy our proxy view item.
1126 DestroyDragIconProxy();
1127 // Re-insert the item and return simply false since the caller will handle
1128 // the move as in any normal case.
1129 dragged_off_shelf_
= false;
1130 drag_view_
->layer()->SetOpacity(1.0f
);
1131 // The size of Overflow bubble should be updated immediately when an item
1133 if (is_overflow_mode())
1134 PreferredSizeChanged();
1136 } else if (is_overflow_mode() &&
1137 main_shelf_
->GetBoundsForDragInsertInScreen().Contains(
1139 if (!dragged_off_from_overflow_to_shelf_
) {
1140 dragged_off_from_overflow_to_shelf_
= true;
1141 drag_image_
->SetOpacity(1.0f
);
1142 main_shelf_
->StartDrag(dragged_app_id
, screen_location
);
1144 main_shelf_
->Drag(screen_location
);
1146 } else if (dragged_off_from_overflow_to_shelf_
) {
1147 // Makes the |drag_image_| partially disappear again.
1148 dragged_off_from_overflow_to_shelf_
= false;
1149 drag_image_
->SetOpacity(kDraggedImageOpacity
);
1150 main_shelf_
->EndDrag(true);
1151 bounds_animator_
->StopAnimatingView(drag_view_
);
1152 int drag_view_index
= view_model_
->GetIndexOfView(drag_view_
);
1153 drag_view_
->SetBoundsRect(view_model_
->ideal_bounds(drag_view_index
));
1155 // Move our proxy view item.
1156 UpdateDragIconProxy(screen_location
);
1159 // Check if we are too far away from the shelf to enter the ripped off state.
1160 // Determine the distance to the shelf.
1161 int delta
= CalculateShelfDistance(screen_location
);
1162 if (delta
> kRipOffDistance
) {
1163 // Create a proxy view item which can be moved anywhere.
1164 ShelfButton
* button
= static_cast<ShelfButton
*>(drag_view_
);
1165 CreateDragIconProxy(event
.root_location(),
1168 gfx::Vector2d(0, 0),
1169 kDragAndDropProxyScale
);
1170 drag_view_
->layer()->SetOpacity(0.0f
);
1171 dragged_off_shelf_
= true;
1172 if (RemovableByRipOff(current_index
) == REMOVABLE
) {
1173 // Move the item to the front of the first panel item and hide it.
1174 // ShelfItemMoved() callback will handle the |view_model_| update and
1175 // call AnimateToIdealBounds().
1176 if (current_index
!= model_
->FirstPanelIndex() - 1) {
1177 model_
->Move(current_index
, model_
->FirstPanelIndex() - 1);
1178 StartFadeInLastVisibleItem();
1179 } else if (is_overflow_mode()) {
1180 // Overflow bubble should be shrunk when an item is ripped off.
1181 PreferredSizeChanged();
1183 // Make the item partially disappear to show that it will get removed if
1185 drag_image_
->SetOpacity(kDraggedImageOpacity
);
1192 void ShelfView::FinalizeRipOffDrag(bool cancel
) {
1193 if (!dragged_off_shelf_
)
1195 // Make sure we do not come in here again.
1196 dragged_off_shelf_
= false;
1198 // Coming here we should always have a |drag_view_|.
1200 int current_index
= view_model_
->GetIndexOfView(drag_view_
);
1201 // If the view isn't part of the model anymore (|current_index| == -1), a sync
1202 // operation must have removed it. In that case we shouldn't change the model
1203 // and only delete the proxy image.
1204 if (current_index
== -1) {
1205 DestroyDragIconProxy();
1209 // Set to true when the animation should snap back to where it was before.
1210 bool snap_back
= false;
1211 // Items which cannot be dragged off will be handled as a cancel.
1213 if (dragged_off_from_overflow_to_shelf_
) {
1214 dragged_off_from_overflow_to_shelf_
= false;
1215 main_shelf_
->EndDrag(false);
1216 drag_view_
->layer()->SetOpacity(1.0f
);
1217 } else if (RemovableByRipOff(current_index
) != REMOVABLE
) {
1218 // Make sure we do not try to remove un-removable items like items which
1219 // were not pinned or have to be always there.
1223 // Make sure the item stays invisible upon removal.
1224 drag_view_
->SetVisible(false);
1225 std::string app_id
=
1226 delegate_
->GetAppIDForShelfID(model_
->items()[current_index
].id
);
1227 delegate_
->UnpinAppWithID(app_id
);
1230 if (cancel
|| snap_back
) {
1231 if (dragged_off_from_overflow_to_shelf_
) {
1232 dragged_off_from_overflow_to_shelf_
= false;
1233 // Main shelf handles revert of dragged item.
1234 main_shelf_
->EndDrag(true);
1235 drag_view_
->layer()->SetOpacity(1.0f
);
1236 } else if (!cancelling_drag_model_changed_
) {
1237 // Only do something if the change did not come through a model change.
1238 gfx::Rect drag_bounds
= drag_image_
->GetBoundsInScreen();
1239 gfx::Point relative_to
= GetBoundsInScreen().origin();
1241 gfx::PointAtOffsetFromOrigin(drag_bounds
.origin()- relative_to
),
1242 drag_bounds
.size());
1243 drag_view_
->SetBoundsRect(target
);
1244 // Hide the status from the active item since we snap it back now. Upon
1245 // animation end the flag gets cleared if |snap_back_from_rip_off_view_|
1247 snap_back_from_rip_off_view_
= drag_view_
;
1248 ShelfButton
* button
= static_cast<ShelfButton
*>(drag_view_
);
1249 button
->AddState(ShelfButton::STATE_HIDDEN
);
1250 // When a canceling drag model is happening, the view model is diverged
1251 // from the menu model and movements / animations should not be done.
1252 model_
->Move(current_index
, start_drag_index_
);
1253 AnimateToIdealBounds();
1255 drag_view_
->layer()->SetOpacity(1.0f
);
1257 DestroyDragIconProxy();
1260 ShelfView::RemovableState
ShelfView::RemovableByRipOff(int index
) {
1261 DCHECK(index
>= 0 && index
< model_
->item_count());
1262 ShelfItemType type
= model_
->items()[index
].type
;
1263 if (type
== TYPE_APP_LIST
|| type
== TYPE_DIALOG
|| !delegate_
->CanPin())
1264 return NOT_REMOVABLE
;
1266 std::string app_id
= delegate_
->GetAppIDForShelfID(model_
->items()[index
].id
);
1267 // Note: Only pinned app shortcuts can be removed!
1268 return (type
== TYPE_APP_SHORTCUT
&& delegate_
->IsAppPinned(app_id
)) ?
1269 REMOVABLE
: DRAGGABLE
;
1272 bool ShelfView::SameDragType(ShelfItemType typea
, ShelfItemType typeb
) const {
1274 case TYPE_APP_SHORTCUT
:
1275 case TYPE_BROWSER_SHORTCUT
:
1276 return (typeb
== TYPE_APP_SHORTCUT
|| typeb
== TYPE_BROWSER_SHORTCUT
);
1278 case TYPE_PLATFORM_APP
:
1279 case TYPE_WINDOWED_APP
:
1280 case TYPE_APP_PANEL
:
1282 return typeb
== typea
;
1283 case TYPE_UNDEFINED
:
1284 NOTREACHED() << "ShelfItemType must be set.";
1291 std::pair
<int, int> ShelfView::GetDragRange(int index
) {
1294 ShelfItemType type
= model_
->items()[index
].type
;
1295 for (int i
= 0; i
< model_
->item_count(); ++i
) {
1296 if (SameDragType(model_
->items()[i
].type
, type
)) {
1297 if (min_index
== -1)
1302 return std::pair
<int, int>(min_index
, max_index
);
1305 void ShelfView::ConfigureChildView(views::View
* view
) {
1306 view
->SetPaintToLayer(true);
1307 view
->layer()->SetFillsBoundsOpaquely(false);
1310 void ShelfView::ToggleOverflowBubble() {
1311 if (IsShowingOverflowBubble()) {
1312 overflow_bubble_
->Hide();
1316 if (!overflow_bubble_
)
1317 overflow_bubble_
.reset(new OverflowBubble());
1319 ShelfView
* overflow_view
=
1320 new ShelfView(model_
, delegate_
, layout_manager_
);
1321 overflow_view
->overflow_mode_
= true;
1322 overflow_view
->Init();
1323 overflow_view
->set_owner_overflow_bubble(overflow_bubble_
.get());
1324 overflow_view
->OnShelfAlignmentChanged();
1325 overflow_view
->main_shelf_
= this;
1326 UpdateOverflowRange(overflow_view
);
1328 overflow_bubble_
->Show(overflow_button_
, overflow_view
);
1330 Shell::GetInstance()->UpdateShelfVisibility();
1333 void ShelfView::UpdateFirstButtonPadding() {
1334 if (ash::switches::UseAlternateShelfLayout())
1337 // Creates an empty border for first shelf button to make included leading
1338 // inset act as the button's padding. This is only needed on button creation
1339 // and when shelf alignment changes.
1340 if (view_model_
->view_size() > 0) {
1341 view_model_
->view_at(0)->SetBorder(views::Border::CreateEmptyBorder(
1342 layout_manager_
->PrimaryAxisValue(0, leading_inset_
),
1343 layout_manager_
->PrimaryAxisValue(leading_inset_
, 0),
1349 void ShelfView::OnFadeOutAnimationEnded() {
1350 AnimateToIdealBounds();
1351 StartFadeInLastVisibleItem();
1354 void ShelfView::StartFadeInLastVisibleItem() {
1355 // If overflow button is visible and there is a valid new last item, fading
1356 // the new last item in after sliding animation is finished.
1357 if (overflow_button_
->visible() && last_visible_index_
>= 0) {
1358 views::View
* last_visible_view
= view_model_
->view_at(last_visible_index_
);
1359 last_visible_view
->layer()->SetOpacity(0);
1360 bounds_animator_
->SetAnimationDelegate(
1362 new ShelfView::StartFadeAnimationDelegate(this, last_visible_view
),
1367 void ShelfView::UpdateOverflowRange(ShelfView
* overflow_view
) {
1368 const int first_overflow_index
= last_visible_index_
+ 1;
1369 const int last_overflow_index
= last_hidden_index_
;
1370 DCHECK_LE(first_overflow_index
, last_overflow_index
);
1371 DCHECK_LT(last_overflow_index
, view_model_
->view_size());
1373 overflow_view
->first_visible_index_
= first_overflow_index
;
1374 overflow_view
->last_visible_index_
= last_overflow_index
;
1377 int ShelfView::GetButtonSize() const {
1378 return ash::switches::UseAlternateShelfLayout() ?
1379 kButtonSize
: kShelfPreferredSize
;
1382 int ShelfView::GetButtonSpacing() const {
1383 return ash::switches::UseAlternateShelfLayout() ?
1384 kAlternateButtonSpacing
: kButtonSpacing
;
1387 bool ShelfView::ShouldHideTooltip(const gfx::Point
& cursor_location
) {
1388 gfx::Rect active_bounds
;
1390 for (int i
= 0; i
< child_count(); ++i
) {
1391 views::View
* child
= child_at(i
);
1392 if (child
== overflow_button_
)
1394 if (!ShouldShowTooltipForView(child
))
1397 gfx::Rect child_bounds
= child
->GetMirroredBounds();
1398 active_bounds
.Union(child_bounds
);
1401 return !active_bounds
.Contains(cursor_location
);
1404 gfx::Rect
ShelfView::GetVisibleItemsBoundsInScreen() {
1405 gfx::Size preferred_size
= GetPreferredSize();
1406 gfx::Point
origin(GetMirroredXWithWidthInView(0, preferred_size
.width()), 0);
1407 ConvertPointToScreen(this, &origin
);
1408 return gfx::Rect(origin
, preferred_size
);
1411 gfx::Rect
ShelfView::GetBoundsForDragInsertInScreen() {
1412 gfx::Size preferred_size
;
1413 if (is_overflow_mode()) {
1414 DCHECK(owner_overflow_bubble_
);
1415 gfx::Rect bubble_bounds
=
1416 owner_overflow_bubble_
->bubble_view()->GetBubbleBounds();
1417 preferred_size
= bubble_bounds
.size();
1419 const int preferred_shelf_size
= layout_manager_
->GetPreferredShelfSize();
1421 const int last_button_index
= view_model_
->view_size() - 1;
1422 gfx::Rect last_button_bounds
=
1423 view_model_
->view_at(last_button_index
)->bounds();
1424 if (overflow_button_
->visible() &&
1425 model_
->GetItemIndexForType(TYPE_APP_PANEL
) == -1) {
1426 // When overflow button is visible and shelf has no panel items,
1427 // last_button_bounds should be overflow button's bounds.
1428 last_button_bounds
= overflow_button_
->bounds();
1431 if (layout_manager_
->IsHorizontalAlignment()) {
1432 preferred_size
= gfx::Size(last_button_bounds
.right() + leading_inset_
,
1433 preferred_shelf_size
);
1435 preferred_size
= gfx::Size(preferred_shelf_size
,
1436 last_button_bounds
.bottom() + leading_inset_
);
1439 gfx::Point
origin(GetMirroredXWithWidthInView(0, preferred_size
.width()), 0);
1441 // In overflow mode, we should use OverflowBubbleView as a source for
1442 // converting |origin| to screen coordinates. When a scroll operation is
1443 // occurred in OverflowBubble, the bounds of ShelfView in OverflowBubble can
1445 if (is_overflow_mode())
1446 ConvertPointToScreen(owner_overflow_bubble_
->bubble_view(), &origin
);
1448 ConvertPointToScreen(this, &origin
);
1450 return gfx::Rect(origin
, preferred_size
);
1453 int ShelfView::CancelDrag(int modified_index
) {
1454 FinalizeRipOffDrag(true);
1456 return modified_index
;
1457 bool was_dragging
= dragging();
1458 int drag_view_index
= view_model_
->GetIndexOfView(drag_view_
);
1459 drag_pointer_
= NONE
;
1461 if (drag_view_index
== modified_index
) {
1462 // The view that was being dragged is being modified. Don't do anything.
1463 return modified_index
;
1466 return modified_index
;
1468 // Restore previous position, tracking the position of the modified view.
1469 bool at_end
= modified_index
== view_model_
->view_size();
1470 views::View
* modified_view
=
1471 (modified_index
>= 0 && !at_end
) ?
1472 view_model_
->view_at(modified_index
) : NULL
;
1473 model_
->Move(drag_view_index
, start_drag_index_
);
1475 // If the modified view will be at the end of the list, return the new end of
1478 return view_model_
->view_size();
1479 return modified_view
? view_model_
->GetIndexOfView(modified_view
) : -1;
1482 gfx::Size
ShelfView::GetPreferredSize() {
1483 IdealBounds ideal_bounds
;
1484 CalculateIdealBounds(&ideal_bounds
);
1486 const int preferred_size
= layout_manager_
->GetPreferredShelfSize();
1488 int last_button_index
= is_overflow_mode() ?
1489 last_visible_index_
: view_model_
->view_size() - 1;
1491 // When an item is dragged off from the overflow bubble, it is moved to last
1492 // position and and changed to invisible. Overflow bubble size should be
1493 // shrunk to fit only for visible items.
1494 // If |dragged_off_from_overflow_to_shelf_| is set, there will be no invisible
1495 // items in the shelf.
1496 if (is_overflow_mode() &&
1497 dragged_off_shelf_
&&
1498 !dragged_off_from_overflow_to_shelf_
&&
1499 RemovableByRipOff(view_model_
->GetIndexOfView(drag_view_
)) == REMOVABLE
)
1500 last_button_index
--;
1502 const gfx::Rect last_button_bounds
=
1503 last_button_index
>= first_visible_index_
?
1504 view_model_
->ideal_bounds(last_button_index
) :
1505 gfx::Rect(gfx::Size(preferred_size
, preferred_size
));
1507 if (layout_manager_
->IsHorizontalAlignment()) {
1508 return gfx::Size(last_button_bounds
.right() + leading_inset_
,
1512 return gfx::Size(preferred_size
,
1513 last_button_bounds
.bottom() + leading_inset_
);
1516 void ShelfView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
1517 // This bounds change is produced by the shelf movement and all content has
1518 // to follow. Using an animation at that time would produce a time lag since
1519 // the animation of the BoundsAnimator has itself a delay before it arrives
1520 // at the required location. As such we tell the animator to go there
1522 BoundsAnimatorDisabler
disabler(bounds_animator_
.get());
1523 LayoutToIdealBounds();
1524 FOR_EACH_OBSERVER(ShelfIconObserver
, observers_
,
1525 OnShelfIconPositionsChanged());
1527 if (IsShowingOverflowBubble())
1528 overflow_bubble_
->Hide();
1531 views::FocusTraversable
* ShelfView::GetPaneFocusTraversable() {
1535 void ShelfView::GetAccessibleState(ui::AXViewState
* state
) {
1536 state
->role
= ui::AX_ROLE_TOOLBAR
;
1537 state
->name
= l10n_util::GetStringUTF16(IDS_ASH_SHELF_ACCESSIBLE_NAME
);
1540 void ShelfView::OnGestureEvent(ui::GestureEvent
* event
) {
1541 if (gesture_handler_
.ProcessGestureEvent(*event
))
1542 event
->StopPropagation();
1545 void ShelfView::ShelfItemAdded(int model_index
) {
1547 base::AutoReset
<bool> cancelling_drag(
1548 &cancelling_drag_model_changed_
, true);
1549 model_index
= CancelDrag(model_index
);
1551 views::View
* view
= CreateViewForItem(model_
->items()[model_index
]);
1553 // Hide the view, it'll be made visible when the animation is done. Using
1554 // opacity 0 here to avoid messing with CalculateIdealBounds which touches
1555 // the view's visibility.
1556 view
->layer()->SetOpacity(0);
1557 view_model_
->Add(view
, model_index
);
1559 // Give the button its ideal bounds. That way if we end up animating the
1560 // button before this animation completes it doesn't appear at some random
1561 // spot (because it was in the middle of animating from 0,0 0x0 to its
1563 IdealBounds ideal_bounds
;
1564 CalculateIdealBounds(&ideal_bounds
);
1565 view
->SetBoundsRect(view_model_
->ideal_bounds(model_index
));
1567 // The first animation moves all the views to their target position. |view|
1568 // is hidden, so it visually appears as though we are providing space for
1569 // it. When done we'll fade the view in.
1570 AnimateToIdealBounds();
1571 if (model_index
<= last_visible_index_
||
1572 model_index
>= model_
->FirstPanelIndex()) {
1573 bounds_animator_
->SetAnimationDelegate(
1574 view
, new StartFadeAnimationDelegate(this, view
), true);
1576 // Undo the hiding if animation does not run.
1577 view
->layer()->SetOpacity(1.0f
);
1581 void ShelfView::ShelfItemRemoved(int model_index
, ShelfID id
) {
1582 if (id
== context_menu_id_
)
1583 launcher_menu_runner_
.reset();
1585 base::AutoReset
<bool> cancelling_drag(
1586 &cancelling_drag_model_changed_
, true);
1587 model_index
= CancelDrag(model_index
);
1589 views::View
* view
= view_model_
->view_at(model_index
);
1590 view_model_
->Remove(model_index
);
1592 // When the overflow bubble is visible, the overflow range needs to be set
1593 // before CalculateIdealBounds() gets called. Otherwise CalculateIdealBounds()
1594 // could trigger a ShelfItemChanged() by hiding the overflow bubble and
1595 // since the overflow bubble is not yet synced with the ShelfModel this
1596 // could cause a crash.
1597 if (overflow_bubble_
&& overflow_bubble_
->IsShowing()) {
1598 last_hidden_index_
= std::min(last_hidden_index_
,
1599 view_model_
->view_size() - 1);
1600 UpdateOverflowRange(overflow_bubble_
->shelf_view());
1603 if (view
->visible()) {
1604 // The first animation fades out the view. When done we'll animate the rest
1605 // of the views to their target location.
1606 bounds_animator_
->AnimateViewTo(view
, view
->bounds());
1607 bounds_animator_
->SetAnimationDelegate(
1608 view
, new FadeOutAnimationDelegate(this, view
), true);
1610 // We don't need to show a fade out animation for invisible |view|. When an
1611 // item is ripped out from the shelf, its |view| is already invisible.
1612 AnimateToIdealBounds();
1615 // Close the tooltip because it isn't needed any longer and its anchor view
1616 // will be deleted soon.
1617 if (tooltip_
->GetCurrentAnchorView() == view
)
1621 void ShelfView::ShelfItemChanged(int model_index
, const ShelfItem
& old_item
) {
1622 const ShelfItem
& item(model_
->items()[model_index
]);
1623 if (old_item
.type
!= item
.type
) {
1624 // Type changed, swap the views.
1625 model_index
= CancelDrag(model_index
);
1626 scoped_ptr
<views::View
> old_view(view_model_
->view_at(model_index
));
1627 bounds_animator_
->StopAnimatingView(old_view
.get());
1628 // Removing and re-inserting a view in our view model will strip the ideal
1629 // bounds from the item. To avoid recalculation of everything the bounds
1630 // get remembered and restored after the insertion to the previous value.
1631 gfx::Rect old_ideal_bounds
= view_model_
->ideal_bounds(model_index
);
1632 view_model_
->Remove(model_index
);
1633 views::View
* new_view
= CreateViewForItem(item
);
1634 AddChildView(new_view
);
1635 view_model_
->Add(new_view
, model_index
);
1636 view_model_
->set_ideal_bounds(model_index
, old_ideal_bounds
);
1637 new_view
->SetBoundsRect(old_view
->bounds());
1641 views::View
* view
= view_model_
->view_at(model_index
);
1642 switch (item
.type
) {
1643 case TYPE_BROWSER_SHORTCUT
:
1644 // Fallthrough for the new Shelf since it needs to show the activation
1646 case TYPE_APP_SHORTCUT
:
1647 case TYPE_WINDOWED_APP
:
1648 case TYPE_PLATFORM_APP
:
1650 case TYPE_APP_PANEL
: {
1651 ShelfButton
* button
= static_cast<ShelfButton
*>(view
);
1652 ReflectItemStatus(item
, button
);
1653 // The browser shortcut is currently not a "real" item and as such the
1654 // the image is bogous as well. We therefore keep the image as is for it.
1655 if (item
.type
!= TYPE_BROWSER_SHORTCUT
)
1656 button
->SetImage(item
.image
);
1657 button
->SchedulePaint();
1666 void ShelfView::ShelfItemMoved(int start_index
, int target_index
) {
1667 view_model_
->Move(start_index
, target_index
);
1668 // When cancelling a drag due to a shelf item being added, the currently
1669 // dragged item is moved back to its initial position. AnimateToIdealBounds
1670 // will be called again when the new item is added to the |view_model_| but
1671 // at this time the |view_model_| is inconsistent with the |model_|.
1672 if (!cancelling_drag_model_changed_
)
1673 AnimateToIdealBounds();
1676 void ShelfView::ShelfStatusChanged() {
1677 if (ash::switches::UseAlternateShelfLayout())
1679 AppListButton
* app_list_button
=
1680 static_cast<AppListButton
*>(GetAppListButtonView());
1681 if (model_
->status() == ShelfModel::STATUS_LOADING
)
1682 app_list_button
->StartLoadingAnimation();
1684 app_list_button
->StopLoadingAnimation();
1687 void ShelfView::PointerPressedOnButton(views::View
* view
,
1689 const ui::LocatedEvent
& event
) {
1693 int index
= view_model_
->GetIndexOfView(view
);
1697 ShelfItemDelegate
* item_delegate
= item_manager_
->GetShelfItemDelegate(
1698 model_
->items()[index
].id
);
1699 if (view_model_
->view_size() <= 1 || !item_delegate
->IsDraggable())
1700 return; // View is being deleted or not draggable, ignore request.
1703 drag_offset_
= layout_manager_
->PrimaryAxisValue(event
.x(), event
.y());
1704 UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage",
1705 layout_manager_
->SelectValueForShelfAlignment(
1706 SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM
,
1707 SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT
,
1708 SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT
,
1710 SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT
);
1713 void ShelfView::PointerDraggedOnButton(views::View
* view
,
1715 const ui::LocatedEvent
& event
) {
1716 // To prepare all drag types (moving an item in the shelf and dragging off),
1717 // we should check the x-axis and y-axis offset.
1718 if (!dragging() && drag_view_
&&
1719 ((abs(event
.x() - drag_offset_
) >= kMinimumDragDistance
) ||
1720 (abs(event
.y() - drag_offset_
) >= kMinimumDragDistance
))) {
1721 PrepareForDrag(pointer
, event
);
1723 if (drag_pointer_
== pointer
)
1724 ContinueDrag(event
);
1727 void ShelfView::PointerReleasedOnButton(views::View
* view
,
1732 } else if (drag_pointer_
== pointer
) {
1733 FinalizeRipOffDrag(false);
1734 drag_pointer_
= NONE
;
1735 AnimateToIdealBounds();
1737 // If the drag pointer is NONE, no drag operation is going on and the
1738 // drag_view can be released.
1739 if (drag_pointer_
== NONE
)
1743 void ShelfView::MouseMovedOverButton(views::View
* view
) {
1744 if (!ShouldShowTooltipForView(view
))
1747 if (!tooltip_
->IsVisible())
1748 tooltip_
->ResetTimer();
1751 void ShelfView::MouseEnteredButton(views::View
* view
) {
1752 if (!ShouldShowTooltipForView(view
))
1755 if (tooltip_
->IsVisible()) {
1756 tooltip_
->ShowImmediately(view
, GetAccessibleName(view
));
1758 tooltip_
->ShowDelayed(view
, GetAccessibleName(view
));
1762 void ShelfView::MouseExitedButton(views::View
* view
) {
1763 if (!tooltip_
->IsVisible())
1764 tooltip_
->StopTimer();
1767 base::string16
ShelfView::GetAccessibleName(const views::View
* view
) {
1768 int view_index
= view_model_
->GetIndexOfView(view
);
1769 // May be -1 while in the process of animating closed.
1770 if (view_index
== -1)
1771 return base::string16();
1773 ShelfItemDelegate
* item_delegate
= item_manager_
->GetShelfItemDelegate(
1774 model_
->items()[view_index
].id
);
1775 return item_delegate
->GetTitle();
1778 void ShelfView::ButtonPressed(views::Button
* sender
, const ui::Event
& event
) {
1779 // Do not handle mouse release during drag.
1783 if (sender
== overflow_button_
) {
1784 ToggleOverflowBubble();
1788 int view_index
= view_model_
->GetIndexOfView(sender
);
1789 // May be -1 while in the process of animating closed.
1790 if (view_index
== -1)
1793 // If the previous menu was closed by the same event as this one, we ignore
1795 if (!IsUsableEvent(event
))
1799 ScopedTargetRootWindow
scoped_target(
1800 sender
->GetWidget()->GetNativeView()->GetRootWindow());
1801 // Slow down activation animations if shift key is pressed.
1802 scoped_ptr
<ui::ScopedAnimationDurationScaleMode
> slowing_animations
;
1803 if (event
.IsShiftDown()) {
1804 slowing_animations
.reset(new ui::ScopedAnimationDurationScaleMode(
1805 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION
));
1808 // Collect usage statistics before we decide what to do with the click.
1809 switch (model_
->items()[view_index
].type
) {
1810 case TYPE_APP_SHORTCUT
:
1811 case TYPE_WINDOWED_APP
:
1812 case TYPE_PLATFORM_APP
:
1813 case TYPE_BROWSER_SHORTCUT
:
1814 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1815 UMA_LAUNCHER_CLICK_ON_APP
);
1819 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1820 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON
);
1823 case TYPE_APP_PANEL
:
1827 case TYPE_UNDEFINED
:
1828 NOTREACHED() << "ShelfItemType must be set.";
1832 ShelfItemDelegate
* item_delegate
=
1833 item_manager_
->GetShelfItemDelegate(model_
->items()[view_index
].id
);
1834 if (!item_delegate
->ItemSelected(event
))
1835 ShowListMenuForView(model_
->items()[view_index
], sender
, event
);
1839 bool ShelfView::ShowListMenuForView(const ShelfItem
& item
,
1840 views::View
* source
,
1841 const ui::Event
& event
) {
1842 scoped_ptr
<ShelfMenuModel
> menu_model
;
1843 ShelfItemDelegate
* item_delegate
=
1844 item_manager_
->GetShelfItemDelegate(item
.id
);
1845 menu_model
.reset(item_delegate
->CreateApplicationMenu(event
.flags()));
1847 // Make sure we have a menu and it has at least two items in addition to the
1848 // application title and the 3 spacing separators.
1849 if (!menu_model
.get() || menu_model
->GetItemCount() <= 5)
1852 ShowMenu(scoped_ptr
<views::MenuModelAdapter
>(
1853 new ShelfMenuModelAdapter(menu_model
.get())),
1857 ui::GetMenuSourceTypeForEvent(event
));
1861 void ShelfView::ShowContextMenuForView(views::View
* source
,
1862 const gfx::Point
& point
,
1863 ui::MenuSourceType source_type
) {
1864 int view_index
= view_model_
->GetIndexOfView(source
);
1865 if (view_index
== -1) {
1866 Shell::GetInstance()->ShowContextMenu(point
, source_type
);
1870 scoped_ptr
<ui::MenuModel
> menu_model
;
1871 ShelfItemDelegate
* item_delegate
= item_manager_
->GetShelfItemDelegate(
1872 model_
->items()[view_index
].id
);
1873 menu_model
.reset(item_delegate
->CreateContextMenu(
1874 source
->GetWidget()->GetNativeView()->GetRootWindow()));
1878 base::AutoReset
<ShelfID
> reseter(
1880 view_index
== -1 ? 0 : model_
->items()[view_index
].id
);
1882 ShowMenu(scoped_ptr
<views::MenuModelAdapter
>(
1883 new views::MenuModelAdapter(menu_model
.get())),
1890 void ShelfView::ShowMenu(scoped_ptr
<views::MenuModelAdapter
> menu_model_adapter
,
1891 views::View
* source
,
1892 const gfx::Point
& click_point
,
1894 ui::MenuSourceType source_type
) {
1895 closing_event_time_
= base::TimeDelta();
1896 launcher_menu_runner_
.reset(
1897 new views::MenuRunner(menu_model_adapter
->CreateMenu()));
1899 ScopedTargetRootWindow
scoped_target(
1900 source
->GetWidget()->GetNativeView()->GetRootWindow());
1902 // Determine the menu alignment dependent on the shelf.
1903 views::MenuItemView::AnchorPosition menu_alignment
=
1904 views::MenuItemView::TOPLEFT
;
1905 gfx::Rect anchor_point
= gfx::Rect(click_point
, gfx::Size());
1907 ShelfWidget
* shelf
= RootWindowController::ForShelf(
1908 GetWidget()->GetNativeView())->shelf();
1909 if (!context_menu
) {
1910 // Application lists use a bubble.
1911 ShelfAlignment align
= shelf
->GetAlignment();
1912 anchor_point
= source
->GetBoundsInScreen();
1914 // It is possible to invoke the menu while it is sliding into view. To cover
1915 // that case, the screen coordinates are offsetted by the animation delta.
1916 gfx::Vector2d offset
=
1917 source
->GetWidget()->GetNativeWindow()->bounds().origin() -
1918 source
->GetWidget()->GetNativeWindow()->GetTargetBounds().origin();
1919 anchor_point
.set_x(anchor_point
.x() - offset
.x());
1920 anchor_point
.set_y(anchor_point
.y() - offset
.y());
1922 // Shelf items can have an asymmetrical border for spacing reasons.
1923 // Adjust anchor location for this.
1924 if (source
->border())
1925 anchor_point
.Inset(source
->border()->GetInsets());
1928 case SHELF_ALIGNMENT_BOTTOM
:
1929 menu_alignment
= views::MenuItemView::BUBBLE_ABOVE
;
1931 case SHELF_ALIGNMENT_LEFT
:
1932 menu_alignment
= views::MenuItemView::BUBBLE_RIGHT
;
1934 case SHELF_ALIGNMENT_RIGHT
:
1935 menu_alignment
= views::MenuItemView::BUBBLE_LEFT
;
1937 case SHELF_ALIGNMENT_TOP
:
1938 menu_alignment
= views::MenuItemView::BUBBLE_BELOW
;
1942 // If this gets deleted while we are in the menu, the shelf will be gone
1944 bool got_deleted
= false;
1945 got_deleted_
= &got_deleted
;
1947 shelf
->ForceUndimming(true);
1948 // NOTE: if you convert to HAS_MNEMONICS be sure and update menu building
1950 if (launcher_menu_runner_
->RunMenuAt(
1951 source
->GetWidget(),
1956 context_menu
? views::MenuRunner::CONTEXT_MENU
: 0) ==
1957 views::MenuRunner::MENU_DELETED
) {
1959 got_deleted_
= NULL
;
1960 shelf
->ForceUndimming(false);
1964 got_deleted_
= NULL
;
1965 shelf
->ForceUndimming(false);
1967 // If it is a context menu and we are showing overflow bubble
1968 // we want to hide overflow bubble.
1969 if (owner_overflow_bubble_
)
1970 owner_overflow_bubble_
->HideBubbleAndRefreshButton();
1972 // Unpinning an item will reset the |launcher_menu_runner_| before coming
1974 if (launcher_menu_runner_
)
1975 closing_event_time_
= launcher_menu_runner_
->closing_event_time();
1976 Shell::GetInstance()->UpdateShelfVisibility();
1979 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator
* animator
) {
1980 FOR_EACH_OBSERVER(ShelfIconObserver
, observers_
,
1981 OnShelfIconPositionsChanged());
1982 PreferredSizeChanged();
1985 void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator
* animator
) {
1986 if (snap_back_from_rip_off_view_
&& animator
== bounds_animator_
) {
1987 if (!animator
->IsAnimating(snap_back_from_rip_off_view_
)) {
1988 // Coming here the animation of the ShelfButton is finished and the
1989 // previously hidden status can be shown again. Since the button itself
1990 // might have gone away or changed locations we check that the button
1991 // is still in the shelf and show its status again.
1992 for (int index
= 0; index
< view_model_
->view_size(); index
++) {
1993 views::View
* view
= view_model_
->view_at(index
);
1994 if (view
== snap_back_from_rip_off_view_
) {
1995 ShelfButton
* button
= static_cast<ShelfButton
*>(view
);
1996 button
->ClearState(ShelfButton::STATE_HIDDEN
);
2000 snap_back_from_rip_off_view_
= NULL
;
2005 bool ShelfView::IsUsableEvent(const ui::Event
& event
) {
2006 if (closing_event_time_
== base::TimeDelta())
2009 base::TimeDelta delta
=
2010 base::TimeDelta(event
.time_stamp() - closing_event_time_
);
2011 closing_event_time_
= base::TimeDelta();
2012 // TODO(skuhne): This time seems excessive, but it appears that the reposting
2013 // takes that long. Need to come up with a better way of doing this.
2014 return (delta
.InMilliseconds() < 0 || delta
.InMilliseconds() > 130);
2017 const ShelfItem
* ShelfView::ShelfItemForView(const views::View
* view
) const {
2018 int view_index
= view_model_
->GetIndexOfView(view
);
2019 if (view_index
== -1)
2021 return &(model_
->items()[view_index
]);
2024 bool ShelfView::ShouldShowTooltipForView(const views::View
* view
) const {
2025 if (view
== GetAppListButtonView() &&
2026 Shell::GetInstance()->GetAppListWindow())
2028 const ShelfItem
* item
= ShelfItemForView(view
);
2031 ShelfItemDelegate
* item_delegate
=
2032 item_manager_
->GetShelfItemDelegate(item
->id
);
2033 return item_delegate
->ShouldShowTooltip();
2036 int ShelfView::CalculateShelfDistance(const gfx::Point
& coordinate
) const {
2037 ShelfWidget
* shelf
= RootWindowController::ForShelf(
2038 GetWidget()->GetNativeView())->shelf();
2039 ShelfAlignment align
= shelf
->GetAlignment();
2040 const gfx::Rect bounds
= GetBoundsInScreen();
2043 case SHELF_ALIGNMENT_BOTTOM
:
2044 distance
= bounds
.y() - coordinate
.y();
2046 case SHELF_ALIGNMENT_LEFT
:
2047 distance
= coordinate
.x() - bounds
.right();
2049 case SHELF_ALIGNMENT_RIGHT
:
2050 distance
= bounds
.x() - coordinate
.x();
2052 case SHELF_ALIGNMENT_TOP
:
2053 distance
= coordinate
.y() - bounds
.bottom();
2056 return distance
> 0 ? distance
: 0;