diff options
author | jeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-08 14:59:06 +0000 |
---|---|---|
committer | jeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-08 14:59:06 +0000 |
commit | 3641da6c02910960ef419d38a161081a9ce1d7b2 (patch) | |
tree | 1123c9e72bc88275779d8e89060ed0cfe73cb172 /chrome/browser | |
parent | 73820078eac1040173047912c1f4d413a0eca696 (diff) | |
download | chromium_src-3641da6c02910960ef419d38a161081a9ce1d7b2.zip chromium_src-3641da6c02910960ef419d38a161081a9ce1d7b2.tar.gz chromium_src-3641da6c02910960ef419d38a161081a9ce1d7b2.tar.bz2 |
Add facitility for Global Keyboard shortcuts.
Also, do some housekeeping for our current shortcuts.
* Command-Option-l - Downloads [same shortcut as Safari].
* Command-Shift-[/] - Back/forward tab.
* cntrl-pageup/pagedn - Back/forward tab.
Global Keyboard events are intercepted by a new BrowserWindow custom class.
BUG=12537,15486
TEST=Check that above keyboard shortcuts work as advertised.
Review URL: http://codereview.chromium.org/149325
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20145 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/cocoa/browser_window.h | 20 | ||||
-rw-r--r-- | chrome/browser/cocoa/browser_window.mm | 37 | ||||
-rw-r--r-- | chrome/browser/cocoa/browser_window_controller.h | 5 | ||||
-rw-r--r-- | chrome/browser/cocoa/browser_window_controller.mm | 7 | ||||
-rw-r--r-- | chrome/browser/global_keyboard_shortcuts_mac.h | 27 | ||||
-rw-r--r-- | chrome/browser/global_keyboard_shortcuts_mac.mm | 43 | ||||
-rw-r--r-- | chrome/browser/global_keyboard_shortcuts_mac_unittest.cc | 22 |
7 files changed, 161 insertions, 0 deletions
diff --git a/chrome/browser/cocoa/browser_window.h b/chrome/browser/cocoa/browser_window.h new file mode 100644 index 0000000..3c43577 --- /dev/null +++ b/chrome/browser/cocoa/browser_window.h @@ -0,0 +1,20 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_COCOA_BROWSER_WINDOW_H_ +#define CHROME_BROWSER_COCOA_BROWSER_WINDOW_H_ + +#import <Cocoa/Cocoa.h> + +// Cocoa class representing a Chrome browser window. +// We need to override NSWindow with our own class since we need access to all +// unhandled keyboard events and subclassing NSWindow is the only method to do +// this. +@interface ChromeBrowserWindow : NSWindow + +// Override, so we can handle global keyboard events. +- (BOOL)performKeyEquivalent:(NSEvent*)theEvent; +@end + +#endif // CHROME_BROWSER_COCOA_BROWSER_WINDOW_H_ diff --git a/chrome/browser/cocoa/browser_window.mm b/chrome/browser/cocoa/browser_window.mm new file mode 100644 index 0000000..0268dc6 --- /dev/null +++ b/chrome/browser/cocoa/browser_window.mm @@ -0,0 +1,37 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "chrome/browser/cocoa/browser_window.h" + +#import "chrome/browser/cocoa/browser_window_controller.h" +#include "chrome/browser/global_keyboard_shortcuts_mac.h" + +@implementation ChromeBrowserWindow + +- (BOOL)performKeyEquivalent:(NSEvent*)event { + // Extract info from |event|. + NSUInteger modifers = [event modifierFlags]; + const bool cmdKey = modifers & NSCommandKeyMask; + const bool shiftKey = modifers & NSShiftKeyMask; + const bool cntrlKey = modifers & NSControlKeyMask; + const int keyCode = [event keyCode]; + + int cmdNum = CommandForKeyboardShortcut(cmdKey, shiftKey, cntrlKey, + keyCode); + + BrowserWindowController* controller = + (BrowserWindowController*)[self delegate]; + // A bit of sanity. + DCHECK([controller isKindOfClass:[BrowserWindowController class]]); + DCHECK([controller respondsToSelector:@selector(executeCommand:)]); + + if (cmdNum != -1) { + [controller executeCommand:cmdNum]; + return YES; + } + + return [super performKeyEquivalent:event]; +} + +@end diff --git a/chrome/browser/cocoa/browser_window_controller.h b/chrome/browser/cocoa/browser_window_controller.h index c7b24f1..54ed849 100644 --- a/chrome/browser/cocoa/browser_window_controller.h +++ b/chrome/browser/cocoa/browser_window_controller.h @@ -117,6 +117,11 @@ class TabStripModelObserverBridge; // Returns fullscreen state. - (BOOL)isFullscreen; +// Executes the command in the context of the current browser. +// |command| is an integer value containing one of the constants defined in the +// "chrome/app/chrome_dll_resource.h" file. +- (void)executeCommand:(int)command; + @end diff --git a/chrome/browser/cocoa/browser_window_controller.mm b/chrome/browser/cocoa/browser_window_controller.mm index da2ede6..041d6e1 100644 --- a/chrome/browser/cocoa/browser_window_controller.mm +++ b/chrome/browser/cocoa/browser_window_controller.mm @@ -375,6 +375,13 @@ willPositionSheet:(NSWindow *)sheet browser_->ExecuteCommand(tag); } +// Called when another part of the internal codebase needs to execute a +// command. +- (void)executeCommand:(int)command { + if (browser_->command_updater()->IsCommandEnabled(command)) + browser_->ExecuteCommand(command); +} + - (LocationBar*)locationBar { return [toolbarController_ locationBar]; } diff --git a/chrome/browser/global_keyboard_shortcuts_mac.h b/chrome/browser/global_keyboard_shortcuts_mac.h new file mode 100644 index 0000000..45d9a69 --- /dev/null +++ b/chrome/browser/global_keyboard_shortcuts_mac.h @@ -0,0 +1,27 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_GLOBAL_KEYBOARD_SHORTCUTS_MAC_H_ +#define CHROME_BROWSER_GLOBAL_KEYBOARD_SHORTCUTS_MAC_H_ + +#include "base/basictypes.h"; + +struct KeyboardShortcutData { + bool command_key; + bool shift_key; + bool cntrl_key; + int vkey_code; // Virtual Key code for the command. + int chrome_command; // The chrome command # to execute for this shortcut. +}; + +// Check if a given keycode + modifiers correspond to a given Chrome command. +// returns: Command number (as passed to Browser::ExecuteCommand) or -1 if there +// was no match. +int CommandForKeyboardShortcut(bool command_key, bool shift_key, bool cntrl_key, + int vkey_code); + +// For testing purposes. +const KeyboardShortcutData* GetKeyboardShortCutTable(size_t* num_entries); + +#endif // #ifndef CHROME_BROWSER_GLOBAL_KEYBOARD_SHORTCUTS_MAC_H_ diff --git a/chrome/browser/global_keyboard_shortcuts_mac.mm b/chrome/browser/global_keyboard_shortcuts_mac.mm new file mode 100644 index 0000000..426d379 --- /dev/null +++ b/chrome/browser/global_keyboard_shortcuts_mac.mm @@ -0,0 +1,43 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/global_keyboard_shortcuts_mac.h" + +#include "base/basictypes.h" +#include "chrome/app/chrome_dll_resource.h" + +const KeyboardShortcutData* GetKeyboardShortCutTable(size_t* num_entries) { + static const KeyboardShortcutData keyboard_shortcuts[] = { + {true, true, false, 30 /* ] */, IDC_SELECT_NEXT_TAB}, + {false, false, true, 121 /* pg down */, IDC_SELECT_NEXT_TAB}, + {true, true, false, 33 /* [ */, IDC_SELECT_PREVIOUS_TAB}, + {false, false, true, 116 /* pg_up */, IDC_SELECT_PREVIOUS_TAB}, + }; + + *num_entries = arraysize(keyboard_shortcuts); + + return keyboard_shortcuts; +} + +int CommandForKeyboardShortcut(bool command_key, bool shift_key, bool cntrl_key, + int vkey_code) { + + // Scan through keycodes and see if it corresponds to one of the global + // shortcuts on file. + // + // TODO(jeremy): Change this into a hash table once we get enough + // entries in the array to make a difference. + size_t num_shortcuts = 0; + const KeyboardShortcutData *it = GetKeyboardShortCutTable(&num_shortcuts); + for (size_t i = 0; i < num_shortcuts; ++i, ++it) { + if (it->command_key == command_key && + it->shift_key == shift_key && + it->cntrl_key == cntrl_key && + it->vkey_code == vkey_code) { + return it->chrome_command; + } + } + + return -1; +} diff --git a/chrome/browser/global_keyboard_shortcuts_mac_unittest.cc b/chrome/browser/global_keyboard_shortcuts_mac_unittest.cc new file mode 100644 index 0000000..c8c22d2 --- /dev/null +++ b/chrome/browser/global_keyboard_shortcuts_mac_unittest.cc @@ -0,0 +1,22 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/global_keyboard_shortcuts_mac.h" + +#include "testing/gtest/include/gtest/gtest.h" + +TEST(GlobalKeyboardShortcuts, ShortCutsToCommand) { + // Test that an invalid shortcut translates into an invalid command id. + ASSERT_EQ(CommandForKeyboardShortcut(false, false, false, 0), -1); + + // Check that all known keyboard shortcuts return valid results. + size_t num_shortcuts = 0; + const KeyboardShortcutData *it = GetKeyboardShortCutTable(&num_shortcuts); + ASSERT_GT(num_shortcuts, 0U); + for (size_t i = 0; i < num_shortcuts; ++i, ++it) { + int cmd_num = CommandForKeyboardShortcut(it->command_key, it->shift_key, + it->cntrl_key, it->vkey_code); + ASSERT_EQ(cmd_num, it->chrome_command); + } +} |