app_menu_model_adapter.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2018 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_MENU_APP_MENU_MODEL_ADAPTER_H_
  5. #define ASH_APP_MENU_APP_MENU_MODEL_ADAPTER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "ash/app_menu/app_menu_export.h"
  9. #include "base/callback.h"
  10. #include "base/time/time.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. #include "ui/views/controls/menu/menu_model_adapter.h"
  13. #include "ui/views/controls/menu/menu_types.h"
  14. namespace ui {
  15. class SimpleMenuModel;
  16. }
  17. namespace views {
  18. class MenuItemView;
  19. class MenuRunner;
  20. class Widget;
  21. } // namespace views
  22. namespace ash {
  23. class NotificationMenuController;
  24. class APP_MENU_EXPORT AppMenuModelAdapter : public views::MenuModelAdapter {
  25. public:
  26. AppMenuModelAdapter(const std::string& app_id,
  27. std::unique_ptr<ui::SimpleMenuModel> model,
  28. views::Widget* widget_owner,
  29. ui::MenuSourceType source_type,
  30. base::OnceClosure on_menu_closed_callback,
  31. bool is_tablet_mode);
  32. AppMenuModelAdapter(const AppMenuModelAdapter&) = delete;
  33. AppMenuModelAdapter& operator=(const AppMenuModelAdapter&) = delete;
  34. ~AppMenuModelAdapter() override;
  35. // Builds the view tree and shows the menu.
  36. void Run(const gfx::Rect& menu_anchor_rect,
  37. views::MenuAnchorPosition menu_anchor_position,
  38. int run_types);
  39. // Whether this is showing a menu.
  40. bool IsShowingMenu() const;
  41. // Closes the menu if one is being shown.
  42. void Cancel();
  43. // Gives subclasses a chance to map |command_id| to a certain range to avoid
  44. // conflicts with other command IDs. See app_menu_constants.h.
  45. virtual int GetCommandIdForHistograms(int command_id);
  46. base::TimeTicks GetClosingEventTime();
  47. // Gets the widget associated with the submenu. May return nullptr.
  48. views::Widget* GetSubmenuWidget();
  49. // views::MenuModelAdapter:
  50. void ExecuteCommand(int id, int mouse_event_flags) override;
  51. void OnMenuClosed(views::MenuItemView* menu) override;
  52. ui::SimpleMenuModel* model() { return model_.get(); }
  53. views::MenuItemView* root_for_testing() { return root_; }
  54. protected:
  55. const std::string& app_id() const { return app_id_; }
  56. base::TimeTicks menu_open_time() const { return menu_open_time_; }
  57. ui::MenuSourceType source_type() const { return source_type_; }
  58. bool is_tablet_mode() const { return is_tablet_mode_; }
  59. // Helper method to record ExecuteCommand() histograms.
  60. void RecordExecuteCommandHistogram(int command_id);
  61. // Records histograms on menu closed, such as the user journey time and show
  62. // source histograms.
  63. virtual void RecordHistogramOnMenuClosed() = 0;
  64. private:
  65. // The application identifier used to fetch active notifications to display.
  66. const std::string app_id_;
  67. // The list of items which will be shown in the menu.
  68. std::unique_ptr<ui::SimpleMenuModel> model_;
  69. // Responsible for adding the container MenuItemView to the parent
  70. // MenuItemView, and adding NOTIFICATION_CONTAINER to the model.
  71. std::unique_ptr<NotificationMenuController> notification_menu_controller_;
  72. // The parent widget of the context menu.
  73. views::Widget* widget_owner_;
  74. // The event type which was used to show the menu.
  75. const ui::MenuSourceType source_type_;
  76. // The callback which is triggered when the menu is closed.
  77. base::OnceClosure on_menu_closed_callback_;
  78. // The root MenuItemView which contains all child MenuItemViews. Owned by
  79. // |menu_runner_|.
  80. views::MenuItemView* root_ = nullptr;
  81. // Used to show the menu.
  82. std::unique_ptr<views::MenuRunner> menu_runner_;
  83. // The timestamp taken when the menu is opened. Used in metrics.
  84. base::TimeTicks menu_open_time_;
  85. // Whether tablet mode is active.
  86. bool is_tablet_mode_;
  87. };
  88. } // namespace ash
  89. #endif // ASH_APP_MENU_APP_MENU_MODEL_ADAPTER_H_