mac: Simulate the behavior of cmd+~ when OS X didn't handle it
authorCheng Zhao <zcbenz@gmail.com>
Thu, 18 Feb 2016 10:32:13 +0000 (18:32 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 18 Feb 2016 10:32:13 +0000 (18:32 +0800)
atom/browser/native_window_mac.mm

index 7967a17717353eeaec6dd29e37dee9b659292fb9..9d9f2a2392d1e96d64fc4204e01426bfe776c2c3 100644 (file)
@@ -892,12 +892,25 @@ void NativeWindowMac::HandleKeyboardEvent(
     return;
 
   BOOL handled = [[NSApp mainMenu] performKeyEquivalent:event.os_event];
-  if (!handled && event.os_event.window != window_.get()) {
-    // The event comes from detached devtools view, and it has already been
-    if (!handled && (event.os_event.modifierFlags & NSCommandKeyMask) &&
-        (event.os_event.keyCode == 50  /* ~ key */)) {
-      // Handle the cmd+~ shortcut.
-      Focus(true);
+  if (!handled && event.os_event.window) {
+    // Handle the cmd+~ shortcut.
+    if ((event.os_event.modifierFlags & NSCommandKeyMask) /* cmd */ &&
+        (event.os_event.keyCode == 50  /* ~ */)) {
+      // Switch to next visible window.
+      NSArray* windows = [NSApp windows];
+      NSIndexSet* indexes = [windows indexesOfObjectsPassingTest:
+          ^BOOL(id window, NSUInteger idx, BOOL* stop) {
+            return [window isVisible];
+          }];
+      if ([indexes count] == 0)
+        return;
+      NSUInteger current = [windows indexOfObject:event.os_event.window];
+      if (current == NSNotFound)  // Some faked event.
+        return;
+      NSUInteger next = [indexes indexGreaterThanIndex:current];
+      if (next == NSNotFound)
+        next = [indexes firstIndex];
+      [[windows objectAtIndex:next] makeKeyAndOrderFront:nil];
     }
   }
 }