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

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

Issue 2413223003: Views:: Make Labels support text selection. (Closed)
Patch Set: Rebase Created 4 years, 1 month 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
« no previous file with comments | « ui/views/examples/label_example.cc ('k') | ui/views/selection_controller_delegate.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/views/selection_controller.h" 5 #include "ui/views/selection_controller.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "ui/events/event.h" 9 #include "ui/events/event.h"
10 #include "ui/gfx/render_text.h" 10 #include "ui/gfx/render_text.h"
11 #include "ui/views/metrics.h" 11 #include "ui/views/metrics.h"
12 #include "ui/views/selection_controller_delegate.h" 12 #include "ui/views/selection_controller_delegate.h"
13 #include "ui/views/style/platform_style.h" 13 #include "ui/views/style/platform_style.h"
14 #include "ui/views/view.h" 14 #include "ui/views/view.h"
15 15
16 namespace views { 16 namespace views {
17 17
18 SelectionController::SelectionController(SelectionControllerDelegate* delegate) 18 SelectionController::SelectionController(SelectionControllerDelegate* delegate)
19 : aggregated_clicks_(0), 19 : aggregated_clicks_(0),
20 delegate_(delegate), 20 delegate_(delegate),
21 handles_selection_clipboard_(false) { 21 handles_selection_clipboard_(false) {
22 // On Linux, update the selection clipboard on a text selection.
23 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
24 set_handles_selection_clipboard(true);
25 #endif
26
22 DCHECK(delegate); 27 DCHECK(delegate);
23 } 28 }
24 29
25 bool SelectionController::OnMousePressed(const ui::MouseEvent& event, 30 bool SelectionController::OnMousePressed(const ui::MouseEvent& event,
26 bool handled) { 31 bool handled) {
27 gfx::RenderText* render_text = GetRenderText(); 32 gfx::RenderText* render_text = GetRenderText();
28 DCHECK(render_text); 33 DCHECK(render_text);
29 34
30 TrackMouseClicks(event); 35 TrackMouseClicks(event);
31 if (handled) 36 if (handled)
32 return true; 37 return true;
33 38
34 if (event.IsOnlyLeftMouseButton()) { 39 if (event.IsOnlyLeftMouseButton()) {
35 delegate_->SetTextBeingDragged(false); 40 if (delegate_->SupportsDrag())
41 delegate_->SetTextBeingDragged(false);
42
36 switch (aggregated_clicks_) { 43 switch (aggregated_clicks_) {
37 case 0: 44 case 0:
38 // If the click location is within an existing selection, it may be a 45 // If the click location is within an existing selection, it may be a
39 // potential drag and drop. 46 // potential drag and drop.
40 if (render_text->IsPointInSelection(event.location())) { 47 if (delegate_->SupportsDrag() &&
48 render_text->IsPointInSelection(event.location())) {
41 delegate_->SetTextBeingDragged(true); 49 delegate_->SetTextBeingDragged(true);
42 } else { 50 } else {
43 delegate_->OnBeforePointerAction(); 51 delegate_->OnBeforePointerAction();
44 const bool selection_changed = 52 const bool selection_changed =
45 render_text->MoveCursorTo(event.location(), event.IsShiftDown()); 53 render_text->MoveCursorTo(event.location(), event.IsShiftDown());
46 delegate_->OnAfterPointerAction(false, selection_changed); 54 delegate_->OnAfterPointerAction(false, selection_changed);
47 } 55 }
48 break; 56 break;
49 case 1: 57 case 1:
50 // Select the word at the click location on a double click. 58 // Select the word at the click location on a double click.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 128
121 drag_selection_timer_.Stop(); 129 drag_selection_timer_.Stop();
122 130
123 // Cancel suspected drag initiations, the user was clicking in the selection. 131 // Cancel suspected drag initiations, the user was clicking in the selection.
124 if (delegate_->HasTextBeingDragged()) { 132 if (delegate_->HasTextBeingDragged()) {
125 delegate_->OnBeforePointerAction(); 133 delegate_->OnBeforePointerAction();
126 const bool selection_changed = 134 const bool selection_changed =
127 render_text->MoveCursorTo(event.location(), false); 135 render_text->MoveCursorTo(event.location(), false);
128 delegate_->OnAfterPointerAction(false, selection_changed); 136 delegate_->OnAfterPointerAction(false, selection_changed);
129 } 137 }
130 delegate_->SetTextBeingDragged(false); 138
139 if (delegate_->SupportsDrag())
140 delegate_->SetTextBeingDragged(false);
131 141
132 if (handles_selection_clipboard_ && !render_text->selection().is_empty()) 142 if (handles_selection_clipboard_ && !render_text->selection().is_empty())
133 delegate_->UpdateSelectionClipboard(); 143 delegate_->UpdateSelectionClipboard();
134 } 144 }
135 145
136 void SelectionController::OnMouseCaptureLost() { 146 void SelectionController::OnMouseCaptureLost() {
137 gfx::RenderText* render_text = GetRenderText(); 147 gfx::RenderText* render_text = GetRenderText();
138 DCHECK(render_text); 148 DCHECK(render_text);
139 149
140 drag_selection_timer_.Stop(); 150 drag_selection_timer_.Stop();
(...skipping 24 matching lines...) Expand all
165 return delegate_->GetRenderTextForSelectionController(); 175 return delegate_->GetRenderTextForSelectionController();
166 } 176 }
167 177
168 void SelectionController::SelectThroughLastDragLocation() { 178 void SelectionController::SelectThroughLastDragLocation() {
169 gfx::RenderText* render_text = GetRenderText(); 179 gfx::RenderText* render_text = GetRenderText();
170 DCHECK(render_text); 180 DCHECK(render_text);
171 181
172 delegate_->OnBeforePointerAction(); 182 delegate_->OnBeforePointerAction();
173 183
174 // TODO(karandeepb): See if this can be handled at the RenderText level. 184 // TODO(karandeepb): See if this can be handled at the RenderText level.
175 const bool drags_to_end = PlatformStyle::kTextfieldDragVerticallyDragsToEnd; 185 const bool drags_to_end = PlatformStyle::kTextDragVerticallyDragsToEnd;
176 if (drags_to_end && last_drag_location_.y() < 0) { 186 if (drags_to_end && last_drag_location_.y() < 0) {
177 render_text->MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_LEFT, 187 render_text->MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_LEFT,
178 gfx::SELECTION_RETAIN); 188 gfx::SELECTION_RETAIN);
179 } else if (drags_to_end && 189 } else if (drags_to_end &&
180 last_drag_location_.y() > delegate_->GetViewHeight()) { 190 last_drag_location_.y() > delegate_->GetViewHeight()) {
181 render_text->MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_RIGHT, 191 render_text->MoveCursor(gfx::LINE_BREAK, gfx::CURSOR_RIGHT,
182 gfx::SELECTION_RETAIN); 192 gfx::SELECTION_RETAIN);
183 } else { 193 } else {
184 render_text->MoveCursorTo(last_drag_location_, true); 194 render_text->MoveCursorTo(last_drag_location_, true);
185 } 195 }
186 196
187 if (aggregated_clicks_ == 1) { 197 if (aggregated_clicks_ == 1) {
188 render_text->SelectWord(); 198 render_text->SelectWord();
189 // Expand the selection so the initially selected word remains selected. 199 // Expand the selection so the initially selected word remains selected.
190 gfx::Range selection = render_text->selection(); 200 gfx::Range selection = render_text->selection();
191 const size_t min = 201 const size_t min =
192 std::min(selection.GetMin(), double_click_word_.GetMin()); 202 std::min(selection.GetMin(), double_click_word_.GetMin());
193 const size_t max = 203 const size_t max =
194 std::max(selection.GetMax(), double_click_word_.GetMax()); 204 std::max(selection.GetMax(), double_click_word_.GetMax());
195 const bool reversed = selection.is_reversed(); 205 const bool reversed = selection.is_reversed();
196 selection.set_start(reversed ? max : min); 206 selection.set_start(reversed ? max : min);
197 selection.set_end(reversed ? min : max); 207 selection.set_end(reversed ? min : max);
198 render_text->SelectRange(selection); 208 render_text->SelectRange(selection);
199 } 209 }
200 delegate_->OnAfterPointerAction(false, true); 210 delegate_->OnAfterPointerAction(false, true);
201 } 211 }
202 212
203 } // namespace views 213 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/examples/label_example.cc ('k') | ui/views/selection_controller_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698