holding_space_tray_icon.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2020 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_SYSTEM_HOLDING_SPACE_HOLDING_SPACE_TRAY_ICON_H_
  5. #define ASH_SYSTEM_HOLDING_SPACE_HOLDING_SPACE_TRAY_ICON_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "ash/ash_export.h"
  9. #include "ash/public/cpp/holding_space/holding_space_controller.h"
  10. #include "ash/public/cpp/holding_space/holding_space_model.h"
  11. #include "ash/public/cpp/shelf_config.h"
  12. #include "ash/shell.h"
  13. #include "ash/shell_observer.h"
  14. #include "base/scoped_observation.h"
  15. #include "base/time/time.h"
  16. #include "ui/base/metadata/metadata_header_macros.h"
  17. #include "ui/views/view.h"
  18. namespace ash {
  19. class HoldingSpaceTrayIconPreview;
  20. class Shelf;
  21. // The icon used to represent holding space in its tray in the shelf.
  22. class ASH_EXPORT HoldingSpaceTrayIcon : public views::View,
  23. public ShellObserver,
  24. public ShelfConfig::Observer {
  25. public:
  26. METADATA_HEADER(HoldingSpaceTrayIcon);
  27. explicit HoldingSpaceTrayIcon(Shelf* shelf);
  28. HoldingSpaceTrayIcon(const HoldingSpaceTrayIcon&) = delete;
  29. HoldingSpaceTrayIcon& operator=(const HoldingSpaceTrayIcon&) = delete;
  30. ~HoldingSpaceTrayIcon() override;
  31. // Updates whether or not this holding space icon is in drop target state.
  32. // If `did_drop_to_pin` is true, the user has just performed a drag-and-drop
  33. // to pin action. Otherwise a drag may still be in progress or the user action
  34. // did not result in an item being pinned to holding space.
  35. void UpdateDropTargetState(bool is_drop_target, bool did_drop_to_pin);
  36. // Updates the list of previews shown in the icon. The icon will be changed to
  37. // show previews for holding space items in `items`. The order of previews in
  38. // the icon will match the order in `items`.
  39. // The items are updated in the following stages:
  40. // 1. Remove obsolete items.
  41. // 2. Update the icon preferred size.
  42. // 3. Shift existing items to their new position.
  43. // 4. Animate new items in.
  44. void UpdatePreviews(const std::vector<const HoldingSpaceItem*> items);
  45. // Clears the icon.
  46. void Clear();
  47. // Called from HoldingSpaceTray when holding space model changes:
  48. void OnHoldingSpaceModelAttached(HoldingSpaceModel* model);
  49. void OnHoldingSpaceModelDetached(HoldingSpaceModel* model);
  50. void OnHoldingSpaceItemAdded(const HoldingSpaceItem* item);
  51. void OnHoldingSpaceItemRemoved(const HoldingSpaceItem* item);
  52. void OnHoldingSpaceItemFinalized(const HoldingSpaceItem* item);
  53. // Sets if updates should be animated.
  54. void set_should_animate_updates(bool should_animate_updates) {
  55. should_animate_updates_ = should_animate_updates;
  56. }
  57. private:
  58. class ResizeAnimation;
  59. // views::View:
  60. int GetHeightForWidth(int width) const override;
  61. gfx::Size CalculatePreferredSize() const override;
  62. void OnThemeChanged() override;
  63. // ShellObserver:
  64. void OnShellDestroying() override;
  65. void OnShelfAlignmentChanged(aura::Window* root_window,
  66. ShelfAlignment old_alignment) override;
  67. // ShelfConfigObserver:
  68. void OnShelfConfigUpdated() override;
  69. void InitLayout();
  70. // Invoked when the specified preview has completed animating out. At this
  71. // point it is owned by `removed_previews_` and should be destroyed.
  72. void OnOldItemAnimatedOut(HoldingSpaceTrayIconPreview*,
  73. const base::RepeatingClosure& callback);
  74. // Called when all obsolete previews have been removed during previews update.
  75. void OnOldItemsRemoved();
  76. // Defines parameters for how to animate a given `preview`.
  77. struct PreviewAnimationParams {
  78. HoldingSpaceTrayIconPreview* preview;
  79. base::TimeDelta delay;
  80. };
  81. // Calculates parameters for how to animate shift/in existing/new items.
  82. std::vector<PreviewAnimationParams> CalculateAnimateShiftParams();
  83. std::vector<PreviewAnimationParams> CalculateAnimateInParams();
  84. // Ensures that preview layers are stacked to match ordering in `item_ids_`.
  85. void EnsurePreviewLayerStackingOrder();
  86. // The shelf associated with this holding space tray icon.
  87. Shelf* const shelf_;
  88. // Whether or not this holding space tray icon is currently in drop target
  89. // state. When in drop target state, preview indices are offset from their
  90. // standard positions by a fixed amount.
  91. bool is_drop_target_ = false;
  92. // True if updates should be animated, false otherwise. Generally speaking,
  93. // updates are animated only if they occur mid-session. Updates that occur
  94. // during session start/unlock or on profile change should not be animated.
  95. bool should_animate_updates_ = false;
  96. // A preview is added to the tray icon to visually represent each holding
  97. // space item. Upon creation, previews are added to `previews_by_id_` where
  98. // they are owned until being animated out. Upon being animated out, previews
  99. // are moved to `removed_previews_` where they are owned until the out
  100. // animation completes and they are subsequently destroyed. NOTE:
  101. // `previews_by_id_` maps holding space item IDs to their respective previews.
  102. std::map<std::string, std::unique_ptr<HoldingSpaceTrayIconPreview>>
  103. previews_by_id_;
  104. std::vector<std::unique_ptr<HoldingSpaceTrayIconPreview>> removed_previews_;
  105. // Ordered list of holding space item IDs represented in the tray icon
  106. // (including items that are not currently visible).
  107. std::vector<std::string> item_ids_;
  108. // A view that serves as a parent for previews' layers. Used to easily
  109. // translate all the previews within the icon during resize animation.
  110. views::View* previews_container_ = nullptr;
  111. // Helper to run icon resize animation.
  112. std::unique_ptr<ResizeAnimation> resize_animation_;
  113. base::ScopedObservation<Shell,
  114. ShellObserver,
  115. &Shell::AddShellObserver,
  116. &Shell::RemoveShellObserver>
  117. shell_observer_{this};
  118. base::ScopedObservation<ShelfConfig, ShelfConfig::Observer>
  119. shelf_config_observer_{this};
  120. // The factory to which callbacks for stages of the previews list update are
  121. // bound to. The goal is to easily cancel in-progress updates if the list of
  122. // items gets updated.
  123. base::WeakPtrFactory<HoldingSpaceTrayIcon> previews_update_weak_factory_{
  124. this};
  125. };
  126. } // namespace ash
  127. #endif // ASH_SYSTEM_HOLDING_SPACE_HOLDING_SPACE_TRAY_ICON_H_