Flutter Engine
flutter::TextInputModel Class Reference

#include <text_input_model.h>

Public Member Functions

 TextInputModel ()
 
virtual ~TextInputModel ()
 
void SetText (const std::string &text)
 
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.

References AddText().

Referenced by fl_text_input_plugin_filter_keypress_default(), and flutter::TextInputPlugin::TextInputPlugin().

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 }
void AddText(const std::u16string &text)

◆ AddText() [1/2]

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

Definition at line 129 of file text_input_model.cc.

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

Referenced by AddCodePoint(), AddText(), flutter::TextInputPlugin::ComposeChangeHook(), and im_commit_cb().

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 }
size_t position() const
Definition: text_range.h:68
void set_end(size_t pos)
Definition: text_range.h:57
size_t start() const
Definition: text_range.h:42
size_t length() const
Definition: text_range.h:74
std::u16string text

◆ AddText() [2/2]

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

Definition at line 142 of file text_input_model.cc.

References AddText().

142  {
143  std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
144  utf16_converter;
145  AddText(utf16_converter.from_bytes(text));
146 }
void AddText(const std::u16string &text)
std::u16string text

◆ Backspace()

bool flutter::TextInputModel::Backspace ( )

Definition at line 148 of file text_input_model.cc.

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

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

◆ BeginComposing()

void flutter::TextInputModel::BeginComposing ( )

Definition at line 65 of file text_input_model.cc.

References flutter::TextRange::start().

Referenced by im_preedit_start_cb().

65  {
66  composing_ = true;
67  composing_range_ = TextRange(selection_.start());
68 }
size_t start() const
Definition: text_range.h:42

◆ CommitComposing()

void flutter::TextInputModel::CommitComposing ( )

Definition at line 87 of file text_input_model.cc.

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

Referenced by im_commit_cb().

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 }
size_t end() const
Definition: text_range.h:54
bool collapsed() const
Definition: text_range.h:77

◆ composing()

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

Definition at line 193 of file text_input_model.h.

References text_range().

Referenced by flutter::TextInputPlugin::ComposeChangeHook(), im_commit_cb(), update_editing_state(), and update_im_cursor_position().

193 { return composing_; }

◆ composing_range()

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

Definition at line 190 of file text_input_model.h.

Referenced by flutter::TextInputPlugin::ComposeChangeHook(), im_preedit_changed_cb(), and update_editing_state().

190 { return composing_range_; }

◆ Delete()

bool flutter::TextInputModel::Delete ( )

Definition at line 166 of file text_input_model.cc.

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

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

◆ DeleteSurrounding()

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

Definition at line 183 of file text_input_model.cc.

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

Referenced by im_delete_surrounding_cb().

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

◆ EndComposing()

void flutter::TextInputModel::EndComposing ( )

Definition at line 96 of file text_input_model.cc.

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

Referenced by im_preedit_end_cb(), and set_editing_state().

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

◆ GetCursorOffset()

int flutter::TextInputModel::GetCursorOffset ( ) const

Definition at line 298 of file text_input_model.cc.

References flutter::TextRange::extent().

Referenced by im_retrieve_surrounding_cb().

298  {
299  // Measure the length of the current text up to the selection extent.
300  // There is probably a much more efficient way of doing this.
301  auto leading_text = text_.substr(0, selection_.extent());
302  std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
303  utf8_converter;
304  return utf8_converter.to_bytes(leading_text).size();
305 }
size_t extent() const
Definition: text_range.h:36

◆ GetText()

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

Definition at line 292 of file text_input_model.cc.

Referenced by flutter::TextInputPlugin::ComposeChangeHook(), im_retrieve_surrounding_cb(), flutter::TextInputPlugin::TextInputPlugin(), and update_editing_state().

292  {
293  std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
294  utf8_converter;
295  return utf8_converter.to_bytes(text_);
296 }

◆ MoveCursorBack()

bool flutter::TextInputModel::MoveCursorBack ( )

Definition at line 276 of file text_input_model.cc.

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

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

◆ MoveCursorForward()

bool flutter::TextInputModel::MoveCursorForward ( )

Definition at line 260 of file text_input_model.cc.

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

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

◆ MoveCursorToBeginning()

bool flutter::TextInputModel::MoveCursorToBeginning ( )

Definition at line 224 of file text_input_model.cc.

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

Referenced by fl_text_input_plugin_filter_keypress_default().

224  {
225  size_t min_pos = editable_range().start();
226  if (selection_.collapsed() && selection_.position() == min_pos) {
227  return false;
228  }
229  selection_ = TextRange(min_pos);
230  return true;
231 }
bool collapsed() const
Definition: text_range.h:77
size_t position() const
Definition: text_range.h:68
size_t start() const
Definition: text_range.h:42

