Skip to content

Commit

Permalink
Views UI: Improve middle-click pasting onto selected text behavior.
Browse files Browse the repository at this point in the history
Previously, there was special-case code for middle-click pasting onto
selected text. This special-case code existed to try to emulate the
middle-click paste behavior of gedit.

However, gedit has some confusing behaviors. This CL modifies
middle-click pasting in the following ways:

 1. Middle-click pasting onto a selection now pastes the contents of
    the PRIMARY (selection) clipboard at the cursor position.

 2. The PRIMARY (selection) clipboard is no longer cleared after
    middle-clicking.

 3. This makes middle-click on UI surfaces like the Omnibox behave
    the same way as HTML text fields, which is a bonus.

BUG=364676

Review-Url: https://codereview.chromium.org/2826843002
Cr-Commit-Position: refs/heads/master@{#465775}
  • Loading branch information
tommycli authored and Commit bot committed Apr 19, 2017
1 parent 0bcba70 commit 54ba8b4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
10 changes: 6 additions & 4 deletions ui/views/controls/textfield/textfield_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2797,13 +2797,15 @@ TEST_F(TextfieldTest, SelectionClipboard) {
EXPECT_EQ(gfx::Range(4, 4), textfield_->GetSelectedRange());
EXPECT_TRUE(GetClipboardText(ui::CLIPBOARD_TYPE_SELECTION).empty());

// Middle clicking in the selection should clear the clipboard and selection.
// Middle clicking in the selection should insert the selection clipboard
// contents into the middle of the selection, and move the cursor to the end
// of the pasted content.
SetClipboardText(ui::CLIPBOARD_TYPE_COPY_PASTE, "foo");
textfield_->SelectRange(gfx::Range(2, 6));
textfield_->OnMousePressed(middle);
EXPECT_STR_EQ("012301230123", textfield_->text());
EXPECT_EQ(gfx::Range(6, 6), textfield_->GetSelectedRange());
EXPECT_TRUE(GetClipboardText(ui::CLIPBOARD_TYPE_SELECTION).empty());
EXPECT_STR_EQ("0123foo01230123", textfield_->text());
EXPECT_EQ(gfx::Range(7, 7), textfield_->GetSelectedRange());
EXPECT_STR_EQ("foo", GetClipboardText(ui::CLIPBOARD_TYPE_SELECTION));

// Double and triple clicking should update the clipboard contents.
textfield_->SetText(ASCIIToUTF16("ab cd ef"));
Expand Down
22 changes: 8 additions & 14 deletions ui/views/selection_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,14 @@ bool SelectionController::OnMousePressed(
}
}

if (handles_selection_clipboard_ && event.IsOnlyMiddleMouseButton()) {
if (render_text->IsPointInSelection(event.location())) {
delegate_->OnBeforePointerAction();
render_text->ClearSelection();
delegate_->UpdateSelectionClipboard();
delegate_->OnAfterPointerAction(false, true);
} else if (!delegate_->IsReadOnly()) {
delegate_->OnBeforePointerAction();
const bool selection_changed =
render_text->MoveCursorTo(event.location(), false);
const bool text_changed = delegate_->PasteSelectionClipboard();
delegate_->OnAfterPointerAction(text_changed,
selection_changed | text_changed);
}
if (handles_selection_clipboard_ && event.IsOnlyMiddleMouseButton() &&
!delegate_->IsReadOnly()) {
delegate_->OnBeforePointerAction();
const bool selection_changed =
render_text->MoveCursorTo(event.location(), false);
const bool text_changed = delegate_->PasteSelectionClipboard();
delegate_->OnAfterPointerAction(text_changed,
selection_changed | text_changed);
}

return true;
Expand Down

0 comments on commit 54ba8b4

Please sign in to comment.