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 // TODO(tdresser): Use gfx::PointF. See crbug.com/337824.
399 gfx::Point drag_location
;
400 if (move_source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
&&
401 aura::Env::GetInstance()->is_touch_down()) {
402 gfx::PointF drag_location_f
;
403 bool has_point
= ui::GestureRecognizer::Get()->
404 GetLastTouchPointForTarget(source
, &drag_location_f
);
405 drag_location
= gfx::ToFlooredPoint(drag_location_f
);
409 root_window
->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
410 aura::Window::ConvertPointToTarget(
411 root_window
, source
->parent(), &drag_location
);
413 // Set the cursor before calling AttemptToStartDrag(), as that will
414 // eventually call LockCursor() and prevent the cursor from changing.
415 aura::client::CursorClient
* cursor_client
=
416 aura::client::GetCursorClient(root_window
);
418 cursor_client
->SetCursor(ui::kCursorPointer
);
419 if (!AttemptToStartDrag(source
, drag_location
, HTCAPTION
, move_source
))
420 return aura::client::MOVE_CANCELED
;
422 in_move_loop_
= true;
423 bool destroyed
= false;
424 destroyed_
= &destroyed
;
425 base::MessageLoopForUI
* loop
= base::MessageLoopForUI::current();
426 base::MessageLoop::ScopedNestableTaskAllower
allow_nested(loop
);
427 base::RunLoop run_loop
;
428 quit_closure_
= run_loop
.QuitClosure();
431 return aura::client::MOVE_CANCELED
;
433 in_move_loop_
= false;
434 return drag_reverted_
? aura::client::MOVE_CANCELED
:
435 aura::client::MOVE_SUCCESSFUL
;
438 void ToplevelWindowEventHandler::EndMoveLoop() {
440 CompleteDrag(DRAG_REVERT
);
443 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
444 CompleteDrag(DRAG_REVERT
);
447 bool ToplevelWindowEventHandler::AttemptToStartDrag(
448 aura::Window
* window
,
449 const gfx::Point
& point_in_parent
,
450 int window_component
,
451 aura::client::WindowMoveSource source
) {
452 if (window_resizer_
.get())
454 WindowResizer
* resizer
= CreateWindowResizer(window
, point_in_parent
,
455 window_component
, source
).release();
459 window_resizer_
.reset(new ScopedWindowResizer(this, resizer
));
461 pre_drag_window_bounds_
= window
->bounds();
462 in_gesture_drag_
= (source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
);
466 bool ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status
) {
467 if (!window_resizer_
)
470 scoped_ptr
<ScopedWindowResizer
> resizer(window_resizer_
.release());
473 resizer
->resizer()->CompleteDrag();
476 resizer
->resizer()->RevertDrag();
478 case DRAG_RESIZER_WINDOW_DESTROYED
:
479 // We explicitly do not invoke RevertDrag() since that may do things to
480 // WindowResizer::GetTarget() which was destroyed.
483 drag_reverted_
= (status
!= DRAG_COMPLETE
);
485 first_finger_hittest_
= HTNOWHERE
;
486 in_gesture_drag_
= false;
492 void ToplevelWindowEventHandler::HandleMousePressed(
493 aura::Window
* target
,
494 ui::MouseEvent
* event
) {
495 if (event
->phase() != ui::EP_PRETARGET
|| !target
->delegate())
498 // We also update the current window component here because for the
499 // mouse-drag-release-press case, where the mouse is released and
500 // pressed without mouse move event.
501 int component
= GetWindowComponent(target
, *event
);
502 if ((event
->flags() &
503 (ui::EF_IS_DOUBLE_CLICK
| ui::EF_IS_TRIPLE_CLICK
)) == 0 &&
504 WindowResizer::GetBoundsChangeForWindowComponent(component
)) {
505 gfx::Point
location_in_parent(
506 ConvertPointToParent(target
, event
->location()));
507 AttemptToStartDrag(target
, location_in_parent
, component
,
508 aura::client::WINDOW_MOVE_SOURCE_MOUSE
);
509 // Set as handled so that other event handlers do no act upon the event
510 // but still receive it so that they receive both parts of each pressed/
514 CompleteDrag(DRAG_COMPLETE
);
518 void ToplevelWindowEventHandler::HandleMouseReleased(
519 aura::Window
* target
,
520 ui::MouseEvent
* event
) {
521 if (event
->phase() == ui::EP_PRETARGET
)
522 CompleteDrag(DRAG_COMPLETE
);
525 void ToplevelWindowEventHandler::HandleDrag(
526 aura::Window
* target
,
527 ui::LocatedEvent
* event
) {
528 // This function only be triggered to move window
529 // by mouse drag or touch move event.
530 DCHECK(event
->type() == ui::ET_MOUSE_DRAGGED
||
531 event
->type() == ui::ET_TOUCH_MOVED
||
532 event
->type() == ui::ET_GESTURE_SCROLL_UPDATE
);
534 // Drag actions are performed pre-target handling to prevent spurious mouse
535 // moves from the move/size operation from being sent to the target.
536 if (event
->phase() != ui::EP_PRETARGET
)
539 if (!window_resizer_
)
541 window_resizer_
->resizer()->Drag(
542 ConvertPointToParent(target
, event
->location()), event
->flags());
543 event
->StopPropagation();
546 void ToplevelWindowEventHandler::HandleMouseMoved(
547 aura::Window
* target
,
548 ui::LocatedEvent
* event
) {
549 // Shadow effects are applied after target handling. Note that we don't
550 // respect ER_HANDLED here right now since we have not had a reason to allow
551 // the target to cancel shadow rendering.
552 if (event
->phase() != ui::EP_POSTTARGET
|| !target
->delegate())
555 // TODO(jamescook): Move the resize cursor update code into here from
556 // CompoundEventFilter?
557 ResizeShadowController
* controller
=
558 Shell::GetInstance()->resize_shadow_controller();
560 if (event
->flags() & ui::EF_IS_NON_CLIENT
) {
562 target
->delegate()->GetNonClientComponent(event
->location());
563 controller
->ShowShadow(target
, component
);
565 controller
->HideShadow(target
);
570 void ToplevelWindowEventHandler::HandleMouseExited(
571 aura::Window
* target
,
572 ui::LocatedEvent
* event
) {
573 // Shadow effects are applied after target handling. Note that we don't
574 // respect ER_HANDLED here right now since we have not had a reason to allow
575 // the target to cancel shadow rendering.
576 if (event
->phase() != ui::EP_POSTTARGET
)
579 ResizeShadowController
* controller
=
580 Shell::GetInstance()->resize_shadow_controller();
582 controller
->HideShadow(target
);
585 void ToplevelWindowEventHandler::HandleCaptureLost(ui::LocatedEvent
* event
) {
586 if (event
->phase() == ui::EP_PRETARGET
)
587 CompleteDrag(DRAG_REVERT
);
590 void ToplevelWindowEventHandler::SetWindowStateTypeFromGesture(
591 aura::Window
* window
,
592 wm::WindowStateType new_state_type
) {
593 wm::WindowState
* window_state
= ash::wm::GetWindowState(window
);
594 // TODO(oshima): Move extra logic (set_unminimize_to_restore_bounds,
595 // SetRestoreBoundsInParent) that modifies the window state
597 switch (new_state_type
) {
598 case wm::WINDOW_STATE_TYPE_MINIMIZED
:
599 if (window_state
->CanMinimize()) {
600 window_state
->Minimize();
601 window_state
->set_unminimize_to_restore_bounds(true);
602 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
605 case wm::WINDOW_STATE_TYPE_MAXIMIZED
:
606 if (window_state
->CanMaximize()) {
607 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
608 window_state
->Maximize();
611 case wm::WINDOW_STATE_TYPE_LEFT_SNAPPED
:
612 if (window_state
->CanSnap()) {
613 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
614 const wm::WMEvent
event(wm::WM_EVENT_SNAP_LEFT
);
615 window_state
->OnWMEvent(&event
);
618 case wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
:
619 if (window_state
->CanSnap()) {
620 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
621 const wm::WMEvent
event(wm::WM_EVENT_SNAP_RIGHT
);
622 window_state
->OnWMEvent(&event
);
630 void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
631 CompleteDrag(DRAG_RESIZER_WINDOW_DESTROYED
);