| Flutter macOS Embedder |

Public Member Functions | List of all members
flutter::TextInputModel Class Reference

`#include <text_input_model.h>`

## Public Member Functions
| TextInputModel ()
virtual | ~TextInputModel ()
bool | SetText (const std::string &text, const TextRange &selection=TextRange(0), const TextRange &composing_range=TextRange(0))
bool | SetSelection (const TextRange &range)
bool | SetComposingRange (const TextRange &range, size_t cursor_offset)
void | BeginComposing ()
void | UpdateComposingText (const std::u16string &text)
void | UpdateComposingText (const std::string &text)
void | CommitComposing ()
void | EndComposing ()
void | AddCodePoint (char32_t c)
void | AddText (const std::u16string &text)
void | AddText (const std::string &text)
bool | Delete ()
bool | DeleteSurrounding (int offset_from_cursor, int count)
bool | Backspace ()
bool | MoveCursorBack ()
bool | MoveCursorForward ()
bool | MoveCursorToBeginning ()
bool | MoveCursorToEnd ()
bool | SelectToBeginning ()
bool | SelectToEnd ()
std::string | GetText () const
int | GetCursorOffset () const
TextRange | text_range () const
TextRange | selection () const
TextRange | composing_range () const
bool | composing () const

## Detailed Description

Definition at line 18 of file text_input_model.h.

## Constructor & Destructor Documentation

## ◆ TextInputModel()

flutter::TextInputModel::TextInputModel | ( |  | ) |
| default

## ◆ ~TextInputModel()

flutter::TextInputModel::~TextInputModel | ( |  | ) |
| virtualdefault

## Member Function Documentation

## ◆ AddCodePoint()

void flutter::TextInputModel::AddCodePoint | ( | char32_t | c | ) |

Definition at line 115 of file text_input_model.cc.

115 {
116 if (c <= 0xFFFF) {
117 AddText(std::u16string({static_cast<char16_t>(c)}));
118 } else {
119 char32_t to_decompose = c - 0x10000;
120 AddText(std::u16string({
121 // High surrogate.
122 static_cast<char16_t>((to_decompose >> 10) + 0xd800),
123 // Low surrogate.
124 static_cast<char16_t>((to_decompose % 0x400) + 0xdc00),
125 }));
126 }
127 }

References AddText().

## ◆ AddText() [1/2]

void flutter::TextInputModel::AddText | ( | const std::string & | text | ) |

Definition at line 142 of file text_input_model.cc.

142 {
143 AddText(fml::Utf8ToUtf16(text));
144 }

References AddText().

## ◆ AddText() [2/2]

void flutter::TextInputModel::AddText | ( | const std::u16string & | text | ) |

Definition at line 129 of file text_input_model.cc.

129 {
130 DeleteSelected();
131 if (composing_) {
132 // Delete the current composing text, set the cursor to composing start.
133 text_.erase(composing_range_.start(), composing_range_.length());
134 selection_ = TextRange(composing_range_.start());
135 composing_range_.set_end(composing_range_.start() + text.length());
136 }
137 size_t position = selection_.position();
138 text_.insert(position, text);
139 selection_ = TextRange(position + text.length());
140 }

References flutter::TextRange::length(), flutter::TextRange::position(), flutter::TextRange::set_end(), and flutter::TextRange::start().

Referenced by AddCodePoint(), and AddText().

## ◆ Backspace()

bool flutter::TextInputModel::Backspace | ( |  | ) |

Definition at line 146 of file text_input_model.cc.

146 {
147 if (DeleteSelected()) {
148 return true;
149 }
150 // There is no selection. Delete the preceding codepoint.
151 size_t position = selection_.position();
152 if (position != editable_range().start()) {
153 int count = IsTrailingSurrogate(text_.at(position - 1)) ? 2 : 1;
154 text_.erase(position - count, count);
155 selection_ = TextRange(position - count);
156 if (composing_) {
157 composing_range_.set_end(composing_range_.end() - count);
158 }
159 return true;
160 }
161 return false;
162 }

References flutter::TextRange::end(), flutter::TextRange::position(), and flutter::TextRange::set_end().

## ◆ BeginComposing()

