]> khamidou — GitHub mirrors - electron.git/commitdiff
views: Toggle the menu bar only when a single Alt is released.
authorCheng Zhao <zcbenz@gmail.com>
Thu, 7 Aug 2014 07:54:05 +0000 (15:54 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 7 Aug 2014 07:54:05 +0000 (15:54 +0800)
atom/browser/native_window_views.cc
atom/browser/native_window_views.h

index 1aafe6c0e2feedbd1b04a09eb9d879c30763b3e5..44f06f3878b9abac745be10a12870df46e851668 100644 (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());
index 0381a9bd05e9529be52825bcb3e57a9b3ee5c916..ba3007e8d923aba60161feae201418580cba2523 100644 (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_;