Commit 3641da6c authored by jeremy@chromium.org's avatar jeremy@chromium.org

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
parent 73820078
......@@ -8,8 +8,8 @@
<string key="IBDocument.HIToolboxVersion">353.00</string>
<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
<bool key="EncodedWithXMLCoder">YES</bool>
<integer value="1"/>
<integer value="56"/>
<integer value="1"/>
</object>
<object class="NSArray" key="IBDocument.PluginDependencies">
<bool key="EncodedWithXMLCoder">YES</bool>
......@@ -41,7 +41,7 @@
<string key="NSWindowRect">{{60, 229}, {750, 600}}</string>
<int key="NSWTFlags">536872960</int>
<string key="NSWindowTitle"/>
<string key="NSWindowClass">NSWindow</string>
<string key="NSWindowClass">ChromeBrowserWindow</string>
<nil key="NSViewClass"/>
<string key="NSWindowContentMaxSize">{3.40282e+38, 3.40282e+38}</string>
<string key="NSWindowContentMinSize">{400, 250}</string>
......@@ -306,6 +306,14 @@
<string key="minorKey">browser/cocoa/browser_window_controller.h</string>
</object>
</object>
<object class="IBPartialClassDescription">
<string key="className">ChromeBrowserWindow</string>
<string key="superclassName">NSWindow</string>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">browser/cocoa/browser_window.h</string>
</object>
</object>
<object class="IBPartialClassDescription">
<string key="className">FirstResponder</string>
<string key="superclassName">NSObject</string>
......
......@@ -8,7 +8,7 @@
<string key="IBDocument.HIToolboxVersion">353.00</string>
<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
<bool key="EncodedWithXMLCoder">YES</bool>
<integer value="24"/>
<integer value="519"/>
</object>
<object class="NSArray" key="IBDocument.PluginDependencies">
<bool key="EncodedWithXMLCoder">YES</bool>
......@@ -787,6 +787,26 @@
<reference key="NSMixedImage" ref="549394948"/>
<int key="NSTag">38003</int>
</object>
<object class="NSMenuItem" id="350076160">
<reference key="NSMenu" ref="466310130"/>
<bool key="NSIsDisabled">YES</bool>
<bool key="NSIsSeparator">YES</bool>
<string key="NSTitle"/>
<string key="NSKeyEquiv"/>
<int key="NSMnemonicLoc">2147483647</int>
<reference key="NSOnImage" ref="353210768"/>
<reference key="NSMixedImage" ref="549394948"/>
</object>
<object class="NSMenuItem" id="1053133947">
<reference key="NSMenu" ref="466310130"/>
<string key="NSTitle">Downloads</string>
<string key="NSKeyEquiv">l</string>
<int key="NSKeyEquivModMask">1572864</int>
<int key="NSMnemonicLoc">2147483647</int>
<reference key="NSOnImage" ref="353210768"/>
<reference key="NSMixedImage" ref="549394948"/>
<int key="NSTag">40012</int>
</object>
<object class="NSMenuItem" id="31790433">
<reference key="NSMenu" ref="466310130"/>
<bool key="NSIsDisabled">YES</bool>
......@@ -1601,6 +1621,14 @@
</object>
<int key="connectionID">630</int>
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">commandDispatch:</string>
<reference key="source" ref="1014"/>
<reference key="destination" ref="1053133947"/>
</object>
<int key="connectionID">633</int>
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
<string key="label">orderFrontStandardAboutPanel:</string>
......@@ -2098,6 +2126,8 @@
<reference ref="249269220"/>
<reference ref="152850290"/>
<reference ref="530225222"/>
<reference ref="350076160"/>
<reference ref="1053133947"/>
</object>
<reference key="parent" ref="586577488"/>
</object>
......@@ -2453,6 +2483,16 @@
<reference key="object" ref="18234385"/>
<reference key="parent" ref="720053764"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">631</int>
<reference key="object" ref="350076160"/>
<reference key="parent" ref="466310130"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">632</int>
<reference key="object" ref="1053133947"/>
<reference key="parent" ref="466310130"/>
</object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
......@@ -2659,6 +2699,8 @@
<string>58.IBPluginDependency</string>
<string>58.ImportedFromIB2</string>
<string>625.IBPluginDependency</string>
<string>631.IBPluginDependency</string>
<string>632.IBPluginDependency</string>
<string>72.IBPluginDependency</string>
<string>72.ImportedFromIB2</string>
<string>73.IBPluginDependency</string>
......@@ -2751,7 +2793,7 @@
<reference ref="9"/>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
<string>{{647, 825}, {243, 263}}</string>
<string>{{678, 873}, {243, 263}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
<string>{{197, 734}, {243, 243}}</string>
......@@ -2797,17 +2839,17 @@
<reference ref="9"/>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
<string>{{933, 1202}, {213, 163}}</string>
<string>{{933, 973}, {213, 163}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
<string>{{525, 802}, {197, 73}}</string>
<string>{{530, 1365}, {535, 20}}</string>
<string>{{530, 1136}, {535, 20}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
<string>{74, 862}</string>
<string>{{11, 977}, {478, 20}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>{{722, 1152}, {287, 213}}</string>
<string>{{722, 893}, {301, 243}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>{{475, 832}, {234, 43}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
......@@ -2840,7 +2882,7 @@
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>{{920, 545}, {64, 6}}</string>
<string>{{1023, 930}, {64, 6}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
......@@ -2850,7 +2892,7 @@
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>{{764, 688}, {254, 33}}</string>
<string>{{839, 1103}, {254, 33}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
......@@ -2861,7 +2903,7 @@
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>{{781, 941}, {188, 123}}</string>
<string>{{772, 1013}, {188, 123}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
......@@ -2887,6 +2929,8 @@
<reference ref="9"/>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
......@@ -2900,7 +2944,7 @@
<reference ref="9"/>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
<string>{{605, 765}, {249, 323}}</string>
<string>{{636, 813}, {249, 323}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
<string>{{323, 672}, {199, 203}}</string>
......@@ -2932,7 +2976,7 @@
</object>
</object>
<nil key="sourceID"/>
<int key="maxID">630</int>
<int key="maxID">633</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
......
// 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_
// 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
......@@ -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
......
......@@ -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];
}
......
// 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_
// 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;
}
// 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);
}
}
......@@ -732,6 +732,8 @@
'browser/cocoa/bookmark_menu_cocoa_controller.h',
'browser/cocoa/bookmark_menu_cocoa_controller.mm',
'browser/cocoa/browser_test_helper.h',
'browser/cocoa/browser_window.h',
'browser/cocoa/browser_window.mm',
'browser/cocoa/browser_window_cocoa.h',
'browser/cocoa/browser_window_cocoa.mm',
'browser/cocoa/browser_window_controller.h',
......@@ -934,6 +936,8 @@
'browser/external_protocol_handler.h',
'browser/external_tab_container.cc',
'browser/external_tab_container.h',
'browser/global_keyboard_shortcuts_mac.h',
'browser/global_keyboard_shortcuts_mac.mm',
'browser/fav_icon_helper.cc',
'browser/fav_icon_helper.h',
'browser/find_bar.h',
......@@ -3486,6 +3490,7 @@
'browser/extensions/extensions_service_unittest.cc',
'browser/extensions/user_script_master_unittest.cc',
'browser/find_backend_unittest.cc',
'browser/global_keyboard_shortcuts_mac_unittest.cc',
'browser/google_url_tracker_unittest.cc',
'browser/google_update_settings_linux_unittest.cc',
'browser/google_update_settings_mac_unittest.mm',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment