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_CPP_TEXT_INPUT_MODEL_H_
6 #define FLUTTER_SHELL_PLATFORM_CPP_TEXT_INPUT_MODEL_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "flutter/shell/platform/common/cpp/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 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  // |composing_text| and the selection base and offset are set to the end of
54  // the composing range.
55  void UpdateComposingText(const std::string& composing_text);
56 
57  // Commits composing range to the string.
58  //
59  // Causes the composing base and extent to be collapsed to the end of the
60  // range.
61  void CommitComposing();
62 
63  // Ends IME composing mode.
64  //
65  // Collapses the composing base and offset to 0.
66  void EndComposing();
67 
68  // Adds a Unicode code point.
69  //
70  // Either appends after the cursor (when selection base and extent are the
71  // same), or deletes the selected text, replacing it with the given
72  // code point.
73  void AddCodePoint(char32_t c);
74 
75  // Adds UTF-16 text.
76  //
77  // Either appends after the cursor (when selection base and extent are the
78  // same), or deletes the selected text, replacing it with the given text.
79  void AddText(const std::u16string& text);
80 
81  // Adds UTF-8 text.
82  //
83  // Either appends after the cursor (when selection base and extent are the
84  // same), or deletes the selected text, replacing it with the given text.
85  void AddText(const std::string& text);
86 
87  // Deletes either the selection, or one character ahead of the cursor.
88  //
89  // Deleting one character ahead of the cursor occurs when the selection base
90  // and extent are the same. When composing is active, deletions are
91  // restricted to text between the composing base and extent.
92  //
93  // Returns true if any deletion actually occurred.
94  bool Delete();
95 
96  // Deletes text near the cursor.
97  //
98  // A section is made starting at |offset_from_cursor| code points past the
99  // cursor (negative values go before the cursor). |count| code points are
100  // removed. The selection may go outside the bounds of the available text and
101  // will result in only the part selection that covers the available text
102  // being deleted. The existing selection is ignored and removed after this
103  // operation. When composing is active, deletions are restricted to the
104  // composing range.
105  //
106  // Returns true if any deletion actually occurred.
107  bool DeleteSurrounding(int offset_from_cursor, int count);
108 
109  // Deletes either the selection, or one character behind the cursor.
110  //
111  // Deleting one character behind the cursor occurs when the selection base
112  // and extent are the same. When composing is active, deletions are
113  // restricted to the text between the composing base and extent.
114  //
115  // Returns true if any deletion actually occurred.
116  bool Backspace();
117 
118  // Attempts to move the cursor backward.
119  //
120  // Returns true if the cursor could be moved. If a selection is active, moves
121  // to the start of the selection. If composing is active, motion is
122  // restricted to the composing range.
123  bool MoveCursorBack();
124 
125  // Attempts to move the cursor forward.
126  //
127  // Returns true if the cursor could be moved. If a selection is active, moves
128  // to the end of the selection. If composing is active, motion is restricted
129  // to the composing range.
130  bool MoveCursorForward();
131 
132  // Attempts to move the cursor to the beginning.
133  //
134  // If composing is active, the cursor is moved to the beginning of the
135  // composing range; otherwise, it is moved to the beginning of the text. If
136  // composing is active, motion is restricted to the composing range.
137  //
138  // Returns true if the cursor could be moved.
139  bool MoveCursorToBeginning();
140 
141  // Attempts to move the cursor to the end.
142  //
143  // If composing is active, the cursor is moved to the end of the composing
144  // range; otherwise, it is moved to the end of the text. If composing is
145  // active, motion is restricted to the composing range.
146  //
147  // Returns true if the cursor could be moved.
148  bool MoveCursorToEnd();
149 
150  // Gets the current text as UTF-8.
151  std::string GetText() const;
152 
153  // Gets the cursor position as a byte offset in UTF-8 string returned from
154  // GetText().
155  int GetCursorOffset() const;
156 
157  // The current selection.
158  TextRange selection() const { return selection_; }
159 
160  // The composing range.
161  //
162  // If not in composing mode, returns a collapsed range at position 0.
163  TextRange composing_range() const { return composing_range_; }
164 
165  // Whether multi-step input composing mode is active.
166  bool composing() const { return composing_; }
167 
168  private:
169  // Deletes the current selection, if any.
170  //
171  // Returns true if any text is deleted. The selection base and extent are
172  // reset to the start of the selected range.
173  bool DeleteSelected();
174 
175  // Returns the currently editable text range.
176  //
177  // In composing mode, returns the composing range; otherwise, returns a range
178  // covering the entire text.
179  TextRange editable_range() const {
180  return composing_ ? composing_range_ : text_range();
181  }
182 
183  // Returns a range covering the entire text.
184  TextRange text_range() const { return TextRange(0, text_.length()); }
185 
186  std::u16string text_;
187  TextRange selection_ = TextRange(0);
188  TextRange composing_range_ = TextRange(0);
189  bool composing_ = false;
190 };
191 
192 } // namespace flutter
193 
194 #endif // FLUTTER_SHELL_PLATFORM_CPP_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)
void AddText(const std::u16string &text)
void UpdateComposingText(const std::string &composing_text)
TextRange composing_range() const
TextRange selection() const
bool SetSelection(const TextRange &range)
bool DeleteSurrounding(int offset_from_cursor, int count)