1 // Copyright (c) 2012 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.
5 #include "ash/wm/toplevel_window_event_handler.h"
8 #include "ash/wm/resize_shadow_controller.h"
9 #include "ash/wm/window_resizer.h"
10 #include "ash/wm/window_state.h"
11 #include "ash/wm/window_state_observer.h"
12 #include "ash/wm/window_util.h"
13 #include "ash/wm/wm_event.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
16 #include "ui/aura/client/cursor_client.h"
17 #include "ui/aura/env.h"
18 #include "ui/aura/window.h"
19 #include "ui/aura/window_delegate.h"
20 #include "ui/aura/window_event_dispatcher.h"
21 #include "ui/aura/window_observer.h"
22 #include "ui/aura/window_tree_host.h"
23 #include "ui/base/cursor/cursor.h"
24 #include "ui/base/hit_test.h"
25 #include "ui/base/ui_base_types.h"
26 #include "ui/events/event.h"
27 #include "ui/events/event_utils.h"
28 #include "ui/events/gestures/gesture_recognizer.h"
29 #include "ui/gfx/geometry/point_conversions.h"
30 #include "ui/gfx/screen.h"
33 const double kMinHorizVelocityForWindowSwipe
= 1100;
34 const double kMinVertVelocityForWindowMinimize
= 1000;
41 // Returns whether |window| can be moved via a two finger drag given
42 // the hittest results of the two fingers.
43 bool CanStartTwoFingerMove(aura::Window
* window
,
44 int window_component1
,
45 int window_component2
) {
46 // We allow moving a window via two fingers when the hittest components are
47 // HTCLIENT. This is done so that a window can be dragged via two fingers when
48 // the tab strip is full and hitting the caption area is difficult. We check
49 // the window type and the state type so that we do not steal touches from the
51 if (!wm::GetWindowState(window
)->IsNormalOrSnapped() ||
52 window
->type() != ui::wm::WINDOW_TYPE_NORMAL
) {
55 int component1_behavior
=
56 WindowResizer::GetBoundsChangeForWindowComponent(window_component1
);
57 int component2_behavior
=
58 WindowResizer::GetBoundsChangeForWindowComponent(window_component2
);
59 return (component1_behavior
& WindowResizer::kBoundsChange_Resizes
) == 0 &&
60 (component2_behavior
& WindowResizer::kBoundsChange_Resizes
) == 0;
63 // Returns whether |window| can be moved or resized via one finger given
64 // |window_component|.
65 bool CanStartOneFingerDrag(int window_component
) {
66 return WindowResizer::GetBoundsChangeForWindowComponent(
67 window_component
) != 0;
70 gfx::Point
ConvertPointToParent(aura::Window
* window
,
71 const gfx::Point
& point
) {
72 gfx::Point
result(point
);
73 aura::Window::ConvertPointToTarget(window
, window
->parent(), &result
);
77 // Returns the window component containing |event|'s location.
78 int GetWindowComponent(aura::Window
* window
, const ui::LocatedEvent
& event
) {
79 return window
->delegate()->GetNonClientComponent(event
.location());
84 // ScopedWindowResizer ---------------------------------------------------------
86 // Wraps a WindowResizer and installs an observer on its target window. When
87 // the window is destroyed ResizerWindowDestroyed() is invoked back on the
88 // ToplevelWindowEventHandler to clean up.
89 class ToplevelWindowEventHandler::ScopedWindowResizer
90 : public aura::WindowObserver
,
91 public wm::WindowStateObserver
{
93 ScopedWindowResizer(ToplevelWindowEventHandler
* handler
,
94 WindowResizer
* resizer
);
95 ~ScopedWindowResizer() override
;
97 // Returns true if the drag moves the window and does not resize.
100 WindowResizer
* resizer() { return resizer_
.get(); }
102 // WindowObserver overrides:
103 void OnWindowDestroying(aura::Window
* window
) override
;
105 // WindowStateObserver overrides:
106 void OnPreWindowStateTypeChange(wm::WindowState
* window_state
,
107 wm::WindowStateType type
) override
;
110 ToplevelWindowEventHandler
* handler_
;
111 scoped_ptr
<WindowResizer
> resizer_
;
113 // Whether ScopedWindowResizer grabbed capture.
114 bool grabbed_capture_
;
116 DISALLOW_COPY_AND_ASSIGN(ScopedWindowResizer
);
119 ToplevelWindowEventHandler::ScopedWindowResizer::ScopedWindowResizer(
120 ToplevelWindowEventHandler
* handler
,
121 WindowResizer
* resizer
)
124 grabbed_capture_(false) {
125 aura::Window
* target
= resizer_
->GetTarget();
126 target
->AddObserver(this);
127 wm::GetWindowState(target
)->AddObserver(this);
129 if (!target
->HasCapture()) {
130 grabbed_capture_
= true;
131 target
->SetCapture();
135 ToplevelWindowEventHandler::ScopedWindowResizer::~ScopedWindowResizer() {
136 aura::Window
* target
= resizer_
->GetTarget();
137 target
->RemoveObserver(this);
138 wm::GetWindowState(target
)->RemoveObserver(this);
139 if (grabbed_capture_
)
140 target
->ReleaseCapture();
143 bool ToplevelWindowEventHandler::ScopedWindowResizer::IsMove() const {
144 return resizer_
->details().bounds_change
==
145 WindowResizer::kBoundsChange_Repositions
;
149 ToplevelWindowEventHandler::ScopedWindowResizer::OnPreWindowStateTypeChange(
150 wm::WindowState
* window_state
,
151 wm::WindowStateType old
) {
152 handler_
->CompleteDrag(DRAG_COMPLETE
);
155 void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowDestroying(
156 aura::Window
* window
) {
157 DCHECK_EQ(resizer_
->GetTarget(), window
);
158 handler_
->ResizerWindowDestroyed();
161 // ToplevelWindowEventHandler --------------------------------------------------
163 ToplevelWindowEventHandler::ToplevelWindowEventHandler()
164 : first_finger_hittest_(HTNOWHERE
),
165 in_move_loop_(false),
166 in_gesture_drag_(false),
167 drag_reverted_(false),
169 Shell::GetInstance()->window_tree_host_manager()->AddObserver(this);
172 ToplevelWindowEventHandler::~ToplevelWindowEventHandler() {
173 Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this);
178 void ToplevelWindowEventHandler::OnKeyEvent(ui::KeyEvent
* event
) {
179 if (window_resizer_
.get() && event
->type() == ui::ET_KEY_PRESSED
&&
180 event
->key_code() == ui::VKEY_ESCAPE
) {
181 CompleteDrag(DRAG_REVERT
);
185 void ToplevelWindowEventHandler::OnMouseEvent(
186 ui::MouseEvent
* event
) {
187 if (event
->handled())
189 if ((event
->flags() &
190 (ui::EF_MIDDLE_MOUSE_BUTTON
| ui::EF_RIGHT_MOUSE_BUTTON
)) != 0)
193 if (event
->type() == ui::ET_MOUSE_CAPTURE_CHANGED
) {
194 // Capture is grabbed when both gesture and mouse drags start. Handle
195 // capture loss regardless of which type of drag is in progress.
196 HandleCaptureLost(event
);
200 if (in_gesture_drag_
)
203 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
204 switch (event
->type()) {
205 case ui::ET_MOUSE_PRESSED
:
206 HandleMousePressed(target
, event
);
208 case ui::ET_MOUSE_DRAGGED
:
209 HandleDrag(target
, event
);
211 case ui::ET_MOUSE_RELEASED
:
212 HandleMouseReleased(target
, event
);
214 case ui::ET_MOUSE_MOVED
:
215 HandleMouseMoved(target
, event
);
217 case ui::ET_MOUSE_EXITED
:
218 HandleMouseExited(target
, event
);
225 void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent
* event
) {
226 if (event
->handled())
228 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
229 if (!target
->delegate())
232 if (window_resizer_
.get() && !in_gesture_drag_
)
235 if (window_resizer_
.get() &&
236 window_resizer_
->resizer()->GetTarget() != target
) {
240 if (event
->details().touch_points() > 2) {
241 if (CompleteDrag(DRAG_COMPLETE
))
242 event
->StopPropagation();
246 switch (event
->type()) {
247 case ui::ET_GESTURE_TAP_DOWN
: {
248 int component
= GetWindowComponent(target
, *event
);
249 if (!(WindowResizer::GetBoundsChangeForWindowComponent(component
) &
250 WindowResizer::kBoundsChange_Resizes
))
252 ResizeShadowController
* controller
=
253 Shell::GetInstance()->resize_shadow_controller();
255 controller
->ShowShadow(target
, component
);
258 case ui::ET_GESTURE_END
: {
259 ResizeShadowController
* controller
=
260 Shell::GetInstance()->resize_shadow_controller();
262 controller
->HideShadow(target
);
264 if (window_resizer_
.get() &&
265 (event
->details().touch_points() == 1 ||
266 !CanStartOneFingerDrag(first_finger_hittest_
))) {
267 CompleteDrag(DRAG_COMPLETE
);
268 event
->StopPropagation();
272 case ui::ET_GESTURE_BEGIN
: {
273 if (event
->details().touch_points() == 1) {
274 first_finger_hittest_
= GetWindowComponent(target
, *event
);
275 } else if (window_resizer_
.get()) {
276 if (!window_resizer_
->IsMove()) {
277 // The transition from resizing with one finger to resizing with two
278 // fingers causes unintended resizing because the location of
279 // ET_GESTURE_SCROLL_UPDATE jumps from the position of the first
280 // finger to the position in the middle of the two fingers. For this
281 // reason two finger resizing is not supported.
282 CompleteDrag(DRAG_COMPLETE
);
283 event
->StopPropagation();
286 int second_finger_hittest
= GetWindowComponent(target
, *event
);
287 if (CanStartTwoFingerMove(
288 target
, first_finger_hittest_
, second_finger_hittest
)) {
289 gfx::Point location_in_parent
=
290 event
->details().bounding_box().CenterPoint();
291 AttemptToStartDrag(target
, location_in_parent
, HTCAPTION
,
292 aura::client::WINDOW_MOVE_SOURCE_TOUCH
);
293 event
->StopPropagation();
298 case ui::ET_GESTURE_SCROLL_BEGIN
: {
299 // The one finger drag is not started in ET_GESTURE_BEGIN to avoid the
300 // window jumping upon initiating a two finger drag. When a one finger
301 // drag is converted to a two finger drag, a jump occurs because the
302 // location of the ET_GESTURE_SCROLL_UPDATE event switches from the single
303 // finger's position to the position in the middle of the two fingers.
304 if (window_resizer_
.get())
306 int component
= GetWindowComponent(target
, *event
);
307 if (!CanStartOneFingerDrag(component
))
309 gfx::Point
location_in_parent(
310 ConvertPointToParent(target
, event
->location()));
311 AttemptToStartDrag(target
, location_in_parent
, component
,
312 aura::client::WINDOW_MOVE_SOURCE_TOUCH
);
313 event
->StopPropagation();
320 if (!window_resizer_
.get())
323 switch (event
->type()) {
324 case ui::ET_GESTURE_SCROLL_UPDATE
:
325 HandleDrag(target
, event
);
326 event
->StopPropagation();
328 case ui::ET_GESTURE_SCROLL_END
:
329 // We must complete the drag here instead of as a result of ET_GESTURE_END
330 // because otherwise the drag will be reverted when EndMoveLoop() is
332 // TODO(pkotwicz): Pass drag completion status to
333 // WindowMoveClient::EndMoveLoop().
334 CompleteDrag(DRAG_COMPLETE
);
335 event
->StopPropagation();
337 case ui::ET_SCROLL_FLING_START
:
338 CompleteDrag(DRAG_COMPLETE
);
340 // TODO(pkotwicz): Fix tests which inadvertantly start flings and check
341 // window_resizer_->IsMove() instead of the hittest component at |event|'s
343 if (GetWindowComponent(target
, *event
) != HTCAPTION
||
344 !wm::GetWindowState(target
)->IsNormalOrSnapped()) {
348 if (event
->details().velocity_y() > kMinVertVelocityForWindowMinimize
) {
349 SetWindowStateTypeFromGesture(target
, wm::WINDOW_STATE_TYPE_MINIMIZED
);
350 } else if (event
->details().velocity_y() <
351 -kMinVertVelocityForWindowMinimize
) {
352 SetWindowStateTypeFromGesture(target
, wm::WINDOW_STATE_TYPE_MAXIMIZED
);
353 } else if (event
->details().velocity_x() >
354 kMinHorizVelocityForWindowSwipe
) {
355 SetWindowStateTypeFromGesture(target
,
356 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
);
357 } else if (event
->details().velocity_x() <
358 -kMinHorizVelocityForWindowSwipe
) {
359 SetWindowStateTypeFromGesture(target
,
360 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED
);
362 event
->StopPropagation();
364 case ui::ET_GESTURE_SWIPE
:
365 DCHECK_GT(event
->details().touch_points(), 0);
366 if (event
->details().touch_points() == 1)
368 if (!wm::GetWindowState(target
)->IsNormalOrSnapped())
371 CompleteDrag(DRAG_COMPLETE
);
373 if (event
->details().swipe_down()) {
374 SetWindowStateTypeFromGesture(target
, wm::WINDOW_STATE_TYPE_MINIMIZED
);
375 } else if (event
->details().swipe_up()) {
376 SetWindowStateTypeFromGesture(target
, wm::WINDOW_STATE_TYPE_MAXIMIZED
);
377 } else if (event
->details().swipe_right()) {
378 SetWindowStateTypeFromGesture(target
,
379 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
);
381 SetWindowStateTypeFromGesture(target
,
382 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED
);
384 event
->StopPropagation();
391 aura::client::WindowMoveResult
ToplevelWindowEventHandler::RunMoveLoop(
392 aura::Window
* source
,
393 const gfx::Vector2d
& drag_offset
,
394 aura::client::WindowMoveSource move_source
) {
395 DCHECK(!in_move_loop_
); // Can only handle one nested loop at a time.
396 aura::Window
* root_window
= source
->GetRootWindow();
398 gfx::Point drag_location
;
399 if (move_source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
&&
400 aura::Env::GetInstance()->is_touch_down()) {
401 gfx::PointF drag_location_f
;
402 bool has_point
= ui::GestureRecognizer::Get()->
403 GetLastTouchPointForTarget(source
, &drag_location_f
);
404 drag_location
= gfx::ToFlooredPoint(drag_location_f
);
408 root_window
->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
409 aura::Window::ConvertPointToTarget(
410 root_window
, source
->parent(), &drag_location
);
412 // Set the cursor before calling AttemptToStartDrag(), as that will
413 // eventually call LockCursor() and prevent the cursor from changing.
414 aura::client::CursorClient
* cursor_client
=
415 aura::client::GetCursorClient(root_window
);
417 cursor_client
->SetCursor(ui::kCursorPointer
);
418 if (!AttemptToStartDrag(source
, drag_location
, HTCAPTION
, move_source
))
419 return aura::client::MOVE_CANCELED
;
421 in_move_loop_
= true;
422 bool destroyed
= false;
423 destroyed_
= &destroyed
;
424 base::MessageLoopForUI
* loop
= base::MessageLoopForUI::current();
425 base::MessageLoop::ScopedNestableTaskAllower
allow_nested(loop
);
426 base::RunLoop run_loop
;
427 quit_closure_
= run_loop
.QuitClosure();
430 return aura::client::MOVE_CANCELED
;
432 in_move_loop_
= false;
433 return drag_reverted_
? aura::client::MOVE_CANCELED
:
434 aura::client::MOVE_SUCCESSFUL
;
437 void ToplevelWindowEventHandler::EndMoveLoop() {
439 CompleteDrag(DRAG_REVERT
);
442 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
443 CompleteDrag(DRAG_REVERT
);
446 bool ToplevelWindowEventHandler::AttemptToStartDrag(
447 aura::Window
* window
,
448 const gfx::Point
& point_in_parent
,
449 int window_component
,
450 aura::client::WindowMoveSource source
) {
451 if (window_resizer_
.get())
453 WindowResizer
* resizer
= CreateWindowResizer(window
, point_in_parent
,
454 window_component
, source
).release();
458 window_resizer_
.reset(new ScopedWindowResizer(this, resizer
));
460 pre_drag_window_bounds_
= window
->bounds();
461 in_gesture_drag_
= (source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
);
465 bool ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status
) {
466 if (!window_resizer_
)
469 scoped_ptr
<ScopedWindowResizer
> resizer(window_resizer_
.release());
472 resizer
->resizer()->CompleteDrag();
475 resizer
->resizer()->RevertDrag();
477 case DRAG_RESIZER_WINDOW_DESTROYED
:
478 // We explicitly do not invoke RevertDrag() since that may do things to
479 // WindowResizer::GetTarget() which was destroyed.
482 drag_reverted_
= (status
!= DRAG_COMPLETE
);
484 first_finger_hittest_
= HTNOWHERE
;
485 in_gesture_drag_
= false;
491 void ToplevelWindowEventHandler::HandleMousePressed(
492 aura::Window
* target
,
493 ui::MouseEvent
* event
) {
494 if (event
->phase() != ui::EP_PRETARGET
|| !target
->delegate())
497 // We also update the current window component here because for the
498 // mouse-drag-release-press case, where the mouse is released and
499 // pressed without mouse move event.
500 int component
= GetWindowComponent(target
, *event
);
501 if ((event
->flags() &
502 (ui::EF_IS_DOUBLE_CLICK
| ui::EF_IS_TRIPLE_CLICK
)) == 0 &&
503 WindowResizer::GetBoundsChangeForWindowComponent(component
)) {
504 gfx::Point
location_in_parent(
505 ConvertPointToParent(target
, event
->location()));
506 AttemptToStartDrag(target
, location_in_parent
, component
,
507 aura::client::WINDOW_MOVE_SOURCE_MOUSE
);
508 // Set as handled so that other event handlers do no act upon the event
509 // but still receive it so that they receive both parts of each pressed/
513 CompleteDrag(DRAG_COMPLETE
);
517 void ToplevelWindowEventHandler::HandleMouseReleased(
518 aura::Window
* target
,
519 ui::MouseEvent
* event
) {
520 if (event
->phase() == ui::EP_PRETARGET
)
521 CompleteDrag(DRAG_COMPLETE
);
524 void ToplevelWindowEventHandler::HandleDrag(
525 aura::Window
* target
,
526 ui::LocatedEvent
* event
) {
527 // This function only be triggered to move window
528 // by mouse drag or touch move event.
529 DCHECK(event
->type() == ui::ET_MOUSE_DRAGGED
||
530 event
->type() == ui::ET_TOUCH_MOVED
||
531 event
->type() == ui::ET_GESTURE_SCROLL_UPDATE
);
533 // Drag actions are performed pre-target handling to prevent spurious mouse
534 // moves from the move/size operation from being sent to the target.
535 if (event
->phase() != ui::EP_PRETARGET
)
538 if (!window_resizer_
)
540 window_resizer_
->resizer()->Drag(
541 ConvertPointToParent(target
, event
->location()), event
->flags());
542 event
->StopPropagation();
545 void ToplevelWindowEventHandler::HandleMouseMoved(
546 aura::Window
* target
,
547 ui::LocatedEvent
* event
) {
548 // Shadow effects are applied after target handling. Note that we don't
549 // respect ER_HANDLED here right now since we have not had a reason to allow
550 // the target to cancel shadow rendering.
551 if (event
->phase() != ui::EP_POSTTARGET
|| !target
->delegate())
554 // TODO(jamescook): Move the resize cursor update code into here from
555 // CompoundEventFilter?
556 ResizeShadowController
* controller
=
557 Shell::GetInstance()->resize_shadow_controller();
559 if (event
->flags() & ui::EF_IS_NON_CLIENT
) {
561 target
->delegate()->GetNonClientComponent(event
->location());
562 controller
->ShowShadow(target
, component
);
564 controller
->HideShadow(target
);
569 void ToplevelWindowEventHandler::HandleMouseExited(
570 aura::Window
* target
,
571 ui::LocatedEvent
* event
) {
572 // Shadow effects are applied after target handling. Note that we don't
573 // respect ER_HANDLED here right now since we have not had a reason to allow
574 // the target to cancel shadow rendering.
575 if (event
->phase() != ui::EP_POSTTARGET
)
578 ResizeShadowController
* controller
=
579 Shell::GetInstance()->resize_shadow_controller();
581 controller
->HideShadow(target
);
584 void ToplevelWindowEventHandler::HandleCaptureLost(ui::LocatedEvent
* event
) {
585 if (event
->phase() == ui::EP_PRETARGET
)
586 CompleteDrag(DRAG_REVERT
);
589 void ToplevelWindowEventHandler::SetWindowStateTypeFromGesture(
590 aura::Window
* window
,
591 wm::WindowStateType new_state_type
) {
592 wm::WindowState
* window_state
= ash::wm::GetWindowState(window
);
593 // TODO(oshima): Move extra logic (set_unminimize_to_restore_bounds,
594 // SetRestoreBoundsInParent) that modifies the window state
596 switch (new_state_type
) {
597 case wm::WINDOW_STATE_TYPE_MINIMIZED
:
598 if (window_state
->CanMinimize()) {
599 window_state
->Minimize();
600 window_state
->set_unminimize_to_restore_bounds(true);
601 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
604 case wm::WINDOW_STATE_TYPE_MAXIMIZED
:
605 if (window_state
->CanMaximize()) {
606 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
607 window_state
->Maximize();
610 case wm::WINDOW_STATE_TYPE_LEFT_SNAPPED
:
611 if (window_state
->CanSnap()) {
612 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
613 const wm::WMEvent
event(wm::WM_EVENT_SNAP_LEFT
);
614 window_state
->OnWMEvent(&event
);
617 case wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
:
618 if (window_state
->CanSnap()) {
619 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
620 const wm::WMEvent
event(wm::WM_EVENT_SNAP_RIGHT
);
621 window_state
->OnWMEvent(&event
);
629 void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
630 CompleteDrag(DRAG_RESIZER_WINDOW_DESTROYED
);