Skip to content

Commit 7e6191d

Browse files
authored
Separate mutators for text and selection (flutter#21612)
Previously, TextInputModel's SetEditingState method was a 1:1 mapping of the underlying protocol used on the text input channel between the framework and the engine. This breaks it up into two methods, which allows the selection to be updated independently of the text, and avoids tying the API the the underlying protocol. This will become more important when we add additional state to support composing regions for multi-step input methods such as those used for Japanese. SetText resets the selection rather than making a best-efforts attempt to preserve it. This choice was primarily to keep the code simple and make the API easier to reason about. An alternative would have been to make a best-effort attempt to preserve the selection, potentially clamping one or both to the end of the new string. In all cases where an embedder resets the string, it is expected that they also have the selection, so can call SetSelection with an updated selection if needed.
1 parent 8842e50 commit 7e6191d

File tree

6 files changed

+194
-95
lines changed

6 files changed

+194
-95
lines changed

shell/platform/common/cpp/text_input_model.cc

+11-8
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,20 @@ TextInputModel::TextInputModel()
3535

3636
TextInputModel::~TextInputModel() = default;
3737

38-
bool TextInputModel::SetEditingState(size_t selection_base,
39-
size_t selection_extent,
40-
const std::string& text) {
41-
if (selection_base > text.size() || selection_extent > text.size()) {
42-
return false;
43-
}
38+
void TextInputModel::SetText(const std::string& text) {
4439
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
4540
utf16_converter;
4641
text_ = utf16_converter.from_bytes(text);
47-
selection_base_ = text_.begin() + selection_base;
48-
selection_extent_ = text_.begin() + selection_extent;
42+
selection_base_ = text_.begin();
43+
selection_extent_ = selection_base_;
44+
}
45+
46+
bool TextInputModel::SetSelection(size_t base, size_t extent) {
47+
if (base > text_.size() || extent > text_.size()) {
48+
return false;
49+
}
50+
selection_base_ = text_.begin() + base;
51+
selection_extent_ = text_.begin() + extent;
4952
return true;
5053
}
5154

shell/platform/common/cpp/text_input_model.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ class TextInputModel {
1717
TextInputModel();
1818
virtual ~TextInputModel();
1919

20-
// Attempts to set the text state.
20+
// Sets the text.
2121
//
22-
// Returns false if the state is not valid (base or extent are out of
23-
// bounds, or base is less than extent).
24-
bool SetEditingState(size_t selection_base,
25-
size_t selection_extent,
26-
const std::string& text);
22+
// Resets the selection base and extent.
23+
void SetText(const std::string& text);
24+
25+
// Attempts to set the text selection.
26+
//
27+
// Returns false if the base or extent are out of bounds.
28+
bool SetSelection(size_t base, size_t extent);
2729

2830
// Adds a Unicode code point.
2931
//

0 commit comments

Comments
 (0)