未验证 提交 48d837b7 编写于 作者: C Chris Bracken 提交者: GitHub

Perform selection check in DeleteSelected (#21711)

At every call site for TextInputModel::DeleteSelected, we perform a
check for a collapsed selection. This moves that check into the method
itself.
上级 346e7e80
......@@ -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.
......
......@@ -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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册