GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • text_input_model.h
  • text_input_model.cc
  • flutter
  • TextInputModel

This view

  • Original page
  • New address

GeezerVison is showing api.flutter.dev — Flutter Linux Embedder: flutter::TextInputModel Class Reference

Logo
Flutter Linux 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, const TextRange &selection)
 
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 136 of file text_input_model.cc.

136  {
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 }
flutter::TextInputModel::AddText
void AddText(const std::u16string &text)
Definition: text_input_model.cc:150

References AddText().

◆ AddText() [1/2]

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

Definition at line 163 of file text_input_model.cc.

163  {
164  AddText(fml::Utf8ToUtf16(text));
165 }

References AddText().

◆ AddText() [2/2]

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

Definition at line 150 of file text_input_model.cc.

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

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 167 of file text_input_model.cc.

167  {
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 }
flutter::TextRange::end
size_t end() const
Definition: text_range.h:54
start
glong start
Definition: fl_accessible_text_field.cc:39

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

◆ BeginComposing()

void flutter::TextInputModel::BeginComposing ( )

Definition at line 72 of file text_input_model.cc.

72  {
73  composing_ = true;
74  composing_range_ = TextRange(selection_.start());
75 }

References flutter::TextRange::start().

◆ CommitComposing()

void flutter::TextInputModel::CommitComposing ( )

Definition at line 108 of file text_input_model.cc.

108  {
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 }
flutter::TextRange::collapsed
bool collapsed() const
Definition: text_range.h:77

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

◆ composing()

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

Definition at line 214 of file text_input_model.h.

214 { return composing_; }

◆ composing_range()

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

Definition at line 211 of file text_input_model.h.

211 { return composing_range_; }

Referenced by SetText().

◆ Delete()

bool flutter::TextInputModel::Delete ( )

Definition at line 185 of file text_input_model.cc.

185  {
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 }
end
glong glong end
Definition: fl_accessible_text_field.cc:40

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

◆ DeleteSurrounding()

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

Definition at line 202 of file text_input_model.cc.

202  {
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 }
flutter::TextRange::set_extent
void set_extent(size_t pos)
Definition: text_range.h:39
flutter::TextRange::set_base
void set_base(size_t pos)
Definition: text_range.h:33
flutter::TextRange::base
size_t base() const
Definition: text_range.h:30
flutter::TextRange::extent
size_t extent() const
Definition: text_range.h:36
i
int i
Definition: fl_socket_accessible.cc:18

References flutter::TextRange::base(), flutter::TextRange::end(), end, flutter::TextRange::extent(), i, flutter::TextRange::set_base(), flutter::TextRange::set_extent(), flutter::TextRange::start(), and start.

◆ EndComposing()

void flutter::TextInputModel::EndComposing ( )

Definition at line 117 of file text_input_model.cc.

117  {
118  composing_ = false;
119  composing_range_ = TextRange(0);
120 }

◆ GetCursorOffset()

int flutter::TextInputModel::GetCursorOffset ( ) const

Definition at line 332 of file text_input_model.cc.

332  {
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 }

References flutter::TextRange::extent().

◆ GetText()

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

Definition at line 328 of file text_input_model.cc.

328  {
329  return fml::Utf16ToUtf8(text_);
330 }

◆ MoveCursorBack()

bool flutter::TextInputModel::MoveCursorBack ( )

Definition at line 312 of file text_input_model.cc.

312  {
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 }

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

◆ MoveCursorForward()

bool flutter::TextInputModel::MoveCursorForward ( )

Definition at line 296 of file text_input_model.cc.

296  {
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 }

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

◆ MoveCursorToBeginning()

bool flutter::TextInputModel::MoveCursorToBeginning ( )

Definition at line 260 of file text_input_model.cc.

260  {
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 }

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

◆ MoveCursorToEnd()

bool flutter::TextInputModel::MoveCursorToEnd ( )

Definition at line 269 of file text_input_model.cc.

269  {
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 }

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

◆ selection()

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

Definition at line 206 of file text_input_model.h.

206 { return selection_; }

Referenced by SetText(), and UpdateComposingText().

◆ SelectToBeginning()

bool flutter::TextInputModel::SelectToBeginning ( )

Definition at line 278 of file text_input_model.cc.

278  {
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 }

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

◆ SelectToEnd()

bool flutter::TextInputModel::SelectToEnd ( )

Definition at line 287 of file text_input_model.cc.

287  {
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 }

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 61 of file text_input_model.cc.

62  {
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 }
flutter::TextInputModel::text_range
TextRange text_range() const
Definition: text_input_model.h:203

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

◆ SetSelection()

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

Definition at line 50 of file text_input_model.cc.

50  {
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 }

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 33 of file text_input_model.cc.

35  {
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 }
flutter::TextInputModel::selection
TextRange selection() const
Definition: text_input_model.h:206
flutter::TextInputModel::composing_range
TextRange composing_range() const
Definition: text_input_model.h:211

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

◆ text_range()

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

Definition at line 203 of file text_input_model.h.

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

Referenced by SetComposingRange(), and UpdateComposingText().

◆ UpdateComposingText() [1/3]

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

Definition at line 104 of file text_input_model.cc.

104  {
105  UpdateComposingText(fml::Utf8ToUtf16(text));
106 }
flutter::TextInputModel::UpdateComposingText
void UpdateComposingText(const std::u16string &text, const TextRange &selection)
Definition: text_input_model.cc:77

References UpdateComposingText().

◆ UpdateComposingText() [2/3]

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

Definition at line 100 of file text_input_model.cc.

100  {
101  UpdateComposingText(text, TextRange(text.length()));
102 }

References UpdateComposingText().

◆ UpdateComposingText() [3/3]

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

Definition at line 77 of file text_input_model.cc.

78  {
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 }
flutter::TextRange::ClampedTo
TextRange ClampedTo(size_t length) const
Definition: text_range.h:95

References flutter::TextRange::ClampedTo(), flutter::TextRange::collapsed(), flutter::TextRange::extent(), flutter::TextRange::length(), selection(), flutter::TextRange::set_end(), flutter::TextRange::start(), and text_range().

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
  • TextInputModel
  • Generated by doxygen 1.9.1
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.