Use common desktop TextInputModel on macOS (flutter/engine#24533)
This replaces the custom Obj-C TextInputModel implementation used on macOS with the common C++ implementation used on Linux and Windows. Note that as a side-effect, this change enables *some* direct IME input for CJK input but full input will land in follow-up patches that land: 1. Add handling for TextInput.setMarkedTextRect message 2. Add handling for TextInput.setEditableSizeAndTransform message 3. Implement firstRectForCharacterRange:actualRange: using the above. 4. Add NSTextInputContext handling.
This commit is contained in:
@@ -1138,8 +1138,6 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResiz
|
||||
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.mm
|
||||
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h
|
||||
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.mm
|
||||
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputModel.h
|
||||
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputModel.mm
|
||||
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.h
|
||||
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm
|
||||
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextureRegistrar.h
|
||||
|
||||
@@ -162,6 +162,9 @@ class TextInputModel {
|
||||
// GetText().
|
||||
int GetCursorOffset() const;
|
||||
|
||||
// Returns a range covering the entire text.
|
||||
TextRange text_range() const { return TextRange(0, text_.length()); }
|
||||
|
||||
// The current selection.
|
||||
TextRange selection() const { return selection_; }
|
||||
|
||||
@@ -188,9 +191,6 @@ class TextInputModel {
|
||||
return composing_ ? composing_range_ : text_range();
|
||||
}
|
||||
|
||||
// Returns a range covering the entire text.
|
||||
TextRange text_range() const { return TextRange(0, text_.length()); }
|
||||
|
||||
std::u16string text_;
|
||||
TextRange selection_ = TextRange(0);
|
||||
TextRange composing_range_ = TextRange(0);
|
||||
|
||||
@@ -88,8 +88,6 @@ source_set("flutter_framework_source") {
|
||||
"framework/Source/FlutterResizeSynchronizer.mm",
|
||||
"framework/Source/FlutterSurfaceManager.h",
|
||||
"framework/Source/FlutterSurfaceManager.mm",
|
||||
"framework/Source/FlutterTextInputModel.h",
|
||||
"framework/Source/FlutterTextInputModel.mm",
|
||||
"framework/Source/FlutterTextInputPlugin.h",
|
||||
"framework/Source/FlutterTextInputPlugin.mm",
|
||||
"framework/Source/FlutterTextureRegistrar.h",
|
||||
@@ -108,6 +106,7 @@ source_set("flutter_framework_source") {
|
||||
":macos_gpu_configuration",
|
||||
"//flutter/flow:flow",
|
||||
"//flutter/fml",
|
||||
"//flutter/shell/platform/common:common_cpp_input",
|
||||
"//flutter/shell/platform/common:common_cpp_switches",
|
||||
"//flutter/shell/platform/darwin/common:framework_shared",
|
||||
"//flutter/shell/platform/darwin/graphics:graphics",
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
// 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 <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
* The affinity of the current cursor position. If the cursor is at a position representing
|
||||
* a line break, the cursor may be drawn either at the end of the current line (upstream)
|
||||
* or at the beginning of the next (downstream).
|
||||
*/
|
||||
typedef NS_ENUM(NSUInteger, FlutterTextAffinity) {
|
||||
FlutterTextAffinityUpstream,
|
||||
FlutterTextAffinityDownstream
|
||||
};
|
||||
|
||||
/**
|
||||
* Data model representing text input state during an editing session.
|
||||
*/
|
||||
@interface FlutterTextInputModel : NSObject
|
||||
|
||||
/**
|
||||
* The full text being edited.
|
||||
*/
|
||||
@property(nonnull, copy) NSMutableString* text;
|
||||
/**
|
||||
* The range of text currently selected. This may have length zero to represent a single
|
||||
* cursor position.
|
||||
*/
|
||||
@property NSRange selectedRange;
|
||||
/**
|
||||
* The affinity for the current cursor position.
|
||||
*/
|
||||
@property FlutterTextAffinity textAffinity;
|
||||
/**
|
||||
* The range of text that is marked for edit, i.e. under the effects of a multi-keystroke input
|
||||
* combination.
|
||||
*/
|
||||
@property NSRange markedRange;
|
||||
|
||||
/**
|
||||
* Representation of the model's data as a state dictionary suitable for interchange with the
|
||||
* Flutter Dart layer.
|
||||
*/
|
||||
@property(nonnull) NSDictionary* state;
|
||||
|
||||
/**
|
||||
* ID of the text input client.
|
||||
*/
|
||||
@property(nonatomic, readonly, nonnull) NSNumber* clientID;
|
||||
|
||||
/**
|
||||
* Keyboard type of the client. See available options:
|
||||
* https://api.flutter.dev/flutter/services/TextInputType-class.html
|
||||
*/
|
||||
@property(nonatomic, readonly, nonnull) NSString* inputType;
|
||||
|
||||
/**
|
||||
* An action requested by the user on the input client. See available options:
|
||||
* https://api.flutter.dev/flutter/services/TextInputAction-class.html
|
||||
*/
|
||||
@property(nonatomic, readonly, nonnull) NSString* inputAction;
|
||||
|
||||
- (nullable instancetype)init NS_UNAVAILABLE;
|
||||
|
||||
/**
|
||||
* Initializes a text input model with a [clientId] and [config] arguments. [config] arguments
|
||||
* provide information on the text input connection.
|
||||
*/
|
||||
- (nullable instancetype)initWithClientID:(nonnull NSNumber*)clientID
|
||||
configuration:(nonnull NSDictionary*)config;
|
||||
@end
|
||||
@@ -1,124 +0,0 @@
|
||||
// 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/FlutterTextInputModel.h"
|
||||
|
||||
static NSString* const kTextAffinityDownstream = @"TextAffinity.downstream";
|
||||
static NSString* const kTextAffinityUpstream = @"TextAffinity.upstream";
|
||||
|
||||
static NSString* const kTextInputAction = @"inputAction";
|
||||
static NSString* const kTextInputType = @"inputType";
|
||||
static NSString* const kTextInputTypeName = @"name";
|
||||
|
||||
static NSString* const kSelectionBaseKey = @"selectionBase";
|
||||
static NSString* const kSelectionExtentKey = @"selectionExtent";
|
||||
static NSString* const kSelectionAffinityKey = @"selectionAffinity";
|
||||
static NSString* const kSelectionIsDirectionalKey = @"selectionIsDirectional";
|
||||
static NSString* const kComposingBaseKey = @"composingBase";
|
||||
static NSString* const kComposingExtentKey = @"composingExtent";
|
||||
static NSString* const kTextKey = @"text";
|
||||
|
||||
/**
|
||||
* These three static methods are necessary because Cocoa and Flutter have different idioms for
|
||||
* signaling an empty range: Flutter uses {-1, -1} while Cocoa uses {NSNotFound, 0}. Also,
|
||||
* despite the name, the "extent" fields are actually end indices, not lengths.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Updates a range given base and extent fields.
|
||||
*/
|
||||
static NSRange UpdateRangeFromBaseExtent(NSNumber* base, NSNumber* extent, NSRange range) {
|
||||
if (base == nil || extent == nil) {
|
||||
return range;
|
||||
}
|
||||
if (base.intValue == -1 && extent.intValue == -1) {
|
||||
range.location = NSNotFound;
|
||||
range.length = 0;
|
||||
} else {
|
||||
range.location = [base unsignedLongValue];
|
||||
range.length = [extent unsignedLongValue] - range.location;
|
||||
}
|
||||
return range;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the appropriate base field for a given range.
|
||||
*/
|
||||
static long GetBaseForRange(NSRange range) {
|
||||
if (range.location == NSNotFound) {
|
||||
return -1;
|
||||
}
|
||||
return range.location;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the appropriate extent field for a given range.
|
||||
*/
|
||||
static long GetExtentForRange(NSRange range) {
|
||||
if (range.location == NSNotFound) {
|
||||
return -1;
|
||||
}
|
||||
return range.location + range.length;
|
||||
}
|
||||
|
||||
@implementation FlutterTextInputModel
|
||||
|
||||
- (instancetype)initWithClientID:(NSNumber*)clientID configuration:(NSDictionary*)config {
|
||||
self = [super init];
|
||||
if (self != nil) {
|
||||
_clientID = clientID;
|
||||
_inputAction = config[kTextInputAction];
|
||||
// There's more information that can be used from this dictionary.
|
||||
// Add more as needed.
|
||||
NSDictionary* inputTypeInfo = config[kTextInputType];
|
||||
_inputType = inputTypeInfo[kTextInputTypeName];
|
||||
if (!_clientID || !_inputAction || !_inputType) {
|
||||
NSLog(@"Missing arguments for %@ init.", [self class]);
|
||||
return nil;
|
||||
}
|
||||
|
||||
_text = [[NSMutableString alloc] init];
|
||||
_selectedRange = NSMakeRange(NSNotFound, 0);
|
||||
_markedRange = NSMakeRange(NSNotFound, 0);
|
||||
_textAffinity = FlutterTextAffinityUpstream;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSDictionary*)state {
|
||||
NSString* const textAffinity = (_textAffinity == FlutterTextAffinityUpstream)
|
||||
? kTextAffinityUpstream
|
||||
: kTextAffinityDownstream;
|
||||
NSDictionary* state = @{
|
||||
kSelectionBaseKey : @(GetBaseForRange(_selectedRange)),
|
||||
kSelectionExtentKey : @(GetExtentForRange(_selectedRange)),
|
||||
kSelectionAffinityKey : textAffinity,
|
||||
kSelectionIsDirectionalKey : @NO,
|
||||
kComposingBaseKey : @(GetBaseForRange(_markedRange)),
|
||||
kComposingExtentKey : @(GetExtentForRange(_markedRange)),
|
||||
kTextKey : _text
|
||||
};
|
||||
return state;
|
||||
}
|
||||
|
||||
- (void)setState:(NSDictionary*)state {
|
||||
if (state == nil)
|
||||
return;
|
||||
|
||||
_selectedRange = UpdateRangeFromBaseExtent(state[kSelectionBaseKey], state[kSelectionExtentKey],
|
||||
_selectedRange);
|
||||
NSString* selectionAffinity = state[kSelectionAffinityKey];
|
||||
if (selectionAffinity != nil) {
|
||||
_textAffinity = [selectionAffinity isEqualToString:kTextAffinityUpstream]
|
||||
? FlutterTextAffinityUpstream
|
||||
: FlutterTextAffinityDownstream;
|
||||
}
|
||||
_markedRange =
|
||||
UpdateRangeFromBaseExtent(state[kComposingBaseKey], state[kComposingExtentKey], _markedRange);
|
||||
NSString* text = state[kTextKey];
|
||||
if (text != nil)
|
||||
[_text setString:text];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -6,8 +6,11 @@
|
||||
|
||||
#import <objc/message.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/shell/platform/common/text_input_model.h"
|
||||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h"
|
||||
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputModel.h"
|
||||
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
|
||||
|
||||
static NSString* const kTextInputChannel = @"flutter/textinput";
|
||||
@@ -22,6 +25,46 @@ static NSString* const kUpdateEditStateResponseMethod = @"TextInputClient.update
|
||||
static NSString* const kPerformAction = @"TextInputClient.performAction";
|
||||
static NSString* const kMultilineInputType = @"TextInputType.multiline";
|
||||
|
||||
static NSString* const kTextAffinityDownstream = @"TextAffinity.downstream";
|
||||
static NSString* const kTextAffinityUpstream = @"TextAffinity.upstream";
|
||||
|
||||
static NSString* const kTextInputAction = @"inputAction";
|
||||
static NSString* const kTextInputType = @"inputType";
|
||||
static NSString* const kTextInputTypeName = @"name";
|
||||
|
||||
static NSString* const kSelectionBaseKey = @"selectionBase";
|
||||
static NSString* const kSelectionExtentKey = @"selectionExtent";
|
||||
static NSString* const kSelectionAffinityKey = @"selectionAffinity";
|
||||
static NSString* const kSelectionIsDirectionalKey = @"selectionIsDirectional";
|
||||
static NSString* const kComposingBaseKey = @"composingBase";
|
||||
static NSString* const kComposingExtentKey = @"composingExtent";
|
||||
static NSString* const kTextKey = @"text";
|
||||
|
||||
/**
|
||||
* The affinity of the current cursor position. If the cursor is at a position representing
|
||||
* a line break, the cursor may be drawn either at the end of the current line (upstream)
|
||||
* or at the beginning of the next (downstream).
|
||||
*/
|
||||
typedef NS_ENUM(NSUInteger, FlutterTextAffinity) {
|
||||
FlutterTextAffinityUpstream,
|
||||
FlutterTextAffinityDownstream
|
||||
};
|
||||
|
||||
/*
|
||||
* Updates a range given base and extent fields.
|
||||
*/
|
||||
static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
NSNumber* extent,
|
||||
const flutter::TextRange& range) {
|
||||
if (base == nil || extent == nil) {
|
||||
return range;
|
||||
}
|
||||
if (base.intValue == -1 && extent.intValue == -1) {
|
||||
return flutter::TextRange(0, 0);
|
||||
}
|
||||
return flutter::TextRange([base unsignedLongValue], [extent unsignedLongValue]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Private properties of FlutterTextInputPlugin.
|
||||
*/
|
||||
@@ -32,11 +75,6 @@ static NSString* const kMultilineInputType = @"TextInputType.multiline";
|
||||
*/
|
||||
@property(nonatomic) NSTextInputContext* textInputContext;
|
||||
|
||||
/**
|
||||
* The currently active text input model.
|
||||
*/
|
||||
@property(nonatomic, nullable) FlutterTextInputModel* activeModel;
|
||||
|
||||
/**
|
||||
* The channel used to communicate with Flutter.
|
||||
*/
|
||||
@@ -47,14 +85,47 @@ static NSString* const kMultilineInputType = @"TextInputType.multiline";
|
||||
*/
|
||||
@property(nonatomic, weak) FlutterViewController* flutterViewController;
|
||||
|
||||
/**
|
||||
* The affinity for the current cursor position.
|
||||
*/
|
||||
@property FlutterTextAffinity textAffinity;
|
||||
|
||||
/**
|
||||
* ID of the text input client.
|
||||
*/
|
||||
@property(nonatomic, nonnull) NSNumber* clientID;
|
||||
|
||||
/**
|
||||
* Keyboard type of the client. See available options:
|
||||
* https://api.flutter.dev/flutter/services/TextInputType-class.html
|
||||
*/
|
||||
@property(nonatomic, nonnull) NSString* inputType;
|
||||
|
||||
/**
|
||||
* An action requested by the user on the input client. See available options:
|
||||
* https://api.flutter.dev/flutter/services/TextInputAction-class.html
|
||||
*/
|
||||
@property(nonatomic, nonnull) NSString* inputAction;
|
||||
|
||||
/**
|
||||
* Handles a Flutter system message on the text input channel.
|
||||
*/
|
||||
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
|
||||
|
||||
/**
|
||||
* Updates the text input model with state received from the framework via the
|
||||
* TextInput.setEditingState message.
|
||||
*/
|
||||
- (void)setEditingState:(NSDictionary*)state;
|
||||
|
||||
@end
|
||||
|
||||
@implementation FlutterTextInputPlugin
|
||||
@implementation FlutterTextInputPlugin {
|
||||
/**
|
||||
* The currently active text input model.
|
||||
*/
|
||||
std::unique_ptr<flutter::TextInputModel> _activeModel;
|
||||
}
|
||||
|
||||
- (instancetype)initWithViewController:(FlutterViewController*)viewController {
|
||||
self = [super init];
|
||||
@@ -87,14 +158,15 @@ static NSString* const kMultilineInputType = @"TextInputType.multiline";
|
||||
}
|
||||
NSNumber* clientID = call.arguments[0];
|
||||
if (clientID != nil) {
|
||||
self.activeModel = [[FlutterTextInputModel alloc] initWithClientID:clientID
|
||||
configuration:call.arguments[1]];
|
||||
if (!self.activeModel) {
|
||||
result([FlutterError errorWithCode:@"error"
|
||||
message:@"Failed to create an input model"
|
||||
details:@"Configuration arguments might be missing"]);
|
||||
return;
|
||||
}
|
||||
NSDictionary* config = call.arguments[1];
|
||||
|
||||
_clientID = clientID;
|
||||
_inputAction = config[kTextInputAction];
|
||||
NSDictionary* inputTypeInfo = config[kTextInputType];
|
||||
_inputType = inputTypeInfo[kTextInputTypeName];
|
||||
self.textAffinity = FlutterTextAffinityUpstream;
|
||||
|
||||
_activeModel = std::make_unique<flutter::TextInputModel>();
|
||||
}
|
||||
} else if ([method isEqualToString:kShowMethod]) {
|
||||
[self.flutterViewController addKeyResponder:self];
|
||||
@@ -103,10 +175,14 @@ static NSString* const kMultilineInputType = @"TextInputType.multiline";
|
||||
[self.flutterViewController removeKeyResponder:self];
|
||||
[_textInputContext deactivate];
|
||||
} else if ([method isEqualToString:kClearClientMethod]) {
|
||||
self.activeModel = nil;
|
||||
_clientID = nil;
|
||||
_inputAction = nil;
|
||||
_inputType = nil;
|
||||
_activeModel = nullptr;
|
||||
} else if ([method isEqualToString:kSetEditingStateMethod]) {
|
||||
NSDictionary* state = call.arguments;
|
||||
self.activeModel.state = state;
|
||||
[self setEditingState:state];
|
||||
|
||||
// Close the loop, since the framework state could have been updated by the
|
||||
// engine since it sent this update, and needs to now be made to match the
|
||||
// engine's version of the state.
|
||||
@@ -117,15 +193,51 @@ static NSString* const kMultilineInputType = @"TextInputType.multiline";
|
||||
result(handled ? nil : FlutterMethodNotImplemented);
|
||||
}
|
||||
|
||||
- (void)setEditingState:(NSDictionary*)state {
|
||||
NSString* selectionAffinity = state[kSelectionAffinityKey];
|
||||
if (selectionAffinity != nil) {
|
||||
_textAffinity = [selectionAffinity isEqualToString:kTextAffinityUpstream]
|
||||
? FlutterTextAffinityUpstream
|
||||
: FlutterTextAffinityDownstream;
|
||||
}
|
||||
|
||||
NSString* text = state[kTextKey];
|
||||
if (text != nil) {
|
||||
_activeModel->SetText([text UTF8String]);
|
||||
}
|
||||
|
||||
flutter::TextRange selected_range = RangeFromBaseExtent(
|
||||
state[kSelectionBaseKey], state[kSelectionExtentKey], _activeModel->selection());
|
||||
_activeModel->SetSelection(selected_range);
|
||||
|
||||
flutter::TextRange composing_range = RangeFromBaseExtent(
|
||||
state[kComposingBaseKey], state[kComposingExtentKey], _activeModel->composing_range());
|
||||
size_t cursor_offset = selected_range.base() - composing_range.start();
|
||||
_activeModel->SetComposingRange(composing_range, cursor_offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Informs the Flutter framework of changes to the text input model's state.
|
||||
*/
|
||||
- (void)updateEditState {
|
||||
if (self.activeModel == nil) {
|
||||
if (_activeModel == nullptr) {
|
||||
return;
|
||||
}
|
||||
[_channel invokeMethod:kUpdateEditStateResponseMethod
|
||||
arguments:@[ self.activeModel.clientID, self.activeModel.state ]];
|
||||
|
||||
NSString* const textAffinity = (self.textAffinity == FlutterTextAffinityUpstream)
|
||||
? kTextAffinityUpstream
|
||||
: kTextAffinityDownstream;
|
||||
NSDictionary* state = @{
|
||||
kSelectionBaseKey : @(_activeModel->selection().base()),
|
||||
kSelectionExtentKey : @(_activeModel->selection().extent()),
|
||||
kSelectionAffinityKey : textAffinity,
|
||||
kSelectionIsDirectionalKey : @NO,
|
||||
kComposingBaseKey : @(_activeModel->composing_range().base()),
|
||||
kComposingExtentKey : @(_activeModel->composing_range().extent()),
|
||||
kTextKey : [NSString stringWithUTF8String:_activeModel->GetText().c_str()]
|
||||
};
|
||||
|
||||
[_channel invokeMethod:kUpdateEditStateResponseMethod arguments:@[ self.clientID, state ]];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
@@ -149,35 +261,29 @@ static NSString* const kMultilineInputType = @"TextInputType.multiline";
|
||||
#pragma mark NSTextInputClient
|
||||
|
||||
- (void)insertText:(id)string replacementRange:(NSRange)range {
|
||||
if (self.activeModel != nil) {
|
||||
if (range.location == NSNotFound && range.length == 0) {
|
||||
// Use selection
|
||||
range = self.activeModel.selectedRange;
|
||||
}
|
||||
if (_activeModel == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (range.location != NSNotFound) {
|
||||
// The selected range can actually have negative numbers, since it can start
|
||||
// at the end of the range if the user selected the text going backwards.
|
||||
// NSRange uses NSUIntegers, however, so we have to cast them to know if the
|
||||
// selection is reversed or not.
|
||||
long signedLength = static_cast<long>(range.length);
|
||||
long location = range.location;
|
||||
long textLength = _activeModel->text_range().end();
|
||||
|
||||
NSUInteger length;
|
||||
NSUInteger location;
|
||||
if (signedLength >= 0) {
|
||||
location = range.location;
|
||||
length = range.length;
|
||||
} else {
|
||||
location = range.location + range.length;
|
||||
length = ABS(signedLength);
|
||||
}
|
||||
if (location > self.activeModel.text.length)
|
||||
location = self.activeModel.text.length;
|
||||
if (length > (self.activeModel.text.length - location))
|
||||
length = self.activeModel.text.length - location;
|
||||
[self.activeModel.text replaceCharactersInRange:NSMakeRange(location, length)
|
||||
withString:string];
|
||||
self.activeModel.selectedRange = NSMakeRange(location + ((NSString*)string).length, 0);
|
||||
[self updateEditState];
|
||||
size_t base = std::clamp(location, 0L, textLength);
|
||||
size_t extent = std::clamp(location + signedLength, 0L, textLength);
|
||||
_activeModel->SetSelection(flutter::TextRange(base, extent));
|
||||
}
|
||||
|
||||
_activeModel->AddText([string UTF8String]);
|
||||
if (_activeModel->composing()) {
|
||||
_activeModel->CommitComposing();
|
||||
}
|
||||
[self updateEditState];
|
||||
}
|
||||
|
||||
- (void)doCommandBySelector:(SEL)selector {
|
||||
@@ -192,54 +298,78 @@ static NSString* const kMultilineInputType = @"TextInputType.multiline";
|
||||
}
|
||||
|
||||
- (void)insertNewline:(id)sender {
|
||||
if (self.activeModel != nil) {
|
||||
if ([self.activeModel.inputType isEqualToString:kMultilineInputType]) {
|
||||
[self insertText:@"\n" replacementRange:self.activeModel.selectedRange];
|
||||
}
|
||||
[_channel invokeMethod:kPerformAction
|
||||
arguments:@[ self.activeModel.clientID, self.activeModel.inputAction ]];
|
||||
if (_activeModel == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (_activeModel->composing()) {
|
||||
_activeModel->CommitComposing();
|
||||
_activeModel->EndComposing();
|
||||
}
|
||||
if ([self.inputType isEqualToString:kMultilineInputType]) {
|
||||
[self insertText:@"\n" replacementRange:self.selectedRange];
|
||||
}
|
||||
[_channel invokeMethod:kPerformAction arguments:@[ self.clientID, self.inputAction ]];
|
||||
}
|
||||
|
||||
- (void)setMarkedText:(id)string
|
||||
selectedRange:(NSRange)selectedRange
|
||||
replacementRange:(NSRange)replacementRange {
|
||||
if (self.activeModel != nil) {
|
||||
[self.activeModel.text replaceCharactersInRange:replacementRange withString:string];
|
||||
self.activeModel.selectedRange = selectedRange;
|
||||
[self updateEditState];
|
||||
if (_activeModel == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (!_activeModel->composing()) {
|
||||
_activeModel->BeginComposing();
|
||||
}
|
||||
|
||||
// Input string may be NSString or NSAttributedString.
|
||||
BOOL isAttributedString = [string isKindOfClass:[NSAttributedString class]];
|
||||
NSString* marked_text = isAttributedString ? [string string] : string;
|
||||
_activeModel->UpdateComposingText([marked_text UTF8String]);
|
||||
|
||||
[self updateEditState];
|
||||
}
|
||||
|
||||
- (void)unmarkText {
|
||||
if (self.activeModel != nil) {
|
||||
self.activeModel.markedRange = NSMakeRange(NSNotFound, 0);
|
||||
[self updateEditState];
|
||||
if (_activeModel != nullptr) {
|
||||
return;
|
||||
}
|
||||
_activeModel->CommitComposing();
|
||||
_activeModel->EndComposing();
|
||||
[self updateEditState];
|
||||
}
|
||||
|
||||
- (NSRange)selectedRange {
|
||||
return (self.activeModel == nil) ? NSMakeRange(NSNotFound, 0) : self.activeModel.selectedRange;
|
||||
if (_activeModel == nullptr) {
|
||||
return NSMakeRange(NSNotFound, 0);
|
||||
}
|
||||
return NSMakeRange(_activeModel->selection().base(),
|
||||
_activeModel->selection().extent() - _activeModel->selection().base());
|
||||
}
|
||||
|
||||
- (NSRange)markedRange {
|
||||
return (self.activeModel == nil) ? NSMakeRange(NSNotFound, 0) : self.activeModel.markedRange;
|
||||
if (_activeModel == nullptr) {
|
||||
return NSMakeRange(NSNotFound, 0);
|
||||
}
|
||||
return NSMakeRange(
|
||||
_activeModel->composing_range().base(),
|
||||
_activeModel->composing_range().extent() - _activeModel->composing_range().base());
|
||||
}
|
||||
|
||||
- (BOOL)hasMarkedText {
|
||||
return (self.activeModel == nil) ? NO : self.activeModel.markedRange.location != NSNotFound;
|
||||
return _activeModel != nullptr && _activeModel->composing_range().length() > 0;
|
||||
}
|
||||
|
||||
- (NSAttributedString*)attributedSubstringForProposedRange:(NSRange)range
|
||||
actualRange:(NSRangePointer)actualRange {
|
||||
if (self.activeModel) {
|
||||
if (actualRange != nil)
|
||||
*actualRange = range;
|
||||
NSString* substring = [self.activeModel.text substringWithRange:range];
|
||||
return [[NSAttributedString alloc] initWithString:substring attributes:nil];
|
||||
} else {
|
||||
if (_activeModel == nullptr) {
|
||||
return nil;
|
||||
}
|
||||
if (actualRange != nil) {
|
||||
*actualRange = range;
|
||||
}
|
||||
NSString* text = [NSString stringWithUTF8String:_activeModel->GetText().c_str()];
|
||||
NSString* substring = [text substringWithRange:range];
|
||||
return [[NSAttributedString alloc] initWithString:substring attributes:nil];
|
||||
}
|
||||
|
||||
- (NSArray<NSString*>*)validAttributesForMarkedText {
|
||||
|
||||
Reference in New Issue
Block a user