OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef UI_BASE_COCOA_COMMAND_DISPATCHER_H_ |
| 6 #define UI_BASE_COCOA_COMMAND_DISPATCHER_H_ |
| 7 |
| 8 #import <Cocoa/Cocoa.h> |
| 9 |
| 10 #import "base/mac/scoped_nsobject.h" |
| 11 #include "ui/base/ui_base_export.h" |
| 12 |
| 13 @protocol CommandDispatcherDelegate; |
| 14 @protocol CommandDispatchingWindow; |
| 15 |
| 16 // CommandDispatcher guides the processing of key events to ensure key commands |
| 17 // are executed in the appropriate order. In particular, it allows a first |
| 18 // responder implementing CommandDispatcherTarget to handle an event |
| 19 // asynchronously and return unhandled events via -redispatchKeyEvent:. An |
| 20 // NSWindow can use CommandDispatcher by implementing CommandDispatchingWindow |
| 21 // and overriding -[NSWindow performKeyEquivalent:] and -[NSWindow sendEvent:] |
| 22 // to call the respective CommandDispatcher methods. |
| 23 UI_BASE_EXPORT @interface CommandDispatcher : NSObject |
| 24 |
| 25 @property(assign, nonatomic) id<CommandDispatcherDelegate> delegate; |
| 26 |
| 27 - (instancetype)initWithOwner:(NSWindow<CommandDispatchingWindow>*)owner; |
| 28 |
| 29 // The main entry point for key events. The CommandDispatchingWindow should |
| 30 // override -[NSResponder performKeyEquivalent:] and call this instead. Returns |
| 31 // YES if the event is handled. |
| 32 - (BOOL)performKeyEquivalent:(NSEvent*)event; |
| 33 |
| 34 // Sends a key event to -[NSApp sendEvent:]. This is used to allow default |
| 35 // AppKit handling of an event that comes back from CommandDispatcherTarget, |
| 36 // e.g. key equivalents in the menu, or window manager commands like Cmd+`. Once |
| 37 // the event returns to the window at -preSendEvent:, handling will stop. The |
| 38 // event must be of type |NSKeyDown|, |NSKeyUp|, or |NSFlagsChanged|. Returns |
| 39 // YES if the event is handled. |
| 40 - (BOOL)redispatchKeyEvent:(NSEvent*)event; |
| 41 |
| 42 // The CommandDispatchingWindow should override -[NSWindow sendEvent:] and call |
| 43 // this before a native -sendEvent:. Ensures that a redispatched event is not |
| 44 // reposted infinitely. Returns YES if the event is handled. |
| 45 - (BOOL)preSendEvent:(NSEvent*)event; |
| 46 |
| 47 @end |
| 48 |
| 49 // If the NSWindow's firstResponder implements CommandDispatcherTarget, it is |
| 50 // given the first opportunity to process a command. |
| 51 @protocol CommandDispatcherTarget |
| 52 |
| 53 // To handle an event asynchronously, return YES. If the event is ultimately not |
| 54 // handled, return the event to the CommandDispatchingWindow via -[[event |
| 55 // window] redispatchKeyEvent:event]. |
| 56 - (BOOL)performKeyEquivalent:(NSEvent*)event; |
| 57 |
| 58 @end |
| 59 |
| 60 // Provides CommandDispatcher with the means to redirect key equivalents at |
| 61 // different stages of event handling. |
| 62 @protocol CommandDispatcherDelegate<NSObject> |
| 63 |
| 64 // Called before any other event handling, and possibly again if an unhandled |
| 65 // event comes back from CommandDispatcherTarget. |
| 66 - (BOOL)eventHandledByExtensionCommand:(NSEvent*)event |
| 67 isRedispatch:(BOOL)isRedispatch; |
| 68 |
| 69 // Called before the default -performKeyEquivalent:, but after the |
| 70 // CommandDispatcherTarget has had a chance to intercept it. |window| is the |
| 71 // CommandDispatchingWindow that owns CommandDispatcher. |
| 72 - (BOOL)prePerformKeyEquivalent:(NSEvent*)event window:(NSWindow*)window; |
| 73 |
| 74 // Called after the default -performKeyEquivalent:. |window| is the |
| 75 // CommandDispatchingWindow that owns CommandDispatcher. |
| 76 - (BOOL)postPerformKeyEquivalent:(NSEvent*)event window:(NSWindow*)window; |
| 77 |
| 78 @end |
| 79 |
| 80 // The set of methods an NSWindow subclass needs to implement to use |
| 81 // CommandDispatcher. |
| 82 @protocol CommandDispatchingWindow |
| 83 |
| 84 // This can be implemented with -[CommandDispatcher redispatchKeyEvent:]. It's |
| 85 // so that callers can simply return events to the NSWindow. |
| 86 - (BOOL)redispatchKeyEvent:(NSEvent*)event; |
| 87 |
| 88 // Short-circuit to the default -[NSResponder performKeyEquivalent:] which |
| 89 // CommandDispatcher calls as part of its -performKeyEquivalent: flow. |
| 90 - (BOOL)defaultPerformKeyEquivalent:(NSEvent*)event; |
| 91 |
| 92 @end |
| 93 |
| 94 #endif // UI_BASE_COCOA_COMMAND_DISPATCHER_H_ |
OLD | NEW |