Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: ui/base/cocoa/command_dispatcher.mm

Issue 1259903002: DO NOT COMMIT: [Mac] Refactor keyboard shortcut handling so it can be reused in Views. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync to new CommandDispatcher design. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/cocoa/command_dispatcher.h ('k') | ui/base/ui_base.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #import "ui/base/cocoa/command_dispatcher.h"
6
7 #include "base/logging.h"
8
9 namespace {
10
11 // Duplicate the given key event, but changing the associated window.
12 NSEvent* KeyEventForWindow(NSWindow* window, NSEvent* event) {
13 NSEventType event_type = [event type];
14
15 // Convert the event's location from the original window's coordinates into
16 // our own.
17 NSPoint location = [event locationInWindow];
18 location = [[event window] convertBaseToScreen:location];
19 location = [window convertScreenToBase:location];
20
21 // Various things *only* apply to key down/up.
22 bool is_a_repeat = false;
23 NSString* characters = nil;
24 NSString* charactors_ignoring_modifiers = nil;
25 if (event_type == NSKeyDown || event_type == NSKeyUp) {
26 is_a_repeat = [event isARepeat];
27 characters = [event characters];
28 charactors_ignoring_modifiers = [event charactersIgnoringModifiers];
29 }
30
31 // This synthesis may be slightly imperfect: we provide nil for the context,
32 // since I (viettrungluu) am sceptical that putting in the original context
33 // (if one is given) is valid.
34 return [NSEvent keyEventWithType:event_type
35 location:location
36 modifierFlags:[event modifierFlags]
37 timestamp:[event timestamp]
38 windowNumber:[window windowNumber]
39 context:nil
40 characters:characters
41 charactersIgnoringModifiers:charactors_ignoring_modifiers
42 isARepeat:is_a_repeat
43 keyCode:[event keyCode]];
44 }
45
46 } // namespace
47
48 @implementation CommandDispatcher {
49 @private
50 BOOL redispatchingEvent_;
51 BOOL eventHandled_;
52 NSWindow<CommandDispatchingWindow>* owner_; // Weak, owns us.
53 }
54
55 @synthesize delegate = delegate_;
56
57 - (instancetype)initWithOwner:(NSWindow<CommandDispatchingWindow>*)owner {
58 if ((self = [super init])) {
59 owner_ = owner;
60 }
61 return self;
62 }
63
64 - (BOOL)performKeyEquivalent:(NSEvent*)event {
65 if ([delegate_ eventHandledByExtensionCommand:event
66 isRedispatch:redispatchingEvent_]) {
67 return YES;
68 }
69
70 if (redispatchingEvent_)
71 return NO;
72
73 // Give a CommandDispatcherTarget (e.g. a web site) a chance to handle the
74 // event. If it doesn't want to handle it, it will call us back with one of
75 // the |handle*| methods above.
76 NSResponder* r = [owner_ firstResponder];
77 if ([r conformsToProtocol:@protocol(CommandDispatcherTarget)])
78 return [r performKeyEquivalent:event];
79
80 if ([delegate_ prePerformKeyEquivalent:event window:owner_])
81 return YES;
82
83 if ([owner_ defaultPerformKeyEquivalent:event])
84 return YES;
85
86 return [delegate_ postPerformKeyEquivalent:event window:owner_];
87 }
88
89 - (BOOL)redispatchKeyEvent:(NSEvent*)event {
90 DCHECK(event);
91 NSEventType eventType = [event type];
92 if (eventType != NSKeyDown && eventType != NSKeyUp &&
93 eventType != NSFlagsChanged) {
94 NOTREACHED();
95 return YES; // Pretend it's been handled in an effort to limit damage.
96 }
97
98 // Ordinarily, the event's window should be |owner_|. However, when switching
99 // between normal and fullscreen mode, we switch out the window, and the
100 // event's window might be the previous window (or even an earlier one if the
101 // renderer is running slowly and several mode switches occur). In this rare
102 // case, we synthesize a new key event so that its associate window (number)
103 // is our |owner_|'s.
104 if ([event window] != owner_)
105 event = KeyEventForWindow(owner_, event);
106
107 // Redispatch the event.
108 eventHandled_ = YES;
109 redispatchingEvent_ = YES;
110 [NSApp sendEvent:event];
111 redispatchingEvent_ = NO;
112
113 // If the event was not handled by [NSApp sendEvent:], the sendEvent:
114 // method below will be called, and because |redispatchingEvent_| is YES,
115 // |eventHandled_| will be set to NO.
116 return eventHandled_;
117 }
118
119 - (BOOL)preSendEvent:(NSEvent*)event {
120 if (redispatchingEvent_) {
121 // If we get here, then the event was not handled by NSApplication.
122 eventHandled_ = NO;
123 // Return YES to stop native -sendEvent handling.
124 return YES;
125 }
126
127 return NO;
128 }
129
130 @end
OLDNEW
« no previous file with comments | « ui/base/cocoa/command_dispatcher.h ('k') | ui/base/ui_base.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698