Flutter Windows Embedder
text_input_model.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 #include <algorithm>
8 #include <string>
9 #include <utility>
10 
11 #include "flutter/fml/logging.h"
12 #include "flutter/fml/string_conversion.h"
13 
14 namespace flutter {
15 
16 namespace {
17 
18 // Returns true if |code_point| is a leading surrogate of a surrogate pair.
19 bool IsLeadingSurrogate(char32_t code_point) {
20  return (code_point & 0xFFFFFC00) == 0xD800;
21 }
22 // Returns true if |code_point| is a trailing surrogate of a surrogate pair.
23 bool IsTrailingSurrogate(char32_t code_point) {
24  return (code_point & 0xFFFFFC00) == 0xDC00;
25 }
26 
27 } // namespace
28 
30 
32 
33 bool TextInputModel::SetText(const std::string& text,
34  const TextRange& selection,
35  const TextRange& composing_range) {
36  std::u16string new_text = fml::Utf8ToUtf16(text);
37  const TextRange new_text_range(0, new_text.length());
38  if (!new_text_range.Contains(selection) ||
39  !new_text_range.Contains(composing_range)) {
40  return false;
41  }
42 
43  text_ = std::move(new_text);
44  selection_ = selection;
45  composing_range_ = composing_range;
46  composing_ = !composing_range.collapsed();
47  return true;
48 }
49 
51  if (composing_ && !range.collapsed()) {
52  return false;
53  }
54  if (!editable_range().Contains(range)) {
55  return false;
56  }
57  selection_ = range;
58  return true;
59 }
60 
62  size_t cursor_offset) {
63  if (!composing_ || !text_range().Contains(range) ||
64  cursor_offset > text_.length() - range.start()) {
65  return false;
66  }
67  composing_range_ = range;
68  selection_ = TextRange(range.start() + cursor_offset);
69  return true;
70 }
71 
73  composing_ = true;
74  composing_range_ = TextRange(selection_.start());
75 }
76 
77 void TextInputModel::UpdateComposingText(const std::u16string& text,
78  const TextRange& selection) {
79  // Re-clamp current selection and composing range to text.
80  FML_DCHECK(text_range().Contains(selection_) &&
81  text_range().Contains(composing_range_));
82  selection_ = selection_.ClampedTo(text_.length());
83  composing_range_ = composing_range_.ClampedTo(text_.length());
84 
85  // Preserve selection if we get a no-op update to the composing region.
86  if (text.length() == 0 && composing_range_.collapsed()) {
87  return;
88  }
89  const TextRange& rangeToDelete =
90  composing_range_.collapsed() ? selection_ : composing_range_;
91  text_.replace(rangeToDelete.start(), rangeToDelete.length(), text);
92  composing_range_.set_end(composing_range_.start() + text.length());
93 
94  // Updated |selection| is supplied by the IME. Clamp to text length.
95  const TextRange clamped_selection = selection.ClampedTo(text.length());
96  selection_ = TextRange(clamped_selection.start() + composing_range_.start(),
97  clamped_selection.extent() + composing_range_.start());
98 }
99 
100 void TextInputModel::UpdateComposingText(const std::u16string& text) {
102 }
103 
104 void TextInputModel::UpdateComposingText(const std::string& text) {
105  UpdateComposingText(fml::Utf8ToUtf16(text));
106 }
107 
109  // Preserve selection if no composing text was entered.
110  if (composing_range_.collapsed()) {
111  return;
112  }
113  composing_range_ = TextRange(composing_range_.end());
114  selection_ = composing_range_;
115 }
116 
118  composing_ = false;
119  composing_range_ = TextRange(0);
120 }
121 
122 bool TextInputModel::DeleteSelected() {
123  if (selection_.collapsed()) {
124  return false;
125  }
126  size_t start = selection_.start();
127  text_.erase(start, selection_.length());
128  selection_ = TextRange(start);
129  if (composing_) {
130  // This occurs only immediately after composing has begun with a selection.
131  composing_range_ = selection_;
132  }
133  return true;
134 }
135 
137  if (c <= 0xFFFF) {
138  AddText(std::u16string({static_cast<char16_t>(c)}));
139  } else {
140  char32_t to_decompose = c - 0x10000;
141  AddText(std::u16string({
142  // High surrogate.
143  static_cast<char16_t>((to_decompose >> 10) + 0xd800),
144  // Low surrogate.
145  static_cast<char16_t>((to_decompose % 0x400) + 0xdc00),
146  }));
147  }
148 }
149 
150 void TextInputModel::AddText(const std::u16string& text) {
151  DeleteSelected();
152  if (composing_) {
153  // Delete the current composing text, set the cursor to composing start.
154  text_.erase(composing_range_.start(), composing_range_.length());
155  selection_ = TextRange(composing_range_.start());
156  composing_range_.set_end(composing_range_.start() + text.length());
157  }
158  size_t position = selection_.position();
159  text_.insert(position, text);
160  selection_ = TextRange(position + text.length());
161 }
162 
163 void TextInputModel::AddText(const std::string& text) {
164  AddText(fml::Utf8ToUtf16(text));
165 }
166 
168  if (DeleteSelected()) {
169  return true;
170  }
171  // There is no selection. Delete the preceding codepoint.
172  size_t position = selection_.position();
173  if (position != editable_range().start()) {
174  int count = IsTrailingSurrogate(text_.at(position - 1)) ? 2 : 1;
175  text_.erase(position - count, count);
176  selection_ = TextRange(position - count);
177  if (composing_) {
178  composing_range_.set_end(composing_range_.end() - count);
179  }
180  return true;
181  }
182  return false;
183 }
184 
186  if (DeleteSelected()) {
187  return true;
188  }
189  // There is no selection. Delete the preceding codepoint.
190  size_t position = selection_.position();
191  if (position < editable_range().end()) {
192  int count = IsLeadingSurrogate(text_.at(position)) ? 2 : 1;
193  text_.erase(position, count);
194  if (composing_) {
195  composing_range_.set_end(composing_range_.end() - count);
196  }
197  return true;
198  }
199  return false;
200 }
201 
202 bool TextInputModel::DeleteSurrounding(int offset_from_cursor, int count) {
203  size_t max_pos = editable_range().end();
204  size_t start = selection_.extent();
205  if (offset_from_cursor < 0) {
206  for (int i = 0; i < -offset_from_cursor; i++) {
207  // If the requested start is at or before the beginning of the
208  // available text then reduce the number of characters to delete. A step
209  // of 2 (crossing a surrogate pair) can land before the start of the
210  // range rather than exactly on it.
211  if (start <= editable_range().start()) {
212  count = i;
213  break;
214  }
215  start -= IsTrailingSurrogate(text_.at(start - 1)) ? 2 : 1;
216  }
217  } else {
218  for (int i = 0; i < offset_from_cursor && start < max_pos; i++) {
219  start += IsLeadingSurrogate(text_.at(start)) ? 2 : 1;
220  }
221  }
222 
223  auto end = start;
224  for (int i = 0; i < count && end < max_pos; i++) {
225  end += IsLeadingSurrogate(text_.at(end)) ? 2 : 1;
226  }
227 
228  if (start == end) {
229  return false;
230  }
231 
232  auto deleted_length = end - start;
233  text_.erase(start, deleted_length);
234 
235  // Cursor moves only if deleted area is before it.
236  selection_ = TextRange(offset_from_cursor <= 0 ? start : selection_.start());
237 
238  // Adjust composing range: a bound inside the deleted span collapses to its
239  // start, a bound after it shifts back by the deleted length, and a bound
240  // before it is unaffected. The deleted span can extend past the composing
241  // range's stored bounds when a surrogate pair straddles them, so shifting
242  // the end unconditionally could underflow, and the start needs the same
243  // treatment when the span extends past it in the other direction.
244  if (composing_) {
245  auto clamp_to_deletion = [start, end, deleted_length](size_t pos) {
246  if (pos <= start) {
247  return pos;
248  }
249  if (pos >= end) {
250  return pos - deleted_length;
251  }
252  return start;
253  };
254  composing_range_.set_base(clamp_to_deletion(composing_range_.base()));
255  composing_range_.set_extent(clamp_to_deletion(composing_range_.extent()));
256  }
257  return true;
258 }
259 
261  size_t min_pos = editable_range().start();
262  if (selection_.collapsed() && selection_.position() == min_pos) {
263  return false;
264  }
265  selection_ = TextRange(min_pos);
266  return true;
267 }
268 
270  size_t max_pos = editable_range().end();
271  if (selection_.collapsed() && selection_.position() == max_pos) {
272  return false;
273  }
274  selection_ = TextRange(max_pos);
275  return true;
276 }
277 
279  size_t min_pos = editable_range().start();
280  if (selection_.collapsed() && selection_.position() == min_pos) {
281  return false;
282  }
283  selection_ = TextRange(selection_.base(), min_pos);
284  return true;
285 }
286 
288  size_t max_pos = editable_range().end();
289  if (selection_.collapsed() && selection_.position() == max_pos) {
290  return false;
291  }
292  selection_ = TextRange(selection_.base(), max_pos);
293  return true;
294 }
295 
297  // If there's a selection, move to the end of the selection.
298  if (!selection_.collapsed()) {
299  selection_ = TextRange(selection_.end());
300  return true;
301  }
302  // Otherwise, move the cursor forward.
303  size_t position = selection_.position();
304  if (position != editable_range().end()) {
305  int count = IsLeadingSurrogate(text_.at(position)) ? 2 : 1;
306  selection_ = TextRange(position + count);
307  return true;
308  }
309  return false;
310 }
311 
313  // If there's a selection, move to the beginning of the selection.
314  if (!selection_.collapsed()) {
315  selection_ = TextRange(selection_.start());
316  return true;
317  }
318  // Otherwise, move the cursor backward.
319  size_t position = selection_.position();
320  if (position != editable_range().start()) {
321  int count = IsTrailingSurrogate(text_.at(position - 1)) ? 2 : 1;
322  selection_ = TextRange(position - count);
323  return true;
324  }
325  return false;
326 }
327 
328 std::string TextInputModel::GetText() const {
329  return fml::Utf16ToUtf8(text_);
330 }
331 
333  // Measure the length of the current text up to the selection extent.
334  // There is probably a much more efficient way of doing this.
335  auto leading_text = text_.substr(0, selection_.extent());
336  return fml::Utf16ToUtf8(leading_text).size();
337 }
338 
339 } // namespace flutter
TextRange selection() const
std::string GetText() const
bool DeleteSurrounding(int offset_from_cursor, int count)
void AddText(const std::u16string &text)
void AddCodePoint(char32_t c)
bool SetComposingRange(const TextRange &range, size_t cursor_offset)
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)
void set_end(size_t pos)
Definition: text_range.h:57
TextRange ClampedTo(size_t length) const
Definition: text_range.h:95
size_t position() const
Definition: text_range.h:68
void set_extent(size_t pos)
Definition: text_range.h:39
void set_base(size_t pos)
Definition: text_range.h:33
bool Contains(size_t position) const
Definition: text_range.h:83
size_t start() const
Definition: text_range.h:42
size_t base() const
Definition: text_range.h:30
bool collapsed() const
Definition: text_range.h:77
size_t length() const
Definition: text_range.h:74
size_t extent() const
Definition: text_range.h:36
size_t end() const
Definition: text_range.h:54
std::u16string text