Index: ui/base/cocoa/command_dispatcher.h |
diff --git a/ui/base/cocoa/command_dispatcher.h b/ui/base/cocoa/command_dispatcher.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8d1ebab22cab69b5cdaf65d26bdca94c029d13c1 |
--- /dev/null |
+++ b/ui/base/cocoa/command_dispatcher.h |
@@ -0,0 +1,96 @@ |
+// 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. |
+ |
+#ifndef UI_BASE_COCOA_COMMAND_DISPATCHER_H_ |
+#define UI_BASE_COCOA_COMMAND_DISPATCHER_H_ |
+ |
+#import <Cocoa/Cocoa.h> |
+ |
+#import "base/mac/scoped_nsobject.h" |
+ |
+@protocol CommandDispatcherDelegate; |
+@protocol CommandDispatchingWindow; |
+ |
+// CommandDispatcher guides the processing of key events to ensure key commands |
+// are executed in the appropriate order. In particular, it allows a first |
+// responder implementing CommandDispatcherTarget to handle an event |
+// asynchronously and return unhandled events via -redispatchKeyEvent. An |
+// NSWindow can use CommandDispatcher by implementing CommandDispatchingWindow |
+// and overriding -[NSWindow -performKeyEquivalent:] and -[NSWindow -sendEvent:] |
Robert Sesek
2015/08/26 17:46:49
nit: -[NSWindow performkeyEquivalent:] (the - is a
jackhou1
2015/08/27 00:01:25
Done.
|
+// to call the respective CommandDispatcher methods. |
+@interface CommandDispatcher : NSObject { |
+ @private |
Robert Sesek
2015/08/26 17:46:49
These ivars can be hidden in the .mm:
@implementa
jackhou1
2015/08/27 00:01:25
Done.
|
+ BOOL redispatchingEvent_; |
+ BOOL eventHandled_; |
+ NSWindow<CommandDispatchingWindow>* owner_; // Weak, owns us. |
+} |
+ |
+@property(retain, nonatomic) id<CommandDispatcherDelegate> delegate; |
Robert Sesek
2015/08/26 17:46:49
Delegates aren't usually retained. Maybe make a no
jackhou1
2015/08/27 00:01:25
Looking at this again, the delegate should be owne
|
+ |
+- (id)initWithOwner:(NSWindow<CommandDispatchingWindow>*)owner; |
Robert Sesek
2015/08/26 17:46:49
instancetype
jackhou1
2015/08/27 00:01:25
Done.
|
+ |
+// The main entry point for key events. The CommandDispatchingWindow should |
+// override -[NSResponder performKeyEquivalent:] and call this instead. Returns |
+// YES if the event is handled. |
+- (BOOL)performKeyEquivalent:(NSEvent*)event; |
+ |
+// Sends a key event to -[NSApp sendEvent:], but also ensures it is not short- |
+// circuited to the CommandDispatcherTarget. This is used to allow default |
Robert Sesek
2015/08/26 17:46:49
" but also ensures it is not short-circuited to th
jackhou1
2015/08/27 00:01:25
Deleted that bit and added a new sentence.
|
+// AppKit handling of an event that comes back from CommandDispatcherTarget, |
+// e.g. key equivalents in the menu, or window manager commands like Cmd+`. The |
+// event must be of type |NSKeyDown|, |NSKeyUp|, or |NSFlagsChanged|. Returns |
+// YES if the event is handled. |
+- (BOOL)redispatchKeyEvent:(NSEvent*)event; |
+ |
+// The CommandDispatchingWindow should call this before a native -sendEvent. |
+// Ensures that a redispatched event is not reposted infinitely. Returns YES if |
+// the event is handled. |
+- (BOOL)preSendEvent:(NSEvent*)event; |
Robert Sesek
2015/08/26 17:46:49
Should this be ordered before -redispatchKeyEvent:
jackhou1
2015/08/27 00:01:25
This is the order that the event is handled, i.e.
|
+ |
+@end |
+ |
+// If the NSWindow's firstResponder implements CommandDispatcherTarget, it is |
+// given the first opportunity to process a command. |
+@protocol CommandDispatcherTarget |
+ |
+// To handle an event asynchronously, return YES. If the event is ultimately not |
+// handled, return the event to the CommandDispatchingWindow via -[[event |
+// window] redispatchKeyEvent:event]. |
+- (BOOL)performKeyEquivalent:(NSEvent*)event; |
+ |
+@end |
+ |
+// Provides CommandDispatcher with the means to redirect key equivalents at |
+// different stages of event handling. |
+@protocol CommandDispatcherDelegate<NSObject> |
+ |
+// Called before any other event handling, and possibly again if an unhandled |
+// event comes back from CommandDispatcherTarget. |
+- (BOOL)handledByExtensionCommand:(NSEvent*)event |
Robert Sesek
2015/08/26 17:46:49
naming: eventHandledByExtensionCommand: ?
jackhou1
2015/08/27 00:01:25
Done.
|
+ isRedispatch:(BOOL)isRedispatch; |
+ |
+// Called before the default -performKeyEquivalent, but after the |
+// CommandDispatcherTarget has had a chance to intercept it. |
+- (BOOL)prePerformKeyEquivalent:(NSEvent*)event window:(NSWindow*)window; |
Robert Sesek
2015/08/26 17:46:49
What |window| is this?
jackhou1
2015/08/27 00:01:25
Added comment.
|
+ |
+// Called after the default -performKeyEquivalent. |
+- (BOOL)postPerformKeyEquivalent:(NSEvent*)event window:(NSWindow*)window; |
+ |
+@end |
+ |
+// The set of methods an NSWindow subclass needs to implement to use |
+// CommandDispatcher. |
+@protocol CommandDispatchingWindow |
+ |
+// This can be implemented with -[CommandDispatcher -redispatchKeyEvent:]. It's |
Robert Sesek
2015/08/26 17:46:49
nit: no - in the []
jackhou1
2015/08/27 00:01:25
Done.
|
+// so that callers can simply return events to the NSWindow. |
+- (BOOL)redispatchKeyEvent:(NSEvent*)event; |
+ |
+// Short-circuit to the default -[NSResponder performKeyEquivalent:] which |
+// CommandDispatcher calls as part of its -performKeyEquivalent flow. |
+- (BOOL)defaultPerformKeyEquivalent:(NSEvent*)event; |
+ |
+@end |
+ |
+#endif // UI_BASE_COCOA_COMMAND_DISPATCHER_H_ |