void flutter::TextInputModel::BeginComposing | ( |  | ) |

Definition at line 67 of file text_input_model.cc.

67 {
68 composing_ = true;
69 composing_range_ = TextRange(selection_.start());
70 }

References flutter::TextRange::start().

## ◆ CommitComposing()

void flutter::TextInputModel::CommitComposing | ( |  | ) |

Definition at line 87 of file text_input_model.cc.

87 {
88 // Preserve selection if no composing text was entered.
89 if (composing_range_.collapsed()) {
90 return;
91 }
92 composing_range_ = TextRange(composing_range_.end());
93 selection_ = composing_range_;
94 }

References flutter::TextRange::collapsed(), and flutter::TextRange::end().

## ◆ composing()

bool flutter::TextInputModel::composing | ( |  | ) | const
| inline

Definition at line 204 of file text_input_model.h.

204 { return composing_; }

## ◆ composing_range()

TextRange flutter::TextInputModel::composing_range | ( |  | ) | const
| inline

Definition at line 201 of file text_input_model.h.

201 { return composing_range_; }

Referenced by SetText().

## ◆ Delete()

bool flutter::TextInputModel::Delete | ( |  | ) |

Definition at line 164 of file text_input_model.cc.

164 {
165 if (DeleteSelected()) {
166 return true;
167 }
168 // There is no selection. Delete the preceding codepoint.
169 size_t position = selection_.position();
170 if (position < editable_range().end()) {
171 int count = IsLeadingSurrogate(text_.at(position)) ? 2 : 1;
172 text_.erase(position, count);
173 if (composing_) {
174 composing_range_.set_end(composing_range_.end() - count);
175 }
176 return true;
177 }
178 return false;
179 }

References flutter::TextRange::end(), flutter::TextRange::position(), and flutter::TextRange::set_end().

## ◆ DeleteSurrounding()

bool flutter::TextInputModel::DeleteSurrounding | ( | int | offset_from_cursor,
|  | int | count
| ) |  |

Definition at line 181 of file text_input_model.cc.

181 {
182 size_t max_pos = editable_range().end();
183 size_t start = selection_.extent();
184 if (offset_from_cursor < 0) {
185 for (int i = 0; i < -offset_from_cursor; i++) {
186 // If requested start is before the available text then reduce the
187 // number of characters to delete.
188 if (start == editable_range().start()) {
189 count = i;
190 break;
191 }
192 start -= IsTrailingSurrogate(text_.at(start - 1)) ? 2 : 1;
193 }
194 } else {
195 for (int i = 0; i < offset_from_cursor && start != max_pos; i++) {
196 start += IsLeadingSurrogate(text_.at(start)) ? 2 : 1;
197 }
198 }
199
200 auto end = start;
201 for (int i = 0; i < count && end != max_pos; i++) {
202 end += IsLeadingSurrogate(text_.at(start)) ? 2 : 1;
203 }
204
205 if (start == end) {
206 return false;
207 }
208
209 auto deleted_length = end - start;
210 text_.erase(start, deleted_length);
211
212 // Cursor moves only if deleted area is before it.
213 selection_ = TextRange(offset_from_cursor <= 0 ? start : selection_.start());
214
215 // Adjust composing range.
216 if (composing_) {
217 composing_range_.set_end(composing_range_.end() - deleted_length);
218 }
219 return true;
220 }

References flutter::TextRange::end(), flutter::TextRange::extent(), flutter::TextRange::set_end(), and flutter::TextRange::start().

## ◆ EndComposing()

void flutter::TextInputModel::EndComposing | ( |  | ) |

Definition at line 96 of file text_input_model.cc.

96 {
97 composing_ = false;
98 composing_range_ = TextRange(0);
99 }

## ◆ GetCursorOffset()

int flutter::TextInputModel::GetCursorOffset | ( |  | ) | const

Definition at line 294 of file text_input_model.cc.

294 {
295 // Measure the length of the current text up to the selection extent.
296 // There is probably a much more efficient way of doing this.
297 auto leading_text = text_.substr(0, selection_.extent());
298 return fml::Utf16ToUtf8(leading_text).size();
299 }

