ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ash / shelf / shelf_view.cc
blob40eafe58518ae403b26012f5bb4715d9e67d2ba4
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_strings.h"
36 #include "ui/accessibility/ax_view_state.h"
37 #include "ui/aura/client/screen_position_client.h"
38 #include "ui/aura/window.h"
39 #include "ui/aura/window_event_dispatcher.h"
40 #include "ui/base/l10n/l10n_util.h"
41 #include "ui/base/models/simple_menu_model.h"
42 #include "ui/base/resource/resource_bundle.h"
43 #include "ui/compositor/layer.h"
44 #include "ui/compositor/layer_animator.h"
45 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
46 #include "ui/events/event_utils.h"
47 #include "ui/gfx/canvas.h"
48 #include "ui/gfx/geometry/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 explicit 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 const gfx::FontList* GetLabelFontList(int command_id) const override;
146 bool IsCommandEnabled(int id) const override;
147 void GetHorizontalIconMargins(int id,
148 int icon_size,
149 int* left_margin,
150 int* right_margin) const override;
151 bool GetForegroundColor(int command_id,
152 bool is_hovered,
153 SkColor* override_color) const override;
154 bool GetBackgroundColor(int command_id,
155 bool is_hovered,
156 SkColor* override_color) const override;
157 int GetMaxWidthForMenu(views::MenuItemView* menu) override;
158 bool ShouldReserveSpaceForSubmenuIndicator() const override;
160 private:
161 ShelfMenuModel* menu_model_;
163 DISALLOW_COPY_AND_ASSIGN(ShelfMenuModelAdapter);
166 ShelfMenuModelAdapter::ShelfMenuModelAdapter(ShelfMenuModel* menu_model)
167 : MenuModelAdapter(menu_model),
168 menu_model_(menu_model) {
171 const gfx::FontList* ShelfMenuModelAdapter::GetLabelFontList(
172 int command_id) const {
173 if (command_id != kCommandIdOfMenuName)
174 return MenuModelAdapter::GetLabelFontList(command_id);
176 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
177 return &rb.GetFontList(ui::ResourceBundle::BoldFont);
180 bool ShelfMenuModelAdapter::IsCommandEnabled(int id) const {
181 return id != kCommandIdOfMenuName;
184 bool ShelfMenuModelAdapter::GetForegroundColor(int command_id,
185 bool is_hovered,
186 SkColor* override_color) const {
187 if (command_id != kCommandIdOfMenuName)
188 return false;
190 *override_color = kCaptionItemForegroundColor;
191 return true;
194 bool ShelfMenuModelAdapter::GetBackgroundColor(int command_id,
195 bool is_hovered,
196 SkColor* override_color) const {
197 if (!menu_model_->IsCommandActive(command_id))
198 return false;
200 *override_color = is_hovered ? kFocusedActiveListItemBackgroundColor :
201 kActiveListItemBackgroundColor;
202 return true;
205 void ShelfMenuModelAdapter::GetHorizontalIconMargins(int command_id,
206 int icon_size,
207 int* left_margin,
208 int* right_margin) const {
209 *left_margin = kHorizontalIconSpacing;
210 *right_margin = (command_id != kCommandIdOfMenuName) ?
211 kHorizontalIconSpacing : -(icon_size + kHorizontalNoIconInsetSpacing);
214 int ShelfMenuModelAdapter::GetMaxWidthForMenu(views::MenuItemView* menu) {
215 return kMaximumAppMenuItemLength;
218 bool ShelfMenuModelAdapter::ShouldReserveSpaceForSubmenuIndicator() const {
219 return false;
222 // Custom FocusSearch used to navigate the shelf in the order items are in
223 // the ViewModel.
224 class ShelfFocusSearch : public views::FocusSearch {
225 public:
226 explicit ShelfFocusSearch(views::ViewModel* view_model)
227 : FocusSearch(NULL, true, true),
228 view_model_(view_model) {}
229 ~ShelfFocusSearch() override {}
231 // views::FocusSearch overrides:
232 View* FindNextFocusableView(View* starting_view,
233 bool reverse,
234 Direction direction,
235 bool check_starting_view,
236 views::FocusTraversable** focus_traversable,
237 View** focus_traversable_view) override {
238 int index = view_model_->GetIndexOfView(starting_view);
239 if (index == -1)
240 return view_model_->view_at(0);
242 if (reverse) {
243 --index;
244 if (index < 0)
245 index = view_model_->view_size() - 1;
246 } else {
247 ++index;
248 if (index >= view_model_->view_size())
249 index = 0;
251 return view_model_->view_at(index);
254 private:
255 views::ViewModel* view_model_;
257 DISALLOW_COPY_AND_ASSIGN(ShelfFocusSearch);
260 // AnimationDelegate used when inserting a new item. This steadily increases the
261 // opacity of the layer as the animation progress.
262 class FadeInAnimationDelegate : public gfx::AnimationDelegate {
263 public:
264 explicit FadeInAnimationDelegate(views::View* view) : view_(view) {}
265 ~FadeInAnimationDelegate() override {}
267 // AnimationDelegate overrides:
268 void AnimationProgressed(const Animation* animation) override {
269 view_->layer()->SetOpacity(animation->GetCurrentValue());
270 view_->layer()->ScheduleDraw();
272 void AnimationEnded(const Animation* animation) override {
273 view_->layer()->SetOpacity(1.0f);
274 view_->layer()->ScheduleDraw();
276 void AnimationCanceled(const Animation* animation) override {
277 view_->layer()->SetOpacity(1.0f);
278 view_->layer()->ScheduleDraw();
281 private:
282 views::View* view_;
284 DISALLOW_COPY_AND_ASSIGN(FadeInAnimationDelegate);
287 void ReflectItemStatus(const ShelfItem& item, ShelfButton* button) {
288 switch (item.status) {
289 case STATUS_CLOSED:
290 button->ClearState(ShelfButton::STATE_ACTIVE);
291 button->ClearState(ShelfButton::STATE_RUNNING);
292 button->ClearState(ShelfButton::STATE_ATTENTION);
293 break;
294 case STATUS_RUNNING:
295 button->ClearState(ShelfButton::STATE_ACTIVE);
296 button->AddState(ShelfButton::STATE_RUNNING);
297 button->ClearState(ShelfButton::STATE_ATTENTION);
298 break;
299 case STATUS_ACTIVE:
300 button->AddState(ShelfButton::STATE_ACTIVE);
301 button->ClearState(ShelfButton::STATE_RUNNING);
302 button->ClearState(ShelfButton::STATE_ATTENTION);
303 break;
304 case STATUS_ATTENTION:
305 button->ClearState(ShelfButton::STATE_ACTIVE);
306 button->ClearState(ShelfButton::STATE_RUNNING);
307 button->AddState(ShelfButton::STATE_ATTENTION);
308 break;
312 } // namespace
314 // AnimationDelegate used when deleting an item. This steadily decreased the
315 // opacity of the layer as the animation progress.
316 class ShelfView::FadeOutAnimationDelegate : public gfx::AnimationDelegate {
317 public:
318 FadeOutAnimationDelegate(ShelfView* host, views::View* view)
319 : shelf_view_(host),
320 view_(view) {}
321 ~FadeOutAnimationDelegate() override {}
323 // AnimationDelegate overrides:
324 void AnimationProgressed(const Animation* animation) override {
325 view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
326 view_->layer()->ScheduleDraw();
328 void AnimationEnded(const Animation* animation) override {
329 shelf_view_->OnFadeOutAnimationEnded();
331 void AnimationCanceled(const Animation* animation) override {}
333 private:
334 ShelfView* shelf_view_;
335 scoped_ptr<views::View> view_;
337 DISALLOW_COPY_AND_ASSIGN(FadeOutAnimationDelegate);
340 // AnimationDelegate used to trigger fading an element in. When an item is
341 // inserted this delegate is attached to the animation that expands the size of
342 // the item. When done it kicks off another animation to fade the item in.
343 class ShelfView::StartFadeAnimationDelegate : public gfx::AnimationDelegate {
344 public:
345 StartFadeAnimationDelegate(ShelfView* host,
346 views::View* view)
347 : shelf_view_(host),
348 view_(view) {}
349 ~StartFadeAnimationDelegate() override {}
351 // AnimationDelegate overrides:
352 void AnimationEnded(const Animation* animation) override {
353 shelf_view_->FadeIn(view_);
355 void AnimationCanceled(const Animation* animation) override {
356 view_->layer()->SetOpacity(1.0f);
359 private:
360 ShelfView* shelf_view_;
361 views::View* view_;
363 DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate);
366 ShelfView::ShelfView(ShelfModel* model,
367 ShelfDelegate* delegate,
368 ShelfLayoutManager* manager)
369 : model_(model),
370 delegate_(delegate),
371 view_model_(new views::ViewModel),
372 first_visible_index_(0),
373 last_visible_index_(-1),
374 overflow_button_(NULL),
375 owner_overflow_bubble_(NULL),
376 drag_pointer_(NONE),
377 drag_view_(NULL),
378 start_drag_index_(-1),
379 context_menu_id_(0),
380 leading_inset_(kDefaultLeadingInset),
381 cancelling_drag_model_changed_(false),
382 last_hidden_index_(0),
383 closing_event_time_(base::TimeDelta()),
384 got_deleted_(NULL),
385 drag_and_drop_item_pinned_(false),
386 drag_and_drop_shelf_id_(0),
387 dragged_off_shelf_(false),
388 snap_back_from_rip_off_view_(NULL),
389 item_manager_(Shell::GetInstance()->shelf_item_delegate_manager()),
390 layout_manager_(manager),
391 overflow_mode_(false),
392 main_shelf_(NULL),
393 dragged_off_from_overflow_to_shelf_(false),
394 is_repost_event_(false),
395 last_pressed_index_(-1) {
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 views::View* view = view_model_->view_at(index);
467 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
468 ShelfButton* button = static_cast<ShelfButton*>(view);
469 gfx::Rect icon_bounds = button->GetIconBounds();
470 return gfx::Rect(GetMirroredXWithWidthInView(
471 ideal_bounds.x() + icon_bounds.x(), icon_bounds.width()),
472 ideal_bounds.y() + icon_bounds.y(),
473 icon_bounds.width(),
474 icon_bounds.height());
477 void ShelfView::UpdatePanelIconPosition(ShelfID id,
478 const gfx::Point& midpoint) {
479 int current_index = model_->ItemIndexByID(id);
480 int first_panel_index = model_->FirstPanelIndex();
481 if (current_index < first_panel_index)
482 return;
484 gfx::Point midpoint_in_view(GetMirroredXInView(midpoint.x()),
485 midpoint.y());
486 int target_index = current_index;
487 while (target_index > first_panel_index &&
488 layout_manager_->PrimaryAxisValue(
489 view_model_->ideal_bounds(target_index).x(),
490 view_model_->ideal_bounds(target_index).y()) >
491 layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
492 midpoint_in_view.y())) {
493 --target_index;
495 while (target_index < view_model_->view_size() - 1 &&
496 layout_manager_->PrimaryAxisValue(
497 view_model_->ideal_bounds(target_index).right(),
498 view_model_->ideal_bounds(target_index).bottom()) <
499 layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
500 midpoint_in_view.y())) {
501 ++target_index;
503 if (current_index != target_index)
504 model_->Move(current_index, target_index);
507 bool ShelfView::IsShowingMenu() const {
508 return (launcher_menu_runner_.get() &&
509 launcher_menu_runner_->IsRunning());
512 bool ShelfView::IsShowingOverflowBubble() const {
513 return overflow_bubble_.get() && overflow_bubble_->IsShowing();
516 views::View* ShelfView::GetAppListButtonView() const {
517 for (int i = 0; i < model_->item_count(); ++i) {
518 if (model_->items()[i].type == TYPE_APP_LIST)
519 return view_model_->view_at(i);
522 NOTREACHED() << "Applist button not found";
523 return NULL;
526 ////////////////////////////////////////////////////////////////////////////////
527 // ShelfView, FocusTraversable implementation:
529 views::FocusSearch* ShelfView::GetFocusSearch() {
530 return focus_search_.get();
533 views::FocusTraversable* ShelfView::GetFocusTraversableParent() {
534 return parent()->GetFocusTraversable();
537 View* ShelfView::GetFocusTraversableParentView() {
538 return this;
541 void ShelfView::CreateDragIconProxy(
542 const gfx::Point& location_in_screen_coordinates,
543 const gfx::ImageSkia& icon,
544 views::View* replaced_view,
545 const gfx::Vector2d& cursor_offset_from_center,
546 float scale_factor) {
547 drag_replaced_view_ = replaced_view;
548 drag_image_.reset(new ash::DragImageView(
549 drag_replaced_view_->GetWidget()->GetNativeWindow()->GetRootWindow(),
550 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE));
551 drag_image_->SetImage(icon);
552 gfx::Size size = drag_image_->GetPreferredSize();
553 size.set_width(size.width() * scale_factor);
554 size.set_height(size.height() * scale_factor);
555 drag_image_offset_ = gfx::Vector2d(size.width() / 2, size.height() / 2) +
556 cursor_offset_from_center;
557 gfx::Rect drag_image_bounds(
558 location_in_screen_coordinates - drag_image_offset_,
559 size);
560 drag_image_->SetBoundsInScreen(drag_image_bounds);
561 drag_image_->SetWidgetVisible(true);
564 void ShelfView::UpdateDragIconProxy(
565 const gfx::Point& location_in_screen_coordinates) {
566 // TODO(jennyz): Investigate why drag_image_ becomes NULL at this point per
567 // crbug.com/34722, while the app list item is still being dragged around.
568 if (drag_image_) {
569 drag_image_->SetScreenPosition(
570 location_in_screen_coordinates - drag_image_offset_);
574 void ShelfView::DestroyDragIconProxy() {
575 drag_image_.reset();
576 drag_image_offset_ = gfx::Vector2d(0, 0);
579 bool ShelfView::StartDrag(const std::string& app_id,
580 const gfx::Point& location_in_screen_coordinates) {
581 // Bail if an operation is already going on - or the cursor is not inside.
582 // This could happen if mouse / touch operations overlap.
583 if (drag_and_drop_shelf_id_ ||
584 !GetBoundsInScreen().Contains(location_in_screen_coordinates))
585 return false;
587 // If the AppsGridView (which was dispatching this event) was opened by our
588 // button, ShelfView dragging operations are locked and we have to unlock.
589 CancelDrag(-1);
590 drag_and_drop_item_pinned_ = false;
591 drag_and_drop_app_id_ = app_id;
592 drag_and_drop_shelf_id_ =
593 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_);
594 // Check if the application is known and pinned - if not, we have to pin it so
595 // that we can re-arrange the shelf order accordingly. Note that items have
596 // to be pinned to give them the same (order) possibilities as a shortcut.
597 // When an item is dragged from overflow to shelf, IsShowingOverflowBubble()
598 // returns true. At this time, we don't need to pin the item.
599 if (!IsShowingOverflowBubble() &&
600 (!drag_and_drop_shelf_id_ ||
601 !delegate_->IsAppPinned(app_id))) {
602 delegate_->PinAppWithID(app_id);
603 drag_and_drop_shelf_id_ =
604 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_);
605 if (!drag_and_drop_shelf_id_)
606 return false;
607 drag_and_drop_item_pinned_ = true;
609 views::View* drag_and_drop_view = view_model_->view_at(
610 model_->ItemIndexByID(drag_and_drop_shelf_id_));
611 DCHECK(drag_and_drop_view);
613 // Since there is already an icon presented by the caller, we hide this item
614 // for now. That has to be done by reducing the size since the visibility will
615 // change once a regrouping animation is performed.
616 pre_drag_and_drop_size_ = drag_and_drop_view->size();
617 drag_and_drop_view->SetSize(gfx::Size());
619 // First we have to center the mouse cursor over the item.
620 gfx::Point pt = drag_and_drop_view->GetBoundsInScreen().CenterPoint();
621 views::View::ConvertPointFromScreen(drag_and_drop_view, &pt);
622 gfx::Point point_in_root = location_in_screen_coordinates;
623 ::wm::ConvertPointFromScreen(
624 ash::wm::GetRootWindowAt(location_in_screen_coordinates), &point_in_root);
625 ui::MouseEvent event(ui::ET_MOUSE_PRESSED, pt, point_in_root,
626 ui::EventTimeForNow(), 0, 0);
627 PointerPressedOnButton(drag_and_drop_view,
628 ShelfButtonHost::DRAG_AND_DROP,
629 event);
631 // Drag the item where it really belongs.
632 Drag(location_in_screen_coordinates);
633 return true;
636 bool ShelfView::Drag(const gfx::Point& location_in_screen_coordinates) {
637 if (!drag_and_drop_shelf_id_ ||
638 !GetBoundsInScreen().Contains(location_in_screen_coordinates))
639 return false;
641 gfx::Point pt = location_in_screen_coordinates;
642 views::View* drag_and_drop_view = view_model_->view_at(
643 model_->ItemIndexByID(drag_and_drop_shelf_id_));
644 ConvertPointFromScreen(drag_and_drop_view, &pt);
645 gfx::Point point_in_root = location_in_screen_coordinates;
646 ::wm::ConvertPointFromScreen(
647 ash::wm::GetRootWindowAt(location_in_screen_coordinates), &point_in_root);
648 ui::MouseEvent event(ui::ET_MOUSE_DRAGGED, pt, point_in_root,
649 ui::EventTimeForNow(), 0, 0);
650 PointerDraggedOnButton(drag_and_drop_view,
651 ShelfButtonHost::DRAG_AND_DROP,
652 event);
653 return true;
656 void ShelfView::EndDrag(bool cancel) {
657 if (!drag_and_drop_shelf_id_)
658 return;
660 views::View* drag_and_drop_view = view_model_->view_at(
661 model_->ItemIndexByID(drag_and_drop_shelf_id_));
662 PointerReleasedOnButton(
663 drag_and_drop_view, ShelfButtonHost::DRAG_AND_DROP, cancel);
665 // Either destroy the temporarily created item - or - make the item visible.
666 if (drag_and_drop_item_pinned_ && cancel) {
667 delegate_->UnpinAppWithID(drag_and_drop_app_id_);
668 } else if (drag_and_drop_view) {
669 if (cancel) {
670 // When a hosted drag gets canceled, the item can remain in the same slot
671 // and it might have moved within the bounds. In that case the item need
672 // to animate back to its correct location.
673 AnimateToIdealBounds();
674 } else {
675 drag_and_drop_view->SetSize(pre_drag_and_drop_size_);
679 drag_and_drop_shelf_id_ = 0;
682 void ShelfView::LayoutToIdealBounds() {
683 if (bounds_animator_->IsAnimating()) {
684 AnimateToIdealBounds();
685 return;
688 IdealBounds ideal_bounds;
689 CalculateIdealBounds(&ideal_bounds);
690 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
691 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
694 void ShelfView::UpdateAllButtonsVisibilityInOverflowMode() {
695 // The overflow button is not shown in overflow mode.
696 overflow_button_->SetVisible(false);
697 DCHECK_LT(last_visible_index_, view_model_->view_size());
698 for (int i = 0; i < view_model_->view_size(); ++i) {
699 bool visible = i >= first_visible_index_ &&
700 i <= last_visible_index_;
701 // To track the dragging of |drag_view_| continuously, its visibility
702 // should be always true regardless of its position.
703 if (dragged_off_from_overflow_to_shelf_ &&
704 view_model_->view_at(i) == drag_view_)
705 view_model_->view_at(i)->SetVisible(true);
706 else
707 view_model_->view_at(i)->SetVisible(visible);
711 void ShelfView::CalculateIdealBounds(IdealBounds* bounds) const {
712 int available_size = layout_manager_->PrimaryAxisValue(width(), height());
713 DCHECK(model_->item_count() == view_model_->view_size());
714 if (!available_size)
715 return;
717 int first_panel_index = model_->FirstPanelIndex();
718 int last_button_index = first_panel_index - 1;
720 int x = 0;
721 int y = 0;
722 int button_size = kShelfButtonSize;
723 int button_spacing = kShelfButtonSpacing;
725 int w = layout_manager_->PrimaryAxisValue(button_size, width());
726 int h = layout_manager_->PrimaryAxisValue(height(), button_size);
727 for (int i = 0; i < view_model_->view_size(); ++i) {
728 if (i < first_visible_index_) {
729 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, 0, 0));
730 continue;
733 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
734 if (i != last_button_index) {
735 x = layout_manager_->PrimaryAxisValue(x + w + button_spacing, x);
736 y = layout_manager_->PrimaryAxisValue(y, y + h + button_spacing);
740 if (is_overflow_mode()) {
741 const_cast<ShelfView*>(this)->UpdateAllButtonsVisibilityInOverflowMode();
742 return;
745 // Right aligned icons.
746 int end_position = available_size - button_spacing;
747 x = layout_manager_->PrimaryAxisValue(end_position, 0);
748 y = layout_manager_->PrimaryAxisValue(0, end_position);
749 for (int i = view_model_->view_size() - 1;
750 i >= first_panel_index; --i) {
751 x = layout_manager_->PrimaryAxisValue(x - w - button_spacing, x);
752 y = layout_manager_->PrimaryAxisValue(y, y - h - button_spacing);
753 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
754 end_position = layout_manager_->PrimaryAxisValue(x, y);
757 // Icons on the left / top are guaranteed up to kLeftIconProportion of
758 // the available space.
759 int last_icon_position = layout_manager_->PrimaryAxisValue(
760 view_model_->ideal_bounds(last_button_index).right(),
761 view_model_->ideal_bounds(last_button_index).bottom()) + button_size;
762 int reserved_icon_space = available_size * kReservedNonPanelIconProportion;
763 if (last_icon_position < reserved_icon_space)
764 end_position = last_icon_position;
765 else
766 end_position = std::max(end_position, reserved_icon_space);
768 bounds->overflow_bounds.set_size(
769 gfx::Size(layout_manager_->PrimaryAxisValue(w, width()),
770 layout_manager_->PrimaryAxisValue(height(), h)));
772 last_visible_index_ = DetermineLastVisibleIndex(
773 end_position - button_size);
774 last_hidden_index_ = DetermineFirstVisiblePanelIndex(end_position) - 1;
775 bool show_overflow = last_visible_index_ < last_button_index ||
776 last_hidden_index_ >= first_panel_index;
778 // Create Space for the overflow button
779 if (show_overflow &&
780 last_visible_index_ > 0 && last_visible_index_ < last_button_index)
781 --last_visible_index_;
782 for (int i = 0; i < view_model_->view_size(); ++i) {
783 bool visible = i <= last_visible_index_ || i > last_hidden_index_;
784 // To receive drag event continuously from |drag_view_| during the dragging
785 // off from the shelf, don't make |drag_view_| invisible. It will be
786 // eventually invisible and removed from the |view_model_| by
787 // FinalizeRipOffDrag().
788 if (dragged_off_shelf_ && view_model_->view_at(i) == drag_view_)
789 continue;
790 view_model_->view_at(i)->SetVisible(visible);
793 overflow_button_->SetVisible(show_overflow);
794 if (show_overflow) {
795 DCHECK_NE(0, view_model_->view_size());
796 if (last_visible_index_ == -1) {
797 x = 0;
798 y = 0;
799 } else {
800 x = layout_manager_->PrimaryAxisValue(
801 view_model_->ideal_bounds(last_visible_index_).right(),
802 view_model_->ideal_bounds(last_visible_index_).x());
803 y = layout_manager_->PrimaryAxisValue(
804 view_model_->ideal_bounds(last_visible_index_).y(),
805 view_model_->ideal_bounds(last_visible_index_).bottom());
807 // Set all hidden panel icon positions to be on the overflow button.
808 for (int i = first_panel_index; i <= last_hidden_index_; ++i)
809 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
811 // Add more space between last visible item and overflow button.
812 // Without this, two buttons look too close compared with other items.
813 x = layout_manager_->PrimaryAxisValue(x + button_spacing, x);
814 y = layout_manager_->PrimaryAxisValue(y, y + button_spacing);
816 bounds->overflow_bounds.set_x(x);
817 bounds->overflow_bounds.set_y(y);
818 if (overflow_bubble_.get() && overflow_bubble_->IsShowing())
819 UpdateOverflowRange(overflow_bubble_->shelf_view());
820 } else {
821 if (overflow_bubble_)
822 overflow_bubble_->Hide();
826 int ShelfView::DetermineLastVisibleIndex(int max_value) const {
827 int index = model_->FirstPanelIndex() - 1;
828 while (index >= 0 &&
829 layout_manager_->PrimaryAxisValue(
830 view_model_->ideal_bounds(index).right(),
831 view_model_->ideal_bounds(index).bottom()) > max_value) {
832 index--;
834 return index;
837 int ShelfView::DetermineFirstVisiblePanelIndex(int min_value) const {
838 int index = model_->FirstPanelIndex();
839 while (index < view_model_->view_size() &&
840 layout_manager_->PrimaryAxisValue(
841 view_model_->ideal_bounds(index).right(),
842 view_model_->ideal_bounds(index).bottom()) < min_value) {
843 ++index;
845 return index;
848 void ShelfView::AddIconObserver(ShelfIconObserver* observer) {
849 observers_.AddObserver(observer);
852 void ShelfView::RemoveIconObserver(ShelfIconObserver* observer) {
853 observers_.RemoveObserver(observer);
856 void ShelfView::AnimateToIdealBounds() {
857 IdealBounds ideal_bounds;
858 CalculateIdealBounds(&ideal_bounds);
859 for (int i = 0; i < view_model_->view_size(); ++i) {
860 View* view = view_model_->view_at(i);
861 bounds_animator_->AnimateViewTo(view, view_model_->ideal_bounds(i));
862 // Now that the item animation starts, we have to make sure that the
863 // padding of the first gets properly transferred to the new first item.
864 if (i && view->border())
865 view->SetBorder(views::Border::NullBorder());
867 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
870 views::View* ShelfView::CreateViewForItem(const ShelfItem& item) {
871 views::View* view = NULL;
872 switch (item.type) {
873 case TYPE_BROWSER_SHORTCUT:
874 case TYPE_APP_SHORTCUT:
875 case TYPE_WINDOWED_APP:
876 case TYPE_PLATFORM_APP:
877 case TYPE_DIALOG:
878 case TYPE_APP_PANEL: {
879 ShelfButton* button = ShelfButton::Create(this, this, layout_manager_);
880 button->SetImage(item.image);
881 ReflectItemStatus(item, button);
882 view = button;
883 break;
886 case TYPE_APP_LIST: {
887 view = new AppListButton(this, this, layout_manager_->shelf_widget());
888 break;
891 default:
892 break;
894 view->set_context_menu_controller(this);
896 DCHECK(view);
897 ConfigureChildView(view);
898 return view;
901 void ShelfView::FadeIn(views::View* view) {
902 view->SetVisible(true);
903 view->layer()->SetOpacity(0);
904 AnimateToIdealBounds();
905 bounds_animator_->SetAnimationDelegate(
906 view,
907 scoped_ptr<gfx::AnimationDelegate>(new FadeInAnimationDelegate(view)));
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_origin_.x());
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_origin_.y());
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 ::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 CreateDragIconProxy(event.root_location(),
1077 drag_view_->GetImage(),
1078 drag_view_,
1079 gfx::Vector2d(0, 0),
1080 kDragAndDropProxyScale);
1081 drag_view_->layer()->SetOpacity(0.0f);
1082 dragged_off_shelf_ = true;
1083 if (RemovableByRipOff(current_index) == REMOVABLE) {
1084 // Move the item to the front of the first panel item and hide it.
1085 // ShelfItemMoved() callback will handle the |view_model_| update and
1086 // call AnimateToIdealBounds().
1087 if (current_index != model_->FirstPanelIndex() - 1) {
1088 model_->Move(current_index, model_->FirstPanelIndex() - 1);
1089 StartFadeInLastVisibleItem();
1090 } else if (is_overflow_mode()) {
1091 // Overflow bubble should be shrunk when an item is ripped off.
1092 PreferredSizeChanged();
1094 // Make the item partially disappear to show that it will get removed if
1095 // dropped.
1096 drag_image_->SetOpacity(kDraggedImageOpacity);
1098 return true;
1100 return false;
1103 void ShelfView::FinalizeRipOffDrag(bool cancel) {
1104 if (!dragged_off_shelf_)
1105 return;
1106 // Make sure we do not come in here again.
1107 dragged_off_shelf_ = false;
1109 // Coming here we should always have a |drag_view_|.
1110 DCHECK(drag_view_);
1111 int current_index = view_model_->GetIndexOfView(drag_view_);
1112 // If the view isn't part of the model anymore (|current_index| == -1), a sync
1113 // operation must have removed it. In that case we shouldn't change the model
1114 // and only delete the proxy image.
1115 if (current_index == -1) {
1116 DestroyDragIconProxy();
1117 return;
1120 // Set to true when the animation should snap back to where it was before.
1121 bool snap_back = false;
1122 // Items which cannot be dragged off will be handled as a cancel.
1123 if (!cancel) {
1124 if (dragged_off_from_overflow_to_shelf_) {
1125 dragged_off_from_overflow_to_shelf_ = false;
1126 main_shelf_->EndDrag(false);
1127 drag_view_->layer()->SetOpacity(1.0f);
1128 } else if (RemovableByRipOff(current_index) != REMOVABLE) {
1129 // Make sure we do not try to remove un-removable items like items which
1130 // were not pinned or have to be always there.
1131 cancel = true;
1132 snap_back = true;
1133 } else {
1134 // Make sure the item stays invisible upon removal.
1135 drag_view_->SetVisible(false);
1136 std::string app_id =
1137 delegate_->GetAppIDForShelfID(model_->items()[current_index].id);
1138 delegate_->UnpinAppWithID(app_id);
1141 if (cancel || snap_back) {
1142 if (dragged_off_from_overflow_to_shelf_) {
1143 dragged_off_from_overflow_to_shelf_ = false;
1144 // Main shelf handles revert of dragged item.
1145 main_shelf_->EndDrag(true);
1146 drag_view_->layer()->SetOpacity(1.0f);
1147 } else if (!cancelling_drag_model_changed_) {
1148 // Only do something if the change did not come through a model change.
1149 gfx::Rect drag_bounds = drag_image_->GetBoundsInScreen();
1150 gfx::Point relative_to = GetBoundsInScreen().origin();
1151 gfx::Rect target(
1152 gfx::PointAtOffsetFromOrigin(drag_bounds.origin()- relative_to),
1153 drag_bounds.size());
1154 drag_view_->SetBoundsRect(target);
1155 // Hide the status from the active item since we snap it back now. Upon
1156 // animation end the flag gets cleared if |snap_back_from_rip_off_view_|
1157 // is set.
1158 snap_back_from_rip_off_view_ = drag_view_;
1159 drag_view_->AddState(ShelfButton::STATE_HIDDEN);
1160 // When a canceling drag model is happening, the view model is diverged
1161 // from the menu model and movements / animations should not be done.
1162 model_->Move(current_index, start_drag_index_);
1163 AnimateToIdealBounds();
1165 drag_view_->layer()->SetOpacity(1.0f);
1167 DestroyDragIconProxy();
1170 ShelfView::RemovableState ShelfView::RemovableByRipOff(int index) const {
1171 DCHECK(index >= 0 && index < model_->item_count());
1172 ShelfItemType type = model_->items()[index].type;
1173 if (type == TYPE_APP_LIST || type == TYPE_DIALOG || !delegate_->CanPin())
1174 return NOT_REMOVABLE;
1176 std::string app_id = delegate_->GetAppIDForShelfID(model_->items()[index].id);
1177 // Note: Only pinned app shortcuts can be removed!
1178 return (type == TYPE_APP_SHORTCUT && delegate_->IsAppPinned(app_id)) ?
1179 REMOVABLE : DRAGGABLE;
1182 bool ShelfView::SameDragType(ShelfItemType typea, ShelfItemType typeb) const {
1183 switch (typea) {
1184 case TYPE_APP_SHORTCUT:
1185 case TYPE_BROWSER_SHORTCUT:
1186 return (typeb == TYPE_APP_SHORTCUT || typeb == TYPE_BROWSER_SHORTCUT);
1187 case TYPE_APP_LIST:
1188 case TYPE_PLATFORM_APP:
1189 case TYPE_WINDOWED_APP:
1190 case TYPE_APP_PANEL:
1191 case TYPE_DIALOG:
1192 return typeb == typea;
1193 case TYPE_UNDEFINED:
1194 NOTREACHED() << "ShelfItemType must be set.";
1195 return false;
1197 NOTREACHED();
1198 return false;
1201 std::pair<int, int> ShelfView::GetDragRange(int index) {
1202 int min_index = -1;
1203 int max_index = -1;
1204 ShelfItemType type = model_->items()[index].type;
1205 for (int i = 0; i < model_->item_count(); ++i) {
1206 if (SameDragType(model_->items()[i].type, type)) {
1207 if (min_index == -1)
1208 min_index = i;
1209 max_index = i;
1212 return std::pair<int, int>(min_index, max_index);
1215 void ShelfView::ConfigureChildView(views::View* view) {
1216 view->SetPaintToLayer(true);
1217 view->layer()->SetFillsBoundsOpaquely(false);
1220 void ShelfView::ToggleOverflowBubble() {
1221 if (IsShowingOverflowBubble()) {
1222 overflow_bubble_->Hide();
1223 return;
1226 if (!overflow_bubble_)
1227 overflow_bubble_.reset(new OverflowBubble());
1229 ShelfView* overflow_view =
1230 new ShelfView(model_, delegate_, layout_manager_);
1231 overflow_view->overflow_mode_ = true;
1232 overflow_view->Init();
1233 overflow_view->set_owner_overflow_bubble(overflow_bubble_.get());
1234 overflow_view->OnShelfAlignmentChanged();
1235 overflow_view->main_shelf_ = this;
1236 UpdateOverflowRange(overflow_view);
1238 overflow_bubble_->Show(overflow_button_, overflow_view);
1240 Shell::GetInstance()->UpdateShelfVisibility();
1243 void ShelfView::OnFadeOutAnimationEnded() {
1244 AnimateToIdealBounds();
1245 StartFadeInLastVisibleItem();
1248 void ShelfView::StartFadeInLastVisibleItem() {
1249 // If overflow button is visible and there is a valid new last item, fading
1250 // the new last item in after sliding animation is finished.
1251 if (overflow_button_->visible() && last_visible_index_ >= 0) {
1252 views::View* last_visible_view = view_model_->view_at(last_visible_index_);
1253 last_visible_view->layer()->SetOpacity(0);
1254 bounds_animator_->SetAnimationDelegate(
1255 last_visible_view,
1256 scoped_ptr<gfx::AnimationDelegate>(
1257 new StartFadeAnimationDelegate(this, last_visible_view)));
1261 void ShelfView::UpdateOverflowRange(ShelfView* overflow_view) const {
1262 const int first_overflow_index = last_visible_index_ + 1;
1263 const int last_overflow_index = last_hidden_index_;
1264 DCHECK_LE(first_overflow_index, last_overflow_index);
1265 DCHECK_LT(last_overflow_index, view_model_->view_size());
1267 overflow_view->first_visible_index_ = first_overflow_index;
1268 overflow_view->last_visible_index_ = last_overflow_index;
1271 bool ShelfView::ShouldHideTooltip(const gfx::Point& cursor_location) {
1272 gfx::Rect active_bounds;
1274 for (int i = 0; i < child_count(); ++i) {
1275 views::View* child = child_at(i);
1276 if (child == overflow_button_)
1277 continue;
1278 if (!ShouldShowTooltipForView(child))
1279 continue;
1281 gfx::Rect child_bounds = child->GetMirroredBounds();
1282 active_bounds.Union(child_bounds);
1285 return !active_bounds.Contains(cursor_location);
1288 gfx::Rect ShelfView::GetVisibleItemsBoundsInScreen() {
1289 gfx::Size preferred_size = GetPreferredSize();
1290 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1291 ConvertPointToScreen(this, &origin);
1292 return gfx::Rect(origin, preferred_size);
1295 gfx::Rect ShelfView::GetBoundsForDragInsertInScreen() {
1296 gfx::Size preferred_size;
1297 if (is_overflow_mode()) {
1298 DCHECK(owner_overflow_bubble_);
1299 gfx::Rect bubble_bounds =
1300 owner_overflow_bubble_->bubble_view()->GetBubbleBounds();
1301 preferred_size = bubble_bounds.size();
1302 } else {
1303 const int last_button_index = view_model_->view_size() - 1;
1304 gfx::Rect last_button_bounds =
1305 view_model_->view_at(last_button_index)->bounds();
1306 if (overflow_button_->visible() &&
1307 model_->GetItemIndexForType(TYPE_APP_PANEL) == -1) {
1308 // When overflow button is visible and shelf has no panel items,
1309 // last_button_bounds should be overflow button's bounds.
1310 last_button_bounds = overflow_button_->bounds();
1313 if (layout_manager_->IsHorizontalAlignment()) {
1314 preferred_size = gfx::Size(last_button_bounds.right() + leading_inset_,
1315 kShelfSize);
1316 } else {
1317 preferred_size = gfx::Size(kShelfSize,
1318 last_button_bounds.bottom() + leading_inset_);
1321 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1323 // In overflow mode, we should use OverflowBubbleView as a source for
1324 // converting |origin| to screen coordinates. When a scroll operation is
1325 // occurred in OverflowBubble, the bounds of ShelfView in OverflowBubble can
1326 // be changed.
1327 if (is_overflow_mode())
1328 ConvertPointToScreen(owner_overflow_bubble_->bubble_view(), &origin);
1329 else
1330 ConvertPointToScreen(this, &origin);
1332 return gfx::Rect(origin, preferred_size);
1335 int ShelfView::CancelDrag(int modified_index) {
1336 FinalizeRipOffDrag(true);
1337 if (!drag_view_)
1338 return modified_index;
1339 bool was_dragging = dragging();
1340 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1341 drag_pointer_ = NONE;
1342 drag_view_ = NULL;
1343 if (drag_view_index == modified_index) {
1344 // The view that was being dragged is being modified. Don't do anything.
1345 return modified_index;
1347 if (!was_dragging)
1348 return modified_index;
1350 // Restore previous position, tracking the position of the modified view.
1351 bool at_end = modified_index == view_model_->view_size();
1352 views::View* modified_view =
1353 (modified_index >= 0 && !at_end) ?
1354 view_model_->view_at(modified_index) : NULL;
1355 model_->Move(drag_view_index, start_drag_index_);
1357 // If the modified view will be at the end of the list, return the new end of
1358 // the list.
1359 if (at_end)
1360 return view_model_->view_size();
1361 return modified_view ? view_model_->GetIndexOfView(modified_view) : -1;
1364 gfx::Size ShelfView::GetPreferredSize() const {
1365 IdealBounds ideal_bounds;
1366 CalculateIdealBounds(&ideal_bounds);
1368 int last_button_index = is_overflow_mode() ?
1369 last_visible_index_ : view_model_->view_size() - 1;
1371 // When an item is dragged off from the overflow bubble, it is moved to last
1372 // position and and changed to invisible. Overflow bubble size should be
1373 // shrunk to fit only for visible items.
1374 // If |dragged_off_from_overflow_to_shelf_| is set, there will be no invisible
1375 // items in the shelf.
1376 if (is_overflow_mode() &&
1377 dragged_off_shelf_ &&
1378 !dragged_off_from_overflow_to_shelf_ &&
1379 RemovableByRipOff(view_model_->GetIndexOfView(drag_view_)) == REMOVABLE)
1380 last_button_index--;
1382 const gfx::Rect last_button_bounds =
1383 last_button_index >= first_visible_index_ ?
1384 view_model_->ideal_bounds(last_button_index) :
1385 gfx::Rect(gfx::Size(kShelfSize, kShelfSize));
1387 if (layout_manager_->IsHorizontalAlignment()) {
1388 return gfx::Size(last_button_bounds.right() + leading_inset_, kShelfSize);
1391 return gfx::Size(kShelfSize,
1392 last_button_bounds.bottom() + leading_inset_);
1395 void ShelfView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1396 // This bounds change is produced by the shelf movement and all content has
1397 // to follow. Using an animation at that time would produce a time lag since
1398 // the animation of the BoundsAnimator has itself a delay before it arrives
1399 // at the required location. As such we tell the animator to go there
1400 // immediately.
1401 BoundsAnimatorDisabler disabler(bounds_animator_.get());
1402 LayoutToIdealBounds();
1403 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1404 OnShelfIconPositionsChanged());
1406 if (IsShowingOverflowBubble())
1407 overflow_bubble_->Hide();
1410 views::FocusTraversable* ShelfView::GetPaneFocusTraversable() {
1411 return this;
1414 void ShelfView::GetAccessibleState(ui::AXViewState* state) {
1415 state->role = ui::AX_ROLE_TOOLBAR;
1416 state->name = l10n_util::GetStringUTF16(IDS_ASH_SHELF_ACCESSIBLE_NAME);
1419 void ShelfView::OnGestureEvent(ui::GestureEvent* event) {
1420 if (gesture_handler_.ProcessGestureEvent(*event))
1421 event->StopPropagation();
1424 void ShelfView::ShelfItemAdded(int model_index) {
1426 base::AutoReset<bool> cancelling_drag(
1427 &cancelling_drag_model_changed_, true);
1428 model_index = CancelDrag(model_index);
1430 views::View* view = CreateViewForItem(model_->items()[model_index]);
1431 AddChildView(view);
1432 // Hide the view, it'll be made visible when the animation is done. Using
1433 // opacity 0 here to avoid messing with CalculateIdealBounds which touches
1434 // the view's visibility.
1435 view->layer()->SetOpacity(0);
1436 view_model_->Add(view, model_index);
1438 // Give the button its ideal bounds. That way if we end up animating the
1439 // button before this animation completes it doesn't appear at some random
1440 // spot (because it was in the middle of animating from 0,0 0x0 to its
1441 // target).
1442 IdealBounds ideal_bounds;
1443 CalculateIdealBounds(&ideal_bounds);
1444 view->SetBoundsRect(view_model_->ideal_bounds(model_index));
1446 // The first animation moves all the views to their target position. |view|
1447 // is hidden, so it visually appears as though we are providing space for
1448 // it. When done we'll fade the view in.
1449 AnimateToIdealBounds();
1450 if (model_index <= last_visible_index_ ||
1451 model_index >= model_->FirstPanelIndex()) {
1452 bounds_animator_->SetAnimationDelegate(
1453 view,
1454 scoped_ptr<gfx::AnimationDelegate>(
1455 new StartFadeAnimationDelegate(this, view)));
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,
1490 scoped_ptr<gfx::AnimationDelegate>(
1491 new FadeOutAnimationDelegate(this, view)));
1492 } else {
1493 // We don't need to show a fade out animation for invisible |view|. When an
1494 // item is ripped out from the shelf, its |view| is already invisible.
1495 AnimateToIdealBounds();
1498 // Close the tooltip because it isn't needed any longer and its anchor view
1499 // will be deleted soon.
1500 if (tooltip_->GetCurrentAnchorView() == view)
1501 tooltip_->Close();
1504 void ShelfView::ShelfItemChanged(int model_index, const ShelfItem& old_item) {
1505 const ShelfItem& item(model_->items()[model_index]);
1506 if (old_item.type != item.type) {
1507 // Type changed, swap the views.
1508 model_index = CancelDrag(model_index);
1509 scoped_ptr<views::View> old_view(view_model_->view_at(model_index));
1510 bounds_animator_->StopAnimatingView(old_view.get());
1511 // Removing and re-inserting a view in our view model will strip the ideal
1512 // bounds from the item. To avoid recalculation of everything the bounds
1513 // get remembered and restored after the insertion to the previous value.
1514 gfx::Rect old_ideal_bounds = view_model_->ideal_bounds(model_index);
1515 view_model_->Remove(model_index);
1516 views::View* new_view = CreateViewForItem(item);
1517 AddChildView(new_view);
1518 view_model_->Add(new_view, model_index);
1519 view_model_->set_ideal_bounds(model_index, old_ideal_bounds);
1520 new_view->SetBoundsRect(old_view->bounds());
1521 return;
1524 views::View* view = view_model_->view_at(model_index);
1525 switch (item.type) {
1526 case TYPE_BROWSER_SHORTCUT:
1527 // Fallthrough for the new Shelf since it needs to show the activation
1528 // change as well.
1529 case TYPE_APP_SHORTCUT:
1530 case TYPE_WINDOWED_APP:
1531 case TYPE_PLATFORM_APP:
1532 case TYPE_DIALOG:
1533 case TYPE_APP_PANEL: {
1534 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1535 ShelfButton* button = static_cast<ShelfButton*>(view);
1536 ReflectItemStatus(item, button);
1537 // The browser shortcut is currently not a "real" item and as such the
1538 // the image is bogous as well. We therefore keep the image as is for it.
1539 if (item.type != TYPE_BROWSER_SHORTCUT)
1540 button->SetImage(item.image);
1541 button->SchedulePaint();
1542 break;
1545 default:
1546 break;
1550 void ShelfView::ShelfItemMoved(int start_index, int target_index) {
1551 view_model_->Move(start_index, target_index);
1552 // When cancelling a drag due to a shelf item being added, the currently
1553 // dragged item is moved back to its initial position. AnimateToIdealBounds
1554 // will be called again when the new item is added to the |view_model_| but
1555 // at this time the |view_model_| is inconsistent with the |model_|.
1556 if (!cancelling_drag_model_changed_)
1557 AnimateToIdealBounds();
1560 void ShelfView::ShelfStatusChanged() {
1561 // Nothing to do here.
1564 void ShelfView::PointerPressedOnButton(views::View* view,
1565 Pointer pointer,
1566 const ui::LocatedEvent& event) {
1567 if (drag_view_)
1568 return;
1570 int index = view_model_->GetIndexOfView(view);
1571 if (index == -1)
1572 return;
1574 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1575 model_->items()[index].id);
1576 if (view_model_->view_size() <= 1 || !item_delegate->IsDraggable())
1577 return; // View is being deleted or not draggable, ignore request.
1579 // Only when the repost event occurs on the same shelf item, we should ignore
1580 // the call in ShelfView::ButtonPressed(...).
1581 is_repost_event_ = IsRepostEvent(event) && (last_pressed_index_ == index);
1583 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1584 drag_view_ = static_cast<ShelfButton*>(view);
1585 drag_origin_ = gfx::Point(event.x(), event.y());
1586 UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage",
1587 layout_manager_->SelectValueForShelfAlignment(
1588 SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM,
1589 SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT,
1590 SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT,
1591 -1),
1592 SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT);
1595 void ShelfView::PointerDraggedOnButton(views::View* view,
1596 Pointer pointer,
1597 const ui::LocatedEvent& event) {
1598 // To prepare all drag types (moving an item in the shelf and dragging off),
1599 // we should check the x-axis and y-axis offset.
1600 if (!dragging() && drag_view_ &&
1601 ((std::abs(event.x() - drag_origin_.x()) >= kMinimumDragDistance) ||
1602 (std::abs(event.y() - drag_origin_.y()) >= kMinimumDragDistance))) {
1603 PrepareForDrag(pointer, event);
1605 if (drag_pointer_ == pointer)
1606 ContinueDrag(event);
1609 void ShelfView::PointerReleasedOnButton(views::View* view,
1610 Pointer pointer,
1611 bool canceled) {
1612 // Reset |is_repost_event| to false.
1613 is_repost_event_ = false;
1615 if (canceled) {
1616 CancelDrag(-1);
1617 } else if (drag_pointer_ == pointer) {
1618 FinalizeRipOffDrag(false);
1619 drag_pointer_ = NONE;
1620 AnimateToIdealBounds();
1622 // If the drag pointer is NONE, no drag operation is going on and the
1623 // drag_view can be released.
1624 if (drag_pointer_ == NONE)
1625 drag_view_ = NULL;
1628 void ShelfView::MouseMovedOverButton(views::View* view) {
1629 if (!ShouldShowTooltipForView(view))
1630 return;
1632 if (!tooltip_->IsVisible())
1633 tooltip_->ResetTimer();
1636 void ShelfView::MouseEnteredButton(views::View* view) {
1637 if (!ShouldShowTooltipForView(view))
1638 return;
1640 if (tooltip_->IsVisible()) {
1641 tooltip_->ShowImmediately(view, GetAccessibleName(view));
1642 } else {
1643 tooltip_->ShowDelayed(view, GetAccessibleName(view));
1647 void ShelfView::MouseExitedButton(views::View* view) {
1648 if (!tooltip_->IsVisible())
1649 tooltip_->StopTimer();
1652 base::string16 ShelfView::GetAccessibleName(const views::View* view) {
1653 int view_index = view_model_->GetIndexOfView(view);
1654 // May be -1 while in the process of animating closed.
1655 if (view_index == -1)
1656 return base::string16();
1658 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1659 model_->items()[view_index].id);
1660 return item_delegate->GetTitle();
1663 void ShelfView::ButtonPressed(views::Button* sender, const ui::Event& event) {
1664 // Do not handle mouse release during drag.
1665 if (dragging())
1666 return;
1668 if (sender == overflow_button_) {
1669 ToggleOverflowBubble();
1670 return;
1673 int view_index = view_model_->GetIndexOfView(sender);
1674 // May be -1 while in the process of animating closed.
1675 if (view_index == -1)
1676 return;
1678 // If the menu was just closed by the same event as this one, we ignore
1679 // the call and don't open the menu again. See crbug.com/343005 for more
1680 // detail.
1681 if (is_repost_event_)
1682 return;
1684 // Record the index for the last pressed shelf item.
1685 last_pressed_index_ = view_index;
1687 // Don't activate the item twice on double-click. Otherwise the window starts
1688 // animating open due to the first click, then immediately minimizes due to
1689 // the second click. The user most likely intended to open or minimize the
1690 // item once, not do both.
1691 if (event.flags() & ui::EF_IS_DOUBLE_CLICK)
1692 return;
1695 ScopedTargetRootWindow scoped_target(
1696 sender->GetWidget()->GetNativeView()->GetRootWindow());
1697 // Slow down activation animations if shift key is pressed.
1698 scoped_ptr<ui::ScopedAnimationDurationScaleMode> slowing_animations;
1699 if (event.IsShiftDown()) {
1700 slowing_animations.reset(new ui::ScopedAnimationDurationScaleMode(
1701 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION));
1704 // Collect usage statistics before we decide what to do with the click.
1705 switch (model_->items()[view_index].type) {
1706 case TYPE_APP_SHORTCUT:
1707 case TYPE_WINDOWED_APP:
1708 case TYPE_PLATFORM_APP:
1709 case TYPE_BROWSER_SHORTCUT:
1710 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1711 UMA_LAUNCHER_CLICK_ON_APP);
1712 break;
1714 case TYPE_APP_LIST:
1715 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1716 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON);
1717 break;
1719 case TYPE_APP_PANEL:
1720 case TYPE_DIALOG:
1721 break;
1723 case TYPE_UNDEFINED:
1724 NOTREACHED() << "ShelfItemType must be set.";
1725 break;
1728 ShelfItemDelegate* item_delegate =
1729 item_manager_->GetShelfItemDelegate(model_->items()[view_index].id);
1730 if (!item_delegate->ItemSelected(event))
1731 ShowListMenuForView(model_->items()[view_index], sender, event);
1735 bool ShelfView::ShowListMenuForView(const ShelfItem& item,
1736 views::View* source,
1737 const ui::Event& event) {
1738 ShelfItemDelegate* item_delegate =
1739 item_manager_->GetShelfItemDelegate(item.id);
1740 scoped_ptr<ui::MenuModel> list_menu_model(
1741 item_delegate->CreateApplicationMenu(event.flags()));
1743 // Make sure we have a menu and it has at least two items in addition to the
1744 // application title and the 3 spacing separators.
1745 if (!list_menu_model.get() || list_menu_model->GetItemCount() <= 5)
1746 return false;
1748 ShowMenu(list_menu_model.get(),
1749 source,
1750 gfx::Point(),
1751 false,
1752 ui::GetMenuSourceTypeForEvent(event));
1753 return true;
1756 void ShelfView::ShowContextMenuForView(views::View* source,
1757 const gfx::Point& point,
1758 ui::MenuSourceType source_type) {
1759 int view_index = view_model_->GetIndexOfView(source);
1760 if (view_index == -1) {
1761 Shell::GetInstance()->ShowContextMenu(point, source_type);
1762 return;
1765 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1766 model_->items()[view_index].id);
1767 context_menu_model_.reset(item_delegate->CreateContextMenu(
1768 source->GetWidget()->GetNativeView()->GetRootWindow()));
1769 if (!context_menu_model_)
1770 return;
1772 base::AutoReset<ShelfID> reseter(
1773 &context_menu_id_,
1774 view_index == -1 ? 0 : model_->items()[view_index].id);
1776 ShowMenu(context_menu_model_.get(),
1777 source,
1778 point,
1779 true,
1780 source_type);
1783 void ShelfView::ShowMenu(ui::MenuModel* menu_model,
1784 views::View* source,
1785 const gfx::Point& click_point,
1786 bool context_menu,
1787 ui::MenuSourceType source_type) {
1788 closing_event_time_ = base::TimeDelta();
1789 launcher_menu_runner_.reset(new views::MenuRunner(
1790 menu_model, context_menu ? views::MenuRunner::CONTEXT_MENU : 0));
1792 ScopedTargetRootWindow scoped_target(
1793 source->GetWidget()->GetNativeView()->GetRootWindow());
1795 // Determine the menu alignment dependent on the shelf.
1796 views::MenuAnchorPosition menu_alignment = views::MENU_ANCHOR_TOPLEFT;
1797 gfx::Rect anchor_point = gfx::Rect(click_point, gfx::Size());
1799 ShelfWidget* shelf = RootWindowController::ForShelf(
1800 GetWidget()->GetNativeView())->shelf();
1801 if (!context_menu) {
1802 // Application lists use a bubble.
1803 ShelfAlignment align = shelf->GetAlignment();
1804 anchor_point = source->GetBoundsInScreen();
1806 // It is possible to invoke the menu while it is sliding into view. To cover
1807 // that case, the screen coordinates are offsetted by the animation delta.
1808 gfx::Vector2d offset =
1809 source->GetWidget()->GetNativeWindow()->bounds().origin() -
1810 source->GetWidget()->GetNativeWindow()->GetTargetBounds().origin();
1811 anchor_point.set_x(anchor_point.x() - offset.x());
1812 anchor_point.set_y(anchor_point.y() - offset.y());
1814 // Shelf items can have an asymmetrical border for spacing reasons.
1815 // Adjust anchor location for this.
1816 if (source->border())
1817 anchor_point.Inset(source->border()->GetInsets());
1819 switch (align) {
1820 case SHELF_ALIGNMENT_BOTTOM:
1821 menu_alignment = views::MENU_ANCHOR_BUBBLE_ABOVE;
1822 break;
1823 case SHELF_ALIGNMENT_LEFT:
1824 menu_alignment = views::MENU_ANCHOR_BUBBLE_RIGHT;
1825 break;
1826 case SHELF_ALIGNMENT_RIGHT:
1827 menu_alignment = views::MENU_ANCHOR_BUBBLE_LEFT;
1828 break;
1829 case SHELF_ALIGNMENT_TOP:
1830 menu_alignment = views::MENU_ANCHOR_BUBBLE_BELOW;
1831 break;
1834 // If this gets deleted while we are in the menu, the shelf will be gone
1835 // as well.
1836 bool got_deleted = false;
1837 got_deleted_ = &got_deleted;
1839 shelf->ForceUndimming(true);
1840 // NOTE: if you convert to HAS_MNEMONICS be sure and update menu building
1841 // code.
1842 if (launcher_menu_runner_->RunMenuAt(source->GetWidget(),
1843 NULL,
1844 anchor_point,
1845 menu_alignment,
1846 source_type) ==
1847 views::MenuRunner::MENU_DELETED) {
1848 if (!got_deleted) {
1849 got_deleted_ = NULL;
1850 shelf->ForceUndimming(false);
1852 return;
1854 got_deleted_ = NULL;
1855 shelf->ForceUndimming(false);
1857 // If it is a context menu and we are showing overflow bubble
1858 // we want to hide overflow bubble.
1859 if (owner_overflow_bubble_)
1860 owner_overflow_bubble_->HideBubbleAndRefreshButton();
1862 // Unpinning an item will reset the |launcher_menu_runner_| before coming
1863 // here.
1864 if (launcher_menu_runner_)
1865 closing_event_time_ = launcher_menu_runner_->closing_event_time();
1866 Shell::GetInstance()->UpdateShelfVisibility();
1869 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) {
1870 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1871 OnShelfIconPositionsChanged());
1872 PreferredSizeChanged();
1875 void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
1876 if (snap_back_from_rip_off_view_ && animator == bounds_animator_) {
1877 if (!animator->IsAnimating(snap_back_from_rip_off_view_)) {
1878 // Coming here the animation of the ShelfButton is finished and the
1879 // previously hidden status can be shown again. Since the button itself
1880 // might have gone away or changed locations we check that the button
1881 // is still in the shelf and show its status again.
1882 for (int index = 0; index < view_model_->view_size(); index++) {
1883 views::View* view = view_model_->view_at(index);
1884 if (view == snap_back_from_rip_off_view_) {
1885 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1886 ShelfButton* button = static_cast<ShelfButton*>(view);
1887 button->ClearState(ShelfButton::STATE_HIDDEN);
1888 break;
1891 snap_back_from_rip_off_view_ = NULL;
1896 bool ShelfView::IsRepostEvent(const ui::Event& event) {
1897 if (closing_event_time_ == base::TimeDelta())
1898 return false;
1900 base::TimeDelta delta =
1901 base::TimeDelta(event.time_stamp() - closing_event_time_);
1902 closing_event_time_ = base::TimeDelta();
1903 // If the current (press down) event is a repost event, the time stamp of
1904 // these two events should be the same.
1905 return (delta.InMilliseconds() == 0);
1908 const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const {
1909 int view_index = view_model_->GetIndexOfView(view);
1910 if (view_index == -1)
1911 return NULL;
1912 return &(model_->items()[view_index]);
1915 bool ShelfView::ShouldShowTooltipForView(const views::View* view) const {
1916 if (view == GetAppListButtonView() &&
1917 Shell::GetInstance()->GetAppListWindow())
1918 return false;
1919 const ShelfItem* item = ShelfItemForView(view);
1920 if (!item)
1921 return true;
1922 ShelfItemDelegate* item_delegate =
1923 item_manager_->GetShelfItemDelegate(item->id);
1924 return item_delegate->ShouldShowTooltip();
1927 int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const {
1928 ShelfWidget* shelf = RootWindowController::ForShelf(
1929 GetWidget()->GetNativeView())->shelf();
1930 ShelfAlignment align = shelf->GetAlignment();
1931 const gfx::Rect bounds = GetBoundsInScreen();
1932 int distance = 0;
1933 switch (align) {
1934 case SHELF_ALIGNMENT_BOTTOM:
1935 distance = bounds.y() - coordinate.y();
1936 break;
1937 case SHELF_ALIGNMENT_LEFT:
1938 distance = coordinate.x() - bounds.right();
1939 break;
1940 case SHELF_ALIGNMENT_RIGHT:
1941 distance = bounds.x() - coordinate.x();
1942 break;
1943 case SHELF_ALIGNMENT_TOP:
1944 distance = coordinate.y() - bounds.bottom();
1945 break;
1947 return distance > 0 ? distance : 0;
1950 } // namespace ash