Fix injecting script into all frames within <webview>.
[chromium-blink-merge.git] / ash / shelf / shelf_view.cc
blobe27b1800058525c8fce98684bb8495264609a473
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/gfx/canvas.h"
47 #include "ui/gfx/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 class ShelfMenuModelAdapter : public views::MenuModelAdapter {
140 public:
141 explicit ShelfMenuModelAdapter(ShelfMenuModel* menu_model);
143 // views::MenuModelAdapter:
144 const gfx::FontList* GetLabelFontList(int command_id) const override;
145 bool IsCommandEnabled(int id) const override;
146 void GetHorizontalIconMargins(int id,
147 int icon_size,
148 int* left_margin,
149 int* right_margin) const override;
150 bool GetForegroundColor(int command_id,
151 bool is_hovered,
152 SkColor* override_color) const override;
153 bool GetBackgroundColor(int command_id,
154 bool is_hovered,
155 SkColor* override_color) const override;
156 int GetMaxWidthForMenu(views::MenuItemView* menu) override;
157 bool ShouldReserveSpaceForSubmenuIndicator() const override;
159 private:
160 ShelfMenuModel* menu_model_;
162 DISALLOW_COPY_AND_ASSIGN(ShelfMenuModelAdapter);
165 ShelfMenuModelAdapter::ShelfMenuModelAdapter(ShelfMenuModel* menu_model)
166 : MenuModelAdapter(menu_model),
167 menu_model_(menu_model) {
170 const gfx::FontList* ShelfMenuModelAdapter::GetLabelFontList(
171 int command_id) const {
172 if (command_id != kCommandIdOfMenuName)
173 return MenuModelAdapter::GetLabelFontList(command_id);
175 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
176 return &rb.GetFontList(ui::ResourceBundle::BoldFont);
179 bool ShelfMenuModelAdapter::IsCommandEnabled(int id) const {
180 return id != kCommandIdOfMenuName;
183 bool ShelfMenuModelAdapter::GetForegroundColor(int command_id,
184 bool is_hovered,
185 SkColor* override_color) const {
186 if (command_id != kCommandIdOfMenuName)
187 return false;
189 *override_color = kCaptionItemForegroundColor;
190 return true;
193 bool ShelfMenuModelAdapter::GetBackgroundColor(int command_id,
194 bool is_hovered,
195 SkColor* override_color) const {
196 if (!menu_model_->IsCommandActive(command_id))
197 return false;
199 *override_color = is_hovered ? kFocusedActiveListItemBackgroundColor :
200 kActiveListItemBackgroundColor;
201 return true;
204 void ShelfMenuModelAdapter::GetHorizontalIconMargins(int command_id,
205 int icon_size,
206 int* left_margin,
207 int* right_margin) const {
208 *left_margin = kHorizontalIconSpacing;
209 *right_margin = (command_id != kCommandIdOfMenuName) ?
210 kHorizontalIconSpacing : -(icon_size + kHorizontalNoIconInsetSpacing);
213 int ShelfMenuModelAdapter::GetMaxWidthForMenu(views::MenuItemView* menu) {
214 return kMaximumAppMenuItemLength;
217 bool ShelfMenuModelAdapter::ShouldReserveSpaceForSubmenuIndicator() const {
218 return false;
221 // Custom FocusSearch used to navigate the shelf in the order items are in
222 // the ViewModel.
223 class ShelfFocusSearch : public views::FocusSearch {
224 public:
225 explicit ShelfFocusSearch(views::ViewModel* view_model)
226 : FocusSearch(NULL, true, true),
227 view_model_(view_model) {}
228 ~ShelfFocusSearch() override {}
230 // views::FocusSearch overrides:
231 View* FindNextFocusableView(View* starting_view,
232 bool reverse,
233 Direction direction,
234 bool check_starting_view,
235 views::FocusTraversable** focus_traversable,
236 View** focus_traversable_view) override {
237 int index = view_model_->GetIndexOfView(starting_view);
238 if (index == -1)
239 return view_model_->view_at(0);
241 if (reverse) {
242 --index;
243 if (index < 0)
244 index = view_model_->view_size() - 1;
245 } else {
246 ++index;
247 if (index >= view_model_->view_size())
248 index = 0;
250 return view_model_->view_at(index);
253 private:
254 views::ViewModel* view_model_;
256 DISALLOW_COPY_AND_ASSIGN(ShelfFocusSearch);
259 // AnimationDelegate used when inserting a new item. This steadily increases the
260 // opacity of the layer as the animation progress.
261 class FadeInAnimationDelegate : public gfx::AnimationDelegate {
262 public:
263 explicit FadeInAnimationDelegate(views::View* view) : view_(view) {}
264 ~FadeInAnimationDelegate() override {}
266 // AnimationDelegate overrides:
267 void AnimationProgressed(const Animation* animation) override {
268 view_->layer()->SetOpacity(animation->GetCurrentValue());
269 view_->layer()->ScheduleDraw();
271 void AnimationEnded(const Animation* animation) override {
272 view_->layer()->SetOpacity(1.0f);
273 view_->layer()->ScheduleDraw();
275 void AnimationCanceled(const Animation* animation) override {
276 view_->layer()->SetOpacity(1.0f);
277 view_->layer()->ScheduleDraw();
280 private:
281 views::View* view_;
283 DISALLOW_COPY_AND_ASSIGN(FadeInAnimationDelegate);
286 void ReflectItemStatus(const ShelfItem& item, ShelfButton* button) {
287 switch (item.status) {
288 case STATUS_CLOSED:
289 button->ClearState(ShelfButton::STATE_ACTIVE);
290 button->ClearState(ShelfButton::STATE_RUNNING);
291 button->ClearState(ShelfButton::STATE_ATTENTION);
292 break;
293 case STATUS_RUNNING:
294 button->ClearState(ShelfButton::STATE_ACTIVE);
295 button->AddState(ShelfButton::STATE_RUNNING);
296 button->ClearState(ShelfButton::STATE_ATTENTION);
297 break;
298 case STATUS_ACTIVE:
299 button->AddState(ShelfButton::STATE_ACTIVE);
300 button->ClearState(ShelfButton::STATE_RUNNING);
301 button->ClearState(ShelfButton::STATE_ATTENTION);
302 break;
303 case STATUS_ATTENTION:
304 button->ClearState(ShelfButton::STATE_ACTIVE);
305 button->ClearState(ShelfButton::STATE_RUNNING);
306 button->AddState(ShelfButton::STATE_ATTENTION);
307 break;
311 } // namespace
313 // AnimationDelegate used when deleting an item. This steadily decreased the
314 // opacity of the layer as the animation progress.
315 class ShelfView::FadeOutAnimationDelegate : public gfx::AnimationDelegate {
316 public:
317 FadeOutAnimationDelegate(ShelfView* host, views::View* view)
318 : shelf_view_(host),
319 view_(view) {}
320 ~FadeOutAnimationDelegate() override {}
322 // AnimationDelegate overrides:
323 void AnimationProgressed(const Animation* animation) override {
324 view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
325 view_->layer()->ScheduleDraw();
327 void AnimationEnded(const Animation* animation) override {
328 shelf_view_->OnFadeOutAnimationEnded();
330 void AnimationCanceled(const Animation* animation) override {}
332 private:
333 ShelfView* shelf_view_;
334 scoped_ptr<views::View> view_;
336 DISALLOW_COPY_AND_ASSIGN(FadeOutAnimationDelegate);
339 // AnimationDelegate used to trigger fading an element in. When an item is
340 // inserted this delegate is attached to the animation that expands the size of
341 // the item. When done it kicks off another animation to fade the item in.
342 class ShelfView::StartFadeAnimationDelegate : public gfx::AnimationDelegate {
343 public:
344 StartFadeAnimationDelegate(ShelfView* host,
345 views::View* view)
346 : shelf_view_(host),
347 view_(view) {}
348 ~StartFadeAnimationDelegate() override {}
350 // AnimationDelegate overrides:
351 void AnimationEnded(const Animation* animation) override {
352 shelf_view_->FadeIn(view_);
354 void AnimationCanceled(const Animation* animation) override {
355 view_->layer()->SetOpacity(1.0f);
358 private:
359 ShelfView* shelf_view_;
360 views::View* view_;
362 DISALLOW_COPY_AND_ASSIGN(StartFadeAnimationDelegate);
365 ShelfView::ShelfView(ShelfModel* model,
366 ShelfDelegate* delegate,
367 ShelfLayoutManager* manager)
368 : model_(model),
369 delegate_(delegate),
370 view_model_(new views::ViewModel),
371 first_visible_index_(0),
372 last_visible_index_(-1),
373 overflow_button_(NULL),
374 owner_overflow_bubble_(NULL),
375 drag_pointer_(NONE),
376 drag_view_(NULL),
377 start_drag_index_(-1),
378 context_menu_id_(0),
379 leading_inset_(kDefaultLeadingInset),
380 cancelling_drag_model_changed_(false),
381 last_hidden_index_(0),
382 closing_event_time_(base::TimeDelta()),
383 got_deleted_(NULL),
384 drag_and_drop_item_pinned_(false),
385 drag_and_drop_shelf_id_(0),
386 dragged_off_shelf_(false),
387 snap_back_from_rip_off_view_(NULL),
388 item_manager_(Shell::GetInstance()->shelf_item_delegate_manager()),
389 layout_manager_(manager),
390 overflow_mode_(false),
391 main_shelf_(NULL),
392 dragged_off_from_overflow_to_shelf_(false),
393 is_repost_event_(false),
394 last_pressed_index_(-1) {
395 DCHECK(model_);
396 bounds_animator_.reset(new views::BoundsAnimator(this));
397 bounds_animator_->AddObserver(this);
398 set_context_menu_controller(this);
399 focus_search_.reset(new ShelfFocusSearch(view_model_.get()));
400 tooltip_.reset(new ShelfTooltipManager(manager, this));
403 ShelfView::~ShelfView() {
404 bounds_animator_->RemoveObserver(this);
405 model_->RemoveObserver(this);
406 // If we are inside the MenuRunner, we need to know if we were getting
407 // deleted while it was running.
408 if (got_deleted_)
409 *got_deleted_ = true;
412 void ShelfView::Init() {
413 model_->AddObserver(this);
415 const ShelfItems& items(model_->items());
416 for (ShelfItems::const_iterator i = items.begin(); i != items.end(); ++i) {
417 views::View* child = CreateViewForItem(*i);
418 child->SetPaintToLayer(true);
419 view_model_->Add(child, static_cast<int>(i - items.begin()));
420 AddChildView(child);
422 overflow_button_ = new OverflowButton(this);
423 overflow_button_->set_context_menu_controller(this);
424 ConfigureChildView(overflow_button_);
425 AddChildView(overflow_button_);
427 // We'll layout when our bounds change.
430 void ShelfView::OnShelfAlignmentChanged() {
431 overflow_button_->OnShelfAlignmentChanged();
432 LayoutToIdealBounds();
433 for (int i = 0; i < view_model_->view_size(); ++i) {
434 if (i >= first_visible_index_ && i <= last_visible_index_)
435 view_model_->view_at(i)->Layout();
437 tooltip_->Close();
438 if (overflow_bubble_)
439 overflow_bubble_->Hide();
442 void ShelfView::SchedulePaintForAllButtons() {
443 for (int i = 0; i < view_model_->view_size(); ++i) {
444 if (i >= first_visible_index_ && i <= last_visible_index_)
445 view_model_->view_at(i)->SchedulePaint();
447 if (overflow_button_ && overflow_button_->visible())
448 overflow_button_->SchedulePaint();
451 gfx::Rect ShelfView::GetIdealBoundsOfItemIcon(ShelfID id) {
452 int index = model_->ItemIndexByID(id);
453 if (index == -1)
454 return gfx::Rect();
455 // Map all items from overflow area to the overflow button. Note that the
456 // section between last_index_hidden_ and model_->FirstPanelIndex() is the
457 // list of invisible panel items. However, these items are currently nowhere
458 // represented and get dropped instead - see (crbug.com/378907). As such there
459 // is no way to address them or place them. We therefore move them over the
460 // overflow button.
461 if (index > last_visible_index_ && index < model_->FirstPanelIndex())
462 index = last_visible_index_ + 1;
463 const gfx::Rect& ideal_bounds(view_model_->ideal_bounds(index));
464 DCHECK_NE(TYPE_APP_LIST, model_->items()[index].type);
465 views::View* view = view_model_->view_at(index);
466 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
467 ShelfButton* button = static_cast<ShelfButton*>(view);
468 gfx::Rect icon_bounds = button->GetIconBounds();
469 return gfx::Rect(GetMirroredXWithWidthInView(
470 ideal_bounds.x() + icon_bounds.x(), icon_bounds.width()),
471 ideal_bounds.y() + icon_bounds.y(),
472 icon_bounds.width(),
473 icon_bounds.height());
476 void ShelfView::UpdatePanelIconPosition(ShelfID id,
477 const gfx::Point& midpoint) {
478 int current_index = model_->ItemIndexByID(id);
479 int first_panel_index = model_->FirstPanelIndex();
480 if (current_index < first_panel_index)
481 return;
483 gfx::Point midpoint_in_view(GetMirroredXInView(midpoint.x()),
484 midpoint.y());
485 int target_index = current_index;
486 while (target_index > first_panel_index &&
487 layout_manager_->PrimaryAxisValue(
488 view_model_->ideal_bounds(target_index).x(),
489 view_model_->ideal_bounds(target_index).y()) >
490 layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
491 midpoint_in_view.y())) {
492 --target_index;
494 while (target_index < view_model_->view_size() - 1 &&
495 layout_manager_->PrimaryAxisValue(
496 view_model_->ideal_bounds(target_index).right(),
497 view_model_->ideal_bounds(target_index).bottom()) <
498 layout_manager_->PrimaryAxisValue(midpoint_in_view.x(),
499 midpoint_in_view.y())) {
500 ++target_index;
502 if (current_index != target_index)
503 model_->Move(current_index, target_index);
506 bool ShelfView::IsShowingMenu() const {
507 return (launcher_menu_runner_.get() &&
508 launcher_menu_runner_->IsRunning());
511 bool ShelfView::IsShowingOverflowBubble() const {
512 return overflow_bubble_.get() && overflow_bubble_->IsShowing();
515 views::View* ShelfView::GetAppListButtonView() const {
516 for (int i = 0; i < model_->item_count(); ++i) {
517 if (model_->items()[i].type == TYPE_APP_LIST)
518 return view_model_->view_at(i);
521 NOTREACHED() << "Applist button not found";
522 return NULL;
525 ////////////////////////////////////////////////////////////////////////////////
526 // ShelfView, FocusTraversable implementation:
528 views::FocusSearch* ShelfView::GetFocusSearch() {
529 return focus_search_.get();
532 views::FocusTraversable* ShelfView::GetFocusTraversableParent() {
533 return parent()->GetFocusTraversable();
536 View* ShelfView::GetFocusTraversableParentView() {
537 return this;
540 void ShelfView::CreateDragIconProxy(
541 const gfx::Point& location_in_screen_coordinates,
542 const gfx::ImageSkia& icon,
543 views::View* replaced_view,
544 const gfx::Vector2d& cursor_offset_from_center,
545 float scale_factor) {
546 drag_replaced_view_ = replaced_view;
547 drag_image_.reset(new ash::DragImageView(
548 drag_replaced_view_->GetWidget()->GetNativeWindow()->GetRootWindow(),
549 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE));
550 drag_image_->SetImage(icon);
551 gfx::Size size = drag_image_->GetPreferredSize();
552 size.set_width(size.width() * scale_factor);
553 size.set_height(size.height() * scale_factor);
554 drag_image_offset_ = gfx::Vector2d(size.width() / 2, size.height() / 2) +
555 cursor_offset_from_center;
556 gfx::Rect drag_image_bounds(
557 location_in_screen_coordinates - drag_image_offset_,
558 size);
559 drag_image_->SetBoundsInScreen(drag_image_bounds);
560 drag_image_->SetWidgetVisible(true);
563 void ShelfView::UpdateDragIconProxy(
564 const gfx::Point& location_in_screen_coordinates) {
565 // TODO(jennyz): Investigate why drag_image_ becomes NULL at this point per
566 // crbug.com/34722, while the app list item is still being dragged around.
567 if (drag_image_) {
568 drag_image_->SetScreenPosition(
569 location_in_screen_coordinates - drag_image_offset_);
573 void ShelfView::DestroyDragIconProxy() {
574 drag_image_.reset();
575 drag_image_offset_ = gfx::Vector2d(0, 0);
578 bool ShelfView::StartDrag(const std::string& app_id,
579 const gfx::Point& location_in_screen_coordinates) {
580 // Bail if an operation is already going on - or the cursor is not inside.
581 // This could happen if mouse / touch operations overlap.
582 if (drag_and_drop_shelf_id_ ||
583 !GetBoundsInScreen().Contains(location_in_screen_coordinates))
584 return false;
586 // If the AppsGridView (which was dispatching this event) was opened by our
587 // button, ShelfView dragging operations are locked and we have to unlock.
588 CancelDrag(-1);
589 drag_and_drop_item_pinned_ = false;
590 drag_and_drop_app_id_ = app_id;
591 drag_and_drop_shelf_id_ =
592 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_);
593 // Check if the application is known and pinned - if not, we have to pin it so
594 // that we can re-arrange the shelf order accordingly. Note that items have
595 // to be pinned to give them the same (order) possibilities as a shortcut.
596 // When an item is dragged from overflow to shelf, IsShowingOverflowBubble()
597 // returns true. At this time, we don't need to pin the item.
598 if (!IsShowingOverflowBubble() &&
599 (!drag_and_drop_shelf_id_ ||
600 !delegate_->IsAppPinned(app_id))) {
601 delegate_->PinAppWithID(app_id);
602 drag_and_drop_shelf_id_ =
603 delegate_->GetShelfIDForAppID(drag_and_drop_app_id_);
604 if (!drag_and_drop_shelf_id_)
605 return false;
606 drag_and_drop_item_pinned_ = true;
608 views::View* drag_and_drop_view = view_model_->view_at(
609 model_->ItemIndexByID(drag_and_drop_shelf_id_));
610 DCHECK(drag_and_drop_view);
612 // Since there is already an icon presented by the caller, we hide this item
613 // for now. That has to be done by reducing the size since the visibility will
614 // change once a regrouping animation is performed.
615 pre_drag_and_drop_size_ = drag_and_drop_view->size();
616 drag_and_drop_view->SetSize(gfx::Size());
618 // First we have to center the mouse cursor over the item.
619 gfx::Point pt = drag_and_drop_view->GetBoundsInScreen().CenterPoint();
620 views::View::ConvertPointFromScreen(drag_and_drop_view, &pt);
621 gfx::Point point_in_root = location_in_screen_coordinates;
622 ::wm::ConvertPointFromScreen(
623 ash::wm::GetRootWindowAt(location_in_screen_coordinates), &point_in_root);
624 ui::MouseEvent event(ui::ET_MOUSE_PRESSED, pt, point_in_root, 0, 0);
625 PointerPressedOnButton(drag_and_drop_view,
626 ShelfButtonHost::DRAG_AND_DROP,
627 event);
629 // Drag the item where it really belongs.
630 Drag(location_in_screen_coordinates);
631 return true;
634 bool ShelfView::Drag(const gfx::Point& location_in_screen_coordinates) {
635 if (!drag_and_drop_shelf_id_ ||
636 !GetBoundsInScreen().Contains(location_in_screen_coordinates))
637 return false;
639 gfx::Point pt = location_in_screen_coordinates;
640 views::View* drag_and_drop_view = view_model_->view_at(
641 model_->ItemIndexByID(drag_and_drop_shelf_id_));
642 ConvertPointFromScreen(drag_and_drop_view, &pt);
643 gfx::Point point_in_root = location_in_screen_coordinates;
644 ::wm::ConvertPointFromScreen(
645 ash::wm::GetRootWindowAt(location_in_screen_coordinates), &point_in_root);
646 ui::MouseEvent event(ui::ET_MOUSE_DRAGGED, pt, point_in_root, 0, 0);
647 PointerDraggedOnButton(drag_and_drop_view,
648 ShelfButtonHost::DRAG_AND_DROP,
649 event);
650 return true;
653 void ShelfView::EndDrag(bool cancel) {
654 if (!drag_and_drop_shelf_id_)
655 return;
657 views::View* drag_and_drop_view = view_model_->view_at(
658 model_->ItemIndexByID(drag_and_drop_shelf_id_));
659 PointerReleasedOnButton(
660 drag_and_drop_view, ShelfButtonHost::DRAG_AND_DROP, cancel);
662 // Either destroy the temporarily created item - or - make the item visible.
663 if (drag_and_drop_item_pinned_ && cancel) {
664 delegate_->UnpinAppWithID(drag_and_drop_app_id_);
665 } else if (drag_and_drop_view) {
666 if (cancel) {
667 // When a hosted drag gets canceled, the item can remain in the same slot
668 // and it might have moved within the bounds. In that case the item need
669 // to animate back to its correct location.
670 AnimateToIdealBounds();
671 } else {
672 drag_and_drop_view->SetSize(pre_drag_and_drop_size_);
676 drag_and_drop_shelf_id_ = 0;
679 void ShelfView::LayoutToIdealBounds() {
680 if (bounds_animator_->IsAnimating()) {
681 AnimateToIdealBounds();
682 return;
685 IdealBounds ideal_bounds;
686 CalculateIdealBounds(&ideal_bounds);
687 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_);
688 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
691 void ShelfView::UpdateAllButtonsVisibilityInOverflowMode() {
692 // The overflow button is not shown in overflow mode.
693 overflow_button_->SetVisible(false);
694 DCHECK_LT(last_visible_index_, view_model_->view_size());
695 for (int i = 0; i < view_model_->view_size(); ++i) {
696 bool visible = i >= first_visible_index_ &&
697 i <= last_visible_index_;
698 // To track the dragging of |drag_view_| continuously, its visibility
699 // should be always true regardless of its position.
700 if (dragged_off_from_overflow_to_shelf_ &&
701 view_model_->view_at(i) == drag_view_)
702 view_model_->view_at(i)->SetVisible(true);
703 else
704 view_model_->view_at(i)->SetVisible(visible);
708 void ShelfView::CalculateIdealBounds(IdealBounds* bounds) const {
709 int available_size = layout_manager_->PrimaryAxisValue(width(), height());
710 DCHECK(model_->item_count() == view_model_->view_size());
711 if (!available_size)
712 return;
714 int first_panel_index = model_->FirstPanelIndex();
715 int last_button_index = first_panel_index - 1;
717 int x = 0;
718 int y = 0;
719 int button_size = kShelfButtonSize;
720 int button_spacing = kShelfButtonSpacing;
722 int w = layout_manager_->PrimaryAxisValue(button_size, width());
723 int h = layout_manager_->PrimaryAxisValue(height(), button_size);
724 for (int i = 0; i < view_model_->view_size(); ++i) {
725 if (i < first_visible_index_) {
726 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, 0, 0));
727 continue;
730 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
731 if (i != last_button_index) {
732 x = layout_manager_->PrimaryAxisValue(x + w + button_spacing, x);
733 y = layout_manager_->PrimaryAxisValue(y, y + h + button_spacing);
737 if (is_overflow_mode()) {
738 const_cast<ShelfView*>(this)->UpdateAllButtonsVisibilityInOverflowMode();
739 return;
742 // Right aligned icons.
743 int end_position = available_size - button_spacing;
744 x = layout_manager_->PrimaryAxisValue(end_position, 0);
745 y = layout_manager_->PrimaryAxisValue(0, end_position);
746 for (int i = view_model_->view_size() - 1;
747 i >= first_panel_index; --i) {
748 x = layout_manager_->PrimaryAxisValue(x - w - button_spacing, x);
749 y = layout_manager_->PrimaryAxisValue(y, y - h - button_spacing);
750 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
751 end_position = layout_manager_->PrimaryAxisValue(x, y);
754 // Icons on the left / top are guaranteed up to kLeftIconProportion of
755 // the available space.
756 int last_icon_position = layout_manager_->PrimaryAxisValue(
757 view_model_->ideal_bounds(last_button_index).right(),
758 view_model_->ideal_bounds(last_button_index).bottom()) + button_size;
759 int reserved_icon_space = available_size * kReservedNonPanelIconProportion;
760 if (last_icon_position < reserved_icon_space)
761 end_position = last_icon_position;
762 else
763 end_position = std::max(end_position, reserved_icon_space);
765 bounds->overflow_bounds.set_size(
766 gfx::Size(layout_manager_->PrimaryAxisValue(w, width()),
767 layout_manager_->PrimaryAxisValue(height(), h)));
769 last_visible_index_ = DetermineLastVisibleIndex(
770 end_position - button_size);
771 last_hidden_index_ = DetermineFirstVisiblePanelIndex(end_position) - 1;
772 bool show_overflow = last_visible_index_ < last_button_index ||
773 last_hidden_index_ >= first_panel_index;
775 // Create Space for the overflow button
776 if (show_overflow &&
777 last_visible_index_ > 0 && last_visible_index_ < last_button_index)
778 --last_visible_index_;
779 for (int i = 0; i < view_model_->view_size(); ++i) {
780 bool visible = i <= last_visible_index_ || i > last_hidden_index_;
781 // To receive drag event continuously from |drag_view_| during the dragging
782 // off from the shelf, don't make |drag_view_| invisible. It will be
783 // eventually invisible and removed from the |view_model_| by
784 // FinalizeRipOffDrag().
785 if (dragged_off_shelf_ && view_model_->view_at(i) == drag_view_)
786 continue;
787 view_model_->view_at(i)->SetVisible(visible);
790 overflow_button_->SetVisible(show_overflow);
791 if (show_overflow) {
792 DCHECK_NE(0, view_model_->view_size());
793 if (last_visible_index_ == -1) {
794 x = 0;
795 y = 0;
796 } else {
797 x = layout_manager_->PrimaryAxisValue(
798 view_model_->ideal_bounds(last_visible_index_).right(),
799 view_model_->ideal_bounds(last_visible_index_).x());
800 y = layout_manager_->PrimaryAxisValue(
801 view_model_->ideal_bounds(last_visible_index_).y(),
802 view_model_->ideal_bounds(last_visible_index_).bottom());
804 // Set all hidden panel icon positions to be on the overflow button.
805 for (int i = first_panel_index; i <= last_hidden_index_; ++i)
806 view_model_->set_ideal_bounds(i, gfx::Rect(x, y, w, h));
808 // Add more space between last visible item and overflow button.
809 // Without this, two buttons look too close compared with other items.
810 x = layout_manager_->PrimaryAxisValue(x + button_spacing, x);
811 y = layout_manager_->PrimaryAxisValue(y, y + button_spacing);
813 bounds->overflow_bounds.set_x(x);
814 bounds->overflow_bounds.set_y(y);
815 if (overflow_bubble_.get() && overflow_bubble_->IsShowing())
816 UpdateOverflowRange(overflow_bubble_->shelf_view());
817 } else {
818 if (overflow_bubble_)
819 overflow_bubble_->Hide();
823 int ShelfView::DetermineLastVisibleIndex(int max_value) const {
824 int index = model_->FirstPanelIndex() - 1;
825 while (index >= 0 &&
826 layout_manager_->PrimaryAxisValue(
827 view_model_->ideal_bounds(index).right(),
828 view_model_->ideal_bounds(index).bottom()) > max_value) {
829 index--;
831 return index;
834 int ShelfView::DetermineFirstVisiblePanelIndex(int min_value) const {
835 int index = model_->FirstPanelIndex();
836 while (index < view_model_->view_size() &&
837 layout_manager_->PrimaryAxisValue(
838 view_model_->ideal_bounds(index).right(),
839 view_model_->ideal_bounds(index).bottom()) < min_value) {
840 ++index;
842 return index;
845 void ShelfView::AddIconObserver(ShelfIconObserver* observer) {
846 observers_.AddObserver(observer);
849 void ShelfView::RemoveIconObserver(ShelfIconObserver* observer) {
850 observers_.RemoveObserver(observer);
853 void ShelfView::AnimateToIdealBounds() {
854 IdealBounds ideal_bounds;
855 CalculateIdealBounds(&ideal_bounds);
856 for (int i = 0; i < view_model_->view_size(); ++i) {
857 View* view = view_model_->view_at(i);
858 bounds_animator_->AnimateViewTo(view, view_model_->ideal_bounds(i));
859 // Now that the item animation starts, we have to make sure that the
860 // padding of the first gets properly transferred to the new first item.
861 if (i && view->border())
862 view->SetBorder(views::Border::NullBorder());
864 overflow_button_->SetBoundsRect(ideal_bounds.overflow_bounds);
867 views::View* ShelfView::CreateViewForItem(const ShelfItem& item) {
868 views::View* view = NULL;
869 switch (item.type) {
870 case TYPE_BROWSER_SHORTCUT:
871 case TYPE_APP_SHORTCUT:
872 case TYPE_WINDOWED_APP:
873 case TYPE_PLATFORM_APP:
874 case TYPE_DIALOG:
875 case TYPE_APP_PANEL: {
876 ShelfButton* button = ShelfButton::Create(this, this, layout_manager_);
877 button->SetImage(item.image);
878 ReflectItemStatus(item, button);
879 view = button;
880 break;
883 case TYPE_APP_LIST: {
884 view = new AppListButton(this, this, layout_manager_->shelf_widget());
885 break;
888 default:
889 break;
891 view->set_context_menu_controller(this);
893 DCHECK(view);
894 ConfigureChildView(view);
895 return view;
898 void ShelfView::FadeIn(views::View* view) {
899 view->SetVisible(true);
900 view->layer()->SetOpacity(0);
901 AnimateToIdealBounds();
902 bounds_animator_->SetAnimationDelegate(
903 view,
904 scoped_ptr<gfx::AnimationDelegate>(new FadeInAnimationDelegate(view)));
907 void ShelfView::PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event) {
908 DCHECK(!dragging());
909 DCHECK(drag_view_);
910 drag_pointer_ = pointer;
911 start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
913 if (start_drag_index_== -1) {
914 CancelDrag(-1);
915 return;
918 // If the item is no longer draggable, bail out.
919 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
920 model_->items()[start_drag_index_].id);
921 if (!item_delegate->IsDraggable()) {
922 CancelDrag(-1);
923 return;
926 // Move the view to the front so that it appears on top of other views.
927 ReorderChildView(drag_view_, -1);
928 bounds_animator_->StopAnimatingView(drag_view_);
931 void ShelfView::ContinueDrag(const ui::LocatedEvent& event) {
932 // Due to a syncing operation the application might have been removed.
933 // Bail if it is gone.
934 int current_index = view_model_->GetIndexOfView(drag_view_);
935 DCHECK_NE(-1, current_index);
937 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
938 model_->items()[current_index].id);
939 if (!item_delegate->IsDraggable()) {
940 CancelDrag(-1);
941 return;
944 // If this is not a drag and drop host operation and not the app list item,
945 // check if the item got ripped off the shelf - if it did we are done.
946 if (!drag_and_drop_shelf_id_ &&
947 RemovableByRipOff(current_index) != NOT_REMOVABLE) {
948 if (HandleRipOffDrag(event))
949 return;
950 // The rip off handler could have changed the location of the item.
951 current_index = view_model_->GetIndexOfView(drag_view_);
954 // TODO: I don't think this works correctly with RTL.
955 gfx::Point drag_point(event.location());
956 ConvertPointToTarget(drag_view_, this, &drag_point);
958 // Constrain the location to the range of valid indices for the type.
959 std::pair<int, int> indices(GetDragRange(current_index));
960 int first_drag_index = indices.first;
961 int last_drag_index = indices.second;
962 // If the last index isn't valid, we're overflowing. Constrain to the app list
963 // (which is the last visible item).
964 if (first_drag_index < model_->FirstPanelIndex() &&
965 last_drag_index > last_visible_index_)
966 last_drag_index = last_visible_index_;
967 int x = 0, y = 0;
968 if (layout_manager_->IsHorizontalAlignment()) {
969 x = std::max(view_model_->ideal_bounds(indices.first).x(),
970 drag_point.x() - drag_origin_.x());
971 x = std::min(view_model_->ideal_bounds(last_drag_index).right() -
972 view_model_->ideal_bounds(current_index).width(),
974 if (drag_view_->x() == x)
975 return;
976 drag_view_->SetX(x);
977 } else {
978 y = std::max(view_model_->ideal_bounds(indices.first).y(),
979 drag_point.y() - drag_origin_.y());
980 y = std::min(view_model_->ideal_bounds(last_drag_index).bottom() -
981 view_model_->ideal_bounds(current_index).height(),
983 if (drag_view_->y() == y)
984 return;
985 drag_view_->SetY(y);
988 int target_index =
989 views::ViewModelUtils::DetermineMoveIndex(
990 *view_model_, drag_view_,
991 layout_manager_->IsHorizontalAlignment() ?
992 views::ViewModelUtils::HORIZONTAL :
993 views::ViewModelUtils::VERTICAL,
994 x, y);
995 target_index =
996 std::min(indices.second, std::max(target_index, indices.first));
997 if (target_index == current_index)
998 return;
1000 // Change the model, the ShelfItemMoved() callback will handle the
1001 // |view_model_| update.
1002 model_->Move(current_index, target_index);
1003 bounds_animator_->StopAnimatingView(drag_view_);
1006 bool ShelfView::HandleRipOffDrag(const ui::LocatedEvent& event) {
1007 int current_index = view_model_->GetIndexOfView(drag_view_);
1008 DCHECK_NE(-1, current_index);
1009 std::string dragged_app_id =
1010 delegate_->GetAppIDForShelfID(model_->items()[current_index].id);
1012 gfx::Point screen_location = event.root_location();
1013 ::wm::ConvertPointToScreen(GetWidget()->GetNativeWindow()->GetRootWindow(),
1014 &screen_location);
1016 // To avoid ugly forwards and backwards flipping we use different constants
1017 // for ripping off / re-inserting the items.
1018 if (dragged_off_shelf_) {
1019 // If the shelf/overflow bubble bounds contains |screen_location| we insert
1020 // the item back into the shelf.
1021 if (GetBoundsForDragInsertInScreen().Contains(screen_location)) {
1022 if (dragged_off_from_overflow_to_shelf_) {
1023 // During the dragging an item from Shelf to Overflow, it can enter here
1024 // directly because both are located very closly.
1025 main_shelf_->EndDrag(true);
1026 // Stops the animation of |drag_view_| and sets its bounds explicitly
1027 // becase ContinueDrag() stops its animation. Without this, unexpected
1028 // bounds will be set.
1029 bounds_animator_->StopAnimatingView(drag_view_);
1030 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1031 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index));
1032 dragged_off_from_overflow_to_shelf_ = false;
1034 // Destroy our proxy view item.
1035 DestroyDragIconProxy();
1036 // Re-insert the item and return simply false since the caller will handle
1037 // the move as in any normal case.
1038 dragged_off_shelf_ = false;
1039 drag_view_->layer()->SetOpacity(1.0f);
1040 // The size of Overflow bubble should be updated immediately when an item
1041 // is re-inserted.
1042 if (is_overflow_mode())
1043 PreferredSizeChanged();
1044 return false;
1045 } else if (is_overflow_mode() &&
1046 main_shelf_->GetBoundsForDragInsertInScreen().Contains(
1047 screen_location)) {
1048 if (!dragged_off_from_overflow_to_shelf_) {
1049 dragged_off_from_overflow_to_shelf_ = true;
1050 drag_image_->SetOpacity(1.0f);
1051 main_shelf_->StartDrag(dragged_app_id, screen_location);
1052 } else {
1053 main_shelf_->Drag(screen_location);
1055 } else if (dragged_off_from_overflow_to_shelf_) {
1056 // Makes the |drag_image_| partially disappear again.
1057 dragged_off_from_overflow_to_shelf_ = false;
1058 drag_image_->SetOpacity(kDraggedImageOpacity);
1059 main_shelf_->EndDrag(true);
1060 bounds_animator_->StopAnimatingView(drag_view_);
1061 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1062 drag_view_->SetBoundsRect(view_model_->ideal_bounds(drag_view_index));
1064 // Move our proxy view item.
1065 UpdateDragIconProxy(screen_location);
1066 return true;
1068 // Check if we are too far away from the shelf to enter the ripped off state.
1069 // Determine the distance to the shelf.
1070 int delta = CalculateShelfDistance(screen_location);
1071 if (delta > kRipOffDistance) {
1072 // Create a proxy view item which can be moved anywhere.
1073 CreateDragIconProxy(event.root_location(),
1074 drag_view_->GetImage(),
1075 drag_view_,
1076 gfx::Vector2d(0, 0),
1077 kDragAndDropProxyScale);
1078 drag_view_->layer()->SetOpacity(0.0f);
1079 dragged_off_shelf_ = true;
1080 if (RemovableByRipOff(current_index) == REMOVABLE) {
1081 // Move the item to the front of the first panel item and hide it.
1082 // ShelfItemMoved() callback will handle the |view_model_| update and
1083 // call AnimateToIdealBounds().
1084 if (current_index != model_->FirstPanelIndex() - 1) {
1085 model_->Move(current_index, model_->FirstPanelIndex() - 1);
1086 StartFadeInLastVisibleItem();
1087 } else if (is_overflow_mode()) {
1088 // Overflow bubble should be shrunk when an item is ripped off.
1089 PreferredSizeChanged();
1091 // Make the item partially disappear to show that it will get removed if
1092 // dropped.
1093 drag_image_->SetOpacity(kDraggedImageOpacity);
1095 return true;
1097 return false;
1100 void ShelfView::FinalizeRipOffDrag(bool cancel) {
1101 if (!dragged_off_shelf_)
1102 return;
1103 // Make sure we do not come in here again.
1104 dragged_off_shelf_ = false;
1106 // Coming here we should always have a |drag_view_|.
1107 DCHECK(drag_view_);
1108 int current_index = view_model_->GetIndexOfView(drag_view_);
1109 // If the view isn't part of the model anymore (|current_index| == -1), a sync
1110 // operation must have removed it. In that case we shouldn't change the model
1111 // and only delete the proxy image.
1112 if (current_index == -1) {
1113 DestroyDragIconProxy();
1114 return;
1117 // Set to true when the animation should snap back to where it was before.
1118 bool snap_back = false;
1119 // Items which cannot be dragged off will be handled as a cancel.
1120 if (!cancel) {
1121 if (dragged_off_from_overflow_to_shelf_) {
1122 dragged_off_from_overflow_to_shelf_ = false;
1123 main_shelf_->EndDrag(false);
1124 drag_view_->layer()->SetOpacity(1.0f);
1125 } else if (RemovableByRipOff(current_index) != REMOVABLE) {
1126 // Make sure we do not try to remove un-removable items like items which
1127 // were not pinned or have to be always there.
1128 cancel = true;
1129 snap_back = true;
1130 } else {
1131 // Make sure the item stays invisible upon removal.
1132 drag_view_->SetVisible(false);
1133 std::string app_id =
1134 delegate_->GetAppIDForShelfID(model_->items()[current_index].id);
1135 delegate_->UnpinAppWithID(app_id);
1138 if (cancel || snap_back) {
1139 if (dragged_off_from_overflow_to_shelf_) {
1140 dragged_off_from_overflow_to_shelf_ = false;
1141 // Main shelf handles revert of dragged item.
1142 main_shelf_->EndDrag(true);
1143 drag_view_->layer()->SetOpacity(1.0f);
1144 } else if (!cancelling_drag_model_changed_) {
1145 // Only do something if the change did not come through a model change.
1146 gfx::Rect drag_bounds = drag_image_->GetBoundsInScreen();
1147 gfx::Point relative_to = GetBoundsInScreen().origin();
1148 gfx::Rect target(
1149 gfx::PointAtOffsetFromOrigin(drag_bounds.origin()- relative_to),
1150 drag_bounds.size());
1151 drag_view_->SetBoundsRect(target);
1152 // Hide the status from the active item since we snap it back now. Upon
1153 // animation end the flag gets cleared if |snap_back_from_rip_off_view_|
1154 // is set.
1155 snap_back_from_rip_off_view_ = drag_view_;
1156 drag_view_->AddState(ShelfButton::STATE_HIDDEN);
1157 // When a canceling drag model is happening, the view model is diverged
1158 // from the menu model and movements / animations should not be done.
1159 model_->Move(current_index, start_drag_index_);
1160 AnimateToIdealBounds();
1162 drag_view_->layer()->SetOpacity(1.0f);
1164 DestroyDragIconProxy();
1167 ShelfView::RemovableState ShelfView::RemovableByRipOff(int index) const {
1168 DCHECK(index >= 0 && index < model_->item_count());
1169 ShelfItemType type = model_->items()[index].type;
1170 if (type == TYPE_APP_LIST || type == TYPE_DIALOG || !delegate_->CanPin())
1171 return NOT_REMOVABLE;
1173 std::string app_id = delegate_->GetAppIDForShelfID(model_->items()[index].id);
1174 // Note: Only pinned app shortcuts can be removed!
1175 return (type == TYPE_APP_SHORTCUT && delegate_->IsAppPinned(app_id)) ?
1176 REMOVABLE : DRAGGABLE;
1179 bool ShelfView::SameDragType(ShelfItemType typea, ShelfItemType typeb) const {
1180 switch (typea) {
1181 case TYPE_APP_SHORTCUT:
1182 case TYPE_BROWSER_SHORTCUT:
1183 return (typeb == TYPE_APP_SHORTCUT || typeb == TYPE_BROWSER_SHORTCUT);
1184 case TYPE_APP_LIST:
1185 case TYPE_PLATFORM_APP:
1186 case TYPE_WINDOWED_APP:
1187 case TYPE_APP_PANEL:
1188 case TYPE_DIALOG:
1189 return typeb == typea;
1190 case TYPE_UNDEFINED:
1191 NOTREACHED() << "ShelfItemType must be set.";
1192 return false;
1194 NOTREACHED();
1195 return false;
1198 std::pair<int, int> ShelfView::GetDragRange(int index) {
1199 int min_index = -1;
1200 int max_index = -1;
1201 ShelfItemType type = model_->items()[index].type;
1202 for (int i = 0; i < model_->item_count(); ++i) {
1203 if (SameDragType(model_->items()[i].type, type)) {
1204 if (min_index == -1)
1205 min_index = i;
1206 max_index = i;
1209 return std::pair<int, int>(min_index, max_index);
1212 void ShelfView::ConfigureChildView(views::View* view) {
1213 view->SetPaintToLayer(true);
1214 view->layer()->SetFillsBoundsOpaquely(false);
1217 void ShelfView::ToggleOverflowBubble() {
1218 if (IsShowingOverflowBubble()) {
1219 overflow_bubble_->Hide();
1220 return;
1223 if (!overflow_bubble_)
1224 overflow_bubble_.reset(new OverflowBubble());
1226 ShelfView* overflow_view =
1227 new ShelfView(model_, delegate_, layout_manager_);
1228 overflow_view->overflow_mode_ = true;
1229 overflow_view->Init();
1230 overflow_view->set_owner_overflow_bubble(overflow_bubble_.get());
1231 overflow_view->OnShelfAlignmentChanged();
1232 overflow_view->main_shelf_ = this;
1233 UpdateOverflowRange(overflow_view);
1235 overflow_bubble_->Show(overflow_button_, overflow_view);
1237 Shell::GetInstance()->UpdateShelfVisibility();
1240 void ShelfView::OnFadeOutAnimationEnded() {
1241 AnimateToIdealBounds();
1242 StartFadeInLastVisibleItem();
1245 void ShelfView::StartFadeInLastVisibleItem() {
1246 // If overflow button is visible and there is a valid new last item, fading
1247 // the new last item in after sliding animation is finished.
1248 if (overflow_button_->visible() && last_visible_index_ >= 0) {
1249 views::View* last_visible_view = view_model_->view_at(last_visible_index_);
1250 last_visible_view->layer()->SetOpacity(0);
1251 bounds_animator_->SetAnimationDelegate(
1252 last_visible_view,
1253 scoped_ptr<gfx::AnimationDelegate>(
1254 new StartFadeAnimationDelegate(this, last_visible_view)));
1258 void ShelfView::UpdateOverflowRange(ShelfView* overflow_view) const {
1259 const int first_overflow_index = last_visible_index_ + 1;
1260 const int last_overflow_index = last_hidden_index_;
1261 DCHECK_LE(first_overflow_index, last_overflow_index);
1262 DCHECK_LT(last_overflow_index, view_model_->view_size());
1264 overflow_view->first_visible_index_ = first_overflow_index;
1265 overflow_view->last_visible_index_ = last_overflow_index;
1268 bool ShelfView::ShouldHideTooltip(const gfx::Point& cursor_location) {
1269 gfx::Rect active_bounds;
1271 for (int i = 0; i < child_count(); ++i) {
1272 views::View* child = child_at(i);
1273 if (child == overflow_button_)
1274 continue;
1275 if (!ShouldShowTooltipForView(child))
1276 continue;
1278 gfx::Rect child_bounds = child->GetMirroredBounds();
1279 active_bounds.Union(child_bounds);
1282 return !active_bounds.Contains(cursor_location);
1285 gfx::Rect ShelfView::GetVisibleItemsBoundsInScreen() {
1286 gfx::Size preferred_size = GetPreferredSize();
1287 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1288 ConvertPointToScreen(this, &origin);
1289 return gfx::Rect(origin, preferred_size);
1292 gfx::Rect ShelfView::GetBoundsForDragInsertInScreen() {
1293 gfx::Size preferred_size;
1294 if (is_overflow_mode()) {
1295 DCHECK(owner_overflow_bubble_);
1296 gfx::Rect bubble_bounds =
1297 owner_overflow_bubble_->bubble_view()->GetBubbleBounds();
1298 preferred_size = bubble_bounds.size();
1299 } else {
1300 const int last_button_index = view_model_->view_size() - 1;
1301 gfx::Rect last_button_bounds =
1302 view_model_->view_at(last_button_index)->bounds();
1303 if (overflow_button_->visible() &&
1304 model_->GetItemIndexForType(TYPE_APP_PANEL) == -1) {
1305 // When overflow button is visible and shelf has no panel items,
1306 // last_button_bounds should be overflow button's bounds.
1307 last_button_bounds = overflow_button_->bounds();
1310 if (layout_manager_->IsHorizontalAlignment()) {
1311 preferred_size = gfx::Size(last_button_bounds.right() + leading_inset_,
1312 kShelfSize);
1313 } else {
1314 preferred_size = gfx::Size(kShelfSize,
1315 last_button_bounds.bottom() + leading_inset_);
1318 gfx::Point origin(GetMirroredXWithWidthInView(0, preferred_size.width()), 0);
1320 // In overflow mode, we should use OverflowBubbleView as a source for
1321 // converting |origin| to screen coordinates. When a scroll operation is
1322 // occurred in OverflowBubble, the bounds of ShelfView in OverflowBubble can
1323 // be changed.
1324 if (is_overflow_mode())
1325 ConvertPointToScreen(owner_overflow_bubble_->bubble_view(), &origin);
1326 else
1327 ConvertPointToScreen(this, &origin);
1329 return gfx::Rect(origin, preferred_size);
1332 int ShelfView::CancelDrag(int modified_index) {
1333 FinalizeRipOffDrag(true);
1334 if (!drag_view_)
1335 return modified_index;
1336 bool was_dragging = dragging();
1337 int drag_view_index = view_model_->GetIndexOfView(drag_view_);
1338 drag_pointer_ = NONE;
1339 drag_view_ = NULL;
1340 if (drag_view_index == modified_index) {
1341 // The view that was being dragged is being modified. Don't do anything.
1342 return modified_index;
1344 if (!was_dragging)
1345 return modified_index;
1347 // Restore previous position, tracking the position of the modified view.
1348 bool at_end = modified_index == view_model_->view_size();
1349 views::View* modified_view =
1350 (modified_index >= 0 && !at_end) ?
1351 view_model_->view_at(modified_index) : NULL;
1352 model_->Move(drag_view_index, start_drag_index_);
1354 // If the modified view will be at the end of the list, return the new end of
1355 // the list.
1356 if (at_end)
1357 return view_model_->view_size();
1358 return modified_view ? view_model_->GetIndexOfView(modified_view) : -1;
1361 gfx::Size ShelfView::GetPreferredSize() const {
1362 IdealBounds ideal_bounds;
1363 CalculateIdealBounds(&ideal_bounds);
1365 int last_button_index = is_overflow_mode() ?
1366 last_visible_index_ : view_model_->view_size() - 1;
1368 // When an item is dragged off from the overflow bubble, it is moved to last
1369 // position and and changed to invisible. Overflow bubble size should be
1370 // shrunk to fit only for visible items.
1371 // If |dragged_off_from_overflow_to_shelf_| is set, there will be no invisible
1372 // items in the shelf.
1373 if (is_overflow_mode() &&
1374 dragged_off_shelf_ &&
1375 !dragged_off_from_overflow_to_shelf_ &&
1376 RemovableByRipOff(view_model_->GetIndexOfView(drag_view_)) == REMOVABLE)
1377 last_button_index--;
1379 const gfx::Rect last_button_bounds =
1380 last_button_index >= first_visible_index_ ?
1381 view_model_->ideal_bounds(last_button_index) :
1382 gfx::Rect(gfx::Size(kShelfSize, kShelfSize));
1384 if (layout_manager_->IsHorizontalAlignment()) {
1385 return gfx::Size(last_button_bounds.right() + leading_inset_, kShelfSize);
1388 return gfx::Size(kShelfSize,
1389 last_button_bounds.bottom() + leading_inset_);
1392 void ShelfView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1393 // This bounds change is produced by the shelf movement and all content has
1394 // to follow. Using an animation at that time would produce a time lag since
1395 // the animation of the BoundsAnimator has itself a delay before it arrives
1396 // at the required location. As such we tell the animator to go there
1397 // immediately.
1398 BoundsAnimatorDisabler disabler(bounds_animator_.get());
1399 LayoutToIdealBounds();
1400 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1401 OnShelfIconPositionsChanged());
1403 if (IsShowingOverflowBubble())
1404 overflow_bubble_->Hide();
1407 views::FocusTraversable* ShelfView::GetPaneFocusTraversable() {
1408 return this;
1411 void ShelfView::GetAccessibleState(ui::AXViewState* state) {
1412 state->role = ui::AX_ROLE_TOOLBAR;
1413 state->name = l10n_util::GetStringUTF16(IDS_ASH_SHELF_ACCESSIBLE_NAME);
1416 void ShelfView::OnGestureEvent(ui::GestureEvent* event) {
1417 if (gesture_handler_.ProcessGestureEvent(*event))
1418 event->StopPropagation();
1421 void ShelfView::ShelfItemAdded(int model_index) {
1423 base::AutoReset<bool> cancelling_drag(
1424 &cancelling_drag_model_changed_, true);
1425 model_index = CancelDrag(model_index);
1427 views::View* view = CreateViewForItem(model_->items()[model_index]);
1428 AddChildView(view);
1429 // Hide the view, it'll be made visible when the animation is done. Using
1430 // opacity 0 here to avoid messing with CalculateIdealBounds which touches
1431 // the view's visibility.
1432 view->layer()->SetOpacity(0);
1433 view_model_->Add(view, model_index);
1435 // Give the button its ideal bounds. That way if we end up animating the
1436 // button before this animation completes it doesn't appear at some random
1437 // spot (because it was in the middle of animating from 0,0 0x0 to its
1438 // target).
1439 IdealBounds ideal_bounds;
1440 CalculateIdealBounds(&ideal_bounds);
1441 view->SetBoundsRect(view_model_->ideal_bounds(model_index));
1443 // The first animation moves all the views to their target position. |view|
1444 // is hidden, so it visually appears as though we are providing space for
1445 // it. When done we'll fade the view in.
1446 AnimateToIdealBounds();
1447 if (model_index <= last_visible_index_ ||
1448 model_index >= model_->FirstPanelIndex()) {
1449 bounds_animator_->SetAnimationDelegate(
1450 view,
1451 scoped_ptr<gfx::AnimationDelegate>(
1452 new StartFadeAnimationDelegate(this, view)));
1453 } else {
1454 // Undo the hiding if animation does not run.
1455 view->layer()->SetOpacity(1.0f);
1459 void ShelfView::ShelfItemRemoved(int model_index, ShelfID id) {
1460 if (id == context_menu_id_)
1461 launcher_menu_runner_.reset();
1463 base::AutoReset<bool> cancelling_drag(
1464 &cancelling_drag_model_changed_, true);
1465 model_index = CancelDrag(model_index);
1467 views::View* view = view_model_->view_at(model_index);
1468 view_model_->Remove(model_index);
1470 // When the overflow bubble is visible, the overflow range needs to be set
1471 // before CalculateIdealBounds() gets called. Otherwise CalculateIdealBounds()
1472 // could trigger a ShelfItemChanged() by hiding the overflow bubble and
1473 // since the overflow bubble is not yet synced with the ShelfModel this
1474 // could cause a crash.
1475 if (overflow_bubble_ && overflow_bubble_->IsShowing()) {
1476 last_hidden_index_ = std::min(last_hidden_index_,
1477 view_model_->view_size() - 1);
1478 UpdateOverflowRange(overflow_bubble_->shelf_view());
1481 if (view->visible()) {
1482 // The first animation fades out the view. When done we'll animate the rest
1483 // of the views to their target location.
1484 bounds_animator_->AnimateViewTo(view, view->bounds());
1485 bounds_animator_->SetAnimationDelegate(
1486 view,
1487 scoped_ptr<gfx::AnimationDelegate>(
1488 new FadeOutAnimationDelegate(this, view)));
1489 } else {
1490 // We don't need to show a fade out animation for invisible |view|. When an
1491 // item is ripped out from the shelf, its |view| is already invisible.
1492 AnimateToIdealBounds();
1495 // Close the tooltip because it isn't needed any longer and its anchor view
1496 // will be deleted soon.
1497 if (tooltip_->GetCurrentAnchorView() == view)
1498 tooltip_->Close();
1501 void ShelfView::ShelfItemChanged(int model_index, const ShelfItem& old_item) {
1502 const ShelfItem& item(model_->items()[model_index]);
1503 if (old_item.type != item.type) {
1504 // Type changed, swap the views.
1505 model_index = CancelDrag(model_index);
1506 scoped_ptr<views::View> old_view(view_model_->view_at(model_index));
1507 bounds_animator_->StopAnimatingView(old_view.get());
1508 // Removing and re-inserting a view in our view model will strip the ideal
1509 // bounds from the item. To avoid recalculation of everything the bounds
1510 // get remembered and restored after the insertion to the previous value.
1511 gfx::Rect old_ideal_bounds = view_model_->ideal_bounds(model_index);
1512 view_model_->Remove(model_index);
1513 views::View* new_view = CreateViewForItem(item);
1514 AddChildView(new_view);
1515 view_model_->Add(new_view, model_index);
1516 view_model_->set_ideal_bounds(model_index, old_ideal_bounds);
1517 new_view->SetBoundsRect(old_view->bounds());
1518 return;
1521 views::View* view = view_model_->view_at(model_index);
1522 switch (item.type) {
1523 case TYPE_BROWSER_SHORTCUT:
1524 // Fallthrough for the new Shelf since it needs to show the activation
1525 // change as well.
1526 case TYPE_APP_SHORTCUT:
1527 case TYPE_WINDOWED_APP:
1528 case TYPE_PLATFORM_APP:
1529 case TYPE_DIALOG:
1530 case TYPE_APP_PANEL: {
1531 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1532 ShelfButton* button = static_cast<ShelfButton*>(view);
1533 ReflectItemStatus(item, button);
1534 // The browser shortcut is currently not a "real" item and as such the
1535 // the image is bogous as well. We therefore keep the image as is for it.
1536 if (item.type != TYPE_BROWSER_SHORTCUT)
1537 button->SetImage(item.image);
1538 button->SchedulePaint();
1539 break;
1542 default:
1543 break;
1547 void ShelfView::ShelfItemMoved(int start_index, int target_index) {
1548 view_model_->Move(start_index, target_index);
1549 // When cancelling a drag due to a shelf item being added, the currently
1550 // dragged item is moved back to its initial position. AnimateToIdealBounds
1551 // will be called again when the new item is added to the |view_model_| but
1552 // at this time the |view_model_| is inconsistent with the |model_|.
1553 if (!cancelling_drag_model_changed_)
1554 AnimateToIdealBounds();
1557 void ShelfView::ShelfStatusChanged() {
1558 // Nothing to do here.
1561 void ShelfView::PointerPressedOnButton(views::View* view,
1562 Pointer pointer,
1563 const ui::LocatedEvent& event) {
1564 if (drag_view_)
1565 return;
1567 int index = view_model_->GetIndexOfView(view);
1568 if (index == -1)
1569 return;
1571 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1572 model_->items()[index].id);
1573 if (view_model_->view_size() <= 1 || !item_delegate->IsDraggable())
1574 return; // View is being deleted or not draggable, ignore request.
1576 // Only when the repost event occurs on the same shelf item, we should ignore
1577 // the call in ShelfView::ButtonPressed(...).
1578 is_repost_event_ = IsRepostEvent(event) && (last_pressed_index_ == index);
1580 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1581 drag_view_ = static_cast<ShelfButton*>(view);
1582 drag_origin_ = gfx::Point(event.x(), event.y());
1583 UMA_HISTOGRAM_ENUMERATION("Ash.ShelfAlignmentUsage",
1584 layout_manager_->SelectValueForShelfAlignment(
1585 SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM,
1586 SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT,
1587 SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT,
1588 -1),
1589 SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT);
1592 void ShelfView::PointerDraggedOnButton(views::View* view,
1593 Pointer pointer,
1594 const ui::LocatedEvent& event) {
1595 // To prepare all drag types (moving an item in the shelf and dragging off),
1596 // we should check the x-axis and y-axis offset.
1597 if (!dragging() && drag_view_ &&
1598 ((std::abs(event.x() - drag_origin_.x()) >= kMinimumDragDistance) ||
1599 (std::abs(event.y() - drag_origin_.y()) >= kMinimumDragDistance))) {
1600 PrepareForDrag(pointer, event);
1602 if (drag_pointer_ == pointer)
1603 ContinueDrag(event);
1606 void ShelfView::PointerReleasedOnButton(views::View* view,
1607 Pointer pointer,
1608 bool canceled) {
1609 // Reset |is_repost_event| to false.
1610 is_repost_event_ = false;
1612 if (canceled) {
1613 CancelDrag(-1);
1614 } else if (drag_pointer_ == pointer) {
1615 FinalizeRipOffDrag(false);
1616 drag_pointer_ = NONE;
1617 AnimateToIdealBounds();
1619 // If the drag pointer is NONE, no drag operation is going on and the
1620 // drag_view can be released.
1621 if (drag_pointer_ == NONE)
1622 drag_view_ = NULL;
1625 void ShelfView::MouseMovedOverButton(views::View* view) {
1626 if (!ShouldShowTooltipForView(view))
1627 return;
1629 if (!tooltip_->IsVisible())
1630 tooltip_->ResetTimer();
1633 void ShelfView::MouseEnteredButton(views::View* view) {
1634 if (!ShouldShowTooltipForView(view))
1635 return;
1637 if (tooltip_->IsVisible()) {
1638 tooltip_->ShowImmediately(view, GetAccessibleName(view));
1639 } else {
1640 tooltip_->ShowDelayed(view, GetAccessibleName(view));
1644 void ShelfView::MouseExitedButton(views::View* view) {
1645 if (!tooltip_->IsVisible())
1646 tooltip_->StopTimer();
1649 base::string16 ShelfView::GetAccessibleName(const views::View* view) {
1650 int view_index = view_model_->GetIndexOfView(view);
1651 // May be -1 while in the process of animating closed.
1652 if (view_index == -1)
1653 return base::string16();
1655 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1656 model_->items()[view_index].id);
1657 return item_delegate->GetTitle();
1660 void ShelfView::ButtonPressed(views::Button* sender, const ui::Event& event) {
1661 // Do not handle mouse release during drag.
1662 if (dragging())
1663 return;
1665 if (sender == overflow_button_) {
1666 ToggleOverflowBubble();
1667 return;
1670 int view_index = view_model_->GetIndexOfView(sender);
1671 // May be -1 while in the process of animating closed.
1672 if (view_index == -1)
1673 return;
1675 // If the menu was just closed by the same event as this one, we ignore
1676 // the call and don't open the menu again. See crbug.com/343005 for more
1677 // detail.
1678 if (is_repost_event_)
1679 return;
1681 // Record the index for the last pressed shelf item.
1682 last_pressed_index_ = view_index;
1684 // Don't activate the item twice on double-click. Otherwise the window starts
1685 // animating open due to the first click, then immediately minimizes due to
1686 // the second click. The user most likely intended to open or minimize the
1687 // item once, not do both.
1688 if (event.flags() & ui::EF_IS_DOUBLE_CLICK)
1689 return;
1692 ScopedTargetRootWindow scoped_target(
1693 sender->GetWidget()->GetNativeView()->GetRootWindow());
1694 // Slow down activation animations if shift key is pressed.
1695 scoped_ptr<ui::ScopedAnimationDurationScaleMode> slowing_animations;
1696 if (event.IsShiftDown()) {
1697 slowing_animations.reset(new ui::ScopedAnimationDurationScaleMode(
1698 ui::ScopedAnimationDurationScaleMode::SLOW_DURATION));
1701 // Collect usage statistics before we decide what to do with the click.
1702 switch (model_->items()[view_index].type) {
1703 case TYPE_APP_SHORTCUT:
1704 case TYPE_WINDOWED_APP:
1705 case TYPE_PLATFORM_APP:
1706 case TYPE_BROWSER_SHORTCUT:
1707 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1708 UMA_LAUNCHER_CLICK_ON_APP);
1709 break;
1711 case TYPE_APP_LIST:
1712 Shell::GetInstance()->metrics()->RecordUserMetricsAction(
1713 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON);
1714 break;
1716 case TYPE_APP_PANEL:
1717 case TYPE_DIALOG:
1718 break;
1720 case TYPE_UNDEFINED:
1721 NOTREACHED() << "ShelfItemType must be set.";
1722 break;
1725 ShelfItemDelegate* item_delegate =
1726 item_manager_->GetShelfItemDelegate(model_->items()[view_index].id);
1727 if (!item_delegate->ItemSelected(event))
1728 ShowListMenuForView(model_->items()[view_index], sender, event);
1732 bool ShelfView::ShowListMenuForView(const ShelfItem& item,
1733 views::View* source,
1734 const ui::Event& event) {
1735 ShelfItemDelegate* item_delegate =
1736 item_manager_->GetShelfItemDelegate(item.id);
1737 scoped_ptr<ui::MenuModel> list_menu_model(
1738 item_delegate->CreateApplicationMenu(event.flags()));
1740 // Make sure we have a menu and it has at least two items in addition to the
1741 // application title and the 3 spacing separators.
1742 if (!list_menu_model.get() || list_menu_model->GetItemCount() <= 5)
1743 return false;
1745 ShowMenu(list_menu_model.get(),
1746 source,
1747 gfx::Point(),
1748 false,
1749 ui::GetMenuSourceTypeForEvent(event));
1750 return true;
1753 void ShelfView::ShowContextMenuForView(views::View* source,
1754 const gfx::Point& point,
1755 ui::MenuSourceType source_type) {
1756 int view_index = view_model_->GetIndexOfView(source);
1757 if (view_index == -1) {
1758 Shell::GetInstance()->ShowContextMenu(point, source_type);
1759 return;
1762 ShelfItemDelegate* item_delegate = item_manager_->GetShelfItemDelegate(
1763 model_->items()[view_index].id);
1764 context_menu_model_.reset(item_delegate->CreateContextMenu(
1765 source->GetWidget()->GetNativeView()->GetRootWindow()));
1766 if (!context_menu_model_)
1767 return;
1769 base::AutoReset<ShelfID> reseter(
1770 &context_menu_id_,
1771 view_index == -1 ? 0 : model_->items()[view_index].id);
1773 ShowMenu(context_menu_model_.get(),
1774 source,
1775 point,
1776 true,
1777 source_type);
1780 void ShelfView::ShowMenu(ui::MenuModel* menu_model,
1781 views::View* source,
1782 const gfx::Point& click_point,
1783 bool context_menu,
1784 ui::MenuSourceType source_type) {
1785 closing_event_time_ = base::TimeDelta();
1786 launcher_menu_runner_.reset(new views::MenuRunner(
1787 menu_model, context_menu ? views::MenuRunner::CONTEXT_MENU : 0));
1789 ScopedTargetRootWindow scoped_target(
1790 source->GetWidget()->GetNativeView()->GetRootWindow());
1792 // Determine the menu alignment dependent on the shelf.
1793 views::MenuAnchorPosition menu_alignment = views::MENU_ANCHOR_TOPLEFT;
1794 gfx::Rect anchor_point = gfx::Rect(click_point, gfx::Size());
1796 ShelfWidget* shelf = RootWindowController::ForShelf(
1797 GetWidget()->GetNativeView())->shelf();
1798 if (!context_menu) {
1799 // Application lists use a bubble.
1800 ShelfAlignment align = shelf->GetAlignment();
1801 anchor_point = source->GetBoundsInScreen();
1803 // It is possible to invoke the menu while it is sliding into view. To cover
1804 // that case, the screen coordinates are offsetted by the animation delta.
1805 gfx::Vector2d offset =
1806 source->GetWidget()->GetNativeWindow()->bounds().origin() -
1807 source->GetWidget()->GetNativeWindow()->GetTargetBounds().origin();
1808 anchor_point.set_x(anchor_point.x() - offset.x());
1809 anchor_point.set_y(anchor_point.y() - offset.y());
1811 // Shelf items can have an asymmetrical border for spacing reasons.
1812 // Adjust anchor location for this.
1813 if (source->border())
1814 anchor_point.Inset(source->border()->GetInsets());
1816 switch (align) {
1817 case SHELF_ALIGNMENT_BOTTOM:
1818 menu_alignment = views::MENU_ANCHOR_BUBBLE_ABOVE;
1819 break;
1820 case SHELF_ALIGNMENT_LEFT:
1821 menu_alignment = views::MENU_ANCHOR_BUBBLE_RIGHT;
1822 break;
1823 case SHELF_ALIGNMENT_RIGHT:
1824 menu_alignment = views::MENU_ANCHOR_BUBBLE_LEFT;
1825 break;
1826 case SHELF_ALIGNMENT_TOP:
1827 menu_alignment = views::MENU_ANCHOR_BUBBLE_BELOW;
1828 break;
1831 // If this gets deleted while we are in the menu, the shelf will be gone
1832 // as well.
1833 bool got_deleted = false;
1834 got_deleted_ = &got_deleted;
1836 shelf->ForceUndimming(true);
1837 // NOTE: if you convert to HAS_MNEMONICS be sure and update menu building
1838 // code.
1839 if (launcher_menu_runner_->RunMenuAt(source->GetWidget(),
1840 NULL,
1841 anchor_point,
1842 menu_alignment,
1843 source_type) ==
1844 views::MenuRunner::MENU_DELETED) {
1845 if (!got_deleted) {
1846 got_deleted_ = NULL;
1847 shelf->ForceUndimming(false);
1849 return;
1851 got_deleted_ = NULL;
1852 shelf->ForceUndimming(false);
1854 // If it is a context menu and we are showing overflow bubble
1855 // we want to hide overflow bubble.
1856 if (owner_overflow_bubble_)
1857 owner_overflow_bubble_->HideBubbleAndRefreshButton();
1859 // Unpinning an item will reset the |launcher_menu_runner_| before coming
1860 // here.
1861 if (launcher_menu_runner_)
1862 closing_event_time_ = launcher_menu_runner_->closing_event_time();
1863 Shell::GetInstance()->UpdateShelfVisibility();
1866 void ShelfView::OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) {
1867 FOR_EACH_OBSERVER(ShelfIconObserver, observers_,
1868 OnShelfIconPositionsChanged());
1869 PreferredSizeChanged();
1872 void ShelfView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
1873 if (snap_back_from_rip_off_view_ && animator == bounds_animator_) {
1874 if (!animator->IsAnimating(snap_back_from_rip_off_view_)) {
1875 // Coming here the animation of the ShelfButton is finished and the
1876 // previously hidden status can be shown again. Since the button itself
1877 // might have gone away or changed locations we check that the button
1878 // is still in the shelf and show its status again.
1879 for (int index = 0; index < view_model_->view_size(); index++) {
1880 views::View* view = view_model_->view_at(index);
1881 if (view == snap_back_from_rip_off_view_) {
1882 CHECK_EQ(ShelfButton::kViewClassName, view->GetClassName());
1883 ShelfButton* button = static_cast<ShelfButton*>(view);
1884 button->ClearState(ShelfButton::STATE_HIDDEN);
1885 break;
1888 snap_back_from_rip_off_view_ = NULL;
1893 bool ShelfView::IsRepostEvent(const ui::Event& event) {
1894 if (closing_event_time_ == base::TimeDelta())
1895 return false;
1897 base::TimeDelta delta =
1898 base::TimeDelta(event.time_stamp() - closing_event_time_);
1899 closing_event_time_ = base::TimeDelta();
1900 // If the current (press down) event is a repost event, the time stamp of
1901 // these two events should be the same.
1902 return (delta.InMilliseconds() == 0);
1905 const ShelfItem* ShelfView::ShelfItemForView(const views::View* view) const {
1906 int view_index = view_model_->GetIndexOfView(view);
1907 if (view_index == -1)
1908 return NULL;
1909 return &(model_->items()[view_index]);
1912 bool ShelfView::ShouldShowTooltipForView(const views::View* view) const {
1913 if (view == GetAppListButtonView() &&
1914 Shell::GetInstance()->GetAppListWindow())
1915 return false;
1916 const ShelfItem* item = ShelfItemForView(view);
1917 if (!item)
1918 return true;
1919 ShelfItemDelegate* item_delegate =
1920 item_manager_->GetShelfItemDelegate(item->id);
1921 return item_delegate->ShouldShowTooltip();
1924 int ShelfView::CalculateShelfDistance(const gfx::Point& coordinate) const {
1925 ShelfWidget* shelf = RootWindowController::ForShelf(
1926 GetWidget()->GetNativeView())->shelf();
1927 ShelfAlignment align = shelf->GetAlignment();
1928 const gfx::Rect bounds = GetBoundsInScreen();
1929 int distance = 0;
1930 switch (align) {
1931 case SHELF_ALIGNMENT_BOTTOM:
1932 distance = bounds.y() - coordinate.y();
1933 break;
1934 case SHELF_ALIGNMENT_LEFT:
1935 distance = coordinate.x() - bounds.right();
1936 break;
1937 case SHELF_ALIGNMENT_RIGHT:
1938 distance = bounds.x() - coordinate.x();
1939 break;
1940 case SHELF_ALIGNMENT_TOP:
1941 distance = coordinate.y() - bounds.bottom();
1942 break;
1944 return distance > 0 ? distance : 0;
1947 } // namespace ash