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()->display_controller()->AddObserver(this);
172 ToplevelWindowEventHandler::~ToplevelWindowEventHandler() {
173 Shell::GetInstance()->display_controller()->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 (in_gesture_drag_
)
196 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
197 switch (event
->type()) {
198 case ui::ET_MOUSE_PRESSED
:
199 HandleMousePressed(target
, event
);
201 case ui::ET_MOUSE_DRAGGED
:
202 HandleDrag(target
, event
);
204 case ui::ET_MOUSE_CAPTURE_CHANGED
:
205 case ui::ET_MOUSE_RELEASED
:
206 HandleMouseReleased(target
, event
);
208 case ui::ET_MOUSE_MOVED
:
209 HandleMouseMoved(target
, event
);
211 case ui::ET_MOUSE_EXITED
:
212 HandleMouseExited(target
, event
);
219 void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent
* event
) {
220 if (event
->handled())
222 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
223 if (!target
->delegate())
226 if (window_resizer_
.get() && !in_gesture_drag_
)
229 if (window_resizer_
.get() &&
230 window_resizer_
->resizer()->GetTarget() != target
) {
234 if (event
->details().touch_points() > 2) {
235 if (CompleteDrag(DRAG_COMPLETE
))
236 event
->StopPropagation();
240 switch (event
->type()) {
241 case ui::ET_GESTURE_TAP_DOWN
: {
242 int component
= GetWindowComponent(target
, *event
);
243 if (!(WindowResizer::GetBoundsChangeForWindowComponent(component
) &
244 WindowResizer::kBoundsChange_Resizes
))
246 ResizeShadowController
* controller
=
247 Shell::GetInstance()->resize_shadow_controller();
249 controller
->ShowShadow(target
, component
);
252 case ui::ET_GESTURE_END
: {
253 ResizeShadowController
* controller
=
254 Shell::GetInstance()->resize_shadow_controller();
256 controller
->HideShadow(target
);
258 if (window_resizer_
.get() &&
259 (event
->details().touch_points() == 1 ||
260 !CanStartOneFingerDrag(first_finger_hittest_
))) {
261 CompleteDrag(DRAG_COMPLETE
);
262 event
->StopPropagation();
266 case ui::ET_GESTURE_BEGIN
: {
267 if (event
->details().touch_points() == 1) {
268 first_finger_hittest_
= GetWindowComponent(target
, *event
);
269 } else if (window_resizer_
.get()) {
270 if (!window_resizer_
->IsMove()) {
271 // The transition from resizing with one finger to resizing with two
272 // fingers causes unintended resizing because the location of
273 // ET_GESTURE_SCROLL_UPDATE jumps from the position of the first
274 // finger to the position in the middle of the two fingers. For this
275 // reason two finger resizing is not supported.
276 CompleteDrag(DRAG_COMPLETE
);
277 event
->StopPropagation();
280 int second_finger_hittest
= GetWindowComponent(target
, *event
);
281 if (CanStartTwoFingerMove(
282 target
, first_finger_hittest_
, second_finger_hittest
)) {
283 gfx::Point location_in_parent
=
284 event
->details().bounding_box().CenterPoint();
285 AttemptToStartDrag(target
, location_in_parent
, HTCAPTION
,
286 aura::client::WINDOW_MOVE_SOURCE_TOUCH
);
287 event
->StopPropagation();
292 case ui::ET_GESTURE_SCROLL_BEGIN
: {
293 // The one finger drag is not started in ET_GESTURE_BEGIN to avoid the
294 // window jumping upon initiating a two finger drag. When a one finger
295 // drag is converted to a two finger drag, a jump occurs because the
296 // location of the ET_GESTURE_SCROLL_UPDATE event switches from the single
297 // finger's position to the position in the middle of the two fingers.
298 if (window_resizer_
.get())
300 int component
= GetWindowComponent(target
, *event
);
301 if (!CanStartOneFingerDrag(component
))
303 gfx::Point
location_in_parent(
304 ConvertPointToParent(target
, event
->location()));
305 AttemptToStartDrag(target
, location_in_parent
, component
,
306 aura::client::WINDOW_MOVE_SOURCE_TOUCH
);
307 event
->StopPropagation();
314 if (!window_resizer_
.get())
317 switch (event
->type()) {
318 case ui::ET_GESTURE_SCROLL_UPDATE
:
319 HandleDrag(target
, event
);
320 event
->StopPropagation();
322 case ui::ET_GESTURE_SCROLL_END
:
323 // We must complete the drag here instead of as a result of ET_GESTURE_END
324 // because otherwise the drag will be reverted when EndMoveLoop() is
326 // TODO(pkotwicz): Pass drag completion status to
327 // WindowMoveClient::EndMoveLoop().
328 CompleteDrag(DRAG_COMPLETE
);
329 event
->StopPropagation();
331 case ui::ET_SCROLL_FLING_START
:
332 CompleteDrag(DRAG_COMPLETE
);
334 // TODO(pkotwicz): Fix tests which inadvertantly start flings and check
335 // window_resizer_->IsMove() instead of the hittest component at |event|'s
337 if (GetWindowComponent(target
, *event
) != HTCAPTION
||
338 !wm::GetWindowState(target
)->IsNormalOrSnapped()) {
342 if (event
->details().velocity_y() > kMinVertVelocityForWindowMinimize
) {
343 SetWindowStateTypeFromGesture(target
, wm::WINDOW_STATE_TYPE_MINIMIZED
);
344 } else if (event
->details().velocity_y() <
345 -kMinVertVelocityForWindowMinimize
) {
346 SetWindowStateTypeFromGesture(target
, wm::WINDOW_STATE_TYPE_MAXIMIZED
);
347 } else if (event
->details().velocity_x() >
348 kMinHorizVelocityForWindowSwipe
) {
349 SetWindowStateTypeFromGesture(target
,
350 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
);
351 } else if (event
->details().velocity_x() <
352 -kMinHorizVelocityForWindowSwipe
) {
353 SetWindowStateTypeFromGesture(target
,
354 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED
);
356 event
->StopPropagation();
358 case ui::ET_GESTURE_SWIPE
:
359 DCHECK_GT(event
->details().touch_points(), 0);
360 if (event
->details().touch_points() == 1)
362 if (!wm::GetWindowState(target
)->IsNormalOrSnapped())
365 CompleteDrag(DRAG_COMPLETE
);
367 if (event
->details().swipe_down()) {
368 SetWindowStateTypeFromGesture(target
, wm::WINDOW_STATE_TYPE_MINIMIZED
);
369 } else if (event
->details().swipe_up()) {
370 SetWindowStateTypeFromGesture(target
, wm::WINDOW_STATE_TYPE_MAXIMIZED
);
371 } else if (event
->details().swipe_right()) {
372 SetWindowStateTypeFromGesture(target
,
373 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
);
375 SetWindowStateTypeFromGesture(target
,
376 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED
);
378 event
->StopPropagation();
385 aura::client::WindowMoveResult
ToplevelWindowEventHandler::RunMoveLoop(
386 aura::Window
* source
,
387 const gfx::Vector2d
& drag_offset
,
388 aura::client::WindowMoveSource move_source
) {
389 DCHECK(!in_move_loop_
); // Can only handle one nested loop at a time.
390 aura::Window
* root_window
= source
->GetRootWindow();
392 // TODO(tdresser): Use gfx::PointF. See crbug.com/337824.
393 gfx::Point drag_location
;
394 if (move_source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
&&
395 aura::Env::GetInstance()->is_touch_down()) {
396 gfx::PointF drag_location_f
;
397 bool has_point
= ui::GestureRecognizer::Get()->
398 GetLastTouchPointForTarget(source
, &drag_location_f
);
399 drag_location
= gfx::ToFlooredPoint(drag_location_f
);
403 root_window
->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
404 aura::Window::ConvertPointToTarget(
405 root_window
, source
->parent(), &drag_location
);
407 // Set the cursor before calling AttemptToStartDrag(), as that will
408 // eventually call LockCursor() and prevent the cursor from changing.
409 aura::client::CursorClient
* cursor_client
=
410 aura::client::GetCursorClient(root_window
);
412 cursor_client
->SetCursor(ui::kCursorPointer
);
413 if (!AttemptToStartDrag(source
, drag_location
, HTCAPTION
, move_source
))
414 return aura::client::MOVE_CANCELED
;
416 in_move_loop_
= true;
417 bool destroyed
= false;
418 destroyed_
= &destroyed
;
419 base::MessageLoopForUI
* loop
= base::MessageLoopForUI::current();
420 base::MessageLoop::ScopedNestableTaskAllower
allow_nested(loop
);
421 base::RunLoop run_loop
;
422 quit_closure_
= run_loop
.QuitClosure();
425 return aura::client::MOVE_CANCELED
;
427 in_move_loop_
= false;
428 return drag_reverted_
? aura::client::MOVE_CANCELED
:
429 aura::client::MOVE_SUCCESSFUL
;
432 void ToplevelWindowEventHandler::EndMoveLoop() {
434 CompleteDrag(DRAG_REVERT
);
437 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
438 CompleteDrag(DRAG_REVERT
);
441 bool ToplevelWindowEventHandler::AttemptToStartDrag(
442 aura::Window
* window
,
443 const gfx::Point
& point_in_parent
,
444 int window_component
,
445 aura::client::WindowMoveSource source
) {
446 if (window_resizer_
.get())
448 WindowResizer
* resizer
= CreateWindowResizer(window
, point_in_parent
,
449 window_component
, source
).release();
453 window_resizer_
.reset(new ScopedWindowResizer(this, resizer
));
455 pre_drag_window_bounds_
= window
->bounds();
456 in_gesture_drag_
= (source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
);
460 bool ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status
) {
461 if (!window_resizer_
)
464 scoped_ptr
<ScopedWindowResizer
> resizer(window_resizer_
.release());
467 resizer
->resizer()->CompleteDrag();
470 resizer
->resizer()->RevertDrag();
472 case DRAG_RESIZER_WINDOW_DESTROYED
:
473 // We explicitly do not invoke RevertDrag() since that may do things to
474 // WindowResizer::GetTarget() which was destroyed.
477 drag_reverted_
= (status
!= DRAG_COMPLETE
);
479 first_finger_hittest_
= HTNOWHERE
;
480 in_gesture_drag_
= false;
486 void ToplevelWindowEventHandler::HandleMousePressed(
487 aura::Window
* target
,
488 ui::MouseEvent
* event
) {
489 if (event
->phase() != ui::EP_PRETARGET
|| !target
->delegate())
492 // We also update the current window component here because for the
493 // mouse-drag-release-press case, where the mouse is released and
494 // pressed without mouse move event.
495 int component
= GetWindowComponent(target
, *event
);
496 if ((event
->flags() &
497 (ui::EF_IS_DOUBLE_CLICK
| ui::EF_IS_TRIPLE_CLICK
)) == 0 &&
498 WindowResizer::GetBoundsChangeForWindowComponent(component
)) {
499 gfx::Point
location_in_parent(
500 ConvertPointToParent(target
, event
->location()));
501 AttemptToStartDrag(target
, location_in_parent
, component
,
502 aura::client::WINDOW_MOVE_SOURCE_MOUSE
);
503 // Set as handled so that other event handlers do no act upon the event
504 // but still receive it so that they receive both parts of each pressed/
508 CompleteDrag(DRAG_COMPLETE
);
512 void ToplevelWindowEventHandler::HandleMouseReleased(
513 aura::Window
* target
,
514 ui::MouseEvent
* event
) {
515 if (event
->phase() != ui::EP_PRETARGET
)
518 CompleteDrag(event
->type() == ui::ET_MOUSE_RELEASED
?
519 DRAG_COMPLETE
: DRAG_REVERT
);
522 void ToplevelWindowEventHandler::HandleDrag(
523 aura::Window
* target
,
524 ui::LocatedEvent
* event
) {
525 // This function only be triggered to move window
526 // by mouse drag or touch move event.
527 DCHECK(event
->type() == ui::ET_MOUSE_DRAGGED
||
528 event
->type() == ui::ET_TOUCH_MOVED
||
529 event
->type() == ui::ET_GESTURE_SCROLL_UPDATE
);
531 // Drag actions are performed pre-target handling to prevent spurious mouse
532 // moves from the move/size operation from being sent to the target.
533 if (event
->phase() != ui::EP_PRETARGET
)
536 if (!window_resizer_
)
538 window_resizer_
->resizer()->Drag(
539 ConvertPointToParent(target
, event
->location()), event
->flags());
540 event
->StopPropagation();
543 void ToplevelWindowEventHandler::HandleMouseMoved(
544 aura::Window
* target
,
545 ui::LocatedEvent
* event
) {
546 // Shadow effects are applied after target handling. Note that we don't
547 // respect ER_HANDLED here right now since we have not had a reason to allow
548 // the target to cancel shadow rendering.
549 if (event
->phase() != ui::EP_POSTTARGET
|| !target
->delegate())
552 // TODO(jamescook): Move the resize cursor update code into here from
553 // CompoundEventFilter?
554 ResizeShadowController
* controller
=
555 Shell::GetInstance()->resize_shadow_controller();
557 if (event
->flags() & ui::EF_IS_NON_CLIENT
) {
559 target
->delegate()->GetNonClientComponent(event
->location());
560 controller
->ShowShadow(target
, component
);
562 controller
->HideShadow(target
);
567 void ToplevelWindowEventHandler::HandleMouseExited(
568 aura::Window
* target
,
569 ui::LocatedEvent
* event
) {
570 // Shadow effects are applied after target handling. Note that we don't
571 // respect ER_HANDLED here right now since we have not had a reason to allow
572 // the target to cancel shadow rendering.
573 if (event
->phase() != ui::EP_POSTTARGET
)
576 ResizeShadowController
* controller
=
577 Shell::GetInstance()->resize_shadow_controller();
579 controller
->HideShadow(target
);
582 void ToplevelWindowEventHandler::SetWindowStateTypeFromGesture(
583 aura::Window
* window
,
584 wm::WindowStateType new_state_type
) {
585 wm::WindowState
* window_state
= ash::wm::GetWindowState(window
);
586 // TODO(oshima): Move extra logic (set_unminimize_to_restore_bounds,
587 // SetRestoreBoundsInParent) that modifies the window state
589 switch (new_state_type
) {
590 case wm::WINDOW_STATE_TYPE_MINIMIZED
:
591 if (window_state
->CanMinimize()) {
592 window_state
->Minimize();
593 window_state
->set_unminimize_to_restore_bounds(true);
594 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
597 case wm::WINDOW_STATE_TYPE_MAXIMIZED
:
598 if (window_state
->CanMaximize()) {
599 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
600 window_state
->Maximize();
603 case wm::WINDOW_STATE_TYPE_LEFT_SNAPPED
:
604 if (window_state
->CanSnap()) {
605 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
606 const wm::WMEvent
event(wm::WM_EVENT_SNAP_LEFT
);
607 window_state
->OnWMEvent(&event
);
610 case wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED
:
611 if (window_state
->CanSnap()) {
612 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
613 const wm::WMEvent
event(wm::WM_EVENT_SNAP_RIGHT
);
614 window_state
->OnWMEvent(&event
);
622 void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
623 CompleteDrag(DRAG_RESIZER_WINDOW_DESTROYED
);