1 // Copyright (c) 2021 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_SIMPLE_MENU_MODEL_IMPL_H_ 7 #define CEF_LIBCEF_BROWSER_SIMPLE_MENU_MODEL_IMPL_H_ 8 #pragma once 9 10 #include <vector> 11 12 #include "include/cef_menu_model.h" 13 14 #include "base/threading/platform_thread.h" 15 #include "ui/base/models/simple_menu_model.h" 16 17 // Implementation of CefMenuModel that wraps an existing ui::SimpleMenuModel. 18 class CefSimpleMenuModelImpl : public CefMenuModel { 19 public: 20 // Interface for setting state using CefMenuModel methods that will later be 21 // retrieved via the ui::SimpleMenuModel::Delegate implementation. 22 class StateDelegate { 23 public: 24 virtual void SetChecked(int command_id, bool checked) = 0; 25 virtual void SetAccelerator(int command_id, 26 absl::optional<ui::Accelerator> accel) = 0; 27 28 protected: ~StateDelegate()29 virtual ~StateDelegate() {} 30 }; 31 32 // |delegate| should be the same that was used to create |model|. 33 // If |is_owned| is true then |model| will be deleted on Detach(). 34 CefSimpleMenuModelImpl(ui::SimpleMenuModel* model, 35 ui::SimpleMenuModel::Delegate* delegate, 36 StateDelegate* state_delegate, 37 bool is_owned, 38 bool is_submenu); 39 40 CefSimpleMenuModelImpl(const CefSimpleMenuModelImpl&) = delete; 41 CefSimpleMenuModelImpl& operator=(const CefSimpleMenuModelImpl&) = delete; 42 43 ~CefSimpleMenuModelImpl() override; 44 45 // Must be called before the object is deleted. 46 void Detach(); 47 48 // CefMenuModel methods. 49 bool IsSubMenu() override; 50 bool Clear() override; 51 int GetCount() override; 52 bool AddSeparator() override; 53 bool AddItem(int command_id, const CefString& label) override; 54 bool AddCheckItem(int command_id, const CefString& label) override; 55 bool AddRadioItem(int command_id, 56 const CefString& label, 57 int group_id) override; 58 CefRefPtr<CefMenuModel> AddSubMenu(int command_id, 59 const CefString& label) override; 60 bool InsertSeparatorAt(int index) override; 61 bool InsertItemAt(int index, int command_id, const CefString& label) override; 62 bool InsertCheckItemAt(int index, 63 int command_id, 64 const CefString& label) override; 65 bool InsertRadioItemAt(int index, 66 int command_id, 67 const CefString& label, 68 int group_id) override; 69 CefRefPtr<CefMenuModel> InsertSubMenuAt(int index, 70 int command_id, 71 const CefString& label) override; 72 bool Remove(int command_id) override; 73 bool RemoveAt(int index) override; 74 int GetIndexOf(int command_id) override; 75 int GetCommandIdAt(int index) override; 76 bool SetCommandIdAt(int index, int command_id) override; 77 CefString GetLabel(int command_id) override; 78 CefString GetLabelAt(int index) override; 79 bool SetLabel(int command_id, const CefString& label) override; 80 bool SetLabelAt(int index, const CefString& label) override; 81 MenuItemType GetType(int command_id) override; 82 MenuItemType GetTypeAt(int index) override; 83 int GetGroupId(int command_id) override; 84 int GetGroupIdAt(int index) override; 85 bool SetGroupId(int command_id, int group_id) override; 86 bool SetGroupIdAt(int index, int group_id) override; 87 CefRefPtr<CefMenuModel> GetSubMenu(int command_id) override; 88 CefRefPtr<CefMenuModel> GetSubMenuAt(int index) override; 89 bool IsVisible(int command_id) override; 90 bool IsVisibleAt(int index) override; 91 bool SetVisible(int command_id, bool visible) override; 92 bool SetVisibleAt(int index, bool visible) override; 93 bool IsEnabled(int command_id) override; 94 bool IsEnabledAt(int index) override; 95 bool SetEnabled(int command_id, bool enabled) override; 96 bool SetEnabledAt(int index, bool enabled) override; 97 bool IsChecked(int command_id) override; 98 bool IsCheckedAt(int index) override; 99 bool SetChecked(int command_id, bool checked) override; 100 bool SetCheckedAt(int index, bool checked) override; 101 bool HasAccelerator(int command_id) override; 102 bool HasAcceleratorAt(int index) override; 103 bool SetAccelerator(int command_id, 104 int key_code, 105 bool shift_pressed, 106 bool ctrl_pressed, 107 bool alt_pressed) override; 108 bool SetAcceleratorAt(int index, 109 int key_code, 110 bool shift_pressed, 111 bool ctrl_pressed, 112 bool alt_pressed) override; 113 bool RemoveAccelerator(int command_id) override; 114 bool RemoveAcceleratorAt(int index) override; 115 bool GetAccelerator(int command_id, 116 int& key_code, 117 bool& shift_pressed, 118 bool& ctrl_pressed, 119 bool& alt_pressed) override; 120 bool GetAcceleratorAt(int index, 121 int& key_code, 122 bool& shift_pressed, 123 bool& ctrl_pressed, 124 bool& alt_pressed) override; 125 bool SetColor(int command_id, 126 cef_menu_color_type_t color_type, 127 cef_color_t color) override; 128 bool SetColorAt(int index, 129 cef_menu_color_type_t color_type, 130 cef_color_t color) override; 131 bool GetColor(int command_id, 132 cef_menu_color_type_t color_type, 133 cef_color_t& color) override; 134 bool GetColorAt(int index, 135 cef_menu_color_type_t color_type, 136 cef_color_t& color) override; 137 bool SetFontList(int command_id, const CefString& font_list) override; 138 bool SetFontListAt(int index, const CefString& font_list) override; 139 model()140 ui::SimpleMenuModel* model() const { return model_; } 141 142 private: 143 // Verify that the object is attached and being accessed from the correct 144 // thread. 145 bool VerifyContext(); 146 147 // Returns true if |index| is valid. 148 bool ValidIndex(int index); 149 150 CefRefPtr<CefSimpleMenuModelImpl> CreateNewSubMenu( 151 ui::SimpleMenuModel* model); 152 153 base::PlatformThreadId supported_thread_id_; 154 155 ui::SimpleMenuModel* model_; 156 ui::SimpleMenuModel::Delegate* const delegate_; 157 StateDelegate* const state_delegate_; 158 const bool is_owned_; 159 const bool is_submenu_; 160 161 // Keep the submenus alive until they're removed, or we're destroyed. 162 using SubMenuMap = 163 std::map<ui::SimpleMenuModel*, CefRefPtr<CefSimpleMenuModelImpl>>; 164 SubMenuMap submenumap_; 165 166 IMPLEMENT_REFCOUNTING(CefSimpleMenuModelImpl); 167 }; 168 169 #endif // CEF_LIBCEF_BROWSER_SIMPLE_MENU_MODEL_IMPL_H_ 170