| Flutter macOS Embedder |

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