apps_grid_context_menu.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2021 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_APP_LIST_VIEWS_APPS_GRID_CONTEXT_MENU_H_
  5. #define ASH_APP_LIST_VIEWS_APPS_GRID_CONTEXT_MENU_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ui/base/models/simple_menu_model.h"
  9. #include "ui/views/context_menu_controller.h"
  10. namespace views {
  11. class MenuItemView;
  12. class MenuModelAdapter;
  13. class MenuRunner;
  14. } // namespace views
  15. namespace ash {
  16. // This class is the context menu controller used by AppsGridView, responsible
  17. // for building, running the menu and executing the commands.
  18. class ASH_EXPORT AppsGridContextMenu : public ui::SimpleMenuModel::Delegate,
  19. public views::ContextMenuController {
  20. public:
  21. AppsGridContextMenu();
  22. AppsGridContextMenu(const AppsGridContextMenu&) = delete;
  23. AppsGridContextMenu& operator=(const AppsGridContextMenu&) = delete;
  24. ~AppsGridContextMenu() override;
  25. // Returns true if the apps grid context menu is showing.
  26. bool IsMenuShowing() const;
  27. // Closes the context menu if it's showning.
  28. void Cancel();
  29. // ui::SimpleMenuModel::Delegate:
  30. void ExecuteCommand(int command_id, int event_flags) override;
  31. void set_owner_touch_dragging(bool touch_dragging) {
  32. owner_touch_dragging_ = touch_dragging;
  33. }
  34. views::MenuItemView* root_menu_item_view() const {
  35. return root_menu_item_view_;
  36. }
  37. private:
  38. // views::ContextMenuController:
  39. void ShowContextMenuForViewImpl(views::View* source,
  40. const gfx::Point& point,
  41. ui::MenuSourceType source_type) override;
  42. // Builds and saves a SimpleMenuModel to `context_menu_model_`;
  43. void BuildMenuModel();
  44. // Called when the context menu is closed. Used as a callback for
  45. // `menu_model_adapter_`.
  46. void OnMenuClosed();
  47. // The context menu model and its adapter for AppsGridView.
  48. std::unique_ptr<ui::SimpleMenuModel> context_menu_model_;
  49. std::unique_ptr<views::MenuModelAdapter> menu_model_adapter_;
  50. // The menu runner that is responsible to run the menu.
  51. std::unique_ptr<views::MenuRunner> menu_runner_;
  52. // The root menu item view of `context_menu_model_`. Cached for testing.
  53. views::MenuItemView* root_menu_item_view_ = nullptr;
  54. // Whether the owner view is currently touch dragging, in which case touch
  55. // events will be forwarded from the context menu to the owner view (so the
  56. // view can transition from showing a context menu to item drag).
  57. bool owner_touch_dragging_ = false;
  58. };
  59. } // namespace ash
  60. #endif // ASH_APP_LIST_VIEWS_APPS_GRID_CONTEXT_MENU_H_