Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 844a3c1

Browse files
committed
Separate mutators for text and selection
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 becomre more important when we add additional state to support composing regions for multi-step input methods such as those used for Japanese.
1 parent 477c84c commit 844a3c1

File tree

6 files changed

+194
-95
lines changed

6 files changed

+194
-95
lines changed

shell/platform/common/cpp/text_input_model.cc

Lines changed: 11 additions & 8 deletions
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

Lines changed: 8 additions & 6 deletions
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)