◆ MoveCursorToEnd()

bool flutter::TextInputModel::MoveCursorToEnd ( )

Definition at line 233 of file text_input_model.cc.

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

Referenced by fl_text_input_plugin_filter_keypress_default().

233  {
234  size_t max_pos = editable_range().end();
235  if (selection_.collapsed() && selection_.position() == max_pos) {
236  return false;
237  }
238  selection_ = TextRange(max_pos);
239  return true;
240 }
size_t end() const
Definition: text_range.h:54
bool collapsed() const
Definition: text_range.h:77
size_t position() const
Definition: text_range.h:68

◆ selection()

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

◆ SelectToBeginning()

bool flutter::TextInputModel::SelectToBeginning ( )

Definition at line 242 of file text_input_model.cc.

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

Referenced by fl_text_input_plugin_filter_keypress_default().

242  {
243  size_t min_pos = editable_range().start();
244  if (selection_.collapsed() && selection_.position() == min_pos) {
245  return false;
246  }
247  selection_ = TextRange(selection_.base(), min_pos);
248  return true;
249 }
bool collapsed() const
Definition: text_range.h:77
size_t position() const
Definition: text_range.h:68
size_t start() const
Definition: text_range.h:42
size_t base() const
Definition: text_range.h:30

◆ SelectToEnd()

bool flutter::TextInputModel::SelectToEnd ( )

Definition at line 251 of file text_input_model.cc.

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

Referenced by fl_text_input_plugin_filter_keypress_default().

251  {
252  size_t max_pos = editable_range().end();
253  if (selection_.collapsed() && selection_.position() == max_pos) {
254  return false;
255  }
256  selection_ = TextRange(selection_.base(), max_pos);
257  return true;
258 }
size_t end() const
Definition: text_range.h:54
bool collapsed() const
Definition: text_range.h:77
size_t position() const
Definition: text_range.h:68
size_t base() const
Definition: text_range.h:30

◆ SetComposingRange()

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

Definition at line 55 of file text_input_model.cc.

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

Referenced by set_editing_state().

56  {
57  if (!composing_ || !text_range().Contains(range)) {
58  return false;
59  }
60  composing_range_ = range;
61  selection_ = TextRange(range.start() + cursor_offset);
62  return true;
63 }
TextRange text_range() const
bool Contains(const Container &container, const Value &value)

◆ SetSelection()

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

Definition at line 44 of file text_input_model.cc.

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

Referenced by im_preedit_changed_cb(), and set_editing_state().

44  {
45  if (composing_ && !range.collapsed()) {
46  return false;
47  }
48  if (!editable_range().Contains(range)) {
49  return false;
50  }
51  selection_ = range;
52  return true;
53 }
bool Contains(size_t position) const
Definition: text_range.h:83

◆ SetText()

void flutter::TextInputModel::SetText ( const std::string &  text)

Definition at line 36 of file text_input_model.cc.

Referenced by set_editing_state().

36  {
37  std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
38  utf16_converter;
39  text_ = utf16_converter.from_bytes(text);
40  selection_ = TextRange(0);
41  composing_range_ = TextRange(0);
42 }
std::u16string text

◆ text_range()

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

Definition at line 182 of file text_input_model.h.

Referenced by composing(), and SetComposingRange().

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

◆ UpdateComposingText() [1/2]

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

Definition at line 70 of file text_input_model.cc.

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

Referenced by im_preedit_changed_cb(), and UpdateComposingText().

70  {
71  // Preserve selection if we get a no-op update to the composing region.
72  if (text.length() == 0 && composing_range_.collapsed()) {
73  return;
74  }
75  DeleteSelected();
76  text_.replace(composing_range_.start(), composing_range_.length(), text);
77  composing_range_.set_end(composing_range_.start() + text.length());
78  selection_ = TextRange(composing_range_.end());
79 }
size_t end() const
Definition: text_range.h:54
bool collapsed() const
Definition: text_range.h:77
void set_end(size_t pos)
Definition: text_range.h:57
size_t start() const
Definition: text_range.h:42
size_t length() const
Definition: text_range.h:74
std::u16string text

◆ UpdateComposingText() [2/2]

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

Definition at line 81 of file text_input_model.cc.

References UpdateComposingText().

81  {
82  std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
83  utf16_converter;
84  UpdateComposingText(utf16_converter.from_bytes(text));
85 }
std::u16string text
void UpdateComposingText(const std::u16string &text)

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