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 "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "ui/aura/client/cursor_client.h"
16 #include "ui/aura/env.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_delegate.h"
19 #include "ui/aura/window_event_dispatcher.h"
20 #include "ui/aura/window_observer.h"
21 #include "ui/base/cursor/cursor.h"
22 #include "ui/base/hit_test.h"
23 #include "ui/base/ui_base_types.h"
24 #include "ui/events/event.h"
25 #include "ui/events/event_utils.h"
26 #include "ui/events/gestures/gesture_recognizer.h"
27 #include "ui/gfx/geometry/point_conversions.h"
28 #include "ui/gfx/screen.h"
31 const double kMinHorizVelocityForWindowSwipe
= 1100;
32 const double kMinVertVelocityForWindowMinimize
= 1000;
39 // Returns whether |window| can be moved via a two finger drag given
40 // the hittest results of the two fingers.
41 bool CanStartTwoFingerMove(aura::Window
* window
,
42 int window_component1
,
43 int window_component2
) {
44 // We allow moving a window via two fingers when the hittest components are
45 // HTCLIENT. This is done so that a window can be dragged via two fingers when
46 // the tab strip is full and hitting the caption area is difficult. We check
47 // the window type and the show type so that we do not steal touches from the
49 if (!wm::GetWindowState(window
)->IsNormalOrSnapped() ||
50 window
->type() != ui::wm::WINDOW_TYPE_NORMAL
) {
53 int component1_behavior
=
54 WindowResizer::GetBoundsChangeForWindowComponent(window_component1
);
55 int component2_behavior
=
56 WindowResizer::GetBoundsChangeForWindowComponent(window_component2
);
57 return (component1_behavior
& WindowResizer::kBoundsChange_Resizes
) == 0 &&
58 (component2_behavior
& WindowResizer::kBoundsChange_Resizes
) == 0;
61 // Returns whether |window| can be moved or resized via one finger given
62 // |window_component|.
63 bool CanStartOneFingerDrag(int window_component
) {
64 return WindowResizer::GetBoundsChangeForWindowComponent(
65 window_component
) != 0;
68 gfx::Point
ConvertPointToParent(aura::Window
* window
,
69 const gfx::Point
& point
) {
70 gfx::Point
result(point
);
71 aura::Window::ConvertPointToTarget(window
, window
->parent(), &result
);
75 // Returns the window component containing |event|'s location.
76 int GetWindowComponent(aura::Window
* window
, const ui::LocatedEvent
& event
) {
77 return window
->delegate()->GetNonClientComponent(event
.location());
82 // ScopedWindowResizer ---------------------------------------------------------
84 // Wraps a WindowResizer and installs an observer on its target window. When
85 // the window is destroyed ResizerWindowDestroyed() is invoked back on the
86 // ToplevelWindowEventHandler to clean up.
87 class ToplevelWindowEventHandler::ScopedWindowResizer
88 : public aura::WindowObserver
,
89 public wm::WindowStateObserver
{
91 ScopedWindowResizer(ToplevelWindowEventHandler
* handler
,
92 WindowResizer
* resizer
);
93 virtual ~ScopedWindowResizer();
95 // Returns true if the drag moves the window and does not resize.
98 WindowResizer
* resizer() { return resizer_
.get(); }
100 // WindowObserver overrides:
101 virtual void OnWindowDestroying(aura::Window
* window
) OVERRIDE
;
103 // WindowStateObserver overrides:
104 virtual void OnPreWindowShowTypeChange(wm::WindowState
* window_state
,
105 wm::WindowShowType type
) OVERRIDE
;
108 ToplevelWindowEventHandler
* handler_
;
109 scoped_ptr
<WindowResizer
> resizer_
;
111 DISALLOW_COPY_AND_ASSIGN(ScopedWindowResizer
);
114 ToplevelWindowEventHandler::ScopedWindowResizer::ScopedWindowResizer(
115 ToplevelWindowEventHandler
* handler
,
116 WindowResizer
* resizer
)
119 resizer_
->GetTarget()->AddObserver(this);
120 wm::GetWindowState(resizer_
->GetTarget())->AddObserver(this);
123 ToplevelWindowEventHandler::ScopedWindowResizer::~ScopedWindowResizer() {
124 resizer_
->GetTarget()->RemoveObserver(this);
125 wm::GetWindowState(resizer_
->GetTarget())->RemoveObserver(this);
128 bool ToplevelWindowEventHandler::ScopedWindowResizer::IsMove() const {
129 return resizer_
->details().bounds_change
==
130 WindowResizer::kBoundsChange_Repositions
;
134 ToplevelWindowEventHandler::ScopedWindowResizer::OnPreWindowShowTypeChange(
135 wm::WindowState
* window_state
,
136 wm::WindowShowType old
) {
137 handler_
->CompleteDrag(DRAG_COMPLETE
);
140 void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowDestroying(
141 aura::Window
* window
) {
142 DCHECK_EQ(resizer_
->GetTarget(), window
);
143 handler_
->ResizerWindowDestroyed();
146 // ToplevelWindowEventHandler --------------------------------------------------
148 ToplevelWindowEventHandler::ToplevelWindowEventHandler()
149 : first_finger_hittest_(HTNOWHERE
),
150 in_move_loop_(false),
151 in_gesture_drag_(false),
152 drag_reverted_(false),
154 Shell::GetInstance()->display_controller()->AddObserver(this);
157 ToplevelWindowEventHandler::~ToplevelWindowEventHandler() {
158 Shell::GetInstance()->display_controller()->RemoveObserver(this);
163 void ToplevelWindowEventHandler::OnKeyEvent(ui::KeyEvent
* event
) {
164 if (window_resizer_
.get() && event
->type() == ui::ET_KEY_PRESSED
&&
165 event
->key_code() == ui::VKEY_ESCAPE
) {
166 CompleteDrag(DRAG_REVERT
);
170 void ToplevelWindowEventHandler::OnMouseEvent(
171 ui::MouseEvent
* event
) {
172 if (event
->handled())
174 if ((event
->flags() &
175 (ui::EF_MIDDLE_MOUSE_BUTTON
| ui::EF_RIGHT_MOUSE_BUTTON
)) != 0)
178 if (in_gesture_drag_
)
181 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
182 switch (event
->type()) {
183 case ui::ET_MOUSE_PRESSED
:
184 HandleMousePressed(target
, event
);
186 case ui::ET_MOUSE_DRAGGED
:
187 HandleDrag(target
, event
);
189 case ui::ET_MOUSE_CAPTURE_CHANGED
:
190 case ui::ET_MOUSE_RELEASED
:
191 HandleMouseReleased(target
, event
);
193 case ui::ET_MOUSE_MOVED
:
194 HandleMouseMoved(target
, event
);
196 case ui::ET_MOUSE_EXITED
:
197 HandleMouseExited(target
, event
);
204 void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent
* event
) {
205 if (event
->handled())
207 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
208 if (!target
->delegate())
211 if (window_resizer_
.get() && !in_gesture_drag_
)
214 if (window_resizer_
.get() &&
215 window_resizer_
->resizer()->GetTarget() != target
) {
219 if (event
->details().touch_points() > 2) {
220 if (window_resizer_
.get()) {
221 CompleteDrag(DRAG_COMPLETE
);
222 event
->StopPropagation();
227 switch (event
->type()) {
228 case ui::ET_GESTURE_TAP_DOWN
: {
229 int component
= GetWindowComponent(target
, *event
);
230 if (!(WindowResizer::GetBoundsChangeForWindowComponent(component
) &
231 WindowResizer::kBoundsChange_Resizes
))
233 internal::ResizeShadowController
* controller
=
234 Shell::GetInstance()->resize_shadow_controller();
236 controller
->ShowShadow(target
, component
);
239 case ui::ET_GESTURE_END
: {
240 internal::ResizeShadowController
* controller
=
241 Shell::GetInstance()->resize_shadow_controller();
243 controller
->HideShadow(target
);
245 if (window_resizer_
.get() &&
246 (event
->details().touch_points() == 1 ||
247 !CanStartOneFingerDrag(first_finger_hittest_
))) {
248 CompleteDrag(DRAG_COMPLETE
);
249 event
->StopPropagation();
253 case ui::ET_GESTURE_BEGIN
: {
254 if (event
->details().touch_points() == 1) {
255 first_finger_hittest_
= GetWindowComponent(target
, *event
);
256 } else if (window_resizer_
.get()) {
257 if (!window_resizer_
->IsMove()) {
258 // The transition from resizing with one finger to resizing with two
259 // fingers causes unintended resizing because the location of
260 // ET_GESTURE_SCROLL_UPDATE jumps from the position of the first
261 // finger to the position in the middle of the two fingers. For this
262 // reason two finger resizing is not supported.
263 CompleteDrag(DRAG_COMPLETE
);
264 event
->StopPropagation();
267 int second_finger_hittest
= GetWindowComponent(target
, *event
);
268 if (CanStartTwoFingerMove(
269 target
, first_finger_hittest_
, second_finger_hittest
)) {
270 gfx::Point location_in_parent
=
271 event
->details().bounding_box().CenterPoint();
272 AttemptToStartDrag(target
, location_in_parent
, HTCAPTION
,
273 aura::client::WINDOW_MOVE_SOURCE_TOUCH
);
274 event
->StopPropagation();
279 case ui::ET_GESTURE_SCROLL_BEGIN
: {
280 // The one finger drag is not started in ET_GESTURE_BEGIN to avoid the
281 // window jumping upon initiating a two finger drag. When a one finger
282 // drag is converted to a two finger drag, a jump occurs because the
283 // location of the ET_GESTURE_SCROLL_UPDATE event switches from the single
284 // finger's position to the position in the middle of the two fingers.
285 if (window_resizer_
.get())
287 int component
= GetWindowComponent(target
, *event
);
288 if (!CanStartOneFingerDrag(component
))
290 gfx::Point
location_in_parent(
291 ConvertPointToParent(target
, event
->location()));
292 AttemptToStartDrag(target
, location_in_parent
, component
,
293 aura::client::WINDOW_MOVE_SOURCE_TOUCH
);
294 event
->StopPropagation();
301 if (!window_resizer_
.get())
304 switch (event
->type()) {
305 case ui::ET_GESTURE_SCROLL_UPDATE
:
306 HandleDrag(target
, event
);
307 event
->StopPropagation();
309 case ui::ET_GESTURE_SCROLL_END
:
310 // We must complete the drag here instead of as a result of ET_GESTURE_END
311 // because otherwise the drag will be reverted when EndMoveLoop() is
313 // TODO(pkotwicz): Pass drag completion status to
314 // WindowMoveClient::EndMoveLoop().
315 CompleteDrag(DRAG_COMPLETE
);
316 event
->StopPropagation();
318 case ui::ET_SCROLL_FLING_START
:
319 CompleteDrag(DRAG_COMPLETE
);
321 // TODO(pkotwicz): Fix tests which inadvertantly start flings and check
322 // window_resizer_->IsMove() instead of the hittest component at |event|'s
324 if (GetWindowComponent(target
, *event
) != HTCAPTION
||
325 !wm::GetWindowState(target
)->IsNormalOrSnapped()) {
329 if (event
->details().velocity_y() > kMinVertVelocityForWindowMinimize
) {
330 SetWindowShowTypeFromGesture(target
, wm::SHOW_TYPE_MINIMIZED
);
331 } else if (event
->details().velocity_y() <
332 -kMinVertVelocityForWindowMinimize
) {
333 SetWindowShowTypeFromGesture(target
, wm::SHOW_TYPE_MAXIMIZED
);
334 } else if (event
->details().velocity_x() >
335 kMinHorizVelocityForWindowSwipe
) {
336 SetWindowShowTypeFromGesture(target
, wm::SHOW_TYPE_RIGHT_SNAPPED
);
337 } else if (event
->details().velocity_x() <
338 -kMinHorizVelocityForWindowSwipe
) {
339 SetWindowShowTypeFromGesture(target
, wm::SHOW_TYPE_LEFT_SNAPPED
);
341 event
->StopPropagation();
343 case ui::ET_GESTURE_MULTIFINGER_SWIPE
:
344 if (!wm::GetWindowState(target
)->IsNormalOrSnapped())
347 CompleteDrag(DRAG_COMPLETE
);
349 if (event
->details().swipe_down())
350 SetWindowShowTypeFromGesture(target
, wm::SHOW_TYPE_MINIMIZED
);
351 else if (event
->details().swipe_up())
352 SetWindowShowTypeFromGesture(target
, wm::SHOW_TYPE_MAXIMIZED
);
353 else if (event
->details().swipe_right())
354 SetWindowShowTypeFromGesture(target
, wm::SHOW_TYPE_RIGHT_SNAPPED
);
356 SetWindowShowTypeFromGesture(target
, wm::SHOW_TYPE_LEFT_SNAPPED
);
357 event
->StopPropagation();
364 aura::client::WindowMoveResult
ToplevelWindowEventHandler::RunMoveLoop(
365 aura::Window
* source
,
366 const gfx::Vector2d
& drag_offset
,
367 aura::client::WindowMoveSource move_source
) {
368 DCHECK(!in_move_loop_
); // Can only handle one nested loop at a time.
369 aura::Window
* root_window
= source
->GetRootWindow();
371 // TODO(tdresser): Use gfx::PointF. See crbug.com/337824.
372 gfx::Point drag_location
;
373 if (move_source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
&&
374 aura::Env::GetInstance()->is_touch_down()) {
375 gfx::PointF drag_location_f
;
376 bool has_point
= ui::GestureRecognizer::Get()->
377 GetLastTouchPointForTarget(source
, &drag_location_f
);
378 drag_location
= gfx::ToFlooredPoint(drag_location_f
);
381 drag_location
= root_window
->GetDispatcher()->GetLastMouseLocationInRoot();
382 aura::Window::ConvertPointToTarget(
383 root_window
, source
->parent(), &drag_location
);
385 // Set the cursor before calling AttemptToStartDrag(), as that will
386 // eventually call LockCursor() and prevent the cursor from changing.
387 aura::client::CursorClient
* cursor_client
=
388 aura::client::GetCursorClient(root_window
);
390 cursor_client
->SetCursor(ui::kCursorPointer
);
391 if (!AttemptToStartDrag(source
, drag_location
, HTCAPTION
, move_source
))
392 return aura::client::MOVE_CANCELED
;
394 in_move_loop_
= true;
395 bool destroyed
= false;
396 destroyed_
= &destroyed
;
397 base::MessageLoopForUI
* loop
= base::MessageLoopForUI::current();
398 base::MessageLoop::ScopedNestableTaskAllower
allow_nested(loop
);
399 base::RunLoop run_loop
;
400 quit_closure_
= run_loop
.QuitClosure();
403 return aura::client::MOVE_CANCELED
;
405 in_move_loop_
= false;
406 return drag_reverted_
? aura::client::MOVE_CANCELED
:
407 aura::client::MOVE_SUCCESSFUL
;
410 void ToplevelWindowEventHandler::EndMoveLoop() {
412 CompleteDrag(DRAG_REVERT
);
415 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
416 CompleteDrag(DRAG_REVERT
);
419 bool ToplevelWindowEventHandler::AttemptToStartDrag(
420 aura::Window
* window
,
421 const gfx::Point
& point_in_parent
,
422 int window_component
,
423 aura::client::WindowMoveSource source
) {
424 if (window_resizer_
.get())
426 WindowResizer
* resizer
= CreateWindowResizer(window
, point_in_parent
,
427 window_component
, source
).release();
431 window_resizer_
.reset(new ScopedWindowResizer(this, resizer
));
433 pre_drag_window_bounds_
= window
->bounds();
434 in_gesture_drag_
= (source
== aura::client::WINDOW_MOVE_SOURCE_TOUCH
);
438 void ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status
) {
439 scoped_ptr
<ScopedWindowResizer
> resizer(window_resizer_
.release());
441 if (status
== DRAG_COMPLETE
)
442 resizer
->resizer()->CompleteDrag();
444 resizer
->resizer()->RevertDrag();
446 drag_reverted_
= (status
== DRAG_REVERT
);
448 first_finger_hittest_
= HTNOWHERE
;
449 in_gesture_drag_
= false;
454 void ToplevelWindowEventHandler::HandleMousePressed(
455 aura::Window
* target
,
456 ui::MouseEvent
* event
) {
457 if (event
->phase() != ui::EP_PRETARGET
|| !target
->delegate())
460 // We also update the current window component here because for the
461 // mouse-drag-release-press case, where the mouse is released and
462 // pressed without mouse move event.
463 int component
= GetWindowComponent(target
, *event
);
464 if ((event
->flags() &
465 (ui::EF_IS_DOUBLE_CLICK
| ui::EF_IS_TRIPLE_CLICK
)) == 0 &&
466 WindowResizer::GetBoundsChangeForWindowComponent(component
)) {
467 gfx::Point
location_in_parent(
468 ConvertPointToParent(target
, event
->location()));
469 AttemptToStartDrag(target
, location_in_parent
, component
,
470 aura::client::WINDOW_MOVE_SOURCE_MOUSE
);
471 event
->StopPropagation();
473 CompleteDrag(DRAG_COMPLETE
);
477 void ToplevelWindowEventHandler::HandleMouseReleased(
478 aura::Window
* target
,
479 ui::MouseEvent
* event
) {
480 if (event
->phase() != ui::EP_PRETARGET
)
483 CompleteDrag(event
->type() == ui::ET_MOUSE_RELEASED
?
484 DRAG_COMPLETE
: DRAG_REVERT
);
485 // Completing the drag may result in hiding the window. If this happens
486 // return true so no other handlers/observers see the event. Otherwise
487 // they see the event on a hidden window.
488 if (window_resizer_
&&
489 event
->type() == ui::ET_MOUSE_CAPTURE_CHANGED
&&
490 !target
->IsVisible()) {
491 event
->StopPropagation();
495 void ToplevelWindowEventHandler::HandleDrag(
496 aura::Window
* target
,
497 ui::LocatedEvent
* event
) {
498 // This function only be triggered to move window
499 // by mouse drag or touch move event.
500 DCHECK(event
->type() == ui::ET_MOUSE_DRAGGED
||
501 event
->type() == ui::ET_TOUCH_MOVED
||
502 event
->type() == ui::ET_GESTURE_SCROLL_UPDATE
);
504 // Drag actions are performed pre-target handling to prevent spurious mouse
505 // moves from the move/size operation from being sent to the target.
506 if (event
->phase() != ui::EP_PRETARGET
)
509 if (!window_resizer_
)
511 window_resizer_
->resizer()->Drag(
512 ConvertPointToParent(target
, event
->location()), event
->flags());
513 event
->StopPropagation();
516 void ToplevelWindowEventHandler::HandleMouseMoved(
517 aura::Window
* target
,
518 ui::LocatedEvent
* event
) {
519 // Shadow effects are applied after target handling. Note that we don't
520 // respect ER_HANDLED here right now since we have not had a reason to allow
521 // the target to cancel shadow rendering.
522 if (event
->phase() != ui::EP_POSTTARGET
|| !target
->delegate())
525 // TODO(jamescook): Move the resize cursor update code into here from
526 // CompoundEventFilter?
527 internal::ResizeShadowController
* controller
=
528 Shell::GetInstance()->resize_shadow_controller();
530 if (event
->flags() & ui::EF_IS_NON_CLIENT
) {
532 target
->delegate()->GetNonClientComponent(event
->location());
533 controller
->ShowShadow(target
, component
);
535 controller
->HideShadow(target
);
540 void ToplevelWindowEventHandler::HandleMouseExited(
541 aura::Window
* target
,
542 ui::LocatedEvent
* event
) {
543 // Shadow effects are applied after target handling. Note that we don't
544 // respect ER_HANDLED here right now since we have not had a reason to allow
545 // the target to cancel shadow rendering.
546 if (event
->phase() != ui::EP_POSTTARGET
)
549 internal::ResizeShadowController
* controller
=
550 Shell::GetInstance()->resize_shadow_controller();
552 controller
->HideShadow(target
);
555 void ToplevelWindowEventHandler::SetWindowShowTypeFromGesture(
556 aura::Window
* window
,
557 wm::WindowShowType new_show_type
) {
558 wm::WindowState
* window_state
= ash::wm::GetWindowState(window
);
559 switch (new_show_type
) {
560 case wm::SHOW_TYPE_MINIMIZED
:
561 if (window_state
->CanMinimize()) {
562 window_state
->Minimize();
563 window_state
->set_unminimize_to_restore_bounds(true);
564 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
567 case wm::SHOW_TYPE_MAXIMIZED
:
568 if (window_state
->CanMaximize()) {
569 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
570 window_state
->Maximize();
573 case wm::SHOW_TYPE_LEFT_SNAPPED
:
574 if (window_state
->CanSnap()) {
575 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
576 window_state
->SnapLeftWithDefaultWidth();
579 case wm::SHOW_TYPE_RIGHT_SNAPPED
:
580 if (window_state
->CanSnap()) {
581 window_state
->SetRestoreBoundsInParent(pre_drag_window_bounds_
);
582 window_state
->SnapRightWithDefaultWidth();
590 void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
591 // We explicitly don't invoke RevertDrag() since that may do things to window.
592 // Instead we destroy the resizer.
593 window_resizer_
.reset();
595 CompleteDrag(DRAG_REVERT
);