提交 38664ac3 编写于 作者: C Chris Bracken 提交者: GitHub

Maintain FlutterTextInputView text in NSMutableString (#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.
上级 68a462fd
......@@ -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",
......
// 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 <string>
#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_
// 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 <Foundation/Foundation.h>
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<const char16_t*>(data.bytes), data.length};
}
} // namespace fml
......@@ -5,11 +5,7 @@
#include "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h"
#include <UIKit/UIKit.h>
#include <unicode/utf16.h>
#include <string>
#include "flutter/fml/platform/darwin/nsstring_utils.h"
#include <algorithm>
static const char _kTextAffinityDownstream[] = "TextAffinity.downstream";
static const char _kTextAffinityUpstream[] = "TextAffinity.upstream";
......@@ -26,6 +22,7 @@ static UIKeyboardType ToUIKeyboardType(NSString* inputType) {
@interface FlutterTextInputView : UIView<UIKeyInput>
@property(nonatomic, readonly) NSMutableString* text;
@property(nonatomic, assign) id<FlutterTextInputDelegate> 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;
......
......@@ -1388,8 +1388,6 @@ FILE: ../../../flutter/fml/platform/darwin/cf_utils.cc
FILE: ../../../flutter/fml/platform/darwin/cf_utils.h
FILE: ../../../flutter/fml/platform/darwin/message_loop_darwin.h
FILE: ../../../flutter/fml/platform/darwin/message_loop_darwin.mm
FILE: ../../../flutter/fml/platform/darwin/nsstring_utils.h
FILE: ../../../flutter/fml/platform/darwin/nsstring_utils.mm
FILE: ../../../flutter/fml/platform/darwin/paths_darwin.mm
FILE: ../../../flutter/fml/platform/darwin/resource_mapping_darwin.h
FILE: ../../../flutter/fml/platform/darwin/resource_mapping_darwin.mm
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册