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
|
// 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/wm/toplevel_window_event_handler.h"
#include "ash/shell.h"
#include "ash/wm/property_util.h"
#include "ash/wm/resize_shadow_controller.h"
#include "ash/wm/window_resizer.h"
#include "ash/wm/window_util.h"
#include "ash/wm/workspace/snap_sizer.h"
#include "base/message_loop.h"
#include "base/run_loop.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/cursor_client.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/aura/window_observer.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/events/event.h"
#include "ui/base/events/event_functions.h"
#include "ui/base/gestures/gesture_recognizer.h"
#include "ui/base/hit_test.h"
#include "ui/base/ui_base_types.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/screen.h"
namespace {
const double kMinHorizVelocityForWindowSwipe = 1100;
const double kMinVertVelocityForWindowMinimize = 1000;
}
namespace ash {
namespace {
gfx::Point ConvertPointToParent(aura::Window* window,
const gfx::Point& point) {
gfx::Point result(point);
aura::Window::ConvertPointToTarget(window, window->parent(), &result);
return result;
}
} // namespace
// ScopedWindowResizer ---------------------------------------------------------
// Wraps a WindowResizer and installs an observer on its target window. When
// the window is destroyed ResizerWindowDestroyed() is invoked back on the
// ToplevelWindowEventHandler to clean up.
class ToplevelWindowEventHandler::ScopedWindowResizer
: public aura::WindowObserver {
public:
ScopedWindowResizer(ToplevelWindowEventHandler* handler,
WindowResizer* resizer);
virtual ~ScopedWindowResizer();
WindowResizer* resizer() { return resizer_.get(); }
// WindowObserver overrides:
virtual void OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) OVERRIDE;
virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
private:
ToplevelWindowEventHandler* handler_;
scoped_ptr<WindowResizer> resizer_;
DISALLOW_COPY_AND_ASSIGN(ScopedWindowResizer);
};
ToplevelWindowEventHandler::ScopedWindowResizer::ScopedWindowResizer(
ToplevelWindowEventHandler* handler,
WindowResizer* resizer)
: handler_(handler),
resizer_(resizer) {
if (resizer_.get())
resizer_->GetTarget()->AddObserver(this);
}
ToplevelWindowEventHandler::ScopedWindowResizer::~ScopedWindowResizer() {
if (resizer_.get())
resizer_->GetTarget()->RemoveObserver(this);
}
void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowPropertyChanged(
aura::Window* window,
const void* key,
intptr_t old) {
if (!wm::IsWindowNormal(window))
handler_->CompleteDrag(DRAG_COMPLETE, 0);
}
void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowDestroying(
aura::Window* window) {
DCHECK(resizer_.get());
DCHECK_EQ(resizer_->GetTarget(), window);
handler_->ResizerWindowDestroyed();
}
// ToplevelWindowEventHandler --------------------------------------------------
ToplevelWindowEventHandler::ToplevelWindowEventHandler(aura::Window* owner)
: in_move_loop_(false),
move_cancelled_(false),
in_gesture_resize_(false),
destroyed_(NULL) {
aura::client::SetWindowMoveClient(owner, this);
Shell::GetInstance()->display_controller()->AddObserver(this);
owner->AddPreTargetHandler(this);
owner->AddPostTargetHandler(this);
}
ToplevelWindowEventHandler::~ToplevelWindowEventHandler() {
Shell::GetInstance()->display_controller()->RemoveObserver(this);
if (destroyed_)
*destroyed_ = true;
}
ui::EventResult ToplevelWindowEventHandler::OnKeyEvent(ui::KeyEvent* event) {
if (window_resizer_.get() && event->type() == ui::ET_KEY_PRESSED &&
event->key_code() == ui::VKEY_ESCAPE) {
CompleteDrag(DRAG_REVERT, event->flags());
}
return ui::ER_UNHANDLED;
}
ui::EventResult ToplevelWindowEventHandler::OnMouseEvent(
ui::MouseEvent* event) {
if ((event->flags() &
(ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)) != 0)
return ui::ER_UNHANDLED;
aura::Window* target = static_cast<aura::Window*>(event->target());
switch (event->type()) {
case ui::ET_MOUSE_PRESSED:
return HandleMousePressed(target, event);
case ui::ET_MOUSE_DRAGGED:
return HandleDrag(target, event) ? ui::ER_CONSUMED : ui::ER_UNHANDLED;
case ui::ET_MOUSE_CAPTURE_CHANGED:
case ui::ET_MOUSE_RELEASED:
return HandleMouseReleased(target, event);
case ui::ET_MOUSE_MOVED:
return HandleMouseMoved(target, event);
case ui::ET_MOUSE_EXITED:
return HandleMouseExited(target, event);
default:
break;
}
return ui::ER_UNHANDLED;
}
ui::EventResult ToplevelWindowEventHandler::OnScrollEvent(
ui::ScrollEvent* event) {
return ui::ER_UNHANDLED;
}
ui::EventResult ToplevelWindowEventHandler::OnTouchEvent(
ui::TouchEvent* event) {
return ui::ER_UNHANDLED;
}
ui::EventResult ToplevelWindowEventHandler::OnGestureEvent(
ui::GestureEvent* event) {
aura::Window* target = static_cast<aura::Window*>(event->target());
switch (event->type()) {
case ui::ET_GESTURE_SCROLL_BEGIN: {
int component =
target->delegate()->GetNonClientComponent(event->location());
if (WindowResizer::GetBoundsChangeForWindowComponent(component) == 0) {
window_resizer_.reset();
return ui::ER_UNHANDLED;
}
in_gesture_resize_ = true;
gfx::Point location_in_parent(
ConvertPointToParent(target, event->location()));
CreateScopedWindowResizer(target, location_in_parent, component);
break;
}
case ui::ET_GESTURE_SCROLL_UPDATE: {
if (!in_gesture_resize_)
return ui::ER_UNHANDLED;
HandleDrag(target, event);
break;
}
case ui::ET_GESTURE_SCROLL_END:
case ui::ET_SCROLL_FLING_START: {
ui::EventResult status = ui::ER_UNHANDLED;
if (in_gesture_resize_) {
// If the window was being resized, then just complete the resize.
CompleteDrag(DRAG_COMPLETE, event->flags());
if (in_move_loop_) {
quit_closure_.Run();
in_move_loop_ = false;
}
in_gesture_resize_ = false;
status = ui::ER_CONSUMED;
}
if (event->type() == ui::ET_GESTURE_SCROLL_END)
return status;
int component =
target->delegate()->GetNonClientComponent(event->location());
if (WindowResizer::GetBoundsChangeForWindowComponent(component) == 0)
return ui::ER_UNHANDLED;
if (!wm::IsWindowNormal(target))
return ui::ER_UNHANDLED;
if (fabs(event->details().velocity_y()) >
kMinVertVelocityForWindowMinimize) {
// Minimize/maximize.
target->SetProperty(aura::client::kShowStateKey,
event->details().velocity_y() > 0 ? ui::SHOW_STATE_MINIMIZED :
ui::SHOW_STATE_MAXIMIZED);
} else if (fabs(event->details().velocity_x()) >
kMinHorizVelocityForWindowSwipe) {
// Snap left/right.
internal::SnapSizer sizer(target,
gfx::Point(),
event->details().velocity_x() < 0 ? internal::SnapSizer::LEFT_EDGE :
internal::SnapSizer::RIGHT_EDGE);
ui::ScopedLayerAnimationSettings scoped_setter(
target->layer()->GetAnimator());
scoped_setter.SetPreemptionStrategy(
ui::LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
target->SetBounds(sizer.target_bounds());
}
break;
}
default:
return ui::ER_UNHANDLED;
}
return ui::ER_CONSUMED;
}
aura::client::WindowMoveResult ToplevelWindowEventHandler::RunMoveLoop(
aura::Window* source,
const gfx::Vector2d& drag_offset) {
DCHECK(!in_move_loop_); // Can only handle one nested loop at a time.
in_move_loop_ = true;
move_cancelled_ = false;
aura::RootWindow* root_window = source->GetRootWindow();
DCHECK(root_window);
gfx::Point drag_location;
if (aura::Env::GetInstance()->is_touch_down()) {
in_gesture_resize_ = true;
bool has_point = root_window->gesture_recognizer()->
GetLastTouchPointForTarget(source, &drag_location);
DCHECK(has_point);
} else {
drag_location = root_window->GetLastMouseLocationInRoot();
aura::Window::ConvertPointToTarget(
root_window, source->parent(), &drag_location);
}
CreateScopedWindowResizer(source, drag_location, HTCAPTION);
aura::client::CursorClient* cursor_client =
aura::client::GetCursorClient(root_window);
if (cursor_client)
cursor_client->SetCursor(ui::kCursorPointer);
bool destroyed = false;
destroyed_ = &destroyed;
#if !defined(OS_MACOSX)
MessageLoopForUI* loop = MessageLoopForUI::current();
MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher());
quit_closure_ = run_loop.QuitClosure();
run_loop.Run();
#endif // !defined(OS_MACOSX)
if (destroyed)
return aura::client::MOVE_CANCELED;
destroyed_ = NULL;
in_gesture_resize_ = in_move_loop_ = false;
return move_cancelled_ ? aura::client::MOVE_CANCELED :
aura::client::MOVE_SUCCESSFUL;
}
void ToplevelWindowEventHandler::EndMoveLoop() {
if (!in_move_loop_)
return;
in_move_loop_ = false;
if (window_resizer_.get()) {
window_resizer_->resizer()->RevertDrag();
window_resizer_.reset();
}
quit_closure_.Run();
}
void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
if (in_move_loop_) {
move_cancelled_ = true;
EndMoveLoop();
} else if (window_resizer_.get()) {
window_resizer_->resizer()->RevertDrag();
window_resizer_.reset();
}
}
void ToplevelWindowEventHandler::CreateScopedWindowResizer(
aura::Window* window,
const gfx::Point& point_in_parent,
int window_component) {
window_resizer_.reset();
WindowResizer* resizer =
CreateWindowResizer(window, point_in_parent, window_component).release();
if (resizer)
window_resizer_.reset(new ScopedWindowResizer(this, resizer));
}
void ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status,
int event_flags) {
scoped_ptr<ScopedWindowResizer> resizer(window_resizer_.release());
if (resizer.get()) {
if (status == DRAG_COMPLETE)
resizer->resizer()->CompleteDrag(event_flags);
else
resizer->resizer()->RevertDrag();
}
}
ui::EventResult ToplevelWindowEventHandler::HandleMousePressed(
aura::Window* target,
ui::MouseEvent* event) {
// Move/size operations are initiated post-target handling to give the target
// an opportunity to cancel this default behavior by returning ER_HANDLED.
if (ui::EventCanceledDefaultHandling(*event))
return ui::ER_UNHANDLED;
// We also update the current window component here because for the
// mouse-drag-release-press case, where the mouse is released and
// pressed without mouse move event.
int component =
target->delegate()->GetNonClientComponent(event->location());
if ((event->flags() &
(ui::EF_IS_DOUBLE_CLICK | ui::EF_IS_TRIPLE_CLICK)) == 0 &&
WindowResizer::GetBoundsChangeForWindowComponent(component)) {
gfx::Point location_in_parent(
ConvertPointToParent(target, event->location()));
CreateScopedWindowResizer(target, location_in_parent, component);
} else {
window_resizer_.reset();
}
return WindowResizer::GetBoundsChangeForWindowComponent(component) != 0 ?
ui::ER_CONSUMED : ui::ER_UNHANDLED;
}
ui::EventResult ToplevelWindowEventHandler::HandleMouseReleased(
aura::Window* target,
ui::MouseEvent* event) {
if (event->phase() != ui::EP_PRETARGET)
return ui::ER_UNHANDLED;
CompleteDrag(event->type() == ui::ET_MOUSE_RELEASED ?
DRAG_COMPLETE : DRAG_REVERT,
event->flags());
if (in_move_loop_) {
quit_closure_.Run();
in_move_loop_ = false;
}
// Completing the drag may result in hiding the window. If this happens
// return true so no other handlers/observers see the event. Otherwise
// they see the event on a hidden window.
if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED &&
!target->IsVisible()) {
return ui::ER_CONSUMED;
}
return ui::ER_UNHANDLED;
}
ui::EventResult ToplevelWindowEventHandler::HandleDrag(
aura::Window* target,
ui::LocatedEvent* event) {
// This function only be triggered to move window
// by mouse drag or touch move event.
DCHECK(event->type() == ui::ET_MOUSE_DRAGGED ||
event->type() == ui::ET_TOUCH_MOVED ||
event->type() == ui::ET_GESTURE_SCROLL_UPDATE);
// Drag actions are performed pre-target handling to prevent spurious mouse
// moves from the move/size operation from being sent to the target.
if (event->phase() != ui::EP_PRETARGET)
return ui::ER_UNHANDLED;
if (!window_resizer_.get())
return ui::ER_UNHANDLED;
window_resizer_->resizer()->Drag(
ConvertPointToParent(target, event->location()), event->flags());
return ui::ER_CONSUMED;
}
ui::EventResult ToplevelWindowEventHandler::HandleMouseMoved(
aura::Window* target,
ui::LocatedEvent* event) {
// Shadow effects are applied after target handling. Note that we don't
// respect ER_HANDLED here right now since we have not had a reason to allow
// the target to cancel shadow rendering.
if (event->phase() != ui::EP_POSTTARGET)
return ui::ER_UNHANDLED;
// TODO(jamescook): Move the resize cursor update code into here from
// CompoundEventFilter?
internal::ResizeShadowController* controller =
Shell::GetInstance()->resize_shadow_controller();
if (controller) {
if (event->flags() & ui::EF_IS_NON_CLIENT) {
int component =
target->delegate()->GetNonClientComponent(event->location());
controller->ShowShadow(target, component);
} else {
controller->HideShadow(target);
}
}
return ui::ER_UNHANDLED;
}
ui::EventResult ToplevelWindowEventHandler::HandleMouseExited(
aura::Window* target,
ui::LocatedEvent* event) {
// Shadow effects are applied after target handling. Note that we don't
// respect ER_HANDLED here right now since we have not had a reason to allow
// the target to cancel shadow rendering.
if (event->phase() != ui::EP_POSTTARGET)
return ui::ER_UNHANDLED;
internal::ResizeShadowController* controller =
Shell::GetInstance()->resize_shadow_controller();
if (controller)
controller->HideShadow(target);
return ui::ER_UNHANDLED;
}
void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
// We explicitly don't invoke RevertDrag() since that may do things to window.
// Instead we destroy the resizer.
window_resizer_.reset();
// End the move loop. This does nothing if we're not in a move loop.
EndMoveLoop();
}
} // namespace ash
|