Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ash / shelf / shelf_view.cc
blob07e812b4ac4eb4f9a5268e0da2a9999b35f5b722
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/shelf/shelf_view.h"
7 #include <algorithm>
9 #include "ash/ash_constants.h"
10 #include "ash/ash_switches.h"
11 #include "ash/drag_drop/drag_image_view.h"
12 #include "ash/metrics/user_metrics_recorder.h"
13 #include "ash/root_window_controller.h"
14 #include "ash/scoped_target_root_window.h"
15 #include "ash/shelf/app_list_button.h"
16 #include "ash/shelf/overflow_bubble.h"
17 #include "ash/shelf/overflow_bubble_view.h"
18 #include "ash/shelf/overflow_button.h"
19 #include "ash/shelf/shelf_button.h"
20 #include "ash/shelf/shelf_constants.h"
21 #include "ash/shelf/shelf_delegate.h"
22 #include "ash/shelf/shelf_icon_observer.h"
23 #include "ash/shelf/shelf_item_delegate.h"
24 #include "ash/shelf/shelf_item_delegate_manager.h"
25 #include "ash/shelf/shelf_layout_manager.h"
26 #include "ash/shelf/shelf_menu_model.h"
27 #include "ash/shelf/shelf_model.h"
28 #include "ash/shelf/shelf_tooltip_manager.h"
29 #include "ash/shelf/shelf_widget.h"
30 #include "ash/shell.h"
31 #include "ash/wm/coordinate_conversion.h"
32 #include "base/auto_reset.h"
33 #include "base/memory/scoped_ptr.h"
34 #include "base/metrics/histogram.h"
35 #include "grit/ash_resources.h"
36 #include "grit/ash_strings.h"
37 #include "ui/accessibility/ax_view_state.h"
38 #include "ui/aura/client/screen_position_client.h"
39 #include "ui/aura/window.h"
40 #include "ui/aura/window_event_dispatcher.h"
41 #include "ui/base/l10n/l10n_util.h"
42 #include "ui/base/models/simple_menu_model.h"
43 #include "ui/base/resource/resource_bundle.h"
44 #include "ui/compositor/layer.h"
45 #include "ui/compositor/layer_animator.h"
46 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
47 #include "ui/gfx/canvas.h"
48 #include "ui/gfx/point.h"
49 #include "ui/views/animation/bounds_animator.h"
50 #include "ui/views/border.h"
51 #include "ui/views/controls/button/image_button.h"
52 #include "ui/views/controls/menu/menu_model_adapter.h"
53 #include "ui/views/controls/menu/menu_runner.h"
54 #include "ui/views/focus/focus_search.h"
55 #include "ui/views/view_model.h"
56 #include "ui/views/view_model_utils.h"
57 #include "ui/views/widget/widget.h"
58 #include "ui/wm/core/coordinate_conversion.h"
60 using gfx::Animation;
61 using views::View;
63 namespace ash {
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 // Additional spacing for the left and right side of icons.
77 const int kHorizontalIconSpacing = 2;
79 // Inset for items which do not have an icon.
80 const int kHorizontalNoIconInsetSpacing =
81 kHorizontalIconSpacing + kDefaultLeadingInset;
83 // The proportion of the shelf space reserved for non-panel icons. Panels
84 // may flow into this space but will be put into the overflow bubble if there
85 // is contention for the space.
86 const float kReservedNonPanelIconProportion = 0.67f;
88 // This is the command id of the menu item which contains the name of the menu.
89 const int kCommandIdOfMenuName = 0;
91 // The background color of the active item in the list.
92 const SkColor kActiveListItemBackgroundColor = SkColorSetRGB(203 , 219, 241);
94 // The background color of the active & hovered item in the list.
95 const SkColor kFocusedActiveListItemBackgroundColor =
96 SkColorSetRGB(193, 211, 236);
98 // The text color of the caption item in a list.
99 const SkColor kCaptionItemForegroundColor = SK_ColorBLACK;
101 // The maximum allowable length of a menu line of an application menu in pixels.
102 const int kMaximumAppMenuItemLength = 350;
104 // The distance of the cursor from the outer rim of the shelf before it
105 // separates.
106 const int kRipOffDistance = 48;
108 // The rip off drag and drop proxy image should get scaled by this factor.
109 const float kDragAndDropProxyScale = 1.5f;
111 // The opacity represents that this partially disappeared item will get removed.
112 const float kDraggedImageOpacity = 0.5f;
114 namespace {
116 // A class to temporarily disable a given bounds animator.
117 class BoundsAnimatorDisabler {
118 public:
119 BoundsAnimatorDisabler(views::BoundsAnimator* bounds_animator)
120 : old_duration_(bounds_animator->GetAnimationDuration()),
121 bounds_animator_(bounds_animator) {
122 bounds_animator_->SetAnimationDuration(1);
125 ~BoundsAnimatorDisabler() {
126 bounds_animator_->SetAnimationDuration(old_duration_);
129 private:
130 // The previous animation duration.
131 int old_duration_;
132 // The bounds animator which gets used.
133 views::BoundsAnimator* bounds_animator_;
135 DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorDisabler);
138 // The MenuModelAdapter gets slightly changed to adapt the menu appearance to
139 // our requirements.
140 class ShelfMenuModelAdapter : public views::MenuModelAdapter {
141 public:
142 explicit ShelfMenuModelAdapter(ShelfMenuModel* menu_model);
144 // views::MenuModelAdapter:
145 virtual const gfx::FontList* GetLabelFontList(int command_id) const OVERRIDE;
146 virtual bool IsCommandEnabled(int id) const OVERRIDE;
147 virtual void GetHorizontalIconMargins(int id,
148 int icon_size,
149 int* left_margin,
150 int* right_margin) const OVERRIDE;
151 virtual bool GetForegroundColor(int command_id,
152 bool is_hovered,
153 SkColor* override_color) const OVERRIDE;
154 virtual bool GetBackgroundColor(int command_id,
155 bool is_hovered,
156 SkColor* override_color) const OVERRIDE;
157 virtual int GetMaxWidthForMenu(views::MenuItemView* menu) OVERRIDE;
158 virtual bool ShouldReserveSpaceForSubmenuIndicator() const OVERRIDE;
160 private:
161 ShelfMenuModel* menu_model_;
163 DISALLOW_COPY_AND_ASSIGN(ShelfMenuModelAdapter);
166 ShelfMenuModelAdapter::ShelfMenuModelAdapter(ShelfMenuModel* menu_model)
167 : MenuModelAdapter(menu_model),
168 menu_model_(menu_model) {
171 const gfx::FontList* ShelfMenuModelAdapter::GetLabelFontList(
172 int command_id) const {
173 if (command_id != kCommandIdOfMenuName)
174 return MenuModelAdapter::GetLabelFontList(command_id);
176 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
177 return &rb.GetFontList(ui::ResourceBundle::BoldFont);
180 bool ShelfMenuModelAdapter::IsCommandEnabled(int id) const {
181 return id != kCommandIdOfMenuName;
184 bool ShelfMenuModelAdapter::GetForegroundColor(int command_id,
185 bool is_hovered,
186 SkColor* override_color) const {
187 if (command_id != kCommandIdOfMenuName)
188 return false;
190 *override_color = kCaptionItemForegroundColor;
191 return true;
194 bool ShelfMenuModelAdapter::GetBackgroundColor(int command_id,
195 bool is_hovered,
196 SkColor* override_color) const {
197 if (!menu_model_->IsCommandActive(command_id))
198 return false;
200 *override_color = is_hovered ? kFocusedActiveListItemBackgroundColor :
201 kActiveListItemBackgroundColor;
202 return true;
205 void ShelfMenuModelAdapter::GetHorizontalIconMargins(int command_id,
206 int icon_size,
207 int* left_margin,
208 int* right_margin) const {
209 *left_margin = kHorizontalIconSpacing;
210 *right_margin = (command_id != kCommandIdOfMenuName) ?
211 kHorizontalIconSpacing : -(icon_size + kHorizontalNoIconInsetSpacing);
214 int ShelfMenuModelAdapter::GetMaxWidthForMenu(views::MenuItemView* menu) {
215 return kMaximumAppMenuItemLength;
218 bool ShelfMenuModelAdapter::ShouldReserveSpaceForSubmenuIndicator() const {
219 return false;
222 // Custom FocusSearch used to navigate the shelf in the order items are in
223 // the ViewModel.
224 class ShelfFocusSearch : public views::FocusSearch {
225 public:
226 explicit ShelfFocusSearch(views::ViewModel* view_model)
227 : FocusSearch(NULL, true, true),
228 view_model_(view_model) {}
229 virtual ~ShelfFocusSearch() {}
231 // views::FocusSearch overrides:
232 virtual View* FindNextFocusableView(
233 View* starting_view,
234 bool reverse,
235 Direction direction,
236 bool check_starting_view,
237 views::FocusTraversable** focus_traversable,
238 View** focus_traversable_view) OVERRIDE {
239 int index = view_model_->GetIndexOfView(starting_view);
240 if (index == -1)
241 return view_model_->view_at(0);
243 if (reverse) {
244 --index;
245 if (index < 0)
246 index = view_model_->view_size() - 1;
247 } else {
248 ++index;
249 if (index >= view_model_->view_size())
250 index = 0;
252 return view_model_->view_at(index);
255 private:
256 views::ViewModel* view_model_;
258 DISALLOW_COPY_AND_ASSIGN(ShelfFocusSearch);
261 // AnimationDelegate used when inserting a new item. This steadily increases the
262 // opacity of the layer as the animation progress.
263 class FadeInAnimationDelegate : public gfx::AnimationDelegate {
264 public:
265 explicit FadeInAnimationDelegate(views::View* view) : view_(view) {}
266 virtual ~FadeInAnimationDelegate() {}
268 // AnimationDelegate overrides:
269 virtual void AnimationProgressed(const Animation* animation) OVERRIDE {
270 view_->layer()->SetOpacity(animation->GetCurrentValue());
271 view_->layer()->ScheduleDraw();
273 virtual void AnimationEnded(const Animation* animation) OVERRIDE {
274 view_->layer()->SetOpacity(1.0f);
275 view_->layer()->ScheduleDraw();
277 virtual void AnimationCanceled(const Animation* animation) OVERRIDE {
278 view_->layer()->SetOpacity(1.0f);
279 view_->layer()->ScheduleDraw();
282 private:
283 views::View* view_;
285 DISALLOW_COPY_AND_ASSIGN(FadeInAnimationDelegate);
288 void ReflectItemStatus(const ShelfItem& item, ShelfButton* button) {
289 switch (item.status) {
290 case STATUS_CLOSED:
291 button->ClearState(ShelfButton::STATE_ACTIVE);
292 button->ClearState(ShelfButton::STATE_RUNNING);
293 button->ClearState(ShelfButton::STATE_ATTENTION);
294 break;
295 case STATUS_RUNNING:
296 button->ClearState(ShelfButton::STATE_ACTIVE);
297 button->AddState(ShelfButton::STATE_RUNNING);
298 button->ClearState(ShelfButton::STATE_ATTENTION);
299 break;
300 case STATUS_ACTIVE:
301 button->AddState(ShelfButton::STATE_ACTIVE);
302 button->ClearState(ShelfButton::STATE_RUNNING);
303 button->ClearState(ShelfButton::STATE_ATTENTION);
304 break;
305 case STATUS_ATTENTION:
306 button->ClearState(ShelfButton::STATE_ACTIVE);
307 button->ClearState(ShelfButton::STATE_RUNNING);
308 button->AddState(ShelfButton::STATE_ATTENTION);
309 break;
313 } // namespace
315 // AnimationDelegate used when deleting an item. This steadily decreased the
316 // opacity of the layer as the animation progress.
317 class ShelfView::FadeOutAnimationDelegate : public gfx::AnimationDelegate {
318 public:
319 FadeOutAnimationDelegate(ShelfView* host, views::View* view)
320 : shelf_view_(host),
321 view_(view) {}
322 virtual ~FadeOutAnimationDelegate() {}
324 // AnimationDelegate overrides:
325 virtual void AnimationProgressed(const Animation* animation) OVERRIDE {
326 view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
327 view_->layer()->ScheduleDraw();
329 virtual void AnimationEnded(const Animation* animation) OVERRIDE {
330 shelf_view_->OnFadeOutAnimationEnded();
332 virtual void AnimationCanceled(const Animation* animation) OVERRIDE {
335 private:
336 ShelfView* shelf_view_;
337 scoped_ptr<views::View> view_;
339 DISALLOW_COPY_AND_ASSIGN(FadeOutAnimationDelegate);
342 // AnimationDelegate used to trigger fading an element in. When an item is
343 // inserted this delegate is attached to the animation that expands the size of
344 // the item. When done it kicks off another animation to fade the item in.
345 class ShelfView::StartFadeAnimationDelegate : public gfx::AnimationDelegate {
346 public:
347 StartFadeAnimationDelegate(ShelfView* host,
348 views::View* view)
349 : shelf_view_(host),
350 view_(view) {}
351 virtual ~StartFadeAnimationDelegate() {}
353 // AnimationDelegate overrides:
354 virtual void AnimationEnded(const Animation* animation) OVERRIDE {
355 shelf_view_->FadeIn(view_);
357 virtual void AnimationCanceled(const Animation* animation) OVERRIDE {
358 view_->layer()->SetOpacity(1.0f);
361 private:
362 ShelfView* shelf_view_;
363 views::View* view_;
365 DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate);
368 ShelfView::ShelfView(ShelfModel* model,
369 ShelfDelegate* delegate,
370 ShelfLayoutManager* manager)
371 : model_(model),
372 delegate_(delegate),
373 view_model_(new views::ViewModel),
374 first_visible_index_(0),
375 last_visible_index_(-1),
376 overflow_button_(NULL),
377 owner_overflow_bubble_(NULL),
378 drag_pointer_(NONE),
379 drag_view_(NULL),
380 start_drag_index_(-1),
381 context_menu_id_(0),
382 leading_inset_(kDefaultLeadingInset),
383 cancelling_drag_model_changed_(false),
384 last_hidden_index_(0),
385 closing_event_time_(base::TimeDelta()),
386 got_deleted_(NULL),
387 drag_and_drop_item_pinned_(false),
388 drag_and_drop_shelf_id_(0),
389 dragged_off_shelf_(false),
390 snap_back_from_rip_off_view_(NULL),
391 item_manager_(Shell::GetInstance()->shelf_item_delegate_manager()),
392 layout_manager_(manager),
393 overflow_mode_(false),
394 main_shelf_(NULL),
395 dragged_off_from_overflow_to_shelf_(false) {
396 DCHECK(model_);
397 bounds_animator_.reset(new views::BoundsAnimator(this));
398 bounds_animator_->AddObserver(this);
399 set_context_menu_controller(this);
400 focus_search_.reset(new ShelfFocusSearch(view_model_.get()));
401 tooltip_.reset(new ShelfTooltipManager(manager, this));
404 ShelfView::~ShelfView() {
405 bounds_animator_->RemoveObserver(this);
406 model_->RemoveObserver(this);
407 // If we are inside the MenuRunner, we need to know if we were getting
408 // deleted while it was running.
409 if (got_deleted_)
410 *got_deleted_ = true;
413 void ShelfView::Init() {
414 model_->AddObserver(this);
416 const ShelfItems& items(model_->items());
417 for (ShelfItems::const_iterator i = items.begin(); i != items.end(); ++i) {
418 views::View* child = CreateViewForItem(*i);
419 child->SetPaintToLayer(true);
420 view_model_->Add(child, static_cast<int>(i - items.begin()));
421 AddChildView(child);
423 overflow_button_ = new OverflowButton(this);
424 overflow_button_->set_context_menu_controller(this);
425 ConfigureChildView(overflow_button_);
426 AddChildView(overflow_button_);
428 // We'll layout when our bounds change.
431 void ShelfView::OnShelfAlignmentChanged() {
432 overflow_button_->OnShelfAlignmentChanged();
433 LayoutToIdealBounds();
434 for (int i=0; i < view_model_->view_size(); ++i) {
435 if (i >= first_visible_index_ && i <= last_visible_index_)
436 view_model_->view_at(i)->Layout();
438 tooltip_->Close();
439 if (overflow_bubble_)
440 overflow_bubble_->Hide();
443 void ShelfView::SchedulePaintForAllButtons() {
444 for (int i = 0; i < view_model_->view_size(); ++i) {
445 if (i >= first_visible_index_ && i <= last_visible_index_)
446 view_model_->view_at(i)->SchedulePaint();
448 if (overflow_button_ && overflow_button_->visible())
449 overflow_button_->SchedulePaint();
452 gfx::Rect ShelfView::GetIdealBoundsOfItemIcon(ShelfID id) {
453 int index = model_->ItemIndexByID(id);
454 if (index == -1)
455 return gfx::Rect();
456 // Map all items from overflow area to the overflow button. Note that the
457 // section between last_index_hidden_ and model_->FirstPanelIndex() is the
458 // list of invisible panel items. However, these items are currently nowhere
459 // represented and get dropped instead - see (crbug.com/378907). As such there
460 // is no way to address them or place them. We therefore move them over the
461 // overflow button.
462 if (index > last_visible_index_ && index < model_->FirstPanelIndex())
463 index = last_visible_index_ + 1;
464 const gfx::Rect& ideal_bounds(view_model_->ideal_bounds(index));
465 DCHECK_NE(TYPE_APP_LIST, model_->items()[index].type);
466 ShelfButton* button = static_cast<ShelfButton*>(view_model_->view_at(index));
467 gfx::Rect icon_bounds = button->GetIconBounds();
468 return gfx::Rect(GetMirroredXWithWidthInView(
469 ideal_bounds.x() + icon_bounds.x(), icon_bounds.width()),
470 ideal_bounds.y() + icon_bounds.y(),
471 icon_bounds.width(),
472 icon_bounds.height());
475 void ShelfView::UpdatePanelIconPosition(ShelfID id,
476 const gfx::Point& midpoint) {
477 int current_index = model_->ItemIndexByID(id);
478 int first_panel_index = model_->FirstPanelIndex();
479 if (current_index < first_panel_index)
480 return;
482 gfx::Point midpoint_in_view(GetMirroredXInView(midpoint.x()),
483 midpoint.y());
484 int target_index = current_index;
485 while (target_index > first_panel_index &&
486 layout_manager_->PrimaryAxisValue(
487 view_model_->ideal_bounds(target_index).x(),
488 view_model_->ideal_bounds(target_index).y()) >
489 layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
490 midpoint_in_view.y())) {
491 --target_index;
493 while (target_index < view_model_->view_size() - 1 &&
494 layout_manager_->PrimaryAxisValue(
495 view_model_->ideal_bounds(target_index).right(),
496 view_model_->ideal_bounds(target_index).bottom()) <
497 layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
498 midpoint_in_view.y())) {
499 ++target_index;
501 if (current_index != target_index)
502 model_->Move(current_index, target_index);
505 bool ShelfView::IsShowingMenu() const {
506 return (launcher_menu_runner_.get() &&
507 launcher_menu_runner_->IsRunning());
510 bool ShelfView::IsShowingOverflowBubble() const {
511 return overflow_bubble_.get() && overflow_bubble_->IsShowing();
514 views::View* ShelfView::GetAppListButtonView() const {
515 for (int i = 0; i < model_->item_count(); ++i) {
516 if (model_->items()[i].type == TYPE_APP_LIST)
517 return view_model_->view_at(i);
520 NOTREACHED() << "Applist button not found";
521 return NULL;
524 ////////////////////////////////////////////////////////////////////////////////
525 // ShelfView, FocusTraversable implementation:
527 views::FocusSearch* ShelfView::GetFocusSearch() {
528 return focus_search_.get();
531 views::FocusTraversable* ShelfView::GetFocusTraversableParent() {
532 return parent()->GetFocusTraversable();
535 View* ShelfView::GetFocusTraversableParentView() {
536 return this;
539 void ShelfView::CreateDragIconProxy(
540 const gfx::Point& location_in_screen_coordinates,
541 const gfx::ImageSkia& icon,
542 views::View* replaced_view,
543 const gfx::Vector2d& cursor_offset_from_center,
544 float scale_factor) {
545 drag_replaced_view_ = replaced_view;
546 drag_image_.reset(new ash::DragImageView(
547 drag_replaced_view_->GetWidget()->GetNativeWindow()->GetRootWindow(),
548 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE));
549 drag_image_->SetImage(icon);
550 gfx::Size size = drag_image_->GetPreferredSize();
551 size.set_width(size.width() * scale_factor);
552 size.set_height(size.height() * scale_factor);
553 drag_image_offset_ = gfx::Vector2d(size.width() / 2, size.height() / 2) +
554 cursor_offset_from_center;
555 gfx::Rect drag_image_bounds(
556 location_in_screen_coordinates - drag_image_offset_,
557 size);
558 drag_image_->SetBoundsInScreen(drag_image_bounds);
559 drag_image_->SetWidgetVisible(true);
562 void ShelfView::UpdateDragIconProxy(
563 const gfx::Point& location_in_screen_coordinates) {
564 // TODO(jennyz): Investigate why drag_image_ becomes NULL at this point per
565 // crbug.com/34722, while the app list item is still being dragged around.
566 if (drag_image_) {
567 drag_image_->SetScreenPosition(
568 location_in_screen_coordinates - drag_image_offset_);
572 void ShelfView::DestroyDragIconProxy() {
573 drag_image_.reset();
574 drag_image_offset_ = gfx::Vector2d(0, 0);
577 bool ShelfView::StartDrag(const std::string& app_id,
578 const gfx::Point& location_in_screen_coordinates) {
579 // Bail if an operation is already going on - or the cursor is not inside.
580 // This could happen if mouse / touch operations overlap.
581 if (drag_and_drop_shelf_id_ ||
582 !GetBoundsInScreen().Contains(location_in_screen_coordinates))
583 return false;
585 // If the AppsGridView (which was dispatching this event) was opened by our
586 // button, ShelfView dragging operations are locked and we have to unlock.
587 CancelDrag(-1);
588 drag_and_drop_item_pinned_ = false;
589 drag_and_drop_app_id_ = app_id;
590 drag_and_drop_shelf_id_ =
591 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_);
592 // Check if the application is known and pinned - if not, we have to pin it so
593 // that we can re-arrange the shelf order accordingly. Note that items have
594 // to be pinned to give them the same (order) possibilities as a shortcut.
595 // When an item is dragged from overflow to shelf, IsShowingOverflowBubble()
596 // returns true. At this time, we don't need to pin the item.
597 if (!IsShowingOverflowBubble() &&
598 (!drag_and_drop_shelf_id_ ||
599 !delegate_->IsAppPinned(app_id))) {
600 delegate_->PinAppWithID(app_id);
601 drag_and_drop_shelf_id_ =
602 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_);
603 if (!drag_and_drop_shelf_id_)
604 return false;
605 drag_and_drop_item_pinned_ = true;
607 views::View* drag_and_drop_view = view_model_->view_at(
608 model_->ItemIndexByID(drag_and_drop_shelf_id_));
609 DCHECK(drag_and_drop_view);
611 // Since there is already an icon presented by the caller, we hide this item
612 // for now. That has to be done by reducing the size since the visibility will
613 // change once a regrouping animation is performed.
614 pre_drag_and_drop_size_ = drag_and_drop_view->size();
615 drag_and_drop_view->SetSize(gfx::Size());
617 // First we have to center the mouse cursor over the item.
618 gfx::Point pt = drag_and_drop_view->GetBoundsInScreen().CenterPoint();
619 views::View::ConvertPointFromScreen(drag_and_drop_view, &pt);
620 gfx::Point point_in_root = location_in_screen_coordinates;
621 ::wm::ConvertPointFromScreen(
622 ash::wm::GetRootWindowAt(location_in_screen_coordinates), &point_in_root);
623 ui::MouseEvent event(ui::ET_MOUSE_PRESSED, pt, point_in_root, 0, 0);
624 PointerPressedOnButton(drag_and_drop_view,
625 ShelfButtonHost::DRAG_AND_DROP,
626 event);
628 // Drag the item where it really belongs.
629 Drag(location_in_screen_coordinates);
630 return true;
633 bool ShelfView::Drag(const gfx::Point& location_in_screen_coordinates) {
634 if (!drag_and_drop_shelf_id_ ||
635 !GetBoundsInScreen().Contains(location_in_screen_coordinates))
636 return false;
638 gfx::Point pt = location_in_screen_coordinates;
639 views::View* drag_and_drop_view = view_model_->view_at(
640 model_->ItemIndexByID(drag_and_drop_shelf_id_));
641 ConvertPointFromScreen(drag_and_drop_view, &pt);
642 gfx::Point point_in_root = location_in_screen_coordinates;
643 ::wm::ConvertPointFromScreen(
644 ash::wm::GetRootWindowAt(location_in_screen_coordinates), &point_in_root);
645 ui::MouseEvent event(ui::ET_MOUSE_DRAGGED, pt, point_in_root, 0, 0);
646 PointerDraggedOnButton(drag_and_drop_view,
647 ShelfButtonHost::DRAG_AND_DROP,
648 event);
649 return true;
652 void ShelfView::EndDrag(bool cancel) {
653 if (!drag_and_drop_shelf_id_)
654 return;
656 views::View* drag_and_drop_view = view_model_->view_at(
657 model_->ItemIndexByID(drag_and_drop_shelf_id_));
658 PointerReleasedOnButton(
659 drag_and_drop_view, ShelfButtonHost::DRAG_AND_DROP, cancel);
661 // Either destroy the temporarily created item - or - make the item visible.
662 if (drag_and_drop_item_pinned_ && cancel)
663 delegate_->UnpinAppWithID(drag_and_drop_app_id_);
664 else if (drag_and_drop_view) {
665 if (cancel) {
666 // When a hosted drag gets canceled, the item can remain in the same slot
667 // and it might have moved within the bounds. In that case the item need
668 // to animate back to its correct location.
669 AnimateToIdealBounds();
670 } else {
671 drag_and_drop_view->SetSize(pre_drag_and_drop_size_);
675 drag_and_drop_shelf_id_ = 0;
678 void ShelfView::LayoutToIdealBounds() {
679 if (bounds_animator_->IsAnimating()) {
680 AnimateToIdealBounds();
681 return;
684 IdealBounds ideal_bounds;
685 CalculateIdealBounds(&ideal_bounds);
686 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
687 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
690 void ShelfView::UpdateAllButtonsVisibilityInOverflowMode() {
691 // The overflow button is not shown in overflow mode.
692 overflow_button_->SetVisible(false);
693 DCHECK_LT(last_visible_index_, view_model_->view_size());
694 for (int i = 0; i < view_model_->view_size(); ++i) {
695 bool visible = i >= first_visible_index_ &&
696 i <= last_visible_index_;
697 // To track the dragging of |drag_view_| continuously, its visibility
698 // should be always true regardless of its position.
699 if (dragged_off_from_overflow_to_shelf_ &&
700 view_model_->view_at(i) == drag_view_)
701 view_model_->view_at(i)->SetVisible(true);
702 else
703 view_model_->view_at(i)->SetVisible(visible);
707 void ShelfView::CalculateIdealBounds(IdealBounds* bounds) const {
708 int available_size = layout_manager_->PrimaryAxisValue(width(), height());
709 DCHECK(model_->item_count() == view_model_->view_size());
710 if (!available_size)
711 return;
713 int first_panel_index = model_->FirstPanelIndex();
714 int last_button_index = first_panel_index - 1;
716 int x = 0;
717 int y = 0;
718 int button_size = kShelfButtonSize;
719 int button_spacing = kShelfButtonSpacing;
721 int w = layout_manager_->PrimaryAxisValue(button_size, width());
722 int h = layout_manager_->PrimaryAxisValue(height(), button_size);
723 for (int i = 0; i < view_model_->view_size(); ++i) {
724 if (i < first_visible_index_) {
725 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, 0, 0));
726 continue;
729 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
730 if (i != last_button_index) {
731 x = layout_manager_->PrimaryAxisValue(x + w + button_spacing, x);
732 y = layout_manager_->PrimaryAxisValue(y, y + h + button_spacing);
736 if (is_overflow_mode()) {
737 const_cast<ShelfView*>(this)->UpdateAllButtonsVisibilityInOverflowMode();
738 return;
741 // Right aligned icons.
742 int end_position = available_size - button_spacing;
743 x = layout_manager_->PrimaryAxisValue(end_position, 0);
744 y = layout_manager_->PrimaryAxisValue(0, end_position);
745 for (int i = view_model_->view_size() - 1;
746 i >= first_panel_index; --i) {
747 x = layout_manager_->PrimaryAxisValue(x - w - button_spacing, x);
748 y = layout_manager_->PrimaryAxisValue(y, y - h - button_spacing);
749 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
750 end_position = layout_manager_->PrimaryAxisValue(x, y);
753 // Icons on the left / top are guaranteed up to kLeftIconProportion of
754 // the available space.
755 int last_icon_position = layout_manager_->PrimaryAxisValue(
756 view_model_->ideal_bounds(last_button_index).right(),
757 view_model_->ideal_bounds(last_button_index).bottom()) + button_size;
758 int reserved_icon_space = available_size * kReservedNonPanelIconProportion;
759 if (last_icon_position < reserved_icon_space)
760 end_position = last_icon_position;
761 else
762 end_position = std::max(end_position, reserved_icon_space);
764 bounds->overflow_bounds.set_size(
765 gfx::Size(layout_manager_->PrimaryAxisValue(w, width()),
766 layout_manager_->PrimaryAxisValue(height(), h)));
768 last_visible_index_ = DetermineLastVisibleIndex(
769 end_position - button_size);
770 last_hidden_index_ = DetermineFirstVisiblePanelIndex(end_position) - 1;
771 bool show_overflow = last_visible_index_ < last_button_index ||
772 last_hidden_index_ >= first_panel_index;
774 // Create Space for the overflow button
775 if (show_overflow &&
776 last_visible_index_ > 0 && last_visible_index_ < last_button_index)
777 --last_visible_index_;
778 for (int i = 0; i < view_model_->view_size(); ++i) {
779 bool visible = i <= last_visible_index_ || i > last_hidden_index_;
780 // To receive drag event continuously from |drag_view_| during the dragging
781 // off from the shelf, don't make |drag_view_| invisible. It will be
782 // eventually invisible and removed from the |view_model_| by
783 // FinalizeRipOffDrag().
784 if (dragged_off_shelf_ && view_model_->view_at(i) == drag_view_)
785 continue;
786 view_model_->view_at(i)->SetVisible(visible);
789 overflow_button_->SetVisible(show_overflow);
790 if (show_overflow) {
791 DCHECK_NE(0, view_model_->view_size());
792 if (last_visible_index_ == -1) {
793 x = 0;
794 y = 0;
795 } else {
796 x = layout_manager_->PrimaryAxisValue(
797 view_model_->ideal_bounds(last_visible_index_).right(),
798 view_model_->ideal_bounds(last_visible_index_).x());
799 y = layout_manager_->PrimaryAxisValue(
800 view_model_->ideal_bounds(last_visible_index_).y(),
801 view_model_->ideal_bounds(last_visible_index_).bottom());
803 // Set all hidden panel icon positions to be on the overflow button.
804 for (int i = first_panel_index; i <= last_hidden_index_; ++i)
805 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
807 // Add more space between last visible item and overflow button.
808 // Without this, two buttons look too close compared with other items.
809 x = layout_manager_->PrimaryAxisValue(x + button_spacing, x);
810 y = layout_manager_->PrimaryAxisValue(y, y + button_spacing);
812 bounds->overflow_bounds.set_x(x);
813 bounds->overflow_bounds.set_y(y);
814 if (overflow_bubble_.get() && overflow_bubble_->IsShowing())
815 UpdateOverflowRange(overflow_bubble_->shelf_view());
816 } else {
817 if (overflow_bubble_)
818 overflow_bubble_->Hide();
822 int ShelfView::DetermineLastVisibleIndex(int max_value) const {
823 int index = model_->FirstPanelIndex() - 1;
824 while (index >= 0 &&
825 layout_manager_->PrimaryAxisValue(
826 view_model_->ideal_bounds(index).right(),
827 view_model_->ideal_bounds(index).bottom()) > max_value) {
828 index--;
830 return index;
833 int ShelfView::DetermineFirstVisiblePanelIndex(int min_value) const {
834 int index = model_->FirstPanelIndex();
835 while (index < view_model_->view_size() &&
836 layout_manager_->PrimaryAxisValue(
837 view_model_->ideal_bounds(index).right(),
838 view_model_->ideal_bounds(index).bottom()) < min_value) {
839 ++index;
841 return index;
844 void ShelfView::AddIconObserver(ShelfIconObserver* observer) {
845 observers_.AddObserver(observer);
848 void ShelfView::RemoveIconObserver(ShelfIconObserver* observer) {
849 observers_.RemoveObserver(observer);
852 void ShelfView::AnimateToIdealBounds() {
853 IdealBounds ideal_bounds;
854 CalculateIdealBounds(&ideal_bounds);
855 for (int i = 0; i < view_model_->view_size(); ++i) {
856 View* view = view_model_->view_at(i);
857 bounds_animator_->AnimateViewTo(view, view_model_->ideal_bounds(i));
858 // Now that the item animation starts, we have to make sure that the
859 // padding of the first gets properly transferred to the new first item.
860 if (i && view->border())
861 view->SetBorder(views::Border::NullBorder());
863 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
866 views::View* ShelfView::CreateViewForItem(const ShelfItem& item) {
867 views::View* view = NULL;
868 switch (item.type) {
869 case TYPE_BROWSER_SHORTCUT:
870 case TYPE_APP_SHORTCUT:
871 case TYPE_WINDOWED_APP:
872 case TYPE_PLATFORM_APP:
873 case TYPE_DIALOG:
874 case TYPE_APP_PANEL: {
875 ShelfButton* button = ShelfButton::Create(this, this, layout_manager_);
876 button->SetImage(item.image);
877 ReflectItemStatus(item, button);
878 view = button;
879 break;
882 case TYPE_APP_LIST: {
883 view = new AppListButton(this, this, layout_manager_->shelf_widget());
884 break;
887 default:
888 break;
890 view->set_context_menu_controller(this);
892 DCHECK(view);
893 ConfigureChildView(view);
894 return view;
897 void ShelfView::FadeIn(views::View* view) {
898 view->SetVisible(true);
899 view->layer()->SetOpacity(0);
900 AnimateToIdealBounds();
901 bounds_animator_->SetAnimationDelegate(
902 view,
903 scoped_ptr<gfx::AnimationDelegate>(new FadeInAnimationDelegate(view)));
906 void ShelfView::PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event) {
907 DCHECK(!dragging());
908 DCHECK(drag_view_);
909 drag_pointer_ = pointer;
910 start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
912 if (start_drag_index_== -1) {
913 CancelDrag(-1);
914 return;
917 // If the item is no longer draggable, bail out.
918 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
919 model_->items()[start_drag_index_].id);
920 if (!item_delegate->IsDraggable()) {
921 CancelDrag(-1);
922 return;
925 // Move the view to the front so that it appears on top of other views.
926 ReorderChildView(drag_view_, -1);
927 bounds_animator_->StopAnimatingView(drag_view_);
930 void ShelfView::ContinueDrag(const ui::LocatedEvent& event) {
931 // Due to a syncing operation the application might have been removed.
932 // Bail if it is gone.
933 int current_index = view_model_->GetIndexOfView(drag_view_);
934 DCHECK_NE(-1, current_index);
936 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
937 model_->items()[current_index].id);
938 if (!item_delegate->IsDraggable()) {
939 CancelDrag(-1);
940 return;
943 // If this is not a drag and drop host operation and not the app list item,
944 // check if the item got ripped off the shelf - if it did we are done.
945 if (!drag_and_drop_shelf_id_ &&
946 RemovableByRipOff(current_index) != NOT_REMOVABLE) {
947 if (HandleRipOffDrag(event))
948 return;
949 // The rip off handler could have changed the location of the item.
950 current_index = view_model_->GetIndexOfView(drag_view_);
953 // TODO: I don't think this works correctly with RTL.
954 gfx::Point drag_point(event.location());
955 ConvertPointToTarget(drag_view_, this, &drag_point);
957 // Constrain the location to the range of valid indices for the type.
958 std::pair<int, int> indices(GetDragRange(current_index));
959 int first_drag_index = indices.first;
960 int last_drag_index = indices.second;
961 // If the last index isn't valid, we're overflowing. Constrain to the app list
962 // (which is the last visible item).
963 if (first_drag_index < model_->FirstPanelIndex() &&
964 last_drag_index > last_visible_index_)
965 last_drag_index = last_visible_index_;
966 int x = 0, y = 0;
967 if (layout_manager_->IsHorizontalAlignment()) {
968 x = std::max(view_model_->ideal_bounds(indices.first).x(),
969 drag_point.x() - drag_origin_.x());
970 x = std::min(view_model_->ideal_bounds(last_drag_index).right() -
971 view_model_->ideal_bounds(current_index).width(),
973 if (drag_view_->x() == x)
974 return;
975 drag_view_->SetX(x);
976 } else {
977 y = std::max(view_model_->ideal_bounds(indices.first).y(),
978 drag_point.y() - drag_origin_.y());
979 y = std::min(view_model_->ideal_bounds(last_drag_index).bottom() -
980 view_model_->ideal_bounds(current_index).height(),
982 if (drag_view_->y() == y)
983 return;
984 drag_view_->SetY(y);
987 int target_index =
988 views::ViewModelUtils::DetermineMoveIndex(
989 *view_model_, drag_view_,
990 layout_manager_->IsHorizontalAlignment() ?
991 views::ViewModelUtils::HORIZONTAL :
992 views::ViewModelUtils::VERTICAL,
993 x, y);
994 target_index =
995 std::min(indices.second, std::max(target_index, indices.first));
996 if (target_index == current_index)
997 return;
999 // Change the model, the ShelfItemMoved() callback will handle the
1000 // |view_model_| update.
1001 model_->Move(current_index, target_index);
1002 bounds_animator_->StopAnimatingView(drag_view_);
1005 bool ShelfView::HandleRipOffDrag(const ui::LocatedEvent& event) {
1006 int current_index = view_model_->GetIndexOfView(drag_view_);
1007 DCHECK_NE(-1, current_index);
1008 std::string dragged_app_id =
1009 delegate_->GetAppIDForShelfID(model_->items()[current_index].id);
1011 gfx::Point screen_location = event.root_location();
1012 ::wm::ConvertPointToScreen(GetWidget()->GetNativeWindow()->GetRootWindow(),
1013 &screen_location);
1015 // To avoid ugly forwards and backwards flipping we use different constants
1016 // for ripping off / re-inserting the items.
1017 if (dragged_off_shelf_) {
1018 // If the shelf/overflow bubble bounds contains |screen_location| we insert
1019 // the item back into the shelf.
1020 if (GetBoundsForDragInsertInScreen().Contains(screen_location)) {
1021 if (dragged_off_from_overflow_to_shelf_) {
1022 // During the dragging an item from Shelf to Overflow, it can enter here
1023 // directly because both are located very closly.
1024 main_shelf_->EndDrag(true);
1025 // Stops the animation of |drag_view_| and sets its bounds explicitly
1026 // becase ContinueDrag() stops its animation. Without this, unexpected
1027 // bounds will be set.
1028 bounds_animator_->StopAnimatingView(drag_view_);
1029 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1030 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index));
1031 dragged_off_from_overflow_to_shelf_ = false;
1033 // Destroy our proxy view item.
1034 DestroyDragIconProxy();
1035 // Re-insert the item and return simply false since the caller will handle
1036 // the move as in any normal case.
1037 dragged_off_shelf_ = false;
1038 drag_view_->layer()->SetOpacity(1.0f);
1039 // The size of Overflow bubble should be updated immediately when an item
1040 // is re-inserted.
1041 if (is_overflow_mode())
1042 PreferredSizeChanged();
1043 return false;
1044 } else if (is_overflow_mode() &&
1045 main_shelf_->GetBoundsForDragInsertInScreen().Contains(
1046 screen_location)) {
1047 if (!dragged_off_from_overflow_to_shelf_) {
1048 dragged_off_from_overflow_to_shelf_ = true;
1049 drag_image_->SetOpacity(1.0f);
1050 main_shelf_->StartDrag(dragged_app_id, screen_location);
1051 } else {
1052 main_shelf_->Drag(screen_location);
1054 } else if (dragged_off_from_overflow_to_shelf_) {
1055 // Makes the |drag_image_| partially disappear again.
1056 dragged_off_from_overflow_to_shelf_ = false;
1057 drag_image_->SetOpacity(kDraggedImageOpacity);
1058 main_shelf_->EndDrag(true);
1059 bounds_animator_->StopAnimatingView(drag_view_);
1060 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1061 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index));
1063 // Move our proxy view item.
1064 UpdateDragIconProxy(screen_location);
1065 return true;
1067 // Check if we are too far away from the shelf to enter the ripped off state.
1068 // Determine the distance to the shelf.
1069 int delta = CalculateShelfDistance(screen_location);
1070 if (delta > kRipOffDistance) {
1071 // Create a proxy view item which can be moved anywhere.
1072 ShelfButton* button = static_cast<ShelfButton*>(drag_view_);
1073 CreateDragIconProxy(event.root_location(),
1074 button->GetImage(),
1075 drag_view_,
1076 gfx::Vector2d(0, 0),
1077 kDragAndDropProxyScale);
1078 drag_view_->layer()->SetOpacity(0.0f);
1079 dragged_off_shelf_ = true;
1080 if (RemovableByRipOff(current_index) == REMOVABLE) {
1081 // Move the item to the front of the first panel item and hide it.
1082 // ShelfItemMoved() callback will handle the |view_model_| update and
1083 // call AnimateToIdealBounds().
1084 if (current_index != model_->FirstPanelIndex() - 1) {
1085 model_->Move(current_index, model_->FirstPanelIndex() - 1);
1086 StartFadeInLastVisibleItem();
1087 } else if (is_overflow_mode()) {
1088 // Overflow bubble should be shrunk when an item is ripped off.
1089 PreferredSizeChanged();
1091 // Make the item partially disappear to show that it will get removed if
1092 // dropped.
1093 drag_image_->SetOpacity(kDraggedImageOpacity);
1095 return true;
1097 return false;
1100 void ShelfView::FinalizeRipOffDrag(bool cancel) {
1101 if (!dragged_off_shelf_)
1102 return;
1103 // Make sure we do not come in here again.
1104 dragged_off_shelf_ = false;
1106 // Coming here we should always have a |drag_view_|.
1107 DCHECK(drag_view_);
1108 int current_index = view_model_->GetIndexOfView(drag_view_);
1109 // If the view isn't part of the model anymore (|current_index| == -1), a sync
1110 // operation must have removed it. In that case we shouldn't change the model
1111 // and only delete the proxy image.
1112 if (current_index == -1) {
1113 DestroyDragIconProxy();
1114 return;
1117 // Set to true when the animation should snap back to where it was before.
1118 bool snap_back = false;
1119 // Items which cannot be dragged off will be handled as a cancel.
1120 if (!cancel) {
1121 if (dragged_off_from_overflow_to_shelf_) {
1122 dragged_off_from_overflow_to_shelf_ = false;
1123 main_shelf_->EndDrag(false);
1124 drag_view_->layer()->SetOpacity(1.0f);
1125 } else if (RemovableByRipOff(current_index) != REMOVABLE) {
1126 // Make sure we do not try to remove un-removable items like items which
1127 // were not pinned or have to be always there.
1128 cancel = true;
1129 snap_back = true;
1130 } else {
1131 // Make sure the item stays invisible upon removal.
1132 drag_view_->SetVisible(false);
1133 std::string app_id =
1134 delegate_->GetAppIDForShelfID(model_->items()[current_index].id);
1135 delegate_->UnpinAppWithID(app_id);
1138 if (cancel || snap_back) {
1139 if (dragged_off_from_overflow_to_shelf_) {
1140 dragged_off_from_overflow_to_shelf_ = false;
1141 // Main shelf handles revert of dragged item.
1142 main_shelf_->EndDrag(true);
1143 drag_view_->layer()->SetOpacity(1.0f);
1144 } else if (!cancelling_drag_model_changed_) {
1145 // Only do something if the change did not come through a model change.
1146 gfx::Rect drag_bounds = drag_image_->GetBoundsInScreen();
1147 gfx::Point relative_to = GetBoundsInScreen().origin();
1148 gfx::Rect target(
1149 gfx::PointAtOffsetFromOrigin(drag_bounds.origin()- relative_to),
1150 drag_bounds.size());
1151 drag_view_->SetBoundsRect(target);
1152 // Hide the status from the active item since we snap it back now. Upon
1153 // animation end the flag gets cleared if |snap_back_from_rip_off_view_|
1154 // is set.
1155 snap_back_from_rip_off_view_ = drag_view_;
1156 ShelfButton* button = static_cast<ShelfButton*>(drag_view_);
1157 button->AddState(ShelfButton::STATE_HIDDEN);
1158 // When a canceling drag model is happening, the view model is diverged
1159 // from the menu model and movements / animations should not be done.
1160 model_->Move(current_index, start_drag_index_);
1161 AnimateToIdealBounds();
1163 drag_view_->layer()->SetOpacity(1.0f);
1165 DestroyDragIconProxy();
1168 ShelfView::RemovableState ShelfView::RemovableByRipOff(int index) const {
1169 DCHECK(index >= 0 && index < model_->item_count());
1170 ShelfItemType type = model_->items()[index].type;
1171 if (type == TYPE_APP_LIST || type == TYPE_DIALOG || !delegate_->CanPin())
1172 return NOT_REMOVABLE;
1174 std::string app_id = delegate_->GetAppIDForShelfID(model_->items()[index].id);
1175 // Note: Only pinned app shortcuts can be removed!
1176 return (type == TYPE_APP_SHORTCUT && delegate_->IsAppPinned(app_id)) ?
1177 REMOVABLE : DRAGGABLE;
1180 bool ShelfView::SameDragType(ShelfItemType typea, ShelfItemType typeb) const {
1181 switch (typea) {
1182 case TYPE_APP_SHORTCUT:
1183 case TYPE_BROWSER_SHORTCUT:
1184 return (typeb == TYPE_APP_SHORTCUT || typeb == TYPE_BROWSER_SHORTCUT);
1185 case TYPE_APP_LIST:
1186 case TYPE_PLATFORM_APP:
1187 case TYPE_WINDOWED_APP:
1188 case TYPE_APP_PANEL:
1189 case TYPE_DIALOG:
1190 return typeb == typea;
1191 case TYPE_UNDEFINED:
1192 NOTREACHED() << "ShelfItemType must be set.";
1193 return false;
1195 NOTREACHED();
1196 return false;
1199 std::pair<int, int> ShelfView::GetDragRange(int index) {
1200 int min_index = -1;
1201 int max_index = -1;
1202 ShelfItemType type = model_->items()[index].type;
1203 for (int i = 0; i < model_->item_count(); ++i) {
1204 if (SameDragType(model_->items()[i].type, type)) {
1205 if (min_index == -1)
1206 min_index = i;
1207 max_index = i;
1210 return std::pair<int, int>(min_index, max_index);
1213 void ShelfView::ConfigureChildView(views::View* view) {
1214 view->SetPaintToLayer(true);
1215 view->layer()->SetFillsBoundsOpaquely(false);
1218 void ShelfView::ToggleOverflowBubble() {
1219 if (IsShowingOverflowBubble()) {
1220 overflow_bubble_->Hide();
1221 return;
1224 if (!overflow_bubble_)
1225 overflow_bubble_.reset(new OverflowBubble());
1227 ShelfView* overflow_view =
1228 new ShelfView(model_, delegate_, layout_manager_);
1229 overflow_view->overflow_mode_ = true;
1230 overflow_view->Init();
1231 overflow_view->set_owner_overflow_bubble(overflow_bubble_.get());
1232 overflow_view->OnShelfAlignmentChanged();
1233 overflow_view->main_shelf_ = this;
1234 UpdateOverflowRange(overflow_view);
1236 overflow_bubble_->Show(overflow_button_, overflow_view);
1238 Shell::GetInstance()->UpdateShelfVisibility();
1241 void ShelfView::OnFadeOutAnimationEnded() {
1242 AnimateToIdealBounds();
1243 StartFadeInLastVisibleItem();
1246 void ShelfView::StartFadeInLastVisibleItem() {
1247 // If overflow button is visible and there is a valid new last item, fading
1248 // the new last item in after sliding animation is finished.
1249 if (overflow_button_->visible() && last_visible_index_ >= 0) {
1250 views::View* last_visible_view = view_model_->view_at(last_visible_index_);
1251 last_visible_view->layer()->SetOpacity(0);
1252 bounds_animator_->SetAnimationDelegate(
1253 last_visible_view,
1254 scoped_ptr<gfx::AnimationDelegate>(
1255 new StartFadeAnimationDelegate(this, last_visible_view)));
1259 void ShelfView::UpdateOverflowRange(ShelfView* overflow_view) const {
1260 const int first_overflow_index = last_visible_index_ + 1;
1261 const int last_overflow_index = last_hidden_index_;
1262 DCHECK_LE(first_overflow_index, last_overflow_index);
1263 DCHECK_LT(last_overflow_index, view_model_->view_size());
1265 overflow_view->first_visible_index_ = first_overflow_index;
1266 overflow_view->last_visible_index_ = last_overflow_index;
1269 bool ShelfView::ShouldHideTooltip(const gfx::Point& cursor_location) {
1270 gfx::Rect active_bounds;
1272 for (int i = 0; i < child_count(); ++i) {
1273 views::View* child = child_at(i);
1274 if (child == overflow_button_)
1275 continue;
1276 if (!ShouldShowTooltipForView(child))
1277 continue;
1279 gfx::Rect child_bounds = child->GetMirroredBounds();
1280 active_bounds.Union(child_bounds);
1283 return !active_bounds.Contains(cursor_location);
1286 gfx::Rect ShelfView::GetVisibleItemsBoundsInScreen() {
1287 gfx::Size preferred_size = GetPreferredSize();
1288 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1289 ConvertPointToScreen(this, &origin);
1290 return gfx::Rect(origin, preferred_size);
1293 gfx::Rect ShelfView::GetBoundsForDragInsertInScreen() {
1294 gfx::Size preferred_size;
1295 if (is_overflow_mode()) {
1296 DCHECK(owner_overflow_bubble_);
1297 gfx::Rect bubble_bounds =
1298 owner_overflow_bubble_->bubble_view()->GetBubbleBounds();
1299 preferred_size = bubble_bounds.size();
1300 } else {
1301 const int last_button_index = view_model_->view_size() - 1;
1302 gfx::Rect last_button_bounds =
1303 view_model_->view_at(last_button_index)->bounds();
1304 if (overflow_button_->visible() &&
1305 model_->GetItemIndexForType(TYPE_APP_PANEL) == -1) {
1306 // When overflow button is visible and shelf has no panel items,
1307 // last_button_bounds should be overflow button's bounds.
1308 last_button_bounds = overflow_button_->bounds();
1311 if (layout_manager_->IsHorizontalAlignment()) {
1312 preferred_size = gfx::Size(last_button_bounds.right() + leading_inset_,
1313 kShelfSize);
1314 } else {
1315 preferred_size = gfx::Size(kShelfSize,
1316 last_button_bounds.bottom() + leading_inset_);
1319 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1321 // In overflow mode, we should use OverflowBubbleView as a source for
1322 // converting |origin| to screen coordinates. When a scroll operation is
1323 // occurred in OverflowBubble, the bounds of ShelfView in OverflowBubble can
1324 // be changed.
1325 if (is_overflow_mode())
1326 ConvertPointToScreen(owner_overflow_bubble_->bubble_view(), &origin);
1327 else
1328 ConvertPointToScreen(this, &origin);
1330 return gfx::Rect(origin, preferred_size);
1333 int ShelfView::CancelDrag(int modified_index) {
1334 FinalizeRipOffDrag(true);
1335 if (!drag_view_)
1336 return modified_index;
1337 bool was_dragging = dragging();
1338 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1339 drag_pointer_ = NONE;
1340 drag_view_ = NULL;
1341 if (drag_view_index == modified_index) {
1342 // The view that was being dragged is being modified. Don't do anything.
1343 return modified_index;
1345 if (!was_dragging)
1346 return modified_index;
1348 // Restore previous position, tracking the position of the modified view.
1349 bool at_end = modified_index == view_model_->view_size();
1350 views::View* modified_view =
1351 (modified_index >= 0 && !at_end) ?
1352 view_model_->view_at(modified_index) : NULL;
1353 model_->Move(drag_view_index, start_drag_index_);
1355 // If the modified view will be at the end of the list, return the new end of
1356 // the list.
1357 if (at_end)
1358 return view_model_->view_size();
1359 return modified_view ? view_model_->GetIndexOfView(modified_view) : -1;
1362 gfx::Size ShelfView::GetPreferredSize() const {
1363 IdealBounds ideal_bounds;
1364 CalculateIdealBounds(&ideal_bounds);
1366 int last_button_index = is_overflow_mode() ?
1367 last_visible_index_ : view_model_->view_size() - 1;
1369 // When an item is dragged off from the overflow bubble, it is moved to last
1370 // position and and changed to invisible. Overflow bubble size should be
1371 // shrunk to fit only for visible items.
1372 // If |dragged_off_from_overflow_to_shelf_| is set, there will be no invisible
1373 // items in the shelf.
1374 if (is_overflow_mode() &&
1375 dragged_off_shelf_ &&
1376 !dragged_off_from_overflow_to_shelf_ &&
1377 RemovableByRipOff(view_model_->GetIndexOfView(drag_view_)) == REMOVABLE)
1378 last_button_index--;
1380 const gfx::Rect last_button_bounds =
1381 last_button_index >= first_visible_index_ ?
1382 view_model_->ideal_bounds(last_button_index) :
1383 gfx::Rect(gfx::Size(kShelfSize, kShelfSize));
1385 if (layout_manager_->IsHorizontalAlignment()) {
1386 return gfx::Size(last_button_bounds.right() + leading_inset_, kShelfSize);
1389 return gfx::Size(kShelfSize,
1390 last_button_bounds.bottom() + leading_inset_);
1393 void ShelfView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1394 // This bounds change is produced by the shelf movement and all content has
1395 // to follow. Using an animation at that time would produce a time lag since
1396 // the animation of the BoundsAnimator has itself a delay before it arrives
1397 // at the required location. As such we tell the animator to go there
1398 // immediately.
1399 BoundsAnimatorDisabler disabler(bounds_animator_.get());
1400 LayoutToIdealBounds();
1401 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1402 OnShelfIconPositionsChanged());
1404 if (IsShowingOverflowBubble())
1405 overflow_bubble_->Hide();
1408 views::FocusTraversable* ShelfView::GetPaneFocusTraversable() {
1409 return this;
1412 void ShelfView::GetAccessibleState(ui::AXViewState* state) {
1413 state->role = ui::AX_ROLE_TOOLBAR;
1414 state->name = l10n_util::GetStringUTF16(IDS_ASH_SHELF_ACCESSIBLE_NAME);
1417 void ShelfView::OnGestureEvent(ui::GestureEvent* event) {
1418 if (gesture_handler_.ProcessGestureEvent(*event))
1419 event->StopPropagation();
1422 void ShelfView::ShelfItemAdded(int model_index) {
1424 base::AutoReset<bool> cancelling_drag(
1425 &cancelling_drag_model_changed_, true);
1426 model_index = CancelDrag(model_index);
1428 views::View* view = CreateViewForItem(model_->items()[model_index]);
1429 AddChildView(view);
1430 // Hide the view, it'll be made visible when the animation is done. Using
1431 // opacity 0 here to avoid messing with CalculateIdealBounds which touches
1432 // the view's visibility.
1433 view->layer()->SetOpacity(0);
1434 view_model_->Add(view, model_index);
1436 // Give the button its ideal bounds. That way if we end up animating the
1437 // button before this animation completes it doesn't appear at some random
1438 // spot (because it was in the middle of animating from 0,0 0x0 to its
1439 // target).
1440 IdealBounds ideal_bounds;
1441 CalculateIdealBounds(&ideal_bounds);
1442 view->SetBoundsRect(view_model_->ideal_bounds(model_index));
1444 // The first animation moves all the views to their target position. |view|
1445 // is hidden, so it visually appears as though we are providing space for
1446 // it. When done we'll fade the view in.
1447 AnimateToIdealBounds();
1448 if (model_index <= last_visible_index_ ||
1449 model_index >= model_->FirstPanelIndex()) {
1450 bounds_animator_->SetAnimationDelegate(
1451 view,
1452 scoped_ptr<gfx::AnimationDelegate>(
1453 new StartFadeAnimationDelegate(this, view)));
1454 } else {
1455 // Undo the hiding if animation does not run.
1456 view->layer()->SetOpacity(1.0f);
1460 void ShelfView::ShelfItemRemoved(int model_index, ShelfID id) {
1461 if (id == context_menu_id_)
1462 launcher_menu_runner_.reset();
1464 base::AutoReset<bool> cancelling_drag(
1465 &cancelling_drag_model_changed_, true);
1466 model_index = CancelDrag(model_index);
1468 views::View* view = view_model_->view_at(model_index);
1469 view_model_->Remove(model_index);
1471 // When the overflow bubble is visible, the overflow range needs to be set
1472 // before CalculateIdealBounds() gets called. Otherwise CalculateIdealBounds()
1473 // could trigger a ShelfItemChanged() by hiding the overflow bubble and
1474 // since the overflow bubble is not yet synced with the ShelfModel this
1475 // could cause a crash.
1476 if (overflow_bubble_ && overflow_bubble_->IsShowing()) {
1477 last_hidden_index_ = std::min(last_hidden_index_,
1478 view_model_->view_size() - 1);
1479 UpdateOverflowRange(overflow_bubble_->shelf_view());
1482 if (view->visible()) {
1483 // The first animation fades out the view. When done we'll animate the rest
1484 // of the views to their target location.
1485 bounds_animator_->AnimateViewTo(view, view->bounds());
1486 bounds_animator_->SetAnimationDelegate(
1487 view,
1488 scoped_ptr<gfx::AnimationDelegate>(
1489 new FadeOutAnimationDelegate(this, view)));
1490 } else {
1491 // We don't need to show a fade out animation for invisible |view|. When an
1492 // item is ripped out from the shelf, its |view| is already invisible.
1493 AnimateToIdealBounds();
1496 // Close the tooltip because it isn't needed any longer and its anchor view
1497 // will be deleted soon.
1498 if (tooltip_->GetCurrentAnchorView() == view)
1499 tooltip_->Close();
1502 void ShelfView::ShelfItemChanged(int model_index, const ShelfItem& old_item) {
1503 const ShelfItem& item(model_->items()[model_index]);
1504 if (old_item.type != item.type) {
1505 // Type changed, swap the views.
1506 model_index = CancelDrag(model_index);
1507 scoped_ptr<views::View> old_view(view_model_->view_at(model_index));
1508 bounds_animator_->StopAnimatingView(old_view.get());
1509 // Removing and re-inserting a view in our view model will strip the ideal
1510 // bounds from the item. To avoid recalculation of everything the bounds
1511 // get remembered and restored after the insertion to the previous value.
1512 gfx::Rect old_ideal_bounds = view_model_->ideal_bounds(model_index);
1513 view_model_->Remove(model_index);
1514 views::View* new_view = CreateViewForItem(item);
1515 AddChildView(new_view);
1516 view_model_->Add(new_view, model_index);
1517 view_model_->set_ideal_bounds(model_index, old_ideal_bounds);
1518 new_view->SetBoundsRect(old_view->bounds());
1519 return;
1522 views::View* view = view_model_->view_at(model_index);
1523 switch (item.type) {
1524 case TYPE_BROWSER_SHORTCUT:
1525 // Fallthrough for the new Shelf since it needs to show the activation
1526 // change as well.
1527 case TYPE_APP_SHORTCUT:
1528 case TYPE_WINDOWED_APP:
1529 case TYPE_PLATFORM_APP:
1530 case TYPE_DIALOG:
1531 case TYPE_APP_PANEL: {
1532 ShelfButton* button = static_cast<ShelfButton*>(view);
1533 ReflectItemStatus(item, button);
1534 // The browser shortcut is currently not a "real" item and as such the
1535 // the image is bogous as well. We therefore keep the image as is for it.
1536 if (item.type != TYPE_BROWSER_SHORTCUT)
1537 button->SetImage(item.image);
1538 button->SchedulePaint();
1539 break;
1542 default:
1543 break;
1547 void ShelfView::ShelfItemMoved(int start_index, int target_index) {
1548 view_model_->Move(start_index, target_index);
1549 // When cancelling a drag due to a shelf item being added, the currently
1550 // dragged item is moved back to its initial position. AnimateToIdealBounds
1551 // will be called again when the new item is added to the |view_model_| but
1552 // at this time the |view_model_| is inconsistent with the |model_|.
1553 if (!cancelling_drag_model_changed_)
1554 AnimateToIdealBounds();
1557 void ShelfView::ShelfStatusChanged() {
1558 // Nothing to do here.
1561 void ShelfView::PointerPressedOnButton(views::View* view,
1562 Pointer pointer,
1563 const ui::LocatedEvent& event) {
1564 if (drag_view_)
1565 return;
1567 int index = view_model_->GetIndexOfView(view);
1568 if (index == -1)
1569 return;
1571 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1572 model_->items()[index].id);
1573 if (view_model_->view_size() <= 1 || !item_delegate->IsDraggable())
1574 return; // View is being deleted or not draggable, ignore request.
1576 drag_view_ = view;
1577 drag_origin_ = gfx::Point(event.x(), event.y());
1578 UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage",
1579 layout_manager_->SelectValueForShelfAlignment(
1580 SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM,
1581 SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT,
1582 SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT,
1583 -1),
1584 SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT);
1587 void ShelfView::PointerDraggedOnButton(views::View* view,
1588 Pointer pointer,
1589 const ui::LocatedEvent& event) {
1590 // To prepare all drag types (moving an item in the shelf and dragging off),
1591 // we should check the x-axis and y-axis offset.
1592 if (!dragging() && drag_view_ &&
1593 ((std::abs(event.x() - drag_origin_.x()) >= kMinimumDragDistance) ||
1594 (std::abs(event.y() - drag_origin_.y()) >= kMinimumDragDistance))) {
1595 PrepareForDrag(pointer, event);
1597 if (drag_pointer_ == pointer)
1598 ContinueDrag(event);
1601 void ShelfView::PointerReleasedOnButton(views::View* view,
1602 Pointer pointer,
1603 bool canceled) {
1604 if (canceled) {
1605 CancelDrag(-1);
1606 } else if (drag_pointer_ == pointer) {
1607 FinalizeRipOffDrag(false);
1608 drag_pointer_ = NONE;
1609 AnimateToIdealBounds();
1611 // If the drag pointer is NONE, no drag operation is going on and the
1612 // drag_view can be released.
1613 if (drag_pointer_ == NONE)
1614 drag_view_ = NULL;
1617 void ShelfView::MouseMovedOverButton(views::View* view) {
1618 if (!ShouldShowTooltipForView(view))
1619 return;
1621 if (!tooltip_->IsVisible())
1622 tooltip_->ResetTimer();
1625 void ShelfView::MouseEnteredButton(views::View* view) {
1626 if (!ShouldShowTooltipForView(view))
1627 return;
1629 if (tooltip_->IsVisible()) {
1630 tooltip_->ShowImmediately(view, GetAccessibleName(view));
1631 } else {
1632 tooltip_->ShowDelayed(view, GetAccessibleName(view));
1636 void ShelfView::MouseExitedButton(views::View* view) {
1637 if (!tooltip_->IsVisible())
1638 tooltip_->StopTimer();
1641 base::string16 ShelfView::GetAccessibleName(const views::View* view) {
1642 int view_index = view_model_->GetIndexOfView(view);
1643 // May be -1 while in the process of animating closed.
1644 if (view_index == -1)
1645 return base::string16();
1647 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1648 model_->items()[view_index].id);
1649 return item_delegate->GetTitle();
1652 void ShelfView::ButtonPressed(views::Button* sender, const ui::Event& event) {
1653 // Do not handle mouse release during drag.
1654 if (dragging())
1655 return;
1657 if (sender == overflow_button_) {
1658 ToggleOverflowBubble();
1659 return;
1662 int view_index = view_model_->GetIndexOfView(sender);
1663 // May be -1 while in the process of animating closed.
1664 if (view_index == -1)
1665 return;
1667 // If the previous menu was closed by the same event as this one, we ignore
1668 // the call.
1669 if (!IsUsableEvent(event))
1670 return;
1672 // Don't activate the item twice on double-click. Otherwise the window starts
1673 // animating open due to the first click, then immediately minimizes due to
1674 // the second click. The user most likely intended to open or minimize the
1675 // item once, not do both.
1676 if (event.flags() & ui::EF_IS_DOUBLE_CLICK)
1677 return;
1680 ScopedTargetRootWindow scoped_target(
1681 sender->GetWidget()->GetNativeView()->GetRootWindow());
1682 // Slow down activation animations if shift key is pressed.
1683 scoped_ptr<ui::ScopedAnimationDurationScaleMode> slowing_animations;
1684 if (event.IsShiftDown()) {
1685 slowing_animations.reset(new ui::ScopedAnimationDurationScaleMode(
1686 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION));
1689 // Collect usage statistics before we decide what to do with the click.
1690 switch (model_->items()[view_index].type) {
1691 case TYPE_APP_SHORTCUT:
1692 case TYPE_WINDOWED_APP:
1693 case TYPE_PLATFORM_APP:
1694 case TYPE_BROWSER_SHORTCUT:
1695 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1696 UMA_LAUNCHER_CLICK_ON_APP);
1697 break;
1699 case TYPE_APP_LIST:
1700 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1701 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON);
1702 break;
1704 case TYPE_APP_PANEL:
1705 case TYPE_DIALOG:
1706 break;
1708 case TYPE_UNDEFINED:
1709 NOTREACHED() << "ShelfItemType must be set.";
1710 break;
1713 ShelfItemDelegate* item_delegate =
1714 item_manager_->GetShelfItemDelegate(model_->items()[view_index].id);
1715 if (!item_delegate->ItemSelected(event))
1716 ShowListMenuForView(model_->items()[view_index], sender, event);
1720 bool ShelfView::ShowListMenuForView(const ShelfItem& item,
1721 views::View* source,
1722 const ui::Event& event) {
1723 ShelfItemDelegate* item_delegate =
1724 item_manager_->GetShelfItemDelegate(item.id);
1725 scoped_ptr<ui::MenuModel> list_menu_model(
1726 item_delegate->CreateApplicationMenu(event.flags()));
1728 // Make sure we have a menu and it has at least two items in addition to the
1729 // application title and the 3 spacing separators.
1730 if (!list_menu_model.get() || list_menu_model->GetItemCount() <= 5)
1731 return false;
1733 ShowMenu(list_menu_model.get(),
1734 source,
1735 gfx::Point(),
1736 false,
1737 ui::GetMenuSourceTypeForEvent(event));
1738 return true;
1741 void ShelfView::ShowContextMenuForView(views::View* source,
1742 const gfx::Point& point,
1743 ui::MenuSourceType source_type) {
1744 int view_index = view_model_->GetIndexOfView(source);
1745 if (view_index == -1) {
1746 Shell::GetInstance()->ShowContextMenu(point, source_type);
1747 return;
1750 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1751 model_->items()[view_index].id);
1752 context_menu_model_.reset(item_delegate->CreateContextMenu(
1753 source->GetWidget()->GetNativeView()->GetRootWindow()));
1754 if (!context_menu_model_)
1755 return;
1757 base::AutoReset<ShelfID> reseter(
1758 &context_menu_id_,
1759 view_index == -1 ? 0 : model_->items()[view_index].id);
1761 ShowMenu(context_menu_model_.get(),
1762 source,
1763 point,
1764 true,
1765 source_type);
1768 void ShelfView::ShowMenu(ui::MenuModel* menu_model,
1769 views::View* source,
1770 const gfx::Point& click_point,
1771 bool context_menu,
1772 ui::MenuSourceType source_type) {
1773 closing_event_time_ = base::TimeDelta();
1774 launcher_menu_runner_.reset(new views::MenuRunner(
1775 menu_model, context_menu ? views::MenuRunner::CONTEXT_MENU : 0));
1777 ScopedTargetRootWindow scoped_target(
1778 source->GetWidget()->GetNativeView()->GetRootWindow());
1780 // Determine the menu alignment dependent on the shelf.
1781 views::MenuAnchorPosition menu_alignment = views::MENU_ANCHOR_TOPLEFT;
1782 gfx::Rect anchor_point = gfx::Rect(click_point, gfx::Size());
1784 ShelfWidget* shelf = RootWindowController::ForShelf(
1785 GetWidget()->GetNativeView())->shelf();
1786 if (!context_menu) {
1787 // Application lists use a bubble.
1788 ShelfAlignment align = shelf->GetAlignment();
1789 anchor_point = source->GetBoundsInScreen();
1791 // It is possible to invoke the menu while it is sliding into view. To cover
1792 // that case, the screen coordinates are offsetted by the animation delta.
1793 gfx::Vector2d offset =
1794 source->GetWidget()->GetNativeWindow()->bounds().origin() -
1795 source->GetWidget()->GetNativeWindow()->GetTargetBounds().origin();
1796 anchor_point.set_x(anchor_point.x() - offset.x());
1797 anchor_point.set_y(anchor_point.y() - offset.y());
1799 // Shelf items can have an asymmetrical border for spacing reasons.
1800 // Adjust anchor location for this.
1801 if (source->border())
1802 anchor_point.Inset(source->border()->GetInsets());
1804 switch (align) {
1805 case SHELF_ALIGNMENT_BOTTOM:
1806 menu_alignment = views::MENU_ANCHOR_BUBBLE_ABOVE;
1807 break;
1808 case SHELF_ALIGNMENT_LEFT:
1809 menu_alignment = views::MENU_ANCHOR_BUBBLE_RIGHT;
1810 break;
1811 case SHELF_ALIGNMENT_RIGHT:
1812 menu_alignment = views::MENU_ANCHOR_BUBBLE_LEFT;
1813 break;
1814 case SHELF_ALIGNMENT_TOP:
1815 menu_alignment = views::MENU_ANCHOR_BUBBLE_BELOW;
1816 break;
1819 // If this gets deleted while we are in the menu, the shelf will be gone
1820 // as well.
1821 bool got_deleted = false;
1822 got_deleted_ = &got_deleted;
1824 shelf->ForceUndimming(true);
1825 // NOTE: if you convert to HAS_MNEMONICS be sure and update menu building
1826 // code.
1827 if (launcher_menu_runner_->RunMenuAt(source->GetWidget(),
1828 NULL,
1829 anchor_point,
1830 menu_alignment,
1831 source_type) ==
1832 views::MenuRunner::MENU_DELETED) {
1833 if (!got_deleted) {
1834 got_deleted_ = NULL;
1835 shelf->ForceUndimming(false);
1837 return;
1839 got_deleted_ = NULL;
1840 shelf->ForceUndimming(false);
1842 // If it is a context menu and we are showing overflow bubble
1843 // we want to hide overflow bubble.
1844 if (owner_overflow_bubble_)
1845 owner_overflow_bubble_->HideBubbleAndRefreshButton();
1847 // Unpinning an item will reset the |launcher_menu_runner_| before coming
1848 // here.
1849 if (launcher_menu_runner_)
1850 closing_event_time_ = launcher_menu_runner_->closing_event_time();
1851 Shell::GetInstance()->UpdateShelfVisibility();
1854 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) {
1855 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1856 OnShelfIconPositionsChanged());
1857 PreferredSizeChanged();
1860 void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
1861 if (snap_back_from_rip_off_view_ && animator == bounds_animator_) {
1862 if (!animator->IsAnimating(snap_back_from_rip_off_view_)) {
1863 // Coming here the animation of the ShelfButton is finished and the
1864 // previously hidden status can be shown again. Since the button itself
1865 // might have gone away or changed locations we check that the button
1866 // is still in the shelf and show its status again.
1867 for (int index = 0; index < view_model_->view_size(); index++) {
1868 views::View* view = view_model_->view_at(index);
1869 if (view == snap_back_from_rip_off_view_) {
1870 ShelfButton* button = static_cast<ShelfButton*>(view);
1871 button->ClearState(ShelfButton::STATE_HIDDEN);
1872 break;
1875 snap_back_from_rip_off_view_ = NULL;
1880 bool ShelfView::IsUsableEvent(const ui::Event& event) {
1881 if (closing_event_time_ == base::TimeDelta())
1882 return true;
1884 base::TimeDelta delta =
1885 base::TimeDelta(event.time_stamp() - closing_event_time_);
1886 closing_event_time_ = base::TimeDelta();
1887 // TODO(skuhne): This time seems excessive, but it appears that the reposting
1888 // takes that long. Need to come up with a better way of doing this.
1889 return (delta.InMilliseconds() < 0 || delta.InMilliseconds() > 130);
1892 const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const {
1893 int view_index = view_model_->GetIndexOfView(view);
1894 if (view_index == -1)
1895 return NULL;
1896 return &(model_->items()[view_index]);
1899 bool ShelfView::ShouldShowTooltipForView(const views::View* view) const {
1900 if (view == GetAppListButtonView() &&
1901 Shell::GetInstance()->GetAppListWindow())
1902 return false;
1903 const ShelfItem* item = ShelfItemForView(view);
1904 if (!item)
1905 return true;
1906 ShelfItemDelegate* item_delegate =
1907 item_manager_->GetShelfItemDelegate(item->id);
1908 return item_delegate->ShouldShowTooltip();
1911 int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const {
1912 ShelfWidget* shelf = RootWindowController::ForShelf(
1913 GetWidget()->GetNativeView())->shelf();
1914 ShelfAlignment align = shelf->GetAlignment();
1915 const gfx::Rect bounds = GetBoundsInScreen();
1916 int distance = 0;
1917 switch (align) {
1918 case SHELF_ALIGNMENT_BOTTOM:
1919 distance = bounds.y() - coordinate.y();
1920 break;
1921 case SHELF_ALIGNMENT_LEFT:
1922 distance = coordinate.x() - bounds.right();
1923 break;
1924 case SHELF_ALIGNMENT_RIGHT:
1925 distance = bounds.x() - coordinate.x();
1926 break;
1927 case SHELF_ALIGNMENT_TOP:
1928 distance = coordinate.y() - bounds.bottom();
1929 break;
1931 return distance > 0 ? distance : 0;
1934 } // namespace ash