Flutter macOS Embedder
FlutterTextInputPlugin.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 
6 
7 #import <Foundation/Foundation.h>
8 #import <objc/message.h>
9 
10 #include <algorithm>
11 #include <memory>
12 #include <string>
13 
14 #include "flutter/common/constants.h"
15 #include "flutter/fml/platform/darwin/string_range_sanitization.h"
22 
23 static NSString* const kTextInputChannel = @"flutter/textinput";
24 
25 #pragma mark - TextInput channel method names
26 // See https://api.flutter.dev/flutter/services/SystemChannels/textInput-constant.html
27 static NSString* const kSetClientMethod = @"TextInput.setClient";
28 static NSString* const kShowMethod = @"TextInput.show";
29 static NSString* const kHideMethod = @"TextInput.hide";
30 static NSString* const kClearClientMethod = @"TextInput.clearClient";
31 static NSString* const kSetEditingStateMethod = @"TextInput.setEditingState";
32 static NSString* const kSetEditableSizeAndTransform = @"TextInput.setEditableSizeAndTransform";
33 static NSString* const kSetCaretRect = @"TextInput.setCaretRect";
34 static NSString* const kUpdateEditStateResponseMethod = @"TextInputClient.updateEditingState";
36  @"TextInputClient.updateEditingStateWithDeltas";
37 static NSString* const kPerformAction = @"TextInputClient.performAction";
38 static NSString* const kPerformSelectors = @"TextInputClient.performSelectors";
39 static NSString* const kMultilineInputType = @"TextInputType.multiline";
40 
41 #pragma mark - TextInputConfiguration field names
42 static NSString* const kViewId = @"viewId";
43 static NSString* const kSecureTextEntry = @"obscureText";
44 static NSString* const kTextInputAction = @"inputAction";
45 static NSString* const kEnableDeltaModel = @"enableDeltaModel";
46 static NSString* const kTextInputType = @"inputType";
47 static NSString* const kTextInputTypeName = @"name";
48 static NSString* const kSelectionBaseKey = @"selectionBase";
49 static NSString* const kSelectionExtentKey = @"selectionExtent";
50 static NSString* const kSelectionAffinityKey = @"selectionAffinity";
51 static NSString* const kSelectionIsDirectionalKey = @"selectionIsDirectional";
52 static NSString* const kComposingBaseKey = @"composingBase";
53 static NSString* const kComposingExtentKey = @"composingExtent";
54 static NSString* const kTextKey = @"text";
55 static NSString* const kTransformKey = @"transform";
56 static NSString* const kAssociatedAutofillFields = @"fields";
57 
58 // TextInputConfiguration.autofill and sub-field names
59 static NSString* const kAutofillProperties = @"autofill";
60 static NSString* const kAutofillId = @"uniqueIdentifier";
61 static NSString* const kAutofillEditingValue = @"editingValue";
62 static NSString* const kAutofillHints = @"hints";
63 
64 // TextAffinity types
65 static NSString* const kTextAffinityDownstream = @"TextAffinity.downstream";
66 static NSString* const kTextAffinityUpstream = @"TextAffinity.upstream";
67 
68 // TextInputAction types
69 static NSString* const kInputActionNewline = @"TextInputAction.newline";
70 
71 #pragma mark - Enums
72 /**
73  * The affinity of the current cursor position. If the cursor is at a position
74  * representing a soft line break, the cursor may be drawn either at the end of
75  * the current line (upstream) or at the beginning of the next (downstream).
76  */
77 typedef NS_ENUM(NSUInteger, FlutterTextAffinity) {
78  kFlutterTextAffinityUpstream,
79  kFlutterTextAffinityDownstream
80 };
81 
82 #pragma mark - Static functions
83 
84 /*
85  * Updates a range given base and extent fields.
86  */
88  NSNumber* extent,
89  const flutter::TextRange& range) {
90  if (base == nil || extent == nil) {
91  return range;
92  }
93  if (base.intValue == -1 && extent.intValue == -1) {
94  return flutter::TextRange(0, 0);
95  }
96  return flutter::TextRange([base unsignedLongValue], [extent unsignedLongValue]);
97 }
98 
99 // Returns |range| as a TextRange clamped to |length|.
100 //
101 // A selection made backwards is reported as a range whose length is negative
102 // when read as a signed value, so the length is interpreted signed and the
103 // direction of the range is preserved. A location of NSNotFound clamps to
104 // |length|.
105 static flutter::TextRange ClampNSRange(NSRange range, size_t length) {
106  const long text_length = static_cast<long>(length);
107  const long signed_length = std::clamp(static_cast<long>(range.length), -text_length, text_length);
108  const long base = std::clamp(static_cast<long>(range.location), 0L, text_length);
109  return flutter::TextRange(base, std::clamp(base + signed_length, 0L, text_length));
110 }
111 
112 // Returns the autofill hint content type, if specified; otherwise nil.
113 static NSString* GetAutofillHint(NSDictionary* autofill) {
114  NSArray<NSString*>* hints = autofill[kAutofillHints];
115  return hints.count > 0 ? hints[0] : nil;
116 }
117 
118 // Returns the text content type for the specified TextInputConfiguration.
119 // NSTextContentType is only available for macOS 11.0 and later.
120 static NSTextContentType GetTextContentType(NSDictionary* configuration)
121  API_AVAILABLE(macos(11.0)) {
122  // Check autofill hints.
123  NSDictionary* autofill = configuration[kAutofillProperties];
124  if (autofill) {
125  NSString* hint = GetAutofillHint(autofill);
126  if ([hint isEqualToString:@"username"]) {
127  return NSTextContentTypeUsername;
128  }
129  if ([hint isEqualToString:@"password"]) {
130  return NSTextContentTypePassword;
131  }
132  if ([hint isEqualToString:@"oneTimeCode"]) {
133  return NSTextContentTypeOneTimeCode;
134  }
135  }
136  // If no autofill hints, guess based on other attributes.
137  if ([configuration[kSecureTextEntry] boolValue]) {
138  return NSTextContentTypePassword;
139  }
140  return nil;
141 }
142 
143 // Returns YES if configuration describes a field for which autocomplete should be enabled for
144 // the specified TextInputConfiguration. Autocomplete is enabled by default, but will be disabled
145 // if the field is password-related, or if the configuration contains no autofill settings.
146 static BOOL EnableAutocompleteForTextInputConfiguration(NSDictionary* configuration) {
147  // Disable if obscureText is set.
148  if ([configuration[kSecureTextEntry] boolValue]) {
149  return NO;
150  }
151 
152  // Disable if autofill properties are not set.
153  NSDictionary* autofill = configuration[kAutofillProperties];
154  if (autofill == nil) {
155  return NO;
156  }
157 
158  // Disable if autofill properties indicate a username/password.
159  // See: https://github.com/flutter/flutter/issues/119824
160  NSString* hint = GetAutofillHint(autofill);
161  if ([hint isEqualToString:@"password"] || [hint isEqualToString:@"username"]) {
162  return NO;
163  }
164  return YES;
165 }
166 
167 // Returns YES if configuration describes a field for which autocomplete should be enabled.
168 // Autocomplete is enabled by default, but will be disabled if the field is password-related, or if
169 // the configuration contains no autofill settings.
170 //
171 // In the case where the current field is part of an AutofillGroup, the configuration will have
172 // a fields attribute with a list of TextInputConfigurations, one for each field. In the case where
173 // any field in the group disables autocomplete, we disable it for all.
174 static BOOL EnableAutocomplete(NSDictionary* configuration) {
175  for (NSDictionary* field in configuration[kAssociatedAutofillFields]) {
177  return NO;
178  }
179  }
180 
181  // Check the top-level TextInputConfiguration.
182  return EnableAutocompleteForTextInputConfiguration(configuration);
183 }
184 
185 #pragma mark - NSEvent (KeyEquivalentMarker) protocol
186 
188 
189 // Internally marks that the event was received through performKeyEquivalent:.
190 // When text editing is active, keyboard events that have modifier keys pressed
191 // are received through performKeyEquivalent: instead of keyDown:. If such event
192 // is passed to TextInputContext but doesn't result in a text editing action it
193 // needs to be forwarded by FlutterKeyboardManager to the next responder.
194 - (void)markAsKeyEquivalent;
195 
196 // Returns YES if the event is marked as a key equivalent.
197 - (BOOL)isKeyEquivalent;
198 
199 @end
200 
201 @implementation NSEvent (KeyEquivalentMarker)
202 
203 // This field doesn't need a value because only its address is used as a unique identifier.
204 static char markerKey;
205 
206 - (void)markAsKeyEquivalent {
207  objc_setAssociatedObject(self, &markerKey, @true, OBJC_ASSOCIATION_RETAIN);
208 }
209 
210 - (BOOL)isKeyEquivalent {
211  return [objc_getAssociatedObject(self, &markerKey) boolValue] == YES;
212 }
213 
214 @end
215 
216 #pragma mark - FlutterTextInputPlugin private interface
217 
218 /**
219  * Private properties of FlutterTextInputPlugin.
220  */
221 @interface FlutterTextInputPlugin () {
222  /**
223  * A text input context, representing a connection to the Cocoa text input system.
224  */
225  NSTextInputContext* _textInputContext;
226 
227  /**
228  * The channel used to communicate with Flutter.
229  */
231 
232  /**
233  * The FlutterViewController to manage input for.
234  */
236 
237  /**
238  * The originally requested view controller. This may be attached to a window
239  * that is a descendant of _currentViewController window in case the window
240  * can not become a key window (i.e. popup window).
241  */
243 
244  /**
245  * Used to obtain view controller on client creation
246  */
247  __weak id<FlutterTextInputPluginDelegate> _delegate;
248 
249  /**
250  * Whether the text input is shown in the view.
251  *
252  * Defaults to TRUE on startup.
253  */
254  BOOL _shown;
255 
256  /**
257  * The current state of the keyboard and pressed keys.
258  */
260 
261  /**
262  * The affinity for the current cursor position.
263  */
264  FlutterTextAffinity _textAffinity;
265 
266  /**
267  * ID of the text input client.
268  */
269  NSNumber* _clientID;
270 
271  /**
272  * Keyboard type of the client. See available options:
273  * https://api.flutter.dev/flutter/services/TextInputType-class.html
274  */
275  NSString* _inputType;
276 
277  /**
278  * An action requested by the user on the input client. See available options:
279  * https://api.flutter.dev/flutter/services/TextInputAction-class.html
280  */
281  NSString* _inputAction;
282 
283  /**
284  * Set to true if the last event fed to the input context produced a text editing command
285  * or text output. It is reset to false at the beginning of every key event, and is only
286  * used while processing this event.
287  */
289 
290  /**
291  * Whether to enable the sending of text input updates from the engine to the
292  * framework as TextEditingDeltas rather than as one TextEditingValue.
293  * For more information on the delta model, see:
294  * https://master-api.flutter.dev/flutter/services/TextInputConfiguration/enableDeltaModel.html
295  */
297 
298  /**
299  * Used to gather multiple selectors performed in one run loop turn. These
300  * will be all sent in one platform channel call so that the framework can process
301  * them in single microtask.
302  */
303  NSMutableArray* _pendingSelectors;
304 
305  /**
306  * The currently active text input model.
307  */
308  std::unique_ptr<flutter::TextInputModel> _activeModel;
309 
310  /**
311  * Transform for current the editable. Used to determine position of accent selection menu.
312  */
313  CATransform3D _editableTransform;
314 
315  /**
316  * Current position of caret in local (editable) coordinates.
317  */
318  CGRect _caretRect;
319 }
320 
321 /**
322  * Handles a Flutter system message on the text input channel.
323  */
324 - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
325 
326 /**
327  * Updates the text input model with state received from the framework via the
328  * TextInput.setEditingState message.
329  */
330 - (void)setEditingState:(NSDictionary*)state;
331 
332 /**
333  * Informs the Flutter framework of changes to the text input model's state by
334  * sending the entire new state.
335  */
336 - (void)updateEditState;
337 
338 /**
339  * Informs the Flutter framework of changes to the text input model's state by
340  * sending only the difference.
341  */
342 - (void)updateEditStateWithDelta:(const flutter::TextEditingDelta)delta;
343 
344 /**
345  * Updates the stringValue and selectedRange that stored in the NSTextView interface
346  * that this plugin inherits from.
347  *
348  * If there is a FlutterTextField uses this plugin as its field editor, this method
349  * will update the stringValue and selectedRange through the API of the FlutterTextField.
350  */
351 - (void)updateTextAndSelection;
352 
353 /**
354  * Return the string representation of the current textAffinity as it should be
355  * sent over the FlutterMethodChannel.
356  */
357 - (NSString*)textAffinityString;
358 
359 /**
360  * Allow overriding run loop mode for test.
361  */
362 @property(readwrite, nonatomic) NSString* customRunLoopMode;
363 @property(nonatomic) NSTextInputContext* textInputContext;
364 
365 @end
366 
367 #pragma mark - FlutterTextInputPlugin
368 
369 @implementation FlutterTextInputPlugin
370 
371 - (instancetype)initWithDelegate:(id<FlutterTextInputPluginDelegate>)delegate {
372  // The view needs an empty frame otherwise it is visible on dark background.
373  // https://github.com/flutter/flutter/issues/118504
374  self = [super initWithFrame:NSZeroRect];
375  self.clipsToBounds = YES;
376  if (self != nil) {
377  _delegate = delegate;
378  _channel = [FlutterMethodChannel methodChannelWithName:kTextInputChannel
379  binaryMessenger:_delegate.binaryMessenger
380  codec:[FlutterJSONMethodCodec sharedInstance]];
381  _shown = FALSE;
382  // NSTextView does not support _weak reference, so this class has to
383  // use __unsafe_unretained and manage the reference by itself.
384  //
385  // Since the dealloc removes the handler, the pointer should
386  // be valid if the handler is ever called.
387  __unsafe_unretained FlutterTextInputPlugin* unsafeSelf = self;
388  [_channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
389  [unsafeSelf handleMethodCall:call result:result];
390  }];
391  _textInputContext = [[NSTextInputContext alloc] initWithClient:unsafeSelf];
392  _previouslyPressedFlags = 0;
393 
394  // Initialize with the zero matrix which is not
395  // an affine transform.
396  _editableTransform = CATransform3D();
397  _caretRect = CGRectNull;
398  }
399  return self;
400 }
401 
402 - (BOOL)isFirstResponder {
403  if (!_currentViewController.viewLoaded) {
404  return false;
405  }
406  return [_currentViewController.view.window firstResponder] == self;
407 }
408 
409 - (void)dealloc {
410  [_channel setMethodCallHandler:nil];
411 }
412 
413 #pragma mark - Private
414 
415 - (void)resignAndRemoveFromSuperview {
416  if (self.superview != nil) {
417  [self.window makeFirstResponder:_currentViewController.flutterView];
418  [self removeFromSuperview];
419  }
420 }
421 
422 - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
423  BOOL handled = YES;
424  NSString* method = call.method;
425  if ([method isEqualToString:kSetClientMethod]) {
426  FML_DCHECK(_currentViewController == nil);
427  if (!call.arguments[0] || !call.arguments[1]) {
428  result([FlutterError
429  errorWithCode:@"error"
430  message:@"Missing arguments"
431  details:@"Missing arguments while trying to set a text input client"]);
432  return;
433  }
434  NSNumber* clientID = call.arguments[0];
435  if (clientID != nil) {
436  NSDictionary* config = call.arguments[1];
437 
438  _clientID = clientID;
439  _inputAction = config[kTextInputAction];
440  _enableDeltaModel = [config[kEnableDeltaModel] boolValue];
441  NSDictionary* inputTypeInfo = config[kTextInputType];
442  _inputType = inputTypeInfo[kTextInputTypeName];
443  _textAffinity = kFlutterTextAffinityUpstream;
444  self.automaticTextCompletionEnabled = EnableAutocomplete(config);
445  if (@available(macOS 11.0, *)) {
446  self.contentType = GetTextContentType(config);
447  }
448 
449  _activeModel = std::make_unique<flutter::TextInputModel>();
450  FlutterViewIdentifier viewId = flutter::kFlutterImplicitViewId;
451  NSObject* requestViewId = config[kViewId];
452  if ([requestViewId isKindOfClass:[NSNumber class]]) {
453  viewId = [(NSNumber*)requestViewId longLongValue];
454  }
455  _currentViewController = [_delegate viewControllerForIdentifier:viewId];
456  _originalViewController = _currentViewController;
457  while (!_currentViewController.view.window.canBecomeKeyWindow) {
458  NSWindow* parentWindow = [_currentViewController.view.window parentWindow];
459  if (parentWindow == nil) {
460  break;
461  }
462  NSViewController* controller = parentWindow.contentViewController;
463  if ([controller isKindOfClass:[FlutterViewController class]]) {
464  _currentViewController = (FlutterViewController*)controller;
465  } else {
466  break;
467  }
468  }
469  FML_DCHECK(_currentViewController != nil);
470  }
471  } else if ([method isEqualToString:kShowMethod]) {
472  FML_DCHECK(_currentViewController != nil);
473  // Ensure the plugin is in hierarchy. Only do this with accessibility disabled.
474  // When accessibility is enabled cocoa will reparent the plugin inside
475  // FlutterTextField in [FlutterTextField startEditing].
476  if (_client == nil) {
477  [_currentViewController.view addSubview:self];
478  }
479  [self.window makeFirstResponder:self];
480  _shown = TRUE;
481  } else if ([method isEqualToString:kHideMethod]) {
482  [self resignAndRemoveFromSuperview];
483  _shown = FALSE;
484  } else if ([method isEqualToString:kClearClientMethod]) {
485  FML_DCHECK(_currentViewController != nil);
486  [self resignAndRemoveFromSuperview];
487  // If there's an active mark region, commit it, end composing, and clear the IME's mark text.
488  if (_activeModel && _activeModel->composing()) {
489  _activeModel->CommitComposing();
490  _activeModel->EndComposing();
491  }
492  [_textInputContext discardMarkedText];
493 
494  _clientID = nil;
495  _inputAction = nil;
496  _enableDeltaModel = NO;
497  _inputType = nil;
498  _activeModel = nullptr;
499  _currentViewController = nil;
500  _originalViewController = nil;
501  } else if ([method isEqualToString:kSetEditingStateMethod]) {
502  FML_DCHECK(_currentViewController != nil);
503  NSDictionary* state = call.arguments;
504  [self setEditingState:state];
505  } else if ([method isEqualToString:kSetEditableSizeAndTransform]) {
506  FML_DCHECK(_currentViewController != nil);
507  NSDictionary* state = call.arguments;
508  [self setEditableTransform:state[kTransformKey]];
509  } else if ([method isEqualToString:kSetCaretRect]) {
510  FML_DCHECK(_currentViewController != nil);
511  NSDictionary* rect = call.arguments;
512  [self updateCaretRect:rect];
513  } else {
514  handled = NO;
515  }
516  result(handled ? nil : FlutterMethodNotImplemented);
517 }
518 
519 - (void)setEditableTransform:(NSArray*)matrix {
520  CATransform3D* transform = &_editableTransform;
521 
522  transform->m11 = [matrix[0] doubleValue];
523  transform->m12 = [matrix[1] doubleValue];
524  transform->m13 = [matrix[2] doubleValue];
525  transform->m14 = [matrix[3] doubleValue];
526 
527  transform->m21 = [matrix[4] doubleValue];
528  transform->m22 = [matrix[5] doubleValue];
529  transform->m23 = [matrix[6] doubleValue];
530  transform->m24 = [matrix[7] doubleValue];
531 
532  transform->m31 = [matrix[8] doubleValue];
533  transform->m32 = [matrix[9] doubleValue];
534  transform->m33 = [matrix[10] doubleValue];
535  transform->m34 = [matrix[11] doubleValue];
536 
537  transform->m41 = [matrix[12] doubleValue];
538  transform->m42 = [matrix[13] doubleValue];
539  transform->m43 = [matrix[14] doubleValue];
540  transform->m44 = [matrix[15] doubleValue];
541 }
542 
543 - (void)updateCaretRect:(NSDictionary*)dictionary {
544  NSAssert(dictionary[@"x"] != nil && dictionary[@"y"] != nil && dictionary[@"width"] != nil &&
545  dictionary[@"height"] != nil,
546  @"Expected a dictionary representing a CGRect, got %@", dictionary);
547  _caretRect = CGRectMake([dictionary[@"x"] doubleValue], [dictionary[@"y"] doubleValue],
548  [dictionary[@"width"] doubleValue], [dictionary[@"height"] doubleValue]);
549 }
550 
551 - (void)setEditingState:(NSDictionary*)state {
552  if (_activeModel == nullptr) {
553  return;
554  }
555  NSString* selectionAffinity = state[kSelectionAffinityKey];
556  if (selectionAffinity != nil) {
557  _textAffinity = [selectionAffinity isEqualToString:kTextAffinityUpstream]
558  ? kFlutterTextAffinityUpstream
559  : kFlutterTextAffinityDownstream;
560  }
561 
562  NSString* text = state[kTextKey];
563  if (![text isKindOfClass:[NSString class]]) {
564  text = @"";
565  }
566 
567  // The framework computes these ranges against its own copy of the text, which may be a different
568  // revision than the text it sends here, and the ranges are dropped entirely by |SetText| if they
569  // don't fit.
570  //
571  // Clamp them to the incoming text so a stale offset degrades the caret position rather than
572  // discarding the text update.
573  size_t text_length = text.length;
574  flutter::TextRange selected_range =
576  _activeModel->selection())
577  .ClampedTo(text_length);
578  flutter::TextRange composing_range =
580  _activeModel->composing_range())
581  .ClampedTo(text_length);
582 
583  const bool wasComposing = _activeModel->composing();
584  _activeModel->SetText([text UTF8String], selected_range, composing_range);
585  if (composing_range.collapsed() && wasComposing) {
586  [_textInputContext discardMarkedText];
587  }
588  [_client startEditing];
589 
590  [self updateTextAndSelection];
591 }
592 
593 - (NSDictionary*)editingState {
594  if (_activeModel == nullptr) {
595  return nil;
596  }
597 
598  NSString* const textAffinity = [self textAffinityString];
599 
600  int composingBase = _activeModel->composing() ? _activeModel->composing_range().base() : -1;
601  int composingExtent = _activeModel->composing() ? _activeModel->composing_range().extent() : -1;
602 
603  return @{
604  kSelectionBaseKey : @(_activeModel->selection().base()),
605  kSelectionExtentKey : @(_activeModel->selection().extent()),
606  kSelectionAffinityKey : textAffinity,
608  kComposingBaseKey : @(composingBase),
609  kComposingExtentKey : @(composingExtent),
610  kTextKey : [NSString stringWithUTF8String:_activeModel->GetText().c_str()] ?: [NSNull null],
611  };
612 }
613 
614 - (void)updateEditState {
615  if (_activeModel == nullptr) {
616  return;
617  }
618 
619  NSDictionary* state = [self editingState];
620  [_channel invokeMethod:kUpdateEditStateResponseMethod arguments:@[ _clientID, state ]];
621  [self updateTextAndSelection];
622 }
623 
624 - (void)updateEditStateWithDelta:(const flutter::TextEditingDelta)delta {
625  NSUInteger selectionBase = _activeModel->selection().base();
626  NSUInteger selectionExtent = _activeModel->selection().extent();
627  int composingBase = _activeModel->composing() ? _activeModel->composing_range().base() : -1;
628  int composingExtent = _activeModel->composing() ? _activeModel->composing_range().extent() : -1;
629 
630  NSString* const textAffinity = [self textAffinityString];
631 
632  NSDictionary* deltaToFramework = @{
633  @"oldText" : @(delta.old_text().c_str()),
634  @"deltaText" : @(delta.delta_text().c_str()),
635  @"deltaStart" : @(delta.delta_start()),
636  @"deltaEnd" : @(delta.delta_end()),
637  @"selectionBase" : @(selectionBase),
638  @"selectionExtent" : @(selectionExtent),
639  @"selectionAffinity" : textAffinity,
640  @"selectionIsDirectional" : @(false),
641  @"composingBase" : @(composingBase),
642  @"composingExtent" : @(composingExtent),
643  };
644 
645  NSDictionary* deltas = @{
646  @"deltas" : @[ deltaToFramework ],
647  };
648 
649  [_channel invokeMethod:kUpdateEditStateWithDeltasResponseMethod arguments:@[ _clientID, deltas ]];
650  [self updateTextAndSelection];
651 }
652 
653 - (void)updateTextAndSelection {
654  NSAssert(_activeModel != nullptr, @"Flutter text model must not be null.");
655  NSString* text = @(_activeModel->GetText().data());
656  int start = _activeModel->selection().base();
657  int extend = _activeModel->selection().extent();
658  NSRange selection = NSMakeRange(MIN(start, extend), ABS(start - extend));
659  // There may be a native text field client if VoiceOver is on.
660  // In this case, this plugin has to update text and selection through
661  // the client in order for VoiceOver to announce the text editing
662  // properly.
663  if (_client) {
664  [_client updateString:text withSelection:selection];
665  } else {
666  self.string = text;
667  [self setSelectedRange:selection];
668  }
669 }
670 
671 - (NSString*)textAffinityString {
672  return (_textAffinity == kFlutterTextAffinityUpstream) ? kTextAffinityUpstream
674 }
675 
676 - (BOOL)handleKeyEvent:(NSEvent*)event {
677  if (event.type == NSEventTypeKeyUp ||
678  (event.type == NSEventTypeFlagsChanged && event.modifierFlags < _previouslyPressedFlags)) {
679  return NO;
680  }
681  _previouslyPressedFlags = event.modifierFlags;
682  if (!_shown) {
683  return NO;
684  }
685 
686  _eventProducedOutput = NO;
687  BOOL res = [_textInputContext handleEvent:event];
688  // NSTextInputContext#handleEvent returns YES if the context handles the event. One of the reasons
689  // the event is handled is because it's a key equivalent. But a key equivalent might produce a
690  // text command (indicated by calling doCommandBySelector) or might not (for example, Cmd+Q). In
691  // the latter case, this command somehow has not been executed yet and Flutter must dispatch it to
692  // the next responder. See https://github.com/flutter/flutter/issues/106354 .
693  // The event is also not redispatched if there is IME composition active, because it might be
694  // handled by the IME. See https://github.com/flutter/flutter/issues/134699
695 
696  // both NSEventModifierFlagNumericPad and NSEventModifierFlagFunction are set for arrow keys.
697  bool is_navigation = event.modifierFlags & NSEventModifierFlagFunction &&
698  event.modifierFlags & NSEventModifierFlagNumericPad;
699  bool is_navigation_in_ime = is_navigation && self.hasMarkedText;
700 
701  if (event.isKeyEquivalent && !is_navigation_in_ime && !_eventProducedOutput) {
702  return NO;
703  }
704  return res;
705 }
706 
707 #pragma mark -
708 #pragma mark NSResponder
709 
710 - (void)keyDown:(NSEvent*)event {
711  [_currentViewController keyDown:event];
712 }
713 
714 - (void)keyUp:(NSEvent*)event {
715  [_currentViewController keyUp:event];
716 }
717 
718 - (BOOL)performKeyEquivalent:(NSEvent*)event {
719  if ([_currentViewController isDispatchingKeyEvent:event]) {
720  // When NSWindow is nextResponder, keyboard manager will send to it
721  // unhandled events (through [NSWindow keyDown:]). If event has both
722  // control and cmd modifiers set (i.e. cmd+control+space - emoji picker)
723  // NSWindow will then send this event as performKeyEquivalent: to first
724  // responder, which is FlutterTextInputPlugin. If that's the case, the
725  // plugin must not handle the event, otherwise the emoji picker would not
726  // work (due to first responder returning YES from performKeyEquivalent:)
727  // and there would be endless loop, because FlutterViewController will
728  // send the event back to [keyboardManager handleEvent:].
729  return NO;
730  }
731  [event markAsKeyEquivalent];
732  [_currentViewController keyDown:event];
733  return YES;
734 }
735 
736 - (void)flagsChanged:(NSEvent*)event {
737  [_currentViewController flagsChanged:event];
738 }
739 
740 - (void)mouseDown:(NSEvent*)event {
741  [_currentViewController mouseDown:event];
742 }
743 
744 - (void)mouseUp:(NSEvent*)event {
745  [_currentViewController mouseUp:event];
746 }
747 
748 - (void)mouseDragged:(NSEvent*)event {
749  [_currentViewController mouseDragged:event];
750 }
751 
752 - (void)rightMouseDown:(NSEvent*)event {
753  [_currentViewController rightMouseDown:event];
754 }
755 
756 - (void)rightMouseUp:(NSEvent*)event {
757  [_currentViewController rightMouseUp:event];
758 }
759 
760 - (void)rightMouseDragged:(NSEvent*)event {
761  [_currentViewController rightMouseDragged:event];
762 }
763 
764 - (void)otherMouseDown:(NSEvent*)event {
765  [_currentViewController otherMouseDown:event];
766 }
767 
768 - (void)otherMouseUp:(NSEvent*)event {
769  [_currentViewController otherMouseUp:event];
770 }
771 
772 - (void)otherMouseDragged:(NSEvent*)event {
773  [_currentViewController otherMouseDragged:event];
774 }
775 
776 - (void)mouseMoved:(NSEvent*)event {
777  [_currentViewController mouseMoved:event];
778 }
779 
780 - (void)scrollWheel:(NSEvent*)event {
781  [_currentViewController scrollWheel:event];
782 }
783 
784 - (NSTextInputContext*)inputContext {
785  return _textInputContext;
786 }
787 
788 #pragma mark -
789 #pragma mark NSTextInputClient
790 
791 - (void)insertTab:(id)sender {
792  // Implementing insertTab: makes AppKit send tab as command, instead of
793  // insertText with '\t'.
794 }
795 
796 - (void)insertText:(id)string replacementRange:(NSRange)range {
797  if (_activeModel == nullptr) {
798  return;
799  }
800 
801  _eventProducedOutput |= true;
802 
803  if (range.location != NSNotFound) {
804  _activeModel->SetSelection(ClampNSRange(range, _activeModel->text_range().end()));
805  } else if (_activeModel->composing() &&
806  !(_activeModel->composing_range() == _activeModel->selection())) {
807  // When confirmed by Japanese IME, string replaces range of composing_range.
808  // If selection == composing_range there is no problem.
809  // If selection ! = composing_range the range of selection is only a part of composing_range.
810  // Since _activeModel->AddText is processed first for selection, the finalization of the
811  // conversion cannot be processed correctly unless selection == composing_range or
812  // selection.collapsed(). Since _activeModel->SetSelection fails if (composing_ &&
813  // !range.collapsed()), selection == composing_range will failed. Therefore, the selection
814  // cursor should only be placed at the beginning of composing_range.
815  flutter::TextRange composing_range = _activeModel->composing_range();
816  _activeModel->SetSelection(flutter::TextRange(composing_range.start()));
817  }
818 
819  flutter::TextRange oldSelection = _activeModel->selection();
820  flutter::TextRange composingBeforeChange = _activeModel->composing_range();
821  flutter::TextRange replacedRange(-1, -1);
822 
823  std::string textBeforeChange = _activeModel->GetText().c_str();
824  // Input string may be NSString or NSAttributedString.
825  BOOL isAttributedString = [string isKindOfClass:[NSAttributedString class]];
826  const NSString* rawString = isAttributedString ? [string string] : string;
827  std::string utf8String = rawString ? [rawString UTF8String] : "";
828  _activeModel->AddText(utf8String);
829  if (_activeModel->composing()) {
830  replacedRange = composingBeforeChange;
831  _activeModel->CommitComposing();
832  _activeModel->EndComposing();
833  } else {
834  replacedRange = range.location == NSNotFound
835  ? flutter::TextRange(oldSelection.base(), oldSelection.extent())
836  : flutter::TextRange(range.location, range.location + range.length);
837  }
838  if (_enableDeltaModel) {
839  [self updateEditStateWithDelta:flutter::TextEditingDelta(textBeforeChange, replacedRange,
840  utf8String)];
841  } else {
842  [self updateEditState];
843  }
844 }
845 
846 - (void)doCommandBySelector:(SEL)selector {
847  _eventProducedOutput |= selector != NSSelectorFromString(@"noop:");
848  if ([self respondsToSelector:selector]) {
849  // Note: The more obvious [self performSelector...] doesn't give ARC enough information to
850  // handle retain semantics properly. See https://stackoverflow.com/questions/7017281/ for more
851  // information.
852  IMP imp = [self methodForSelector:selector];
853  void (*func)(id, SEL, id) = reinterpret_cast<void (*)(id, SEL, id)>(imp);
854  func(self, selector, nil);
855  }
856  if (_clientID == nil) {
857  // The macOS may still call selector even if it is no longer a first responder.
858  return;
859  }
860 
861  if (selector == @selector(insertNewline:)) {
862  // Already handled through text insertion (multiline) or action.
863  return;
864  }
865 
866  // Group multiple selectors received within a single run loop turn so that
867  // the framework can process them in single microtask.
868  NSString* name = NSStringFromSelector(selector);
869  if (_pendingSelectors == nil) {
870  _pendingSelectors = [NSMutableArray array];
871  }
872  [_pendingSelectors addObject:name];
873 
874  if (_pendingSelectors.count == 1) {
875  __weak NSMutableArray* selectors = _pendingSelectors;
876  __weak FlutterMethodChannel* channel = _channel;
877  __weak NSNumber* clientID = _clientID;
878 
879  CFStringRef runLoopMode = self.customRunLoopMode != nil
880  ? (__bridge CFStringRef)self.customRunLoopMode
881  : kCFRunLoopCommonModes;
882 
883  CFRunLoopPerformBlock(CFRunLoopGetMain(), runLoopMode, ^{
884  if (selectors.count > 0) {
885  [channel invokeMethod:kPerformSelectors arguments:@[ clientID, selectors ]];
886  [selectors removeAllObjects];
887  }
888  });
889  }
890 }
891 
892 - (void)insertNewline:(id)sender {
893  if (_activeModel == nullptr) {
894  return;
895  }
896  if (_activeModel->composing()) {
897  _activeModel->CommitComposing();
898  _activeModel->EndComposing();
899  }
900  if ([_inputType isEqualToString:kMultilineInputType] &&
901  [_inputAction isEqualToString:kInputActionNewline]) {
902  [self insertText:@"\n" replacementRange:self.selectedRange];
903  }
904  [_channel invokeMethod:kPerformAction arguments:@[ _clientID, _inputAction ]];
905 }
906 
907 - (void)setMarkedText:(id)string
908  selectedRange:(NSRange)selectedRange
909  replacementRange:(NSRange)replacementRange {
910  if (_activeModel == nullptr) {
911  return;
912  }
913  std::string textBeforeChange = _activeModel->GetText().c_str();
914  if (!_activeModel->composing()) {
915  _activeModel->BeginComposing();
916  }
917 
918  if (replacementRange.location != NSNotFound) {
919  // According to the NSTextInputClient documentation replacementRange is
920  // computed from the beginning of the marked text. That doesn't seem to be
921  // the case, because in situations where the replacementRange is actually
922  // specified (i.e. when switching between characters equivalent after long
923  // key press) the replacementRange is provided while there is no composition.
924  //
925  // The range is clamped to the text because SetComposingRange rejects ranges
926  // that don't fit.
927  _activeModel->SetComposingRange(
928  ClampNSRange(replacementRange, _activeModel->text_range().end()), 0);
929  }
930 
931  flutter::TextRange composingBeforeChange = _activeModel->composing_range();
932  flutter::TextRange selectionBeforeChange = _activeModel->selection();
933 
934  // Input string may be NSString or NSAttributedString.
935  BOOL isAttributedString = [string isKindOfClass:[NSAttributedString class]];
936  const NSString* rawString = isAttributedString ? [string string] : string;
937 
938  // Copy the UTF-16 code units directly. The C string form truncates at an
939  // embedded NUL and returns NULL when the string cannot be converted.
940  std::u16string marked_text_utf16(rawString.length, u'\0');
941  [rawString getCharacters:reinterpret_cast<unichar*>(marked_text_utf16.data())
942  range:NSMakeRange(0, marked_text_utf16.length())];
943 
944  // selectedRange is provided by the IME. Re-clamp to the text.
945  // NSNotFound (no selection), clamps to end of the marked text.
946  _activeModel->UpdateComposingText(marked_text_utf16,
947  ClampNSRange(selectedRange, marked_text_utf16.length()));
948 
949  if (_enableDeltaModel) {
950  std::string marked_text = rawString ? [rawString UTF8String] : "";
951  [self updateEditStateWithDelta:flutter::TextEditingDelta(textBeforeChange,
952  selectionBeforeChange.collapsed()
953  ? composingBeforeChange
954  : selectionBeforeChange,
955  marked_text)];
956  } else {
957  [self updateEditState];
958  }
959 }
960 
961 - (void)unmarkText {
962  if (_activeModel == nullptr) {
963  return;
964  }
965  _activeModel->CommitComposing();
966  _activeModel->EndComposing();
967  if (_enableDeltaModel) {
968  [self updateEditStateWithDelta:flutter::TextEditingDelta(_activeModel->GetText().c_str())];
969  } else {
970  [self updateEditState];
971  }
972 }
973 
974 - (NSRange)markedRange {
975  if (_activeModel == nullptr) {
976  return NSMakeRange(NSNotFound, 0);
977  }
978  return NSMakeRange(
979  _activeModel->composing_range().base(),
980  _activeModel->composing_range().extent() - _activeModel->composing_range().base());
981 }
982 
983 - (BOOL)hasMarkedText {
984  return _activeModel != nullptr && _activeModel->composing_range().length() > 0;
985 }
986 
987 - (NSAttributedString*)attributedSubstringForProposedRange:(NSRange)range
988  actualRange:(NSRangePointer)actualRange {
989  if (_activeModel == nullptr) {
990  return nil;
991  }
992  NSString* text = [NSString stringWithUTF8String:_activeModel->GetText().c_str()];
993  if (range.location >= text.length) {
994  return nil;
995  }
996  range.length = std::min(range.length, text.length - range.location);
997  if (actualRange != nil) {
998  *actualRange = range;
999  }
1000  NSString* substring = [text substringWithRange:range];
1001  return [[NSAttributedString alloc] initWithString:substring attributes:nil];
1002 }
1003 
1004 - (NSArray<NSString*>*)validAttributesForMarkedText {
1005  return @[];
1006 }
1007 
1008 // Returns the bounding CGRect of the transformed incomingRect, in screen
1009 // coordinates.
1010 - (CGRect)screenRectFromFrameworkTransform:(CGRect)incomingRect {
1011  CGPoint points[] = {
1012  incomingRect.origin,
1013  CGPointMake(incomingRect.origin.x, incomingRect.origin.y + incomingRect.size.height),
1014  CGPointMake(incomingRect.origin.x + incomingRect.size.width, incomingRect.origin.y),
1015  CGPointMake(incomingRect.origin.x + incomingRect.size.width,
1016  incomingRect.origin.y + incomingRect.size.height)};
1017 
1018  CGPoint origin = CGPointMake(CGFLOAT_MAX, CGFLOAT_MAX);
1019  CGPoint farthest = CGPointMake(-CGFLOAT_MAX, -CGFLOAT_MAX);
1020 
1021  for (int i = 0; i < 4; i++) {
1022  const CGPoint point = points[i];
1023 
1024  CGFloat x = _editableTransform.m11 * point.x + _editableTransform.m21 * point.y +
1025  _editableTransform.m41;
1026  CGFloat y = _editableTransform.m12 * point.x + _editableTransform.m22 * point.y +
1027  _editableTransform.m42;
1028 
1029  const CGFloat w = _editableTransform.m14 * point.x + _editableTransform.m24 * point.y +
1030  _editableTransform.m44;
1031 
1032  if (w == 0.0) {
1033  return CGRectZero;
1034  } else if (w != 1.0) {
1035  x /= w;
1036  y /= w;
1037  }
1038 
1039  origin.x = MIN(origin.x, x);
1040  origin.y = MIN(origin.y, y);
1041  farthest.x = MAX(farthest.x, x);
1042  farthest.y = MAX(farthest.y, y);
1043  }
1044 
1045  const NSView* fromView = _originalViewController.flutterView;
1046  const CGRect rectInWindow = [fromView
1047  convertRect:CGRectMake(origin.x, origin.y, farthest.x - origin.x, farthest.y - origin.y)
1048  toView:nil];
1049  NSWindow* window = fromView.window;
1050  return window ? [window convertRectToScreen:rectInWindow] : rectInWindow;
1051 }
1052 
1053 - (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(NSRangePointer)actualRange {
1054  // This only determines position of caret instead of any arbitrary range, but it's enough
1055  // to properly position accent selection popup
1056  return !_originalViewController.viewLoaded || CGRectEqualToRect(_caretRect, CGRectNull)
1057  ? CGRectZero
1058  : [self screenRectFromFrameworkTransform:_caretRect];
1059 }
1060 
1061 - (NSUInteger)characterIndexForPoint:(NSPoint)point {
1062  // TODO(cbracken): Implement.
1063  // Note: This function can't easily be implemented under the system-message architecture.
1064  return 0;
1065 }
1066 
1067 @end
void(^ FlutterResult)(id _Nullable result)
FLUTTER_DARWIN_EXPORT NSObject const * FlutterMethodNotImplemented
typedef NS_ENUM(NSUInteger, FlutterTextAffinity)
static NSString *const kViewId
static NSString *const kUpdateEditStateWithDeltasResponseMethod
static NSString *const kTextKey
static NSString *const kMultilineInputType
static NSString *const kAutofillHints
static NSString *const kAutofillEditingValue
static NSString *const kSecureTextEntry
static NSString *const kShowMethod
static NSString *const kPerformSelectors
static NSString *const kSetEditableSizeAndTransform
static NSTextContentType GetTextContentType(NSDictionary *configuration) API_AVAILABLE(macos(11.0))
static NSString *const kSelectionBaseKey
static char markerKey
static NSString *const kSelectionAffinityKey
static NSString *const kAssociatedAutofillFields
static flutter::TextRange ClampNSRange(NSRange range, size_t length)
static NSString *const kSetEditingStateMethod
static NSString *const kComposingExtentKey
static NSString *const kAutofillId
static NSString *const kSelectionExtentKey
static NSString *const kClearClientMethod
static NSString *const kEnableDeltaModel
static NSString *const kPerformAction
static NSString * GetAutofillHint(NSDictionary *autofill)
static NSString *const kInputActionNewline
static NSString *const kTextInputChannel
static NSString *const kTextAffinityDownstream
static NSString *const kTextInputType
static NSString *const kSetClientMethod
static NSString *const kTextAffinityUpstream
static NSString *const kTransformKey
static NSString *const kAutofillProperties
static NSString *const kUpdateEditStateResponseMethod
static BOOL EnableAutocompleteForTextInputConfiguration(NSDictionary *configuration)
static BOOL EnableAutocomplete(NSDictionary *configuration)
static NSString *const kSetCaretRect
static NSString *const kTextInputAction
static flutter::TextRange RangeFromBaseExtent(NSNumber *base, NSNumber *extent, const flutter::TextRange &range)
static NSString *const kComposingBaseKey
static NSString *const kHideMethod
static NSString *const kTextInputTypeName
static NSString *const kSelectionIsDirectionalKey
int64_t FlutterViewIdentifier
NSTextInputContext * _textInputContext
std::unique_ptr< flutter::TextInputModel > _activeModel
__weak FlutterViewController * _originalViewController
__weak FlutterViewController * _currentViewController
FlutterMethodChannel * _channel
__weak id< FlutterTextInputPluginDelegate > _delegate
TextRange ClampedTo(size_t length) const
Definition: text_range.h:95
size_t start() const
Definition: text_range.h:42
size_t base() const
Definition: text_range.h:30
bool collapsed() const
Definition: text_range.h:77
size_t extent() const
Definition: text_range.h:36
instancetype methodChannelWithName:binaryMessenger:codec:(NSString *name,[binaryMessenger] NSObject< FlutterBinaryMessenger > *messenger,[codec] NSObject< FlutterMethodCodec > *codec)