1 // Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights 2 // reserved. Use of this source code is governed by a BSD-style license that 3 // can be found in the LICENSE file. 4 5 #ifndef CEF_TESTS_CEFCLIENT_BROWSER_VIEWS_MENU_BAR_H_ 6 #define CEF_TESTS_CEFCLIENT_BROWSER_VIEWS_MENU_BAR_H_ 7 #pragma once 8 9 #include <map> 10 #include <string> 11 #include <vector> 12 13 #include "include/cef_menu_model.h" 14 #include "include/cef_menu_model_delegate.h" 15 #include "include/views/cef_menu_button.h" 16 #include "include/views/cef_menu_button_delegate.h" 17 #include "include/views/cef_panel.h" 18 19 namespace client { 20 21 // Implements a menu bar which is composed of CefMenuButtons positioned in a 22 // row with automatic switching between them via mouse/keyboard. All methods 23 // must be called on the browser process UI thread. 24 class ViewsMenuBar : public CefMenuButtonDelegate, public CefMenuModelDelegate { 25 public: 26 // Delegate methods will be called on the browser process UI thread. 27 class Delegate { 28 public: 29 // Called when a menu command is selected. 30 virtual void MenuBarExecuteCommand(CefRefPtr<CefMenuModel> menu_model, 31 int command_id, 32 cef_event_flags_t event_flags) = 0; 33 34 protected: ~Delegate()35 virtual ~Delegate() {} 36 }; 37 38 // |delegate| must outlive this object. 39 // |menu_id_start| is the ID for the first CefMenuButton in the bar. An ID 40 // range starting with |menu_id_start| and extending for a reasonable distance 41 // should be reserved in the client for MenuBar usage. 42 ViewsMenuBar(Delegate* delegate, int menu_id_start); 43 44 // Returns true if |menu_id| exists in the menu bar. 45 bool HasMenuId(int menu_id) const; 46 47 // Returns the CefPanel that represents the menu bar. 48 CefRefPtr<CefPanel> GetMenuPanel(); 49 50 // Create a new menu with the specified |label|. If |menu_id| is non-nullptr 51 // it will be populated with the new menu ID. 52 CefRefPtr<CefMenuModel> CreateMenuModel(const CefString& label, int* menu_id); 53 54 // Returns the menu with the specified |menu_id|, or nullptr if no such menu 55 // exists. 56 CefRefPtr<CefMenuModel> GetMenuModel(int menu_id) const; 57 58 // Assign or remove focus from the menu bar. 59 // Focus is assigned to the menu bar by ViewsWindow::OnKeyEvent when the ALT 60 // key is pressed. Focus is removed from the menu bar by ViewsWindow::OnFocus 61 // when a control not in the menu bar gains focus. 62 void SetMenuFocusable(bool focusable); 63 64 // Key events forwarded from ViewsWindow::OnKeyEvent when the menu bar has 65 // focus. 66 bool OnKeyEvent(const CefKeyEvent& event); 67 68 // Reset menu bar state. 69 void Reset(); 70 71 protected: 72 // CefButtonDelegate methods: OnButtonPressed(CefRefPtr<CefButton> button)73 void OnButtonPressed(CefRefPtr<CefButton> button) override {} 74 75 // CefMenuButtonDelegate methods: 76 void OnMenuButtonPressed( 77 CefRefPtr<CefMenuButton> menu_button, 78 const CefPoint& screen_point, 79 CefRefPtr<CefMenuButtonPressedLock> button_pressed_lock) override; 80 81 // CefMenuModelDelegate methods: 82 void ExecuteCommand(CefRefPtr<CefMenuModel> menu_model, 83 int command_id, 84 cef_event_flags_t event_flags) override; 85 void MouseOutsideMenu(CefRefPtr<CefMenuModel> menu_model, 86 const CefPoint& screen_point) override; 87 void UnhandledOpenSubmenu(CefRefPtr<CefMenuModel> menu_model, 88 bool is_rtl) override; 89 void UnhandledCloseSubmenu(CefRefPtr<CefMenuModel> menu_model, 90 bool is_rtl) override; 91 void MenuClosed(CefRefPtr<CefMenuModel> menu_model) override; 92 93 private: 94 // Creates the menu panel if it doesn't already exist. 95 void EnsureMenuPanel(); 96 97 // Returns the ID for the currently active menu, or -1 if no menu is currently 98 // active. 99 int GetActiveMenuId(); 100 101 // Triggers the menu at the specified |offset| from the currently active menu. 102 void TriggerNextMenu(int offset); 103 104 // Triggers the specified MenuButton |button|. 105 void TriggerMenuButton(CefRefPtr<CefView> button); 106 107 Delegate* delegate_; // Not owned by this object. 108 const int id_start_; 109 int id_next_; 110 CefRefPtr<CefPanel> panel_; 111 std::vector<CefRefPtr<CefMenuModel>> models_; 112 bool last_nav_with_keyboard_; 113 114 // Map of mnemonic to MenuButton ID. 115 typedef std::map<char16, int> MnemonicMap; 116 MnemonicMap mnemonics_; 117 118 IMPLEMENT_REFCOUNTING(ViewsMenuBar); 119 DISALLOW_COPY_AND_ASSIGN(ViewsMenuBar); 120 }; 121 122 } // namespace client 123 124 #endif // CEF_TESTS_CEFCLIENT_BROWSER_VIEWS_MENU_BAR_H_ 125