TextEditingDelta Mac (flutter/engine#29036)
Enables the TextEditingDelta API for Mac, and abstracts iOS's TextEditingDelta class for multi-platfor reuse.
This commit is contained in:
committed by
GitHub
parent
99b17af458
commit
3d60f36a21
@@ -1027,6 +1027,9 @@ FILE: ../../../flutter/shell/platform/common/public/flutter_plugin_registrar.h
|
||||
FILE: ../../../flutter/shell/platform/common/public/flutter_texture_registrar.h
|
||||
FILE: ../../../flutter/shell/platform/common/test_accessibility_bridge.cc
|
||||
FILE: ../../../flutter/shell/platform/common/test_accessibility_bridge.h
|
||||
FILE: ../../../flutter/shell/platform/common/text_editing_delta.cc
|
||||
FILE: ../../../flutter/shell/platform/common/text_editing_delta.h
|
||||
FILE: ../../../flutter/shell/platform/common/text_editing_delta_unittests.cc
|
||||
FILE: ../../../flutter/shell/platform/common/text_input_model.cc
|
||||
FILE: ../../../flutter/shell/platform/common/text_input_model.h
|
||||
FILE: ../../../flutter/shell/platform/common/text_input_model_unittests.cc
|
||||
@@ -1118,9 +1121,6 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterRestora
|
||||
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterRestorationPluginTest.mm
|
||||
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.h
|
||||
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.mm
|
||||
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.h
|
||||
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.mm
|
||||
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDeltaTest.mm
|
||||
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h
|
||||
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h
|
||||
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm
|
||||
|
||||
@@ -39,11 +39,15 @@ copy("publish_headers") {
|
||||
|
||||
source_set("common_cpp_input") {
|
||||
public = [
|
||||
"text_editing_delta.h",
|
||||
"text_input_model.h",
|
||||
"text_range.h",
|
||||
]
|
||||
|
||||
sources = [ "text_input_model.cc" ]
|
||||
sources = [
|
||||
"text_editing_delta.cc",
|
||||
"text_input_model.cc",
|
||||
]
|
||||
|
||||
configs += [ ":desktop_library_implementation" ]
|
||||
|
||||
@@ -167,6 +171,7 @@ if (enable_unittests) {
|
||||
"geometry_unittests.cc",
|
||||
"json_message_codec_unittests.cc",
|
||||
"json_method_codec_unittests.cc",
|
||||
"text_editing_delta_unittests.cc",
|
||||
"text_input_model_unittests.cc",
|
||||
"text_range_unittests.cc",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// 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.
|
||||
|
||||
#include "flutter/shell/platform/common/text_editing_delta.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
TextEditingDelta::TextEditingDelta(const std::u16string& text_before_change,
|
||||
TextRange range,
|
||||
const std::u16string& text)
|
||||
: old_text_(text_before_change),
|
||||
delta_text_(text),
|
||||
delta_start_(range.start()),
|
||||
delta_end_(range.start() + range.length()) {}
|
||||
|
||||
TextEditingDelta::TextEditingDelta(const std::string& text_before_change,
|
||||
TextRange range,
|
||||
const std::string& text)
|
||||
: old_text_(Utf8ToUtf16(text_before_change)),
|
||||
delta_text_(Utf8ToUtf16(text)),
|
||||
delta_start_(range.start()),
|
||||
delta_end_(range.start() + range.length()) {}
|
||||
|
||||
TextEditingDelta::TextEditingDelta(const std::u16string& text)
|
||||
: old_text_(text), delta_text_(u""), delta_start_(-1), delta_end_(-1) {}
|
||||
|
||||
TextEditingDelta::TextEditingDelta(const std::string& text)
|
||||
: old_text_(Utf8ToUtf16(text)),
|
||||
delta_text_(u""),
|
||||
delta_start_(-1),
|
||||
delta_end_(-1) {}
|
||||
|
||||
} // namespace flutter
|
||||
@@ -0,0 +1,92 @@
|
||||
// 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.
|
||||
|
||||
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_TEXT_EDITING_DELTA_H_
|
||||
#define FLUTTER_SHELL_PLATFORM_COMMON_TEXT_EDITING_DELTA_H_
|
||||
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
|
||||
|
||||
#include <codecvt>
|
||||
#include <string>
|
||||
|
||||
#include "flutter/shell/platform/common/text_range.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
/// A change in the state of an input field.
|
||||
struct TextEditingDelta {
|
||||
TextEditingDelta(const std::u16string& text_before_change,
|
||||
TextRange range,
|
||||
const std::u16string& text);
|
||||
|
||||
TextEditingDelta(const std::string& text_before_change,
|
||||
TextRange range,
|
||||
const std::string& text);
|
||||
|
||||
TextEditingDelta(const std::u16string& text);
|
||||
|
||||
TextEditingDelta(const std::string& text);
|
||||
|
||||
virtual ~TextEditingDelta() = default;
|
||||
|
||||
/// Get the old_text_ value.
|
||||
///
|
||||
/// All strings are stored as UTF16 but converted to UTF8 when accessed.
|
||||
std::string old_text() const { return Utf16ToUtf8(old_text_); }
|
||||
|
||||
/// Get the delta_text value.
|
||||
///
|
||||
/// All strings are stored as UTF16 but converted to UTF8 when accessed.
|
||||
std::string delta_text() const { return Utf16ToUtf8(delta_text_); }
|
||||
|
||||
/// Get the delta_start_ value.
|
||||
int delta_start() const { return delta_start_; }
|
||||
|
||||
/// Get the delta_end_ value.
|
||||
int delta_end() const { return delta_end_; }
|
||||
|
||||
bool operator==(const TextEditingDelta& rhs) const {
|
||||
return old_text_ == rhs.old_text_ && delta_text_ == rhs.delta_text_ &&
|
||||
delta_start_ == rhs.delta_start_ && delta_end_ == rhs.delta_end_;
|
||||
}
|
||||
|
||||
bool operator!=(const TextEditingDelta& rhs) const { return !(*this == rhs); }
|
||||
|
||||
TextEditingDelta(const TextEditingDelta& other) = default;
|
||||
|
||||
TextEditingDelta& operator=(const TextEditingDelta& other) = default;
|
||||
|
||||
private:
|
||||
std::u16string old_text_;
|
||||
std::u16string delta_text_;
|
||||
int delta_start_;
|
||||
int delta_end_;
|
||||
|
||||
void set_old_text(const std::u16string& old_text) { old_text_ = old_text; }
|
||||
|
||||
void set_delta_text(const std::u16string& delta_text) {
|
||||
delta_text_ = delta_text;
|
||||
}
|
||||
|
||||
void set_delta_start(int delta_start) { delta_start_ = delta_start; }
|
||||
|
||||
void set_delta_end(int delta_end) { delta_end_ = delta_end; }
|
||||
|
||||
// Given a UTF16-encoded string, returns the string encoded in UTF8.
|
||||
static std::string Utf16ToUtf8(const std::u16string& string) {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
|
||||
utf8_converter;
|
||||
return utf8_converter.to_bytes(string);
|
||||
}
|
||||
|
||||
// Given a UTF8-encoded string, returns the string encoded in UTF16.
|
||||
static std::u16string Utf8ToUtf16(const std::string& string) {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
|
||||
utf16_converter;
|
||||
return utf16_converter.from_bytes(string);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
#endif // FLUTTER_SHELL_PLATFORM_COMMON_TEXT_EDITING_DELTA_H_
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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.
|
||||
|
||||
#include "flutter/shell/platform/common/text_editing_delta.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace flutter {
|
||||
|
||||
TEST(TextEditingDeltaTest, TestTextEditingDeltaConstructor) {
|
||||
// Here we are simulating inserting an "o" at the end of "hell".
|
||||
std::string old_text = "hell";
|
||||
std::string replacement_text = "hello";
|
||||
TextRange range(0, 4);
|
||||
|
||||
TextEditingDelta delta = TextEditingDelta(old_text, range, replacement_text);
|
||||
|
||||
EXPECT_EQ(delta.old_text(), old_text);
|
||||
EXPECT_EQ(delta.delta_text(), "hello");
|
||||
EXPECT_EQ(delta.delta_start(), 0);
|
||||
EXPECT_EQ(delta.delta_end(), 4);
|
||||
}
|
||||
|
||||
TEST(TextEditingDeltaTest, TestTextEditingDeltaNonTextConstructor) {
|
||||
// Here we are simulating inserting an "o" at the end of "hell".
|
||||
std::string old_text = "hello";
|
||||
|
||||
TextEditingDelta delta = TextEditingDelta(old_text);
|
||||
|
||||
EXPECT_EQ(delta.old_text(), old_text);
|
||||
EXPECT_EQ(delta.delta_text(), "");
|
||||
EXPECT_EQ(delta.delta_start(), -1);
|
||||
EXPECT_EQ(delta.delta_end(), -1);
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
@@ -79,8 +79,6 @@ source_set("flutter_framework_source") {
|
||||
"framework/Source/FlutterRestorationPlugin.mm",
|
||||
"framework/Source/FlutterSemanticsScrollView.h",
|
||||
"framework/Source/FlutterSemanticsScrollView.mm",
|
||||
"framework/Source/FlutterTextEditingDelta.h",
|
||||
"framework/Source/FlutterTextEditingDelta.mm",
|
||||
"framework/Source/FlutterTextInputDelegate.h",
|
||||
"framework/Source/FlutterTextInputPlugin.h",
|
||||
"framework/Source/FlutterTextInputPlugin.mm",
|
||||
@@ -161,6 +159,7 @@ source_set("flutter_framework_source") {
|
||||
"//flutter/runtime",
|
||||
"//flutter/runtime:libdart",
|
||||
"//flutter/shell/common",
|
||||
"//flutter/shell/platform/common:common_cpp_input",
|
||||
"//flutter/shell/platform/darwin/common",
|
||||
"//flutter/shell/platform/darwin/common:framework_shared",
|
||||
"//flutter/shell/platform/embedder:embedder_as_internal_library",
|
||||
@@ -254,7 +253,6 @@ shared_library("ios_test_flutter") {
|
||||
"framework/Source/FlutterKeyboardManagerTest.mm",
|
||||
"framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm",
|
||||
"framework/Source/FlutterRestorationPluginTest.mm",
|
||||
"framework/Source/FlutterTextEditingDeltaTest.mm",
|
||||
"framework/Source/FlutterTextInputPluginTest.mm",
|
||||
"framework/Source/FlutterViewControllerTest.mm",
|
||||
"framework/Source/SemanticsObjectTest.mm",
|
||||
|
||||
@@ -1,24 +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.
|
||||
|
||||
#ifndef SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERTEXTEDITINGDELTA_H_
|
||||
#define SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERTEXTEDITINGDELTA_H_
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface FlutterTextEditingDelta : NSObject
|
||||
|
||||
@property(nonatomic, readonly) NSString* oldText;
|
||||
@property(nonatomic, readonly) NSString* deltaText;
|
||||
@property(nonatomic, readonly) NSInteger deltaStart;
|
||||
@property(nonatomic, readonly) NSInteger deltaEnd;
|
||||
|
||||
+ (instancetype)textEditingDelta:(NSString*)textBeforeChange
|
||||
replacedRange:(NSRange)range
|
||||
updatedText:(NSString*)text;
|
||||
|
||||
+ (instancetype)deltaWithNonText:(NSString*)text;
|
||||
|
||||
@end
|
||||
#endif // SHELL_PLATFORM_IOS_FRAMEWORK_SOURCE_FLUTTERTEXTEDITINGDELTA_H_
|
||||
@@ -1,61 +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/ios/framework/Source/FlutterTextEditingDelta.h"
|
||||
|
||||
@implementation FlutterTextEditingDelta
|
||||
|
||||
- (instancetype)initTextEditingDelta:(NSString*)textBeforeChange
|
||||
replacedRange:(NSRange)range
|
||||
updatedText:(NSString*)text {
|
||||
self = [super init];
|
||||
|
||||
if (self) {
|
||||
NSInteger start = range.location;
|
||||
NSInteger end = range.location + range.length;
|
||||
[self setDeltas:textBeforeChange newText:text deltaStart:start deltaEnd:end];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithNonText:(NSString*)text {
|
||||
self = [super init];
|
||||
|
||||
if (self) {
|
||||
[self setDeltas:text newText:@"" deltaStart:-1 deltaEnd:-1];
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setDeltas:(NSString*)oldText
|
||||
newText:(NSString*)newTxt
|
||||
deltaStart:(NSInteger)newStart
|
||||
deltaEnd:(NSInteger)newEnd {
|
||||
_oldText = [oldText retain];
|
||||
_deltaText = [newTxt retain];
|
||||
_deltaStart = newStart;
|
||||
_deltaEnd = newEnd;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[_oldText release];
|
||||
[_deltaText release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
+ (instancetype)textEditingDelta:(NSString*)textBeforeChange
|
||||
replacedRange:(NSRange)range
|
||||
updatedText:(NSString*)text {
|
||||
return [[[FlutterTextEditingDelta alloc] initTextEditingDelta:textBeforeChange
|
||||
replacedRange:range
|
||||
updatedText:text] autorelease];
|
||||
}
|
||||
|
||||
+ (instancetype)deltaWithNonText:(NSString*)text {
|
||||
return [[[FlutterTextEditingDelta alloc] initWithNonText:text] autorelease];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -1,46 +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/ios/framework/Source/FlutterTextEditingDelta.h"
|
||||
|
||||
#import <XCTest/XCTest.h>
|
||||
|
||||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
|
||||
|
||||
FLUTTER_ASSERT_ARC
|
||||
|
||||
@interface FlutterTextEditingDeltaTest : XCTestCase
|
||||
@end
|
||||
|
||||
@implementation FlutterTextEditingDeltaTest
|
||||
|
||||
- (void)testTextEditingDeltaConstructor {
|
||||
// Here we are simulating inserting an "o" at the end of "hell".
|
||||
NSString* oldText = @"hell";
|
||||
NSString* replacementText = @"hello";
|
||||
NSRange range = NSMakeRange(0, 4);
|
||||
|
||||
FlutterTextEditingDelta* delta = [FlutterTextEditingDelta textEditingDelta:oldText
|
||||
replacedRange:range
|
||||
updatedText:replacementText];
|
||||
|
||||
XCTAssertEqual(delta.oldText, oldText);
|
||||
XCTAssertEqualObjects(delta.deltaText, @"hello");
|
||||
XCTAssertEqual(delta.deltaStart, 0);
|
||||
XCTAssertEqual(delta.deltaEnd, 4);
|
||||
}
|
||||
|
||||
- (void)testTextEditingDeltaNonTextConstructor {
|
||||
// Here we are simulating inserting an "o" at the end of "hell".
|
||||
NSString* oldText = @"hello";
|
||||
|
||||
FlutterTextEditingDelta* delta = [FlutterTextEditingDelta deltaWithNonText:oldText];
|
||||
|
||||
XCTAssertEqual(delta.oldText, oldText);
|
||||
XCTAssertEqualObjects(delta.deltaText, @"");
|
||||
XCTAssertEqual(delta.deltaStart, -1);
|
||||
XCTAssertEqual(delta.deltaEnd, -1);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -7,10 +7,10 @@
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
#include "flutter/shell/platform/common/text_editing_delta.h"
|
||||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterIndirectScribbleDelegate.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterKeySecondaryResponder.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextEditingDelta.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h"
|
||||
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewResponder.h"
|
||||
|
||||
|
||||
@@ -1111,7 +1111,7 @@ static BOOL IsScribbleAvailable() {
|
||||
[self setSelectedTextRangeLocal:selectedTextRange];
|
||||
|
||||
if (_enableDeltaModel) {
|
||||
[self updateEditingStateWithDelta:[FlutterTextEditingDelta deltaWithNonText:self.text]];
|
||||
[self updateEditingStateWithDelta:flutter::TextEditingDelta([self.text UTF8String])];
|
||||
} else {
|
||||
[self updateEditingState];
|
||||
}
|
||||
@@ -1180,11 +1180,13 @@ static BOOL IsScribbleAvailable() {
|
||||
NSRange replaceRange = ((FlutterTextRange*)range).range;
|
||||
[self replaceRangeLocal:replaceRange withText:text];
|
||||
if (_enableDeltaModel) {
|
||||
[self updateEditingStateWithDelta:[FlutterTextEditingDelta
|
||||
textEditingDelta:textBeforeChange
|
||||
replacedRange:[self clampSelection:replaceRange
|
||||
forText:textBeforeChange]
|
||||
updatedText:text]];
|
||||
NSRange nextReplaceRange = [self clampSelection:replaceRange forText:textBeforeChange];
|
||||
[self updateEditingStateWithDelta:flutter::TextEditingDelta(
|
||||
[textBeforeChange UTF8String],
|
||||
flutter::TextRange(
|
||||
nextReplaceRange.location,
|
||||
nextReplaceRange.location + nextReplaceRange.length),
|
||||
[text UTF8String])];
|
||||
} else {
|
||||
[self updateEditingState];
|
||||
}
|
||||
@@ -1280,11 +1282,13 @@ static BOOL IsScribbleAvailable() {
|
||||
rangeWithNSRange:[self clampSelection:selectedRange
|
||||
forText:self.text]]];
|
||||
if (_enableDeltaModel) {
|
||||
[self updateEditingStateWithDelta:[FlutterTextEditingDelta
|
||||
textEditingDelta:textBeforeChange
|
||||
replacedRange:[self clampSelection:actualReplacedRange
|
||||
forText:textBeforeChange]
|
||||
updatedText:markedText]];
|
||||
NSRange nextReplaceRange = [self clampSelection:actualReplacedRange forText:textBeforeChange];
|
||||
[self updateEditingStateWithDelta:flutter::TextEditingDelta(
|
||||
[textBeforeChange UTF8String],
|
||||
flutter::TextRange(
|
||||
nextReplaceRange.location,
|
||||
nextReplaceRange.location + nextReplaceRange.length),
|
||||
[markedText UTF8String])];
|
||||
} else {
|
||||
[self updateEditingState];
|
||||
}
|
||||
@@ -1296,7 +1300,7 @@ static BOOL IsScribbleAvailable() {
|
||||
}
|
||||
self.markedTextRange = nil;
|
||||
if (_enableDeltaModel) {
|
||||
[self updateEditingStateWithDelta:[FlutterTextEditingDelta deltaWithNonText:self.text]];
|
||||
[self updateEditingStateWithDelta:flutter::TextEditingDelta([self.text UTF8String])];
|
||||
} else {
|
||||
[self updateEditingState];
|
||||
}
|
||||
@@ -1753,7 +1757,7 @@ static BOOL IsScribbleAvailable() {
|
||||
}
|
||||
}
|
||||
|
||||
- (void)updateEditingStateWithDelta:(FlutterTextEditingDelta*)delta {
|
||||
- (void)updateEditingStateWithDelta:(flutter::TextEditingDelta)delta {
|
||||
NSUInteger selectionBase = ((FlutterTextPosition*)_selectedTextRange.start).index;
|
||||
NSUInteger selectionExtent = ((FlutterTextPosition*)_selectedTextRange.end).index;
|
||||
|
||||
@@ -1766,10 +1770,10 @@ static BOOL IsScribbleAvailable() {
|
||||
}
|
||||
|
||||
NSDictionary* deltaToFramework = @{
|
||||
@"oldText" : delta.oldText,
|
||||
@"deltaText" : delta.deltaText,
|
||||
@"deltaStart" : @(delta.deltaStart),
|
||||
@"deltaEnd" : @(delta.deltaEnd),
|
||||
@"oldText" : @(delta.old_text().c_str()),
|
||||
@"deltaText" : @(delta.delta_text().c_str()),
|
||||
@"deltaStart" : @(delta.delta_start()),
|
||||
@"deltaEnd" : @(delta.delta_end()),
|
||||
@"selectionBase" : @(selectionBase),
|
||||
@"selectionExtent" : @(selectionExtent),
|
||||
@"selectionAffinity" : @(_selectionAffinity),
|
||||
|
||||
@@ -4,11 +4,14 @@
|
||||
|
||||
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.h"
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <objc/message.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
|
||||
#include "flutter/fml/platform/darwin/string_range_sanitization.h"
|
||||
#include "flutter/shell/platform/common/text_editing_delta.h"
|
||||
#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/Headers/FlutterAppDelegate.h"
|
||||
@@ -26,6 +29,8 @@ static NSString* const kSetEditingStateMethod = @"TextInput.setEditingState";
|
||||
static NSString* const kSetEditableSizeAndTransform = @"TextInput.setEditableSizeAndTransform";
|
||||
static NSString* const kSetCaretRect = @"TextInput.setCaretRect";
|
||||
static NSString* const kUpdateEditStateResponseMethod = @"TextInputClient.updateEditingState";
|
||||
static NSString* const kUpdateEditStateWithDeltasResponseMethod =
|
||||
@"TextInputClient.updateEditingStateWithDeltas";
|
||||
static NSString* const kPerformAction = @"TextInputClient.performAction";
|
||||
static NSString* const kMultilineInputType = @"TextInputType.multiline";
|
||||
|
||||
@@ -33,6 +38,7 @@ static NSString* const kTextAffinityDownstream = @"TextAffinity.downstream";
|
||||
static NSString* const kTextAffinityUpstream = @"TextAffinity.upstream";
|
||||
|
||||
static NSString* const kTextInputAction = @"inputAction";
|
||||
static NSString* const kEnableDeltaModel = @"enableDeltaModel";
|
||||
static NSString* const kTextInputType = @"inputType";
|
||||
static NSString* const kTextInputTypeName = @"name";
|
||||
|
||||
@@ -124,6 +130,14 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
*/
|
||||
@property(nonatomic, nonnull) NSString* inputAction;
|
||||
|
||||
/**
|
||||
* Whether to enable the sending of text input updates from the engine to the
|
||||
* framework as TextEditingDeltas rather than as one TextEditingValue.
|
||||
* For more information on the delta model, see:
|
||||
* https://master-api.flutter.dev/flutter/services/TextInputConfiguration/enableDeltaModel.html
|
||||
*/
|
||||
@property(nonatomic) BOOL enableDeltaModel;
|
||||
|
||||
/**
|
||||
* Handles a Flutter system message on the text input channel.
|
||||
*/
|
||||
@@ -136,10 +150,17 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
- (void)setEditingState:(NSDictionary*)state;
|
||||
|
||||
/**
|
||||
* Informs the Flutter framework of changes to the text input model's state.
|
||||
* Informs the Flutter framework of changes to the text input model's state by
|
||||
* sending the entire new state.
|
||||
*/
|
||||
- (void)updateEditState;
|
||||
|
||||
/**
|
||||
* Informs the Flutter framework of changes to the text input model's state by
|
||||
* sending only the difference.
|
||||
*/
|
||||
- (void)updateEditStateWithDelta:(const flutter::TextEditingDelta)delta;
|
||||
|
||||
/**
|
||||
* Updates the stringValue and selectedRange that stored in the NSTextView interface
|
||||
* that this plugin inherits from.
|
||||
@@ -149,6 +170,12 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
*/
|
||||
- (void)updateTextAndSelection;
|
||||
|
||||
/**
|
||||
* Return the string representation of the current textAffinity as it should be
|
||||
* sent over the FlutterMethodChannel.
|
||||
*/
|
||||
- (NSString*)textAffinityString;
|
||||
|
||||
@end
|
||||
|
||||
@implementation FlutterTextInputPlugin {
|
||||
@@ -234,6 +261,7 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
|
||||
_clientID = clientID;
|
||||
_inputAction = config[kTextInputAction];
|
||||
_enableDeltaModel = [config[kEnableDeltaModel] boolValue];
|
||||
NSDictionary* inputTypeInfo = config[kTextInputType];
|
||||
_inputType = inputTypeInfo[kTextInputTypeName];
|
||||
self.textAffinity = FlutterTextAffinityUpstream;
|
||||
@@ -249,6 +277,7 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
} else if ([method isEqualToString:kClearClientMethod]) {
|
||||
_clientID = nil;
|
||||
_inputAction = nil;
|
||||
_enableDeltaModel = NO;
|
||||
_inputType = nil;
|
||||
_activeModel = nullptr;
|
||||
} else if ([method isEqualToString:kSetEditingStateMethod]) {
|
||||
@@ -258,7 +287,15 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
// 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.
|
||||
[self updateEditState];
|
||||
if (!_enableDeltaModel) {
|
||||
[self updateEditState];
|
||||
} else {
|
||||
// Send an "empty" delta. The client can compare the old_text with their
|
||||
// current text and update with that if the race condition described above
|
||||
// occurs.
|
||||
[self updateEditStateWithDelta:flutter::TextEditingDelta(_activeModel->GetText().c_str(),
|
||||
flutter::TextRange(0, 0), "")];
|
||||
}
|
||||
} else if ([method isEqualToString:kSetEditableSizeAndTransform]) {
|
||||
NSDictionary* state = call.arguments;
|
||||
[self setEditableTransform:state[kTransformKey]];
|
||||
@@ -333,9 +370,7 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
return;
|
||||
}
|
||||
|
||||
NSString* const textAffinity = (self.textAffinity == FlutterTextAffinityUpstream)
|
||||
? kTextAffinityUpstream
|
||||
: kTextAffinityDownstream;
|
||||
NSString* const textAffinity = [self textAffinityString];
|
||||
|
||||
int composingBase = _activeModel->composing() ? _activeModel->composing_range().base() : -1;
|
||||
int composingExtent = _activeModel->composing() ? _activeModel->composing_range().extent() : -1;
|
||||
@@ -354,6 +389,35 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
[self updateTextAndSelection];
|
||||
}
|
||||
|
||||
- (void)updateEditStateWithDelta:(const flutter::TextEditingDelta)delta {
|
||||
NSUInteger selectionBase = _activeModel->selection().base();
|
||||
NSUInteger selectionExtent = _activeModel->selection().extent();
|
||||
int composingBase = _activeModel->composing() ? _activeModel->composing_range().base() : -1;
|
||||
int composingExtent = _activeModel->composing() ? _activeModel->composing_range().extent() : -1;
|
||||
|
||||
NSString* const textAffinity = [self textAffinityString];
|
||||
|
||||
NSDictionary* deltaToFramework = @{
|
||||
@"oldText" : @(delta.old_text().c_str()),
|
||||
@"deltaText" : @(delta.delta_text().c_str()),
|
||||
@"deltaStart" : @(delta.delta_start()),
|
||||
@"deltaEnd" : @(delta.delta_end()),
|
||||
@"selectionBase" : @(selectionBase),
|
||||
@"selectionExtent" : @(selectionExtent),
|
||||
@"selectionAffinity" : textAffinity,
|
||||
@"selectionIsDirectional" : @(false),
|
||||
@"composingBase" : @(composingBase),
|
||||
@"composingExtent" : @(composingExtent),
|
||||
};
|
||||
|
||||
NSDictionary* deltas = @{
|
||||
@"deltas" : @[ deltaToFramework ],
|
||||
};
|
||||
|
||||
[_channel invokeMethod:kUpdateEditStateWithDeltasResponseMethod
|
||||
arguments:@[ self.clientID, deltas ]];
|
||||
}
|
||||
|
||||
- (void)updateTextAndSelection {
|
||||
NSAssert(_activeModel != nullptr, @"Flutter text model must not be null.");
|
||||
NSString* text = @(_activeModel->GetText().data());
|
||||
@@ -372,6 +436,11 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString*)textAffinityString {
|
||||
return (self.textAffinity == FlutterTextAffinityUpstream) ? kTextAffinityUpstream
|
||||
: kTextAffinityDownstream;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark FlutterKeySecondaryResponder
|
||||
|
||||
@@ -478,15 +547,29 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
|
||||
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]);
|
||||
flutter::TextRange oldSelection = _activeModel->selection();
|
||||
|
||||
std::string textBeforeChange = _activeModel->GetText().c_str();
|
||||
std::string utf8String = [string UTF8String];
|
||||
_activeModel->AddText(utf8String);
|
||||
if (_activeModel->composing()) {
|
||||
_activeModel->CommitComposing();
|
||||
_activeModel->EndComposing();
|
||||
}
|
||||
[self updateEditState];
|
||||
if (_enableDeltaModel) {
|
||||
flutter::TextRange replacedRange =
|
||||
range.location == NSNotFound
|
||||
? flutter::TextRange(oldSelection.base(), oldSelection.extent())
|
||||
: flutter::TextRange(range.location, range.location + range.length);
|
||||
[self updateEditStateWithDelta:flutter::TextEditingDelta(textBeforeChange, replacedRange,
|
||||
utf8String)];
|
||||
} else {
|
||||
[self updateEditState];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)doCommandBySelector:(SEL)selector {
|
||||
@@ -520,16 +603,23 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
if (_activeModel == nullptr) {
|
||||
return;
|
||||
}
|
||||
std::string textBeforeChange = _activeModel->GetText().c_str();
|
||||
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]);
|
||||
std::string marked_text = isAttributedString ? [[string string] UTF8String] : [string UTF8String];
|
||||
_activeModel->UpdateComposingText(marked_text);
|
||||
|
||||
[self updateEditState];
|
||||
if (_enableDeltaModel) {
|
||||
flutter::TextRange composing = _activeModel->composing_range();
|
||||
[self updateEditStateWithDelta:flutter::TextEditingDelta(textBeforeChange, composing,
|
||||
marked_text)];
|
||||
} else {
|
||||
[self updateEditState];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)unmarkText {
|
||||
@@ -538,7 +628,11 @@ static flutter::TextRange RangeFromBaseExtent(NSNumber* base,
|
||||
}
|
||||
_activeModel->CommitComposing();
|
||||
_activeModel->EndComposing();
|
||||
[self updateEditState];
|
||||
if (_enableDeltaModel) {
|
||||
[self updateEditStateWithDelta:flutter::TextEditingDelta(_activeModel->GetText().c_str())];
|
||||
} else {
|
||||
[self updateEditState];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSRange)markedRange {
|
||||
|
||||
@@ -176,6 +176,179 @@
|
||||
return NSEqualRects(rect, NSMakeRect(38, 20, 2, 19));
|
||||
}
|
||||
|
||||
- (bool)testSetEditingStateWithTextEditingDelta {
|
||||
id engineMock = OCMClassMock([FlutterEngine class]);
|
||||
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[engineMock binaryMessenger])
|
||||
.andReturn(binaryMessengerMock);
|
||||
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
|
||||
nibName:@""
|
||||
bundle:nil];
|
||||
|
||||
FlutterTextInputPlugin* plugin =
|
||||
[[FlutterTextInputPlugin alloc] initWithViewController:viewController];
|
||||
|
||||
[plugin handleMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInput.setClient"
|
||||
arguments:@[
|
||||
@(1), @{
|
||||
@"inputAction" : @"action",
|
||||
@"enableDeltaModel" : @"true",
|
||||
@"inputType" : @{@"name" : @"inputName"},
|
||||
}
|
||||
]]
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
FlutterMethodCall* call = [FlutterMethodCall methodCallWithMethodName:@"TextInput.setEditingState"
|
||||
arguments:@{
|
||||
@"text" : @"Text",
|
||||
@"selectionBase" : @(0),
|
||||
@"selectionExtent" : @(0),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
}];
|
||||
|
||||
[plugin handleMethodCall:call
|
||||
result:^(id){
|
||||
}];
|
||||
|
||||
// The setEditingState call is ACKed back to the framework.
|
||||
@try {
|
||||
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock
|
||||
sendOnChannel:@"flutter/textinput"
|
||||
message:[OCMArg checkWithBlock:^BOOL(NSData* callData) {
|
||||
FlutterMethodCall* call =
|
||||
[[FlutterJSONMethodCodec sharedInstance] decodeMethodCall:callData];
|
||||
return [[call method]
|
||||
isEqualToString:@"TextInputClient.updateEditingStateWithDeltas"];
|
||||
}]]);
|
||||
} @catch (...) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
- (bool)testOperationsThatTriggerDelta {
|
||||
id engineMock = OCMClassMock([FlutterEngine class]);
|
||||
id binaryMessengerMock = OCMProtocolMock(@protocol(FlutterBinaryMessenger));
|
||||
OCMStub( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[engineMock binaryMessenger])
|
||||
.andReturn(binaryMessengerMock);
|
||||
|
||||
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engineMock
|
||||
nibName:@""
|
||||
bundle:nil];
|
||||
|
||||
FlutterTextInputPlugin* plugin =
|
||||
[[FlutterTextInputPlugin alloc] initWithViewController:viewController];
|
||||
|
||||
[plugin handleMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInput.setClient"
|
||||
arguments:@[
|
||||
@(1), @{
|
||||
@"inputAction" : @"action",
|
||||
@"enableDeltaModel" : @"true",
|
||||
@"inputType" : @{@"name" : @"inputName"},
|
||||
}
|
||||
]]
|
||||
result:^(id){
|
||||
}];
|
||||
[plugin insertText:@"text to insert"];
|
||||
|
||||
NSDictionary* deltaToFramework = @{
|
||||
@"oldText" : @"",
|
||||
@"deltaText" : @"text to insert",
|
||||
@"deltaStart" : @(0),
|
||||
@"deltaEnd" : @(0),
|
||||
@"selectionBase" : @(14),
|
||||
@"selectionExtent" : @(14),
|
||||
@"selectionAffinity" : @"TextAffinity.upstream",
|
||||
@"selectionIsDirectional" : @(false),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
};
|
||||
NSDictionary* expectedState = @{
|
||||
@"deltas" : @[ deltaToFramework ],
|
||||
};
|
||||
|
||||
NSData* updateCall = [[FlutterJSONMethodCodec sharedInstance]
|
||||
encodeMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
|
||||
arguments:@[ @(1), expectedState ]]];
|
||||
|
||||
@try {
|
||||
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
} @catch (...) {
|
||||
return false;
|
||||
}
|
||||
|
||||
[plugin setMarkedText:@"marked text" selectedRange:NSMakeRange(0, 1)];
|
||||
|
||||
deltaToFramework = @{
|
||||
@"oldText" : @"text to insert",
|
||||
@"deltaText" : @"marked text",
|
||||
@"deltaStart" : @(14),
|
||||
@"deltaEnd" : @(25),
|
||||
@"selectionBase" : @(25),
|
||||
@"selectionExtent" : @(25),
|
||||
@"selectionAffinity" : @"TextAffinity.upstream",
|
||||
@"selectionIsDirectional" : @(false),
|
||||
@"composingBase" : @(14),
|
||||
@"composingExtent" : @(25),
|
||||
};
|
||||
expectedState = @{
|
||||
@"deltas" : @[ deltaToFramework ],
|
||||
};
|
||||
|
||||
updateCall = [[FlutterJSONMethodCodec sharedInstance]
|
||||
encodeMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
|
||||
arguments:@[ @(1), expectedState ]]];
|
||||
|
||||
@try {
|
||||
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
} @catch (...) {
|
||||
return false;
|
||||
}
|
||||
|
||||
[plugin unmarkText];
|
||||
|
||||
deltaToFramework = @{
|
||||
@"oldText" : @"text to insertmarked text",
|
||||
@"deltaText" : @"",
|
||||
@"deltaStart" : @(-1),
|
||||
@"deltaEnd" : @(-1),
|
||||
@"selectionBase" : @(25),
|
||||
@"selectionExtent" : @(25),
|
||||
@"selectionAffinity" : @"TextAffinity.upstream",
|
||||
@"selectionIsDirectional" : @(false),
|
||||
@"composingBase" : @(-1),
|
||||
@"composingExtent" : @(-1),
|
||||
};
|
||||
expectedState = @{
|
||||
@"deltas" : @[ deltaToFramework ],
|
||||
};
|
||||
|
||||
updateCall = [[FlutterJSONMethodCodec sharedInstance]
|
||||
encodeMethodCall:[FlutterMethodCall
|
||||
methodCallWithMethodName:@"TextInputClient.updateEditingStateWithDeltas"
|
||||
arguments:@[ @(1), expectedState ]]];
|
||||
|
||||
@try {
|
||||
OCMVerify( // NOLINT(google-objc-avoid-throwing-exception)
|
||||
[binaryMessengerMock sendOnChannel:@"flutter/textinput" message:updateCall]);
|
||||
} @catch (...) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
namespace flutter::testing {
|
||||
@@ -199,6 +372,14 @@ TEST(FlutterTextInputPluginTest, TestFirstRectForCharacterRange) {
|
||||
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testFirstRectForCharacterRange]);
|
||||
}
|
||||
|
||||
TEST(FlutterTextInputPluginTest, TestSetEditingStateWithTextEditingDelta) {
|
||||
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testSetEditingStateWithTextEditingDelta]);
|
||||
}
|
||||
|
||||
TEST(FlutterTextInputPluginTest, TestOperationsThatTriggerDelta) {
|
||||
ASSERT_TRUE([[FlutterInputPluginTestObjc alloc] testOperationsThatTriggerDelta]);
|
||||
}
|
||||
|
||||
TEST(FlutterTextInputPluginTest, CanWorkWithFlutterTextField) {
|
||||
FlutterEngine* engine = CreateTestEngine();
|
||||
NSString* fixtures = @(testing::GetFixturesPath());
|
||||
|
||||
Reference in New Issue
Block a user