shelf_item_delegate.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2017 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_PUBLIC_CPP_SHELF_ITEM_DELEGATE_H_
  5. #define ASH_PUBLIC_CPP_SHELF_ITEM_DELEGATE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "ash/public/cpp/ash_public_export.h"
  9. #include "ash/public/cpp/shelf_types.h"
  10. #include "base/callback.h"
  11. #include "base/callback_forward.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "ui/events/event.h"
  14. #include "ui/gfx/image/image_skia.h"
  15. class AppWindowShelfItemController;
  16. namespace aura {
  17. class Window;
  18. } // namespace aura
  19. namespace gfx {
  20. class ImageSkia;
  21. }
  22. namespace ui {
  23. class SimpleMenuModel;
  24. }
  25. namespace ash {
  26. // ShelfItemDelegate tracks some item state, handles shelf item selection, menu
  27. // command execution, etc.
  28. class ASH_PUBLIC_EXPORT ShelfItemDelegate {
  29. public:
  30. explicit ShelfItemDelegate(const ShelfID& shelf_id);
  31. ShelfItemDelegate(const ShelfItemDelegate&) = delete;
  32. ShelfItemDelegate& operator=(const ShelfItemDelegate&) = delete;
  33. virtual ~ShelfItemDelegate();
  34. const ShelfID& shelf_id() const { return shelf_id_; }
  35. void set_shelf_id(const ShelfID& shelf_id) { shelf_id_ = shelf_id; }
  36. const std::string& app_id() const { return shelf_id_.app_id; }
  37. const std::string& launch_id() const { return shelf_id_.launch_id; }
  38. bool image_set_by_controller() const { return image_set_by_controller_; }
  39. void set_image_set_by_controller(bool image_set_by_controller) {
  40. image_set_by_controller_ = image_set_by_controller;
  41. }
  42. // A predicate to filter app menu items based on their associated windows. If
  43. // true is returned, then the item should be included in the menu, otherwise
  44. // it should be discarded.
  45. using ItemFilterPredicate = base::RepeatingCallback<bool(aura::Window*)>;
  46. // Called when the user selects a shelf item. The event, display, and source
  47. // info should be provided if known; some implementations use these arguments.
  48. // Defaults: (nullptr, kInvalidDisplayId, LAUNCH_FROM_UNKNOWN)
  49. // The callback reports the action taken and any application menu to show.
  50. // NOTE: This codepath is not currently used for context menu triggering.
  51. struct AppMenuItem {
  52. // The ID that will be used by ShelfApplicationMenuModel to represent this
  53. // item in the shelf app menu. This ID will be used when ExecuteCommand() is
  54. // called when this item is selected from the menu.
  55. int command_id;
  56. // The title and icon shown for this item in the app menu.
  57. std::u16string title;
  58. gfx::ImageSkia icon;
  59. };
  60. using AppMenuItems = std::vector<AppMenuItem>;
  61. using ItemSelectedCallback =
  62. base::OnceCallback<void(ShelfAction, AppMenuItems)>;
  63. virtual void ItemSelected(std::unique_ptr<ui::Event> event,
  64. int64_t display_id,
  65. ShelfLaunchSource source,
  66. ItemSelectedCallback callback,
  67. const ItemFilterPredicate& filter_predicate);
  68. // Returns items for the application menu; used for convenience and testing.
  69. // |filter_predicate| is used to filter items out of the menu based on their
  70. // corresponding windows.
  71. virtual AppMenuItems GetAppMenuItems(
  72. int event_flags,
  73. const ItemFilterPredicate& filter_predicate);
  74. // Returns the context menu model; used to show ShelfItem context menus.
  75. using GetContextMenuCallback =
  76. base::OnceCallback<void(std::unique_ptr<ui::SimpleMenuModel>)>;
  77. virtual void GetContextMenu(int64_t display_id,
  78. GetContextMenuCallback callback);
  79. // Returns nullptr if class is not AppWindowShelfItemController.
  80. virtual AppWindowShelfItemController* AsAppWindowShelfItemController();
  81. // Called on invocation of a shelf item's context or application menu command.
  82. // |from_context_menu| is true if the command came from a context menu, or
  83. // false if the command came from an application menu. If the |display_id| is
  84. // unknown or irrelevant, callers may pass |display::kInvalidDisplayId|.
  85. virtual void ExecuteCommand(bool from_context_menu,
  86. int64_t command_id,
  87. int32_t event_flags,
  88. int64_t display_id) = 0;
  89. // Closes all windows associated with this shelf item.
  90. virtual void Close() = 0;
  91. private:
  92. // The shelf id; empty if there is no app associated with the item.
  93. // Besides the application id, ShelfID also contains a launch id, which is an
  94. // id that can be passed to an app when launched in order to support multiple
  95. // shelf items per app. This id is used together with the app_id to uniquely
  96. // identify each shelf item that has the same app_id.
  97. ShelfID shelf_id_;
  98. // Set to true if the launcher item image has been set by the controller.
  99. bool image_set_by_controller_ = false;
  100. base::WeakPtrFactory<ShelfItemDelegate> weak_ptr_factory_{this};
  101. };
  102. } // namespace ash
  103. #endif // ASH_PUBLIC_CPP_SHELF_ITEM_DELEGATE_H_