| Flutter macOS Embedder |

Instance Methods | List of all members
FlutterTextField Class Reference

`#import <FlutterTextInputSemanticsObject.h>`

Inheritance diagram for FlutterTextField:

## Instance Methods
(instancetype) | - initWithPlatformNode:fieldEditor:
(void) | - updateString:withSelection:
(void) | - startEditing

## Detailed Description

An NSTextField implementation that represents the NativeViewAccessible for the FlutterTextPlatformNode

The NSAccessibility protocol does not provide full support for text editing. This appkit text field is used to get around this problem. The FlutterTextPlatformNode creates a hidden FlutterTextField, since VoiceOver only provides text editing announcements for NSTextField subclasses.

All of the text editing events in this native text field are redirected to the FlutterTextInputPlugin.

Definition at line 81 of file FlutterTextInputSemanticsObject.h.

## Method Documentation

## ◆ initWithPlatformNode:fieldEditor:

- (instancetype) initWithPlatformNode: |  | (flutter::FlutterTextPlatformNode*) | node
fieldEditor: |  | (FlutterTextInputPlugin*) | plugin

Initializes a FlutterTextField that uses the FlutterTextInputPlugin as its field editor. The text field redirects all of the text editing events to the FlutterTextInputPlugin.

Definition at line 69 of file FlutterTextInputSemanticsObject.mm.

69 :(flutter::FlutterTextPlatformNode*)node
70 fieldEditor:(FlutterTextInputPlugin*)plugin {
71 self = [super initWithFrame:NSZeroRect];
72 if (self) {
73 _node = node;
74 _plugin = plugin;
75 [self setCell:[[FlutterTextFieldCell alloc] initWithTextField:self fieldEditor:plugin]];
76 }
77 return self;
78 }

References _plugin.

## ◆ startEditing

- (void) startEditing |  |  |

Makes the field editor (plugin) current editor for this TextField, meaning that the text field will start getting editing events.

Definition at line 112 of file FlutterTextInputSemanticsObject.mm.

112 {
113 if (!_plugin) {
114 return;
115 }
116 if (self.currentEditor == _plugin) {
117 return;
118 }
119 if (!_node) {
120 return;
121 }
122 // Selecting text seems to be the only way to make the field editor
123 // current editor.
124 [self selectText:self];
125 NSAssert(self.currentEditor == _plugin, @"Failed to set current editor");
126
127 _plugin.client = self;
128
129 // Restore previous selection.
130 NSString* textValue = @(_node->GetStringAttribute(ax::mojom::StringAttribute::kValue).data());
131 int start = _node->GetIntAttribute(ax::mojom::IntAttribute::kTextSelStart);
132 int end = _node->GetIntAttribute(ax::mojom::IntAttribute::kTextSelEnd);
133 NSAssert((start >= 0 && end >= 0) || (start == -1 && end == -1), @"selection is invalid");
134 NSRange selection;
135 if (start >= 0 && end >= 0) {
136 selection = NSMakeRange(MIN(start, end), ABS(end - start));
137 } else {
138 // The native behavior is to place the cursor at the end of the string if
139 // there is no selection.
140 selection = NSMakeRange([self stringValue].length, 0);
141 }
142 [self updateString:textValue withSelection:selection];
143 }

References _plugin, FlutterTextInputPlugin::client, and updateString:withSelection:.

Referenced by flutter::testing::TEST().

## ◆ updateString:withSelection:

- (void) updateString: |  | (NSString*) | string
withSelection: |  | (NSRange) | selection

Updates the string value and the selection of this text field.

Calling this method is necessary for macOS to get notified about string and selection changes.

Definition at line 80 of file FlutterTextInputSemanticsObject.mm.

80 :(NSString*)string withSelection:(NSRange)selection {
81 NSAssert(_plugin.client == self,
82 @"Can't update FlutterTextField when it is not the first responder");
83 if (![[self stringValue] isEqualToString:string]) {
84 [self setStringValue:string];
85 }
86 if (!NSEqualRanges(_plugin.selectedRange, selection)) {
87 [_plugin setSelectedRange:selection];
88 }
89 }

References _plugin, and FlutterTextInputPlugin::client.

Referenced by startEditing.

The documentation for this class was generated from the following files:

- shell/platform/darwin/macos/framework/Source/FlutterTextInputSemanticsObject.h
- shell/platform/darwin/macos/framework/Source/FlutterTextInputSemanticsObject.mm

flutter::FlutterTextPlatformNode
The ax platform node for a text field.
Definition: FlutterTextInputSemanticsObject.h:22
FlutterTextInputPlugin::client
FlutterTextField * client
Definition: FlutterTextInputPlugin.h:34
FlutterTextInputPlugin
Definition: FlutterTextInputPlugin.h:27
_plugin
FlutterTextInputPlugin * _plugin
Definition: FlutterTextInputSemanticsObject.mm:62
FlutterTextFieldCell
Definition: FlutterTextInputSemanticsObject.mm:23