Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/browser_window_utils.h" | 5 #import "chrome/browser/ui/cocoa/browser_window_utils.h" |
| 6 | 6 |
| 7 #include <Carbon/Carbon.h> | 7 #include <Carbon/Carbon.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
| 11 #include "chrome/browser/global_keyboard_shortcuts_mac.h" | 11 #include "chrome/browser/global_keyboard_shortcuts_mac.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" | 13 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" |
| 14 #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h" | |
| 15 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" | 14 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" |
| 16 #include "content/public/browser/native_web_keyboard_event.h" | 15 #include "content/public/browser/native_web_keyboard_event.h" |
| 17 | 16 |
| 18 using content::NativeWebKeyboardEvent; | 17 using content::NativeWebKeyboardEvent; |
| 19 | 18 |
| 20 @interface MenuWalker : NSObject | |
| 21 + (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key | |
| 22 menu:(NSMenu*)menu; | |
| 23 @end | |
| 24 | |
| 25 @implementation MenuWalker | |
| 26 + (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key | |
|
Robert Sesek
2015/03/31 22:51:36
(Why was this ever a class?)
Andre
2015/03/31 23:21:57
It came from here: https://codereview.chromium.org
| |
| 27 menu:(NSMenu*)menu { | |
| 28 NSMenuItem* result = nil; | |
| 29 | |
| 30 for (NSMenuItem* item in [menu itemArray]) { | |
| 31 NSMenu* submenu = [item submenu]; | |
| 32 if (submenu) { | |
| 33 if (submenu != [NSApp servicesMenu]) | |
| 34 result = [self itemForKeyEquivalent:key | |
| 35 menu:submenu]; | |
| 36 } else if ([item cr_firesForKeyEventIfEnabled:key]) { | |
| 37 result = item; | |
| 38 } | |
| 39 | |
| 40 if (result) | |
| 41 break; | |
| 42 } | |
| 43 | |
| 44 return result; | |
| 45 } | |
| 46 @end | |
| 47 | |
| 48 @implementation BrowserWindowUtils | 19 @implementation BrowserWindowUtils |
| 49 + (BOOL)shouldHandleKeyboardEvent:(const NativeWebKeyboardEvent&)event { | 20 + (BOOL)shouldHandleKeyboardEvent:(const NativeWebKeyboardEvent&)event { |
| 50 if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char) | 21 if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char) |
| 51 return NO; | 22 return NO; |
| 52 DCHECK(event.os_event != NULL); | 23 DCHECK(event.os_event != NULL); |
| 53 return YES; | 24 return YES; |
| 54 } | 25 } |
| 55 | 26 |
| 56 + (int)getCommandId:(const NativeWebKeyboardEvent&)event { | 27 + (int)getCommandId:(const NativeWebKeyboardEvent&)event { |
| 57 if ([event.os_event type] != NSKeyDown) | 28 return CommandForKeyEvent(event.os_event); |
| 58 return -1; | |
| 59 | |
| 60 // Look in menu. | |
| 61 NSMenuItem* item = [MenuWalker itemForKeyEquivalent:event.os_event | |
| 62 menu:[NSApp mainMenu]]; | |
| 63 | |
| 64 if (item && [item action] == @selector(commandDispatch:) && [item tag] > 0) | |
| 65 return [item tag]; | |
| 66 | |
| 67 // "Close window" doesn't use the |commandDispatch:| mechanism. Menu items | |
| 68 // that do not correspond to IDC_ constants need no special treatment however, | |
| 69 // as they can't be blacklisted in | |
| 70 // |BrowserCommandController::IsReservedCommandOrKey()| anyhow. | |
| 71 if (item && [item action] == @selector(performClose:)) | |
| 72 return IDC_CLOSE_WINDOW; | |
| 73 | |
| 74 // "Exit" doesn't use the |commandDispatch:| mechanism either. | |
| 75 if (item && [item action] == @selector(terminate:)) | |
| 76 return IDC_EXIT; | |
| 77 | |
| 78 // Look in secondary keyboard shortcuts. | |
| 79 NSUInteger modifiers = [event.os_event modifierFlags]; | |
| 80 const bool cmdKey = (modifiers & NSCommandKeyMask) != 0; | |
| 81 const bool shiftKey = (modifiers & NSShiftKeyMask) != 0; | |
| 82 const bool cntrlKey = (modifiers & NSControlKeyMask) != 0; | |
| 83 const bool optKey = (modifiers & NSAlternateKeyMask) != 0; | |
| 84 const int keyCode = [event.os_event keyCode]; | |
| 85 const unichar keyChar = KeyCharacterForEvent(event.os_event); | |
| 86 | |
| 87 int cmdNum = CommandForWindowKeyboardShortcut( | |
| 88 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); | |
| 89 if (cmdNum != -1) | |
| 90 return cmdNum; | |
| 91 | |
| 92 cmdNum = CommandForBrowserKeyboardShortcut( | |
| 93 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); | |
| 94 if (cmdNum != -1) | |
| 95 return cmdNum; | |
| 96 | |
| 97 return -1; | |
| 98 } | 29 } |
| 99 | 30 |
| 100 + (BOOL)handleKeyboardEvent:(NSEvent*)event | 31 + (BOOL)handleKeyboardEvent:(NSEvent*)event |
| 101 inWindow:(NSWindow*)window { | 32 inWindow:(NSWindow*)window { |
| 102 ChromeEventProcessingWindow* event_window = | 33 ChromeEventProcessingWindow* event_window = |
| 103 static_cast<ChromeEventProcessingWindow*>(window); | 34 static_cast<ChromeEventProcessingWindow*>(window); |
| 104 DCHECK([event_window isKindOfClass:[ChromeEventProcessingWindow class]]); | 35 DCHECK([event_window isKindOfClass:[ChromeEventProcessingWindow class]]); |
| 105 | 36 |
| 106 // Do not fire shortcuts on key up. | 37 // Do not fire shortcuts on key up. |
| 107 if ([event type] == NSKeyDown) { | 38 if ([event type] == NSKeyDown) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 + (void)activateWindowForController:(NSWindowController*)controller { | 132 + (void)activateWindowForController:(NSWindowController*)controller { |
| 202 // Per http://crbug.com/73779 and http://crbug.com/75223, we need this to | 133 // Per http://crbug.com/73779 and http://crbug.com/75223, we need this to |
| 203 // properly activate windows if Chrome is not the active application. | 134 // properly activate windows if Chrome is not the active application. |
| 204 [[controller window] makeKeyAndOrderFront:controller]; | 135 [[controller window] makeKeyAndOrderFront:controller]; |
| 205 ProcessSerialNumber psn; | 136 ProcessSerialNumber psn; |
| 206 GetCurrentProcess(&psn); | 137 GetCurrentProcess(&psn); |
| 207 SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly); | 138 SetFrontProcessWithOptions(&psn, kSetFrontProcessFrontWindowOnly); |
| 208 } | 139 } |
| 209 | 140 |
| 210 @end | 141 @end |
| OLD | NEW |