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

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

Issue 9390022: Simplify handling of BiDi cursor movement (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 8 years, 9 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_views_model.cc
diff --git a/ui/views/controls/textfield/textfield_views_model.cc b/ui/views/controls/textfield/textfield_views_model.cc
index a2f84397759388219b40f20ff5c900975b20a36b..069013b1a2193f64582eb5351821205bb132f251 100644
--- a/ui/views/controls/textfield/textfield_views_model.cc
+++ b/ui/views/controls/textfield/textfield_views_model.cc
@@ -346,7 +346,8 @@ bool TextfieldViewsModel::Delete() {
size_t cursor_position = GetCursorPosition();
size_t next_grapheme_index = render_text_->IndexOfAdjacentGrapheme(
cursor_position, gfx::CURSOR_FORWARD);
- ExecuteAndRecordDelete(cursor_position, next_grapheme_index, true);
+ ExecuteAndRecordDelete(ui::Range(cursor_position, next_grapheme_index),
+ true);
return true;
}
return false;
@@ -367,14 +368,14 @@ bool TextfieldViewsModel::Backspace() {
// Delete one code point, which may be two UTF-16 words.
size_t previous_char =
ui::UTF16OffsetToIndex(GetText(), cursor_position, -1);
- ExecuteAndRecordDelete(cursor_position, previous_char, true);
+ ExecuteAndRecordDelete(ui::Range(cursor_position, previous_char), true);
return true;
}
return false;
}
size_t TextfieldViewsModel::GetCursorPosition() const {
- return render_text_->GetCursorPosition();
+ return render_text_->cursor_position();
}
void TextfieldViewsModel::MoveCursor(gfx::BreakType break_type,
@@ -385,20 +386,18 @@ void TextfieldViewsModel::MoveCursor(gfx::BreakType break_type,
render_text_->MoveCursor(break_type, direction, select);
}
-bool TextfieldViewsModel::MoveCursorTo(const gfx::SelectionModel& selection) {
+bool TextfieldViewsModel::MoveCursorTo(const gfx::SelectionModel& model) {
if (HasCompositionText()) {
ConfirmCompositionText();
// ConfirmCompositionText() updates cursor position. Need to reflect it in
// the SelectionModel parameter of MoveCursorTo().
- if (render_text_->GetSelectionStart() != selection.selection_end())
- return render_text_->SelectRange(ui::Range(
- render_text_->GetSelectionStart(), selection.selection_end()));
- gfx::SelectionModel sel(selection.selection_end(),
- selection.caret_pos(),
- selection.caret_placement());
- return render_text_->MoveCursorTo(sel);
+ ui::Range range(render_text_->selection().start(), model.caret_pos());
+ if (!range.is_empty())
+ return render_text_->SelectRange(range);
+ return render_text_->MoveCursorTo(
+ gfx::SelectionModel(model.caret_pos(), model.caret_affinity()));
}
- return render_text_->MoveCursorTo(selection);
+ return render_text_->MoveCursorTo(model);
}
bool TextfieldViewsModel::MoveCursorTo(const gfx::Point& point, bool select) {
@@ -408,13 +407,8 @@ bool TextfieldViewsModel::MoveCursorTo(const gfx::Point& point, bool select) {
}
string16 TextfieldViewsModel::GetSelectedText() const {
- return GetText().substr(render_text_->MinOfSelection(),
- (render_text_->MaxOfSelection() - render_text_->MinOfSelection()));
-}
-
-void TextfieldViewsModel::GetSelectedRange(ui::Range* range) const {
- range->set_start(render_text_->GetSelectionStart());
- range->set_end(render_text_->GetCursorPosition());
+ return GetText().substr(render_text_->selection().GetMin(),
+ render_text_->selection().length());
}
void TextfieldViewsModel::SelectRange(const ui::Range& range) {
@@ -511,8 +505,8 @@ bool TextfieldViewsModel::Cut() {
// than beginning, unlike Delete/Backspace.
// TODO(oshima): Change Delete/Backspace to use DeleteSelection,
// update DeleteEdit and remove this trick.
- render_text_->SelectRange(ui::Range(render_text_->GetCursorPosition(),
- render_text_->GetSelectionStart()));
+ const ui::Range& selection = render_text_->selection();
+ render_text_->SelectRange(ui::Range(selection.end(), selection.start()));
DeleteSelection();
return true;
}
@@ -541,14 +535,13 @@ bool TextfieldViewsModel::Paste() {
}
bool TextfieldViewsModel::HasSelection() const {
- return !render_text_->EmptySelection();
+ return !render_text_->selection().is_empty();
}
void TextfieldViewsModel::DeleteSelection() {
DCHECK(!HasCompositionText());
DCHECK(HasSelection());
- ExecuteAndRecordDelete(render_text_->GetSelectionStart(),
- render_text_->GetCursorPosition(), false);
+ ExecuteAndRecordDelete(render_text_->selection(), false);
}
void TextfieldViewsModel::DeleteSelectionAndInsertTextAt(
@@ -663,10 +656,10 @@ void TextfieldViewsModel::ReplaceTextInternal(const string16& text,
// with |text|. So, need to find the index of next grapheme first.
size_t next =
render_text_->IndexOfAdjacentGrapheme(cursor, gfx::CURSOR_FORWARD);
- if (next == model.selection_end())
+ if (next == model.caret_pos())
render_text_->MoveCursorTo(model);
else
- render_text_->SelectRange(ui::Range(next, model.selection_end()));
+ render_text_->SelectRange(ui::Range(next, model.caret_pos()));
}
// Edit history is recorded in InsertText.
InsertTextInternal(text, mergeable);
@@ -692,13 +685,11 @@ void TextfieldViewsModel::ClearRedoHistory() {
edit_history_.erase(delete_start, edit_history_.end());
}
-void TextfieldViewsModel::ExecuteAndRecordDelete(size_t from,
- size_t to,
+void TextfieldViewsModel::ExecuteAndRecordDelete(ui::Range range,
bool mergeable) {
- size_t old_text_start = std::min(from, to);
- const string16 text = GetText().substr(old_text_start,
- std::abs(static_cast<long>(from - to)));
- bool backward = from > to;
+ size_t old_text_start = range.GetMin();
+ const string16 text = GetText().substr(old_text_start, range.length());
+ bool backward = range.is_reversed();
Edit* edit = new DeleteEdit(mergeable, text, old_text_start, backward);
bool delete_edit = AddOrMergeEditHistory(edit);
edit->Redo(this);
@@ -708,7 +699,7 @@ void TextfieldViewsModel::ExecuteAndRecordDelete(size_t from,
void TextfieldViewsModel::ExecuteAndRecordReplaceSelection(
MergeType merge_type, const string16& new_text) {
- size_t new_text_start = render_text_->MinOfSelection();
+ size_t new_text_start = render_text_->selection().GetMin();
size_t new_cursor_pos = new_text_start + new_text.length();
ExecuteAndRecordReplace(merge_type,
GetCursorPosition(),
@@ -722,9 +713,8 @@ void TextfieldViewsModel::ExecuteAndRecordReplace(MergeType merge_type,
size_t new_cursor_pos,
const string16& new_text,
size_t new_text_start) {
- size_t old_text_start = render_text_->MinOfSelection();
- bool backward =
- render_text_->GetSelectionStart() > render_text_->GetCursorPosition();
+ size_t old_text_start = render_text_->selection().GetMin();
+ bool backward = render_text_->selection().is_reversed();
Edit* edit = new ReplaceEdit(merge_type,
GetSelectedText(),
old_cursor_pos,

Powered by Google App Engine
This is Rietveld 408576698