References flutter::TextRange::extent().

## ◆ GetText()

std::string flutter::TextInputModel::GetText | ( |  | ) | const

Definition at line 290 of file text_input_model.cc.

290 {
291 return fml::Utf16ToUtf8(text_);
292 }

## ◆ MoveCursorBack()

bool flutter::TextInputModel::MoveCursorBack | ( |  | ) |

Definition at line 274 of file text_input_model.cc.

274 {
275 // If there's a selection, move to the beginning of the selection.
276 if (!selection_.collapsed()) {
277 selection_ = TextRange(selection_.start());
278 return true;
279 }
280 // Otherwise, move the cursor backward.
281 size_t position = selection_.position();
282 if (position != editable_range().start()) {
283 int count = IsTrailingSurrogate(text_.at(position - 1)) ? 2 : 1;
284 selection_ = TextRange(position - count);
285 return true;
286 }
287 return false;
288 }

References flutter::TextRange::collapsed(), flutter::TextRange::position(), and flutter::TextRange::start().

## ◆ MoveCursorForward()

bool flutter::TextInputModel::MoveCursorForward | ( |  | ) |

Definition at line 258 of file text_input_model.cc.

258 {
259 // If there's a selection, move to the end of the selection.
260 if (!selection_.collapsed()) {
261 selection_ = TextRange(selection_.end());
262 return true;
263 }
264 // Otherwise, move the cursor forward.
265 size_t position = selection_.position();
266 if (position != editable_range().end()) {
267 int count = IsLeadingSurrogate(text_.at(position)) ? 2 : 1;
268 selection_ = TextRange(position + count);
269 return true;
270 }
271 return false;
272 }

References flutter::TextRange::collapsed(), flutter::TextRange::end(), and flutter::TextRange::position().

## ◆ MoveCursorToBeginning()

bool flutter::TextInputModel::MoveCursorToBeginning | ( |  | ) |

Definition at line 222 of file text_input_model.cc.

222 {
223 size_t min_pos = editable_range().start();
224 if (selection_.collapsed() && selection_.position() == min_pos) {
225 return false;
226 }
227 selection_ = TextRange(min_pos);
228 return true;
229 }

References flutter::TextRange::collapsed(), flutter::TextRange::position(), and flutter::TextRange::start().

## ◆ MoveCursorToEnd()

bool flutter::TextInputModel::MoveCursorToEnd | ( |  | ) |

Definition at line 231 of file text_input_model.cc.

231 {
232 size_t max_pos = editable_range().end();
233 if (selection_.collapsed() && selection_.position() == max_pos) {
234 return false;
235 }
236 selection_ = TextRange(max_pos);
237 return true;
238 }

References flutter::TextRange::collapsed(), flutter::TextRange::end(), and flutter::TextRange::position().

## ◆ selection()

TextRange flutter::TextInputModel::selection | ( |  | ) | const
| inline

Definition at line 196 of file text_input_model.h.

196 { return selection_; }

Referenced by SetText().

## ◆ SelectToBeginning()

bool flutter::TextInputModel::SelectToBeginning | ( |  | ) |

Definition at line 240 of file text_input_model.cc.

240 {
241 size_t min_pos = editable_range().start();
242 if (selection_.collapsed() && selection_.position() == min_pos) {
243 return false;
244 }
245 selection_ = TextRange(selection_.base(), min_pos);
246 return true;
247 }

References flutter::TextRange::base(), flutter::TextRange::collapsed(), flutter::TextRange::position(), and flutter::TextRange::start().

## ◆ SelectToEnd()

bool flutter::TextInputModel::SelectToEnd | ( |  | ) |

Definition at line 249 of file text_input_model.cc.

249 {
250 size_t max_pos = editable_range().end();
251 if (selection_.collapsed() && selection_.position() == max_pos) {
252 return false;
253 }
254 selection_ = TextRange(selection_.base(), max_pos);
255 return true;
256 }

References flutter::TextRange::base(), flutter::TextRange::collapsed(), flutter::TextRange::end(), and flutter::TextRange::position().

## ◆ SetComposingRange()

bool flutter::TextInputModel::SetComposingRange | ( | const TextRange & | range,
|  | size_t | cursor_offset
| ) |  |

