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

Side by Side Diff: content/browser/renderer_host/input/touch_selection_controller.cc

Issue 335943002: [Android] Composited selection handle rendering (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@input_native_handles_final
Patch Set: Tweaks to dragging and visibility Created 6 years, 5 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 2014 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 "content/browser/renderer_host/input/touch_selection_controller.h"
6
7 #include "base/logging.h"
8 #include "third_party/WebKit/public/web/WebInputEvent.h"
9
10 namespace content {
11
12 TouchSelectionController::TouchSelectionController(
13 TouchSelectionControllerClient* client)
14 : client_(client),
15 start_orientation_(TOUCH_HANDLE_ORIENTATION_UNDEFINED),
16 start_visible_(false),
17 end_orientation_(TOUCH_HANDLE_ORIENTATION_UNDEFINED),
18 end_visible_(false),
19 is_insertion_active_(false),
20 allow_automatic_insertion_activation_(false),
21 is_selection_active_(false),
22 allow_automatic_selection_activation_(false),
23 selection_editable_(false),
24 selection_editable_for_last_update_(false) {
25 DCHECK(client_);
26 HideAndDisallowAutomaticShowing();
27 }
28
29 TouchSelectionController::~TouchSelectionController() {
30 }
31
32 void TouchSelectionController::OnSelectionBoundsChanged(
33 const gfx::RectF& start_rect,
34 TouchHandleOrientation start_orientation,
35 bool start_visible,
36 const gfx::RectF& end_rect,
37 TouchHandleOrientation end_orientation,
38 bool end_visible) {
39 if (!allow_automatic_selection_activation_ &&
40 !allow_automatic_insertion_activation_)
41 return;
42
43 if (start_rect_ == start_rect && end_rect_ == end_rect &&
44 start_orientation_ == start_orientation &&
45 end_orientation_ == end_orientation &&
46 start_visible_ == start_visible && end_visible_ == end_visible &&
47 selection_editable_ == selection_editable_for_last_update_)
48 return;
49
50 start_rect_ = start_rect;
51 start_orientation_ = start_orientation;
52 start_visible_ = start_visible;
53 end_rect_ = end_rect;
54 end_orientation_ = end_orientation;
55 end_visible_ = end_visible;
56 selection_editable_for_last_update_ = selection_editable_;
57
58 const bool is_selection_dragging =
59 is_selection_active_ && (start_selection_handle_->is_dragging() ||
60 end_selection_handle_->is_dragging());
61
62 // It's possible that the bounds temporarily overlap while a selection handle
63 // is being dragged, incorrectly reporting a CENTER orientation.
64 // TODO(jdduke): This safeguard is racy, as it's possible the delayed response
65 // from handle positioning occurs *after* the handle dragging has ceased.
66 // Instead, prevent selection -> insertion transitions without an intervening
67 // action or selection clearing of some sort, crbug.com/392696.
68 if (is_selection_dragging) {
69 if (start_orientation_ == TOUCH_HANDLE_CENTER)
70 start_orientation_ = start_selection_handle_->orientation();
71 if (end_orientation_ == TOUCH_HANDLE_CENTER)
72 end_orientation_ = end_selection_handle_->orientation();
73 }
74
75 const gfx::PointF start = GetStartPosition();
76 const gfx::PointF end = GetEndPosition();
77 if (start != end ||
78 (is_selection_dragging &&
79 start_orientation_ != TOUCH_HANDLE_ORIENTATION_UNDEFINED &&
80 end_orientation_ != TOUCH_HANDLE_ORIENTATION_UNDEFINED)) {
81 OnSelectionChanged();
82 return;
83 }
84
85 if (start_orientation_ == TOUCH_HANDLE_CENTER) {
86 OnInsertionChanged();
87 return;
88 }
89
90 HideAndDisallowAutomaticShowing();
91 }
92
93 bool TouchSelectionController::WillHandleTouchEvent(
94 const ui::MotionEvent& event) {
95 if (is_insertion_active_) {
96 DCHECK(insertion_handle_);
97 return insertion_handle_->WillHandleTouchEvent(event);
98 }
99
100 if (is_selection_active_) {
101 DCHECK(start_selection_handle_);
102 DCHECK(end_selection_handle_);
103 return start_selection_handle_->WillHandleTouchEvent(event) ||
104 end_selection_handle_->WillHandleTouchEvent(event);
105 }
106
107 return false;
108 }
109
110 void TouchSelectionController::AllowAutomaticInsertionShowing() {
111 if (allow_automatic_insertion_activation_)
112 return;
113 allow_automatic_insertion_activation_ = true;
114 if (!is_insertion_active_ && !is_selection_active_)
115 ResetCachedValues();
116 }
117
118 void TouchSelectionController::AllowAutomaticSelectionShowing() {
119 if (allow_automatic_selection_activation_)
120 return;
121 allow_automatic_selection_activation_ = true;
122 if (!is_insertion_active_ && !is_selection_active_)
123 ResetCachedValues();
124 }
125
126 void TouchSelectionController::HideAndDisallowAutomaticShowing() {
127 DeactivateInsertion();
128 DeactivateSelection();
129 allow_automatic_insertion_activation_ = false;
130 allow_automatic_selection_activation_ = false;
131 }
132
133 void TouchSelectionController::OnSelectionEditable(bool editable) {
134 if (selection_editable_ == editable)
135 return;
136 selection_editable_ = editable;
137 if (!selection_editable_)
138 DeactivateInsertion();
139 }
140
141 bool TouchSelectionController::Animate(base::TimeTicks frame_time) {
142 if (is_insertion_active_)
143 return insertion_handle_->Animate(frame_time);
144
145 if (is_selection_active_) {
146 bool needs_animate = start_selection_handle_->Animate(frame_time);
147 needs_animate |= end_selection_handle_->Animate(frame_time);
148 return needs_animate;
149 }
150
151 return false;
152 }
153
154 void TouchSelectionController::OnHandleDragBegin(const TouchHandle& handle) {
155 if (&handle == insertion_handle_.get())
156 return;
157
158 if (&handle == start_selection_handle_.get()) {
159 fixed_handle_position_ = end_selection_handle_->position() -
160 gfx::Vector2dF(0, GetEndLineHeight() / 2.f);
161 } else {
162 fixed_handle_position_ = start_selection_handle_->position() -
163 gfx::Vector2dF(0, GetStartLineHeight() / 2.f);
164 }
165 }
166
167 void TouchSelectionController::OnHandleDragUpdate(const TouchHandle& handle,
168 const gfx::PointF& position) {
169 // As the position corresponds to the bottom left point of the selection
170 // bound, offset it by half the corresponding line height.
171 float half_line_height = &handle == end_selection_handle_.get()
172 ? GetEndLineHeight() / 2.f
173 : GetStartLineHeight() / 2.f;
174 gfx::PointF line_position = position - gfx::Vector2dF(0, half_line_height);
175 if (&handle == insertion_handle_.get()) {
176 client_->MoveCaret(line_position);
177 } else {
178 client_->SelectBetweenCoordinates(fixed_handle_position_, line_position);
179 }
180 }
181
182 void TouchSelectionController::OnHandleDragEnd(const TouchHandle& handle) {
183 }
184
185 void TouchSelectionController::OnHandleTapped(const TouchHandle& handle) {
186 if (insertion_handle_ && &handle == insertion_handle_.get())
187 client_->OnSelectionEvent(INSERTION_TAPPED, GetStartPosition());
188 }
189
190 void TouchSelectionController::SetNeedsAnimate() {
191 client_->SetNeedsAnimate();
192 }
193
194 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() {
195 return client_->CreateDrawable();
196 }
197
198 void TouchSelectionController::OnInsertionChanged() {
199 DeactivateSelection();
200
201 if (!allow_automatic_insertion_activation_ || !selection_editable_)
202 return;
203
204 const bool was_active = is_insertion_active_;
205 const gfx::PointF position = GetStartPosition();
206 if (!is_insertion_active_)
207 ActivateInsertion();
208 else
209 client_->OnSelectionEvent(INSERTION_MOVED, position);
210
211 insertion_handle_->SetVisible(start_visible_, GetAnimationStyle(was_active));
212 insertion_handle_->SetPosition(position);
213 }
214
215 void TouchSelectionController::OnSelectionChanged() {
216 DeactivateInsertion();
217
218 if (!allow_automatic_selection_activation_)
219 return;
220
221 const bool was_active = is_selection_active_;
222 ActivateSelection();
223
224 const TouchHandle::AnimationStyle animation = GetAnimationStyle(was_active);
225 start_selection_handle_->SetVisible(start_visible_, animation);
226 end_selection_handle_->SetVisible(end_visible_, animation);
227
228 start_selection_handle_->SetPosition(GetStartPosition());
229 end_selection_handle_->SetPosition(GetEndPosition());
230 }
231
232 void TouchSelectionController::ActivateInsertion() {
233 DCHECK(!is_selection_active_);
234
235 if (!insertion_handle_)
236 insertion_handle_.reset(new TouchHandle(this, TOUCH_HANDLE_CENTER));
237
238 if (!is_insertion_active_) {
239 is_insertion_active_ = true;
240 insertion_handle_->SetEnabled(true);
241 client_->OnSelectionEvent(INSERTION_SHOWN, GetStartPosition());
242 }
243 }
244
245 void TouchSelectionController::DeactivateInsertion() {
246 if (!is_insertion_active_)
247 return;
248 DCHECK(insertion_handle_);
249 is_insertion_active_ = false;
250 insertion_handle_->SetEnabled(false);
251 client_->OnSelectionEvent(INSERTION_CLEARED, gfx::PointF());
252 }
253
254 void TouchSelectionController::ActivateSelection() {
255 DCHECK(!is_insertion_active_);
256
257 if (!start_selection_handle_)
258 start_selection_handle_.reset(new TouchHandle(this, start_orientation_));
259 else
260 start_selection_handle_->SetOrientation(start_orientation_);
261
262 if (!end_selection_handle_)
263 end_selection_handle_.reset(new TouchHandle(this, end_orientation_));
264 else
265 end_selection_handle_->SetOrientation(end_orientation_);
266
267 if (!is_selection_active_) {
268 is_selection_active_ = true;
269 start_selection_handle_->SetEnabled(true);
270 end_selection_handle_->SetEnabled(true);
271 client_->OnSelectionEvent(SELECTION_SHOWN, GetStartPosition());
272 }
273 }
274
275 void TouchSelectionController::DeactivateSelection() {
276 if (!is_selection_active_)
277 return;
278 DCHECK(start_selection_handle_);
279 DCHECK(end_selection_handle_);
280 start_selection_handle_->SetEnabled(false);
281 end_selection_handle_->SetEnabled(false);
282 is_selection_active_ = false;
283 client_->OnSelectionEvent(SELECTION_CLEARED, gfx::PointF());
284 }
285
286 void TouchSelectionController::ResetCachedValues() {
287 start_rect_ = gfx::RectF();
288 end_rect_ = gfx::RectF();
289 start_orientation_ = TOUCH_HANDLE_ORIENTATION_UNDEFINED;
290 end_orientation_ = TOUCH_HANDLE_ORIENTATION_UNDEFINED;
291 start_visible_ = false;
292 end_visible_ = false;
293 selection_editable_for_last_update_ = false;
294 }
295
296 gfx::PointF TouchSelectionController::GetStartPosition() const {
297 return start_rect_.bottom_left();
298 }
299
300 gfx::PointF TouchSelectionController::GetEndPosition() const {
301 return end_rect_.bottom_left();
302 }
303
304 float TouchSelectionController::GetStartLineHeight() const {
305 return start_rect_.height();
306 }
307
308 float TouchSelectionController::GetEndLineHeight() const {
309 return end_rect_.height();
310 }
311
312 TouchHandle::AnimationStyle
313 TouchSelectionController::GetAnimationStyle(bool was_active) const {
314 return was_active && client_->SupportsAnimation()
315 ? TouchHandle::ANIMATION_SMOOTH
316 : TouchHandle::ANIMATION_NONE;
317 }
318
319 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698