Perform selection check in DeleteSelected (flutter/engine#21711)

At every call site for TextInputModel::DeleteSelected, we perform a
check for a collapsed selection. This moves that check into the method
itself.
This commit is contained in:
Chris Bracken
2020-10-08 16:02:06 -07:00
committed by GitHub
parent 51d3d099d4
commit 919cb5634c
2 changed files with 13 additions and 9 deletions

View File

@@ -52,10 +52,14 @@ bool TextInputModel::SetSelection(size_t base, size_t extent) {
return true;
}
void TextInputModel::DeleteSelected() {
bool TextInputModel::DeleteSelected() {
if (selection_base_ == selection_extent_) {
return false;
}
text_.erase(selection_start(), selection_end() - selection_start());
selection_base_ = selection_start();
selection_extent_ = selection_base_;
return true;
}
void TextInputModel::AddCodePoint(char32_t c) {
@@ -73,9 +77,7 @@ void TextInputModel::AddCodePoint(char32_t c) {
}
void TextInputModel::AddText(const std::u16string& text) {
if (selection_base_ != selection_extent_) {
DeleteSelected();
}
DeleteSelected();
text_.insert(selection_extent_, text);
selection_extent_ += text.length();
selection_base_ = selection_extent_;
@@ -89,8 +91,7 @@ void TextInputModel::AddText(const std::string& text) {
bool TextInputModel::Backspace() {
// If there's a selection, delete it.
if (selection_base_ != selection_extent_) {
DeleteSelected();
if (DeleteSelected()) {
return true;
}
// There's no selection; delete the preceding codepoint.
@@ -106,8 +107,7 @@ bool TextInputModel::Backspace() {
bool TextInputModel::Delete() {
// If there's a selection, delete it.
if (selection_base_ != selection_extent_) {
DeleteSelected();
if (DeleteSelected()) {
return true;
}
// There's no selection; delete the following codepoint.

View File

@@ -110,7 +110,11 @@ class TextInputModel {
int selection_extent() const { return selection_extent_; }
private:
void DeleteSelected();
// Deletes the current selection, if any.
//
// Returns true if any text is deleted. The selection base and extent are
// reset to the start of the selected range.
bool DeleteSelected();
std::u16string text_;
size_t selection_base_ = 0;