Remove now obsolete checkdeps copy
[chromium-blink-merge.git] / ash / shelf / shelf_view.cc
blob0ef9b405589d1895ec23641589eed641d48d6e67
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"
59 using gfx::Animation;
60 using views::View;
62 namespace ash {
64 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM = 0;
65 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT = 1;
66 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT = 2;
67 const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT = 3;
69 // Default amount content is inset on the left edge.
70 const int kDefaultLeadingInset = 8;
72 // Minimum distance before drag starts.
73 const int kMinimumDragDistance = 8;
75 // Additional spacing for the left and right side of icons.
76 const int kHorizontalIconSpacing = 2;
78 // Inset for items which do not have an icon.
79 const int kHorizontalNoIconInsetSpacing =
80 kHorizontalIconSpacing + kDefaultLeadingInset;
82 // The proportion of the shelf space reserved for non-panel icons. Panels
83 // may flow into this space but will be put into the overflow bubble if there
84 // is contention for the space.
85 const float kReservedNonPanelIconProportion = 0.67f;
87 // This is the command id of the menu item which contains the name of the menu.
88 const int kCommandIdOfMenuName = 0;
90 // The background color of the active item in the list.
91 const SkColor kActiveListItemBackgroundColor = SkColorSetRGB(203 , 219, 241);
93 // The background color of the active & hovered item in the list.
94 const SkColor kFocusedActiveListItemBackgroundColor =
95 SkColorSetRGB(193, 211, 236);
97 // The text color of the caption item in a list.
98 const SkColor kCaptionItemForegroundColor = SK_ColorBLACK;
100 // The maximum allowable length of a menu line of an application menu in pixels.
101 const int kMaximumAppMenuItemLength = 350;
103 // The distance of the cursor from the outer rim of the shelf before it
104 // separates.
105 const int kRipOffDistance = 48;
107 // The rip off drag and drop proxy image should get scaled by this factor.
108 const float kDragAndDropProxyScale = 1.5f;
110 // The opacity represents that this partially disappeared item will get removed.
111 const float kDraggedImageOpacity = 0.5f;
113 namespace {
115 // A class to temporarily disable a given bounds animator.
116 class BoundsAnimatorDisabler {
117 public:
118 BoundsAnimatorDisabler(views::BoundsAnimator* bounds_animator)
119 : old_duration_(bounds_animator->GetAnimationDuration()),
120 bounds_animator_(bounds_animator) {
121 bounds_animator_->SetAnimationDuration(1);
124 ~BoundsAnimatorDisabler() {
125 bounds_animator_->SetAnimationDuration(old_duration_);
128 private:
129 // The previous animation duration.
130 int old_duration_;
131 // The bounds animator which gets used.
132 views::BoundsAnimator* bounds_animator_;
134 DISALLOW_COPY_AND_ASSIGN(BoundsAnimatorDisabler);
137 // The MenuModelAdapter gets slightly changed to adapt the menu appearance to
138 // our requirements.
139 class ShelfMenuModelAdapter : public views::MenuModelAdapter {
140 public:
141 explicit ShelfMenuModelAdapter(ShelfMenuModel* menu_model);
143 // views::MenuModelAdapter:
144 virtual const gfx::FontList* GetLabelFontList(int command_id) const OVERRIDE;
145 virtual bool IsCommandEnabled(int id) const OVERRIDE;
146 virtual void GetHorizontalIconMargins(int id,
147 int icon_size,
148 int* left_margin,
149 int* right_margin) const OVERRIDE;
150 virtual bool GetForegroundColor(int command_id,
151 bool is_hovered,
152 SkColor* override_color) const OVERRIDE;
153 virtual bool GetBackgroundColor(int command_id,
154 bool is_hovered,
155 SkColor* override_color) const OVERRIDE;
156 virtual int GetMaxWidthForMenu(views::MenuItemView* menu) OVERRIDE;
157 virtual bool ShouldReserveSpaceForSubmenuIndicator() const OVERRIDE;
159 private:
160 ShelfMenuModel* menu_model_;
162 DISALLOW_COPY_AND_ASSIGN(ShelfMenuModelAdapter);
165 ShelfMenuModelAdapter::ShelfMenuModelAdapter(ShelfMenuModel* menu_model)
166 : MenuModelAdapter(menu_model),
167 menu_model_(menu_model) {
170 const gfx::FontList* ShelfMenuModelAdapter::GetLabelFontList(
171 int command_id) const {
172 if (command_id != kCommandIdOfMenuName)
173 return MenuModelAdapter::GetLabelFontList(command_id);
175 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
176 return &rb.GetFontList(ui::ResourceBundle::BoldFont);
179 bool ShelfMenuModelAdapter::IsCommandEnabled(int id) const {
180 return id != kCommandIdOfMenuName;
183 bool ShelfMenuModelAdapter::GetForegroundColor(int command_id,
184 bool is_hovered,
185 SkColor* override_color) const {
186 if (command_id != kCommandIdOfMenuName)
187 return false;
189 *override_color = kCaptionItemForegroundColor;
190 return true;
193 bool ShelfMenuModelAdapter::GetBackgroundColor(int command_id,
194 bool is_hovered,
195 SkColor* override_color) const {
196 if (!menu_model_->IsCommandActive(command_id))
197 return false;
199 *override_color = is_hovered ? kFocusedActiveListItemBackgroundColor :
200 kActiveListItemBackgroundColor;
201 return true;
204 void ShelfMenuModelAdapter::GetHorizontalIconMargins(int command_id,
205 int icon_size,
206 int* left_margin,
207 int* right_margin) const {
208 *left_margin = kHorizontalIconSpacing;
209 *right_margin = (command_id != kCommandIdOfMenuName) ?
210 kHorizontalIconSpacing : -(icon_size + kHorizontalNoIconInsetSpacing);
213 int ShelfMenuModelAdapter::GetMaxWidthForMenu(views::MenuItemView* menu) {
214 return kMaximumAppMenuItemLength;
217 bool ShelfMenuModelAdapter::ShouldReserveSpaceForSubmenuIndicator() const {
218 return false;
221 // Custom FocusSearch used to navigate the shelf in the order items are in
222 // the ViewModel.
223 class ShelfFocusSearch : public views::FocusSearch {
224 public:
225 explicit ShelfFocusSearch(views::ViewModel* view_model)
226 : FocusSearch(NULL, true, true),
227 view_model_(view_model) {}
228 virtual ~ShelfFocusSearch() {}
230 // views::FocusSearch overrides:
231 virtual View* FindNextFocusableView(
232 View* starting_view,
233 bool reverse,
234 Direction direction,
235 bool check_starting_view,
236 views::FocusTraversable** focus_traversable,
237 View** focus_traversable_view) OVERRIDE {
238 int index = view_model_->GetIndexOfView(starting_view);
239 if (index == -1)
240 return view_model_->view_at(0);
242 if (reverse) {
243 --index;
244 if (index < 0)
245 index = view_model_->view_size() - 1;
246 } else {
247 ++index;
248 if (index >= view_model_->view_size())
249 index = 0;
251 return view_model_->view_at(index);
254 private:
255 views::ViewModel* view_model_;
257 DISALLOW_COPY_AND_ASSIGN(ShelfFocusSearch);
260 // AnimationDelegate used when inserting a new item. This steadily increases the
261 // opacity of the layer as the animation progress.
262 class FadeInAnimationDelegate
263 : public views::BoundsAnimator::OwnedAnimationDelegate {
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
318 : public views::BoundsAnimator::OwnedAnimationDelegate {
319 public:
320 FadeOutAnimationDelegate(ShelfView* host, views::View* view)
321 : shelf_view_(host),
322 view_(view) {}
323 virtual ~FadeOutAnimationDelegate() {}
325 // AnimationDelegate overrides:
326 virtual void AnimationProgressed(const Animation* animation) OVERRIDE {
327 view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
328 view_->layer()->ScheduleDraw();
330 virtual void AnimationEnded(const Animation* animation) OVERRIDE {
331 shelf_view_->OnFadeOutAnimationEnded();
333 virtual void AnimationCanceled(const Animation* animation) OVERRIDE {
336 private:
337 ShelfView* shelf_view_;
338 scoped_ptr<views::View> view_;
340 DISALLOW_COPY_AND_ASSIGN(FadeOutAnimationDelegate);
343 // AnimationDelegate used to trigger fading an element in. When an item is
344 // inserted this delegate is attached to the animation that expands the size of
345 // the item. When done it kicks off another animation to fade the item in.
346 class ShelfView::StartFadeAnimationDelegate
347 : public views::BoundsAnimator::OwnedAnimationDelegate {
348 public:
349 StartFadeAnimationDelegate(ShelfView* host,
350 views::View* view)
351 : shelf_view_(host),
352 view_(view) {}
353 virtual ~StartFadeAnimationDelegate() {}
355 // AnimationDelegate overrides:
356 virtual void AnimationEnded(const Animation* animation) OVERRIDE {
357 shelf_view_->FadeIn(view_);
359 virtual void AnimationCanceled(const Animation* animation) OVERRIDE {
360 view_->layer()->SetOpacity(1.0f);
363 private:
364 ShelfView* shelf_view_;
365 views::View* view_;
367 DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate);
370 ShelfView::ShelfView(ShelfModel* model,
371 ShelfDelegate* delegate,
372 ShelfLayoutManager* manager)
373 : model_(model),
374 delegate_(delegate),
375 view_model_(new views::ViewModel),
376 first_visible_index_(0),
377 last_visible_index_(-1),
378 overflow_button_(NULL),
379 owner_overflow_bubble_(NULL),
380 drag_pointer_(NONE),
381 drag_view_(NULL),
382 drag_offset_(0),
383 start_drag_index_(-1),
384 context_menu_id_(0),
385 leading_inset_(kDefaultLeadingInset),
386 cancelling_drag_model_changed_(false),
387 last_hidden_index_(0),
388 closing_event_time_(base::TimeDelta()),
389 got_deleted_(NULL),
390 drag_and_drop_item_pinned_(false),
391 drag_and_drop_shelf_id_(0),
392 dragged_off_shelf_(false),
393 snap_back_from_rip_off_view_(NULL),
394 item_manager_(Shell::GetInstance()->shelf_item_delegate_manager()),
395 layout_manager_(manager),
396 overflow_mode_(false),
397 main_shelf_(NULL),
398 dragged_off_from_overflow_to_shelf_(false) {
399 DCHECK(model_);
400 bounds_animator_.reset(new views::BoundsAnimator(this));
401 bounds_animator_->AddObserver(this);
402 set_context_menu_controller(this);
403 focus_search_.reset(new ShelfFocusSearch(view_model_.get()));
404 tooltip_.reset(new ShelfTooltipManager(manager, this));
407 ShelfView::~ShelfView() {
408 bounds_animator_->RemoveObserver(this);
409 model_->RemoveObserver(this);
410 // If we are inside the MenuRunner, we need to know if we were getting
411 // deleted while it was running.
412 if (got_deleted_)
413 *got_deleted_ = true;
416 void ShelfView::Init() {
417 model_->AddObserver(this);
419 const ShelfItems& items(model_->items());
420 for (ShelfItems::const_iterator i = items.begin(); i != items.end(); ++i) {
421 views::View* child = CreateViewForItem(*i);
422 child->SetPaintToLayer(true);
423 view_model_->Add(child, static_cast<int>(i - items.begin()));
424 AddChildView(child);
426 overflow_button_ = new OverflowButton(this);
427 overflow_button_->set_context_menu_controller(this);
428 ConfigureChildView(overflow_button_);
429 AddChildView(overflow_button_);
431 // We'll layout when our bounds change.
434 void ShelfView::OnShelfAlignmentChanged() {
435 overflow_button_->OnShelfAlignmentChanged();
436 LayoutToIdealBounds();
437 for (int i=0; i < view_model_->view_size(); ++i) {
438 if (i >= first_visible_index_ && i <= last_visible_index_)
439 view_model_->view_at(i)->Layout();
441 tooltip_->Close();
442 if (overflow_bubble_)
443 overflow_bubble_->Hide();
446 void ShelfView::SchedulePaintForAllButtons() {
447 for (int i = 0; i < view_model_->view_size(); ++i) {
448 if (i >= first_visible_index_ && i <= last_visible_index_)
449 view_model_->view_at(i)->SchedulePaint();
451 if (overflow_button_ && overflow_button_->visible())
452 overflow_button_->SchedulePaint();
455 gfx::Rect ShelfView::GetIdealBoundsOfItemIcon(ShelfID id) {
456 int index = model_->ItemIndexByID(id);
457 if (index == -1)
458 return gfx::Rect();
459 // Map all items from overflow area to the overflow button. Note that the
460 // section between last_index_hidden_ and model_->FirstPanelIndex() is the
461 // list of invisible panel items. However, these items are currently nowhere
462 // represented and get dropped instead - see (crbug.com/378907). As such there
463 // is no way to address them or place them. We therefore move them over the
464 // overflow button.
465 if (index > last_visible_index_ && index < model_->FirstPanelIndex())
466 index = last_visible_index_ + 1;
467 const gfx::Rect& ideal_bounds(view_model_->ideal_bounds(index));
468 DCHECK_NE(TYPE_APP_LIST, model_->items()[index].type);
469 ShelfButton* button = static_cast<ShelfButton*>(view_model_->view_at(index));
470 gfx::Rect icon_bounds = button->GetIconBounds();
471 return gfx::Rect(GetMirroredXWithWidthInView(
472 ideal_bounds.x() + icon_bounds.x(), icon_bounds.width()),
473 ideal_bounds.y() + icon_bounds.y(),
474 icon_bounds.width(),
475 icon_bounds.height());
478 void ShelfView::UpdatePanelIconPosition(ShelfID id,
479 const gfx::Point& midpoint) {
480 int current_index = model_->ItemIndexByID(id);
481 int first_panel_index = model_->FirstPanelIndex();
482 if (current_index < first_panel_index)
483 return;
485 gfx::Point midpoint_in_view(GetMirroredXInView(midpoint.x()),
486 midpoint.y());
487 int target_index = current_index;
488 while (target_index > first_panel_index &&
489 layout_manager_->PrimaryAxisValue(
490 view_model_->ideal_bounds(target_index).x(),
491 view_model_->ideal_bounds(target_index).y()) >
492 layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
493 midpoint_in_view.y())) {
494 --target_index;
496 while (target_index < view_model_->view_size() - 1 &&
497 layout_manager_->PrimaryAxisValue(
498 view_model_->ideal_bounds(target_index).right(),
499 view_model_->ideal_bounds(target_index).bottom()) <
500 layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
501 midpoint_in_view.y())) {
502 ++target_index;
504 if (current_index != target_index)
505 model_->Move(current_index, target_index);
508 bool ShelfView::IsShowingMenu() const {
509 return (launcher_menu_runner_.get() &&
510 launcher_menu_runner_->IsRunning());
513 bool ShelfView::IsShowingOverflowBubble() const {
514 return overflow_bubble_.get() && overflow_bubble_->IsShowing();
517 views::View* ShelfView::GetAppListButtonView() const {
518 for (int i = 0; i < model_->item_count(); ++i) {
519 if (model_->items()[i].type == TYPE_APP_LIST)
520 return view_model_->view_at(i);
523 NOTREACHED() << "Applist button not found";
524 return NULL;
527 ////////////////////////////////////////////////////////////////////////////////
528 // ShelfView, FocusTraversable implementation:
530 views::FocusSearch* ShelfView::GetFocusSearch() {
531 return focus_search_.get();
534 views::FocusTraversable* ShelfView::GetFocusTraversableParent() {
535 return parent()->GetFocusTraversable();
538 View* ShelfView::GetFocusTraversableParentView() {
539 return this;
542 void ShelfView::CreateDragIconProxy(
543 const gfx::Point& location_in_screen_coordinates,
544 const gfx::ImageSkia& icon,
545 views::View* replaced_view,
546 const gfx::Vector2d& cursor_offset_from_center,
547 float scale_factor) {
548 drag_replaced_view_ = replaced_view;
549 drag_image_.reset(new ash::DragImageView(
550 drag_replaced_view_->GetWidget()->GetNativeWindow()->GetRootWindow(),
551 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE));
552 drag_image_->SetImage(icon);
553 gfx::Size size = drag_image_->GetPreferredSize();
554 size.set_width(size.width() * scale_factor);
555 size.set_height(size.height() * scale_factor);
556 drag_image_offset_ = gfx::Vector2d(size.width() / 2, size.height() / 2) +
557 cursor_offset_from_center;
558 gfx::Rect drag_image_bounds(
559 location_in_screen_coordinates - drag_image_offset_,
560 size);
561 drag_image_->SetBoundsInScreen(drag_image_bounds);
562 drag_image_->SetWidgetVisible(true);
565 void ShelfView::UpdateDragIconProxy(
566 const gfx::Point& location_in_screen_coordinates) {
567 // TODO(jennyz): Investigate why drag_image_ becomes NULL at this point per
568 // crbug.com/34722, while the app list item is still being dragged around.
569 if (drag_image_) {
570 drag_image_->SetScreenPosition(
571 location_in_screen_coordinates - drag_image_offset_);
575 void ShelfView::DestroyDragIconProxy() {
576 drag_image_.reset();
577 drag_image_offset_ = gfx::Vector2d(0, 0);
580 bool ShelfView::StartDrag(const std::string& app_id,
581 const gfx::Point& location_in_screen_coordinates) {
582 // Bail if an operation is already going on - or the cursor is not inside.
583 // This could happen if mouse / touch operations overlap.
584 if (drag_and_drop_shelf_id_ ||
585 !GetBoundsInScreen().Contains(location_in_screen_coordinates))
586 return false;
588 // If the AppsGridView (which was dispatching this event) was opened by our
589 // button, ShelfView dragging operations are locked and we have to unlock.
590 CancelDrag(-1);
591 drag_and_drop_item_pinned_ = false;
592 drag_and_drop_app_id_ = app_id;
593 drag_and_drop_shelf_id_ =
594 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_);
595 // Check if the application is known and pinned - if not, we have to pin it so
596 // that we can re-arrange the shelf order accordingly. Note that items have
597 // to be pinned to give them the same (order) possibilities as a shortcut.
598 // When an item is dragged from overflow to shelf, IsShowingOverflowBubble()
599 // returns true. At this time, we don't need to pin the item.
600 if (!IsShowingOverflowBubble() &&
601 (!drag_and_drop_shelf_id_ ||
602 !delegate_->IsAppPinned(app_id))) {
603 delegate_->PinAppWithID(app_id);
604 drag_and_drop_shelf_id_ =
605 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_);
606 if (!drag_and_drop_shelf_id_)
607 return false;
608 drag_and_drop_item_pinned_ = true;
610 views::View* drag_and_drop_view = view_model_->view_at(
611 model_->ItemIndexByID(drag_and_drop_shelf_id_));
612 DCHECK(drag_and_drop_view);
614 // Since there is already an icon presented by the caller, we hide this item
615 // for now. That has to be done by reducing the size since the visibility will
616 // change once a regrouping animation is performed.
617 pre_drag_and_drop_size_ = drag_and_drop_view->size();
618 drag_and_drop_view->SetSize(gfx::Size());
620 // First we have to center the mouse cursor over the item.
621 gfx::Point pt = drag_and_drop_view->GetBoundsInScreen().CenterPoint();
622 views::View::ConvertPointFromScreen(drag_and_drop_view, &pt);
623 gfx::Point point_in_root = location_in_screen_coordinates;
624 ash::wm::ConvertPointFromScreen(
625 ash::wm::GetRootWindowAt(location_in_screen_coordinates),
626 &point_in_root);
627 ui::MouseEvent event(ui::ET_MOUSE_PRESSED, pt, point_in_root, 0, 0);
628 PointerPressedOnButton(drag_and_drop_view,
629 ShelfButtonHost::DRAG_AND_DROP,
630 event);
632 // Drag the item where it really belongs.
633 Drag(location_in_screen_coordinates);
634 return true;
637 bool ShelfView::Drag(const gfx::Point& location_in_screen_coordinates) {
638 if (!drag_and_drop_shelf_id_ ||
639 !GetBoundsInScreen().Contains(location_in_screen_coordinates))
640 return false;
642 gfx::Point pt = location_in_screen_coordinates;
643 views::View* drag_and_drop_view = view_model_->view_at(
644 model_->ItemIndexByID(drag_and_drop_shelf_id_));
645 ConvertPointFromScreen(drag_and_drop_view, &pt);
646 gfx::Point point_in_root = location_in_screen_coordinates;
647 ash::wm::ConvertPointFromScreen(
648 ash::wm::GetRootWindowAt(location_in_screen_coordinates),
649 &point_in_root);
650 ui::MouseEvent event(ui::ET_MOUSE_DRAGGED, pt, point_in_root, 0, 0);
651 PointerDraggedOnButton(drag_and_drop_view,
652 ShelfButtonHost::DRAG_AND_DROP,
653 event);
654 return true;
657 void ShelfView::EndDrag(bool cancel) {
658 if (!drag_and_drop_shelf_id_)
659 return;
661 views::View* drag_and_drop_view = view_model_->view_at(
662 model_->ItemIndexByID(drag_and_drop_shelf_id_));
663 PointerReleasedOnButton(
664 drag_and_drop_view, ShelfButtonHost::DRAG_AND_DROP, cancel);
666 // Either destroy the temporarily created item - or - make the item visible.
667 if (drag_and_drop_item_pinned_ && cancel)
668 delegate_->UnpinAppWithID(drag_and_drop_app_id_);
669 else if (drag_and_drop_view) {
670 if (cancel) {
671 // When a hosted drag gets canceled, the item can remain in the same slot
672 // and it might have moved within the bounds. In that case the item need
673 // to animate back to its correct location.
674 AnimateToIdealBounds();
675 } else {
676 drag_and_drop_view->SetSize(pre_drag_and_drop_size_);
680 drag_and_drop_shelf_id_ = 0;
683 void ShelfView::LayoutToIdealBounds() {
684 if (bounds_animator_->IsAnimating()) {
685 AnimateToIdealBounds();
686 return;
689 IdealBounds ideal_bounds;
690 CalculateIdealBounds(&ideal_bounds);
691 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
692 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
695 void ShelfView::UpdateAllButtonsVisibilityInOverflowMode() {
696 // The overflow button is not shown in overflow mode.
697 overflow_button_->SetVisible(false);
698 DCHECK_LT(last_visible_index_, view_model_->view_size());
699 for (int i = 0; i < view_model_->view_size(); ++i) {
700 bool visible = i >= first_visible_index_ &&
701 i <= last_visible_index_;
702 // To track the dragging of |drag_view_| continuously, its visibility
703 // should be always true regardless of its position.
704 if (dragged_off_from_overflow_to_shelf_ &&
705 view_model_->view_at(i) == drag_view_)
706 view_model_->view_at(i)->SetVisible(true);
707 else
708 view_model_->view_at(i)->SetVisible(visible);
712 void ShelfView::CalculateIdealBounds(IdealBounds* bounds) const {
713 int available_size = layout_manager_->PrimaryAxisValue(width(), height());
714 DCHECK(model_->item_count() == view_model_->view_size());
715 if (!available_size)
716 return;
718 int first_panel_index = model_->FirstPanelIndex();
719 int last_button_index = first_panel_index - 1;
721 int x = 0;
722 int y = 0;
723 int button_size = kShelfButtonSize;
724 int button_spacing = kShelfButtonSpacing;
726 int w = layout_manager_->PrimaryAxisValue(button_size, width());
727 int h = layout_manager_->PrimaryAxisValue(height(), button_size);
728 for (int i = 0; i < view_model_->view_size(); ++i) {
729 if (i < first_visible_index_) {
730 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, 0, 0));
731 continue;
734 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
735 if (i != last_button_index) {
736 x = layout_manager_->PrimaryAxisValue(x + w + button_spacing, x);
737 y = layout_manager_->PrimaryAxisValue(y, y + h + button_spacing);
741 if (is_overflow_mode()) {
742 const_cast<ShelfView*>(this)->UpdateAllButtonsVisibilityInOverflowMode();
743 return;
746 // Right aligned icons.
747 int end_position = available_size - button_spacing;
748 x = layout_manager_->PrimaryAxisValue(end_position, 0);
749 y = layout_manager_->PrimaryAxisValue(0, end_position);
750 for (int i = view_model_->view_size() - 1;
751 i >= first_panel_index; --i) {
752 x = layout_manager_->PrimaryAxisValue(x - w - button_spacing, x);
753 y = layout_manager_->PrimaryAxisValue(y, y - h - button_spacing);
754 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
755 end_position = layout_manager_->PrimaryAxisValue(x, y);
758 // Icons on the left / top are guaranteed up to kLeftIconProportion of
759 // the available space.
760 int last_icon_position = layout_manager_->PrimaryAxisValue(
761 view_model_->ideal_bounds(last_button_index).right(),
762 view_model_->ideal_bounds(last_button_index).bottom()) + button_size;
763 int reserved_icon_space = available_size * kReservedNonPanelIconProportion;
764 if (last_icon_position < reserved_icon_space)
765 end_position = last_icon_position;
766 else
767 end_position = std::max(end_position, reserved_icon_space);
769 bounds->overflow_bounds.set_size(
770 gfx::Size(layout_manager_->PrimaryAxisValue(w, width()),
771 layout_manager_->PrimaryAxisValue(height(), h)));
773 last_visible_index_ = DetermineLastVisibleIndex(
774 end_position - button_size);
775 last_hidden_index_ = DetermineFirstVisiblePanelIndex(end_position) - 1;
776 bool show_overflow = last_visible_index_ < last_button_index ||
777 last_hidden_index_ >= first_panel_index;
779 // Create Space for the overflow button
780 if (show_overflow &&
781 last_visible_index_ > 0 && last_visible_index_ < last_button_index)
782 --last_visible_index_;
783 for (int i = 0; i < view_model_->view_size(); ++i) {
784 bool visible = i <= last_visible_index_ || i > last_hidden_index_;
785 // To receive drag event continuously from |drag_view_| during the dragging
786 // off from the shelf, don't make |drag_view_| invisible. It will be
787 // eventually invisible and removed from the |view_model_| by
788 // FinalizeRipOffDrag().
789 if (dragged_off_shelf_ && view_model_->view_at(i) == drag_view_)
790 continue;
791 view_model_->view_at(i)->SetVisible(visible);
794 overflow_button_->SetVisible(show_overflow);
795 if (show_overflow) {
796 DCHECK_NE(0, view_model_->view_size());
797 if (last_visible_index_ == -1) {
798 x = 0;
799 y = 0;
800 } else {
801 x = layout_manager_->PrimaryAxisValue(
802 view_model_->ideal_bounds(last_visible_index_).right(),
803 view_model_->ideal_bounds(last_visible_index_).x());
804 y = layout_manager_->PrimaryAxisValue(
805 view_model_->ideal_bounds(last_visible_index_).y(),
806 view_model_->ideal_bounds(last_visible_index_).bottom());
808 // Set all hidden panel icon positions to be on the overflow button.
809 for (int i = first_panel_index; i <= last_hidden_index_; ++i)
810 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
812 // Add more space between last visible item and overflow button.
813 // Without this, two buttons look too close compared with other items.
814 x = layout_manager_->PrimaryAxisValue(x + button_spacing, x);
815 y = layout_manager_->PrimaryAxisValue(y, y + button_spacing);
817 bounds->overflow_bounds.set_x(x);
818 bounds->overflow_bounds.set_y(y);
819 if (overflow_bubble_.get() && overflow_bubble_->IsShowing())
820 UpdateOverflowRange(overflow_bubble_->shelf_view());
821 } else {
822 if (overflow_bubble_)
823 overflow_bubble_->Hide();
827 int ShelfView::DetermineLastVisibleIndex(int max_value) const {
828 int index = model_->FirstPanelIndex() - 1;
829 while (index >= 0 &&
830 layout_manager_->PrimaryAxisValue(
831 view_model_->ideal_bounds(index).right(),
832 view_model_->ideal_bounds(index).bottom()) > max_value) {
833 index--;
835 return index;
838 int ShelfView::DetermineFirstVisiblePanelIndex(int min_value) const {
839 int index = model_->FirstPanelIndex();
840 while (index < view_model_->view_size() &&
841 layout_manager_->PrimaryAxisValue(
842 view_model_->ideal_bounds(index).right(),
843 view_model_->ideal_bounds(index).bottom()) < min_value) {
844 ++index;
846 return index;
849 void ShelfView::AddIconObserver(ShelfIconObserver* observer) {
850 observers_.AddObserver(observer);
853 void ShelfView::RemoveIconObserver(ShelfIconObserver* observer) {
854 observers_.RemoveObserver(observer);
857 void ShelfView::AnimateToIdealBounds() {
858 IdealBounds ideal_bounds;
859 CalculateIdealBounds(&ideal_bounds);
860 for (int i = 0; i < view_model_->view_size(); ++i) {
861 View* view = view_model_->view_at(i);
862 bounds_animator_->AnimateViewTo(view, view_model_->ideal_bounds(i));
863 // Now that the item animation starts, we have to make sure that the
864 // padding of the first gets properly transferred to the new first item.
865 if (i && view->border())
866 view->SetBorder(views::Border::NullBorder());
868 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
871 views::View* ShelfView::CreateViewForItem(const ShelfItem& item) {
872 views::View* view = NULL;
873 switch (item.type) {
874 case TYPE_BROWSER_SHORTCUT:
875 case TYPE_APP_SHORTCUT:
876 case TYPE_WINDOWED_APP:
877 case TYPE_PLATFORM_APP:
878 case TYPE_DIALOG:
879 case TYPE_APP_PANEL: {
880 ShelfButton* button = ShelfButton::Create(this, this, layout_manager_);
881 button->SetImage(item.image);
882 ReflectItemStatus(item, button);
883 view = button;
884 break;
887 case TYPE_APP_LIST: {
888 view = new AppListButton(this, this, layout_manager_->shelf_widget());
889 break;
892 default:
893 break;
895 view->set_context_menu_controller(this);
897 DCHECK(view);
898 ConfigureChildView(view);
899 return view;
902 void ShelfView::FadeIn(views::View* view) {
903 view->SetVisible(true);
904 view->layer()->SetOpacity(0);
905 AnimateToIdealBounds();
906 bounds_animator_->SetAnimationDelegate(
907 view, new FadeInAnimationDelegate(view), true);
910 void ShelfView::PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event) {
911 DCHECK(!dragging());
912 DCHECK(drag_view_);
913 drag_pointer_ = pointer;
914 start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
916 if (start_drag_index_== -1) {
917 CancelDrag(-1);
918 return;
921 // If the item is no longer draggable, bail out.
922 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
923 model_->items()[start_drag_index_].id);
924 if (!item_delegate->IsDraggable()) {
925 CancelDrag(-1);
926 return;
929 // Move the view to the front so that it appears on top of other views.
930 ReorderChildView(drag_view_, -1);
931 bounds_animator_->StopAnimatingView(drag_view_);
934 void ShelfView::ContinueDrag(const ui::LocatedEvent& event) {
935 // Due to a syncing operation the application might have been removed.
936 // Bail if it is gone.
937 int current_index = view_model_->GetIndexOfView(drag_view_);
938 DCHECK_NE(-1, current_index);
940 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
941 model_->items()[current_index].id);
942 if (!item_delegate->IsDraggable()) {
943 CancelDrag(-1);
944 return;
947 // If this is not a drag and drop host operation and not the app list item,
948 // check if the item got ripped off the shelf - if it did we are done.
949 if (!drag_and_drop_shelf_id_ &&
950 RemovableByRipOff(current_index) != NOT_REMOVABLE) {
951 if (HandleRipOffDrag(event))
952 return;
953 // The rip off handler could have changed the location of the item.
954 current_index = view_model_->GetIndexOfView(drag_view_);
957 // TODO: I don't think this works correctly with RTL.
958 gfx::Point drag_point(event.location());
959 ConvertPointToTarget(drag_view_, this, &drag_point);
961 // Constrain the location to the range of valid indices for the type.
962 std::pair<int, int> indices(GetDragRange(current_index));
963 int first_drag_index = indices.first;
964 int last_drag_index = indices.second;
965 // If the last index isn't valid, we're overflowing. Constrain to the app list
966 // (which is the last visible item).
967 if (first_drag_index < model_->FirstPanelIndex() &&
968 last_drag_index > last_visible_index_)
969 last_drag_index = last_visible_index_;
970 int x = 0, y = 0;
971 if (layout_manager_->IsHorizontalAlignment()) {
972 x = std::max(view_model_->ideal_bounds(indices.first).x(),
973 drag_point.x() - drag_offset_);
974 x = std::min(view_model_->ideal_bounds(last_drag_index).right() -
975 view_model_->ideal_bounds(current_index).width(),
977 if (drag_view_->x() == x)
978 return;
979 drag_view_->SetX(x);
980 } else {
981 y = std::max(view_model_->ideal_bounds(indices.first).y(),
982 drag_point.y() - drag_offset_);
983 y = std::min(view_model_->ideal_bounds(last_drag_index).bottom() -
984 view_model_->ideal_bounds(current_index).height(),
986 if (drag_view_->y() == y)
987 return;
988 drag_view_->SetY(y);
991 int target_index =
992 views::ViewModelUtils::DetermineMoveIndex(
993 *view_model_, drag_view_,
994 layout_manager_->IsHorizontalAlignment() ?
995 views::ViewModelUtils::HORIZONTAL :
996 views::ViewModelUtils::VERTICAL,
997 x, y);
998 target_index =
999 std::min(indices.second, std::max(target_index, indices.first));
1000 if (target_index == current_index)
1001 return;
1003 // Change the model, the ShelfItemMoved() callback will handle the
1004 // |view_model_| update.
1005 model_->Move(current_index, target_index);
1006 bounds_animator_->StopAnimatingView(drag_view_);
1009 bool ShelfView::HandleRipOffDrag(const ui::LocatedEvent& event) {
1010 int current_index = view_model_->GetIndexOfView(drag_view_);
1011 DCHECK_NE(-1, current_index);
1012 std::string dragged_app_id =
1013 delegate_->GetAppIDForShelfID(model_->items()[current_index].id);
1015 gfx::Point screen_location = event.root_location();
1016 ash::wm::ConvertPointToScreen(GetWidget()->GetNativeWindow()->GetRootWindow(),
1017 &screen_location);
1019 // To avoid ugly forwards and backwards flipping we use different constants
1020 // for ripping off / re-inserting the items.
1021 if (dragged_off_shelf_) {
1022 // If the shelf/overflow bubble bounds contains |screen_location| we insert
1023 // the item back into the shelf.
1024 if (GetBoundsForDragInsertInScreen().Contains(screen_location)) {
1025 if (dragged_off_from_overflow_to_shelf_) {
1026 // During the dragging an item from Shelf to Overflow, it can enter here
1027 // directly because both are located very closly.
1028 main_shelf_->EndDrag(true);
1029 // Stops the animation of |drag_view_| and sets its bounds explicitly
1030 // becase ContinueDrag() stops its animation. Without this, unexpected
1031 // bounds will be set.
1032 bounds_animator_->StopAnimatingView(drag_view_);
1033 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1034 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index));
1035 dragged_off_from_overflow_to_shelf_ = false;
1037 // Destroy our proxy view item.
1038 DestroyDragIconProxy();
1039 // Re-insert the item and return simply false since the caller will handle
1040 // the move as in any normal case.
1041 dragged_off_shelf_ = false;
1042 drag_view_->layer()->SetOpacity(1.0f);
1043 // The size of Overflow bubble should be updated immediately when an item
1044 // is re-inserted.
1045 if (is_overflow_mode())
1046 PreferredSizeChanged();
1047 return false;
1048 } else if (is_overflow_mode() &&
1049 main_shelf_->GetBoundsForDragInsertInScreen().Contains(
1050 screen_location)) {
1051 if (!dragged_off_from_overflow_to_shelf_) {
1052 dragged_off_from_overflow_to_shelf_ = true;
1053 drag_image_->SetOpacity(1.0f);
1054 main_shelf_->StartDrag(dragged_app_id, screen_location);
1055 } else {
1056 main_shelf_->Drag(screen_location);
1058 } else if (dragged_off_from_overflow_to_shelf_) {
1059 // Makes the |drag_image_| partially disappear again.
1060 dragged_off_from_overflow_to_shelf_ = false;
1061 drag_image_->SetOpacity(kDraggedImageOpacity);
1062 main_shelf_->EndDrag(true);
1063 bounds_animator_->StopAnimatingView(drag_view_);
1064 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1065 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index));
1067 // Move our proxy view item.
1068 UpdateDragIconProxy(screen_location);
1069 return true;
1071 // Check if we are too far away from the shelf to enter the ripped off state.
1072 // Determine the distance to the shelf.
1073 int delta = CalculateShelfDistance(screen_location);
1074 if (delta > kRipOffDistance) {
1075 // Create a proxy view item which can be moved anywhere.
1076 ShelfButton* button = static_cast<ShelfButton*>(drag_view_);
1077 CreateDragIconProxy(event.root_location(),
1078 button->GetImage(),
1079 drag_view_,
1080 gfx::Vector2d(0, 0),
1081 kDragAndDropProxyScale);
1082 drag_view_->layer()->SetOpacity(0.0f);
1083 dragged_off_shelf_ = true;
1084 if (RemovableByRipOff(current_index) == REMOVABLE) {
1085 // Move the item to the front of the first panel item and hide it.
1086 // ShelfItemMoved() callback will handle the |view_model_| update and
1087 // call AnimateToIdealBounds().
1088 if (current_index != model_->FirstPanelIndex() - 1) {
1089 model_->Move(current_index, model_->FirstPanelIndex() - 1);
1090 StartFadeInLastVisibleItem();
1091 } else if (is_overflow_mode()) {
1092 // Overflow bubble should be shrunk when an item is ripped off.
1093 PreferredSizeChanged();
1095 // Make the item partially disappear to show that it will get removed if
1096 // dropped.
1097 drag_image_->SetOpacity(kDraggedImageOpacity);
1099 return true;
1101 return false;
1104 void ShelfView::FinalizeRipOffDrag(bool cancel) {
1105 if (!dragged_off_shelf_)
1106 return;
1107 // Make sure we do not come in here again.
1108 dragged_off_shelf_ = false;
1110 // Coming here we should always have a |drag_view_|.
1111 DCHECK(drag_view_);
1112 int current_index = view_model_->GetIndexOfView(drag_view_);
1113 // If the view isn't part of the model anymore (|current_index| == -1), a sync
1114 // operation must have removed it. In that case we shouldn't change the model
1115 // and only delete the proxy image.
1116 if (current_index == -1) {
1117 DestroyDragIconProxy();
1118 return;
1121 // Set to true when the animation should snap back to where it was before.
1122 bool snap_back = false;
1123 // Items which cannot be dragged off will be handled as a cancel.
1124 if (!cancel) {
1125 if (dragged_off_from_overflow_to_shelf_) {
1126 dragged_off_from_overflow_to_shelf_ = false;
1127 main_shelf_->EndDrag(false);
1128 drag_view_->layer()->SetOpacity(1.0f);
1129 } else if (RemovableByRipOff(current_index) != REMOVABLE) {
1130 // Make sure we do not try to remove un-removable items like items which
1131 // were not pinned or have to be always there.
1132 cancel = true;
1133 snap_back = true;
1134 } else {
1135 // Make sure the item stays invisible upon removal.
1136 drag_view_->SetVisible(false);
1137 std::string app_id =
1138 delegate_->GetAppIDForShelfID(model_->items()[current_index].id);
1139 delegate_->UnpinAppWithID(app_id);
1142 if (cancel || snap_back) {
1143 if (dragged_off_from_overflow_to_shelf_) {
1144 dragged_off_from_overflow_to_shelf_ = false;
1145 // Main shelf handles revert of dragged item.
1146 main_shelf_->EndDrag(true);
1147 drag_view_->layer()->SetOpacity(1.0f);
1148 } else if (!cancelling_drag_model_changed_) {
1149 // Only do something if the change did not come through a model change.
1150 gfx::Rect drag_bounds = drag_image_->GetBoundsInScreen();
1151 gfx::Point relative_to = GetBoundsInScreen().origin();
1152 gfx::Rect target(
1153 gfx::PointAtOffsetFromOrigin(drag_bounds.origin()- relative_to),
1154 drag_bounds.size());
1155 drag_view_->SetBoundsRect(target);
1156 // Hide the status from the active item since we snap it back now. Upon
1157 // animation end the flag gets cleared if |snap_back_from_rip_off_view_|
1158 // is set.
1159 snap_back_from_rip_off_view_ = drag_view_;
1160 ShelfButton* button = static_cast<ShelfButton*>(drag_view_);
1161 button->AddState(ShelfButton::STATE_HIDDEN);
1162 // When a canceling drag model is happening, the view model is diverged
1163 // from the menu model and movements / animations should not be done.
1164 model_->Move(current_index, start_drag_index_);
1165 AnimateToIdealBounds();
1167 drag_view_->layer()->SetOpacity(1.0f);
1169 DestroyDragIconProxy();
1172 ShelfView::RemovableState ShelfView::RemovableByRipOff(int index) const {
1173 DCHECK(index >= 0 && index < model_->item_count());
1174 ShelfItemType type = model_->items()[index].type;
1175 if (type == TYPE_APP_LIST || type == TYPE_DIALOG || !delegate_->CanPin())
1176 return NOT_REMOVABLE;
1178 std::string app_id = delegate_->GetAppIDForShelfID(model_->items()[index].id);
1179 // Note: Only pinned app shortcuts can be removed!
1180 return (type == TYPE_APP_SHORTCUT && delegate_->IsAppPinned(app_id)) ?
1181 REMOVABLE : DRAGGABLE;
1184 bool ShelfView::SameDragType(ShelfItemType typea, ShelfItemType typeb) const {
1185 switch (typea) {
1186 case TYPE_APP_SHORTCUT:
1187 case TYPE_BROWSER_SHORTCUT:
1188 return (typeb == TYPE_APP_SHORTCUT || typeb == TYPE_BROWSER_SHORTCUT);
1189 case TYPE_APP_LIST:
1190 case TYPE_PLATFORM_APP:
1191 case TYPE_WINDOWED_APP:
1192 case TYPE_APP_PANEL:
1193 case TYPE_DIALOG:
1194 return typeb == typea;
1195 case TYPE_UNDEFINED:
1196 NOTREACHED() << "ShelfItemType must be set.";
1197 return false;
1199 NOTREACHED();
1200 return false;
1203 std::pair<int, int> ShelfView::GetDragRange(int index) {
1204 int min_index = -1;
1205 int max_index = -1;
1206 ShelfItemType type = model_->items()[index].type;
1207 for (int i = 0; i < model_->item_count(); ++i) {
1208 if (SameDragType(model_->items()[i].type, type)) {
1209 if (min_index == -1)
1210 min_index = i;
1211 max_index = i;
1214 return std::pair<int, int>(min_index, max_index);
1217 void ShelfView::ConfigureChildView(views::View* view) {
1218 view->SetPaintToLayer(true);
1219 view->layer()->SetFillsBoundsOpaquely(false);
1222 void ShelfView::ToggleOverflowBubble() {
1223 if (IsShowingOverflowBubble()) {
1224 overflow_bubble_->Hide();
1225 return;
1228 if (!overflow_bubble_)
1229 overflow_bubble_.reset(new OverflowBubble());
1231 ShelfView* overflow_view =
1232 new ShelfView(model_, delegate_, layout_manager_);
1233 overflow_view->overflow_mode_ = true;
1234 overflow_view->Init();
1235 overflow_view->set_owner_overflow_bubble(overflow_bubble_.get());
1236 overflow_view->OnShelfAlignmentChanged();
1237 overflow_view->main_shelf_ = this;
1238 UpdateOverflowRange(overflow_view);
1240 overflow_bubble_->Show(overflow_button_, overflow_view);
1242 Shell::GetInstance()->UpdateShelfVisibility();
1245 void ShelfView::OnFadeOutAnimationEnded() {
1246 AnimateToIdealBounds();
1247 StartFadeInLastVisibleItem();
1250 void ShelfView::StartFadeInLastVisibleItem() {
1251 // If overflow button is visible and there is a valid new last item, fading
1252 // the new last item in after sliding animation is finished.
1253 if (overflow_button_->visible() && last_visible_index_ >= 0) {
1254 views::View* last_visible_view = view_model_->view_at(last_visible_index_);
1255 last_visible_view->layer()->SetOpacity(0);
1256 bounds_animator_->SetAnimationDelegate(
1257 last_visible_view,
1258 new ShelfView::StartFadeAnimationDelegate(this, last_visible_view),
1259 true);
1263 void ShelfView::UpdateOverflowRange(ShelfView* overflow_view) const {
1264 const int first_overflow_index = last_visible_index_ + 1;
1265 const int last_overflow_index = last_hidden_index_;
1266 DCHECK_LE(first_overflow_index, last_overflow_index);
1267 DCHECK_LT(last_overflow_index, view_model_->view_size());
1269 overflow_view->first_visible_index_ = first_overflow_index;
1270 overflow_view->last_visible_index_ = last_overflow_index;
1273 bool ShelfView::ShouldHideTooltip(const gfx::Point& cursor_location) {
1274 gfx::Rect active_bounds;
1276 for (int i = 0; i < child_count(); ++i) {
1277 views::View* child = child_at(i);
1278 if (child == overflow_button_)
1279 continue;
1280 if (!ShouldShowTooltipForView(child))
1281 continue;
1283 gfx::Rect child_bounds = child->GetMirroredBounds();
1284 active_bounds.Union(child_bounds);
1287 return !active_bounds.Contains(cursor_location);
1290 gfx::Rect ShelfView::GetVisibleItemsBoundsInScreen() {
1291 gfx::Size preferred_size = GetPreferredSize();
1292 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1293 ConvertPointToScreen(this, &origin);
1294 return gfx::Rect(origin, preferred_size);
1297 gfx::Rect ShelfView::GetBoundsForDragInsertInScreen() {
1298 gfx::Size preferred_size;
1299 if (is_overflow_mode()) {
1300 DCHECK(owner_overflow_bubble_);
1301 gfx::Rect bubble_bounds =
1302 owner_overflow_bubble_->bubble_view()->GetBubbleBounds();
1303 preferred_size = bubble_bounds.size();
1304 } else {
1305 const int last_button_index = view_model_->view_size() - 1;
1306 gfx::Rect last_button_bounds =
1307 view_model_->view_at(last_button_index)->bounds();
1308 if (overflow_button_->visible() &&
1309 model_->GetItemIndexForType(TYPE_APP_PANEL) == -1) {
1310 // When overflow button is visible and shelf has no panel items,
1311 // last_button_bounds should be overflow button's bounds.
1312 last_button_bounds = overflow_button_->bounds();
1315 if (layout_manager_->IsHorizontalAlignment()) {
1316 preferred_size = gfx::Size(last_button_bounds.right() + leading_inset_,
1317 kShelfSize);
1318 } else {
1319 preferred_size = gfx::Size(kShelfSize,
1320 last_button_bounds.bottom() + leading_inset_);
1323 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1325 // In overflow mode, we should use OverflowBubbleView as a source for
1326 // converting |origin| to screen coordinates. When a scroll operation is
1327 // occurred in OverflowBubble, the bounds of ShelfView in OverflowBubble can
1328 // be changed.
1329 if (is_overflow_mode())
1330 ConvertPointToScreen(owner_overflow_bubble_->bubble_view(), &origin);
1331 else
1332 ConvertPointToScreen(this, &origin);
1334 return gfx::Rect(origin, preferred_size);
1337 int ShelfView::CancelDrag(int modified_index) {
1338 FinalizeRipOffDrag(true);
1339 if (!drag_view_)
1340 return modified_index;
1341 bool was_dragging = dragging();
1342 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1343 drag_pointer_ = NONE;
1344 drag_view_ = NULL;
1345 if (drag_view_index == modified_index) {
1346 // The view that was being dragged is being modified. Don't do anything.
1347 return modified_index;
1349 if (!was_dragging)
1350 return modified_index;
1352 // Restore previous position, tracking the position of the modified view.
1353 bool at_end = modified_index == view_model_->view_size();
1354 views::View* modified_view =
1355 (modified_index >= 0 && !at_end) ?
1356 view_model_->view_at(modified_index) : NULL;
1357 model_->Move(drag_view_index, start_drag_index_);
1359 // If the modified view will be at the end of the list, return the new end of
1360 // the list.
1361 if (at_end)
1362 return view_model_->view_size();
1363 return modified_view ? view_model_->GetIndexOfView(modified_view) : -1;
1366 gfx::Size ShelfView::GetPreferredSize() const {
1367 IdealBounds ideal_bounds;
1368 CalculateIdealBounds(&ideal_bounds);
1370 int last_button_index = is_overflow_mode() ?
1371 last_visible_index_ : view_model_->view_size() - 1;
1373 // When an item is dragged off from the overflow bubble, it is moved to last
1374 // position and and changed to invisible. Overflow bubble size should be
1375 // shrunk to fit only for visible items.
1376 // If |dragged_off_from_overflow_to_shelf_| is set, there will be no invisible
1377 // items in the shelf.
1378 if (is_overflow_mode() &&
1379 dragged_off_shelf_ &&
1380 !dragged_off_from_overflow_to_shelf_ &&
1381 RemovableByRipOff(view_model_->GetIndexOfView(drag_view_)) == REMOVABLE)
1382 last_button_index--;
1384 const gfx::Rect last_button_bounds =
1385 last_button_index >= first_visible_index_ ?
1386 view_model_->ideal_bounds(last_button_index) :
1387 gfx::Rect(gfx::Size(kShelfSize, kShelfSize));
1389 if (layout_manager_->IsHorizontalAlignment()) {
1390 return gfx::Size(last_button_bounds.right() + leading_inset_, kShelfSize);
1393 return gfx::Size(kShelfSize,
1394 last_button_bounds.bottom() + leading_inset_);
1397 void ShelfView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1398 // This bounds change is produced by the shelf movement and all content has
1399 // to follow. Using an animation at that time would produce a time lag since
1400 // the animation of the BoundsAnimator has itself a delay before it arrives
1401 // at the required location. As such we tell the animator to go there
1402 // immediately.
1403 BoundsAnimatorDisabler disabler(bounds_animator_.get());
1404 LayoutToIdealBounds();
1405 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1406 OnShelfIconPositionsChanged());
1408 if (IsShowingOverflowBubble())
1409 overflow_bubble_->Hide();
1412 views::FocusTraversable* ShelfView::GetPaneFocusTraversable() {
1413 return this;
1416 void ShelfView::GetAccessibleState(ui::AXViewState* state) {
1417 state->role = ui::AX_ROLE_TOOLBAR;
1418 state->name = l10n_util::GetStringUTF16(IDS_ASH_SHELF_ACCESSIBLE_NAME);
1421 void ShelfView::OnGestureEvent(ui::GestureEvent* event) {
1422 if (gesture_handler_.ProcessGestureEvent(*event))
1423 event->StopPropagation();
1426 void ShelfView::ShelfItemAdded(int model_index) {
1428 base::AutoReset<bool> cancelling_drag(
1429 &cancelling_drag_model_changed_, true);
1430 model_index = CancelDrag(model_index);
1432 views::View* view = CreateViewForItem(model_->items()[model_index]);
1433 AddChildView(view);
1434 // Hide the view, it'll be made visible when the animation is done. Using
1435 // opacity 0 here to avoid messing with CalculateIdealBounds which touches
1436 // the view's visibility.
1437 view->layer()->SetOpacity(0);
1438 view_model_->Add(view, model_index);
1440 // Give the button its ideal bounds. That way if we end up animating the
1441 // button before this animation completes it doesn't appear at some random
1442 // spot (because it was in the middle of animating from 0,0 0x0 to its
1443 // target).
1444 IdealBounds ideal_bounds;
1445 CalculateIdealBounds(&ideal_bounds);
1446 view->SetBoundsRect(view_model_->ideal_bounds(model_index));
1448 // The first animation moves all the views to their target position. |view|
1449 // is hidden, so it visually appears as though we are providing space for
1450 // it. When done we'll fade the view in.
1451 AnimateToIdealBounds();
1452 if (model_index <= last_visible_index_ ||
1453 model_index >= model_->FirstPanelIndex()) {
1454 bounds_animator_->SetAnimationDelegate(
1455 view, new StartFadeAnimationDelegate(this, view), true);
1456 } else {
1457 // Undo the hiding if animation does not run.
1458 view->layer()->SetOpacity(1.0f);
1462 void ShelfView::ShelfItemRemoved(int model_index, ShelfID id) {
1463 if (id == context_menu_id_)
1464 launcher_menu_runner_.reset();
1466 base::AutoReset<bool> cancelling_drag(
1467 &cancelling_drag_model_changed_, true);
1468 model_index = CancelDrag(model_index);
1470 views::View* view = view_model_->view_at(model_index);
1471 view_model_->Remove(model_index);
1473 // When the overflow bubble is visible, the overflow range needs to be set
1474 // before CalculateIdealBounds() gets called. Otherwise CalculateIdealBounds()
1475 // could trigger a ShelfItemChanged() by hiding the overflow bubble and
1476 // since the overflow bubble is not yet synced with the ShelfModel this
1477 // could cause a crash.
1478 if (overflow_bubble_ && overflow_bubble_->IsShowing()) {
1479 last_hidden_index_ = std::min(last_hidden_index_,
1480 view_model_->view_size() - 1);
1481 UpdateOverflowRange(overflow_bubble_->shelf_view());
1484 if (view->visible()) {
1485 // The first animation fades out the view. When done we'll animate the rest
1486 // of the views to their target location.
1487 bounds_animator_->AnimateViewTo(view, view->bounds());
1488 bounds_animator_->SetAnimationDelegate(
1489 view, new FadeOutAnimationDelegate(this, view), true);
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_offset_ = layout_manager_->PrimaryAxisValue(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_offset_) >= kMinimumDragDistance) ||
1594 (std::abs(event.y() - drag_offset_) >= 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;
1673 ScopedTargetRootWindow scoped_target(
1674 sender->GetWidget()->GetNativeView()->GetRootWindow());
1675 // Slow down activation animations if shift key is pressed.
1676 scoped_ptr<ui::ScopedAnimationDurationScaleMode> slowing_animations;
1677 if (event.IsShiftDown()) {
1678 slowing_animations.reset(new ui::ScopedAnimationDurationScaleMode(
1679 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION));
1682 // Collect usage statistics before we decide what to do with the click.
1683 switch (model_->items()[view_index].type) {
1684 case TYPE_APP_SHORTCUT:
1685 case TYPE_WINDOWED_APP:
1686 case TYPE_PLATFORM_APP:
1687 case TYPE_BROWSER_SHORTCUT:
1688 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1689 UMA_LAUNCHER_CLICK_ON_APP);
1690 break;
1692 case TYPE_APP_LIST:
1693 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1694 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON);
1695 break;
1697 case TYPE_APP_PANEL:
1698 case TYPE_DIALOG:
1699 break;
1701 case TYPE_UNDEFINED:
1702 NOTREACHED() << "ShelfItemType must be set.";
1703 break;
1706 ShelfItemDelegate* item_delegate =
1707 item_manager_->GetShelfItemDelegate(model_->items()[view_index].id);
1708 if (!item_delegate->ItemSelected(event))
1709 ShowListMenuForView(model_->items()[view_index], sender, event);
1713 bool ShelfView::ShowListMenuForView(const ShelfItem& item,
1714 views::View* source,
1715 const ui::Event& event) {
1716 ShelfItemDelegate* item_delegate =
1717 item_manager_->GetShelfItemDelegate(item.id);
1718 list_menu_model_.reset(item_delegate->CreateApplicationMenu(event.flags()));
1720 // Make sure we have a menu and it has at least two items in addition to the
1721 // application title and the 3 spacing separators.
1722 if (!list_menu_model_.get() || list_menu_model_->GetItemCount() <= 5)
1723 return false;
1725 ShowMenu(list_menu_model_.get(),
1726 source,
1727 gfx::Point(),
1728 false,
1729 ui::GetMenuSourceTypeForEvent(event));
1730 return true;
1733 void ShelfView::ShowContextMenuForView(views::View* source,
1734 const gfx::Point& point,
1735 ui::MenuSourceType source_type) {
1736 int view_index = view_model_->GetIndexOfView(source);
1737 if (view_index == -1) {
1738 Shell::GetInstance()->ShowContextMenu(point, source_type);
1739 return;
1742 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1743 model_->items()[view_index].id);
1744 context_menu_model_.reset(item_delegate->CreateContextMenu(
1745 source->GetWidget()->GetNativeView()->GetRootWindow()));
1746 if (!context_menu_model_)
1747 return;
1749 base::AutoReset<ShelfID> reseter(
1750 &context_menu_id_,
1751 view_index == -1 ? 0 : model_->items()[view_index].id);
1753 ShowMenu(context_menu_model_.get(),
1754 source,
1755 point,
1756 true,
1757 source_type);
1760 void ShelfView::ShowMenu(ui::MenuModel* menu_model,
1761 views::View* source,
1762 const gfx::Point& click_point,
1763 bool context_menu,
1764 ui::MenuSourceType source_type) {
1765 closing_event_time_ = base::TimeDelta();
1766 launcher_menu_runner_.reset(new views::MenuRunner(menu_model));
1768 ScopedTargetRootWindow scoped_target(
1769 source->GetWidget()->GetNativeView()->GetRootWindow());
1771 // Determine the menu alignment dependent on the shelf.
1772 views::MenuAnchorPosition menu_alignment = views::MENU_ANCHOR_TOPLEFT;
1773 gfx::Rect anchor_point = gfx::Rect(click_point, gfx::Size());
1775 ShelfWidget* shelf = RootWindowController::ForShelf(
1776 GetWidget()->GetNativeView())->shelf();
1777 if (!context_menu) {
1778 // Application lists use a bubble.
1779 ShelfAlignment align = shelf->GetAlignment();
1780 anchor_point = source->GetBoundsInScreen();
1782 // It is possible to invoke the menu while it is sliding into view. To cover
1783 // that case, the screen coordinates are offsetted by the animation delta.
1784 gfx::Vector2d offset =
1785 source->GetWidget()->GetNativeWindow()->bounds().origin() -
1786 source->GetWidget()->GetNativeWindow()->GetTargetBounds().origin();
1787 anchor_point.set_x(anchor_point.x() - offset.x());
1788 anchor_point.set_y(anchor_point.y() - offset.y());
1790 // Shelf items can have an asymmetrical border for spacing reasons.
1791 // Adjust anchor location for this.
1792 if (source->border())
1793 anchor_point.Inset(source->border()->GetInsets());
1795 switch (align) {
1796 case SHELF_ALIGNMENT_BOTTOM:
1797 menu_alignment = views::MENU_ANCHOR_BUBBLE_ABOVE;
1798 break;
1799 case SHELF_ALIGNMENT_LEFT:
1800 menu_alignment = views::MENU_ANCHOR_BUBBLE_RIGHT;
1801 break;
1802 case SHELF_ALIGNMENT_RIGHT:
1803 menu_alignment = views::MENU_ANCHOR_BUBBLE_LEFT;
1804 break;
1805 case SHELF_ALIGNMENT_TOP:
1806 menu_alignment = views::MENU_ANCHOR_BUBBLE_BELOW;
1807 break;
1810 // If this gets deleted while we are in the menu, the shelf will be gone
1811 // as well.
1812 bool got_deleted = false;
1813 got_deleted_ = &got_deleted;
1815 shelf->ForceUndimming(true);
1816 // NOTE: if you convert to HAS_MNEMONICS be sure and update menu building
1817 // code.
1818 if (launcher_menu_runner_->RunMenuAt(
1819 source->GetWidget(),
1820 NULL,
1821 anchor_point,
1822 menu_alignment,
1823 source_type,
1824 context_menu ? views::MenuRunner::CONTEXT_MENU : 0) ==
1825 views::MenuRunner::MENU_DELETED) {
1826 if (!got_deleted) {
1827 got_deleted_ = NULL;
1828 shelf->ForceUndimming(false);
1830 return;
1832 got_deleted_ = NULL;
1833 shelf->ForceUndimming(false);
1835 // If it is a context menu and we are showing overflow bubble
1836 // we want to hide overflow bubble.
1837 if (owner_overflow_bubble_)
1838 owner_overflow_bubble_->HideBubbleAndRefreshButton();
1840 // Unpinning an item will reset the |launcher_menu_runner_| before coming
1841 // here.
1842 if (launcher_menu_runner_)
1843 closing_event_time_ = launcher_menu_runner_->closing_event_time();
1844 Shell::GetInstance()->UpdateShelfVisibility();
1847 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) {
1848 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1849 OnShelfIconPositionsChanged());
1850 PreferredSizeChanged();
1853 void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
1854 if (snap_back_from_rip_off_view_ && animator == bounds_animator_) {
1855 if (!animator->IsAnimating(snap_back_from_rip_off_view_)) {
1856 // Coming here the animation of the ShelfButton is finished and the
1857 // previously hidden status can be shown again. Since the button itself
1858 // might have gone away or changed locations we check that the button
1859 // is still in the shelf and show its status again.
1860 for (int index = 0; index < view_model_->view_size(); index++) {
1861 views::View* view = view_model_->view_at(index);
1862 if (view == snap_back_from_rip_off_view_) {
1863 ShelfButton* button = static_cast<ShelfButton*>(view);
1864 button->ClearState(ShelfButton::STATE_HIDDEN);
1865 break;
1868 snap_back_from_rip_off_view_ = NULL;
1873 bool ShelfView::IsUsableEvent(const ui::Event& event) {
1874 if (closing_event_time_ == base::TimeDelta())
1875 return true;
1877 base::TimeDelta delta =
1878 base::TimeDelta(event.time_stamp() - closing_event_time_);
1879 closing_event_time_ = base::TimeDelta();
1880 // TODO(skuhne): This time seems excessive, but it appears that the reposting
1881 // takes that long. Need to come up with a better way of doing this.
1882 return (delta.InMilliseconds() < 0 || delta.InMilliseconds() > 130);
1885 const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const {
1886 int view_index = view_model_->GetIndexOfView(view);
1887 if (view_index == -1)
1888 return NULL;
1889 return &(model_->items()[view_index]);
1892 bool ShelfView::ShouldShowTooltipForView(const views::View* view) const {
1893 if (view == GetAppListButtonView() &&
1894 Shell::GetInstance()->GetAppListWindow())
1895 return false;
1896 const ShelfItem* item = ShelfItemForView(view);
1897 if (!item)
1898 return true;
1899 ShelfItemDelegate* item_delegate =
1900 item_manager_->GetShelfItemDelegate(item->id);
1901 return item_delegate->ShouldShowTooltip();
1904 int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const {
1905 ShelfWidget* shelf = RootWindowController::ForShelf(
1906 GetWidget()->GetNativeView())->shelf();
1907 ShelfAlignment align = shelf->GetAlignment();
1908 const gfx::Rect bounds = GetBoundsInScreen();
1909 int distance = 0;
1910 switch (align) {
1911 case SHELF_ALIGNMENT_BOTTOM:
1912 distance = bounds.y() - coordinate.y();
1913 break;
1914 case SHELF_ALIGNMENT_LEFT:
1915 distance = coordinate.x() - bounds.right();
1916 break;
1917 case SHELF_ALIGNMENT_RIGHT:
1918 distance = bounds.x() - coordinate.x();
1919 break;
1920 case SHELF_ALIGNMENT_TOP:
1921 distance = coordinate.y() - bounds.bottom();
1922 break;
1924 return distance > 0 ? distance : 0;
1927 } // namespace ash