Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" | 5 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/global_keyboard_shortcuts_mac.h" | 8 #include "chrome/browser/global_keyboard_shortcuts_mac.h" |
| 9 #import "chrome/browser/ui/cocoa/browser_command_executor.h" | 9 #include "chrome/browser/ui/browser_commands.h" |
| 10 #include "chrome/browser/ui/browser_finder.h" | |
| 10 #import "chrome/browser/ui/cocoa/browser_window_controller_private.h" | 11 #import "chrome/browser/ui/cocoa/browser_window_controller_private.h" |
| 11 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" | 12 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" |
| 12 #import "content/public/browser/render_widget_host_view_mac_base.h" | 13 #import "content/public/browser/render_widget_host_view_mac_base.h" |
| 13 | 14 |
| 14 typedef int (*KeyToCommandMapper)(bool, bool, bool, bool, int, unichar); | 15 typedef int (*KeyToCommandMapper)(bool, bool, bool, bool, int, unichar); |
| 15 | 16 |
| 16 @interface ChromeEventProcessingWindow () | 17 @interface ChromeEventProcessingWindow () |
| 17 // Duplicate the given key event, but changing the associated window. | 18 // Duplicate the given key event, but changing the associated window. |
| 18 - (NSEvent*)keyEventForWindow:(NSWindow*)window fromKeyEvent:(NSEvent*)event; | 19 + (NSEvent*)keyEventForWindow:(NSWindow*)window fromKeyEvent:(NSEvent*)event; |
|
tapted
2015/07/27 04:58:57
Should this be a function just declared in an anon
jackhou1
2015/07/28 05:19:45
Made it anonymous.
This will be factored out into
| |
| 19 @end | 20 @end |
| 20 | 21 |
| 21 @implementation ChromeEventProcessingWindow | 22 @implementation ChromeEventProcessingWindow |
| 22 | 23 |
| 23 - (BOOL)handleExtraKeyboardShortcut:(NSEvent*)event fromTable: | 24 - (BOOL)handleExtraKeyboardShortcut:(NSEvent*)event |
|
tapted
2015/07/27 04:58:57
Can this just be a function in an anonymous namesp
jackhou1
2015/07/28 05:19:45
Done.
| |
| 24 (KeyToCommandMapper)commandForKeyboardShortcut { | 25 fromTable: |
| 26 (KeyToCommandMapper)commandForKeyboardShortcut { | |
| 25 // Extract info from |event|. | 27 // Extract info from |event|. |
| 26 NSUInteger modifers = [event modifierFlags]; | 28 NSUInteger modifers = [event modifierFlags]; |
| 27 const bool cmdKey = modifers & NSCommandKeyMask; | 29 const bool cmdKey = modifers & NSCommandKeyMask; |
| 28 const bool shiftKey = modifers & NSShiftKeyMask; | 30 const bool shiftKey = modifers & NSShiftKeyMask; |
| 29 const bool cntrlKey = modifers & NSControlKeyMask; | 31 const bool cntrlKey = modifers & NSControlKeyMask; |
| 30 const bool optKey = modifers & NSAlternateKeyMask; | 32 const bool optKey = modifers & NSAlternateKeyMask; |
| 31 const unichar keyCode = [event keyCode]; | 33 const unichar keyCode = [event keyCode]; |
|
tapted
2015/07/27 04:58:57
hrm. this should probably be `int`. [NSEvent keyCo
jackhou1
2015/07/28 05:19:45
Done.
| |
| 32 const unichar keyChar = KeyCharacterForEvent(event); | 34 const unichar keyChar = KeyCharacterForEvent(event); |
| 33 | 35 |
| 34 int cmdNum = commandForKeyboardShortcut(cmdKey, shiftKey, cntrlKey, optKey, | 36 int cmdNum = commandForKeyboardShortcut(cmdKey, shiftKey, cntrlKey, optKey, |
| 35 keyCode, keyChar); | 37 keyCode, keyChar); |
| 36 | 38 |
| 37 if (cmdNum != -1) { | 39 if (cmdNum == -1) |
| 38 id executor = [self delegate]; | 40 return NO; |
| 39 // A bit of sanity. | 41 |
| 40 DCHECK([executor conformsToProtocol:@protocol(BrowserCommandExecutor)]); | 42 // Only handle event if this is a browser window. |
| 41 DCHECK([executor respondsToSelector:@selector(executeCommand:)]); | 43 Browser* browser = chrome::FindBrowserWithWindow(event.window); |
|
tapted
2015/07/27 04:58:57
Pretty sure that it's not guaranteed that event.wi
jackhou1
2015/07/28 05:19:45
This is also code that we want to factor out, so c
| |
| 42 [executor executeCommand:cmdNum]; | 44 if (!browser) |
| 43 return YES; | 45 return NO; |
| 44 } | 46 |
| 45 return NO; | 47 chrome::ExecuteCommand(browser, cmdNum); |
| 48 return YES; | |
| 46 } | 49 } |
| 47 | 50 |
| 48 - (BOOL)handleExtraWindowKeyboardShortcut:(NSEvent*)event { | 51 - (BOOL)handleExtraWindowKeyboardShortcut:(NSEvent*)event { |
| 49 return [self handleExtraKeyboardShortcut:event | 52 return [self handleExtraKeyboardShortcut:event |
| 50 fromTable:CommandForWindowKeyboardShortcut]; | 53 fromTable:CommandForWindowKeyboardShortcut]; |
| 51 } | 54 } |
| 52 | 55 |
| 53 - (BOOL)handleDelayedWindowKeyboardShortcut:(NSEvent*)event { | 56 - (BOOL)handleDelayedWindowKeyboardShortcut:(NSEvent*)event { |
| 54 return [self handleExtraKeyboardShortcut:event | 57 return [self handleExtraKeyboardShortcut:event |
| 55 fromTable:CommandForDelayedWindowKeyboardShortcut]; | 58 fromTable:CommandForDelayedWindowKeyboardShortcut]; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 79 | 82 |
| 80 if (redispatchingEvent_) | 83 if (redispatchingEvent_) |
| 81 return NO; | 84 return NO; |
| 82 | 85 |
| 83 // Give the web site a chance to handle the event. If it doesn't want to | 86 // Give the web site a chance to handle the event. If it doesn't want to |
| 84 // handle it, it will call us back with one of the |handle*| methods above. | 87 // handle it, it will call us back with one of the |handle*| methods above. |
| 85 NSResponder* r = [self firstResponder]; | 88 NSResponder* r = [self firstResponder]; |
| 86 if ([r conformsToProtocol:@protocol(RenderWidgetHostViewMacBase)]) | 89 if ([r conformsToProtocol:@protocol(RenderWidgetHostViewMacBase)]) |
| 87 return [r performKeyEquivalent:event]; | 90 return [r performKeyEquivalent:event]; |
| 88 | 91 |
| 89 // If the delegate does not implement the BrowserCommandExecutor protocol, | |
| 90 // then we don't need to handle browser specific shortcut keys. | |
| 91 if (![[self delegate] conformsToProtocol:@protocol(BrowserCommandExecutor)]) | |
| 92 return [super performKeyEquivalent:event]; | |
| 93 | |
| 94 // Handle per-window shortcuts like cmd-1, but do not handle browser-level | 92 // Handle per-window shortcuts like cmd-1, but do not handle browser-level |
| 95 // shortcuts like cmd-left (else, cmd-left would do history navigation even | 93 // shortcuts like cmd-left (else, cmd-left would do history navigation even |
| 96 // if e.g. the Omnibox has focus). | 94 // if e.g. the Omnibox has focus). |
| 97 if ([self handleExtraWindowKeyboardShortcut:event]) | 95 if ([self handleExtraWindowKeyboardShortcut:event]) |
| 98 return YES; | 96 return YES; |
| 99 | 97 |
| 100 if ([super performKeyEquivalent:event]) | 98 if ([super performKeyEquivalent:event]) |
| 101 return YES; | 99 return YES; |
| 102 | 100 |
| 103 // Handle per-window shortcuts like Esc after giving everybody else a chance | 101 // Handle per-window shortcuts like Esc after giving everybody else a chance |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 114 NOTREACHED(); | 112 NOTREACHED(); |
| 115 return YES; // Pretend it's been handled in an effort to limit damage. | 113 return YES; // Pretend it's been handled in an effort to limit damage. |
| 116 } | 114 } |
| 117 | 115 |
| 118 // Ordinarily, the event's window should be this window. However, when | 116 // Ordinarily, the event's window should be this window. However, when |
| 119 // switching between normal and fullscreen mode, we switch out the window, and | 117 // switching between normal and fullscreen mode, we switch out the window, and |
| 120 // the event's window might be the previous window (or even an earlier one if | 118 // the event's window might be the previous window (or even an earlier one if |
| 121 // the renderer is running slowly and several mode switches occur). In this | 119 // the renderer is running slowly and several mode switches occur). In this |
| 122 // rare case, we synthesize a new key event so that its associate window | 120 // rare case, we synthesize a new key event so that its associate window |
| 123 // (number) is our own. | 121 // (number) is our own. |
| 124 if ([event window] != self) | 122 if ([event window] != self) |
|
tapted
2015/07/27 04:58:57
nit: needs curlies (if it stays this way)
jackhou1
2015/07/28 05:19:45
Done.
| |
| 125 event = [self keyEventForWindow:self fromKeyEvent:event]; | 123 event = |
| 124 [ChromeEventProcessingWindow keyEventForWindow:self fromKeyEvent:event]; | |
| 126 | 125 |
| 127 // Redispatch the event. | 126 // Redispatch the event. |
| 128 eventHandled_ = YES; | 127 eventHandled_ = YES; |
| 129 redispatchingEvent_ = YES; | 128 redispatchingEvent_ = YES; |
| 130 [NSApp sendEvent:event]; | 129 [NSApp sendEvent:event]; |
| 131 redispatchingEvent_ = NO; | 130 redispatchingEvent_ = NO; |
| 132 | 131 |
| 133 // If the event was not handled by [NSApp sendEvent:], the sendEvent: | 132 // If the event was not handled by [NSApp sendEvent:], the sendEvent: |
| 134 // method below will be called, and because |redispatchingEvent_| is YES, | 133 // method below will be called, and because |redispatchingEvent_| is YES, |
| 135 // |eventHandled_| will be set to NO. | 134 // |eventHandled_| will be set to NO. |
| 136 return eventHandled_; | 135 return eventHandled_; |
| 137 } | 136 } |
| 138 | 137 |
| 139 - (void)sendEvent:(NSEvent*)event { | 138 - (void)sendEvent:(NSEvent*)event { |
| 140 if (!redispatchingEvent_) | 139 if (!redispatchingEvent_) |
| 141 [super sendEvent:event]; | 140 [super sendEvent:event]; |
| 142 else | 141 else |
| 143 eventHandled_ = NO; | 142 eventHandled_ = NO; |
| 144 } | 143 } |
| 145 | 144 |
| 146 - (NSEvent*)keyEventForWindow:(NSWindow*)window fromKeyEvent:(NSEvent*)event { | 145 // Private interface. |
| 146 | |
| 147 + (NSEvent*)keyEventForWindow:(NSWindow*)window fromKeyEvent:(NSEvent*)event { | |
| 147 NSEventType eventType = [event type]; | 148 NSEventType eventType = [event type]; |
| 148 | 149 |
| 149 // Convert the event's location from the original window's coordinates into | 150 // Convert the event's location from the original window's coordinates into |
| 150 // our own. | 151 // our own. |
| 151 NSPoint eventLoc = [event locationInWindow]; | 152 NSPoint eventLoc = [event locationInWindow]; |
| 152 eventLoc = [[event window] convertBaseToScreen:eventLoc]; | 153 eventLoc = [[event window] convertBaseToScreen:eventLoc]; |
| 153 eventLoc = [self convertScreenToBase:eventLoc]; | 154 eventLoc = [window convertScreenToBase:eventLoc]; |
| 154 | 155 |
| 155 // Various things *only* apply to key down/up. | 156 // Various things *only* apply to key down/up. |
| 156 BOOL eventIsARepeat = NO; | 157 BOOL eventIsARepeat = NO; |
| 157 NSString* eventCharacters = nil; | 158 NSString* eventCharacters = nil; |
| 158 NSString* eventUnmodCharacters = nil; | 159 NSString* eventUnmodCharacters = nil; |
| 159 if (eventType == NSKeyDown || eventType == NSKeyUp) { | 160 if (eventType == NSKeyDown || eventType == NSKeyUp) { |
| 160 eventIsARepeat = [event isARepeat]; | 161 eventIsARepeat = [event isARepeat]; |
| 161 eventCharacters = [event characters]; | 162 eventCharacters = [event characters]; |
| 162 eventUnmodCharacters = [event charactersIgnoringModifiers]; | 163 eventUnmodCharacters = [event charactersIgnoringModifiers]; |
| 163 } | 164 } |
| 164 | 165 |
| 165 // This synthesis may be slightly imperfect: we provide nil for the context, | 166 // This synthesis may be slightly imperfect: we provide nil for the context, |
| 166 // since I (viettrungluu) am sceptical that putting in the original context | 167 // since I (viettrungluu) am sceptical that putting in the original context |
| 167 // (if one is given) is valid. | 168 // (if one is given) is valid. |
| 168 return [NSEvent keyEventWithType:eventType | 169 return [NSEvent keyEventWithType:eventType |
| 169 location:eventLoc | 170 location:eventLoc |
| 170 modifierFlags:[event modifierFlags] | 171 modifierFlags:[event modifierFlags] |
| 171 timestamp:[event timestamp] | 172 timestamp:[event timestamp] |
| 172 windowNumber:[window windowNumber] | 173 windowNumber:[window windowNumber] |
| 173 context:nil | 174 context:nil |
| 174 characters:eventCharacters | 175 characters:eventCharacters |
| 175 charactersIgnoringModifiers:eventUnmodCharacters | 176 charactersIgnoringModifiers:eventUnmodCharacters |
| 176 isARepeat:eventIsARepeat | 177 isARepeat:eventIsARepeat |
| 177 keyCode:[event keyCode]]; | 178 keyCode:[event keyCode]]; |
| 178 } | 179 } |
| 179 | 180 |
| 180 @end // ChromeEventProcessingWindow | 181 @end // ChromeEventProcessingWindow |
| OLD | NEW |