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/workspace/snap_sizer.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/root_window.h"
19 #include "ui/aura/window.h"
20 #include "ui/aura/window_delegate.h"
21 #include "ui/aura/window_observer.h"
22 #include "ui/base/cursor/cursor.h"
23 #include "ui/base/hit_test.h"
24 #include "ui/base/ui_base_types.h"
25 #include "ui/events/event.h"
26 #include "ui/events/event_utils.h"
27 #include "ui/events/gestures/gesture_recognizer.h"
28 #include "ui/gfx/geometry/point_conversions.h"
29 #include "ui/gfx/screen.h"
32 const double kMinHorizVelocityForWindowSwipe = 1100;
33 const double kMinVertVelocityForWindowMinimize = 1000;
40 // Returns whether |window| can be moved via a two finger drag given
41 // the hittest results of the two fingers.
42 bool CanStartTwoFingerMove(aura::Window* window,
43 int window_component1,
44 int window_component2) {
45 // We allow moving a window via two fingers when the hittest components are
46 // HTCLIENT. This is done so that a window can be dragged via two fingers when
47 // the tab strip is full and hitting the caption area is difficult. We check
48 // the window type and the show state so that we do not steal touches from the
50 if (!ash::wm::GetWindowState(window)->IsNormalShowState() ||
51 window->type() != ui::wm::WINDOW_TYPE_NORMAL) {
54 int component1_behavior =
55 WindowResizer::GetBoundsChangeForWindowComponent(window_component1);
56 int component2_behavior =
57 WindowResizer::GetBoundsChangeForWindowComponent(window_component2);
58 return (component1_behavior & WindowResizer::kBoundsChange_Resizes) == 0 &&
59 (component2_behavior & WindowResizer::kBoundsChange_Resizes) == 0;
62 // Returns whether |window| can be moved or resized via one finger given
63 // |window_component|.
64 bool CanStartOneFingerDrag(int window_component) {
65 return WindowResizer::GetBoundsChangeForWindowComponent(
66 window_component) != 0;
69 gfx::Point ConvertPointToParent(aura::Window* window,
70 const gfx::Point& point) {
71 gfx::Point result(point);
72 aura::Window::ConvertPointToTarget(window, window->parent(), &result);
76 // Returns the window component containing |event|'s location.
77 int GetWindowComponent(aura::Window* window, const ui::LocatedEvent& event) {
78 return window->delegate()->GetNonClientComponent(event.location());
83 // ScopedWindowResizer ---------------------------------------------------------
85 // Wraps a WindowResizer and installs an observer on its target window. When
86 // the window is destroyed ResizerWindowDestroyed() is invoked back on the
87 // ToplevelWindowEventHandler to clean up.
88 class ToplevelWindowEventHandler::ScopedWindowResizer
89 : public aura::WindowObserver,
90 public wm::WindowStateObserver {
92 ScopedWindowResizer(ToplevelWindowEventHandler* handler,
93 WindowResizer* resizer);
94 virtual ~ScopedWindowResizer();
96 // Returns true if the drag moves the window and does not resize.
99 WindowResizer* resizer() { return resizer_.get(); }
101 // WindowObserver overrides:
102 virtual void OnWindowHierarchyChanging(
103 const HierarchyChangeParams& params) OVERRIDE;
104 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
106 // WindowStateObserver overrides:
107 virtual void OnWindowShowTypeChanged(wm::WindowState* window_state,
108 wm::WindowShowType type) OVERRIDE;
111 ToplevelWindowEventHandler* handler_;
112 scoped_ptr<WindowResizer> resizer_;
114 DISALLOW_COPY_AND_ASSIGN(ScopedWindowResizer);
117 ToplevelWindowEventHandler::ScopedWindowResizer::ScopedWindowResizer(
118 ToplevelWindowEventHandler* handler,
119 WindowResizer* resizer)
122 resizer_->GetTarget()->AddObserver(this);
123 wm::GetWindowState(resizer_->GetTarget())->AddObserver(this);
126 ToplevelWindowEventHandler::ScopedWindowResizer::~ScopedWindowResizer() {
127 resizer_->GetTarget()->RemoveObserver(this);
128 wm::GetWindowState(resizer_->GetTarget())->RemoveObserver(this);
131 bool ToplevelWindowEventHandler::ScopedWindowResizer::IsMove() const {
132 return resizer_->details().bounds_change ==
133 WindowResizer::kBoundsChange_Repositions;
136 void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowHierarchyChanging(
137 const HierarchyChangeParams& params) {
138 if (params.receiver != resizer_->GetTarget())
140 wm::WindowState* state = wm::GetWindowState(params.receiver);
141 if (state->continue_drag_after_reparent())
142 state->set_continue_drag_after_reparent(false);
144 handler_->CompleteDrag(DRAG_COMPLETE);
147 void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowShowTypeChanged(
148 wm::WindowState* window_state,
149 wm::WindowShowType old) {
150 if (!window_state->IsNormalShowState())
151 handler_->CompleteDrag(DRAG_COMPLETE);
154 void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowDestroying(
155 aura::Window* window) {
156 DCHECK_EQ(resizer_->GetTarget(), window);
157 handler_->ResizerWindowDestroyed();
160 // ToplevelWindowEventHandler --------------------------------------------------
162 ToplevelWindowEventHandler::ToplevelWindowEventHandler()
163 : first_finger_hittest_(HTNOWHERE),
164 in_move_loop_(false),
165 in_gesture_drag_(false),
166 drag_reverted_(false),
168 Shell::GetInstance()->display_controller()->AddObserver(this);
171 ToplevelWindowEventHandler::~ToplevelWindowEventHandler() {
172 Shell::GetInstance()->display_controller()->RemoveObserver(this);
177 void ToplevelWindowEventHandler::OnKeyEvent(ui::KeyEvent* event) {
178 if (window_resizer_.get() && event->type() == ui::ET_KEY_PRESSED &&
179 event->key_code() == ui::VKEY_ESCAPE) {
180 CompleteDrag(DRAG_REVERT);
184 void ToplevelWindowEventHandler::OnMouseEvent(
185 ui::MouseEvent* event) {
186 if ((event->flags() &
187 (ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)) != 0)
190 if (in_gesture_drag_)
193 aura::Window* target = static_cast<aura::Window*>(event->target());
194 switch (event->type()) {
195 case ui::ET_MOUSE_PRESSED:
196 HandleMousePressed(target, event);
198 case ui::ET_MOUSE_DRAGGED:
199 HandleDrag(target, event);
201 case ui::ET_MOUSE_CAPTURE_CHANGED:
202 case ui::ET_MOUSE_RELEASED:
203 HandleMouseReleased(target, event);
205 case ui::ET_MOUSE_MOVED:
206 HandleMouseMoved(target, event);
208 case ui::ET_MOUSE_EXITED:
209 HandleMouseExited(target, event);
216 void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent* event) {
217 aura::Window* target = static_cast<aura::Window*>(event->target());
218 if (!target->delegate())
221 if (window_resizer_.get() && !in_gesture_drag_)
224 if (window_resizer_.get() &&
225 window_resizer_->resizer()->GetTarget() != target) {
229 if (event->details().touch_points() > 2) {
230 if (window_resizer_.get()) {
231 CompleteDrag(DRAG_COMPLETE);
232 event->StopPropagation();
237 switch (event->type()) {
238 case ui::ET_GESTURE_TAP_DOWN: {
239 int component = GetWindowComponent(target, *event);
240 if (!(WindowResizer::GetBoundsChangeForWindowComponent(component) &
241 WindowResizer::kBoundsChange_Resizes))
243 internal::ResizeShadowController* controller =
244 Shell::GetInstance()->resize_shadow_controller();
246 controller->ShowShadow(target, component);
249 case ui::ET_GESTURE_END: {
250 internal::ResizeShadowController* controller =
251 Shell::GetInstance()->resize_shadow_controller();
253 controller->HideShadow(target);
255 if (window_resizer_.get() &&
256 (event->details().touch_points() == 1 ||
257 !CanStartOneFingerDrag(first_finger_hittest_))) {
258 CompleteDrag(DRAG_COMPLETE);
259 event->StopPropagation();
263 case ui::ET_GESTURE_BEGIN: {
264 if (event->details().touch_points() == 1) {
265 first_finger_hittest_ = GetWindowComponent(target, *event);
266 } else if (window_resizer_.get()) {
267 if (!window_resizer_->IsMove()) {
268 // The transition from resizing with one finger to resizing with two
269 // fingers causes unintended resizing because the location of
270 // ET_GESTURE_SCROLL_UPDATE jumps from the position of the first
271 // finger to the position in the middle of the two fingers. For this
272 // reason two finger resizing is not supported.
273 CompleteDrag(DRAG_COMPLETE);
274 event->StopPropagation();
277 int second_finger_hittest = GetWindowComponent(target, *event);
278 if (CanStartTwoFingerMove(
279 target, first_finger_hittest_, second_finger_hittest)) {
280 gfx::Point location_in_parent =
281 event->details().bounding_box().CenterPoint();
282 AttemptToStartDrag(target, location_in_parent, HTCAPTION,
283 aura::client::WINDOW_MOVE_SOURCE_TOUCH);
284 event->StopPropagation();
289 case ui::ET_GESTURE_SCROLL_BEGIN: {
290 // The one finger drag is not started in ET_GESTURE_BEGIN to avoid the
291 // window jumping upon initiating a two finger drag. When a one finger
292 // drag is converted to a two finger drag, a jump occurs because the
293 // location of the ET_GESTURE_SCROLL_UPDATE event switches from the single
294 // finger's position to the position in the middle of the two fingers.
295 if (window_resizer_.get())
297 int component = GetWindowComponent(target, *event);
298 if (!CanStartOneFingerDrag(component))
300 gfx::Point location_in_parent(
301 ConvertPointToParent(target, event->location()));
302 AttemptToStartDrag(target, location_in_parent, component,
303 aura::client::WINDOW_MOVE_SOURCE_TOUCH);
304 event->StopPropagation();
311 if (!window_resizer_.get())
314 switch (event->type()) {
315 case ui::ET_GESTURE_SCROLL_UPDATE:
316 HandleDrag(target, event);
317 event->StopPropagation();
319 case ui::ET_GESTURE_SCROLL_END:
320 // We must complete the drag here instead of as a result of ET_GESTURE_END
321 // because otherwise the drag will be reverted when EndMoveLoop() is
323 // TODO(pkotwicz): Pass drag completion status to
324 // WindowMoveClient::EndMoveLoop().
325 CompleteDrag(DRAG_COMPLETE);
326 event->StopPropagation();
328 case ui::ET_SCROLL_FLING_START:
329 CompleteDrag(DRAG_COMPLETE);
331 // TODO(pkotwicz): Fix tests which inadvertantly start flings and check
332 // window_resizer_->IsMove() instead of the hittest component at |event|'s
334 if (GetWindowComponent(target, *event) != HTCAPTION ||
335 !wm::GetWindowState(target)->IsNormalShowState()) {
339 if (event->details().velocity_y() > kMinVertVelocityForWindowMinimize) {
340 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_MINIMIZED);
341 } else if (event->details().velocity_y() <
342 -kMinVertVelocityForWindowMinimize) {
343 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_MAXIMIZED);
344 } else if (event->details().velocity_x() >
345 kMinHorizVelocityForWindowSwipe) {
346 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_RIGHT_SNAPPED);
347 } else if (event->details().velocity_x() <
348 -kMinHorizVelocityForWindowSwipe) {
349 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_LEFT_SNAPPED);
351 event->StopPropagation();
353 case ui::ET_GESTURE_MULTIFINGER_SWIPE:
354 if (!wm::GetWindowState(target)->IsNormalShowState())
357 CompleteDrag(DRAG_COMPLETE);
359 if (event->details().swipe_down())
360 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_MINIMIZED);
361 else if (event->details().swipe_up())
362 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_MAXIMIZED);
363 else if (event->details().swipe_right())
364 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_RIGHT_SNAPPED);
366 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_LEFT_SNAPPED);
367 event->StopPropagation();
374 aura::client::WindowMoveResult ToplevelWindowEventHandler::RunMoveLoop(
375 aura::Window* source,
376 const gfx::Vector2d& drag_offset,
377 aura::client::WindowMoveSource move_source) {
378 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time.
379 aura::Window* root_window = source->GetRootWindow();
381 // TODO(tdresser): Use gfx::PointF. See crbug.com/337824.
382 gfx::Point drag_location;
383 if (move_source == aura::client::WINDOW_MOVE_SOURCE_TOUCH &&
384 aura::Env::GetInstance()->is_touch_down()) {
385 gfx::PointF drag_location_f;
386 bool has_point = ui::GestureRecognizer::Get()->
387 GetLastTouchPointForTarget(source, &drag_location_f);
388 drag_location = gfx::ToFlooredPoint(drag_location_f);
391 drag_location = root_window->GetDispatcher()->GetLastMouseLocationInRoot();
392 aura::Window::ConvertPointToTarget(
393 root_window, source->parent(), &drag_location);
395 // Set the cursor before calling AttemptToStartDrag(), as that will
396 // eventually call LockCursor() and prevent the cursor from changing.
397 aura::client::CursorClient* cursor_client =
398 aura::client::GetCursorClient(root_window);
400 cursor_client->SetCursor(ui::kCursorPointer);
401 if (!AttemptToStartDrag(source, drag_location, HTCAPTION, move_source))
402 return aura::client::MOVE_CANCELED;
404 in_move_loop_ = true;
405 bool destroyed = false;
406 destroyed_ = &destroyed;
407 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
408 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
409 base::RunLoop run_loop(aura::Env::GetInstance()->GetDispatcher());
410 quit_closure_ = run_loop.QuitClosure();
413 return aura::client::MOVE_CANCELED;
415 in_move_loop_ = false;
416 return drag_reverted_ ? aura::client::MOVE_CANCELED :
417 aura::client::MOVE_SUCCESSFUL;
420 void ToplevelWindowEventHandler::EndMoveLoop() {
422 CompleteDrag(DRAG_REVERT);
425 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
426 CompleteDrag(DRAG_REVERT);
429 bool ToplevelWindowEventHandler::AttemptToStartDrag(
430 aura::Window* window,
431 const gfx::Point& point_in_parent,
432 int window_component,
433 aura::client::WindowMoveSource source) {
434 if (window_resizer_.get())
436 WindowResizer* resizer = CreateWindowResizer(window, point_in_parent,
437 window_component, source).release();
441 window_resizer_.reset(new ScopedWindowResizer(this, resizer));
443 pre_drag_window_bounds_ = window->bounds();
444 in_gesture_drag_ = (source == aura::client::WINDOW_MOVE_SOURCE_TOUCH);
448 void ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status) {
449 scoped_ptr<ScopedWindowResizer> resizer(window_resizer_.release());
451 if (status == DRAG_COMPLETE)
452 resizer->resizer()->CompleteDrag();
454 resizer->resizer()->RevertDrag();
456 drag_reverted_ = (status == DRAG_REVERT);
458 first_finger_hittest_ = HTNOWHERE;
459 in_gesture_drag_ = false;
464 void ToplevelWindowEventHandler::HandleMousePressed(
465 aura::Window* target,
466 ui::MouseEvent* event) {
467 if (event->phase() != ui::EP_PRETARGET)
470 // We also update the current window component here because for the
471 // mouse-drag-release-press case, where the mouse is released and
472 // pressed without mouse move event.
473 int component = GetWindowComponent(target, *event);
474 if ((event->flags() &
475 (ui::EF_IS_DOUBLE_CLICK | ui::EF_IS_TRIPLE_CLICK)) == 0 &&
476 WindowResizer::GetBoundsChangeForWindowComponent(component)) {
477 gfx::Point location_in_parent(
478 ConvertPointToParent(target, event->location()));
479 AttemptToStartDrag(target, location_in_parent, component,
480 aura::client::WINDOW_MOVE_SOURCE_MOUSE);
481 event->StopPropagation();
483 CompleteDrag(DRAG_COMPLETE);
487 void ToplevelWindowEventHandler::HandleMouseReleased(
488 aura::Window* target,
489 ui::MouseEvent* event) {
490 if (event->phase() != ui::EP_PRETARGET)
493 CompleteDrag(event->type() == ui::ET_MOUSE_RELEASED ?
494 DRAG_COMPLETE : DRAG_REVERT);
495 // Completing the drag may result in hiding the window. If this happens
496 // return true so no other handlers/observers see the event. Otherwise
497 // they see the event on a hidden window.
498 if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED &&
499 !target->IsVisible()) {
500 event->StopPropagation();
504 void ToplevelWindowEventHandler::HandleDrag(
505 aura::Window* target,
506 ui::LocatedEvent* event) {
507 // This function only be triggered to move window
508 // by mouse drag or touch move event.
509 DCHECK(event->type() == ui::ET_MOUSE_DRAGGED ||
510 event->type() == ui::ET_TOUCH_MOVED ||
511 event->type() == ui::ET_GESTURE_SCROLL_UPDATE);
513 // Drag actions are performed pre-target handling to prevent spurious mouse
514 // moves from the move/size operation from being sent to the target.
515 if (event->phase() != ui::EP_PRETARGET)
518 if (!window_resizer_)
520 window_resizer_->resizer()->Drag(
521 ConvertPointToParent(target, event->location()), event->flags());
522 event->StopPropagation();
525 void ToplevelWindowEventHandler::HandleMouseMoved(
526 aura::Window* target,
527 ui::LocatedEvent* event) {
528 // Shadow effects are applied after target handling. Note that we don't
529 // respect ER_HANDLED here right now since we have not had a reason to allow
530 // the target to cancel shadow rendering.
531 if (event->phase() != ui::EP_POSTTARGET)
534 // TODO(jamescook): Move the resize cursor update code into here from
535 // CompoundEventFilter?
536 internal::ResizeShadowController* controller =
537 Shell::GetInstance()->resize_shadow_controller();
539 if (event->flags() & ui::EF_IS_NON_CLIENT) {
541 target->delegate()->GetNonClientComponent(event->location());
542 controller->ShowShadow(target, component);
544 controller->HideShadow(target);
549 void ToplevelWindowEventHandler::HandleMouseExited(
550 aura::Window* target,
551 ui::LocatedEvent* event) {
552 // Shadow effects are applied after target handling. Note that we don't
553 // respect ER_HANDLED here right now since we have not had a reason to allow
554 // the target to cancel shadow rendering.
555 if (event->phase() != ui::EP_POSTTARGET)
558 internal::ResizeShadowController* controller =
559 Shell::GetInstance()->resize_shadow_controller();
561 controller->HideShadow(target);
564 void ToplevelWindowEventHandler::SetWindowShowTypeFromGesture(
565 aura::Window* window,
566 wm::WindowShowType new_show_type) {
567 wm::WindowState* window_state = ash::wm::GetWindowState(window);
568 switch (new_show_type) {
569 case wm::SHOW_TYPE_MINIMIZED:
570 if (window_state->CanMinimize()) {
571 window_state->Minimize();
572 window_state->set_always_restores_to_restore_bounds(true);
573 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
576 case wm::SHOW_TYPE_MAXIMIZED:
577 if (window_state->CanMaximize()) {
578 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
579 window_state->Maximize();
582 case wm::SHOW_TYPE_LEFT_SNAPPED:
583 if (window_state->CanSnap()) {
584 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
585 internal::SnapSizer::SnapWindow(window_state,
586 internal::SnapSizer::LEFT_EDGE);
589 case wm::SHOW_TYPE_RIGHT_SNAPPED:
590 if (window_state->CanSnap()) {
591 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
592 internal::SnapSizer::SnapWindow(window_state,
593 internal::SnapSizer::RIGHT_EDGE);
601 void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
602 // We explicitly don't invoke RevertDrag() since that may do things to window.
603 // Instead we destroy the resizer.
604 window_resizer_.reset();
606 CompleteDrag(DRAG_REVERT);