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