Skip to content

Commit b715d3f

Browse files
authored
Migrate TextInputPlugin API to TextRange (flutter#21854)
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.
1 parent b22809b commit b715d3f

8 files changed

+287
-215
lines changed

shell/platform/common/cpp/text_input_model.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ void TextInputModel::SetText(const std::string& text) {
4141
selection_ = TextRange(0);
4242
}
4343

44-
bool TextInputModel::SetSelection(size_t base, size_t extent) {
45-
size_t max_pos = text_.length();
46-
if (base > max_pos || extent > max_pos) {
44+
bool TextInputModel::SetSelection(const TextRange& range) {
45+
if (!text_range().Contains(range)) {
4746
return false;
4847
}
49-
selection_ = TextRange(base, extent);
48+
selection_ = range;
5049
return true;
5150
}
5251

shell/platform/common/cpp/text_input_model.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class TextInputModel {
2727

2828
// Attempts to set the text selection.
2929
//
30-
// Returns false if the base or extent are out of bounds.
31-
bool SetSelection(size_t base, size_t extent);
30+
// Returns false if the selection is not within the bounds of the text.
31+
bool SetSelection(const TextRange& range);
3232

3333
// Adds a Unicode code point.
3434
//
@@ -105,11 +105,8 @@ class TextInputModel {
105105
// GetText().
106106
int GetCursorOffset() const;
107107

108-
// The position where the selection starts.
109-
int selection_base() const { return selection_.base(); }
110-
111-
// The position of the cursor.
112-
int selection_extent() const { return selection_.extent(); }
108+
// The current selection.
109+
TextRange selection() const { return selection_; }
113110

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

118+
// Returns a range covering the entire text.
119+
TextRange text_range() const { return TextRange(0, text_.length()); }
120+
121121
std::u16string text_;
122122
TextRange selection_ = TextRange(0);
123123
};

0 commit comments

Comments
 (0)