Index: ui/base/cocoa/command_dispatcher.mm |
diff --git a/chrome/browser/ui/cocoa/chrome_event_processing_window.mm b/ui/base/cocoa/command_dispatcher.mm |
similarity index 34% |
copy from chrome/browser/ui/cocoa/chrome_event_processing_window.mm |
copy to ui/base/cocoa/command_dispatcher.mm |
index 4dd2c644a574850fc78ae54c78c0da9b3a7c252b..64ac8fd1bb229d5c448b7f73195ead700d89e2cd 100644 |
--- a/chrome/browser/ui/cocoa/chrome_event_processing_window.mm |
+++ b/ui/base/cocoa/command_dispatcher.mm |
@@ -1,67 +1,13 @@ |
-// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Copyright 2015 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/ui/cocoa/chrome_event_processing_window.h" |
+#import "ui/base/cocoa/command_dispatcher.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]; |
@@ -99,72 +45,64 @@ NSEvent* KeyEventForWindow(NSWindow* window, NSEvent* event) { |
} // namespace |
-@implementation ChromeEventProcessingWindow |
+@implementation CommandDispatcher { |
+ @private |
+ BOOL redispatchingEvent_; |
+ BOOL eventHandled_; |
+ NSWindow<CommandDispatchingWindow>* owner_; // Weak, owns us. |
+} |
+ |
+@synthesize delegate = delegate_; |
-- (BOOL)handleExtraKeyboardShortcut:(NSEvent*)event { |
- return HandleExtraBrowserKeyboardShortcut(event, self) || |
- HandleExtraWindowKeyboardShortcut(event, self) || |
- HandleDelayedWindowKeyboardShortcut(event, self); |
+- (instancetype)initWithOwner:(NSWindow<CommandDispatchingWindow>*)owner { |
+ if ((self = [super init])) { |
+ owner_ = owner; |
+ } |
+ return 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 ([delegate_ eventHandledByExtensionCommand:event |
+ isRedispatch:redispatchingEvent_]) { |
+ 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)]) |
+ // Give a CommandDispatcherTarget (e.g. a web site) a chance to handle the |
+ // event. If it doesn't want to handle it, it will call us back with |
+ // -redispatchKeyEvent:. |
+ NSResponder* r = [owner_ firstResponder]; |
+ if ([r conformsToProtocol:@protocol(CommandDispatcherTarget)]) |
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)) |
+ if ([delegate_ prePerformKeyEquivalent:event window:owner_]) |
return YES; |
- if ([super performKeyEquivalent:event]) |
+ if ([owner_ defaultPerformKeyEquivalent:event]) |
return YES; |
- // Handle per-window shortcuts like Esc after giving everybody else a chance |
- // to handle them |
- return HandleDelayedWindowKeyboardShortcut(event, self); |
+ return [delegate_ postPerformKeyEquivalent:event window:owner_]; |
} |
- (BOOL)redispatchKeyEvent:(NSEvent*)event { |
DCHECK(event); |
NSEventType eventType = [event type]; |
- if (eventType != NSKeyDown && |
- eventType != NSKeyUp && |
+ if (eventType != NSKeyDown && eventType != NSKeyUp && |
eventType != NSFlagsChanged) { |
NOTREACHED(); |
return YES; // Pretend it's been handled in an effort to limit damage. |
} |
- // 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); |
+ // Ordinarily, the event's window should be |owner_|. 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 |owner_|'s. |
+ if ([event window] != owner_) |
+ event = KeyEventForWindow(owner_, event); |
// Redispatch the event. |
eventHandled_ = YES; |
@@ -178,11 +116,15 @@ NSEvent* KeyEventForWindow(NSWindow* window, NSEvent* event) { |
return eventHandled_; |
} |
-- (void)sendEvent:(NSEvent*)event { |
- if (!redispatchingEvent_) |
- [super sendEvent:event]; |
- else |
+- (BOOL)preSendEvent:(NSEvent*)event { |
+ if (redispatchingEvent_) { |
+ // If we get here, then the event was not handled by NSApplication. |
eventHandled_ = NO; |
+ // Return YES to stop native -sendEvent handling. |
+ return YES; |
+ } |
+ |
+ return NO; |
} |
-@end // ChromeEventProcessingWindow |
+@end |