Definition at line 57 of file text_input_model.cc.

58 {
59 if (!composing_ || !text_range().Contains(range)) {
60 return false;
61 }
62 composing_range_ = range;
63 selection_ = TextRange(range.start() + cursor_offset);
64 return true;
65 }

References flutter::TextRange::start(), and text_range().

## ◆ SetSelection()

bool flutter::TextInputModel::SetSelection | ( | const TextRange & | range | ) |

Definition at line 46 of file text_input_model.cc.

46 {
47 if (composing_ && !range.collapsed()) {
48 return false;
49 }
50 if (!editable_range().Contains(range)) {
51 return false;
52 }
53 selection_ = range;
54 return true;
55 }

References flutter::TextRange::collapsed().

## ◆ SetText()

bool flutter::TextInputModel::SetText | ( | const std::string & | text,
|  | const TextRange & | selection = `TextRange(0)`,
|  | const TextRange & | composing_range = `TextRange(0)`
| ) |  |

Definition at line 31 of file text_input_model.cc.

33 {
34 text_ = fml::Utf8ToUtf16(text);
35 if (!text_range().Contains(selection) ||
36 !text_range().Contains(composing_range)) {
37 return false;
38 }
39
40 selection_ = selection;
41 composing_range_ = composing_range;
42 composing_ = !composing_range.collapsed();
43 return true;
44 }

References flutter::TextRange::collapsed(), composing_range(), selection(), and text_range().

## ◆ text_range()

TextRange flutter::TextInputModel::text_range | ( |  | ) | const
| inline

Definition at line 193 of file text_input_model.h.

193 { return TextRange(0, text_.length()); }

Referenced by SetComposingRange(), and SetText().

## ◆ UpdateComposingText() [1/2]

void flutter::TextInputModel::UpdateComposingText | ( | const std::string & | text | ) |

Definition at line 83 of file text_input_model.cc.

83 {
84 UpdateComposingText(fml::Utf8ToUtf16(text));
85 }

References UpdateComposingText().

## ◆ UpdateComposingText() [2/2]

void flutter::TextInputModel::UpdateComposingText | ( | const std::u16string & | text | ) |

Definition at line 72 of file text_input_model.cc.

72 {
73 // Preserve selection if we get a no-op update to the composing region.
74 if (text.length() == 0 && composing_range_.collapsed()) {
75 return;
76 }
77 DeleteSelected();
78 text_.replace(composing_range_.start(), composing_range_.length(), text);
79 composing_range_.set_end(composing_range_.start() + text.length());
80 selection_ = TextRange(composing_range_.end());
81 }

References flutter::TextRange::collapsed(), flutter::TextRange::end(), flutter::TextRange::length(), flutter::TextRange::set_end(), and flutter::TextRange::start().

Referenced by UpdateComposingText().

The documentation for this class was generated from the following files:

- shell/platform/common/text_input_model.h
- shell/platform/common/text_input_model.cc

flutter::TextRange::end
size_t end() const
Definition: text_range.h:54
flutter::TextInputModel::text_range
TextRange text_range() const
Definition: text_input_model.h:193
flutter::TextInputModel::composing_range
TextRange composing_range() const
Definition: text_input_model.h:201
flutter::TextRange::position
size_t position() const
Definition: text_range.h:68
flutter::TextInputModel::selection
TextRange selection() const
Definition: text_input_model.h:196
flutter::TextRange::base
size_t base() const
Definition: text_range.h:30
flutter::TextInputModel::UpdateComposingText
void UpdateComposingText(const std::u16string &text)
Definition: text_input_model.cc:72
flutter::TextRange::collapsed
bool collapsed() const
Definition: text_range.h:77
flutter::TextRange::extent
size_t extent() const
Definition: text_range.h:36
flutter::TextRange::start
size_t start() const
Definition: text_range.h:42
flutter::TextRange::length
size_t length() const
Definition: text_range.h:74
flutter::TextRange::set_end
void set_end(size_t pos)
Definition: text_range.h:57
flutter::TextInputModel::AddText
void AddText(const std::u16string &text)
Definition: text_input_model.cc:129