Skip to content

Commit

Permalink
Separate mutators for text and selection (#21612)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cbracken committed Oct 6, 2020
1 parent 8842e50 commit 7e6191d
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 95 deletions.
19 changes: 11 additions & 8 deletions shell/platform/common/cpp/text_input_model.cc
Expand Up @@ -35,17 +35,20 @@ TextInputModel::TextInputModel()

TextInputModel::~TextInputModel() = default;

bool TextInputModel::SetEditingState(size_t selection_base,
size_t selection_extent,
const std::string& text) {
if (selection_base > text.size() || selection_extent > text.size()) {
return false;
}
void TextInputModel::SetText(const std::string& text) {
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>
utf16_converter;
text_ = utf16_converter.from_bytes(text);
selection_base_ = text_.begin() + selection_base;
selection_extent_ = text_.begin() + selection_extent;
selection_base_ = text_.begin();
selection_extent_ = selection_base_;
}

bool TextInputModel::SetSelection(size_t base, size_t extent) {
if (base > text_.size() || extent > text_.size()) {
return false;
}
selection_base_ = text_.begin() + base;
selection_extent_ = text_.begin() + extent;
return true;
}

Expand Down
14 changes: 8 additions & 6 deletions shell/platform/common/cpp/text_input_model.h
Expand Up @@ -17,13 +17,15 @@ class TextInputModel {
TextInputModel();
virtual ~TextInputModel();

// Attempts to set the text state.
// Sets the text.
//
// Returns false if the state is not valid (base or extent are out of
// bounds, or base is less than extent).
bool SetEditingState(size_t selection_base,
size_t selection_extent,
const std::string& text);
// Resets the selection base and extent.
void SetText(const std::string& text);

// Attempts to set the text selection.
//
// Returns false if the base or extent are out of bounds.
bool SetSelection(size_t base, size_t extent);

// Adds a Unicode code point.
//
Expand Down

0 comments on commit 7e6191d

Please sign in to comment.