forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlutterKeyboardManager.mm
More file actions
359 lines (310 loc) · 12.9 KB
/
FlutterKeyboardManager.mm
File metadata and controls
359 lines (310 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.h"
#include <cctype>
#include <map>
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponder.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponder.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyPrimaryResponder.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/KeyCodeMap_Internal.h"
// Turn on this flag to print complete layout data when switching IMEs. The data
// is used in unit tests.
// #define DEBUG_PRINT_LAYOUT
namespace {
using flutter::LayoutClue;
using flutter::LayoutGoal;
#ifdef DEBUG_PRINT_LAYOUT
// Prints layout entries that will be parsed by `MockLayoutData`.
NSString* debugFormatLayoutData(NSString* debugLayoutData,
uint16_t keyCode,
LayoutClue clue1,
LayoutClue clue2) {
return [NSString
stringWithFormat:@" %@%@0x%d%04x, 0x%d%04x,", debugLayoutData,
keyCode % 4 == 0 ? [NSString stringWithFormat:@"\n/* 0x%02x */ ", keyCode]
: @" ",
clue1.isDeadKey, clue1.character, clue2.isDeadKey, clue2.character];
}
#endif
// Someohow this pointer type must be defined as a single type for the compiler
// to compile the function pointer type (due to _Nullable).
typedef NSResponder* _NSResponderPtr;
typedef _Nullable _NSResponderPtr (^NextResponderProvider)();
bool isEascii(const LayoutClue& clue) {
return clue.character < 256 && !clue.isDeadKey;
}
typedef void (^VoidBlock)();
// Someohow this pointer type must be defined as a single type for the compiler
// to compile the function pointer type (due to _Nullable).
typedef NSResponder* _NSResponderPtr;
typedef _Nullable _NSResponderPtr (^NextResponderProvider)();
} // namespace
@interface FlutterKeyboardManager ()
/**
* The text input plugin set by initialization.
*/
@property(nonatomic, weak) id<FlutterKeyboardViewDelegate> viewDelegate;
/**
* The primary responders added by addPrimaryResponder.
*/
@property(nonatomic) NSMutableArray<id<FlutterKeyPrimaryResponder>>* primaryResponders;
@property(nonatomic) NSMutableArray<NSEvent*>* pendingEvents;
@property(nonatomic) BOOL processingEvent;
@property(nonatomic) NSMutableDictionary<NSNumber*, NSNumber*>* layoutMap;
@property(nonatomic, nullable) NSEvent* eventBeingDispatched;
/**
* Add a primary responder, which asynchronously decides whether to handle an
* event.
*/
- (void)addPrimaryResponder:(nonnull id<FlutterKeyPrimaryResponder>)responder;
/**
* Start processing the next event if not started already.
*
* This function might initiate an async process, whose callback calls this
* function again.
*/
- (void)processNextEvent;
/**
* Implement how to process an event.
*
* The `onFinish` must be called eventually, either during this function or
* asynchronously later, otherwise the event queue will be stuck.
*
* This function is called by processNextEvent.
*/
- (void)performProcessEvent:(NSEvent*)event onFinish:(nonnull VoidBlock)onFinish;
/**
* Dispatch an event that's not hadled by the responders to text input plugin,
* and potentially to the next responder.
*/
- (void)dispatchTextEvent:(nonnull NSEvent*)pendingEvent;
/**
* Clears the current layout and build a new one based on the current layout.
*/
- (void)buildLayout;
@end
@implementation FlutterKeyboardManager {
NextResponderProvider _getNextResponder;
}
- (nonnull instancetype)initWithViewDelegate:(nonnull id<FlutterKeyboardViewDelegate>)viewDelegate {
self = [super init];
if (self != nil) {
_processingEvent = FALSE;
_viewDelegate = viewDelegate;
FlutterMethodChannel* keyboardChannel =
[FlutterMethodChannel methodChannelWithName:@"flutter/keyboard"
binaryMessenger:[_viewDelegate getBinaryMessenger]
codec:[FlutterStandardMethodCodec sharedInstance]];
[keyboardChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
[self handleKeyboardMethodCall:call result:result];
}];
_primaryResponders = [[NSMutableArray alloc] init];
[self addPrimaryResponder:[[FlutterEmbedderKeyResponder alloc]
initWithSendEvent:^(const FlutterKeyEvent& event,
FlutterKeyEventCallback callback,
void* userData) {
[_viewDelegate sendKeyEvent:event
callback:callback
userData:userData];
}]];
[self
addPrimaryResponder:[[FlutterChannelKeyResponder alloc]
initWithChannel:[FlutterBasicMessageChannel
messageChannelWithName:@"flutter/keyevent"
binaryMessenger:[_viewDelegate
getBinaryMessenger]
codec:[FlutterJSONMessageCodec
sharedInstance]]]];
_pendingEvents = [[NSMutableArray alloc] init];
_layoutMap = [NSMutableDictionary<NSNumber*, NSNumber*> dictionary];
[self buildLayout];
for (id<FlutterKeyPrimaryResponder> responder in _primaryResponders) {
responder.layoutMap = _layoutMap;
}
__weak __typeof__(self) weakSelf = self;
[_viewDelegate subscribeToKeyboardLayoutChange:^() {
[weakSelf buildLayout];
}];
}
return self;
}
- (void)handleKeyboardMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([[call method] isEqualToString:@"getKeyboardState"]) {
result([self getPressedState]);
} else {
result(FlutterMethodNotImplemented);
}
}
- (void)addPrimaryResponder:(nonnull id<FlutterKeyPrimaryResponder>)responder {
[_primaryResponders addObject:responder];
}
- (void)handleEvent:(nonnull NSEvent*)event {
// The `handleEvent` does not process the event immediately, but instead put
// events into a queue. Events are processed one by one by `processNextEvent`.
// Be sure to add a handling method in propagateKeyEvent when allowing more
// event types here.
if (event.type != NSEventTypeKeyDown && event.type != NSEventTypeKeyUp &&
event.type != NSEventTypeFlagsChanged) {
return;
}
[_pendingEvents addObject:event];
[self processNextEvent];
}
- (BOOL)isDispatchingKeyEvent:(NSEvent*)event {
return _eventBeingDispatched == event;
}
#pragma mark - Private
- (void)processNextEvent {
@synchronized(self) {
if (_processingEvent || [_pendingEvents count] == 0) {
return;
}
_processingEvent = TRUE;
}
NSEvent* pendingEvent = [_pendingEvents firstObject];
[_pendingEvents removeObjectAtIndex:0];
__weak __typeof__(self) weakSelf = self;
VoidBlock onFinish = ^() {
weakSelf.processingEvent = FALSE;
[weakSelf processNextEvent];
};
[self performProcessEvent:pendingEvent onFinish:onFinish];
}
- (void)performProcessEvent:(NSEvent*)event onFinish:(VoidBlock)onFinish {
// Having no primary responders require extra logic, but Flutter hard-codes
// all primary responders, so this is a situation that Flutter will never
// encounter.
NSAssert([_primaryResponders count] >= 0, @"At least one primary responder must be added.");
__weak __typeof__(self) weakSelf = self;
__block int unreplied = [_primaryResponders count];
__block BOOL anyHandled = false;
FlutterAsyncKeyCallback replyCallback = ^(BOOL handled) {
unreplied -= 1;
NSAssert(unreplied >= 0, @"More primary responders replied than possible.");
anyHandled = anyHandled || handled;
if (unreplied == 0) {
if (!anyHandled) {
[weakSelf dispatchTextEvent:event];
}
onFinish();
}
};
for (id<FlutterKeyPrimaryResponder> responder in _primaryResponders) {
[responder handleEvent:event callback:replyCallback];
}
}
- (void)dispatchTextEvent:(NSEvent*)event {
if ([_viewDelegate onTextInputKeyEvent:event]) {
return;
}
NSResponder* nextResponder = _viewDelegate.nextResponder;
if (nextResponder == nil) {
return;
}
NSAssert(_eventBeingDispatched == nil, @"An event is already being dispached.");
_eventBeingDispatched = event;
switch (event.type) {
case NSEventTypeKeyDown:
if ([nextResponder respondsToSelector:@selector(keyDown:)]) {
[nextResponder keyDown:event];
}
break;
case NSEventTypeKeyUp:
if ([nextResponder respondsToSelector:@selector(keyUp:)]) {
[nextResponder keyUp:event];
}
break;
case NSEventTypeFlagsChanged:
if ([nextResponder respondsToSelector:@selector(flagsChanged:)]) {
[nextResponder flagsChanged:event];
}
break;
default:
NSAssert(false, @"Unexpected key event type (got %lu).", event.type);
}
NSAssert(_eventBeingDispatched != nil, @"_eventBeingDispatched was cleared unexpectedly.");
_eventBeingDispatched = nil;
}
- (void)buildLayout {
[_layoutMap removeAllObjects];
std::map<uint32_t, LayoutGoal> mandatoryGoalsByChar;
std::map<uint32_t, LayoutGoal> usLayoutGoalsByKeyCode;
for (const LayoutGoal& goal : flutter::kLayoutGoals) {
if (goal.mandatory) {
mandatoryGoalsByChar[goal.keyChar] = goal;
} else {
usLayoutGoalsByKeyCode[goal.keyCode] = goal;
}
}
// Derive key mapping for each key code based on their layout clues.
// Key code 0x00 - 0x32 are typewriter keys (letters, digits, and symbols.)
// See keyCodeToPhysicalKey.
const uint16_t kMaxKeyCode = 0x32;
#ifdef DEBUG_PRINT_LAYOUT
NSString* debugLayoutData = @"";
#endif
for (uint16_t keyCode = 0; keyCode <= kMaxKeyCode; keyCode += 1) {
std::vector<LayoutClue> thisKeyClues = {
[_viewDelegate lookUpLayoutForKeyCode:keyCode shift:false],
[_viewDelegate lookUpLayoutForKeyCode:keyCode shift:true]};
#ifdef DEBUG_PRINT_LAYOUT
debugLayoutData =
debugFormatLayoutData(debugLayoutData, keyCode, thisKeyClues[0], thisKeyClues[1]);
#endif
// The logical key should be the first available clue from below:
//
// - Mandatory goal, if it matches any clue. This ensures that all alnum
// keys can be found somewhere.
// - US layout, if neither clue of the key is EASCII. This ensures that
// there are no non-latin logical keys.
// - Derived on the fly from keyCode & characters.
for (const LayoutClue& clue : thisKeyClues) {
uint32_t keyChar = clue.isDeadKey ? 0 : clue.character;
auto matchingGoal = mandatoryGoalsByChar.find(keyChar);
if (matchingGoal != mandatoryGoalsByChar.end()) {
// Found a key that produces a mandatory char. Use it.
NSAssert(_layoutMap[@(keyCode)] == nil, @"Attempting to assign an assigned key code.");
_layoutMap[@(keyCode)] = @(keyChar);
mandatoryGoalsByChar.erase(matchingGoal);
break;
}
}
bool hasAnyEascii = isEascii(thisKeyClues[0]) || isEascii(thisKeyClues[1]);
// See if any produced char meets the requirement as a logical key.
auto foundUsLayoutGoal = usLayoutGoalsByKeyCode.find(keyCode);
if (foundUsLayoutGoal != usLayoutGoalsByKeyCode.end() && _layoutMap[@(keyCode)] == nil &&
!hasAnyEascii) {
_layoutMap[@(keyCode)] = @(foundUsLayoutGoal->second.keyChar);
}
}
#ifdef DEBUG_PRINT_LAYOUT
NSLog(@"%@", debugLayoutData);
#endif
// Ensure all mandatory goals are assigned.
for (auto mandatoryGoalIter : mandatoryGoalsByChar) {
const LayoutGoal& goal = mandatoryGoalIter.second;
_layoutMap[@(goal.keyCode)] = @(goal.keyChar);
}
}
- (void)syncModifiersIfNeeded:(NSEventModifierFlags)modifierFlags
timestamp:(NSTimeInterval)timestamp {
// The embedder responder is the first element in _primaryResponders.
FlutterEmbedderKeyResponder* embedderResponder =
(FlutterEmbedderKeyResponder*)_primaryResponders[0];
[embedderResponder syncModifiersIfNeeded:modifierFlags timestamp:timestamp];
}
/**
* Returns the keyboard pressed state.
*
* Returns the keyboard pressed state. The dictionary contains one entry per
* pressed keys, mapping from the logical key to the physical key.
*/
- (nonnull NSDictionary*)getPressedState {
// The embedder responder is the first element in _primaryResponders.
FlutterEmbedderKeyResponder* embedderResponder =
(FlutterEmbedderKeyResponder*)_primaryResponders[0];
return [embedderResponder getPressedState];
}
@end