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

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: Fix animation tests 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 activate_insertion_automatically_(false),
21 is_selection_active_(false),
22 activate_selection_automatically_(false),
23 selection_editable_(false),
24 selection_editable_for_last_update_(false),
25 temporarily_hidden_(false) {
26 DCHECK(client_);
27 HideAndDisallowAutomaticShowing();
28 }
29
30 TouchSelectionController::~TouchSelectionController() {
31 }
32
33 void TouchSelectionController::OnSelectionBoundsChanged(
34 const gfx::RectF& start_rect,
35 TouchHandleOrientation start_orientation,
36 bool start_visible,
37 const gfx::RectF& end_rect,
38 TouchHandleOrientation end_orientation,
39 bool end_visible) {
40 if (!activate_selection_automatically_ && !activate_insertion_automatically_)
41 return;
42
43 if (start_rect_ == start_rect && end_rect_ == end_rect &&
44 start_orientation_ == start_orientation &&
45 end_orientation_ == end_orientation && start_visible_ == start_visible &&
46 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::ShowInsertionHandleAutomatically() {
111 if (activate_insertion_automatically_)
112 return;
113 activate_insertion_automatically_ = true;
114 if (!is_insertion_active_ && !is_selection_active_)
115 ResetCachedValues();
116 }
117
118 void TouchSelectionController::ShowSelectionHandlesAutomatically() {
119 if (activate_selection_automatically_)
120 return;
121 activate_selection_automatically_ = true;
122 if (!is_insertion_active_ && !is_selection_active_)
123 ResetCachedValues();
124 }
125
126 void TouchSelectionController::HideAndDisallowAutomaticShowing() {
127 DeactivateInsertion();
128 DeactivateSelection();
129 activate_insertion_automatically_ = false;
130 activate_selection_automatically_ = false;
131 }
132
133 void TouchSelectionController::SetTemporarilyHidden(bool hidden) {
134 if (temporarily_hidden_ == hidden)
135 return;
136 temporarily_hidden_ = hidden;
137
138 TouchHandle::AnimationStyle animation_style = GetAnimationStyle(true);
139 if (is_selection_active_) {
140 start_selection_handle_->SetVisible(GetStartVisible(), animation_style);
141 end_selection_handle_->SetVisible(GetEndVisible(), animation_style);
142 }
143 if (is_insertion_active_)
144 insertion_handle_->SetVisible(GetStartVisible(), animation_style);
145 }
146
147 void TouchSelectionController::OnSelectionEditable(bool editable) {
148 if (selection_editable_ == editable)
149 return;
150 selection_editable_ = editable;
151 if (!selection_editable_)
152 DeactivateInsertion();
153 }
154
155 bool TouchSelectionController::Animate(base::TimeTicks frame_time) {
156 if (is_insertion_active_)
157 return insertion_handle_->Animate(frame_time);
158
159 if (is_selection_active_) {
160 bool needs_animate = start_selection_handle_->Animate(frame_time);
161 needs_animate |= end_selection_handle_->Animate(frame_time);
162 return needs_animate;
163 }
164
165 return false;
166 }
167
168 void TouchSelectionController::OnHandleDragBegin(const TouchHandle& handle) {
169 if (&handle == insertion_handle_.get())
170 return;
171
172 if (&handle == start_selection_handle_.get()) {
173 fixed_handle_position_ = end_selection_handle_->position() -
174 gfx::Vector2dF(0, GetEndLineHeight() / 2.f);
175 } else {
176 fixed_handle_position_ = start_selection_handle_->position() -
177 gfx::Vector2dF(0, GetStartLineHeight() / 2.f);
178 }
179 }
180
181 void TouchSelectionController::OnHandleDragUpdate(const TouchHandle& handle,
182 const gfx::PointF& position) {
183 // As the position corresponds to the bottom left point of the selection
184 // bound, offset it by half the corresponding line height.
185 float half_line_height = &handle == end_selection_handle_.get()
186 ? GetEndLineHeight() / 2.f
187 : GetStartLineHeight() / 2.f;
188 gfx::PointF line_position = position - gfx::Vector2dF(0, half_line_height);
189 if (&handle == insertion_handle_.get()) {
190 client_->MoveCaret(line_position);
191 } else {
192 client_->SelectBetweenCoordinates(fixed_handle_position_, line_position);
193 }
194 }
195
196 void TouchSelectionController::OnHandleDragEnd(const TouchHandle& handle) {
197 }
198
199 void TouchSelectionController::OnHandleTapped(const TouchHandle& handle) {
200 if (insertion_handle_ && &handle == insertion_handle_.get())
201 client_->OnSelectionEvent(INSERTION_TAPPED, GetStartPosition());
202 }
203
204 void TouchSelectionController::SetNeedsAnimate() {
205 client_->SetNeedsAnimate();
206 }
207
208 scoped_ptr<TouchHandleDrawable> TouchSelectionController::CreateDrawable() {
209 return client_->CreateDrawable();
210 }
211
212 void TouchSelectionController::OnInsertionChanged() {
213 DeactivateSelection();
214
215 if (!activate_insertion_automatically_ || !selection_editable_)
216 return;
217
218 const bool was_active = is_insertion_active_;
219 const gfx::PointF position = GetStartPosition();
220 if (!is_insertion_active_)
221 ActivateInsertion();
222 else
223 client_->OnSelectionEvent(INSERTION_MOVED, position);
224
225 insertion_handle_->SetVisible(GetStartVisible(),
226 GetAnimationStyle(was_active));
227 insertion_handle_->SetPosition(position);
228 }
229
230 void TouchSelectionController::OnSelectionChanged() {
231 DeactivateInsertion();
232
233 if (!activate_selection_automatically_)
234 return;
235
236 const bool was_active = is_selection_active_;
237 ActivateSelection();
238
239 const TouchHandle::AnimationStyle animation = GetAnimationStyle(was_active);
240 start_selection_handle_->SetVisible(GetStartVisible(), animation);
241 end_selection_handle_->SetVisible(GetEndVisible(), animation);
242
243 start_selection_handle_->SetPosition(GetStartPosition());
244 end_selection_handle_->SetPosition(GetEndPosition());
245 }
246
247 void TouchSelectionController::ActivateInsertion() {
248 DCHECK(!is_selection_active_);
249
250 if (!insertion_handle_)
251 insertion_handle_.reset(new TouchHandle(this, TOUCH_HANDLE_CENTER));
252
253 if (!is_insertion_active_) {
254 is_insertion_active_ = true;
255 insertion_handle_->SetEnabled(true);
256 client_->OnSelectionEvent(INSERTION_SHOWN, GetStartPosition());
257 }
258 }
259
260 void TouchSelectionController::DeactivateInsertion() {
261 if (!is_insertion_active_)
262 return;
263 DCHECK(insertion_handle_);
264 is_insertion_active_ = false;
265 insertion_handle_->SetEnabled(false);
266 client_->OnSelectionEvent(INSERTION_CLEARED, gfx::PointF());
267 }
268
269 void TouchSelectionController::ActivateSelection() {
270 DCHECK(!is_insertion_active_);
271
272 if (!start_selection_handle_) {
273 start_selection_handle_.reset(new TouchHandle(this, start_orientation_));
274 } else {
275 start_selection_handle_->SetEnabled(true);
276 start_selection_handle_->SetOrientation(start_orientation_);
277 }
278
279 if (!end_selection_handle_) {
280 end_selection_handle_.reset(new TouchHandle(this, end_orientation_));
281 } else {
282 end_selection_handle_->SetEnabled(true);
283 end_selection_handle_->SetOrientation(end_orientation_);
284 }
285
286 if (!is_selection_active_) {
287 is_selection_active_ = true;
288 client_->OnSelectionEvent(SELECTION_SHOWN, GetStartPosition());
289 }
290 }
291
292 void TouchSelectionController::DeactivateSelection() {
293 if (!is_selection_active_)
294 return;
295 DCHECK(start_selection_handle_);
296 DCHECK(end_selection_handle_);
297 start_selection_handle_->SetEnabled(false);
298 end_selection_handle_->SetEnabled(false);
299 is_selection_active_ = false;
300 client_->OnSelectionEvent(SELECTION_CLEARED, gfx::PointF());
301 }
302
303 void TouchSelectionController::ResetCachedValues() {
304 start_rect_ = gfx::RectF();
305 end_rect_ = gfx::RectF();
306 start_orientation_ = TOUCH_HANDLE_ORIENTATION_UNDEFINED;
307 end_orientation_ = TOUCH_HANDLE_ORIENTATION_UNDEFINED;
308 start_visible_ = false;
309 end_visible_ = false;
310 selection_editable_for_last_update_ = false;
311 }
312
313 gfx::PointF TouchSelectionController::GetStartPosition() const {
314 return start_rect_.bottom_left();
315 }
316
317 gfx::PointF TouchSelectionController::GetEndPosition() const {
318 return end_rect_.bottom_left();
319 }
320
321 float TouchSelectionController::GetStartLineHeight() const {
322 return start_rect_.height();
323 }
324
325 float TouchSelectionController::GetEndLineHeight() const {
326 return end_rect_.height();
327 }
328
329 bool TouchSelectionController::GetStartVisible() const {
330 return start_visible_ && !temporarily_hidden_;
331 }
332
333 bool TouchSelectionController::GetEndVisible() const {
334 return end_visible_ && !temporarily_hidden_;
335 }
336
337 TouchHandle::AnimationStyle TouchSelectionController::GetAnimationStyle(
338 bool was_active) const {
339 return was_active && client_->SupportsAnimation()
340 ? TouchHandle::ANIMATION_SMOOTH
341 : TouchHandle::ANIMATION_NONE;
342 }
343
344 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698