Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Unified Diff: ui/views/controls/textfield/textfield_model.cc

Issue 1999773002: views::Textfield: Implement transpose editing command. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/views/controls/textfield/textfield_model.cc
diff --git a/ui/views/controls/textfield/textfield_model.cc b/ui/views/controls/textfield/textfield_model.cc
index d5bec8b33248d549600280d137186bbcf8557850..297ea1978cb84132388d102cbef5833fa8ade271 100644
--- a/ui/views/controls/textfield/textfield_model.cc
+++ b/ui/views/controls/textfield/textfield_model.cc
@@ -527,6 +527,36 @@ bool TextfieldModel::Paste() {
return true;
}
+bool TextfieldModel::Transpose() {
+ if (HasCompositionText() || HasSelection())
+ return false;
+
+ size_t cur = GetCursorPosition();
+ size_t next = render_text_->IndexOfAdjacentGrapheme(cur, gfx::CURSOR_FORWARD);
+ size_t prev =
+ render_text_->IndexOfAdjacentGrapheme(cur, gfx::CURSOR_BACKWARD);
+
+ // At the end of the line, the last two characters should be transposed.
+ if (cur == text().length()) {
+ DCHECK_EQ(cur, next);
+ cur = prev;
+ prev = render_text_->IndexOfAdjacentGrapheme(prev, gfx::CURSOR_BACKWARD);
+ }
+
+ // This happens at the beginning of the line or when the line has less than
+ // two graphemes.
+ if (gfx::UTF16IndexToOffset(text(), prev, next) != 2)
+ return false;
+
+ SelectRange(gfx::Range(prev, next));
+ base::string16 text = GetSelectedText();
+ base::string16 transposed_text =
+ text.substr(cur - prev) + text.substr(0, cur - prev);
+
+ InsertTextInternal(transposed_text, false);
+ return true;
+}
+
bool TextfieldModel::HasSelection() const {
return !render_text_->selection().is_empty();
}
« no previous file with comments | « ui/views/controls/textfield/textfield_model.h ('k') | ui/views/controls/textfield/textfield_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698