Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3595)

Unified Diff: chrome/browser/ui/cocoa/chrome_event_processing_window.mm

Issue 1255783002: [Mac] Factor out keyboard shortcut handling from ChromeEventProcessingWindow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@execute
Patch Set: UI_BASE_EXPORT CommandDispatcher Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/ui/cocoa/chrome_event_processing_window.h ('k') | chrome/chrome_browser_ui.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/ui/cocoa/chrome_event_processing_window.mm
diff --git a/chrome/browser/ui/cocoa/chrome_event_processing_window.mm b/chrome/browser/ui/cocoa/chrome_event_processing_window.mm
index 4dd2c644a574850fc78ae54c78c0da9b3a7c252b..b7b437d923f67200e1a961bb126a05312fb709a3 100644
--- a/chrome/browser/ui/cocoa/chrome_event_processing_window.mm
+++ b/chrome/browser/ui/cocoa/chrome_event_processing_window.mm
@@ -5,184 +5,55 @@
#import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
#include "base/logging.h"
-#include "chrome/browser/global_keyboard_shortcuts_mac.h"
-#include "chrome/browser/ui/browser_commands.h"
-#include "chrome/browser/ui/browser_finder.h"
-#import "chrome/browser/ui/cocoa/browser_window_controller_private.h"
-#import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
-#import "content/public/browser/render_widget_host_view_mac_base.h"
-
-namespace {
-
-// Type of functions listed in global_keyboard_shortcuts_mac.h.
-typedef int (*KeyToCommandMapper)(bool, bool, bool, bool, int, unichar);
-
-// If the event is for a Browser window, and the key combination has an
-// associated command, execute the command.
-bool HandleExtraKeyboardShortcut(
- NSEvent* event,
- NSWindow* window,
- KeyToCommandMapper command_for_keyboard_shortcut) {
- // Extract info from |event|.
- NSUInteger modifers = [event modifierFlags];
- const bool command = modifers & NSCommandKeyMask;
- const bool shift = modifers & NSShiftKeyMask;
- const bool control = modifers & NSControlKeyMask;
- const bool option = modifers & NSAlternateKeyMask;
- const int key_code = [event keyCode];
- const unichar key_char = KeyCharacterForEvent(event);
-
- int cmd = command_for_keyboard_shortcut(command, shift, control, option,
- key_code, key_char);
-
- if (cmd == -1)
- return false;
-
- // Only handle event if this is a browser window.
- Browser* browser = chrome::FindBrowserWithWindow(window);
- if (!browser)
- return false;
-
- chrome::ExecuteCommand(browser, cmd);
- return true;
-}
-
-bool HandleExtraWindowKeyboardShortcut(NSEvent* event, NSWindow* window) {
- return HandleExtraKeyboardShortcut(event, window,
- CommandForWindowKeyboardShortcut);
-}
-
-bool HandleDelayedWindowKeyboardShortcut(NSEvent* event, NSWindow* window) {
- return HandleExtraKeyboardShortcut(event, window,
- CommandForDelayedWindowKeyboardShortcut);
-}
-
-bool HandleExtraBrowserKeyboardShortcut(NSEvent* event, NSWindow* window) {
- return HandleExtraKeyboardShortcut(event, window,
- CommandForBrowserKeyboardShortcut);
-}
-
-// Duplicate the given key event, but changing the associated window.
-NSEvent* KeyEventForWindow(NSWindow* window, NSEvent* event) {
- NSEventType event_type = [event type];
-
- // Convert the event's location from the original window's coordinates into
- // our own.
- NSPoint location = [event locationInWindow];
- location = [[event window] convertBaseToScreen:location];
- location = [window convertScreenToBase:location];
-
- // Various things *only* apply to key down/up.
- bool is_a_repeat = false;
- NSString* characters = nil;
- NSString* charactors_ignoring_modifiers = nil;
- if (event_type == NSKeyDown || event_type == NSKeyUp) {
- is_a_repeat = [event isARepeat];
- characters = [event characters];
- charactors_ignoring_modifiers = [event charactersIgnoringModifiers];
+#import "chrome/browser/ui/cocoa/chrome_command_dispatcher_delegate.h"
+
+@implementation ChromeEventProcessingWindow {
+ @private
+ base::scoped_nsobject<CommandDispatcher> commandDispatcher_;
+ base::scoped_nsobject<ChromeCommandDispatcherDelegate>
+ commandDispatcherDelegate_;
+}
+
+- (instancetype)initWithContentRect:(NSRect)contentRect
+ styleMask:(NSUInteger)windowStyle
+ backing:(NSBackingStoreType)bufferingType
+ defer:(BOOL)deferCreation {
+ if ((self = [super initWithContentRect:contentRect
+ styleMask:windowStyle
+ backing:bufferingType
+ defer:deferCreation])) {
+ commandDispatcher_.reset([[CommandDispatcher alloc] initWithOwner:self]);
+ commandDispatcherDelegate_.reset(
+ [[ChromeCommandDispatcherDelegate alloc] init]);
+ [commandDispatcher_ setDelegate:commandDispatcherDelegate_];
}
-
- // This synthesis may be slightly imperfect: we provide nil for the context,
- // since I (viettrungluu) am sceptical that putting in the original context
- // (if one is given) is valid.
- return [NSEvent keyEventWithType:event_type
- location:location
- modifierFlags:[event modifierFlags]
- timestamp:[event timestamp]
- windowNumber:[window windowNumber]
- context:nil
- characters:characters
- charactersIgnoringModifiers:charactors_ignoring_modifiers
- isARepeat:is_a_repeat
- keyCode:[event keyCode]];
+ return self;
}
-} // namespace
-
-@implementation ChromeEventProcessingWindow
-
- (BOOL)handleExtraKeyboardShortcut:(NSEvent*)event {
- return HandleExtraBrowserKeyboardShortcut(event, self) ||
- HandleExtraWindowKeyboardShortcut(event, self) ||
- HandleDelayedWindowKeyboardShortcut(event, self);
+ return [commandDispatcherDelegate_ handleExtraKeyboardShortcut:event
+ window:self];
}
-- (BOOL)performKeyEquivalent:(NSEvent*)event {
- // Some extension commands have higher priority than web content, and some
- // have lower priority. Regardless of whether the event is being
- // redispatched, let the extension system try to handle the event.
- NSWindow* window = event.window;
- if (window) {
- BrowserWindowController* controller = [window windowController];
- if ([controller respondsToSelector:@selector(handledByExtensionCommand:
- priority:)]) {
- ui::AcceleratorManager::HandlerPriority priority =
- redispatchingEvent_ ? ui::AcceleratorManager::kNormalPriority
- : ui::AcceleratorManager::kHighPriority;
- if ([controller handledByExtensionCommand:event priority:priority])
- return YES;
- }
- }
-
- if (redispatchingEvent_)
- return NO;
-
- // Give the web site a chance to handle the event. If it doesn't want to
- // handle it, it will call us back with one of the |handle*| methods above.
- NSResponder* r = [self firstResponder];
- if ([r conformsToProtocol:@protocol(RenderWidgetHostViewMacBase)])
- return [r performKeyEquivalent:event];
-
- // Handle per-window shortcuts like cmd-1, but do not handle browser-level
- // shortcuts like cmd-left (else, cmd-left would do history navigation even
- // if e.g. the Omnibox has focus).
- if (HandleExtraWindowKeyboardShortcut(event, self))
- return YES;
-
- if ([super performKeyEquivalent:event])
- return YES;
-
- // Handle per-window shortcuts like Esc after giving everybody else a chance
- // to handle them
- return HandleDelayedWindowKeyboardShortcut(event, self);
-}
+// CommandDispatchingWindow implementation.
- (BOOL)redispatchKeyEvent:(NSEvent*)event {
- DCHECK(event);
- NSEventType eventType = [event type];
- if (eventType != NSKeyDown &&
- eventType != NSKeyUp &&
- eventType != NSFlagsChanged) {
- NOTREACHED();
- return YES; // Pretend it's been handled in an effort to limit damage.
- }
+ return [commandDispatcher_ redispatchKeyEvent:event];
+}
- // Ordinarily, the event's window should be this window. However, when
- // switching between normal and fullscreen mode, we switch out the window, and
- // the event's window might be the previous window (or even an earlier one if
- // the renderer is running slowly and several mode switches occur). In this
- // rare case, we synthesize a new key event so that its associate window
- // (number) is our own.
- if ([event window] != self)
- event = KeyEventForWindow(self, event);
+- (BOOL)defaultPerformKeyEquivalent:(NSEvent*)event {
+ return [super performKeyEquivalent:event];
+}
- // Redispatch the event.
- eventHandled_ = YES;
- redispatchingEvent_ = YES;
- [NSApp sendEvent:event];
- redispatchingEvent_ = NO;
+// NSWindow overrides.
- // If the event was not handled by [NSApp sendEvent:], the sendEvent:
- // method below will be called, and because |redispatchingEvent_| is YES,
- // |eventHandled_| will be set to NO.
- return eventHandled_;
+- (BOOL)performKeyEquivalent:(NSEvent*)event {
+ return [commandDispatcher_ performKeyEquivalent:event];
}
- (void)sendEvent:(NSEvent*)event {
- if (!redispatchingEvent_)
+ if (![commandDispatcher_ preSendEvent:event])
[super sendEvent:event];
- else
- eventHandled_ = NO;
}
@end // ChromeEventProcessingWindow
« no previous file with comments | « chrome/browser/ui/cocoa/chrome_event_processing_window.h ('k') | chrome/chrome_browser_ui.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698