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

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

Issue 2408623002: Views: Extract text selection code from Textfield. (Closed)
Patch Set: Rename [Starting/Ending]MouseAction. 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_host.h"
13 #include "ui/views/style/platform_style.h"
14 #include "ui/views/view.h"
15
16 namespace views {
17
18 SelectionController::SelectionController(SelectionControllerHost* host)
19 : aggregated_clicks_(0), host_(host) {
20 DCHECK(host);
21 }
22
23 bool SelectionController::OnMousePressed(const ui::MouseEvent& event,
24 bool handled) {
25 TrackMouseClicks(event);
26
27 if (!GetRenderText() || handled)
28 return handled;
29
30 if (event.IsOnlyLeftMouseButton()) {
31 host_->OnBeforeMouseAction();
32 bool selection_changed = false;
33 host_->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 (GetRenderText()->IsPointInSelection(event.location())) {
39 host_->SetTextBeingDragged(true);
40 } else {
41 host_->MoveCursorTo(event.location(), event.IsShiftDown());
42 selection_changed = true;
43 }
44 break;
45 case 1:
46 // Select the word at the click location on a double click.
47 host_->SelectWordAt(event.location());
48 selection_changed = true;
49 double_click_word_ = GetRenderText()->selection();
50 break;
51 case 2:
52 // Select all the text on a triple click.
53 host_->SelectAll(false);
54 selection_changed = true;
55 break;
56 default:
57 NOTREACHED();
58 }
59 host_->OnAfterMouseAction(false, selection_changed);
60 }
61
62 // On Linux, middle click should update or paste the selection clipboard.
63 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
64 if (event.IsOnlyMiddleMouseButton()) {
65 host_->OnBeforeMouseAction();
66 bool text_changed = false;
67 bool selection_changed = false;
68 if (GetRenderText()->IsPointInSelection(event.location())) {
69 host_->ClearSelection();
70 host_->UpdateSelectionClipboard();
71 selection_changed = true;
72 } else if (!host_->IsReadOnly()) {
73 host_->MoveCursorTo(event.location(), false);
74 host_->PasteSelectionClipboard();
75 selection_changed = text_changed = true;
76 }
77 host_->OnAfterMouseAction(text_changed, selection_changed);
78 }
79 #endif
80
81 return true;
82 }
83
84 bool SelectionController::OnMouseDragged(const ui::MouseEvent& event) {
85 // If |drag_selection_timer_| is running, |last_drag_location_| will be used
86 // to update the selection.
87 last_drag_location_ = event.location();
88
89 if (!GetRenderText())
90 return false;
91
92 // Don't adjust the cursor on a potential drag and drop.
93 if (host_->HasTextBeingDragged() || !event.IsOnlyLeftMouseButton())
94 return true;
95
96 // A timer is used to continuously scroll while selecting beyond side edges.
97 const int x = event.location().x();
98 if ((x >= 0 && x <= host_->GetViewWidth()) ||
99 host_->GetDragSelectionDelay() == 0) {
100 drag_selection_timer_.Stop();
101 SelectThroughLastDragLocation();
102 } else if (!drag_selection_timer_.IsRunning()) {
103 // Select through the edge of the visible text, then start the scroll timer.
104 last_drag_location_.set_x(std::min(std::max(0, x), host_->GetViewWidth()));
105 SelectThroughLastDragLocation();
106
107 // Todo what happens when this object is destroyed before timer is invoked
108 // but it's scheduled?
109 drag_selection_timer_.Start(
110 FROM_HERE,
111 base::TimeDelta::FromMilliseconds(host_->GetDragSelectionDelay()), this,
112 &SelectionController::SelectThroughLastDragLocation);
113 }
114
115 return true;
116 }
117
118 void SelectionController::OnMouseReleased(const ui::MouseEvent& event) {
119 host_->OnBeforeMouseAction();
120 drag_selection_timer_.Stop();
121
122 bool selection_changed = false;
123 // Cancel suspected drag initiations, the user was clicking in the selection.
124 if (host_->HasTextBeingDragged()) {
125 selection_changed = true;
126 host_->MoveCursorTo(event.location(), false);
127 }
128 host_->SetTextBeingDragged(false);
129 host_->UpdateSelectionClipboard();
130 host_->OnAfterMouseAction(false, selection_changed);
131 }
132
133 void SelectionController::TrackMouseClicks(const ui::MouseEvent& event) {
134 if (event.IsOnlyLeftMouseButton()) {
135 base::TimeDelta time_delta = event.time_stamp() - last_click_time_;
136 if (!last_click_time_.is_null() &&
137 time_delta.InMilliseconds() <= GetDoubleClickInterval() &&
138 !View::ExceededDragThreshold(event.location() - last_click_location_)) {
139 // Upon clicking after a triple click, the count should go back to
140 // double click and alternate between double and triple. This assignment
141 // maps 0 to 1, 1 to 2, 2 to 1.
142 aggregated_clicks_ = (aggregated_clicks_ % 2) + 1;
143 } else {
144 aggregated_clicks_ = 0;
145 }
146 last_click_time_ = event.time_stamp();
147 last_click_location_ = event.location();
148 }
149 }
150
151 gfx::RenderText* SelectionController::GetRenderText() {
152 return host_->GetRenderTextForSelection();
153 }
154
155 void SelectionController::SelectThroughLastDragLocation() {
156 host_->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 host_->SelectTillEdge(gfx::CURSOR_LEFT);
162 else if (drags_to_end && last_drag_location_.y() > host_->GetViewHeight())
163 host_->SelectTillEdge(gfx::CURSOR_RIGHT);
164 else
165 host_->MoveCursorTo(last_drag_location_, true);
166
167 if (aggregated_clicks_ == 1) {
168 host_->SelectWord();
169
170 // Expand the selection so the initially selected word remains selected.
171 gfx::Range selection = GetRenderText()->selection();
172 const size_t min =
173 std::min(selection.GetMin(), double_click_word_.GetMin());
174 const size_t max =
175 std::max(selection.GetMax(), double_click_word_.GetMax());
176 const bool reversed = selection.is_reversed();
177 selection.set_start(reversed ? max : min);
178 selection.set_end(reversed ? min : max);
179 host_->SelectRange(selection);
180 }
181 host_->OnAfterMouseAction(false, true);
182 }
183
184 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698