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 #include "tests/cefclient/browser/views_menu_bar.h"
6
7 #include "include/views/cef_box_layout.h"
8 #include "include/views/cef_window.h"
9 #include "tests/cefclient/browser/views_style.h"
10
11 namespace client {
12
13 namespace {
14
15 const int kMenuBarGroupId = 100;
16
17 // Convert |c| to lowercase using the current ICU locale.
18 // TODO(jshin): What about Turkish locale? See http://crbug.com/81719.
19 // If the mnemonic is capital I and the UI language is Turkish, lowercasing it
20 // results in 'small dotless i', which is different from a 'dotted i'. Similar
21 // issues may exist for az and lt locales.
ToLower(char16 c)22 char16 ToLower(char16 c) {
23 CefStringUTF16 str16;
24 cef_string_utf16_to_lower(&c, 1, str16.GetWritableStruct());
25 return str16.length() > 0 ? str16.c_str()[0] : 0;
26 }
27
28 // Extract the mnemonic character from |title|. For example, if |title| is
29 // "&Test" then the mnemonic character is 'T'.
GetMnemonic(const std::u16string & title)30 char16 GetMnemonic(const std::u16string& title) {
31 size_t index = 0;
32 do {
33 index = title.find('&', index);
34 if (index != std::u16string::npos) {
35 if (index + 1 != title.size() && title[index + 1] != '&')
36 return ToLower(title[index + 1]);
37 index++;
38 }
39 } while (index != std::u16string::npos);
40 return 0;
41 }
42
43 } // namespace
44
ViewsMenuBar(Delegate * delegate,int menu_id_start)45 ViewsMenuBar::ViewsMenuBar(Delegate* delegate, int menu_id_start)
46 : delegate_(delegate),
47 id_start_(menu_id_start),
48 id_next_(menu_id_start),
49 last_nav_with_keyboard_(false) {
50 DCHECK(delegate_);
51 DCHECK_GT(id_start_, 0);
52 }
53
HasMenuId(int menu_id) const54 bool ViewsMenuBar::HasMenuId(int menu_id) const {
55 return menu_id >= id_start_ && menu_id < id_next_;
56 }
57
GetMenuPanel()58 CefRefPtr<CefPanel> ViewsMenuBar::GetMenuPanel() {
59 EnsureMenuPanel();
60 return panel_;
61 }
62
CreateMenuModel(const CefString & label,int * menu_id)63 CefRefPtr<CefMenuModel> ViewsMenuBar::CreateMenuModel(const CefString& label,
64 int* menu_id) {
65 EnsureMenuPanel();
66
67 // Assign the new menu ID.
68 const int new_menu_id = id_next_++;
69 if (menu_id)
70 *menu_id = new_menu_id;
71
72 // Create the new MenuModel.
73 CefRefPtr<CefMenuModel> model = CefMenuModel::CreateMenuModel(this);
74 views_style::ApplyTo(model);
75 models_.push_back(model);
76
77 // Create the new MenuButton.
78 CefRefPtr<CefMenuButton> button =
79 CefMenuButton::CreateMenuButton(this, label);
80 button->SetID(new_menu_id);
81 views_style::ApplyTo(button.get());
82 button->SetInkDropEnabled(true);
83
84 // Assign a group ID to allow focus traversal between MenuButtons using the
85 // arrow keys when the menu is not displayed.
86 button->SetGroupID(kMenuBarGroupId);
87
88 // Add the new MenuButton to the Planel.
89 panel_->AddChildView(button);
90
91 // Extract the mnemonic that triggers the menu, if any.
92 char16 mnemonic = GetMnemonic(label);
93 if (mnemonic != 0)
94 mnemonics_.insert(std::make_pair(mnemonic, new_menu_id));
95
96 return model;
97 }
98
GetMenuModel(int menu_id) const99 CefRefPtr<CefMenuModel> ViewsMenuBar::GetMenuModel(int menu_id) const {
100 if (HasMenuId(menu_id))
101 return models_[menu_id - id_start_];
102 return nullptr;
103 }
104
SetMenuFocusable(bool focusable)105 void ViewsMenuBar::SetMenuFocusable(bool focusable) {
106 if (!panel_)
107 return;
108
109 for (int id = id_start_; id < id_next_; ++id)
110 panel_->GetViewForID(id)->SetFocusable(focusable);
111
112 if (focusable) {
113 // Give focus to the first MenuButton.
114 panel_->GetViewForID(id_start_)->RequestFocus();
115 }
116 }
117
OnKeyEvent(const CefKeyEvent & event)118 bool ViewsMenuBar::OnKeyEvent(const CefKeyEvent& event) {
119 if (!panel_)
120 return false;
121
122 if (event.type != KEYEVENT_RAWKEYDOWN)
123 return false;
124
125 // Do not check mnemonics if the Alt or Ctrl modifiers are pressed. For
126 // example Ctrl+<T> is an accelerator, but <T> only is a mnemonic.
127 if (event.modifiers & (EVENTFLAG_ALT_DOWN | EVENTFLAG_CONTROL_DOWN))
128 return false;
129
130 MnemonicMap::const_iterator it = mnemonics_.find(ToLower(event.character));
131 if (it == mnemonics_.end())
132 return false;
133
134 // Set status indicating that we navigated using the keyboard.
135 last_nav_with_keyboard_ = true;
136
137 // Show the selected menu.
138 TriggerMenuButton(panel_->GetViewForID(it->second));
139
140 return true;
141 }
142
Reset()143 void ViewsMenuBar::Reset() {
144 panel_ = nullptr;
145 models_.clear();
146 mnemonics_.clear();
147 id_next_ = id_start_;
148 }
149
OnMenuButtonPressed(CefRefPtr<CefMenuButton> menu_button,const CefPoint & screen_point,CefRefPtr<CefMenuButtonPressedLock> button_pressed_lock)150 void ViewsMenuBar::OnMenuButtonPressed(
151 CefRefPtr<CefMenuButton> menu_button,
152 const CefPoint& screen_point,
153 CefRefPtr<CefMenuButtonPressedLock> button_pressed_lock) {
154 CefRefPtr<CefMenuModel> menu_model = GetMenuModel(menu_button->GetID());
155
156 // Adjust menu position left by button width.
157 CefPoint point = screen_point;
158 point.x -= menu_button->GetBounds().width - 4;
159
160 // Keep track of the current |last_nav_with_keyboard_| status and restore it
161 // after displaying the new menu.
162 bool cur_last_nav_with_keyboard = last_nav_with_keyboard_;
163
164 // May result in the previous menu being closed, in which case MenuClosed will
165 // be called before the new menu is displayed.
166 menu_button->ShowMenu(menu_model, point, CEF_MENU_ANCHOR_TOPLEFT);
167
168 last_nav_with_keyboard_ = cur_last_nav_with_keyboard;
169 }
170
ExecuteCommand(CefRefPtr<CefMenuModel> menu_model,int command_id,cef_event_flags_t event_flags)171 void ViewsMenuBar::ExecuteCommand(CefRefPtr<CefMenuModel> menu_model,
172 int command_id,
173 cef_event_flags_t event_flags) {
174 delegate_->MenuBarExecuteCommand(menu_model, command_id, event_flags);
175 }
176
MouseOutsideMenu(CefRefPtr<CefMenuModel> menu_model,const CefPoint & screen_point)177 void ViewsMenuBar::MouseOutsideMenu(CefRefPtr<CefMenuModel> menu_model,
178 const CefPoint& screen_point) {
179 DCHECK(panel_);
180
181 // Retrieve the Window hosting the Panel.
182 CefRefPtr<CefWindow> window = panel_->GetWindow();
183 DCHECK(window);
184
185 // Convert the point from screen to window coordinates.
186 CefPoint window_point = screen_point;
187 if (!window->ConvertPointFromScreen(window_point))
188 return;
189
190 CefRect panel_bounds = panel_->GetBounds();
191
192 if (last_nav_with_keyboard_) {
193 // The user navigated last using the keyboard. Don't change menus using
194 // mouse movements until the mouse exits and re-enters the Panel.
195 if (panel_bounds.Contains(window_point))
196 return;
197 last_nav_with_keyboard_ = false;
198 }
199
200 // Check that the point is inside the Panel.
201 if (!panel_bounds.Contains(window_point))
202 return;
203
204 const int active_menu_id = GetActiveMenuId();
205
206 // Determine which MenuButton is under the specified point.
207 for (int id = id_start_; id < id_next_; ++id) {
208 // Skip the currently active MenuButton.
209 if (id == active_menu_id)
210 continue;
211
212 CefRefPtr<CefView> button = panel_->GetViewForID(id);
213 CefRect button_bounds = button->GetBounds();
214 if (button_bounds.Contains(window_point)) {
215 // Trigger the hovered MenuButton.
216 TriggerMenuButton(button);
217 break;
218 }
219 }
220 }
221
UnhandledOpenSubmenu(CefRefPtr<CefMenuModel> menu_model,bool is_rtl)222 void ViewsMenuBar::UnhandledOpenSubmenu(CefRefPtr<CefMenuModel> menu_model,
223 bool is_rtl) {
224 TriggerNextMenu(is_rtl ? 1 : -1);
225 }
226
UnhandledCloseSubmenu(CefRefPtr<CefMenuModel> menu_model,bool is_rtl)227 void ViewsMenuBar::UnhandledCloseSubmenu(CefRefPtr<CefMenuModel> menu_model,
228 bool is_rtl) {
229 TriggerNextMenu(is_rtl ? -1 : 1);
230 }
231
MenuClosed(CefRefPtr<CefMenuModel> menu_model)232 void ViewsMenuBar::MenuClosed(CefRefPtr<CefMenuModel> menu_model) {
233 // Reset |last_nav_with_keyboard_| status whenever the main menu closes.
234 if (!menu_model->IsSubMenu() && last_nav_with_keyboard_)
235 last_nav_with_keyboard_ = false;
236 }
237
EnsureMenuPanel()238 void ViewsMenuBar::EnsureMenuPanel() {
239 if (panel_)
240 return;
241
242 panel_ = CefPanel::CreatePanel(nullptr);
243 views_style::ApplyTo(panel_);
244
245 // Use a horizontal box layout.
246 CefBoxLayoutSettings top_panel_layout_settings;
247 top_panel_layout_settings.horizontal = true;
248 panel_->SetToBoxLayout(top_panel_layout_settings);
249 }
250
GetActiveMenuId()251 int ViewsMenuBar::GetActiveMenuId() {
252 DCHECK(panel_);
253
254 for (int id = id_start_; id < id_next_; ++id) {
255 CefRefPtr<CefButton> button = panel_->GetViewForID(id)->AsButton();
256 if (button->GetState() == CEF_BUTTON_STATE_PRESSED)
257 return id;
258 }
259
260 return -1;
261 }
262
TriggerNextMenu(int offset)263 void ViewsMenuBar::TriggerNextMenu(int offset) {
264 DCHECK(panel_);
265
266 const int active_menu_id = GetActiveMenuId();
267 const int menu_count = id_next_ - id_start_;
268 const int active_menu_index = active_menu_id - id_start_;
269
270 // Compute the modulus to avoid negative values.
271 int next_menu_index = (active_menu_index + offset) % menu_count;
272 if (next_menu_index < 0)
273 next_menu_index += menu_count;
274
275 // Cancel the existing menu. MenuClosed may be called.
276 panel_->GetWindow()->CancelMenu();
277
278 // Set status indicating that we navigated using the keyboard.
279 last_nav_with_keyboard_ = true;
280
281 // Show the new menu.
282 TriggerMenuButton(panel_->GetViewForID(id_start_ + next_menu_index));
283 }
284
TriggerMenuButton(CefRefPtr<CefView> button)285 void ViewsMenuBar::TriggerMenuButton(CefRefPtr<CefView> button) {
286 CefRefPtr<CefMenuButton> menu_button =
287 button->AsButton()->AsLabelButton()->AsMenuButton();
288 if (menu_button->IsFocusable())
289 menu_button->RequestFocus();
290 menu_button->TriggerMenu();
291 }
292
293 } // namespace client
294