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 #ifndef ASH_SHELF_SHELF_VIEW_H_
6 #define ASH_SHELF_SHELF_VIEW_H_
11 #include "ash/shelf/shelf_button_host.h"
12 #include "ash/shelf/shelf_model_observer.h"
13 #include "ash/wm/gestures/shelf_gesture_handler.h"
14 #include "base/observer_list.h"
15 #include "ui/app_list/views/app_list_drag_and_drop_host.h"
16 #include "ui/views/animation/bounds_animator_observer.h"
17 #include "ui/views/context_menu_controller.h"
18 #include "ui/views/controls/button/button.h"
19 #include "ui/views/focus/focus_manager.h"
20 #include "ui/views/view.h"
21 #include "ui/views/view_model.h"
34 class ShelfIconObserver
;
35 class ShelfItemDelegateManager
;
42 class ShelfLayoutManager
;
43 class ShelfTooltipManager
;
46 class ShelfViewTestAPI
;
49 extern const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM
;
50 extern const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT
;
51 extern const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT
;
52 extern const int SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT
;
54 class ASH_EXPORT ShelfView
: public views::View
,
55 public ShelfModelObserver
,
56 public views::ButtonListener
,
57 public ShelfButtonHost
,
58 public views::ContextMenuController
,
59 public views::FocusTraversable
,
60 public views::BoundsAnimatorObserver
,
61 public app_list::ApplicationDragAndDropHost
{
63 ShelfView(ShelfModel
* model
,
64 ShelfDelegate
* delegate
,
65 ShelfLayoutManager
* manager
);
66 ~ShelfView() override
;
68 ShelfTooltipManager
* tooltip_manager() { return tooltip_
.get(); }
70 ShelfLayoutManager
* shelf_layout_manager() { return layout_manager_
; }
72 ShelfModel
* model() { return model_
; }
76 void OnShelfAlignmentChanged();
77 void SchedulePaintForAllButtons();
79 // Returns the ideal bounds of the specified item, or an empty rect if id
80 // isn't know. If the item is in an overflow shelf, the overflow icon location
82 gfx::Rect
GetIdealBoundsOfItemIcon(ShelfID id
);
84 // Repositions the icon for the specified item by the midpoint of the window.
85 void UpdatePanelIconPosition(ShelfID id
, const gfx::Point
& midpoint
);
87 void AddIconObserver(ShelfIconObserver
* observer
);
88 void RemoveIconObserver(ShelfIconObserver
* observer
);
90 // Returns true if we're showing a menu.
91 bool IsShowingMenu() const;
93 // Returns true if overflow bubble is shown.
94 bool IsShowingOverflowBubble() const;
96 // Sets owner overflow bubble instance from which this shelf view pops
98 void set_owner_overflow_bubble(OverflowBubble
* owner
) {
99 owner_overflow_bubble_
= owner
;
102 views::View
* GetAppListButtonView() const;
104 // Returns true if the mouse cursor exits the area for launcher tooltip.
105 // There are thin gaps between launcher buttons but the tooltip shouldn't hide
106 // in the gaps, but the tooltip should hide if the mouse moved totally outside
107 // of the buttons area.
108 bool ShouldHideTooltip(const gfx::Point
& cursor_location
);
110 // Returns rectangle bounding all visible launcher items. Used screen
111 // coordinate system.
112 gfx::Rect
GetVisibleItemsBoundsInScreen();
114 // Overridden from FocusTraversable:
115 views::FocusSearch
* GetFocusSearch() override
;
116 FocusTraversable
* GetFocusTraversableParent() override
;
117 View
* GetFocusTraversableParentView() override
;
119 // Overridden from app_list::ApplicationDragAndDropHost:
120 void CreateDragIconProxy(const gfx::Point
& location_in_screen_coordinates
,
121 const gfx::ImageSkia
& icon
,
122 views::View
* replaced_view
,
123 const gfx::Vector2d
& cursor_offset_from_center
,
124 float scale_factor
) override
;
125 void UpdateDragIconProxy(
126 const gfx::Point
& location_in_screen_coordinates
) override
;
127 void DestroyDragIconProxy() override
;
128 bool StartDrag(const std::string
& app_id
,
129 const gfx::Point
& location_in_screen_coordinates
) override
;
130 bool Drag(const gfx::Point
& location_in_screen_coordinates
) override
;
131 void EndDrag(bool cancel
) override
;
133 // Return the view model for test purposes.
134 const views::ViewModel
* view_model_for_test() const {
135 return view_model_
.get();
139 friend class ash::test::ShelfViewTestAPI
;
141 class FadeOutAnimationDelegate
;
142 class StartFadeAnimationDelegate
;
145 gfx::Rect overflow_bounds
;
148 enum RemovableState
{
149 REMOVABLE
, // Item can be removed when dragged away.
150 DRAGGABLE
, // Item can be dragged, but will snap always back to origin.
151 NOT_REMOVABLE
, // Item is fixed and can never be removed.
154 // Returns true when this ShelfView is used for Overflow Bubble.
155 // In this mode, it does not show app list, panel and overflow button.
157 // * When Shelf can contain only one item (overflow button) due to very
158 // small resolution screen, overflow bubble can show app list and panel
160 bool is_overflow_mode() const { return overflow_mode_
; }
162 bool dragging() const {
163 return drag_pointer_
!= NONE
;
166 // Sets the bounds of each view to its ideal bounds.
167 void LayoutToIdealBounds();
169 // Update all button's visibility in overflow.
170 void UpdateAllButtonsVisibilityInOverflowMode();
172 // Calculates the ideal bounds. The bounds of each button corresponding to an
173 // item in the model is set in |view_model_|.
174 void CalculateIdealBounds(IdealBounds
* bounds
) const;
176 // Returns the index of the last view whose max primary axis coordinate is
177 // less than |max_value|. Returns -1 if nothing fits, or there are no views.
178 int DetermineLastVisibleIndex(int max_value
) const;
180 // Returns the index of the first panel whose min primary axis coordinate is
181 // at least |min_value|. Returns the index past the last panel if none fit.
182 int DetermineFirstVisiblePanelIndex(int min_value
) const;
184 // Animates the bounds of each view to its ideal bounds.
185 void AnimateToIdealBounds();
187 // Creates the view used to represent |item|.
188 views::View
* CreateViewForItem(const ShelfItem
& item
);
190 // Fades |view| from an opacity of 0 to 1. This is when adding a new item.
191 void FadeIn(views::View
* view
);
193 // Invoked when the pointer has moved enough to trigger a drag. Sets
194 // internal state in preparation for the drag.
195 void PrepareForDrag(Pointer pointer
, const ui::LocatedEvent
& event
);
197 // Invoked when the mouse is dragged. Updates the models as appropriate.
198 void ContinueDrag(const ui::LocatedEvent
& event
);
200 // Handles ripping off an item from the shelf. Returns true when the item got
202 bool HandleRipOffDrag(const ui::LocatedEvent
& event
);
204 // Finalize the rip off dragging by either |cancel| the action or validating.
205 void FinalizeRipOffDrag(bool cancel
);
207 // Check if an item can be ripped off or not.
208 RemovableState
RemovableByRipOff(int index
) const;
210 // Returns true if |typea| and |typeb| should be in the same drag range.
211 bool SameDragType(ShelfItemType typea
, ShelfItemType typeb
) const;
213 // Returns the range (in the model) the item at the specified index can be
215 std::pair
<int, int> GetDragRange(int index
);
217 // If there is a drag operation in progress it's canceled. If |modified_index|
218 // is valid, the new position of the corresponding item is returned.
219 int CancelDrag(int modified_index
);
221 // Returns rectangle bounds used for drag insertion.
223 // * When overflow button is visible, returns bounds from first item
224 // to overflow button.
225 // * When overflow button is visible and one or more panel items exists,
226 // returns bounds from first item to last panel item.
227 // * In the overflow mode, returns only bubble's bounds.
228 gfx::Rect
GetBoundsForDragInsertInScreen();
230 // Common setup done for all children.
231 void ConfigureChildView(views::View
* view
);
233 // Toggles the overflow menu.
234 void ToggleOverflowBubble();
236 // Invoked after the fading out animation for item deletion is ended.
237 void OnFadeOutAnimationEnded();
239 // Fade in last visible item.
240 void StartFadeInLastVisibleItem();
242 // Updates the visible range of overflow items in |overflow_view|.
243 void UpdateOverflowRange(ShelfView
* overflow_view
) const;
245 // Overridden from views::View:
246 gfx::Size
GetPreferredSize() const override
;
247 void OnBoundsChanged(const gfx::Rect
& previous_bounds
) override
;
248 FocusTraversable
* GetPaneFocusTraversable() override
;
249 void GetAccessibleState(ui::AXViewState
* state
) override
;
251 // Overridden from ui::EventHandler:
252 void OnGestureEvent(ui::GestureEvent
* event
) override
;
254 // Overridden from ShelfModelObserver:
255 void ShelfItemAdded(int model_index
) override
;
256 void ShelfItemRemoved(int model_index
, ShelfID id
) override
;
257 void ShelfItemChanged(int model_index
, const ShelfItem
& old_item
) override
;
258 void ShelfItemMoved(int start_index
, int target_index
) override
;
259 void ShelfStatusChanged() override
;
261 // Overridden from ShelfButtonHost:
262 void PointerPressedOnButton(views::View
* view
,
264 const ui::LocatedEvent
& event
) override
;
265 void PointerDraggedOnButton(views::View
* view
,
267 const ui::LocatedEvent
& event
) override
;
268 void PointerReleasedOnButton(views::View
* view
,
270 bool canceled
) override
;
271 void MouseMovedOverButton(views::View
* view
) override
;
272 void MouseEnteredButton(views::View
* view
) override
;
273 void MouseExitedButton(views::View
* view
) override
;
274 base::string16
GetAccessibleName(const views::View
* view
) override
;
276 // Overridden from views::ButtonListener:
277 void ButtonPressed(views::Button
* sender
, const ui::Event
& event
) override
;
279 // Show the list of all running items for this |item|. It will return true
280 // when the menu was shown and false if there were no possible items to
281 // choose from. |source| specifies the view which is responsible for showing
282 // the menu, and the bubble will point towards it.
283 // The |event_flags| are the flags of the event which triggered this menu.
284 bool ShowListMenuForView(const ShelfItem
& item
,
286 const ui::Event
& event
);
288 // Overridden from views::ContextMenuController:
289 void ShowContextMenuForView(views::View
* source
,
290 const gfx::Point
& point
,
291 ui::MenuSourceType source_type
) override
;
293 // Show either a context or normal click menu of given |menu_model|.
294 // If |context_menu| is set, the displayed menu is a context menu and not
295 // a menu listing one or more running applications.
296 // The |click_point| is only used for |context_menu|'s.
297 void ShowMenu(ui::MenuModel
* menu_model
,
299 const gfx::Point
& click_point
,
301 ui::MenuSourceType source_type
);
303 // Overridden from views::BoundsAnimatorObserver:
304 void OnBoundsAnimatorProgressed(views::BoundsAnimator
* animator
) override
;
305 void OnBoundsAnimatorDone(views::BoundsAnimator
* animator
) override
;
307 // Returns false if the click which closed the previous menu is the click
308 // which triggered this event.
309 bool IsUsableEvent(const ui::Event
& event
);
311 // Convenience accessor to model_->items().
312 const ShelfItem
* ShelfItemForView(const views::View
* view
) const;
314 // Returns true if a tooltip should be shown for |view|.
315 bool ShouldShowTooltipForView(const views::View
* view
) const;
317 // Get the distance from the given |coordinate| to the closest point on this
319 int CalculateShelfDistance(const gfx::Point
& coordinate
) const;
321 // The model; owned by Launcher.
324 // Delegate; owned by Launcher.
325 ShelfDelegate
* delegate_
;
327 // Used to manage the set of active launcher buttons. There is a view per
329 scoped_ptr
<views::ViewModel
> view_model_
;
331 // Index of first visible launcher item.
332 int first_visible_index_
;
334 // Last index of a launcher button that is visible
335 // (does not go into overflow).
336 mutable int last_visible_index_
;
338 scoped_ptr
<views::BoundsAnimator
> bounds_animator_
;
340 OverflowButton
* overflow_button_
;
342 scoped_ptr
<OverflowBubble
> overflow_bubble_
;
344 OverflowBubble
* owner_overflow_bubble_
;
346 scoped_ptr
<ShelfTooltipManager
> tooltip_
;
348 // Pointer device that initiated the current drag operation. If there is no
349 // current dragging operation, this is NONE.
350 Pointer drag_pointer_
;
352 // The view being dragged. This is set immediately when the mouse is pressed.
353 // |dragging_| is set only if the mouse is dragged far enough.
354 ShelfButton
* drag_view_
;
356 // Position of the mouse down event in |drag_view_|'s coordinates.
357 gfx::Point drag_origin_
;
359 // Index |drag_view_| was initially at.
360 int start_drag_index_
;
362 // Used for the context menu of a particular item.
363 ShelfID context_menu_id_
;
365 scoped_ptr
<views::FocusSearch
> focus_search_
;
367 scoped_ptr
<ui::MenuModel
> context_menu_model_
;
369 scoped_ptr
<views::MenuRunner
> launcher_menu_runner_
;
371 ObserverList
<ShelfIconObserver
> observers_
;
373 // Amount content is inset on the left edge (or top edge for vertical
377 ShelfGestureHandler gesture_handler_
;
379 // True when an item being inserted or removed in the model cancels a drag.
380 bool cancelling_drag_model_changed_
;
382 // Index of the last hidden launcher item. If there are no hidden items this
383 // will be equal to last_visible_index_ + 1.
384 mutable int last_hidden_index_
;
386 // The timestamp of the event which closed the last menu - or 0.
387 base::TimeDelta closing_event_time_
;
389 // When this object gets deleted while a menu is shown, this pointed
390 // element will be set to false.
393 // True if a drag and drop operation created/pinned the item in the launcher
394 // and it needs to be deleted/unpinned again if the operation gets cancelled.
395 bool drag_and_drop_item_pinned_
;
397 // The ShelfItem which is currently used for a drag and a drop operation
399 ShelfID drag_and_drop_shelf_id_
;
401 // The application ID of the application which we drag and drop.
402 std::string drag_and_drop_app_id_
;
404 // The original launcher item's size before the dragging operation.
405 gfx::Size pre_drag_and_drop_size_
;
407 // The image proxy for drag operations when a drag and drop host exists and
408 // the item can be dragged outside the app grid.
409 scoped_ptr
<ash::DragImageView
> drag_image_
;
411 // The cursor offset to the middle of the dragged item.
412 gfx::Vector2d drag_image_offset_
;
414 // The view which gets replaced by our drag icon proxy.
415 views::View
* drag_replaced_view_
;
417 // True when the icon was dragged off the shelf.
418 bool dragged_off_shelf_
;
420 // The rip off view when a snap back operation is underway.
421 views::View
* snap_back_from_rip_off_view_
;
423 // Holds ShelfItemDelegateManager.
424 ShelfItemDelegateManager
* item_manager_
;
426 // Holds ShelfLayoutManager.
427 ShelfLayoutManager
* layout_manager_
;
429 // True when this ShelfView is used for Overflow Bubble.
432 // Holds a pointer to main ShelfView when a ShelfView is in overflow mode.
433 ShelfView
* main_shelf_
;
435 // True when ripped item from overflow bubble is entered into Shelf.
436 bool dragged_off_from_overflow_to_shelf_
;
438 DISALLOW_COPY_AND_ASSIGN(ShelfView
);
443 #endif // ASH_SHELF_SHELF_VIEW_H_