shelf_view.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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.
  4. #ifndef ASH_SHELF_SHELF_VIEW_H_
  5. #define ASH_SHELF_SHELF_VIEW_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "ash/app_list/views/app_list_drag_and_drop_host.h"
  11. #include "ash/ash_export.h"
  12. #include "ash/public/cpp/app_list/app_list_types.h"
  13. #include "ash/public/cpp/shelf_config.h"
  14. #include "ash/public/cpp/shelf_item_delegate.h"
  15. #include "ash/public/cpp/shelf_model_observer.h"
  16. #include "ash/public/cpp/tablet_mode_observer.h"
  17. #include "ash/shelf/shelf.h"
  18. #include "ash/shelf/shelf_button_delegate.h"
  19. #include "ash/shelf/shelf_button_pressed_metric_tracker.h"
  20. #include "ash/shelf/shelf_observer.h"
  21. #include "ash/shelf/shelf_tooltip_delegate.h"
  22. #include "ash/shell_observer.h"
  23. #include "base/cancelable_callback.h"
  24. #include "base/memory/weak_ptr.h"
  25. #include "base/time/time.h"
  26. #include "base/timer/timer.h"
  27. #include "third_party/abseil-cpp/absl/types/optional.h"
  28. #include "third_party/skia/include/core/SkColor.h"
  29. #include "ui/base/metadata/metadata_header_macros.h"
  30. #include "ui/compositor/throughput_tracker.h"
  31. #include "ui/views/accessibility/view_accessibility.h"
  32. #include "ui/views/accessible_pane_view.h"
  33. #include "ui/views/animation/bounds_animator_observer.h"
  34. #include "ui/views/animation/ink_drop_state.h"
  35. #include "ui/views/context_menu_controller.h"
  36. #include "ui/views/controls/button/button.h"
  37. #include "ui/views/controls/menu/menu_types.h"
  38. #include "ui/views/focus/focus_manager.h"
  39. #include "ui/views/view_model.h"
  40. #include "ui/views/widget/unique_widget_ptr.h"
  41. namespace ui {
  42. class SimpleMenuModel;
  43. }
  44. namespace display {
  45. class ScopedDisplayForNewWindows;
  46. }
  47. namespace views {
  48. class BoundsAnimator;
  49. class MenuRunner;
  50. class Separator;
  51. } // namespace views
  52. namespace ash {
  53. class GhostImageView;
  54. class ShelfAppButton;
  55. class ShelfButton;
  56. class ShelfModel;
  57. struct ShelfItem;
  58. class ShelfMenuModelAdapter;
  59. class ShelfWidget;
  60. enum ShelfAlignmentUmaEnumValue {
  61. SHELF_ALIGNMENT_UMA_ENUM_VALUE_BOTTOM,
  62. SHELF_ALIGNMENT_UMA_ENUM_VALUE_LEFT,
  63. SHELF_ALIGNMENT_UMA_ENUM_VALUE_RIGHT,
  64. // Must be last entry in enum.
  65. SHELF_ALIGNMENT_UMA_ENUM_VALUE_COUNT,
  66. };
  67. // ShelfView contains the shelf items visible within an active user session.
  68. // ShelfView and LoginShelfView should never be shown together.
  69. class ASH_EXPORT ShelfView : public views::AccessiblePaneView,
  70. public ShelfButtonDelegate,
  71. public ShelfModelObserver,
  72. public ShellObserver,
  73. public ShelfObserver,
  74. public views::ContextMenuController,
  75. public views::BoundsAnimatorObserver,
  76. public ApplicationDragAndDropHost,
  77. public ShelfTooltipDelegate,
  78. public TabletModeObserver {
  79. public:
  80. METADATA_HEADER(ShelfView);
  81. // Used to communicate with the container class ScrollableShelfView.
  82. class Delegate {
  83. public:
  84. virtual ~Delegate() = default;
  85. // Called on each shelf item drag update to update the parent view scroll
  86. // offset if needed.
  87. virtual void ScheduleScrollForItemDragIfNeeded(
  88. const gfx::Rect& item_bounds_in_screen) = 0;
  89. // Called when a shelf item drag ends to cancel any parent view scroll
  90. // offset updates previously scheduled by
  91. // `ScheduleDcrollForItemDragIfNeeded()`.
  92. virtual void CancelScrollForItemDrag() = 0;
  93. // Returns whether the provided `bounds_in_screen` are within visible
  94. // portion of the shelf.
  95. virtual bool AreBoundsWithinVisibleSpace(
  96. const gfx::Rect& bounds_in_screen) const = 0;
  97. };
  98. ShelfView(ShelfModel* model,
  99. Shelf* shelf,
  100. Delegate* delegate,
  101. ShelfButtonDelegate* button_delegate);
  102. ShelfView(const ShelfView&) = delete;
  103. ShelfView& operator=(const ShelfView&) = delete;
  104. ~ShelfView() override;
  105. Shelf* shelf() const { return shelf_; }
  106. ShelfModel* model() const { return model_; }
  107. // Returns the size occupied by |count| app buttons. |button_size| indicates
  108. // the size of each app button.
  109. static int GetSizeOfAppButtons(int count, int button_size);
  110. // Initializes shelf view elements.
  111. void Init(views::FocusSearch* focus_search);
  112. // Returns true if we're showing a menu. Note the menu could be either the
  113. // context menu or the application select menu.
  114. bool IsShowingMenu() const;
  115. // Returns true if we're showing a menu for |view|. |view| could be a
  116. // ShelfAppButton or the ShelfView.
  117. bool IsShowingMenuForView(const views::View* view) const;
  118. // Updates the union of all the shelf item bounds shown by this shelf view.
  119. // This is used to determine the common area where the mouse can hover
  120. // for showing tooltips without stuttering over gaps.
  121. void UpdateVisibleShelfItemBoundsUnion();
  122. // Returns true if the given location is within the bounds of all visiable app
  123. // icons. Used for tool tip visibility and scrolling event propogation.
  124. bool LocationInsideVisibleShelfItemBounds(const gfx::Point& location) const;
  125. // ShelfTooltipDelegate:
  126. bool ShouldShowTooltipForView(const views::View* view) const override;
  127. bool ShouldHideTooltip(const gfx::Point& cursor_location) const override;
  128. const std::vector<aura::Window*> GetOpenWindowsForView(
  129. views::View* view) override;
  130. std::u16string GetTitleForView(const views::View* view) const override;
  131. views::View* GetViewForEvent(const ui::Event& event) override;
  132. // Returns rectangle bounding all visible launcher items. Used screen
  133. // coordinate system.
  134. gfx::Rect GetVisibleItemsBoundsInScreen();
  135. // views::View:
  136. gfx::Size CalculatePreferredSize() const override;
  137. void OnThemeChanged() override;
  138. void OnBoundsChanged(const gfx::Rect& previous_bounds) override;
  139. FocusTraversable* GetPaneFocusTraversable() override;
  140. bool OnKeyPressed(const ui::KeyEvent& event) override;
  141. void OnMouseEvent(ui::MouseEvent* event) override;
  142. void ViewHierarchyChanged(
  143. const views::ViewHierarchyChangedDetails& details) override;
  144. void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
  145. View* GetTooltipHandlerForPoint(const gfx::Point& point) override;
  146. // ShelfButtonDelegate:
  147. void OnShelfButtonAboutToRequestFocusFromTabTraversal(ShelfButton* button,
  148. bool reverse) override;
  149. void ButtonPressed(views::Button* sender,
  150. const ui::Event& event,
  151. views::InkDrop* ink_drop) override;
  152. // FocusTraversable:
  153. views::FocusSearch* GetFocusSearch() override;
  154. // AccessiblePaneView:
  155. views::View* GetDefaultFocusableChild() override;
  156. // Overridden from views::ContextMenuController:
  157. void ShowContextMenuForViewImpl(views::View* source,
  158. const gfx::Point& point,
  159. ui::MenuSourceType source_type) override;
  160. // ash::TabletModeObserver:
  161. void OnTabletModeStarted() override;
  162. void OnTabletModeEnded() override;
  163. // Called from ScrollableShelfView when shelf config is updated.
  164. void OnShelfConfigUpdated();
  165. // Returns true if |event| on the shelf item is going to activate the
  166. // ShelfItem associated with |view|. Used to determine whether a pending ink
  167. // drop should be shown or not.
  168. bool ShouldEventActivateButton(views::View* view, const ui::Event& event);
  169. // ApplicationDragAndDropHost:
  170. bool ShouldHandleDrag(const std::string& app_id,
  171. const gfx::Point& location_in_screen) const override;
  172. bool StartDrag(const std::string& app_id,
  173. const gfx::Point& location_in_screen,
  174. const gfx::Rect& drag_icon_bounds_in_screen) override;
  175. bool Drag(const gfx::Point& location_in_screen,
  176. const gfx::Rect& drag_icon_bounds_in_screen) override;
  177. void EndDrag(bool cancel,
  178. std::unique_ptr<AppDragIconProxy> icon_proxy) override;
  179. // Swaps the given button with the next one if |with_next| is true, or with
  180. // the previous one if |with_next| is false.
  181. void SwapButtons(views::View* button_to_swap, bool with_next);
  182. // The ShelfAppButtons use the Pointer interface to enable item reordering.
  183. enum Pointer { NONE, DRAG_AND_DROP, MOUSE, TOUCH };
  184. void PointerPressedOnButton(views::View* view,
  185. Pointer pointer,
  186. const ui::LocatedEvent& event);
  187. void PointerDraggedOnButton(const views::View* view,
  188. Pointer pointer,
  189. const ui::LocatedEvent& event);
  190. void PointerReleasedOnButton(const views::View* view,
  191. Pointer pointer,
  192. bool canceled);
  193. // Returns whether |item| should belong in the pinned section of the shelf.
  194. bool IsItemPinned(const ShelfItem& item) const;
  195. // Returns whether |item| should be visible or hidden.
  196. bool IsItemVisible(const ShelfItem& item) const;
  197. // Update the layout when entering or exiting tablet mode. Have the owning
  198. // widget call this instead of observing changes ourselves to ensure this
  199. // happens after the tablet related changes in ShelfController.
  200. void OnTabletModeChanged();
  201. // True if the current |drag_view_| is the given |drag_view|.
  202. bool IsDraggedView(const views::View* drag_view) const;
  203. // These three methods return the first or last focuable child of the whole
  204. // shelf view.
  205. views::View* FindFirstOrLastFocusableChild(bool last);
  206. views::View* FindFirstFocusableChild();
  207. views::View* FindLastFocusableChild();
  208. // Handles the gesture event. Returns true if |event| has been consumed.
  209. bool HandleGestureEvent(const ui::GestureEvent* event);
  210. // Different from ShouldShowTooltipForView, |view| here must be a child view.
  211. bool ShouldShowTooltipForChildView(const views::View* child_view) const;
  212. // Returns the ShelfAppButton associated with |id|.
  213. ShelfAppButton* GetShelfAppButton(const ShelfID& id);
  214. // Updates the visibility of the views of the shelf items and the
  215. // |visible_views_indices_|.
  216. void UpdateShelfItemViewsVisibility();
  217. // If there is animation associated with |view| in |bounds_animator_|,
  218. // stops the animation.
  219. void StopAnimatingViewIfAny(views::View* view);
  220. // Returns the the shelf button size.
  221. int GetButtonSize() const;
  222. // Returns the size of a shelf button icon.
  223. int GetButtonIconSize() const;
  224. // Returns the size of the shelf item ripple ring.
  225. int GetShelfItemRippleSize() const;
  226. // If |app_icons_layout_offset_| is outdated, re-layout children to ideal
  227. // bounds.
  228. void LayoutIfAppIconsOffsetUpdates();
  229. // Returns the app button whose context menu is shown. Returns nullptr if no
  230. // app buttons have a context menu showing.
  231. ShelfAppButton* GetShelfItemViewWithContextMenu();
  232. // Modifies the announcement view to verbalize that the focused app button has
  233. // new updates, based on the item having a notification badge.
  234. void AnnounceShelfItemNotificationBadge(views::View* button);
  235. // Returns whether `bounds_animator_` is animating any view.
  236. bool IsAnimating() const;
  237. // If a shelf icon is being dragged (and drag icon proxy is created), returns
  238. // the drag icon bounds in screen coordinate. Otherwise, returns empty bounds.
  239. gfx::Rect GetDragIconBoundsInScreenForTest() const;
  240. // Return the view model for test purposes.
  241. views::ViewModel* view_model_for_test() { return view_model_.get(); }
  242. void set_default_last_focusable_child(bool default_last_focusable_child) {
  243. default_last_focusable_child_ = default_last_focusable_child;
  244. }
  245. ShelfAppButton* drag_view() { return drag_view_; }
  246. const std::vector<size_t>& visible_views_indices() const {
  247. return visible_views_indices_;
  248. }
  249. size_t number_of_visible_apps() const {
  250. return visible_views_indices_.size();
  251. }
  252. ShelfWidget* shelf_widget() const { return shelf_->shelf_widget(); }
  253. const views::ViewModel* view_model() const { return view_model_.get(); }
  254. ShelfID drag_and_drop_shelf_id() const { return drag_and_drop_shelf_id_; }
  255. views::View* first_visible_button_for_testing() {
  256. DCHECK(!visible_views_indices_.empty());
  257. return view_model_->view_at(visible_views_indices_[0]);
  258. }
  259. ShelfMenuModelAdapter* shelf_menu_model_adapter_for_testing() {
  260. return shelf_menu_model_adapter_.get();
  261. }
  262. absl::optional<size_t> current_ghost_view_index() const {
  263. return current_ghost_view_index_;
  264. }
  265. private:
  266. friend class ShelfViewTestAPI;
  267. class FadeInAnimationDelegate;
  268. class FadeOutAnimationDelegate;
  269. class StartFadeAnimationDelegate;
  270. class ViewOpacityResetter;
  271. enum RemovableState {
  272. REMOVABLE, // Item can be removed when dragged away.
  273. DRAGGABLE, // Item can be dragged, but will snap always back to origin.
  274. NOT_REMOVABLE, // Item is fixed and can never be removed.
  275. };
  276. // Minimum distance before drag starts.
  277. static const int kMinimumDragDistance;
  278. // Common setup done for all children views. |layer_type| specifies the type
  279. // of layer for the |view|. Use ui::LAYER_NOT_DRAWN if the content of the view
  280. // do not have to be painted (e.g. a container for views that have its own
  281. // texture layer).
  282. static void ConfigureChildView(views::View* view, ui::LayerType layer_type);
  283. bool dragging() const { return drag_pointer_ != NONE; }
  284. // Calculates the ideal bounds of shelf elements.
  285. // The bounds of each button corresponding to an item in the model is set in
  286. // |view_model_|.
  287. void CalculateIdealBounds();
  288. // Creates the view used to represent given shelf |item|.
  289. // Returns unowned pointer (view is owned by the view hierarchy).
  290. views::View* CreateViewForItem(const ShelfItem& item);
  291. // Returns the size that's actually available for app icons. Size occupied
  292. // by the home button and back button plus all appropriate margins is
  293. // not available for app icons.
  294. int GetAvailableSpaceForAppIcons() const;
  295. // Updates the index of the separator and save it to |separator_index_|.
  296. void UpdateSeparatorIndex();
  297. // Sets the bounds of each view to its ideal bounds.
  298. void LayoutToIdealBounds();
  299. void LayoutBackAndHomeButtons();
  300. // Returns the index of the last view whose max primary axis coordinate is
  301. // less than |max_value|. Returns -1 if nothing fits, or there are no views.
  302. int IndexOfLastItemThatFitsSize(int max_value) const;
  303. // Animates the bounds of each view to its ideal bounds.
  304. void AnimateToIdealBounds();
  305. // Fades |view| from an opacity of 0 to 1. This is when adding a new item.
  306. void FadeIn(views::View* view);
  307. // Invoked when the pointer has moved enough to trigger a drag. Sets
  308. // internal state in preparation for the drag.
  309. void PrepareForDrag(Pointer pointer, const ui::LocatedEvent& event);
  310. // Invoked when the mouse is dragged. Updates the models as appropriate.
  311. void ContinueDrag(const ui::LocatedEvent& event);
  312. // Scroll the view to show more content in the direction of the user's drag.
  313. void ScrollForUserDrag(int offset);
  314. // Increase the speed of an existing scroll.
  315. void SpeedUpDragScrolling();
  316. // Reorder |drag_view_| according to the latest dragging coordinate.
  317. void MoveDragViewTo(int primary_axis_coordinate);
  318. // Called when drag icon proxy closure animation completes.
  319. // Runs `opacity_resetter` which is expected to reset the drag view opacity to
  320. // visible state (as drag view may get hidden while shelf item drag is in
  321. // progress).
  322. void OnDragIconProxyAnimatedOut(
  323. std::unique_ptr<ViewOpacityResetter> opacity_resetter);
  324. // Handles ripping off an item from the shelf.
  325. void HandleRipOffDrag(const ui::LocatedEvent& event);
  326. // Finalize the rip off dragging by either |cancel| the action or validating.
  327. void FinalizeRipOffDrag(bool cancel);
  328. // Check if an item can be ripped off or not.
  329. RemovableState RemovableByRipOff(int index) const;
  330. // Returns true if |typea| and |typeb| should be in the same drag range.
  331. bool SameDragType(ShelfItemType typea, ShelfItemType typeb) const;
  332. // Returns true if focus should move out of the ShelfView view tree.
  333. bool ShouldFocusOut(bool reverse, views::View* button);
  334. // Returns the range (in the model) the item at the specified index can be
  335. // dragged to.
  336. std::pair<size_t, size_t> GetDragRange(size_t index);
  337. // Checks if the item at |dragged_item_index| should be pinned or unpinned on
  338. // pointer release.
  339. bool ShouldUpdateDraggedViewPinStatus(size_t dragged_item_index);
  340. // Checks if |dragged_view| is allowed to be dragged across the separator to
  341. // perform pinning and unpinning. Note that this function doesn't check if the
  342. // separator exists.
  343. bool CanDragAcrossSeparator(views::View* dragged_view) const;
  344. // If there is a drag operation in progress it's canceled. If |modified_index|
  345. // is valid, the new position of the corresponding item is returned.
  346. absl::optional<size_t> CancelDrag(absl::optional<size_t> modified_index);
  347. // Returns rectangle bounds used for drag insertion.
  348. gfx::Rect GetBoundsForDragInsertInScreen();
  349. // Invoked after the fading in animation for item addition is ended.
  350. void OnFadeInAnimationEnded();
  351. // Invoked after the fading out animation for item deletion is ended.
  352. void OnFadeOutAnimationEnded();
  353. // Gets the menu anchor rect for menus. |source| is the view that is
  354. // asking for a menu, |location| is the location of the event, |context_menu|
  355. // is whether the menu is for a context or application menu.
  356. gfx::Rect GetMenuAnchorRect(const views::View& source,
  357. const gfx::Point& location,
  358. bool context_menu) const;
  359. void AnnounceShelfAlignment();
  360. void AnnounceShelfAutohideBehavior();
  361. void AnnouncePinUnpinEvent(const ShelfItem& item, bool pinned);
  362. void AnnounceSwapEvent(const ShelfItem& first_item,
  363. const ShelfItem& second_item);
  364. // Overridden from ui::EventHandler:
  365. void OnGestureEvent(ui::GestureEvent* event) override;
  366. // Overridden from ShelfModelObserver:
  367. void ShelfItemAdded(int model_index) override;
  368. void ShelfItemRemoved(int model_index, const ShelfItem& old_item) override;
  369. void ShelfItemChanged(int model_index, const ShelfItem& old_item) override;
  370. void ShelfItemsUpdatedForDeskChange() override;
  371. void ShelfItemMoved(int start_index, int target_index) override;
  372. void ShelfItemDelegateChanged(const ShelfID& id,
  373. ShelfItemDelegate* old_delegate,
  374. ShelfItemDelegate* delegate) override;
  375. void ShelfItemStatusChanged(const ShelfID& id) override;
  376. void ShelfItemRippedOff() override;
  377. void ShelfItemReturnedFromRipOff(int index) override;
  378. void ShelfPartyToggled(bool in_shelf_party) override;
  379. // Overridden from ShellObserver:
  380. void OnShelfAlignmentChanged(aura::Window* root_window,
  381. ShelfAlignment old_alignment) override;
  382. // ShelfObserver:
  383. void OnShelfAutoHideBehaviorChanged() override;
  384. // Shows a shelf context menu with the given |model|, or a default menu.
  385. void ShowShelfContextMenu(const ShelfID& shelf_id,
  386. const gfx::Point& point,
  387. views::View* source,
  388. ui::MenuSourceType source_type,
  389. std::unique_ptr<ui::SimpleMenuModel> model);
  390. // Handles the result of an item selection, records the |action| taken and
  391. // optionally shows an application menu with the given |menu_items|.
  392. void AfterItemSelected(const ShelfItem& item,
  393. views::Button* sender,
  394. std::unique_ptr<ui::Event> event,
  395. views::InkDrop* ink_drop,
  396. ShelfAction action,
  397. ShelfItemDelegate::AppMenuItems menu_items);
  398. // Show either a context or normal click menu of given |menu_model|.
  399. // If |context_menu| is set, the displayed menu is a context menu and not
  400. // a menu listing one or more running applications.
  401. // The |click_point| is only used for |context_menu|'s.
  402. void ShowMenu(std::unique_ptr<ui::SimpleMenuModel> menu_model,
  403. views::View* source,
  404. const ShelfID& shelf_id,
  405. const gfx::Point& click_point,
  406. bool context_menu,
  407. ui::MenuSourceType source_type);
  408. // Callback for MenuRunner.
  409. void OnMenuClosed(views::View* source);
  410. // Overridden from views::BoundsAnimatorObserver:
  411. void OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) override;
  412. void OnBoundsAnimatorDone(views::BoundsAnimator* animator) override;
  413. // Returns true if the (press down) |event| is a repost event from an event
  414. // which just closed the menu of a shelf item. If it occurs on the same shelf
  415. // item, we should ignore the call.
  416. bool IsRepostEvent(const ui::Event& event);
  417. // Returns true if the given |item| is supposed to be shown to the user.
  418. bool ShouldShowShelfItem(const ShelfItem& item);
  419. // Convenience accessor to model_->items().
  420. const ShelfItem* ShelfItemForView(const views::View* view) const;
  421. // Get the distance from the given |coordinate| to the closest point on this
  422. // launcher/shelf.
  423. int CalculateShelfDistance(const gfx::Point& coordinate) const;
  424. bool CanPrepareForDrag(Pointer pointer, const ui::LocatedEvent& event);
  425. bool ShouldHandleGestures(const ui::GestureEvent& event) const;
  426. void DestroyScopedDisplay();
  427. // Different from GetTitleForView, |view| here must be a child view.
  428. std::u16string GetTitleForChildView(const views::View* view) const;
  429. int CalculateAppIconsLayoutOffset() const;
  430. // Returns the bounds of the given |child| view taken into account RTL layouts
  431. // and on-going bounds animations on |child|.
  432. gfx::Rect GetChildViewTargetMirroredBounds(const views::View* child) const;
  433. // Calls |UpdateShelfItemViewsVisibility| and updates the shelf for changes in
  434. // visibility of items. Used when items may have joined or left shelf party,
  435. // because partying items are hidden from the shelf. After the party ends,
  436. // this function causes the items that were partying to reappear on the shelf.
  437. void HandleShelfParty();
  438. // Removes and reset |current_ghost_view| and |last_ghost_view|.
  439. void RemoveGhostView();
  440. // Resets the data members related to the app item context menu model request.
  441. void ResetActiveMenuModelRequest();
  442. // The model; owned by Launcher.
  443. ShelfModel* const model_;
  444. // The shelf controller; owned by RootWindowController.
  445. Shelf* const shelf_;
  446. // Used to manage the set of active launcher buttons. There is a view per
  447. // item in |model_|.
  448. std::unique_ptr<views::ViewModel> view_model_;
  449. // The indices of the views in |view_model_| that are visible.
  450. std::vector<size_t> visible_views_indices_;
  451. std::unique_ptr<views::BoundsAnimator> bounds_animator_;
  452. // Pointer device that initiated the current drag operation. If there is no
  453. // current dragging operation, this is NONE.
  454. Pointer drag_pointer_ = NONE;
  455. // The view being dragged. This is set immediately when the mouse is pressed.
  456. // |dragging_| is set only if the mouse is dragged far enough.
  457. ShelfAppButton* drag_view_ = nullptr;
  458. // The view showing a context menu. This can be either a ShelfView or
  459. // ShelfAppButton.
  460. views::View* menu_owner_ = nullptr;
  461. // A reference to the view used as a separator between pinned and unpinned
  462. // items.
  463. views::Separator* separator_ = nullptr;
  464. // Index of |separator_|. It is set to nullopt if it is invisible.
  465. absl::optional<size_t> separator_index_ = absl::nullopt;
  466. // Used in |drag_view_relative_to_ideal_bounds_| to represent the relative
  467. // position between |drag_view_| and its ideal bounds in shelf.
  468. enum class RelativePosition {
  469. // Set if |drag_view_| is not available or the relative position is not
  470. // calculated yet.
  471. kNotAvailable,
  472. // Set if |drag_view_| is to the left of its ideal bounds.
  473. kLeft,
  474. // Set if |drag_view_| is to the right of its ideal bounds.
  475. kRight
  476. };
  477. // The |drag_view_|'s current position relative to its ideal bounds.
  478. RelativePosition drag_view_relative_to_ideal_bounds_ =
  479. RelativePosition::kNotAvailable;
  480. // Position of the mouse down event in |drag_view_|'s coordinates.
  481. gfx::Point drag_origin_;
  482. // Index |drag_view_| was initially at.
  483. absl::optional<size_t> start_drag_index_ = absl::nullopt;
  484. // Used for the context menu of a particular item.
  485. ShelfID context_menu_id_;
  486. // Responsible for building and running all menus.
  487. std::unique_ptr<ShelfMenuModelAdapter> shelf_menu_model_adapter_;
  488. // Created when a shelf icon is pressed, so that new windows will be on the
  489. // same display as the press event.
  490. std::unique_ptr<display::ScopedDisplayForNewWindows>
  491. scoped_display_for_new_windows_;
  492. // True when an item being inserted or removed in the model cancels a drag.
  493. bool cancelling_drag_model_changed_ = false;
  494. // The item with an in-flight async request for a context menu or selection
  495. // (which shows a shelf item application menu if multiple windows are open).
  496. // Used to avoid multiple concurrent menu requests. The value is null if none.
  497. ShelfID item_awaiting_response_;
  498. // The callback for in-flight async request for a context menu.
  499. // Used to cancel the request if context menu should be
  500. // cancelled, for example if shelf item drag starts.
  501. base::CancelableOnceCallback<void(std::unique_ptr<ui::SimpleMenuModel> model)>
  502. context_menu_callback_;
  503. // The timestamp of the event which closed the last menu - or 0.
  504. base::TimeTicks closing_event_time_;
  505. // True if a drag and drop operation created/pinned the item in the launcher
  506. // and it needs to be deleted/unpinned again if the operation gets cancelled.
  507. bool drag_and_drop_item_pinned_ = false;
  508. // The ShelfItem currently used for drag and drop; empty if none.
  509. ShelfID drag_and_drop_shelf_id_;
  510. // Last received drag icon bounds during drag and drop received using
  511. // `ApplicationDragAndDropHost` interface.
  512. gfx::Rect drag_icon_bounds_in_screen_;
  513. // The original launcher item's size before the dragging operation.
  514. gfx::Size pre_drag_and_drop_size_;
  515. // True when the icon was dragged off the shelf.
  516. bool dragged_off_shelf_ = false;
  517. // The rip off view when a snap back operation is underway.
  518. ShelfAppButton* snap_back_from_rip_off_view_ = nullptr;
  519. // True if the event is a repost event from a event which has just closed the
  520. // menu of the same shelf item.
  521. bool is_repost_event_on_same_item_ = false;
  522. // Record the index for the last pressed shelf item. This variable is used to
  523. // check if a repost event occurs on the same shelf item as previous one. If
  524. // so, the repost event should be ignored.
  525. absl::optional<size_t> last_pressed_index_ = absl::nullopt;
  526. // Tracks UMA metrics based on shelf button press actions.
  527. ShelfButtonPressedMetricTracker shelf_button_pressed_metric_tracker_;
  528. // The union of all visible shelf item bounds. Used for showing tooltips in
  529. // a continuous manner.
  530. gfx::Rect visible_shelf_item_bounds_union_;
  531. // A view used to make accessibility announcements (changes in the shelf's
  532. // alignment or auto-hide state).
  533. views::View* announcement_view_ = nullptr; // Owned by ShelfView
  534. // For dragging: -1 if scrolling back, 1 if scrolling forward, 0 if neither.
  535. int drag_scroll_dir_ = 0;
  536. // Used to periodically call ScrollForUserDrag.
  537. base::RepeatingTimer scrolling_timer_;
  538. // Used to call SpeedUpDragScrolling.
  539. base::OneShotTimer speed_up_drag_scrolling_;
  540. // Whether this view should focus its last focusable child (instead of its
  541. // first) when focused.
  542. bool default_last_focusable_child_ = false;
  543. // Indicates the starting position of shelf items on the main axis. (Main
  544. // axis is x-axis when the shelf is horizontally aligned; otherwise, it
  545. // becomes y-axis)
  546. int app_icons_layout_offset_ = 0;
  547. Delegate* const delegate_;
  548. // Whether the shelf view is actively acting as an application drag and drop
  549. // host. Note that shelf view is not expected to create its own
  550. // `AppDragIconProxy` for item drag operation, but a `drag_icon_proxy_` may
  551. // get passed to the shelf view using `ApplicationDragAndDropHost::EndDrag()`
  552. // interface.
  553. bool is_active_drag_and_drop_host_ = false;
  554. // The app item icon proxy created for drag operation.
  555. std::unique_ptr<AppDragIconProxy> drag_icon_proxy_;
  556. // Placeholder ghost icon to show where an app will drop on the shelf.
  557. GhostImageView* current_ghost_view_ = nullptr;
  558. // The latest ghost icon shown set to be replaced by |current_ghost_view_|.
  559. GhostImageView* last_ghost_view_ = nullptr;
  560. // The index in the shelf app icons where the |current_ghost_view_| will show.
  561. absl::optional<size_t> current_ghost_view_index_ = absl::nullopt;
  562. // When the scrollable shelf is enabled, |shelf_button_delegate_| should
  563. // be ScrollableShelfView.
  564. ShelfButtonDelegate* shelf_button_delegate_ = nullptr;
  565. // Owned by ScrollableShelfView.
  566. views::FocusSearch* focus_search_ = nullptr;
  567. std::unique_ptr<FadeInAnimationDelegate> fade_in_animation_delegate_;
  568. // Tracks the icon move animation.
  569. absl::optional<ui::ThroughputTracker> move_animation_tracker_;
  570. // Tracks the icon fade-out animation.
  571. absl::optional<ui::ThroughputTracker> fade_out_animation_tracker_;
  572. // Called when showing shelf context menu.
  573. base::RepeatingClosure context_menu_shown_callback_;
  574. base::WeakPtrFactory<ShelfView> weak_factory_{this};
  575. };
  576. } // namespace ash
  577. #endif // ASH_SHELF_SHELF_VIEW_H_