views: Toggle the menu bar only when a single Alt is released.

This commit is contained in:
Cheng Zhao 2014-08-07 15:54:05 +08:00
parent 09f9d0729c
commit 5d5a3138bc
2 changed files with 29 additions and 8 deletions

View file

@ -71,6 +71,21 @@ bool ShouldUseGlobalMenuBar() {
}
#endif
bool IsAltKey(const content::NativeWebKeyboardEvent& event) {
#if defined(USE_X11)
// 164 and 165 represent VK_LALT and VK_RALT.
return event.windowsKeyCode == 164 || event.windowsKeyCode == 165;
#else
return event.windowsKeyCode == ui::VKEY_MENU;
#endif
}
bool IsAltModifier(const content::NativeWebKeyboardEvent& event) {
typedef content::NativeWebKeyboardEvent::Modifiers Modifiers;
return (event.modifiers == (Modifiers::AltKey | Modifiers::IsLeft)) ||
(event.modifiers == (Modifiers::AltKey | Modifiers::IsRight));
}
class NativeWindowClientView : public views::ClientView {
public:
NativeWindowClientView(views::Widget* widget,
@ -97,6 +112,7 @@ NativeWindowViews::NativeWindowViews(content::WebContents* web_contents,
web_view_(inspectable_web_contents()->GetView()->GetView()),
menu_bar_autohide_(false),
menu_bar_show_(false),
menu_bar_alt_pressed_(false),
keyboard_event_handler_(new views::UnhandledKeyboardEventHandler),
use_content_size_(false),
resizable_(true) {
@ -548,16 +564,20 @@ void NativeWindowViews::HandleMouseDown() {
void NativeWindowViews::HandleKeyboardEvent(
content::WebContents*,
const content::NativeWebKeyboardEvent& event) {
if (menu_bar_autohide_ &&
#if defined(USE_X11)
// 164 and 165 represent VK_LALT and VK_RALT.
(event.windowsKeyCode == 164 || event.windowsKeyCode == 165) &&
#else
(event.windowsKeyCode == ui::VKEY_MENU) &&
#endif
(event.type == blink::WebInputEvent::KeyUp)) {
// Toggle the menu bar only when a single Alt is released.
if (event.type == blink::WebInputEvent::RawKeyDown && IsAltKey(event) &&
IsAltModifier(event)) {
// When a single Alt is pressed:
menu_bar_alt_pressed_ = true;
} else if (event.type == blink::WebInputEvent::KeyUp && IsAltKey(event) &&
event.modifiers == 0 && menu_bar_alt_pressed_) {
// When a single Alt is released right after a Alt is pressed:
menu_bar_alt_pressed_ = false;
SetMenuBarVisibility(!menu_bar_show_);
Layout();
} else {
// When any other keys except single Alt have been pressed/released:
menu_bar_alt_pressed_ = false;
}
keyboard_event_handler_->HandleKeyboardEvent(event, GetFocusManager());

View file

@ -131,6 +131,7 @@ class NativeWindowViews : public NativeWindow,
scoped_ptr<MenuBar> menu_bar_;
bool menu_bar_autohide_;
bool menu_bar_show_;
bool menu_bar_alt_pressed_;
#if defined(USE_X11)
scoped_ptr<GlobalMenuBarX11> global_menu_bar_;