mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Smarter text/selection will/didChange events (flutter/engine#3699)
Check incoming text editing state and only fire textWillChange:, textDidChange:, selectionWillChange:, selectionDidChange: when the text or selection actually changes. On selectionWillChange: in a text field where auto-correct is enabled, iOS will attempt to auto-correct the word preceding the cursor. This change also updates the text before calling selectionWillChange: to prevent auto-correction on the preceding value of the text field.
This commit is contained in:
parent
c2e3ef609a
commit
819a40bc22
@ -162,25 +162,32 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) {
|
||||
}
|
||||
|
||||
- (void)setTextInputState:(NSDictionary*)state {
|
||||
[self.inputDelegate selectionWillChange:self];
|
||||
[self.inputDelegate textWillChange:self];
|
||||
|
||||
[self.text setString:state[@"text"]];
|
||||
NSString* newText = state[@"text"];
|
||||
BOOL textChanged = ![self.text isEqualToString:newText];
|
||||
if (textChanged) {
|
||||
[self.inputDelegate textWillChange:self];
|
||||
[self.text setString:newText];
|
||||
}
|
||||
|
||||
NSInteger selectionBase = [state[@"selectionBase"] intValue];
|
||||
NSInteger selectionExtent = [state[@"selectionExtent"] intValue];
|
||||
NSUInteger start = MIN(MAX(0, MIN(selectionBase, selectionExtent)), (NSInteger)self.text.length);
|
||||
NSUInteger end = MIN(MAX(0, MAX(selectionBase, selectionExtent)), (NSInteger)self.text.length);
|
||||
NSRange selectedRange = NSMakeRange(start, end - start);
|
||||
[self setSelectedTextRange:[FlutterTextRange rangeWithNSRange:selectedRange]
|
||||
updateEditingState:NO];
|
||||
NSRange oldSelectedRange = [(FlutterTextRange*)self.selectedTextRange range];
|
||||
if (selectedRange.location != oldSelectedRange.location ||
|
||||
selectedRange.length != oldSelectedRange.length) {
|
||||
[self.inputDelegate selectionWillChange:self];
|
||||
[self setSelectedTextRange:[FlutterTextRange rangeWithNSRange:selectedRange]
|
||||
updateEditingState:NO];
|
||||
_selectionAffinity = _kTextAffinityDownstream;
|
||||
if ([state[@"selectionAffinity"] isEqualToString:@(_kTextAffinityUpstream)])
|
||||
_selectionAffinity = _kTextAffinityUpstream;
|
||||
[self.inputDelegate selectionDidChange:self];
|
||||
}
|
||||
|
||||
_selectionAffinity = _kTextAffinityDownstream;
|
||||
if ([state[@"selectionAffinity"] isEqualToString:@(_kTextAffinityUpstream)])
|
||||
_selectionAffinity = _kTextAffinityUpstream;
|
||||
|
||||
[self.inputDelegate selectionDidChange:self];
|
||||
[self.inputDelegate textDidChange:self];
|
||||
if (textChanged)
|
||||
[self.inputDelegate textDidChange:self];
|
||||
}
|
||||
|
||||
#pragma mark - UIResponder Overrides
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user