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

Side by Side Diff: ui/views/selection_controller.cc

Issue 2408623002: Views: Extract text selection code from Textfield. (Closed)
Patch Set: Removed On[Before|After]SelectionUpdated. Created 4 years, 2 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
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ui/views/selection_controller.h"
6
7 #include <algorithm>
8
9 #include "ui/events/event.h"
10 #include "ui/gfx/render_text.h"
11 #include "ui/views/metrics.h"
12 #include "ui/views/selection_controller_delegate.h"
13 #include "ui/views/style/platform_style.h"
14 #include "ui/views/view.h"
15
16 namespace views {
17
18 SelectionController::SelectionController(SelectionControllerDelegate* delegate)
19 : aggregated_clicks_(0), delegate_(delegate) {
20 DCHECK(delegate);
21 }
22
23 bool SelectionController::OnMousePressed(const ui::MouseEvent& event,
24 bool handled) {
25 gfx::RenderText* render_text = GetRenderText();
26 DCHECK(render_text);
27
28 TrackMouseClicks(event);
29 if (handled)
30 return true;
31
32 if (event.IsOnlyLeftMouseButton()) {
33 delegate_->SetTextBeingDragged(false);
34 switch (aggregated_clicks_) {
35 case 0:
36 // If the click location is within existing selection, it may be a
37 // potential drag and drop.
38 if (render_text->IsPointInSelection(event.location())) {
39 delegate_->SetTextBeingDragged(true);
40 } else {
41 delegate_->OnBeforeMouseAction();
42 render_text->MoveCursorTo(event.location(), event.IsShiftDown());
43 delegate_->OnAfterMouseAction(false, true);
44 }
45 break;
46 case 1:
47 // Select the word at the click location on a double click.
48 delegate_->OnBeforeMouseAction();
49 render_text->MoveCursorTo(event.location(), false);
50 render_text->SelectWord();
51 delegate_->OnAfterMouseAction(false, true);
52 double_click_word_ = render_text->selection();
53 break;
54 case 2:
55 // Select all the text on a triple click.
56 delegate_->OnBeforeMouseAction();
57 render_text->SelectAll(false);
58 delegate_->OnAfterMouseAction(false, true);
59 break;
60 default:
61 NOTREACHED();
62 }
63 }
64
65 // On Linux, middle click should update or paste the selection clipboard.
66 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
67 if (event.IsOnlyMiddleMouseButton()) {
68 if (render_text->IsPointInSelection(event.location())) {
69 delegate_->OnBeforeMouseAction();
70 ClearSelection();
71 delegate_->OnAfterMouseAction(false, true);
72 delegate_->UpdateSelectionClipboard();
73 } else if (!delegate_->IsReadOnly()) {
74 delegate_->OnBeforeMouseAction();
75 delegate_->PasteSelectionClipboard(event);
76 delegate_->OnAfterMouseAction(true, true);
77 }
78 }
79 #endif
80
81 return true;
82 }
83
84 bool SelectionController::OnMouseDragged(const ui::MouseEvent& event) {
85 DCHECK(GetRenderText());
86 // If |drag_selection_timer_| is running, |last_drag_location_| will be used
87 // to update the selection.
88 last_drag_location_ = event.location();
89
90 // Don't adjust the cursor on a potential drag and drop.
91 if (delegate_->HasTextBeingDragged() || !event.IsOnlyLeftMouseButton())
92 return true;
93
94 // A timer is used to continuously scroll while selecting beyond side edges.
95 const int x = event.location().x();
96 const int width = delegate_->GetViewWidth();
97 if ((x >= 0 && x <= width) || delegate_->GetDragSelectionDelay() == 0) {
98 drag_selection_timer_.Stop();
99 SelectThroughLastDragLocation();
100 } else if (!drag_selection_timer_.IsRunning()) {
101 // Select through the edge of the visible text, then start the scroll timer.
102 last_drag_location_.set_x(std::min(std::max(0, x), width));
103 SelectThroughLastDragLocation();
104
105 drag_selection_timer_.Start(
106 FROM_HERE,
107 base::TimeDelta::FromMilliseconds(delegate_->GetDragSelectionDelay()),
108 this, &SelectionController::SelectThroughLastDragLocation);
109 }
110
111 return true;
112 }
113
114 void SelectionController::OnMouseReleased(const ui::MouseEvent& event) {
115 gfx::RenderText* render_text = GetRenderText();
116 DCHECK(render_text);
117
118 drag_selection_timer_.Stop();
119
120 // Cancel suspected drag initiations, the user was clicking in the selection.
121 if (delegate_->HasTextBeingDragged()) {
122 delegate_->OnBeforeMouseAction();
123 render_text->MoveCursorTo(event.location(), false);
124 delegate_->OnAfterMouseAction(false, true);
125 }
126 delegate_->SetTextBeingDragged(false);
127 delegate_->UpdateSelectionClipboard();
128 }
129
130 void SelectionController::TrackMouseClicks(const ui::MouseEvent& event) {
131 if (event.IsOnlyLeftMouseButton()) {
132 base::TimeDelta time_delta = event.time_stamp() - last_click_time_;
133 if (!last_click_time_.is_null() &&
134 time_delta.InMilliseconds() <= GetDoubleClickInterval() &&
135 !View::ExceededDragThreshold(event.location() - last_click_location_)) {
136 // Upon clicking after a triple click, the count should go back to
137 // double click and alternate between double and triple. This assignment
138 // maps 0 to 1, 1 to 2, 2 to 1.
139 aggregated_clicks_ = (aggregated_clicks_ % 2) + 1;
140 } else {
141 aggregated_clicks_ = 0;
142 }
143 last_click_time_ = event.time_stamp();
144 last_click_location_ = event.location();
145 }
146 }
147
148 gfx::RenderText* SelectionController::GetRenderText() {
149 return delegate_->GetRenderTextForSelectionController();
150 }
151
152 void SelectionController::SelectThroughLastDragLocation() {
153 gfx::RenderText* render_text = GetRenderText();
154 DCHECK(render_text);
155
156 delegate_->OnBeforeMouseAction();
157
158 // TODO(karandeepb): See if this can be handled at the RenderText level.
159 const bool drags_to_end = PlatformStyle::kTextfieldDragVerticallyDragsToEnd;
160 if (drags_to_end && last_drag_location_.y() < 0) {
161 render_text->MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_LEFT,
162 gfx::SELECTION_RETAIN);
163 } else if (drags_to_end &&
164 last_drag_location_.y() > delegate_->GetViewHeight()) {
165 render_text->MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_RIGHT,
166 gfx::SELECTION_RETAIN);
167 } else {
168 render_text->MoveCursorTo(last_drag_location_, true);
169 }
170
171 if (aggregated_clicks_ == 1) {
172 render_text->SelectWord();
173 // Expand the selection so the initially selected word remains selected.
174 gfx::Range selection = GetRenderText()->selection();
175 const size_t min =
176 std::min(selection.GetMin(), double_click_word_.GetMin());
177 const size_t max =
178 std::max(selection.GetMax(), double_click_word_.GetMax());
179 const bool reversed = selection.is_reversed();
180 selection.set_start(reversed ? max : min);
181 selection.set_end(reversed ? min : max);
182 render_text->SelectRange(selection);
183 }
184 delegate_->OnAfterMouseAction(false, true);
185 }
186
187 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698