Commit e5e5b5a3 authored by Elly Fong-Jones's avatar Elly Fong-Jones Committed by Commit Bot

macviews: don't select words on right-clicks outside text bounds

This matches the behavior of Cocoa textfields, and also makes it possible to
right-click paste at the end of a textfield.

Bug: 856609
Change-Id: Ic609b2bb78737d379e7e0a57ab3f4c7f16a3cec9
Reviewed-on: https://chromium-review.googlesource.com/1160768
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Reviewed-by: default avatarMichael Wasserman <msw@chromium.org>
Cr-Commit-Position: refs/heads/master@{#580260}
parent 935af327
...@@ -75,7 +75,8 @@ bool SelectionController::OnMousePressed( ...@@ -75,7 +75,8 @@ bool SelectionController::OnMousePressed(
initial_focus_state == InitialFocusStateOnMousePress::UNFOCUSED) { initial_focus_state == InitialFocusStateOnMousePress::UNFOCUSED) {
SelectAll(); SelectAll();
} else if (PlatformStyle::kSelectWordOnRightClick && } else if (PlatformStyle::kSelectWordOnRightClick &&
!render_text->IsPointInSelection(event.location())) { !render_text->IsPointInSelection(event.location()) &&
IsInsideText(event.location())) {
SelectWord(event.location()); SelectWord(event.location());
} }
} }
...@@ -223,4 +224,16 @@ void SelectionController::SelectThroughLastDragLocation() { ...@@ -223,4 +224,16 @@ void SelectionController::SelectThroughLastDragLocation() {
delegate_->OnAfterPointerAction(false, true); delegate_->OnAfterPointerAction(false, true);
} }
bool SelectionController::IsInsideText(const gfx::Point& point) {
gfx::RenderText* render_text = GetRenderText();
std::vector<gfx::Rect> bounds_rects = render_text->GetSubstringBounds(
gfx::Range(0, render_text->text().length()));
for (const auto& bounds : bounds_rects)
if (bounds.Contains(point))
return true;
return false;
}
} // namespace views } // namespace views
...@@ -82,6 +82,9 @@ class VIEWS_EXPORT SelectionController { ...@@ -82,6 +82,9 @@ class VIEWS_EXPORT SelectionController {
// |last_drag_location_|. Can be called asynchronously, through a timer. // |last_drag_location_|. Can be called asynchronously, through a timer.
void SelectThroughLastDragLocation(); void SelectThroughLastDragLocation();
// Returns whether |point| is inside any substring of the text.
bool IsInsideText(const gfx::Point& point);
// A timer and point used to modify the selection when dragging. // A timer and point used to modify the selection when dragging.
base::RepeatingTimer drag_selection_timer_; base::RepeatingTimer drag_selection_timer_;
gfx::Point last_drag_location_; gfx::Point last_drag_location_;
......
...@@ -168,18 +168,22 @@ TEST_F(SelectionControllerTest, RightClickWhenUnfocused) { ...@@ -168,18 +168,22 @@ TEST_F(SelectionControllerTest, RightClickWhenUnfocused) {
EXPECT_EQ("", GetSelectedText()); EXPECT_EQ("", GetSelectedText());
} }
// This tests the current incorrect behavior. When the behavior is fixed this TEST_F(SelectionControllerTest, RightClickSelectsWord) {
// test can be updated.
// TODO(ellyjones): Do that - https://crbug.com/856609
TEST_F(SelectionControllerTest, RightClickPastEndSelectsLastWord) {
SetText("abc def"); SetText("abc def");
RightMouseDown(CenterRight(BoundsOfChar(5)), true);
RightMouseDown(CenterRight(BoundsOfChar(6)), true);
if (PlatformStyle::kSelectWordOnRightClick) if (PlatformStyle::kSelectWordOnRightClick)
EXPECT_EQ("def", GetSelectedText()); EXPECT_EQ("def", GetSelectedText());
else else
EXPECT_EQ("", GetSelectedText()); EXPECT_EQ("", GetSelectedText());
} }
// Regression test for https://crbug.com/856609
TEST_F(SelectionControllerTest, RightClickPastEndDoesntSelectLastWord) {
SetText("abc def");
RightMouseDown(CenterRight(BoundsOfChar(6)), true);
EXPECT_EQ("", GetSelectedText());
}
} // namespace } // namespace
} // namespace views } // namespace views
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment