From d3149a5875404a4dacc7e4dde9c96045e2cbe6e5 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Mon, 3 Apr 2017 17:23:07 -0700 Subject: [PATCH] Maintain FlutterTextInputView text in NSMutableString (flutter/engine#3554) * Maintain FlutterTextInputView text in NSMutableString We were converting from/to NSString at the interface of FlutterTextInputView; instead use NSMutableString to maintain the internal buffer. Eliminates nsstring_utils.{h,mm} which no longer has any users. --- engine/src/flutter/fml/BUILD.gn | 2 -- .../fml/platform/darwin/nsstring_utils.h | 22 ------------- .../fml/platform/darwin/nsstring_utils.mm | 25 --------------- .../Source/FlutterTextInputPlugin.mm | 31 ++++++++----------- 4 files changed, 13 insertions(+), 67 deletions(-) delete mode 100644 engine/src/flutter/fml/platform/darwin/nsstring_utils.h delete mode 100644 engine/src/flutter/fml/platform/darwin/nsstring_utils.mm diff --git a/engine/src/flutter/fml/BUILD.gn b/engine/src/flutter/fml/BUILD.gn index c8fe26e3b1..0713ddab24 100644 --- a/engine/src/flutter/fml/BUILD.gn +++ b/engine/src/flutter/fml/BUILD.gn @@ -43,8 +43,6 @@ source_set("fml") { "platform/darwin/cf_utils.h", "platform/darwin/message_loop_darwin.h", "platform/darwin/message_loop_darwin.mm", - "platform/darwin/nsstring_utils.h", - "platform/darwin/nsstring_utils.mm", "platform/darwin/paths_darwin.mm", "platform/darwin/resource_mapping_darwin.h", "platform/darwin/resource_mapping_darwin.mm", diff --git a/engine/src/flutter/fml/platform/darwin/nsstring_utils.h b/engine/src/flutter/fml/platform/darwin/nsstring_utils.h deleted file mode 100644 index b6748ffb12..0000000000 --- a/engine/src/flutter/fml/platform/darwin/nsstring_utils.h +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2017 The Chromium 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_FML_PLATFORM_DARWIN_NSSTRING_UTILS_H_ -#define FLUTTER_FML_PLATFORM_DARWIN_NSSTRING_UTILS_H_ - -#include - -#include "lib/ftl/macros.h" - -@class NSString; - -namespace fml { - -NSString* StringToNSString(const std::u16string& string); - -std::u16string StringFromNSString(NSString* string); - -} // namespace fml - -#endif // FLUTTER_FML_PLATFORM_DARWIN_NSSTRING_UTILS_H_ diff --git a/engine/src/flutter/fml/platform/darwin/nsstring_utils.mm b/engine/src/flutter/fml/platform/darwin/nsstring_utils.mm deleted file mode 100644 index 266d4a389f..0000000000 --- a/engine/src/flutter/fml/platform/darwin/nsstring_utils.mm +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2017 The Chromium 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/fml/platform/darwin/nsstring_utils.h" - -#include - -namespace fml { - -NSString* StringToNSString(const std::u16string& string) { - return [[[NSString alloc] initWithBytes:string.data() - length:string.length() - encoding:NSUTF16StringEncoding] autorelease]; -} - -std::u16string StringFromNSString(NSString* string) { - if (string.length == 0) { - return {}; - } - NSData* data = [string dataUsingEncoding:NSUTF16StringEncoding]; - return {reinterpret_cast(data.bytes), data.length}; -} - -} // namespace fml diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm index 973fb902e8..9c99cc37b7 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.mm @@ -5,11 +5,7 @@ #include "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h" #include -#include - -#include - -#include "flutter/fml/platform/darwin/nsstring_utils.h" +#include static const char _kTextAffinityDownstream[] = "TextAffinity.downstream"; static const char _kTextAffinityUpstream[] = "TextAffinity.upstream"; @@ -26,6 +22,7 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) { @interface FlutterTextInputView : UIView +@property(nonatomic, readonly) NSMutableString* text; @property(nonatomic, assign) id textInputDelegate; @end @@ -35,7 +32,6 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) { int _selectionBase; int _selectionExtent; const char* _selectionAffinity; - std::u16string _text; } @synthesize keyboardType = _keyboardType; @@ -48,11 +44,17 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) { if (self) { _selectionBase = -1; _selectionExtent = -1; + _text = [[NSMutableString alloc] init]; } return self; } +- (void)dealloc { + [_text release]; + [super dealloc]; +} + - (void)setTextInputClient:(int)client { _textInputClient = client; } @@ -63,7 +65,7 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) { _selectionAffinity = _kTextAffinityDownstream; if ([state[@"selectionAffinity"] isEqualToString:@(_kTextAffinityUpstream)]) _selectionAffinity = _kTextAffinityUpstream; - _text = fml::StringFromNSString(state[@"text"]); + [self.text setString:state[@"text"]]; } - (UITextAutocorrectionType)autocorrectionType { @@ -87,7 +89,7 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) { @"selectionIsDirectional" : @(false), @"composingBase" : @(0), @"composingExtent" : @(0), - @"text" : fml::StringToNSString(_text), + @"text" : [NSString stringWithString:self.text], }]; } @@ -99,7 +101,7 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) { int start = std::max(0, std::min(_selectionBase, _selectionExtent)); int end = std::max(0, std::max(_selectionBase, _selectionExtent)); int len = end - start; - _text.replace(start, len, fml::StringFromNSString(text)); + [self.text replaceCharactersInRange:NSMakeRange(start, len) withString:text]; int caret = start + text.length; _selectionBase = caret; _selectionExtent = caret; @@ -111,18 +113,11 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) { int start = std::max(0, std::min(_selectionBase, _selectionExtent)); int end = std::max(0, std::max(_selectionBase, _selectionExtent)); int len = end - start; - if (len > 0) { - _text.erase(start, len); - } else if (start > 0) { + if (len == 0 && start > 0) { start -= 1; len = 1; - if (start > 0 && UTF16_IS_LEAD(_text[start - 1]) && - UTF16_IS_TRAIL(_text[start])) { - start -= 1; - len += 1; - } - _text.erase(start, len); } + [self.text deleteCharactersInRange:NSMakeRange(start, len)]; _selectionBase = start; _selectionExtent = start; _selectionAffinity = _kTextAffinityDownstream;