5#include "flutter/shell/platform/common/text_input_model.h"
10#include "flutter/fml/string_conversion.h"
17bool IsLeadingSurrogate(
char32_t code_point) {
18 return (code_point & 0xFFFFFC00) == 0xD800;
21bool IsTrailingSurrogate(
char32_t code_point) {
22 return (code_point & 0xFFFFFC00) == 0xDC00;
50 if (!editable_range().
Contains(range)) {
58 size_t cursor_offset) {
62 composing_range_ = range;
79 composing_range_.
collapsed() ? selection_ : composing_range_;
100 selection_ = composing_range_;
108bool TextInputModel::DeleteSelected() {
113 text_.erase(start, selection_.
length());
117 composing_range_ = selection_;
124 AddText(std::u16string({
static_cast<char16_t>(c)}));
126 char32_t to_decompose = c - 0x10000;
129 static_cast<char16_t>((to_decompose >> 10) + 0xd800),
131 static_cast<char16_t>((to_decompose % 0x400) + 0xdc00),
140 text_.erase(composing_range_.
start(), composing_range_.
length());
144 size_t position = selection_.
position();
145 text_.insert(position,
text);
154 if (DeleteSelected()) {
158 size_t position = selection_.
position();
159 if (position != editable_range().
start()) {
160 int count = IsTrailingSurrogate(text_.at(position - 1)) ? 2 : 1;
172 if (DeleteSelected()) {
176 size_t position = selection_.
position();
177 if (position < editable_range().
end()) {
178 int count = IsLeadingSurrogate(text_.at(position)) ? 2 : 1;
179 text_.erase(position,
count);
189 size_t max_pos = editable_range().
end();
191 if (offset_from_cursor < 0) {
192 for (
int i = 0;
i < -offset_from_cursor;
i++) {
199 start -= IsTrailingSurrogate(text_.at(
start - 1)) ? 2 : 1;
202 for (
int i = 0;
i < offset_from_cursor &&
start != max_pos;
i++) {
203 start += IsLeadingSurrogate(text_.at(
start)) ? 2 : 1;
208 for (
int i = 0;
i <
count &&
end != max_pos;
i++) {
209 end += IsLeadingSurrogate(text_.at(
start)) ? 2 : 1;
217 text_.erase(
start, deleted_length);
224 composing_range_.
set_end(composing_range_.
end() - deleted_length);
230 size_t min_pos = editable_range().
start();
239 size_t max_pos = editable_range().
end();
248 size_t min_pos = editable_range().
start();
257 size_t max_pos = editable_range().
end();
272 size_t position = selection_.
position();
273 if (position != editable_range().
end()) {
274 int count = IsLeadingSurrogate(text_.at(position)) ? 2 : 1;
288 size_t position = selection_.
position();
289 if (position != editable_range().
start()) {
290 int count = IsTrailingSurrogate(text_.at(position - 1)) ? 2 : 1;
304 auto leading_text = text_.substr(0, selection_.
extent());
TextRange selection() const
bool MoveCursorToBeginning()
std::string GetText() const
bool DeleteSurrounding(int offset_from_cursor, int count)
virtual ~TextInputModel()
void AddText(const std::u16string &text)
void AddCodePoint(char32_t c)
bool SetComposingRange(const TextRange &range, size_t cursor_offset)
int GetCursorOffset() const
TextRange text_range() const
TextRange composing_range() const
bool SetSelection(const TextRange &range)
bool SetText(const std::string &text, const TextRange &selection=TextRange(0), const TextRange &composing_range=TextRange(0))
void UpdateComposingText(const std::u16string &text, const TextRange &selection)
bool Contains(const Container &container, const Value &value)
std::string Utf16ToUtf8(const std::u16string_view string)
std::u16string Utf8ToUtf16(const std::string_view string)
SkRange< size_t > TextRange