|
67 | 67 |
|
68 | 68 | #pragma mark - Enums |
69 | 69 | /** |
70 | | - * The affinity of the current cursor position. If the cursor is at a position representing |
71 | | - * a line break, the cursor may be drawn either at the end of the current line (upstream) |
72 | | - * or at the beginning of the next (downstream). |
| 70 | + * The affinity of the current cursor position. If the cursor is at a position |
| 71 | + * representing a soft line break, the cursor may be drawn either at the end of |
| 72 | + * the current line (upstream) or at the beginning of the next (downstream). |
73 | 73 | */ |
74 | 74 | typedef NS_ENUM(NSUInteger, FlutterTextAffinity) { |
75 | 75 | kFlutterTextAffinityUpstream, |
@@ -523,20 +523,19 @@ - (NSDictionary*)editingState { |
523 | 523 | return nil; |
524 | 524 | } |
525 | 525 |
|
526 | | - NSString* const textAffinity = [self textAffinityString]; |
527 | | - |
528 | | - int composingBase = _activeModel->composing() ? _activeModel->composing_range().base() : -1; |
529 | | - int composingExtent = _activeModel->composing() ? _activeModel->composing_range().extent() : -1; |
530 | | - |
531 | | - return @{ |
| 526 | + NSDictionary* const dictionary = @{ |
532 | 527 | kSelectionBaseKey : @(_activeModel->selection().base()), |
533 | 528 | kSelectionExtentKey : @(_activeModel->selection().extent()), |
534 | | - kSelectionAffinityKey : textAffinity, |
535 | | - kSelectionIsDirectionalKey : @NO, |
536 | | - kComposingBaseKey : @(composingBase), |
537 | | - kComposingExtentKey : @(composingExtent), |
| 529 | + kSelectionAffinityKey : [self textAffinityString], |
538 | 530 | kTextKey : [NSString stringWithUTF8String:_activeModel->GetText().c_str()] ?: [NSNull null], |
539 | 531 | }; |
| 532 | + if (!_activeModel->composing()) { |
| 533 | + return dictionary; |
| 534 | + } |
| 535 | + NSMutableDictionary* mutableDictionary = [dictionary mutableCopy]; |
| 536 | + mutableDictionary[kComposingBaseKey] = @(_activeModel->composing_range().base()); |
| 537 | + mutableDictionary[kComposingExtentKey] = @(_activeModel->composing_range().extent()); |
| 538 | + return mutableDictionary; |
540 | 539 | } |
541 | 540 |
|
542 | 541 | - (void)updateEditState { |
@@ -852,19 +851,13 @@ - (void)setMarkedText:(id)string |
852 | 851 |
|
853 | 852 | // Input string may be NSString or NSAttributedString. |
854 | 853 | BOOL isAttributedString = [string isKindOfClass:[NSAttributedString class]]; |
855 | | - std::string marked_text = isAttributedString ? [[string string] UTF8String] : [string UTF8String]; |
856 | | - _activeModel->UpdateComposingText(marked_text); |
857 | | - |
858 | | - // Update the selection within the marked text. |
859 | | - long signedLength = static_cast<long>(selectedRange.length); |
860 | | - long location = selectedRange.location + _activeModel->composing_range().base(); |
861 | | - long textLength = _activeModel->text_range().end(); |
862 | | - |
863 | | - size_t base = std::clamp(location, 0L, textLength); |
864 | | - size_t extent = std::clamp(location + signedLength, 0L, textLength); |
865 | | - _activeModel->SetSelection(flutter::TextRange(base, extent)); |
| 854 | + const NSString* rawString = isAttributedString ? [string string] : string; |
| 855 | + _activeModel->UpdateComposingText( |
| 856 | + (const char16_t*)[rawString cStringUsingEncoding:NSUTF16StringEncoding], |
| 857 | + flutter::TextRange(selectedRange.location, selectedRange.location + selectedRange.length)); |
866 | 858 |
|
867 | 859 | if (_enableDeltaModel) { |
| 860 | + std::string marked_text = [rawString UTF8String]; |
868 | 861 | [self updateEditStateWithDelta:flutter::TextEditingDelta(textBeforeChange, |
869 | 862 | selectionBeforeChange.collapsed() |
870 | 863 | ? composingBeforeChange |
|
0 commit comments