Flutter Engine
text_input_model.h
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 
5 #ifndef FLUTTER_SHELL_PLATFORM_COMMON_TEXT_INPUT_MODEL_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_TEXT_INPUT_MODEL_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "flutter/shell/platform/common/text_range.h"
12 
13 namespace flutter {
14 
15 // Handles underlying text input state, using a simple ASCII model.
16 //
17 // Ignores special states like "insert mode" for now.
19  public:
21  virtual ~TextInputModel();
22 
23  // Sets the text.
24  //
25  // Resets the selection base and extent.
26  void SetText(const std::string& text);
27 
28  // Attempts to set the text selection.
29  //
30  // Returns false if the selection is not within the bounds of the text.
31  // While in composing mode, the selection is restricted to the composing
32  // range; otherwise, it is restricted to the length of the text.
33  bool SetSelection(const TextRange& range);
34 
35  // Attempts to set the composing range.
36  //
37  // Returns false if the range or offset are out of range for the text, or if
38  // the offset is outside the composing range.
39  bool SetComposingRange(const TextRange& range, size_t cursor_offset);
40 
41  // Begins IME composing mode.
42  //
43  // Resets the composing base and extent to the selection start. The existing
44  // selection is preserved in case composing is aborted with no changes. Until
45  // |EndComposing| is called, any further changes to selection base and extent
46  // are restricted to the composing range.
47  void BeginComposing();
48 
49  // Replaces the composing range with new UTF-16 text.
50  //
51  // If a selection of non-zero length exists, it is deleted if the composing
52  // text is non-empty. The composing range is adjusted to the length of
53  // |text| and the selection base and offset are set to the end of the
54  // composing range.
55  void UpdateComposingText(const std::u16string& text);
56 
57  // Replaces the composing range with new UTF-8 text.
58  //
59  // If a selection of non-zero length exists, it is deleted if the composing
60  // text is non-empty. The composing range is adjusted to the length of
61  // |text| and the selection base and offset are set to the end of the
62  // composing range.
63  void UpdateComposingText(const std::string& text);
64 
65  // Commits composing range to the string.
66  //
67  // Causes the composing base and extent to be collapsed to the end of the
68  // range.
69  void CommitComposing();
70 
71  // Ends IME composing mode.
72  //
73  // Collapses the composing base and offset to 0.
74  void EndComposing();
75 
76  // Adds a Unicode code point.
77  //
78  // Either appends after the cursor (when selection base and extent are the
79  // same), or deletes the selected text, replacing it with the given
80  // code point.
81  void AddCodePoint(char32_t c);
82 
83  // Adds UTF-16 text.
84  //
85  // Either appends after the cursor (when selection base and extent are the
86  // same), or deletes the selected text, replacing it with the given text.
87  void AddText(const std::u16string& text);
88 
89  // Adds UTF-8 text.
90  //
91  // Either appends after the cursor (when selection base and extent are the
92  // same), or deletes the selected text, replacing it with the given text.
93  void AddText(const std::string& text);
94 
95  // Deletes either the selection, or one character ahead of the cursor.
96  //
97  // Deleting one character ahead of the cursor occurs when the selection base
98  // and extent are the same. When composing is active, deletions are
99  // restricted to text between the composing base and extent.
100  //
101  // Returns true if any deletion actually occurred.
102  bool Delete();
103 
104  // Deletes text near the cursor.
105  //
106  // A section is made starting at |offset_from_cursor| code points past the
107  // cursor (negative values go before the cursor). |count| code points are
108  // removed. The selection may go outside the bounds of the available text and
109  // will result in only the part selection that covers the available text
110  // being deleted. The existing selection is ignored and removed after this
111  // operation. When composing is active, deletions are restricted to the
112  // composing range.
113  //
114  // Returns true if any deletion actually occurred.
115  bool DeleteSurrounding(int offset_from_cursor, int count);
116 
117  // Deletes either the selection, or one character behind the cursor.
118  //
119  // Deleting one character behind the cursor occurs when the selection base
120  // and extent are the same. When composing is active, deletions are
121  // restricted to the text between the composing base and extent.
122  //
123  // Returns true if any deletion actually occurred.
124  bool Backspace();
125 
126  // Attempts to move the cursor backward.
127  //
128  // Returns true if the cursor could be moved. If a selection is active, moves
129  // to the start of the selection. If composing is active, motion is
130  // restricted to the composing range.
131  bool MoveCursorBack();
132 
133  // Attempts to move the cursor forward.
134  //
135  // Returns true if the cursor could be moved. If a selection is active, moves
136  // to the end of the selection. If composing is active, motion is restricted
137  // to the composing range.
138  bool MoveCursorForward();
139 
140  // Attempts to move the cursor to the beginning.
141  //
142  // If composing is active, the cursor is moved to the beginning of the
143  // composing range; otherwise, it is moved to the beginning of the text. If
144  // composing is active, motion is restricted to the composing range.
145  //
146  // Returns true if the cursor could be moved.
147  bool MoveCursorToBeginning();
148 
149  // Attempts to move the cursor to the end.
150  //
151  // If composing is active, the cursor is moved to the end of the composing
152  // range; otherwise, it is moved to the end of the text. If composing is
153  // active, motion is restricted to the composing range.
154  //
155  // Returns true if the cursor could be moved.
156  bool MoveCursorToEnd();
157 
158  // Attempts to select text from the cursor position to the beginning.
159  //
160  // If composing is active, the selection is applied to the beginning of the
161  // composing range; otherwise, it is applied to the beginning of the text.
162  //
163  // Returns true if the selection could be applied.
164  bool SelectToBeginning();
165 
166  // Attempts to select text from the cursor position to the end.
167  //
168  // If composing is active, the selection is applied to the end of the
169  // composing range; otherwise, it is moved to the end of the text.
170  //
171  // Returns true if the selection could be applied.
172  bool SelectToEnd();
173 
174  // Gets the current text as UTF-8.
175  std::string GetText() const;
176 
177  // Gets the cursor position as a byte offset in UTF-8 string returned from
178  // GetText().
179  int GetCursorOffset() const;
180 
181  // Returns a range covering the entire text.
182  TextRange text_range() const { return TextRange(0, text_.length()); }
183 
184  // The current selection.
185  TextRange selection() const { return selection_; }
186 
187  // The composing range.
188  //
189  // If not in composing mode, returns a collapsed range at position 0.
190  TextRange composing_range() const { return composing_range_; }
191 
192  // Whether multi-step input composing mode is active.
193  bool composing() const { return composing_; }
194 
195  private:
196  // Deletes the current selection, if any.
197  //
198  // Returns true if any text is deleted. The selection base and extent are
199  // reset to the start of the selected range.
200  bool DeleteSelected();
201 
202  // Returns the currently editable text range.
203  //
204  // In composing mode, returns the composing range; otherwise, returns a range
205  // covering the entire text.
206  TextRange editable_range() const {
207  return composing_ ? composing_range_ : text_range();
208  }
209 
210  std::u16string text_;
211  TextRange selection_ = TextRange(0);
212  TextRange composing_range_ = TextRange(0);
213  bool composing_ = false;
214 };
215 
216 } // namespace flutter
217 
218 #endif // FLUTTER_SHELL_PLATFORM_COMMON_TEXT_INPUT_MODEL_H_
void SetText(const std::string &text)
std::string GetText() const
void AddCodePoint(char32_t c)
bool SetComposingRange(const TextRange &range, size_t cursor_offset)
TextRange text_range() const
void AddText(const std::u16string &text)
TextRange composing_range() const
TextRange selection() const
std::u16string text
bool SetSelection(const TextRange &range)
bool DeleteSurrounding(int offset_from_cursor, int count)
void UpdateComposingText(const std::u16string &text)