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 |
| 12 @protocol CommandDispatcherDelegate; |
| 13 @protocol CommandDispatchingWindow; |
| 14 @protocol UserInterfaceItemCommandHandler; |
| 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 @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<NSUserInterfaceValidations> |
| 83 |
| 84 @property(assign, nonatomic) id<UserInterfaceItemCommandHandler> commandHandler; |
| 85 |
| 86 // This can be implemented with -[CommandDispatcher redispatchKeyEvent:]. It's |
| 87 // so that callers can simply return events to the NSWindow. |
| 88 - (BOOL)redispatchKeyEvent:(NSEvent*)event; |
| 89 |
| 90 // Short-circuit to the default -[NSResponder performKeyEquivalent:] which |
| 91 // CommandDispatcher calls as part of its -performKeyEquivalent flow. |
| 92 - (BOOL)defaultPerformKeyEquivalent:(NSEvent*)event; |
| 93 |
| 94 // AppKit will call -[NSUserInterfaceValidations validateUserInterfaceItem:] to |
| 95 // validate UI items. Any item whose target is FirstResponder, or nil, will |
| 96 // traverse the responder chain looking for a responder that implements the |
| 97 // item's selector. Thus NSWindow is usually the last to be checked and will |
| 98 // handle any items that are not validated elsewhere in the chain. Implement the |
| 99 // following so that menu items with these selectors are validated by |
| 100 // CommandDispatchingWindow. |
| 101 - (void)commandDispatch:(id)sender; |
| 102 - (void)commandDispatchUsingKeyModifiers:(id)sender; |
| 103 |
| 104 @end |
| 105 |
| 106 // Used by CommandDispatchingWindow to implement UI item validation. |
| 107 @protocol UserInterfaceItemCommandHandler |
| 108 |
| 109 // Called by CommandDispatchingWindow to validate menu and toolbar items. All |
| 110 // the items we care about have been set with the -commandDispatch or |
| 111 // -commandDispatchUsingKeyModifiers selectors and a target of FirstResponder in |
| 112 // IB. If it's not one of those, it should be handled elsewhere in the responder |
| 113 // chain. |
| 114 - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)item |
| 115 window:(NSWindow*)window; |
| 116 |
| 117 // Called by CommandDispatchingWindow to execute commands. This assumes that the |
| 118 // command is supported and doesn't check, otherwise it would have been disabled |
| 119 // in the UI in validateUserInterfaceItem:. |
| 120 - (void)commandDispatch:(id)sender window:(NSWindow*)window; |
| 121 |
| 122 // Same as |-commandDispatch:|, but executes commands using a disposition |
| 123 // determined by the key flags. If the window is in the background and the |
| 124 // command key is down, ignore the command key, but process any other modifiers. |
| 125 - (void)commandDispatchUsingKeyModifiers:(id)sender window:(NSWindow*)window; |
| 126 |
| 127 @end |
| 128 |
| 129 #endif // UI_BASE_COCOA_COMMAND_DISPATCHER_H_ |
OLD | NEW |