• 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   ~CefMenuModelImpl() override;
62 
63   // CefMenuModel methods.
64   bool IsSubMenu() override;
65   bool Clear() override;
66   int GetCount() override;
67   bool AddSeparator() override;
68   bool AddItem(int command_id, const CefString& label) override;
69   bool AddCheckItem(int command_id, const CefString& label) override;
70   bool AddRadioItem(int command_id,
71                     const CefString& label,
72                     int group_id) override;
73   CefRefPtr<CefMenuModel> AddSubMenu(int command_id,
74                                      const CefString& label) override;
75   bool InsertSeparatorAt(int index) override;
76   bool InsertItemAt(int index, int command_id, const CefString& label) override;
77   bool InsertCheckItemAt(int index,
78                          int command_id,
79                          const CefString& label) override;
80   bool InsertRadioItemAt(int index,
81                          int command_id,
82                          const CefString& label,
83                          int group_id) override;
84   CefRefPtr<CefMenuModel> InsertSubMenuAt(int index,
85                                           int command_id,
86                                           const CefString& label) override;
87   bool Remove(int command_id) override;
88   bool RemoveAt(int index) override;
89   int GetIndexOf(int command_id) override;
90   int GetCommandIdAt(int index) override;
91   bool SetCommandIdAt(int index, int command_id) override;
92   CefString GetLabel(int command_id) override;
93   CefString GetLabelAt(int index) override;
94   bool SetLabel(int command_id, const CefString& label) override;
95   bool SetLabelAt(int index, const CefString& label) override;
96   MenuItemType GetType(int command_id) override;
97   MenuItemType GetTypeAt(int index) override;
98   int GetGroupId(int command_id) override;
99   int GetGroupIdAt(int index) override;
100   bool SetGroupId(int command_id, int group_id) override;
101   bool SetGroupIdAt(int index, int group_id) override;
102   CefRefPtr<CefMenuModel> GetSubMenu(int command_id) override;
103   CefRefPtr<CefMenuModel> GetSubMenuAt(int index) override;
104   bool IsVisible(int command_id) override;
105   bool IsVisibleAt(int index) override;
106   bool SetVisible(int command_id, bool visible) override;
107   bool SetVisibleAt(int index, bool visible) override;
108   bool IsEnabled(int command_id) override;
109   bool IsEnabledAt(int index) override;
110   bool SetEnabled(int command_id, bool enabled) override;
111   bool SetEnabledAt(int index, bool enabled) override;
112   bool IsChecked(int command_id) override;
113   bool IsCheckedAt(int index) override;
114   bool SetChecked(int command_id, bool checked) override;
115   bool SetCheckedAt(int index, bool checked) override;
116   bool HasAccelerator(int command_id) override;
117   bool HasAcceleratorAt(int index) override;
118   bool SetAccelerator(int command_id,
119                       int key_code,
120                       bool shift_pressed,
121                       bool ctrl_pressed,
122                       bool alt_pressed) override;
123   bool SetAcceleratorAt(int index,
124                         int key_code,
125                         bool shift_pressed,
126                         bool ctrl_pressed,
127                         bool alt_pressed) override;
128   bool RemoveAccelerator(int command_id) override;
129   bool RemoveAcceleratorAt(int index) override;
130   bool GetAccelerator(int command_id,
131                       int& key_code,
132                       bool& shift_pressed,
133                       bool& ctrl_pressed,
134                       bool& alt_pressed) override;
135   bool GetAcceleratorAt(int index,
136                         int& key_code,
137                         bool& shift_pressed,
138                         bool& ctrl_pressed,
139                         bool& alt_pressed) override;
140   bool SetColor(int command_id,
141                 cef_menu_color_type_t color_type,
142                 cef_color_t color) override;
143   bool SetColorAt(int index,
144                   cef_menu_color_type_t color_type,
145                   cef_color_t color) override;
146   bool GetColor(int command_id,
147                 cef_menu_color_type_t color_type,
148                 cef_color_t& color) override;
149   bool GetColorAt(int index,
150                   cef_menu_color_type_t color_type,
151                   cef_color_t& color) override;
152   bool SetFontList(int command_id, const CefString& font_list) override;
153   bool SetFontListAt(int index, const CefString& font_list) override;
154 
155   // Callbacks from the ui::MenuModel implementation.
156   void ActivatedAt(int index, cef_event_flags_t event_flags);
157   void MouseOutsideMenu(const gfx::Point& screen_point);
158   void UnhandledOpenSubmenu(bool is_rtl);
159   void UnhandledCloseSubmenu(bool is_rtl);
160   bool GetTextColor(int index,
161                     bool is_accelerator,
162                     bool is_hovered,
163                     SkColor* override_color) const;
164   bool GetBackgroundColor(int index,
165                           bool is_hovered,
166                           SkColor* override_color) const;
167   void MenuWillShow();
168   void MenuWillClose();
169   std::u16string GetFormattedLabelAt(int index);
170   const gfx::FontList* GetLabelFontListAt(int index) const;
171 
172   // Verify that only a single reference exists to all CefMenuModelImpl objects.
173   bool VerifyRefCount();
174 
175   // Helper for adding custom menu items originating from the renderer process.
176   void AddMenuItem(const blink::mojom::CustomContextMenuItem& menu_item);
177 
model()178   ui::MenuModel* model() const { return model_.get(); }
179 
180   // Used when created via CefMenuManager.
delegate()181   Delegate* delegate() const { return delegate_; }
set_delegate(Delegate * delegate)182   void set_delegate(Delegate* delegate) { delegate_ = delegate; }
183 
184   // Used for menus run via CefWindowImpl::ShowMenu to provide more accurate
185   // menu close notification.
set_auto_notify_menu_closed(bool val)186   void set_auto_notify_menu_closed(bool val) { auto_notify_menu_closed_ = val; }
187   void NotifyMenuClosed();
188 
189  private:
190   struct Item;
191 
192   typedef std::vector<Item> ItemVector;
193 
194   // Functions for inserting items into |items_|.
195   void AppendItem(const Item& item);
196   void InsertItemAt(const Item& item, int index);
197   void ValidateItem(const Item& item);
198 
199   // Notify the delegate asynchronously.
200   void OnMouseOutsideMenu(const gfx::Point& screen_point);
201   void OnUnhandledOpenSubmenu(bool is_rtl);
202   void OnUnhandledCloseSubmenu(bool is_rtl);
203   void OnMenuClosed();
204 
205   // Verify that the object is being accessed from the correct thread.
206   bool VerifyContext();
207 
208   base::PlatformThreadId supported_thread_id_;
209 
210   // Used when created via CefMenuManager.
211   Delegate* delegate_;
212 
213   // Used when created via CefMenuModel::CreateMenuModel().
214   CefRefPtr<CefMenuModelDelegate> menu_model_delegate_;
215 
216   const bool is_submenu_;
217 
218   ItemVector items_;
219   std::unique_ptr<ui::MenuModel> model_;
220 
221   // Style information.
222   cef_color_t default_colors_[CEF_MENU_COLOR_COUNT] = {0};
223   gfx::FontList default_font_list_;
224   bool has_default_font_list_ = false;
225 
226   bool auto_notify_menu_closed_ = true;
227 
228   IMPLEMENT_REFCOUNTING(CefMenuModelImpl);
229   DISALLOW_COPY_AND_ASSIGN(CefMenuModelImpl);
230 };
231 
232 #endif  // CEF_LIBCEF_BROWSER_MENU_MODEL_IMPL_H_
233