Skip to content

Commit

Permalink
Migrate TextInputPlugin API to TextRange (flutter#21854)
Browse files Browse the repository at this point in the history
Replaces selection_base() and selection_extent() with selection() and
SetSelection(int, int) with SetSelection(range).

This also adds the following convenience methods to TextRange:
* reversed()
* Contains(size_t position)
* Contains(const TextRange& range)

as well as operator== for use in unit tests. When Flutter migrates to
C++20, we can replace that method with a default declaration.
  • Loading branch information
cbracken authored Oct 15, 2020
1 parent b22809b commit b715d3f
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 215 deletions.
7 changes: 3 additions & 4 deletions shell/platform/common/cpp/text_input_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ void TextInputModel::SetText(const std::string& text) {
selection_ = TextRange(0);
}

bool TextInputModel::SetSelection(size_t base, size_t extent) {
size_t max_pos = text_.length();
if (base > max_pos || extent > max_pos) {
bool TextInputModel::SetSelection(const TextRange& range) {
if (!text_range().Contains(range)) {
return false;
}
selection_ = TextRange(base, extent);
selection_ = range;
return true;
}

Expand Down
14 changes: 7 additions & 7 deletions shell/platform/common/cpp/text_input_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class TextInputModel {

// 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);
// Returns false if the selection is not within the bounds of the text.
bool SetSelection(const TextRange& range);

// Adds a Unicode code point.
//
Expand Down Expand Up @@ -105,11 +105,8 @@ class TextInputModel {
// GetText().
int GetCursorOffset() const;

// The position where the selection starts.
int selection_base() const { return selection_.base(); }

// The position of the cursor.
int selection_extent() const { return selection_.extent(); }
// The current selection.
TextRange selection() const { return selection_; }

private:
// Deletes the current selection, if any.
Expand All @@ -118,6 +115,9 @@ class TextInputModel {
// reset to the start of the selected range.
bool DeleteSelected();

// Returns a range covering the entire text.
TextRange text_range() const { return TextRange(0, text_.length()); }

std::u16string text_;
TextRange selection_ = TextRange(0);
};
Expand Down
Loading

0 comments on commit b715d3f

Please sign in to comment.