1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/drag_drop/drag_drop_controller.h"
#include <utility>
#include "ash/drag_drop/drag_drop_tracker.h"
#include "ash/drag_drop/drag_image_view.h"
#include "ash/shell.h"
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram_macros.h"
#include "base/run_loop.h"
#include "base/thread_task_runner_handle.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/base/hit_test.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/gfx/animation/linear_animation.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/path.h"
#include "ui/views/widget/native_widget_aura.h"
#include "ui/wm/core/coordinate_conversion.h"
#include "ui/wm/public/drag_drop_delegate.h"
namespace ash {
namespace {
// The duration of the drag cancel animation in millisecond.
const int kCancelAnimationDuration = 250;
const int kTouchCancelAnimationDuration = 20;
// The frame rate of the drag cancel animation in hertz.
const int kCancelAnimationFrameRate = 60;
// For touch initiated dragging, we scale and shift drag image by the following:
static const float kTouchDragImageScale = 1.2f;
static const int kTouchDragImageVerticalOffset = -25;
// Adjusts the drag image bounds such that the new bounds are scaled by |scale|
// and translated by the |drag_image_offset| and and additional
// |vertical_offset|.
gfx::Rect AdjustDragImageBoundsForScaleAndOffset(
const gfx::Rect& drag_image_bounds,
int vertical_offset,
float scale,
gfx::Vector2d* drag_image_offset) {
gfx::Point final_origin = drag_image_bounds.origin();
gfx::SizeF final_size = gfx::SizeF(drag_image_bounds.size());
final_size.Scale(scale);
drag_image_offset->set_x(drag_image_offset->x() * scale);
drag_image_offset->set_y(drag_image_offset->y() * scale);
int total_x_offset = drag_image_offset->x();
int total_y_offset = drag_image_offset->y() - vertical_offset;
final_origin.Offset(-total_x_offset, -total_y_offset);
return gfx::ToEnclosingRect(
gfx::RectF(gfx::PointF(final_origin), final_size));
}
void DispatchGestureEndToWindow(aura::Window* window) {
if (window && window->delegate()) {
ui::GestureEvent gesture_end(0,
0,
0,
ui::EventTimeForNow(),
ui::GestureEventDetails(ui::ET_GESTURE_END));
window->delegate()->OnGestureEvent(&gesture_end);
}
}
} // namespace
class DragDropTrackerDelegate : public aura::WindowDelegate {
public:
explicit DragDropTrackerDelegate(DragDropController* controller)
: drag_drop_controller_(controller) {}
~DragDropTrackerDelegate() override {}
// Overridden from WindowDelegate:
gfx::Size GetMinimumSize() const override { return gfx::Size(); }
gfx::Size GetMaximumSize() const override { return gfx::Size(); }
void OnBoundsChanged(const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds) override {}
gfx::NativeCursor GetCursor(const gfx::Point& point) override {
return gfx::kNullCursor;
}
int GetNonClientComponent(const gfx::Point& point) const override {
return HTCAPTION;
}
bool ShouldDescendIntoChildForEventHandling(
aura::Window* child,
const gfx::Point& location) override {
return true;
}
bool CanFocus() override { return true; }
void OnCaptureLost() override {
if (drag_drop_controller_->IsDragDropInProgress())
drag_drop_controller_->DragCancel();
}
void OnPaint(const ui::PaintContext& context) override {}
void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
void OnWindowDestroying(aura::Window* window) override {}
void OnWindowDestroyed(aura::Window* window) override {}
void OnWindowTargetVisibilityChanged(bool visible) override {}
bool HasHitTestMask() const override { return true; }
void GetHitTestMask(gfx::Path* mask) const override {
DCHECK(mask->isEmpty());
}
private:
DragDropController* drag_drop_controller_;
DISALLOW_COPY_AND_ASSIGN(DragDropTrackerDelegate);
};
////////////////////////////////////////////////////////////////////////////////
// DragDropController, public:
DragDropController::DragDropController()
: drag_data_(NULL),
drag_operation_(0),
drag_window_(NULL),
drag_source_window_(NULL),
should_block_during_drag_drop_(true),
drag_drop_window_delegate_(new DragDropTrackerDelegate(this)),
current_drag_event_source_(ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE),
weak_factory_(this) {
Shell::GetInstance()->PrependPreTargetHandler(this);
}
DragDropController::~DragDropController() {
Shell::GetInstance()->RemovePreTargetHandler(this);
Cleanup();
if (cancel_animation_)
cancel_animation_->End();
if (drag_image_)
drag_image_.reset();
}
int DragDropController::StartDragAndDrop(
const ui::OSExchangeData& data,
aura::Window* root_window,
aura::Window* source_window,
const gfx::Point& screen_location,
int operation,
ui::DragDropTypes::DragEventSource source) {
if (IsDragDropInProgress())
return 0;
const ui::OSExchangeData::Provider* provider = &data.provider();
// We do not support touch drag/drop without a drag image.
if (source == ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH &&
provider->GetDragImage().size().IsEmpty())
return 0;
UMA_HISTOGRAM_ENUMERATION("Event.DragDrop.Start", source,
ui::DragDropTypes::DRAG_EVENT_SOURCE_COUNT);
current_drag_event_source_ = source;
DragDropTracker* tracker =
new DragDropTracker(root_window, drag_drop_window_delegate_.get());
if (source == ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH) {
// We need to transfer the current gesture sequence and the GR's touch event
// queue to the |drag_drop_tracker_|'s capture window so that when it takes
// capture, it still gets a valid gesture state.
ui::GestureRecognizer::Get()->TransferEventsTo(source_window,
tracker->capture_window());
// We also send a gesture end to the source window so it can clear state.
// TODO(varunjain): Remove this whole block when gesture sequence
// transferring is properly done in the GR (http://crbug.com/160558)
DispatchGestureEndToWindow(source_window);
}
tracker->TakeCapture();
drag_drop_tracker_.reset(tracker);
drag_source_window_ = source_window;
if (drag_source_window_)
drag_source_window_->AddObserver(this);
pending_long_tap_.reset();
drag_data_ = &data;
drag_operation_ = operation;
float drag_image_scale = 1;
int drag_image_vertical_offset = 0;
if (source == ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH) {
drag_image_scale = kTouchDragImageScale;
drag_image_vertical_offset = kTouchDragImageVerticalOffset;
}
gfx::Point start_location = screen_location;
drag_image_final_bounds_for_cancel_animation_ = gfx::Rect(
start_location - provider->GetDragImageOffset(),
provider->GetDragImage().size());
drag_image_.reset(new DragImageView(source_window->GetRootWindow(), source));
drag_image_->SetImage(provider->GetDragImage());
drag_image_offset_ = provider->GetDragImageOffset();
gfx::Rect drag_image_bounds(start_location, drag_image_->GetPreferredSize());
drag_image_bounds = AdjustDragImageBoundsForScaleAndOffset(drag_image_bounds,
drag_image_vertical_offset, drag_image_scale, &drag_image_offset_);
drag_image_->SetBoundsInScreen(drag_image_bounds);
drag_image_->SetWidgetVisible(true);
if (source == ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH) {
drag_image_->SetTouchDragOperationHintPosition(gfx::Point(
drag_image_offset_.x(),
drag_image_offset_.y() + drag_image_vertical_offset));
}
drag_window_ = NULL;
// Ends cancel animation if it's in progress.
if (cancel_animation_)
cancel_animation_->End();
if (should_block_during_drag_drop_) {
base::RunLoop run_loop;
quit_closure_ = run_loop.QuitClosure();
base::MessageLoop* loop = base::MessageLoop::current();
base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
run_loop.Run();
}
if (drag_operation_ == 0) {
UMA_HISTOGRAM_ENUMERATION("Event.DragDrop.Cancel", source,
ui::DragDropTypes::DRAG_EVENT_SOURCE_COUNT);
} else {
UMA_HISTOGRAM_ENUMERATION("Event.DragDrop.Drop", source,
ui::DragDropTypes::DRAG_EVENT_SOURCE_COUNT);
}
if (!cancel_animation_.get() || !cancel_animation_->is_animating() ||
!pending_long_tap_.get()) {
// If drag cancel animation is running, this cleanup is done when the
// animation completes.
if (drag_source_window_)
drag_source_window_->RemoveObserver(this);
drag_source_window_ = NULL;
}
return drag_operation_;
}
void DragDropController::DragUpdate(aura::Window* target,
const ui::LocatedEvent& event) {
int op = ui::DragDropTypes::DRAG_NONE;
if (target != drag_window_) {
if (drag_window_) {
aura::client::DragDropDelegate* delegate =
aura::client::GetDragDropDelegate(drag_window_);
if (delegate)
delegate->OnDragExited();
if (drag_window_ != drag_source_window_)
drag_window_->RemoveObserver(this);
}
drag_window_ = target;
// We are already an observer of |drag_source_window_| so no need to add.
if (drag_window_ != drag_source_window_)
drag_window_->AddObserver(this);
aura::client::DragDropDelegate* delegate =
aura::client::GetDragDropDelegate(drag_window_);
if (delegate) {
ui::DropTargetEvent e(*drag_data_, gfx::Point(), gfx::Point(),
drag_operation_);
e.set_location_f(event.location_f());
e.set_root_location_f(event.root_location_f());
e.set_flags(event.flags());
delegate->OnDragEntered(e);
}
} else {
aura::client::DragDropDelegate* delegate =
aura::client::GetDragDropDelegate(drag_window_);
if (delegate) {
ui::DropTargetEvent e(*drag_data_, gfx::Point(), gfx::Point(),
drag_operation_);
e.set_location_f(event.location_f());
e.set_root_location_f(event.root_location_f());
e.set_flags(event.flags());
op = delegate->OnDragUpdated(e);
gfx::NativeCursor cursor = ui::kCursorNoDrop;
if (op & ui::DragDropTypes::DRAG_COPY)
cursor = ui::kCursorCopy;
else if (op & ui::DragDropTypes::DRAG_LINK)
cursor = ui::kCursorAlias;
else if (op & ui::DragDropTypes::DRAG_MOVE)
cursor = ui::kCursorGrabbing;
ash::Shell::GetInstance()->cursor_manager()->SetCursor(cursor);
}
}
DCHECK(drag_image_.get());
if (drag_image_->visible()) {
gfx::Point root_location_in_screen = event.root_location();
::wm::ConvertPointToScreen(target->GetRootWindow(),
&root_location_in_screen);
drag_image_->SetScreenPosition(
root_location_in_screen - drag_image_offset_);
drag_image_->SetTouchDragOperation(op);
}
}
void DragDropController::Drop(aura::Window* target,
const ui::LocatedEvent& event) {
ash::Shell::GetInstance()->cursor_manager()->SetCursor(ui::kCursorPointer);
// We must guarantee that a target gets a OnDragEntered before Drop. WebKit
// depends on not getting a Drop without DragEnter. This behavior is
// consistent with drag/drop on other platforms.
if (target != drag_window_)
DragUpdate(target, event);
DCHECK(target == drag_window_);
aura::client::DragDropDelegate* delegate =
aura::client::GetDragDropDelegate(target);
if (delegate) {
ui::DropTargetEvent e(*drag_data_, gfx::Point(), gfx::Point(),
drag_operation_);
e.set_location_f(event.location_f());
e.set_root_location_f(event.root_location_f());
e.set_flags(event.flags());
drag_operation_ = delegate->OnPerformDrop(e);
if (drag_operation_ == 0)
StartCanceledAnimation(kCancelAnimationDuration);
else
drag_image_.reset();
} else {
drag_image_.reset();
}
Cleanup();
if (should_block_during_drag_drop_)
quit_closure_.Run();
}
void DragDropController::DragCancel() {
DoDragCancel(kCancelAnimationDuration);
}
bool DragDropController::IsDragDropInProgress() {
return !!drag_drop_tracker_.get();
}
void DragDropController::OnKeyEvent(ui::KeyEvent* event) {
if (IsDragDropInProgress() && event->key_code() == ui::VKEY_ESCAPE) {
DragCancel();
event->StopPropagation();
}
}
void DragDropController::OnMouseEvent(ui::MouseEvent* event) {
if (!IsDragDropInProgress())
return;
// If current drag session was not started by mouse, dont process this mouse
// event, but consume it so it does not interfere with current drag session.
if (current_drag_event_source_ !=
ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE) {
event->StopPropagation();
return;
}
aura::Window* translated_target = drag_drop_tracker_->GetTarget(*event);
if (!translated_target) {
DragCancel();
event->StopPropagation();
return;
}
scoped_ptr<ui::LocatedEvent> translated_event(
drag_drop_tracker_->ConvertEvent(translated_target, *event));
switch (translated_event->type()) {
case ui::ET_MOUSE_DRAGGED:
DragUpdate(translated_target, *translated_event.get());
break;
case ui::ET_MOUSE_RELEASED:
Drop(translated_target, *translated_event.get());
break;
default:
// We could also reach here because RootWindow may sometimes generate a
// bunch of fake mouse events
// (aura::RootWindow::PostMouseMoveEventAfterWindowChange).
break;
}
event->StopPropagation();
}
void DragDropController::OnTouchEvent(ui::TouchEvent* event) {
if (!IsDragDropInProgress())
return;
// If current drag session was not started by touch, dont process this touch
// event, but consume it so it does not interfere with current drag session.
if (current_drag_event_source_ != ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH)
event->StopPropagation();
if (event->handled())
return;
if (event->type() == ui::ET_TOUCH_CANCELLED)
DragCancel();
}
void DragDropController::OnGestureEvent(ui::GestureEvent* event) {
if (!IsDragDropInProgress())
return;
// No one else should handle gesture events when in drag drop. Note that it is
// not enough to just set ER_HANDLED because the dispatcher only stops
// dispatching when the event has ER_CONSUMED. If we just set ER_HANDLED, the
// event will still be dispatched to other handlers and we depend on
// individual handlers' kindness to not touch events marked ER_HANDLED (not
// all handlers are so kind and may cause bugs like crbug.com/236493).
event->StopPropagation();
// If current drag session was not started by touch, dont process this event.
if (current_drag_event_source_ != ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH)
return;
// Apply kTouchDragImageVerticalOffset to the location.
ui::GestureEvent touch_offset_event(*event,
static_cast<aura::Window*>(NULL),
static_cast<aura::Window*>(NULL));
gfx::PointF touch_offset_location = touch_offset_event.location_f();
gfx::PointF touch_offset_root_location = touch_offset_event.root_location_f();
touch_offset_location.Offset(0, kTouchDragImageVerticalOffset);
touch_offset_root_location.Offset(0, kTouchDragImageVerticalOffset);
touch_offset_event.set_location_f(touch_offset_location);
touch_offset_event.set_root_location_f(touch_offset_root_location);
aura::Window* translated_target =
drag_drop_tracker_->GetTarget(touch_offset_event);
if (!translated_target) {
DragCancel();
event->SetHandled();
return;
}
scoped_ptr<ui::LocatedEvent> translated_event(
drag_drop_tracker_->ConvertEvent(translated_target, touch_offset_event));
switch (event->type()) {
case ui::ET_GESTURE_SCROLL_UPDATE:
DragUpdate(translated_target, *translated_event.get());
break;
case ui::ET_GESTURE_SCROLL_END:
case ui::ET_SCROLL_FLING_START:
Drop(translated_target, *translated_event.get());
break;
case ui::ET_GESTURE_LONG_TAP:
// Ideally we would want to just forward this long tap event to the
// |drag_source_window_|. However, webkit does not accept events while a
// drag drop is still in progress. The drag drop ends only when the nested
// message loop ends. Due to this stupidity, we have to defer forwarding
// the long tap.
pending_long_tap_.reset(
new ui::GestureEvent(*event,
static_cast<aura::Window*>(drag_drop_tracker_->capture_window()),
static_cast<aura::Window*>(drag_source_window_)));
DoDragCancel(kTouchCancelAnimationDuration);
break;
default:
break;
}
event->SetHandled();
}
void DragDropController::OnWindowDestroyed(aura::Window* window) {
if (drag_window_ == window)
drag_window_ = NULL;
if (drag_source_window_ == window)
drag_source_window_ = NULL;
}
////////////////////////////////////////////////////////////////////////////////
// DragDropController, protected:
gfx::LinearAnimation* DragDropController::CreateCancelAnimation(
int duration,
int frame_rate,
gfx::AnimationDelegate* delegate) {
return new gfx::LinearAnimation(duration, frame_rate, delegate);
}
////////////////////////////////////////////////////////////////////////////////
// DragDropController, private:
void DragDropController::AnimationEnded(const gfx::Animation* animation) {
cancel_animation_.reset();
// By the time we finish animation, another drag/drop session may have
// started. We do not want to destroy the drag image in that case.
if (!IsDragDropInProgress())
drag_image_.reset();
if (pending_long_tap_) {
// If not in a nested message loop, we can forward the long tap right now.
if (!should_block_during_drag_drop_) {
ForwardPendingLongTap();
} else {
// See comment about this in OnGestureEvent().
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&DragDropController::ForwardPendingLongTap,
weak_factory_.GetWeakPtr()));
}
}
}
void DragDropController::DoDragCancel(int drag_cancel_animation_duration_ms) {
ash::Shell::GetInstance()->cursor_manager()->SetCursor(ui::kCursorPointer);
// |drag_window_| can be NULL if we have just started the drag and have not
// received any DragUpdates, or, if the |drag_window_| gets destroyed during
// a drag/drop.
aura::client::DragDropDelegate* delegate = drag_window_?
aura::client::GetDragDropDelegate(drag_window_) : NULL;
if (delegate)
delegate->OnDragExited();
Cleanup();
drag_operation_ = 0;
StartCanceledAnimation(drag_cancel_animation_duration_ms);
if (should_block_during_drag_drop_)
quit_closure_.Run();
}
void DragDropController::AnimationProgressed(const gfx::Animation* animation) {
gfx::Rect current_bounds = animation->CurrentValueBetween(
drag_image_initial_bounds_for_cancel_animation_,
drag_image_final_bounds_for_cancel_animation_);
drag_image_->SetBoundsInScreen(current_bounds);
}
void DragDropController::AnimationCanceled(const gfx::Animation* animation) {
AnimationEnded(animation);
}
void DragDropController::StartCanceledAnimation(int animation_duration_ms) {
DCHECK(drag_image_.get());
drag_image_->SetTouchDragOperationHintOff();
drag_image_initial_bounds_for_cancel_animation_ =
drag_image_->GetBoundsInScreen();
cancel_animation_.reset(CreateCancelAnimation(animation_duration_ms,
kCancelAnimationFrameRate,
this));
cancel_animation_->Start();
}
void DragDropController::ForwardPendingLongTap() {
if (drag_source_window_ && drag_source_window_->delegate()) {
drag_source_window_->delegate()->OnGestureEvent(pending_long_tap_.get());
DispatchGestureEndToWindow(drag_source_window_);
}
pending_long_tap_.reset();
if (drag_source_window_)
drag_source_window_->RemoveObserver(this);
drag_source_window_ = NULL;
}
void DragDropController::Cleanup() {
if (drag_window_)
drag_window_->RemoveObserver(this);
drag_window_ = NULL;
drag_data_ = NULL;
// Cleanup can be called again while deleting DragDropTracker, so delete
// the pointer with a local variable to avoid double free.
scoped_ptr<ash::DragDropTracker> holder = std::move(drag_drop_tracker_);
}
} // namespace ash
|