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::string &composing_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 ()
 
std::string GetText () const
 
int GetCursorOffset () 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 113 of file text_input_model.cc.

References AddText().

Referenced by flutter::TextInputPlugin::TextInputPlugin().

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

◆ AddText() [1/2]

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

Definition at line 127 of file text_input_model.cc.

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

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

127  {
128  DeleteSelected();
129  if (composing_) {
130  // Delete the current composing text, set the cursor to composing start.
131  text_.erase(composing_range_.start(), composing_range_.length());
132  selection_ = TextRange(composing_range_.start());
133  composing_range_.set_end(composing_range_.start() + text.length());
134  }
135  size_t position = selection_.position();
136  text_.insert(position, text);
137  selection_ = TextRange(position + text.length());
138 }
size_t length() const
Definition: text_range.h:69
size_t position() const
Definition: text_range.h:63
void set_end(size_t pos)
Definition: text_range.h:52
size_t start() const
Definition: text_range.h:37

◆ AddText() [2/2]

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

Definition at line 140 of file text_input_model.cc.

References AddText().

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

◆ Backspace()

bool flutter::TextInputModel::Backspace ( )

Definition at line 146 of file text_input_model.cc.

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

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 }
size_t end() const
Definition: text_range.h:49
size_t position() const
Definition: text_range.h:63
void set_end(size_t pos)
Definition: text_range.h:52

◆ BeginComposing()

void flutter::TextInputModel::BeginComposing ( )

Definition at line 65 of file text_input_model.cc.

References TextRange::start().

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

◆ CommitComposing()

void flutter::TextInputModel::CommitComposing ( )

Definition at line 85 of file text_input_model.cc.

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

85  {
86  // Preserve selection if no composing text was entered.
87  if (composing_range_.collapsed()) {
88  return;
89  }
90  composing_range_ = TextRange(composing_range_.end());
91  selection_ = composing_range_;
92 }
size_t end() const
Definition: text_range.h:49
bool collapsed() const
Definition: text_range.h:72

◆ composing()

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

Definition at line 166 of file text_input_model.h.

166 { return composing_; }

◆ composing_range()

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

Definition at line 163 of file text_input_model.h.

163 { return composing_range_; }

◆ Delete()

bool flutter::TextInputModel::Delete ( )

Definition at line 164 of file text_input_model.cc.

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

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 }
size_t end() const
Definition: text_range.h:49
size_t position() const
Definition: text_range.h:63
void set_end(size_t pos)
Definition: text_range.h:52

◆ DeleteSurrounding()

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

Definition at line 181 of file text_input_model.cc.

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

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 }
size_t end() const
Definition: text_range.h:49
size_t extent() const
Definition: text_range.h:31
void set_end(size_t pos)
Definition: text_range.h:52
size_t start() const
Definition: text_range.h:37

◆ EndComposing()

void flutter::TextInputModel::EndComposing ( )

Definition at line 94 of file text_input_model.cc.

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

94  {
95  composing_ = false;
96  composing_range_ = TextRange(0);
97 }

◆ GetCursorOffset()

int flutter::TextInputModel::GetCursorOffset ( ) const

Definition at line 278 of file text_input_model.cc.

References TextRange::extent().

278  {
279  // Measure the length of the current text up to the selection extent.
280  // There is probably a much more efficient way of doing this.
281  auto leading_text = text_.substr(0, selection_.extent());
282  std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
283  utf8_converter;
284  return utf8_converter.to_bytes(leading_text).size();
285 }
size_t extent() const
Definition: text_range.h:31

◆ GetText()

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

Definition at line 272 of file text_input_model.cc.

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

272  {
273  std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
274  utf8_converter;
275  return utf8_converter.to_bytes(text_);
276 }

◆ MoveCursorBack()

bool flutter::TextInputModel::MoveCursorBack ( )

Definition at line 256 of file text_input_model.cc.

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

256  {
257  // If there's a selection, move to the beginning of the selection.
258  if (!selection_.collapsed()) {
259  selection_ = TextRange(selection_.start());
260  return true;
261  }
262  // Otherwise, move the cursor backward.
263  size_t position = selection_.position();
264  if (position != editable_range().start()) {
265  int count = IsTrailingSurrogate(text_.at(position - 1)) ? 2 : 1;
266  selection_ = TextRange(position - count);
267  return true;
268  }
269  return false;
270 }
size_t position() const
Definition: text_range.h:63
size_t start() const
Definition: text_range.h:37
bool collapsed() const
Definition: text_range.h:72

◆ MoveCursorForward()

bool flutter::TextInputModel::MoveCursorForward ( )

Definition at line 240 of file text_input_model.cc.

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

240  {
241  // If there's a selection, move to the end of the selection.
242  if (!selection_.collapsed()) {
243  selection_ = TextRange(selection_.end());
244  return true;
245  }
246  // Otherwise, move the cursor forward.
247  size_t position = selection_.position();
248  if (position != editable_range().end()) {
249  int count = IsLeadingSurrogate(text_.at(position)) ? 2 : 1;
250  selection_ = TextRange(position + count);
251  return true;
252  }
253  return false;
254 }
size_t end() const
Definition: text_range.h:49
size_t position() const
Definition: text_range.h:63
bool collapsed() const
Definition: text_range.h:72

◆ MoveCursorToBeginning()

bool flutter::TextInputModel::MoveCursorToBeginning ( )

Definition at line 222 of file text_input_model.cc.

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

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 }
size_t position() const
Definition: text_range.h:63
size_t start() const
Definition: text_range.h:37
bool collapsed() const
Definition: text_range.h:72

◆ MoveCursorToEnd()

bool flutter::TextInputModel::MoveCursorToEnd ( )

Definition at line 231 of file text_input_model.cc.

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

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 }
size_t end() const
Definition: text_range.h:49
size_t position() const
Definition: text_range.h:63
bool collapsed() const
Definition: text_range.h:72

◆ selection()

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

Definition at line 158 of file text_input_model.h.

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

158 { return selection_; }

◆ SetComposingRange()

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

Definition at line 55 of file text_input_model.cc.

References TextRange::start().

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 }
size_t start() const
Definition: text_range.h:37

◆ SetSelection()

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

Definition at line 44 of file text_input_model.cc.

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

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:78
bool collapsed() const
Definition: text_range.h:72

◆ SetText()

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

Definition at line 36 of file text_input_model.cc.

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 }

◆ UpdateComposingText()

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

Definition at line 70 of file text_input_model.cc.

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

70  {
71  std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
72  utf16_converter;
73  std::u16string text = utf16_converter.from_bytes(composing_text);
74 
75  // Preserve selection if we get a no-op update to the composing region.
76  if (text.length() == 0 && composing_range_.collapsed()) {
77  return;
78  }
79  DeleteSelected();
80  text_.replace(composing_range_.start(), composing_range_.length(), text);
81  composing_range_.set_end(composing_range_.start() + text.length());
82  selection_ = TextRange(composing_range_.end());
83 }
size_t end() const
Definition: text_range.h:49
size_t length() const
Definition: text_range.h:69
void set_end(size_t pos)
Definition: text_range.h:52
size_t start() const
Definition: text_range.h:37
bool collapsed() const
Definition: text_range.h:72

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