third_party: Add OWNERS for re2 library.
[chromium-blink-merge.git] / ash / shelf / shelf_view.cc
blobea12477658b5fe6055f201088be9d9f5281bad46
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_manager.h"
24 #include "ash/shelf/shelf_layout_manager.h"
25 #include "ash/shelf/shelf_menu_model.h"
26 #include "ash/shelf/shelf_model.h"
27 #include "ash/shelf/shelf_tooltip_manager.h"
28 #include "ash/shelf/shelf_widget.h"
29 #include "ash/shell.h"
30 #include "ash/wm/coordinate_conversion.h"
31 #include "base/auto_reset.h"
32 #include "base/memory/scoped_ptr.h"
33 #include "base/metrics/histogram.h"
34 #include "grit/ash_strings.h"
35 #include "ui/accessibility/ax_view_state.h"
36 #include "ui/aura/client/screen_position_client.h"
37 #include "ui/aura/window.h"
38 #include "ui/aura/window_event_dispatcher.h"
39 #include "ui/base/l10n/l10n_util.h"
40 #include "ui/base/models/simple_menu_model.h"
41 #include "ui/base/resource/resource_bundle.h"
42 #include "ui/compositor/layer.h"
43 #include "ui/compositor/layer_animator.h"
44 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
45 #include "ui/events/event_utils.h"
46 #include "ui/gfx/canvas.h"
47 #include "ui/gfx/geometry/point.h"
48 #include "ui/views/animation/bounds_animator.h"
49 #include "ui/views/border.h"
50 #include "ui/views/controls/button/image_button.h"
51 #include "ui/views/controls/menu/menu_model_adapter.h"
52 #include "ui/views/controls/menu/menu_runner.h"
53 #include "ui/views/focus/focus_search.h"
54 #include "ui/views/view_model.h"
55 #include "ui/views/view_model_utils.h"
56 #include "ui/views/widget/widget.h"
57 #include "ui/wm/core/coordinate_conversion.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 explicit 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 // TODO(bruthig): ShelfMenuModelAdapter does not appear to be used, remove it.
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 x = layout_manager_->PrimaryAxisValue(x + w + button_spacing, x);
735 y = layout_manager_->PrimaryAxisValue(y, y + h + button_spacing);
738 if (is_overflow_mode()) {
739 const_cast<ShelfView*>(this)->UpdateAllButtonsVisibilityInOverflowMode();
740 return;
743 // Right aligned icons.
744 int end_position = available_size - button_spacing;
745 x = layout_manager_->PrimaryAxisValue(end_position, 0);
746 y = layout_manager_->PrimaryAxisValue(0, end_position);
747 for (int i = view_model_->view_size() - 1;
748 i >= first_panel_index; --i) {
749 x = layout_manager_->PrimaryAxisValue(x - w - button_spacing, x);
750 y = layout_manager_->PrimaryAxisValue(y, y - h - button_spacing);
751 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
752 end_position = layout_manager_->PrimaryAxisValue(x, y);
755 // Icons on the left / top are guaranteed up to kLeftIconProportion of
756 // the available space.
757 int last_icon_position = layout_manager_->PrimaryAxisValue(
758 view_model_->ideal_bounds(last_button_index).right(),
759 view_model_->ideal_bounds(last_button_index).bottom()) + button_size;
760 int reserved_icon_space = available_size * kReservedNonPanelIconProportion;
761 if (last_icon_position < reserved_icon_space)
762 end_position = last_icon_position;
763 else
764 end_position = std::max(end_position, reserved_icon_space);
766 bounds->overflow_bounds.set_size(
767 gfx::Size(layout_manager_->PrimaryAxisValue(w, width()),
768 layout_manager_->PrimaryAxisValue(height(), h)));
770 last_visible_index_ = DetermineLastVisibleIndex(
771 end_position - button_size);
772 last_hidden_index_ = DetermineFirstVisiblePanelIndex(end_position) - 1;
773 bool show_overflow = last_visible_index_ < last_button_index ||
774 last_hidden_index_ >= first_panel_index;
776 // Create Space for the overflow button
777 if (show_overflow) {
778 // The following code makes sure that platform apps icons (aligned to left /
779 // top) are favored over panel apps icons (aligned to right / bottom).
780 if (last_visible_index_ > 0 && last_visible_index_ < last_button_index) {
781 // This condition means that we will take one platform app and replace it
782 // with the overflow button and put the app in the overflow bubble.
783 // This happens when the space needed for platform apps exceeds the
784 // reserved area for non-panel icons,
785 // (i.e. |last_icon_position| > |reserved_icon_space|).
786 --last_visible_index_;
787 } else if (last_hidden_index_ >= first_panel_index &&
788 last_hidden_index_ < view_model_->view_size() - 1) {
789 // This condition means that we will take a panel app icon and replace it
790 // with the overflow button.
791 // This happens when there is still room for platform apps in the reserved
792 // area for non-panel icons,
793 // (i.e. |last_icon_position| < |reserved_icon_space|).
794 ++last_hidden_index_;
798 for (int i = 0; i < view_model_->view_size(); ++i) {
799 bool visible = i <= last_visible_index_ || i > last_hidden_index_;
800 // To receive drag event continuously from |drag_view_| during the dragging
801 // off from the shelf, don't make |drag_view_| invisible. It will be
802 // eventually invisible and removed from the |view_model_| by
803 // FinalizeRipOffDrag().
804 if (dragged_off_shelf_ && view_model_->view_at(i) == drag_view_)
805 continue;
806 view_model_->view_at(i)->SetVisible(visible);
809 overflow_button_->SetVisible(show_overflow);
810 if (show_overflow) {
811 DCHECK_NE(0, view_model_->view_size());
812 if (last_visible_index_ == -1) {
813 x = 0;
814 y = 0;
815 } else {
816 x = layout_manager_->PrimaryAxisValue(
817 view_model_->ideal_bounds(last_visible_index_).right(),
818 view_model_->ideal_bounds(last_visible_index_).x());
819 y = layout_manager_->PrimaryAxisValue(
820 view_model_->ideal_bounds(last_visible_index_).y(),
821 view_model_->ideal_bounds(last_visible_index_).bottom());
823 // Set all hidden panel icon positions to be on the overflow button.
824 for (int i = first_panel_index; i <= last_hidden_index_; ++i)
825 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
827 // Add more space between last visible item and overflow button.
828 // Without this, two buttons look too close compared with other items.
829 x = layout_manager_->PrimaryAxisValue(x + button_spacing, x);
830 y = layout_manager_->PrimaryAxisValue(y, y + button_spacing);
832 bounds->overflow_bounds.set_x(x);
833 bounds->overflow_bounds.set_y(y);
834 if (overflow_bubble_.get() && overflow_bubble_->IsShowing())
835 UpdateOverflowRange(overflow_bubble_->shelf_view());
836 } else {
837 if (overflow_bubble_)
838 overflow_bubble_->Hide();
842 int ShelfView::DetermineLastVisibleIndex(int max_value) const {
843 int index = model_->FirstPanelIndex() - 1;
844 while (index >= 0 &&
845 layout_manager_->PrimaryAxisValue(
846 view_model_->ideal_bounds(index).right(),
847 view_model_->ideal_bounds(index).bottom()) > max_value) {
848 index--;
850 return index;
853 int ShelfView::DetermineFirstVisiblePanelIndex(int min_value) const {
854 int index = model_->FirstPanelIndex();
855 while (index < view_model_->view_size() &&
856 layout_manager_->PrimaryAxisValue(
857 view_model_->ideal_bounds(index).right(),
858 view_model_->ideal_bounds(index).bottom()) < min_value) {
859 ++index;
861 return index;
864 void ShelfView::AddIconObserver(ShelfIconObserver* observer) {
865 observers_.AddObserver(observer);
868 void ShelfView::RemoveIconObserver(ShelfIconObserver* observer) {
869 observers_.RemoveObserver(observer);
872 void ShelfView::AnimateToIdealBounds() {
873 IdealBounds ideal_bounds;
874 CalculateIdealBounds(&ideal_bounds);
875 for (int i = 0; i < view_model_->view_size(); ++i) {
876 View* view = view_model_->view_at(i);
877 bounds_animator_->AnimateViewTo(view, view_model_->ideal_bounds(i));
878 // Now that the item animation starts, we have to make sure that the
879 // padding of the first gets properly transferred to the new first item.
880 if (i && view->border())
881 view->SetBorder(views::Border::NullBorder());
883 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
886 views::View* ShelfView::CreateViewForItem(const ShelfItem& item) {
887 views::View* view = NULL;
888 switch (item.type) {
889 case TYPE_BROWSER_SHORTCUT:
890 case TYPE_APP_SHORTCUT:
891 case TYPE_WINDOWED_APP:
892 case TYPE_PLATFORM_APP:
893 case TYPE_DIALOG:
894 case TYPE_APP_PANEL: {
895 ShelfButton* button = ShelfButton::Create(this, this, layout_manager_);
896 button->SetImage(item.image);
897 ReflectItemStatus(item, button);
898 view = button;
899 break;
902 case TYPE_APP_LIST: {
903 view = new AppListButton(this, this, layout_manager_->shelf_widget());
904 break;
907 default:
908 break;
910 view->set_context_menu_controller(this);
912 DCHECK(view);
913 ConfigureChildView(view);
914 return view;
917 void ShelfView::FadeIn(views::View* view) {
918 view->SetVisible(true);
919 view->layer()->SetOpacity(0);
920 AnimateToIdealBounds();
921 bounds_animator_->SetAnimationDelegate(
922 view,
923 scoped_ptr<gfx::AnimationDelegate>(new FadeInAnimationDelegate(view)));
926 void ShelfView::PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event) {
927 DCHECK(!dragging());
928 DCHECK(drag_view_);
929 drag_pointer_ = pointer;
930 start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
932 if (start_drag_index_== -1) {
933 CancelDrag(-1);
934 return;
937 // If the item is no longer draggable, bail out.
938 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
939 model_->items()[start_drag_index_].id);
940 if (!item_delegate->IsDraggable()) {
941 CancelDrag(-1);
942 return;
945 // Move the view to the front so that it appears on top of other views.
946 ReorderChildView(drag_view_, -1);
947 bounds_animator_->StopAnimatingView(drag_view_);
950 void ShelfView::ContinueDrag(const ui::LocatedEvent& event) {
951 // Due to a syncing operation the application might have been removed.
952 // Bail if it is gone.
953 int current_index = view_model_->GetIndexOfView(drag_view_);
954 DCHECK_NE(-1, current_index);
956 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
957 model_->items()[current_index].id);
958 if (!item_delegate->IsDraggable()) {
959 CancelDrag(-1);
960 return;
963 // If this is not a drag and drop host operation and not the app list item,
964 // check if the item got ripped off the shelf - if it did we are done.
965 if (!drag_and_drop_shelf_id_ &&
966 RemovableByRipOff(current_index) != NOT_REMOVABLE) {
967 if (HandleRipOffDrag(event))
968 return;
969 // The rip off handler could have changed the location of the item.
970 current_index = view_model_->GetIndexOfView(drag_view_);
973 // TODO: I don't think this works correctly with RTL.
974 gfx::Point drag_point(event.location());
975 ConvertPointToTarget(drag_view_, this, &drag_point);
977 // Constrain the location to the range of valid indices for the type.
978 std::pair<int, int> indices(GetDragRange(current_index));
979 int first_drag_index = indices.first;
980 int last_drag_index = indices.second;
981 // If the last index isn't valid, we're overflowing. Constrain to the app list
982 // (which is the last visible item).
983 if (first_drag_index < model_->FirstPanelIndex() &&
984 last_drag_index > last_visible_index_)
985 last_drag_index = last_visible_index_;
986 int x = 0, y = 0;
987 if (layout_manager_->IsHorizontalAlignment()) {
988 x = std::max(view_model_->ideal_bounds(indices.first).x(),
989 drag_point.x() - drag_origin_.x());
990 x = std::min(view_model_->ideal_bounds(last_drag_index).right() -
991 view_model_->ideal_bounds(current_index).width(),
993 if (drag_view_->x() == x)
994 return;
995 drag_view_->SetX(x);
996 } else {
997 y = std::max(view_model_->ideal_bounds(indices.first).y(),
998 drag_point.y() - drag_origin_.y());
999 y = std::min(view_model_->ideal_bounds(last_drag_index).bottom() -
1000 view_model_->ideal_bounds(current_index).height(),
1002 if (drag_view_->y() == y)
1003 return;
1004 drag_view_->SetY(y);
1007 int target_index =
1008 views::ViewModelUtils::DetermineMoveIndex(
1009 *view_model_, drag_view_,
1010 layout_manager_->IsHorizontalAlignment() ?
1011 views::ViewModelUtils::HORIZONTAL :
1012 views::ViewModelUtils::VERTICAL,
1013 x, y);
1014 target_index =
1015 std::min(indices.second, std::max(target_index, indices.first));
1016 if (target_index == current_index)
1017 return;
1019 // Change the model, the ShelfItemMoved() callback will handle the
1020 // |view_model_| update.
1021 model_->Move(current_index, target_index);
1022 bounds_animator_->StopAnimatingView(drag_view_);
1025 bool ShelfView::HandleRipOffDrag(const ui::LocatedEvent& event) {
1026 int current_index = view_model_->GetIndexOfView(drag_view_);
1027 DCHECK_NE(-1, current_index);
1028 std::string dragged_app_id =
1029 delegate_->GetAppIDForShelfID(model_->items()[current_index].id);
1031 gfx::Point screen_location = event.root_location();
1032 ::wm::ConvertPointToScreen(GetWidget()->GetNativeWindow()->GetRootWindow(),
1033 &screen_location);
1035 // To avoid ugly forwards and backwards flipping we use different constants
1036 // for ripping off / re-inserting the items.
1037 if (dragged_off_shelf_) {
1038 // If the shelf/overflow bubble bounds contains |screen_location| we insert
1039 // the item back into the shelf.
1040 if (GetBoundsForDragInsertInScreen().Contains(screen_location)) {
1041 if (dragged_off_from_overflow_to_shelf_) {
1042 // During the dragging an item from Shelf to Overflow, it can enter here
1043 // directly because both are located very closly.
1044 main_shelf_->EndDrag(true);
1045 // Stops the animation of |drag_view_| and sets its bounds explicitly
1046 // becase ContinueDrag() stops its animation. Without this, unexpected
1047 // bounds will be set.
1048 bounds_animator_->StopAnimatingView(drag_view_);
1049 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1050 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index));
1051 dragged_off_from_overflow_to_shelf_ = false;
1053 // Destroy our proxy view item.
1054 DestroyDragIconProxy();
1055 // Re-insert the item and return simply false since the caller will handle
1056 // the move as in any normal case.
1057 dragged_off_shelf_ = false;
1058 drag_view_->layer()->SetOpacity(1.0f);
1059 // The size of Overflow bubble should be updated immediately when an item
1060 // is re-inserted.
1061 if (is_overflow_mode())
1062 PreferredSizeChanged();
1063 return false;
1064 } else if (is_overflow_mode() &&
1065 main_shelf_->GetBoundsForDragInsertInScreen().Contains(
1066 screen_location)) {
1067 if (!dragged_off_from_overflow_to_shelf_) {
1068 dragged_off_from_overflow_to_shelf_ = true;
1069 drag_image_->SetOpacity(1.0f);
1070 main_shelf_->StartDrag(dragged_app_id, screen_location);
1071 } else {
1072 main_shelf_->Drag(screen_location);
1074 } else if (dragged_off_from_overflow_to_shelf_) {
1075 // Makes the |drag_image_| partially disappear again.
1076 dragged_off_from_overflow_to_shelf_ = false;
1077 drag_image_->SetOpacity(kDraggedImageOpacity);
1078 main_shelf_->EndDrag(true);
1079 bounds_animator_->StopAnimatingView(drag_view_);
1080 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1081 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index));
1083 // Move our proxy view item.
1084 UpdateDragIconProxy(screen_location);
1085 return true;
1087 // Check if we are too far away from the shelf to enter the ripped off state.
1088 // Determine the distance to the shelf.
1089 int delta = CalculateShelfDistance(screen_location);
1090 if (delta > kRipOffDistance) {
1091 // Create a proxy view item which can be moved anywhere.
1092 CreateDragIconProxy(event.root_location(),
1093 drag_view_->GetImage(),
1094 drag_view_,
1095 gfx::Vector2d(0, 0),
1096 kDragAndDropProxyScale);
1097 drag_view_->layer()->SetOpacity(0.0f);
1098 dragged_off_shelf_ = true;
1099 if (RemovableByRipOff(current_index) == REMOVABLE) {
1100 // Move the item to the front of the first panel item and hide it.
1101 // ShelfItemMoved() callback will handle the |view_model_| update and
1102 // call AnimateToIdealBounds().
1103 if (current_index != model_->FirstPanelIndex() - 1) {
1104 model_->Move(current_index, model_->FirstPanelIndex() - 1);
1105 StartFadeInLastVisibleItem();
1106 } else if (is_overflow_mode()) {
1107 // Overflow bubble should be shrunk when an item is ripped off.
1108 PreferredSizeChanged();
1110 // Make the item partially disappear to show that it will get removed if
1111 // dropped.
1112 drag_image_->SetOpacity(kDraggedImageOpacity);
1114 return true;
1116 return false;
1119 void ShelfView::FinalizeRipOffDrag(bool cancel) {
1120 if (!dragged_off_shelf_)
1121 return;
1122 // Make sure we do not come in here again.
1123 dragged_off_shelf_ = false;
1125 // Coming here we should always have a |drag_view_|.
1126 DCHECK(drag_view_);
1127 int current_index = view_model_->GetIndexOfView(drag_view_);
1128 // If the view isn't part of the model anymore (|current_index| == -1), a sync
1129 // operation must have removed it. In that case we shouldn't change the model
1130 // and only delete the proxy image.
1131 if (current_index == -1) {
1132 DestroyDragIconProxy();
1133 return;
1136 // Set to true when the animation should snap back to where it was before.
1137 bool snap_back = false;
1138 // Items which cannot be dragged off will be handled as a cancel.
1139 if (!cancel) {
1140 if (dragged_off_from_overflow_to_shelf_) {
1141 dragged_off_from_overflow_to_shelf_ = false;
1142 main_shelf_->EndDrag(false);
1143 drag_view_->layer()->SetOpacity(1.0f);
1144 } else if (RemovableByRipOff(current_index) != REMOVABLE) {
1145 // Make sure we do not try to remove un-removable items like items which
1146 // were not pinned or have to be always there.
1147 cancel = true;
1148 snap_back = true;
1149 } else {
1150 // Make sure the item stays invisible upon removal.
1151 drag_view_->SetVisible(false);
1152 std::string app_id =
1153 delegate_->GetAppIDForShelfID(model_->items()[current_index].id);
1154 delegate_->UnpinAppWithID(app_id);
1157 if (cancel || snap_back) {
1158 if (dragged_off_from_overflow_to_shelf_) {
1159 dragged_off_from_overflow_to_shelf_ = false;
1160 // Main shelf handles revert of dragged item.
1161 main_shelf_->EndDrag(true);
1162 drag_view_->layer()->SetOpacity(1.0f);
1163 } else if (!cancelling_drag_model_changed_) {
1164 // Only do something if the change did not come through a model change.
1165 gfx::Rect drag_bounds = drag_image_->GetBoundsInScreen();
1166 gfx::Point relative_to = GetBoundsInScreen().origin();
1167 gfx::Rect target(
1168 gfx::PointAtOffsetFromOrigin(drag_bounds.origin()- relative_to),
1169 drag_bounds.size());
1170 drag_view_->SetBoundsRect(target);
1171 // Hide the status from the active item since we snap it back now. Upon
1172 // animation end the flag gets cleared if |snap_back_from_rip_off_view_|
1173 // is set.
1174 snap_back_from_rip_off_view_ = drag_view_;
1175 drag_view_->AddState(ShelfButton::STATE_HIDDEN);
1176 // When a canceling drag model is happening, the view model is diverged
1177 // from the menu model and movements / animations should not be done.
1178 model_->Move(current_index, start_drag_index_);
1179 AnimateToIdealBounds();
1181 drag_view_->layer()->SetOpacity(1.0f);
1183 DestroyDragIconProxy();
1186 ShelfView::RemovableState ShelfView::RemovableByRipOff(int index) const {
1187 DCHECK(index >= 0 && index < model_->item_count());
1188 ShelfItemType type = model_->items()[index].type;
1189 if (type == TYPE_APP_LIST || type == TYPE_DIALOG || !delegate_->CanPin())
1190 return NOT_REMOVABLE;
1192 std::string app_id = delegate_->GetAppIDForShelfID(model_->items()[index].id);
1193 // Note: Only pinned app shortcuts can be removed!
1194 return (type == TYPE_APP_SHORTCUT && delegate_->IsAppPinned(app_id)) ?
1195 REMOVABLE : DRAGGABLE;
1198 bool ShelfView::SameDragType(ShelfItemType typea, ShelfItemType typeb) const {
1199 switch (typea) {
1200 case TYPE_APP_SHORTCUT:
1201 case TYPE_BROWSER_SHORTCUT:
1202 return (typeb == TYPE_APP_SHORTCUT || typeb == TYPE_BROWSER_SHORTCUT);
1203 case TYPE_APP_LIST:
1204 case TYPE_PLATFORM_APP:
1205 case TYPE_WINDOWED_APP:
1206 case TYPE_APP_PANEL:
1207 case TYPE_DIALOG:
1208 return typeb == typea;
1209 case TYPE_UNDEFINED:
1210 NOTREACHED() << "ShelfItemType must be set.";
1211 return false;
1213 NOTREACHED();
1214 return false;
1217 std::pair<int, int> ShelfView::GetDragRange(int index) {
1218 int min_index = -1;
1219 int max_index = -1;
1220 ShelfItemType type = model_->items()[index].type;
1221 for (int i = 0; i < model_->item_count(); ++i) {
1222 if (SameDragType(model_->items()[i].type, type)) {
1223 if (min_index == -1)
1224 min_index = i;
1225 max_index = i;
1228 return std::pair<int, int>(min_index, max_index);
1231 void ShelfView::ConfigureChildView(views::View* view) {
1232 view->SetPaintToLayer(true);
1233 view->layer()->SetFillsBoundsOpaquely(false);
1236 void ShelfView::ToggleOverflowBubble() {
1237 if (IsShowingOverflowBubble()) {
1238 overflow_bubble_->Hide();
1239 return;
1242 if (!overflow_bubble_)
1243 overflow_bubble_.reset(new OverflowBubble());
1245 ShelfView* overflow_view =
1246 new ShelfView(model_, delegate_, layout_manager_);
1247 overflow_view->overflow_mode_ = true;
1248 overflow_view->Init();
1249 overflow_view->set_owner_overflow_bubble(overflow_bubble_.get());
1250 overflow_view->OnShelfAlignmentChanged();
1251 overflow_view->main_shelf_ = this;
1252 UpdateOverflowRange(overflow_view);
1254 overflow_bubble_->Show(overflow_button_, overflow_view);
1256 Shell::GetInstance()->UpdateShelfVisibility();
1259 void ShelfView::OnFadeOutAnimationEnded() {
1260 AnimateToIdealBounds();
1261 StartFadeInLastVisibleItem();
1264 void ShelfView::StartFadeInLastVisibleItem() {
1265 // If overflow button is visible and there is a valid new last item, fading
1266 // the new last item in after sliding animation is finished.
1267 if (overflow_button_->visible() && last_visible_index_ >= 0) {
1268 views::View* last_visible_view = view_model_->view_at(last_visible_index_);
1269 last_visible_view->layer()->SetOpacity(0);
1270 bounds_animator_->SetAnimationDelegate(
1271 last_visible_view,
1272 scoped_ptr<gfx::AnimationDelegate>(
1273 new StartFadeAnimationDelegate(this, last_visible_view)));
1277 void ShelfView::UpdateOverflowRange(ShelfView* overflow_view) const {
1278 const int first_overflow_index = last_visible_index_ + 1;
1279 const int last_overflow_index = last_hidden_index_;
1280 DCHECK_LE(first_overflow_index, last_overflow_index);
1281 DCHECK_LT(last_overflow_index, view_model_->view_size());
1283 overflow_view->first_visible_index_ = first_overflow_index;
1284 overflow_view->last_visible_index_ = last_overflow_index;
1287 bool ShelfView::ShouldHideTooltip(const gfx::Point& cursor_location) {
1288 gfx::Rect active_bounds;
1290 for (int i = 0; i < child_count(); ++i) {
1291 views::View* child = child_at(i);
1292 if (child == overflow_button_)
1293 continue;
1294 if (!ShouldShowTooltipForView(child))
1295 continue;
1297 gfx::Rect child_bounds = child->GetMirroredBounds();
1298 active_bounds.Union(child_bounds);
1301 return !active_bounds.Contains(cursor_location);
1304 gfx::Rect ShelfView::GetVisibleItemsBoundsInScreen() {
1305 gfx::Size preferred_size = GetPreferredSize();
1306 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1307 ConvertPointToScreen(this, &origin);
1308 return gfx::Rect(origin, preferred_size);
1311 gfx::Rect ShelfView::GetBoundsForDragInsertInScreen() {
1312 gfx::Size preferred_size;
1313 if (is_overflow_mode()) {
1314 DCHECK(owner_overflow_bubble_);
1315 gfx::Rect bubble_bounds =
1316 owner_overflow_bubble_->bubble_view()->GetBubbleBounds();
1317 preferred_size = bubble_bounds.size();
1318 } else {
1319 const int last_button_index = view_model_->view_size() - 1;
1320 gfx::Rect last_button_bounds =
1321 view_model_->view_at(last_button_index)->bounds();
1322 if (overflow_button_->visible() &&
1323 model_->GetItemIndexForType(TYPE_APP_PANEL) == -1) {
1324 // When overflow button is visible and shelf has no panel items,
1325 // last_button_bounds should be overflow button's bounds.
1326 last_button_bounds = overflow_button_->bounds();
1329 if (layout_manager_->IsHorizontalAlignment()) {
1330 preferred_size = gfx::Size(last_button_bounds.right() + leading_inset_,
1331 kShelfSize);
1332 } else {
1333 preferred_size = gfx::Size(kShelfSize,
1334 last_button_bounds.bottom() + leading_inset_);
1337 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1339 // In overflow mode, we should use OverflowBubbleView as a source for
1340 // converting |origin| to screen coordinates. When a scroll operation is
1341 // occurred in OverflowBubble, the bounds of ShelfView in OverflowBubble can
1342 // be changed.
1343 if (is_overflow_mode())
1344 ConvertPointToScreen(owner_overflow_bubble_->bubble_view(), &origin);
1345 else
1346 ConvertPointToScreen(this, &origin);
1348 return gfx::Rect(origin, preferred_size);
1351 int ShelfView::CancelDrag(int modified_index) {
1352 FinalizeRipOffDrag(true);
1353 if (!drag_view_)
1354 return modified_index;
1355 bool was_dragging = dragging();
1356 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1357 drag_pointer_ = NONE;
1358 drag_view_ = NULL;
1359 if (drag_view_index == modified_index) {
1360 // The view that was being dragged is being modified. Don't do anything.
1361 return modified_index;
1363 if (!was_dragging)
1364 return modified_index;
1366 // Restore previous position, tracking the position of the modified view.
1367 bool at_end = modified_index == view_model_->view_size();
1368 views::View* modified_view =
1369 (modified_index >= 0 && !at_end) ?
1370 view_model_->view_at(modified_index) : NULL;
1371 model_->Move(drag_view_index, start_drag_index_);
1373 // If the modified view will be at the end of the list, return the new end of
1374 // the list.
1375 if (at_end)
1376 return view_model_->view_size();
1377 return modified_view ? view_model_->GetIndexOfView(modified_view) : -1;
1380 gfx::Size ShelfView::GetPreferredSize() const {
1381 IdealBounds ideal_bounds;
1382 CalculateIdealBounds(&ideal_bounds);
1384 int last_button_index = is_overflow_mode() ?
1385 last_visible_index_ : view_model_->view_size() - 1;
1387 // When an item is dragged off from the overflow bubble, it is moved to last
1388 // position and and changed to invisible. Overflow bubble size should be
1389 // shrunk to fit only for visible items.
1390 // If |dragged_off_from_overflow_to_shelf_| is set, there will be no invisible
1391 // items in the shelf.
1392 if (is_overflow_mode() &&
1393 dragged_off_shelf_ &&
1394 !dragged_off_from_overflow_to_shelf_ &&
1395 RemovableByRipOff(view_model_->GetIndexOfView(drag_view_)) == REMOVABLE)
1396 last_button_index--;
1398 const gfx::Rect last_button_bounds =
1399 last_button_index >= first_visible_index_ ?
1400 view_model_->ideal_bounds(last_button_index) :
1401 gfx::Rect(gfx::Size(kShelfSize, kShelfSize));
1403 if (layout_manager_->IsHorizontalAlignment()) {
1404 return gfx::Size(last_button_bounds.right() + leading_inset_, kShelfSize);
1407 return gfx::Size(kShelfSize,
1408 last_button_bounds.bottom() + leading_inset_);
1411 void ShelfView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1412 // This bounds change is produced by the shelf movement and all content has
1413 // to follow. Using an animation at that time would produce a time lag since
1414 // the animation of the BoundsAnimator has itself a delay before it arrives
1415 // at the required location. As such we tell the animator to go there
1416 // immediately.
1417 BoundsAnimatorDisabler disabler(bounds_animator_.get());
1418 LayoutToIdealBounds();
1419 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1420 OnShelfIconPositionsChanged());
1422 if (IsShowingOverflowBubble())
1423 overflow_bubble_->Hide();
1426 views::FocusTraversable* ShelfView::GetPaneFocusTraversable() {
1427 return this;
1430 void ShelfView::GetAccessibleState(ui::AXViewState* state) {
1431 state->role = ui::AX_ROLE_TOOLBAR;
1432 state->name = l10n_util::GetStringUTF16(IDS_ASH_SHELF_ACCESSIBLE_NAME);
1435 void ShelfView::OnGestureEvent(ui::GestureEvent* event) {
1436 aura::Window* target_window = static_cast<views::View*>(event->target())
1437 ->GetWidget()
1438 ->GetNativeWindow();
1439 if (gesture_handler_.ProcessGestureEvent(*event, target_window))
1440 event->StopPropagation();
1443 void ShelfView::ShelfItemAdded(int model_index) {
1445 base::AutoReset<bool> cancelling_drag(
1446 &cancelling_drag_model_changed_, true);
1447 model_index = CancelDrag(model_index);
1449 views::View* view = CreateViewForItem(model_->items()[model_index]);
1450 AddChildView(view);
1451 // Hide the view, it'll be made visible when the animation is done. Using
1452 // opacity 0 here to avoid messing with CalculateIdealBounds which touches
1453 // the view's visibility.
1454 view->layer()->SetOpacity(0);
1455 view_model_->Add(view, model_index);
1457 // Give the button its ideal bounds. That way if we end up animating the
1458 // button before this animation completes it doesn't appear at some random
1459 // spot (because it was in the middle of animating from 0,0 0x0 to its
1460 // target).
1461 IdealBounds ideal_bounds;
1462 CalculateIdealBounds(&ideal_bounds);
1463 view->SetBoundsRect(view_model_->ideal_bounds(model_index));
1465 // The first animation moves all the views to their target position. |view|
1466 // is hidden, so it visually appears as though we are providing space for
1467 // it. When done we'll fade the view in.
1468 AnimateToIdealBounds();
1469 if (model_index <= last_visible_index_ ||
1470 model_index >= model_->FirstPanelIndex()) {
1471 bounds_animator_->SetAnimationDelegate(
1472 view,
1473 scoped_ptr<gfx::AnimationDelegate>(
1474 new StartFadeAnimationDelegate(this, view)));
1475 } else {
1476 // Undo the hiding if animation does not run.
1477 view->layer()->SetOpacity(1.0f);
1481 void ShelfView::ShelfItemRemoved(int model_index, ShelfID id) {
1482 if (id == context_menu_id_)
1483 launcher_menu_runner_.reset();
1485 base::AutoReset<bool> cancelling_drag(
1486 &cancelling_drag_model_changed_, true);
1487 model_index = CancelDrag(model_index);
1489 views::View* view = view_model_->view_at(model_index);
1490 view_model_->Remove(model_index);
1492 // When the overflow bubble is visible, the overflow range needs to be set
1493 // before CalculateIdealBounds() gets called. Otherwise CalculateIdealBounds()
1494 // could trigger a ShelfItemChanged() by hiding the overflow bubble and
1495 // since the overflow bubble is not yet synced with the ShelfModel this
1496 // could cause a crash.
1497 if (overflow_bubble_ && overflow_bubble_->IsShowing()) {
1498 last_hidden_index_ = std::min(last_hidden_index_,
1499 view_model_->view_size() - 1);
1500 UpdateOverflowRange(overflow_bubble_->shelf_view());
1503 if (view->visible()) {
1504 // The first animation fades out the view. When done we'll animate the rest
1505 // of the views to their target location.
1506 bounds_animator_->AnimateViewTo(view, view->bounds());
1507 bounds_animator_->SetAnimationDelegate(
1508 view,
1509 scoped_ptr<gfx::AnimationDelegate>(
1510 new FadeOutAnimationDelegate(this, view)));
1511 } else {
1512 // We don't need to show a fade out animation for invisible |view|. When an
1513 // item is ripped out from the shelf, its |view| is already invisible.
1514 AnimateToIdealBounds();
1517 // Close the tooltip because it isn't needed any longer and its anchor view
1518 // will be deleted soon.
1519 if (tooltip_->GetCurrentAnchorView() == view)
1520 tooltip_->Close();
1523 void ShelfView::ShelfItemChanged(int model_index, const ShelfItem& old_item) {
1524 const ShelfItem& item(model_->items()[model_index]);
1525 if (old_item.type != item.type) {
1526 // Type changed, swap the views.
1527 model_index = CancelDrag(model_index);
1528 scoped_ptr<views::View> old_view(view_model_->view_at(model_index));
1529 bounds_animator_->StopAnimatingView(old_view.get());
1530 // Removing and re-inserting a view in our view model will strip the ideal
1531 // bounds from the item. To avoid recalculation of everything the bounds
1532 // get remembered and restored after the insertion to the previous value.
1533 gfx::Rect old_ideal_bounds = view_model_->ideal_bounds(model_index);
1534 view_model_->Remove(model_index);
1535 views::View* new_view = CreateViewForItem(item);
1536 AddChildView(new_view);
1537 view_model_->Add(new_view, model_index);
1538 view_model_->set_ideal_bounds(model_index, old_ideal_bounds);
1539 new_view->SetBoundsRect(old_view->bounds());
1540 return;
1543 views::View* view = view_model_->view_at(model_index);
1544 switch (item.type) {
1545 case TYPE_BROWSER_SHORTCUT:
1546 // Fallthrough for the new Shelf since it needs to show the activation
1547 // change as well.
1548 case TYPE_APP_SHORTCUT:
1549 case TYPE_WINDOWED_APP:
1550 case TYPE_PLATFORM_APP:
1551 case TYPE_DIALOG:
1552 case TYPE_APP_PANEL: {
1553 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1554 ShelfButton* button = static_cast<ShelfButton*>(view);
1555 ReflectItemStatus(item, button);
1556 // The browser shortcut is currently not a "real" item and as such the
1557 // the image is bogous as well. We therefore keep the image as is for it.
1558 if (item.type != TYPE_BROWSER_SHORTCUT)
1559 button->SetImage(item.image);
1560 button->SchedulePaint();
1561 break;
1564 default:
1565 break;
1569 void ShelfView::ShelfItemMoved(int start_index, int target_index) {
1570 view_model_->Move(start_index, target_index);
1571 // When cancelling a drag due to a shelf item being added, the currently
1572 // dragged item is moved back to its initial position. AnimateToIdealBounds
1573 // will be called again when the new item is added to the |view_model_| but
1574 // at this time the |view_model_| is inconsistent with the |model_|.
1575 if (!cancelling_drag_model_changed_)
1576 AnimateToIdealBounds();
1579 void ShelfView::ShelfStatusChanged() {
1580 // Nothing to do here.
1583 void ShelfView::PointerPressedOnButton(views::View* view,
1584 Pointer pointer,
1585 const ui::LocatedEvent& event) {
1586 if (drag_view_)
1587 return;
1589 int index = view_model_->GetIndexOfView(view);
1590 if (index == -1)
1591 return;
1593 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1594 model_->items()[index].id);
1595 if (view_model_->view_size() <= 1 || !item_delegate->IsDraggable())
1596 return; // View is being deleted or not draggable, ignore request.
1598 // Only when the repost event occurs on the same shelf item, we should ignore
1599 // the call in ShelfView::ButtonPressed(...).
1600 is_repost_event_ = IsRepostEvent(event) && (last_pressed_index_ == index);
1602 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1603 drag_view_ = static_cast<ShelfButton*>(view);
1604 drag_origin_ = gfx::Point(event.x(), event.y());
1605 UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage",
1606 layout_manager_->SelectValueForShelfAlignment(
1607 SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM,
1608 SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT,
1609 SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT,
1610 -1),
1611 SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT);
1614 void ShelfView::PointerDraggedOnButton(views::View* view,
1615 Pointer pointer,
1616 const ui::LocatedEvent& event) {
1617 // To prepare all drag types (moving an item in the shelf and dragging off),
1618 // we should check the x-axis and y-axis offset.
1619 if (!dragging() && drag_view_ &&
1620 ((std::abs(event.x() - drag_origin_.x()) >= kMinimumDragDistance) ||
1621 (std::abs(event.y() - drag_origin_.y()) >= kMinimumDragDistance))) {
1622 PrepareForDrag(pointer, event);
1624 if (drag_pointer_ == pointer)
1625 ContinueDrag(event);
1628 void ShelfView::PointerReleasedOnButton(views::View* view,
1629 Pointer pointer,
1630 bool canceled) {
1631 // Reset |is_repost_event| to false.
1632 is_repost_event_ = false;
1634 if (canceled) {
1635 CancelDrag(-1);
1636 } else if (drag_pointer_ == pointer) {
1637 FinalizeRipOffDrag(false);
1638 drag_pointer_ = NONE;
1639 AnimateToIdealBounds();
1641 // If the drag pointer is NONE, no drag operation is going on and the
1642 // drag_view can be released.
1643 if (drag_pointer_ == NONE)
1644 drag_view_ = NULL;
1647 void ShelfView::MouseMovedOverButton(views::View* view) {
1648 if (!ShouldShowTooltipForView(view))
1649 return;
1651 if (!tooltip_->IsVisible())
1652 tooltip_->ResetTimer();
1655 void ShelfView::MouseEnteredButton(views::View* view) {
1656 if (!ShouldShowTooltipForView(view))
1657 return;
1659 if (tooltip_->IsVisible()) {
1660 tooltip_->ShowImmediately(view, GetAccessibleName(view));
1661 } else {
1662 tooltip_->ShowDelayed(view, GetAccessibleName(view));
1666 void ShelfView::MouseExitedButton(views::View* view) {
1667 if (!tooltip_->IsVisible())
1668 tooltip_->StopTimer();
1671 base::string16 ShelfView::GetAccessibleName(const views::View* view) {
1672 int view_index = view_model_->GetIndexOfView(view);
1673 // May be -1 while in the process of animating closed.
1674 if (view_index == -1)
1675 return base::string16();
1677 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1678 model_->items()[view_index].id);
1679 return item_delegate->GetTitle();
1682 void ShelfView::ButtonPressed(views::Button* sender, const ui::Event& event) {
1683 // Do not handle mouse release during drag.
1684 if (dragging())
1685 return;
1687 if (sender == overflow_button_) {
1688 ToggleOverflowBubble();
1689 shelf_button_pressed_metric_tracker_.ButtonPressed(
1690 event, sender, ShelfItemDelegate::kNoAction);
1691 return;
1694 int view_index = view_model_->GetIndexOfView(sender);
1695 // May be -1 while in the process of animating closed.
1696 if (view_index == -1)
1697 return;
1699 // If the menu was just closed by the same event as this one, we ignore
1700 // the call and don't open the menu again. See crbug.com/343005 for more
1701 // detail.
1702 if (is_repost_event_)
1703 return;
1705 // Record the index for the last pressed shelf item.
1706 last_pressed_index_ = view_index;
1708 // Don't activate the item twice on double-click. Otherwise the window starts
1709 // animating open due to the first click, then immediately minimizes due to
1710 // the second click. The user most likely intended to open or minimize the
1711 // item once, not do both.
1712 if (event.flags() & ui::EF_IS_DOUBLE_CLICK)
1713 return;
1716 ScopedTargetRootWindow scoped_target(
1717 sender->GetWidget()->GetNativeView()->GetRootWindow());
1718 // Slow down activation animations if shift key is pressed.
1719 scoped_ptr<ui::ScopedAnimationDurationScaleMode> slowing_animations;
1720 if (event.IsShiftDown()) {
1721 slowing_animations.reset(new ui::ScopedAnimationDurationScaleMode(
1722 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION));
1725 // Collect usage statistics before we decide what to do with the click.
1726 switch (model_->items()[view_index].type) {
1727 case TYPE_APP_SHORTCUT:
1728 case TYPE_WINDOWED_APP:
1729 case TYPE_PLATFORM_APP:
1730 case TYPE_BROWSER_SHORTCUT:
1731 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1732 UMA_LAUNCHER_CLICK_ON_APP);
1733 break;
1735 case TYPE_APP_LIST:
1736 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1737 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON);
1738 break;
1740 case TYPE_APP_PANEL:
1741 case TYPE_DIALOG:
1742 break;
1744 case TYPE_UNDEFINED:
1745 NOTREACHED() << "ShelfItemType must be set.";
1746 break;
1749 ShelfItemDelegate::PerformedAction performed_action =
1750 item_manager_->GetShelfItemDelegate(model_->items()[view_index].id)
1751 ->ItemSelected(event);
1753 shelf_button_pressed_metric_tracker_.ButtonPressed(event, sender,
1754 performed_action);
1756 if (performed_action != ShelfItemDelegate::kNewWindowCreated)
1757 ShowListMenuForView(model_->items()[view_index], sender, event);
1761 bool ShelfView::ShowListMenuForView(const ShelfItem& item,
1762 views::View* source,
1763 const ui::Event& event) {
1764 ShelfItemDelegate* item_delegate =
1765 item_manager_->GetShelfItemDelegate(item.id);
1766 scoped_ptr<ui::MenuModel> list_menu_model(
1767 item_delegate->CreateApplicationMenu(event.flags()));
1769 // Make sure we have a menu and it has at least two items in addition to the
1770 // application title and the 3 spacing separators.
1771 if (!list_menu_model.get() || list_menu_model->GetItemCount() <= 5)
1772 return false;
1774 ShowMenu(list_menu_model.get(),
1775 source,
1776 gfx::Point(),
1777 false,
1778 ui::GetMenuSourceTypeForEvent(event));
1779 return true;
1782 void ShelfView::ShowContextMenuForView(views::View* source,
1783 const gfx::Point& point,
1784 ui::MenuSourceType source_type) {
1785 int view_index = view_model_->GetIndexOfView(source);
1786 if (view_index == -1) {
1787 Shell::GetInstance()->ShowContextMenu(point, source_type);
1788 return;
1791 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1792 model_->items()[view_index].id);
1793 context_menu_model_.reset(item_delegate->CreateContextMenu(
1794 source->GetWidget()->GetNativeView()->GetRootWindow()));
1795 if (!context_menu_model_)
1796 return;
1798 base::AutoReset<ShelfID> reseter(
1799 &context_menu_id_,
1800 view_index == -1 ? 0 : model_->items()[view_index].id);
1802 ShowMenu(context_menu_model_.get(),
1803 source,
1804 point,
1805 true,
1806 source_type);
1809 void ShelfView::ShowMenu(ui::MenuModel* menu_model,
1810 views::View* source,
1811 const gfx::Point& click_point,
1812 bool context_menu,
1813 ui::MenuSourceType source_type) {
1814 closing_event_time_ = base::TimeDelta();
1815 launcher_menu_runner_.reset(new views::MenuRunner(
1816 menu_model, context_menu ? views::MenuRunner::CONTEXT_MENU : 0));
1818 ScopedTargetRootWindow scoped_target(
1819 source->GetWidget()->GetNativeView()->GetRootWindow());
1821 // Determine the menu alignment dependent on the shelf.
1822 views::MenuAnchorPosition menu_alignment = views::MENU_ANCHOR_TOPLEFT;
1823 gfx::Rect anchor_point = gfx::Rect(click_point, gfx::Size());
1825 ShelfWidget* shelf = RootWindowController::ForShelf(
1826 GetWidget()->GetNativeView())->shelf();
1827 if (!context_menu) {
1828 // Application lists use a bubble.
1829 ShelfAlignment align = shelf->GetAlignment();
1830 anchor_point = source->GetBoundsInScreen();
1832 // It is possible to invoke the menu while it is sliding into view. To cover
1833 // that case, the screen coordinates are offsetted by the animation delta.
1834 gfx::Vector2d offset =
1835 source->GetWidget()->GetNativeWindow()->bounds().origin() -
1836 source->GetWidget()->GetNativeWindow()->GetTargetBounds().origin();
1837 anchor_point.set_x(anchor_point.x() - offset.x());
1838 anchor_point.set_y(anchor_point.y() - offset.y());
1840 // Shelf items can have an asymmetrical border for spacing reasons.
1841 // Adjust anchor location for this.
1842 if (source->border())
1843 anchor_point.Inset(source->border()->GetInsets());
1845 switch (align) {
1846 case SHELF_ALIGNMENT_BOTTOM:
1847 menu_alignment = views::MENU_ANCHOR_BUBBLE_ABOVE;
1848 break;
1849 case SHELF_ALIGNMENT_LEFT:
1850 menu_alignment = views::MENU_ANCHOR_BUBBLE_RIGHT;
1851 break;
1852 case SHELF_ALIGNMENT_RIGHT:
1853 menu_alignment = views::MENU_ANCHOR_BUBBLE_LEFT;
1854 break;
1855 case SHELF_ALIGNMENT_TOP:
1856 menu_alignment = views::MENU_ANCHOR_BUBBLE_BELOW;
1857 break;
1860 // If this gets deleted while we are in the menu, the shelf will be gone
1861 // as well.
1862 bool got_deleted = false;
1863 got_deleted_ = &got_deleted;
1865 shelf->ForceUndimming(true);
1866 // NOTE: if you convert to HAS_MNEMONICS be sure and update menu building
1867 // code.
1868 if (launcher_menu_runner_->RunMenuAt(source->GetWidget(),
1869 NULL,
1870 anchor_point,
1871 menu_alignment,
1872 source_type) ==
1873 views::MenuRunner::MENU_DELETED) {
1874 if (!got_deleted) {
1875 got_deleted_ = NULL;
1876 shelf->ForceUndimming(false);
1878 return;
1880 got_deleted_ = NULL;
1881 shelf->ForceUndimming(false);
1883 // If it is a context menu and we are showing overflow bubble
1884 // we want to hide overflow bubble.
1885 if (owner_overflow_bubble_)
1886 owner_overflow_bubble_->HideBubbleAndRefreshButton();
1888 // Unpinning an item will reset the |launcher_menu_runner_| before coming
1889 // here.
1890 if (launcher_menu_runner_)
1891 closing_event_time_ = launcher_menu_runner_->closing_event_time();
1892 Shell::GetInstance()->UpdateShelfVisibility();
1895 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) {
1896 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1897 OnShelfIconPositionsChanged());
1898 PreferredSizeChanged();
1901 void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
1902 if (snap_back_from_rip_off_view_ && animator == bounds_animator_) {
1903 if (!animator->IsAnimating(snap_back_from_rip_off_view_)) {
1904 // Coming here the animation of the ShelfButton is finished and the
1905 // previously hidden status can be shown again. Since the button itself
1906 // might have gone away or changed locations we check that the button
1907 // is still in the shelf and show its status again.
1908 for (int index = 0; index < view_model_->view_size(); index++) {
1909 views::View* view = view_model_->view_at(index);
1910 if (view == snap_back_from_rip_off_view_) {
1911 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1912 ShelfButton* button = static_cast<ShelfButton*>(view);
1913 button->ClearState(ShelfButton::STATE_HIDDEN);
1914 break;
1917 snap_back_from_rip_off_view_ = NULL;
1922 bool ShelfView::IsRepostEvent(const ui::Event& event) {
1923 if (closing_event_time_ == base::TimeDelta())
1924 return false;
1926 base::TimeDelta delta =
1927 base::TimeDelta(event.time_stamp() - closing_event_time_);
1928 closing_event_time_ = base::TimeDelta();
1929 // If the current (press down) event is a repost event, the time stamp of
1930 // these two events should be the same.
1931 return (delta.InMilliseconds() == 0);
1934 const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const {
1935 int view_index = view_model_->GetIndexOfView(view);
1936 if (view_index == -1)
1937 return NULL;
1938 return &(model_->items()[view_index]);
1941 bool ShelfView::ShouldShowTooltipForView(const views::View* view) const {
1942 if (view == GetAppListButtonView() &&
1943 Shell::GetInstance()->GetAppListWindow())
1944 return false;
1945 const ShelfItem* item = ShelfItemForView(view);
1946 if (!item)
1947 return true;
1948 ShelfItemDelegate* item_delegate =
1949 item_manager_->GetShelfItemDelegate(item->id);
1950 return item_delegate->ShouldShowTooltip();
1953 int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const {
1954 ShelfWidget* shelf = RootWindowController::ForShelf(
1955 GetWidget()->GetNativeView())->shelf();
1956 ShelfAlignment align = shelf->GetAlignment();
1957 const gfx::Rect bounds = GetBoundsInScreen();
1958 int distance = 0;
1959 switch (align) {
1960 case SHELF_ALIGNMENT_BOTTOM:
1961 distance = bounds.y() - coordinate.y();
1962 break;
1963 case SHELF_ALIGNMENT_LEFT:
1964 distance = coordinate.x() - bounds.right();
1965 break;
1966 case SHELF_ALIGNMENT_RIGHT:
1967 distance = bounds.x() - coordinate.x();
1968 break;
1969 case SHELF_ALIGNMENT_TOP:
1970 distance = coordinate.y() - bounds.bottom();
1971 break;
1973 return distance > 0 ? distance : 0;
1976 } // namespace ash