Chromium Code Reviews| 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..89e1caa12b396dedaae140124e8eefc15f404dc2 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); |
| + |
| + // If we are at the end of the line, last two characters should be transposed. |
|
tapted
2016/06/28 04:52:32
nit: "If we are at .. line, last" -> "At .. line,
karandeepb
2016/06/29 05:44:37
Done.
|
| + if (cur == text().length()) { |
| + DCHECK_EQ(cur, next); |
| + cur = prev; |
| + prev = render_text_->IndexOfAdjacentGrapheme(prev, gfx::CURSOR_BACKWARD); |
| + } |
| + |
| + // This happens when we are at the beginning of the line or the line has less |
|
tapted
2016/06/28 04:52:32
nit: remove "when we are", add `when` in "..line o
karandeepb
2016/06/29 05:44:37
Done.
|
| + // 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); |
|
tapted
2016/06/28 04:52:32
Should we add some emoji/surrogate pairs to Bridge
karandeepb
2016/06/29 05:44:37
Tried this but there are a few minor differences b
tapted
2016/06/29 07:06:14
Acknowledged.
|
| + |
| + InsertTextInternal(transposed_text, false); |
| + return true; |
| +} |
|
tapted
2016/06/28 04:52:32
do you need to do anything special to get `undo` t
karandeepb
2016/06/29 05:44:37
Undo behavior is not completely correct currently,
tapted
2016/06/29 07:06:14
Acknowledged.
|
| + |
| bool TextfieldModel::HasSelection() const { |
| return !render_text_->selection().is_empty(); |
| } |