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

Migrate TextInputPlugin API to TextRange #21854

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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