diff options
author | andresantoso <andresantoso@chromium.org> | 2015-03-31 16:26:36 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-31 23:27:12 +0000 |
commit | 2389f84559bde7ed9a91b25aed69a7e532b48864 (patch) | |
tree | b17adae96e75fb8a3edbc9ab63bee013f900d125 | |
parent | 1dc4af7b5a6e56244f2755f7ece6039ec3e6cc41 (diff) | |
download | chromium_src-2389f84559bde7ed9a91b25aed69a7e532b48864.zip chromium_src-2389f84559bde7ed9a91b25aed69a7e532b48864.tar.gz chromium_src-2389f84559bde7ed9a91b25aed69a7e532b48864.tar.bz2 |
MacViews: Remove BrowserWindowUtils dependency from accelerator_utils_cocoa
MacViews browser wants to link with accelerator_utils_cocoa but not with
BrowserWindowUtils. To achieve this, move the code in +getCommandId
from BrowserWindowUtils to global_keyboard_shortcuts_mac.
Also simplified MenuWalker helper class into a function in anon namespace.
BUG=425229
Review URL: https://codereview.chromium.org/1044233004
Cr-Commit-Position: refs/heads/master@{#323140}
-rw-r--r-- | chrome/browser/global_keyboard_shortcuts_mac.h | 3 | ||||
-rw-r--r-- | chrome/browser/global_keyboard_shortcuts_mac.mm | 69 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/accelerator_utils_cocoa.mm | 12 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/browser_window_utils.mm | 71 |
4 files changed, 78 insertions, 77 deletions
diff --git a/chrome/browser/global_keyboard_shortcuts_mac.h b/chrome/browser/global_keyboard_shortcuts_mac.h index f639ea9a..ab87ef9 100644 --- a/chrome/browser/global_keyboard_shortcuts_mac.h +++ b/chrome/browser/global_keyboard_shortcuts_mac.h @@ -62,6 +62,9 @@ int CommandForBrowserKeyboardShortcut( bool command_key, bool shift_key, bool cntrl_key, bool opt_key, int vkey_code, unichar key_char); +// Returns the Chrome command associated with |event|, or -1 if not found. +int CommandForKeyEvent(NSEvent* event); + // Returns a keyboard event character for the given |event|. In most cases // this returns the first character of [NSEvent charactersIgnoringModifiers], // but when [NSEvent character] has different printable ascii character diff --git a/chrome/browser/global_keyboard_shortcuts_mac.mm b/chrome/browser/global_keyboard_shortcuts_mac.mm index a4ee31e..9327213 100644 --- a/chrome/browser/global_keyboard_shortcuts_mac.mm +++ b/chrome/browser/global_keyboard_shortcuts_mac.mm @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include <AppKit/NSEvent.h> +#import <AppKit/AppKit.h> #include <Carbon/Carbon.h> #include "chrome/browser/global_keyboard_shortcuts_mac.h" @@ -10,6 +10,31 @@ #include "base/basictypes.h" #include "base/logging.h" #include "chrome/app/chrome_command_ids.h" +#import "chrome/browser/ui/cocoa/nsmenuitem_additions.h" + +namespace { + +// Returns the menu item associated with |key| in |menu|, or nil if not found. +NSMenuItem* FindMenuItem(NSEvent* key, NSMenu* menu) { + NSMenuItem* result = nil; + + for (NSMenuItem* item in [menu itemArray]) { + NSMenu* submenu = [item submenu]; + if (submenu) { + if (submenu != [NSApp servicesMenu]) + result = FindMenuItem(key, submenu); + } else if ([item cr_firesForKeyEventIfEnabled:key]) { + result = item; + } + + if (result) + break; + } + + return result; +} + +} // namespace // Basically, there are two kinds of keyboard shortcuts: Ones that should work // only if the tab contents is focused (BrowserKeyboardShortcut), and ones that @@ -169,6 +194,48 @@ int CommandForBrowserKeyboardShortcut( key_char); } +int CommandForKeyEvent(NSEvent* event) { + if ([event type] != NSKeyDown) + return -1; + + // Look in menu. + NSMenuItem* item = FindMenuItem(event, [NSApp mainMenu]); + if (item && [item action] == @selector(commandDispatch:) && [item tag] > 0) + return [item tag]; + + // "Close window" doesn't use the |commandDispatch:| mechanism. Menu items + // that do not correspond to IDC_ constants need no special treatment however, + // as they can't be blacklisted in + // |BrowserCommandController::IsReservedCommandOrKey()| anyhow. + if (item && [item action] == @selector(performClose:)) + return IDC_CLOSE_WINDOW; + + // "Exit" doesn't use the |commandDispatch:| mechanism either. + if (item && [item action] == @selector(terminate:)) + return IDC_EXIT; + + // Look in secondary keyboard shortcuts. + NSUInteger modifiers = [event modifierFlags]; + const bool cmdKey = (modifiers & NSCommandKeyMask) != 0; + const bool shiftKey = (modifiers & NSShiftKeyMask) != 0; + const bool cntrlKey = (modifiers & NSControlKeyMask) != 0; + const bool optKey = (modifiers & NSAlternateKeyMask) != 0; + const int keyCode = [event keyCode]; + const unichar keyChar = KeyCharacterForEvent(event); + + int cmdNum = CommandForWindowKeyboardShortcut( + cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); + if (cmdNum != -1) + return cmdNum; + + cmdNum = CommandForBrowserKeyboardShortcut( + cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); + if (cmdNum != -1) + return cmdNum; + + return -1; +} + unichar KeyCharacterForEvent(NSEvent* event) { NSString* eventString = [event charactersIgnoringModifiers]; NSString* characters = [event characters]; diff --git a/chrome/browser/ui/cocoa/accelerator_utils_cocoa.mm b/chrome/browser/ui/cocoa/accelerator_utils_cocoa.mm index 7cfa92b..294536f 100644 --- a/chrome/browser/ui/cocoa/accelerator_utils_cocoa.mm +++ b/chrome/browser/ui/cocoa/accelerator_utils_cocoa.mm @@ -2,10 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/accelerator_utils.h" + +#import <Cocoa/Cocoa.h> + +#include "chrome/browser/global_keyboard_shortcuts_mac.h" #include "chrome/browser/ui/cocoa/accelerators_cocoa.h" -#import "chrome/browser/ui/cocoa/browser_window_utils.h" -#include "content/public/browser/native_web_keyboard_event.h" #include "ui/base/accelerators/accelerator.h" #import "ui/base/accelerators/platform_accelerator_cocoa.h" #import "ui/events/keycodes/keyboard_code_conversion_mac.h" @@ -43,9 +45,7 @@ bool IsChromeAccelerator(const ui::Accelerator& accelerator, Profile* profile) { isARepeat:NO keyCode:accelerator.key_code()]; - content::NativeWebKeyboardEvent keyboard_event(event); - int id = [BrowserWindowUtils getCommandId:keyboard_event]; - return id != -1; + return CommandForKeyEvent(event) != -1; } ui::Accelerator GetPrimaryChromeAcceleratorForCommandId(int command_id) { diff --git a/chrome/browser/ui/cocoa/browser_window_utils.mm b/chrome/browser/ui/cocoa/browser_window_utils.mm index 83c73b5..60dd4d1 100644 --- a/chrome/browser/ui/cocoa/browser_window_utils.mm +++ b/chrome/browser/ui/cocoa/browser_window_utils.mm @@ -11,40 +11,11 @@ #include "chrome/browser/global_keyboard_shortcuts_mac.h" #include "chrome/browser/ui/browser.h" #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" -#import "chrome/browser/ui/cocoa/nsmenuitem_additions.h" #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" #include "content/public/browser/native_web_keyboard_event.h" using content::NativeWebKeyboardEvent; -@interface MenuWalker : NSObject -+ (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key - menu:(NSMenu*)menu; -@end - -@implementation MenuWalker -+ (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key - menu:(NSMenu*)menu { - NSMenuItem* result = nil; - - for (NSMenuItem* item in [menu itemArray]) { - NSMenu* submenu = [item submenu]; - if (submenu) { - if (submenu != [NSApp servicesMenu]) - result = [self itemForKeyEquivalent:key - menu:submenu]; - } else if ([item cr_firesForKeyEventIfEnabled:key]) { - result = item; - } - - if (result) - break; - } - - return result; -} -@end - @implementation BrowserWindowUtils + (BOOL)shouldHandleKeyboardEvent:(const NativeWebKeyboardEvent&)event { if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char) @@ -54,47 +25,7 @@ using content::NativeWebKeyboardEvent; } + (int)getCommandId:(const NativeWebKeyboardEvent&)event { - if ([event.os_event type] != NSKeyDown) - return -1; - - // Look in menu. - NSMenuItem* item = [MenuWalker itemForKeyEquivalent:event.os_event - menu:[NSApp mainMenu]]; - - if (item && [item action] == @selector(commandDispatch:) && [item tag] > 0) - return [item tag]; - - // "Close window" doesn't use the |commandDispatch:| mechanism. Menu items - // that do not correspond to IDC_ constants need no special treatment however, - // as they can't be blacklisted in - // |BrowserCommandController::IsReservedCommandOrKey()| anyhow. - if (item && [item action] == @selector(performClose:)) - return IDC_CLOSE_WINDOW; - - // "Exit" doesn't use the |commandDispatch:| mechanism either. - if (item && [item action] == @selector(terminate:)) - return IDC_EXIT; - - // Look in secondary keyboard shortcuts. - NSUInteger modifiers = [event.os_event modifierFlags]; - const bool cmdKey = (modifiers & NSCommandKeyMask) != 0; - const bool shiftKey = (modifiers & NSShiftKeyMask) != 0; - const bool cntrlKey = (modifiers & NSControlKeyMask) != 0; - const bool optKey = (modifiers & NSAlternateKeyMask) != 0; - const int keyCode = [event.os_event keyCode]; - const unichar keyChar = KeyCharacterForEvent(event.os_event); - - int cmdNum = CommandForWindowKeyboardShortcut( - cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); - if (cmdNum != -1) - return cmdNum; - - cmdNum = CommandForBrowserKeyboardShortcut( - cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); - if (cmdNum != -1) - return cmdNum; - - return -1; + return CommandForKeyEvent(event.os_event); } + (BOOL)handleKeyboardEvent:(NSEvent*)event |