clipboard_history_menu_model_adapter.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_CLIPBOARD_CLIPBOARD_HISTORY_MENU_MODEL_ADAPTER_H_
  5. #define ASH_CLIPBOARD_CLIPBOARD_HISTORY_MENU_MODEL_ADAPTER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "base/time/time.h"
  9. #include "base/unguessable_token.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. #include "ui/base/models/simple_menu_model.h"
  12. #include "ui/views/controls/menu/menu_model_adapter.h"
  13. namespace gfx {
  14. class Rect;
  15. } // namespace gfx
  16. namespace views {
  17. class MenuItemView;
  18. class MenuRunner;
  19. } // namespace views
  20. namespace ash {
  21. namespace ClipboardHistoryUtil {
  22. enum class Action;
  23. } // namespace ClipboardHistoryUtil
  24. class ClipboardHistory;
  25. class ClipboardHistoryItem;
  26. class ClipboardHistoryItemView;
  27. class ClipboardHistoryResourceManager;
  28. // Used to show the clipboard history menu, which holds the last few things
  29. // copied.
  30. class ASH_EXPORT ClipboardHistoryMenuModelAdapter : views::MenuModelAdapter {
  31. public:
  32. static std::unique_ptr<ClipboardHistoryMenuModelAdapter> Create(
  33. ui::SimpleMenuModel::Delegate* delegate,
  34. base::RepeatingClosure menu_closed_callback,
  35. const ClipboardHistory* clipboard_history,
  36. const ClipboardHistoryResourceManager* resource_manager);
  37. ClipboardHistoryMenuModelAdapter(const ClipboardHistoryMenuModelAdapter&) =
  38. delete;
  39. ClipboardHistoryMenuModelAdapter& operator=(
  40. const ClipboardHistoryMenuModelAdapter&) = delete;
  41. ~ClipboardHistoryMenuModelAdapter() override;
  42. // Shows the menu anchored at `anchor_rect`, `source_type` indicates how the
  43. // menu is triggered.
  44. void Run(const gfx::Rect& anchor_rect,
  45. ui::MenuSourceType source_type);
  46. // Returns if the menu is currently running.
  47. bool IsRunning() const;
  48. // Hides and cancels the menu.
  49. void Cancel();
  50. // Returns the command of the currently selected menu item. If no menu item is
  51. // currently selected, returns |absl::nullopt|.
  52. absl::optional<int> GetSelectedMenuItemCommand() const;
  53. // Returns the item mapped by `command_id` in `item_snapshots_`.
  54. const ClipboardHistoryItem& GetItemFromCommandId(int command_id) const;
  55. // Returns the count of menu items.
  56. size_t GetMenuItemsCount() const;
  57. // Selects the menu item specified by `command_id`.
  58. void SelectMenuItemWithCommandId(int command_id);
  59. // Selects the menu item hovered by mouse.
  60. void SelectMenuItemHoveredByMouse();
  61. // Removes the menu item specified by `command_id`.
  62. void RemoveMenuItemWithCommandId(int command_id);
  63. // Advances the pseudo focus (backward if `reverse` is true).
  64. void AdvancePseudoFocus(bool reverse);
  65. // Returns the action to take on the menu item specified by `command_id`.
  66. ClipboardHistoryUtil::Action GetActionForCommandId(int command_id) const;
  67. // Returns menu bounds in screen coordinates.
  68. gfx::Rect GetMenuBoundsInScreenForTest() const;
  69. const views::MenuItemView* GetMenuItemViewAtForTest(size_t index) const;
  70. views::MenuItemView* GetMenuItemViewAtForTest(size_t index);
  71. private:
  72. class ScopedA11yIgnore;
  73. ClipboardHistoryMenuModelAdapter(
  74. std::unique_ptr<ui::SimpleMenuModel> model,
  75. base::RepeatingClosure menu_closed_callback,
  76. const ClipboardHistory* clipboard_history,
  77. const ClipboardHistoryResourceManager* resource_manager);
  78. // Advances the pseduo focus from the selected history item view (backward if
  79. // `reverse` is true).
  80. void AdvancePseudoFocusFromSelectedItem(bool reverse);
  81. // Returns the command id of the menu item to be selected after the
  82. // menu item specified by `command_id` is deleted.
  83. int CalculateSelectedCommandIdAfterDeletion(int command_id) const;
  84. // Removes the item view specified by `command_id` from the root menu.
  85. void RemoveItemView(int command_id);
  86. // views::MenuModelAdapter:
  87. views::MenuItemView* AppendMenuItem(views::MenuItemView* menu,
  88. ui::MenuModel* model,
  89. size_t index) override;
  90. void OnMenuClosed(views::MenuItemView* menu) override;
  91. // The model which holds the contents of the menu.
  92. std::unique_ptr<ui::SimpleMenuModel> const model_;
  93. // The root MenuItemView which contains all child MenuItemViews. Owned by
  94. // |menu_runner_|.
  95. views::MenuItemView* root_view_ = nullptr;
  96. // Responsible for showing |root_view_|.
  97. std::unique_ptr<views::MenuRunner> menu_runner_;
  98. // The timestamp taken when the menu is opened. Used in metrics.
  99. base::TimeTicks menu_open_time_;
  100. // The mapping between the command ids and items that are copied from
  101. // `clipboard_history_` when the menu is created. It is used to solve the
  102. // possible inconsistency between the menu model data and the clipboard
  103. // history data. For example, a new item is added to `clipboard_history_`
  104. // while the menu is showing.
  105. // It updates synchronously when a item is removed.
  106. std::map<int, ClipboardHistoryItem> item_snapshots_;
  107. // Stores mappings between command ids and history item view pointers.
  108. // It updates synchronously when a item is removed.
  109. std::map<int, ClipboardHistoryItemView*> item_views_by_command_id_;
  110. const ClipboardHistory* const clipboard_history_;
  111. // Resource manager used to fetch image models. Owned by
  112. // ClipboardHistoryController.
  113. const ClipboardHistoryResourceManager* const resource_manager_;
  114. // Indicates the number of item deletion operations in progress. Note that
  115. // a `ClipboardHistoryItemView` instance is deleted asynchronously.
  116. int item_deletion_in_progress_count_ = 0;
  117. std::unique_ptr<ScopedA11yIgnore> scoped_ignore_;
  118. // Indicates whether `Run()` has been called before.
  119. bool run_before_ = false;
  120. base::WeakPtrFactory<ClipboardHistoryMenuModelAdapter> weak_ptr_factory_{
  121. this};
  122. };
  123. } // namespace ash
  124. #endif // ASH_CLIPBOARD_CLIPBOARD_HISTORY_MENU_MODEL_ADAPTER_H_