• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Embedded Framework Authors.
2 // Portions copyright (c) 2012 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 
6 #ifndef CEF_LIBCEF_BROWSER_MENU_MODEL_IMPL_H_
7 #define CEF_LIBCEF_BROWSER_MENU_MODEL_IMPL_H_
8 #pragma once
9 
10 #include <vector>
11 
12 #include "include/cef_menu_model.h"
13 #include "include/cef_menu_model_delegate.h"
14 
15 #include "base/threading/platform_thread.h"
16 #include "third_party/blink/public/mojom/context_menu/context_menu.mojom-forward.h"
17 #include "ui/base/models/menu_model.h"
18 #include "ui/gfx/font_list.h"
19 
20 class CefMenuModelImpl : public CefMenuModel {
21  public:
22   class Delegate {
23    public:
24     // Perform the action associated with the specified |command_id| and
25     // optional |event_flags|.
26     virtual void ExecuteCommand(CefRefPtr<CefMenuModelImpl> source,
27                                 int command_id,
28                                 cef_event_flags_t event_flags) = 0;
29 
30     // Called when the user moves the mouse outside the menu and over the owning
31     // window.
MouseOutsideMenu(CefRefPtr<CefMenuModelImpl> source,const gfx::Point & screen_point)32     virtual void MouseOutsideMenu(CefRefPtr<CefMenuModelImpl> source,
33                                   const gfx::Point& screen_point) {}
34 
35     // Called on unhandled open/close submenu keyboard commands. |is_rtl| will
36     // be true if the menu is displaying a right-to-left language.
UnhandledOpenSubmenu(CefRefPtr<CefMenuModelImpl> source,bool is_rtl)37     virtual void UnhandledOpenSubmenu(CefRefPtr<CefMenuModelImpl> source,
38                                       bool is_rtl) {}
UnhandledCloseSubmenu(CefRefPtr<CefMenuModelImpl> source,bool is_rtl)39     virtual void UnhandledCloseSubmenu(CefRefPtr<CefMenuModelImpl> source,
40                                        bool is_rtl) {}
41 
42     // Called when the menu is about to show.
43     virtual void MenuWillShow(CefRefPtr<CefMenuModelImpl> source) = 0;
44 
45     // Called when the menu has closed.
46     virtual void MenuClosed(CefRefPtr<CefMenuModelImpl> source) = 0;
47 
48     // Allows the delegate to modify a menu item label before it's displayed.
49     virtual bool FormatLabel(CefRefPtr<CefMenuModelImpl> source,
50                              std::u16string& label) = 0;
51 
52    protected:
~Delegate()53     virtual ~Delegate() {}
54   };
55 
56   // Either |delegate| or |menu_model_delegate| must be non-nullptr.
57   // If |delegate| is non-nullptr it must outlive this class.
58   CefMenuModelImpl(Delegate* delegate,
59                    CefRefPtr<CefMenuModelDelegate> menu_model_delegate,
60                    bool is_submenu);
61 
62   CefMenuModelImpl(const CefMenuModelImpl&) = delete;
63   CefMenuModelImpl& operator=(const CefMenuModelImpl&) = delete;
64 
65   ~CefMenuModelImpl() override;
66 
67   // CefMenuModel methods.
68   bool IsSubMenu() override;
69   bool Clear() override;
70   int GetCount() override;
71   bool AddSeparator() override;
72   bool AddItem(int command_id, const CefString& label) override;
73   bool AddCheckItem(int command_id, const CefString& label) override;
74   bool AddRadioItem(int command_id,
75                     const CefString& label,
76                     int group_id) override;
77   CefRefPtr<CefMenuModel> AddSubMenu(int command_id,
78                                      const CefString& label) override;
79   bool InsertSeparatorAt(int index) override;
80   bool InsertItemAt(int index, int command_id, const CefString& label) override;
81   bool InsertCheckItemAt(int index,
82                          int command_id,
83                          const CefString& label) override;
84   bool InsertRadioItemAt(int index,
85                          int command_id,
86                          const CefString& label,
87                          int group_id) override;
88   CefRefPtr<CefMenuModel> InsertSubMenuAt(int index,
89                                           int command_id,
90                                           const CefString& label) override;
91   bool Remove(int command_id) override;
92   bool RemoveAt(int index) override;
93   int GetIndexOf(int command_id) override;
94   int GetCommandIdAt(int index) override;
95   bool SetCommandIdAt(int index, int command_id) override;
96   CefString GetLabel(int command_id) override;
97   CefString GetLabelAt(int index) override;
98   bool SetLabel(int command_id, const CefString& label) override;
99   bool SetLabelAt(int index, const CefString& label) override;
100   MenuItemType GetType(int command_id) override;
101   MenuItemType GetTypeAt(int index) override;
102   int GetGroupId(int command_id) override;
103   int GetGroupIdAt(int index) override;
104   bool SetGroupId(int command_id, int group_id) override;
105   bool SetGroupIdAt(int index, int group_id) override;
106   CefRefPtr<CefMenuModel> GetSubMenu(int command_id) override;
107   CefRefPtr<CefMenuModel> GetSubMenuAt(int index) override;
108   bool IsVisible(int command_id) override;
109   bool IsVisibleAt(int index) override;
110   bool SetVisible(int command_id, bool visible) override;
111   bool SetVisibleAt(int index, bool visible) override;
112   bool IsEnabled(int command_id) override;
113   bool IsEnabledAt(int index) override;
114   bool SetEnabled(int command_id, bool enabled) override;
115   bool SetEnabledAt(int index, bool enabled) override;
116   bool IsChecked(int command_id) override;
117   bool IsCheckedAt(int index) override;
118   bool SetChecked(int command_id, bool checked) override;
119   bool SetCheckedAt(int index, bool checked) override;
120   bool HasAccelerator(int command_id) override;
121   bool HasAcceleratorAt(int index) override;
122   bool SetAccelerator(int command_id,
123                       int key_code,
124                       bool shift_pressed,
125                       bool ctrl_pressed,
126                       bool alt_pressed) override;
127   bool SetAcceleratorAt(int index,
128                         int key_code,
129                         bool shift_pressed,
130                         bool ctrl_pressed,
131                         bool alt_pressed) override;
132   bool RemoveAccelerator(int command_id) override;
133   bool RemoveAcceleratorAt(int index) override;
134   bool GetAccelerator(int command_id,
135                       int& key_code,
136                       bool& shift_pressed,
137                       bool& ctrl_pressed,
138                       bool& alt_pressed) override;
139   bool GetAcceleratorAt(int index,
140                         int& key_code,
141                         bool& shift_pressed,
142                         bool& ctrl_pressed,
143                         bool& alt_pressed) override;
144   bool SetColor(int command_id,
145                 cef_menu_color_type_t color_type,
146                 cef_color_t color) override;
147   bool SetColorAt(int index,
148                   cef_menu_color_type_t color_type,
149                   cef_color_t color) override;
150   bool GetColor(int command_id,
151                 cef_menu_color_type_t color_type,
152                 cef_color_t& color) override;
153   bool GetColorAt(int index,
154                   cef_menu_color_type_t color_type,
155                   cef_color_t& color) override;
156   bool SetFontList(int command_id, const CefString& font_list) override;
157   bool SetFontListAt(int index, const CefString& font_list) override;
158 
159   // Callbacks from the ui::MenuModel implementation.
160   void ActivatedAt(int index, cef_event_flags_t event_flags);
161   void MouseOutsideMenu(const gfx::Point& screen_point);
162   void UnhandledOpenSubmenu(bool is_rtl);
163   void UnhandledCloseSubmenu(bool is_rtl);
164   bool GetTextColor(int index,
165                     bool is_accelerator,
166                     bool is_hovered,
167                     SkColor* override_color) const;
168   bool GetBackgroundColor(int index,
169                           bool is_hovered,
170                           SkColor* override_color) const;
171   void MenuWillShow();
172   void MenuWillClose();
173   std::u16string GetFormattedLabelAt(int index);
174   const gfx::FontList* GetLabelFontListAt(int index) const;
175 
176   // Verify that only a single reference exists to all CefMenuModelImpl objects.
177   bool VerifyRefCount();
178 
179   // Helper for adding custom menu items originating from the renderer process.
180   void AddMenuItem(const blink::mojom::CustomContextMenuItem& menu_item);
181 
model()182   ui::MenuModel* model() const { return model_.get(); }
183 
184   // Used when created via CefMenuManager.
delegate()185   Delegate* delegate() const { return delegate_; }
set_delegate(Delegate * delegate)186   void set_delegate(Delegate* delegate) { delegate_ = delegate; }
187 
188   // Used for menus run via CefWindowImpl::ShowMenu to provide more accurate
189   // menu close notification.
set_auto_notify_menu_closed(bool val)190   void set_auto_notify_menu_closed(bool val) { auto_notify_menu_closed_ = val; }
191   void NotifyMenuClosed();
192 
193  private:
194   struct Item;
195 
196   using ItemVector = std::vector<Item>;
197 
198   // Functions for inserting items into |items_|.
199   void AppendItem(const Item& item);
200   void InsertItemAt(const Item& item, int index);
201   void ValidateItem(const Item& item);
202 
203   // Notify the delegate asynchronously.
204   void OnMouseOutsideMenu(const gfx::Point& screen_point);
205   void OnUnhandledOpenSubmenu(bool is_rtl);
206   void OnUnhandledCloseSubmenu(bool is_rtl);
207   void OnMenuClosed();
208 
209   // Verify that the object is being accessed from the correct thread.
210   bool VerifyContext();
211 
212   base::PlatformThreadId supported_thread_id_;
213 
214   // Used when created via CefMenuManager.
215   Delegate* delegate_;
216 
217   // Used when created via CefMenuModel::CreateMenuModel().
218   CefRefPtr<CefMenuModelDelegate> menu_model_delegate_;
219 
220   const bool is_submenu_;
221 
222   ItemVector items_;
223   std::unique_ptr<ui::MenuModel> model_;
224 
225   // Style information.
226   cef_color_t default_colors_[CEF_MENU_COLOR_COUNT] = {0};
227   gfx::FontList default_font_list_;
228   bool has_default_font_list_ = false;
229 
230   bool auto_notify_menu_closed_ = true;
231 
232   IMPLEMENT_REFCOUNTING(CefMenuModelImpl);
233 };
234 
235 #endif  // CEF_LIBCEF_BROWSER_MENU_MODEL_IMPL_H_
236