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

Side by Side 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, 5 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/controls/textfield/textfield_model.h" 5 #include "ui/views/controls/textfield/textfield_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 520
521 base::string16 actual_text = base::CollapseWhitespace(text, false); 521 base::string16 actual_text = base::CollapseWhitespace(text, false);
522 // If the clipboard contains all whitespaces then paste a single space. 522 // If the clipboard contains all whitespaces then paste a single space.
523 if (actual_text.empty()) 523 if (actual_text.empty())
524 actual_text = base::ASCIIToUTF16(" "); 524 actual_text = base::ASCIIToUTF16(" ");
525 525
526 InsertTextInternal(actual_text, false); 526 InsertTextInternal(actual_text, false);
527 return true; 527 return true;
528 } 528 }
529 529
530 bool TextfieldModel::Transpose() {
531 if (HasCompositionText() || HasSelection())
532 return false;
533
534 size_t cur = GetCursorPosition();
535 size_t next = render_text_->IndexOfAdjacentGrapheme(cur, gfx::CURSOR_FORWARD);
536 size_t prev =
537 render_text_->IndexOfAdjacentGrapheme(cur, gfx::CURSOR_BACKWARD);
538
539 // At the end of the line, the last two characters should be transposed.
540 if (cur == text().length()) {
541 DCHECK_EQ(cur, next);
542 cur = prev;
543 prev = render_text_->IndexOfAdjacentGrapheme(prev, gfx::CURSOR_BACKWARD);
544 }
545
546 // This happens at the beginning of the line or when the line has less than
547 // two graphemes.
548 if (gfx::UTF16IndexToOffset(text(), prev, next) != 2)
549 return false;
550
551 SelectRange(gfx::Range(prev, next));
552 base::string16 text = GetSelectedText();
553 base::string16 transposed_text =
554 text.substr(cur - prev) + text.substr(0, cur - prev);
555
556 InsertTextInternal(transposed_text, false);
557 return true;
558 }
559
530 bool TextfieldModel::HasSelection() const { 560 bool TextfieldModel::HasSelection() const {
531 return !render_text_->selection().is_empty(); 561 return !render_text_->selection().is_empty();
532 } 562 }
533 563
534 void TextfieldModel::DeleteSelection() { 564 void TextfieldModel::DeleteSelection() {
535 DCHECK(!HasCompositionText()); 565 DCHECK(!HasCompositionText());
536 DCHECK(HasSelection()); 566 DCHECK(HasSelection());
537 ExecuteAndRecordDelete(render_text_->selection(), false); 567 ExecuteAndRecordDelete(render_text_->selection(), false);
538 } 568 }
539 569
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
774 ClearComposition(); 804 ClearComposition();
775 if (delete_from != delete_to) 805 if (delete_from != delete_to)
776 render_text_->SetText(old_text.erase(delete_from, delete_to - delete_from)); 806 render_text_->SetText(old_text.erase(delete_from, delete_to - delete_from));
777 if (!new_text.empty()) 807 if (!new_text.empty())
778 render_text_->SetText(old_text.insert(new_text_insert_at, new_text)); 808 render_text_->SetText(old_text.insert(new_text_insert_at, new_text));
779 render_text_->SetCursorPosition(new_cursor_pos); 809 render_text_->SetCursorPosition(new_cursor_pos);
780 // TODO(oshima): Select text that was just undone, like Mac (but not GTK). 810 // TODO(oshima): Select text that was just undone, like Mac (but not GTK).
781 } 811 }
782 812
783 } // namespace views 813 } // namespace views
OLDNEW
« 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