From 919cb5634c798056b04b36a098d36d15b4eedf41 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Thu, 8 Oct 2020 16:02:06 -0700 Subject: [PATCH] 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. --- .../platform/common/cpp/text_input_model.cc | 16 ++++++++-------- .../shell/platform/common/cpp/text_input_model.h | 6 +++++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc b/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc index c30ff61f4d..8b17a3a26d 100644 --- a/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc +++ b/engine/src/flutter/shell/platform/common/cpp/text_input_model.cc @@ -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. diff --git a/engine/src/flutter/shell/platform/common/cpp/text_input_model.h b/engine/src/flutter/shell/platform/common/cpp/text_input_model.h index 866969fc88..fc2bd85ec8 100644 --- a/engine/src/flutter/shell/platform/common/cpp/text_input_model.h +++ b/engine/src/flutter/shell/platform/common/cpp/text_input_model.h @@ -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;