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 OnPreWindowShowTypeChange(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);
148 ToplevelWindowEventHandler::ScopedWindowResizer::OnPreWindowShowTypeChange(
149 wm::WindowState* window_state,
150 wm::WindowShowType old) {
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->handled())
188 if ((event->flags() &
189 (ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)) != 0)
192 if (in_gesture_drag_)
195 aura::Window* target = static_cast<aura::Window*>(event->target());
196 switch (event->type()) {
197 case ui::ET_MOUSE_PRESSED:
198 HandleMousePressed(target, event);
200 case ui::ET_MOUSE_DRAGGED:
201 HandleDrag(target, event);
203 case ui::ET_MOUSE_CAPTURE_CHANGED:
204 case ui::ET_MOUSE_RELEASED:
205 HandleMouseReleased(target, event);
207 case ui::ET_MOUSE_MOVED:
208 HandleMouseMoved(target, event);
210 case ui::ET_MOUSE_EXITED:
211 HandleMouseExited(target, event);
218 void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent* event) {
219 if (event->handled())
221 aura::Window* target = static_cast<aura::Window*>(event->target());
222 if (!target->delegate())
225 if (window_resizer_.get() && !in_gesture_drag_)
228 if (window_resizer_.get() &&
229 window_resizer_->resizer()->GetTarget() != target) {
233 if (event->details().touch_points() > 2) {
234 if (window_resizer_.get()) {
235 CompleteDrag(DRAG_COMPLETE);
236 event->StopPropagation();
241 switch (event->type()) {
242 case ui::ET_GESTURE_TAP_DOWN: {
243 int component = GetWindowComponent(target, *event);
244 if (!(WindowResizer::GetBoundsChangeForWindowComponent(component) &
245 WindowResizer::kBoundsChange_Resizes))
247 internal::ResizeShadowController* controller =
248 Shell::GetInstance()->resize_shadow_controller();
250 controller->ShowShadow(target, component);
253 case ui::ET_GESTURE_END: {
254 internal::ResizeShadowController* controller =
255 Shell::GetInstance()->resize_shadow_controller();
257 controller->HideShadow(target);
259 if (window_resizer_.get() &&
260 (event->details().touch_points() == 1 ||
261 !CanStartOneFingerDrag(first_finger_hittest_))) {
262 CompleteDrag(DRAG_COMPLETE);
263 event->StopPropagation();
267 case ui::ET_GESTURE_BEGIN: {
268 if (event->details().touch_points() == 1) {
269 first_finger_hittest_ = GetWindowComponent(target, *event);
270 } else if (window_resizer_.get()) {
271 if (!window_resizer_->IsMove()) {
272 // The transition from resizing with one finger to resizing with two
273 // fingers causes unintended resizing because the location of
274 // ET_GESTURE_SCROLL_UPDATE jumps from the position of the first
275 // finger to the position in the middle of the two fingers. For this
276 // reason two finger resizing is not supported.
277 CompleteDrag(DRAG_COMPLETE);
278 event->StopPropagation();
281 int second_finger_hittest = GetWindowComponent(target, *event);
282 if (CanStartTwoFingerMove(
283 target, first_finger_hittest_, second_finger_hittest)) {
284 gfx::Point location_in_parent =
285 event->details().bounding_box().CenterPoint();
286 AttemptToStartDrag(target, location_in_parent, HTCAPTION,
287 aura::client::WINDOW_MOVE_SOURCE_TOUCH);
288 event->StopPropagation();
293 case ui::ET_GESTURE_SCROLL_BEGIN: {
294 // The one finger drag is not started in ET_GESTURE_BEGIN to avoid the
295 // window jumping upon initiating a two finger drag. When a one finger
296 // drag is converted to a two finger drag, a jump occurs because the
297 // location of the ET_GESTURE_SCROLL_UPDATE event switches from the single
298 // finger's position to the position in the middle of the two fingers.
299 if (window_resizer_.get())
301 int component = GetWindowComponent(target, *event);
302 if (!CanStartOneFingerDrag(component))
304 gfx::Point location_in_parent(
305 ConvertPointToParent(target, event->location()));
306 AttemptToStartDrag(target, location_in_parent, component,
307 aura::client::WINDOW_MOVE_SOURCE_TOUCH);
308 event->StopPropagation();
315 if (!window_resizer_.get())
318 switch (event->type()) {
319 case ui::ET_GESTURE_SCROLL_UPDATE:
320 HandleDrag(target, event);
321 event->StopPropagation();
323 case ui::ET_GESTURE_SCROLL_END:
324 // We must complete the drag here instead of as a result of ET_GESTURE_END
325 // because otherwise the drag will be reverted when EndMoveLoop() is
327 // TODO(pkotwicz): Pass drag completion status to
328 // WindowMoveClient::EndMoveLoop().
329 CompleteDrag(DRAG_COMPLETE);
330 event->StopPropagation();
332 case ui::ET_SCROLL_FLING_START:
333 CompleteDrag(DRAG_COMPLETE);
335 // TODO(pkotwicz): Fix tests which inadvertantly start flings and check
336 // window_resizer_->IsMove() instead of the hittest component at |event|'s
338 if (GetWindowComponent(target, *event) != HTCAPTION ||
339 !wm::GetWindowState(target)->IsNormalShowState()) {
343 if (event->details().velocity_y() > kMinVertVelocityForWindowMinimize) {
344 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_MINIMIZED);
345 } else if (event->details().velocity_y() <
346 -kMinVertVelocityForWindowMinimize) {
347 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_MAXIMIZED);
348 } else if (event->details().velocity_x() >
349 kMinHorizVelocityForWindowSwipe) {
350 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_RIGHT_SNAPPED);
351 } else if (event->details().velocity_x() <
352 -kMinHorizVelocityForWindowSwipe) {
353 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_LEFT_SNAPPED);
355 event->StopPropagation();
357 case ui::ET_GESTURE_MULTIFINGER_SWIPE:
358 if (!wm::GetWindowState(target)->IsNormalShowState())
361 CompleteDrag(DRAG_COMPLETE);
363 if (event->details().swipe_down())
364 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_MINIMIZED);
365 else if (event->details().swipe_up())
366 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_MAXIMIZED);
367 else if (event->details().swipe_right())
368 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_RIGHT_SNAPPED);
370 SetWindowShowTypeFromGesture(target, wm::SHOW_TYPE_LEFT_SNAPPED);
371 event->StopPropagation();
378 aura::client::WindowMoveResult ToplevelWindowEventHandler::RunMoveLoop(
379 aura::Window* source,
380 const gfx::Vector2d& drag_offset,
381 aura::client::WindowMoveSource move_source) {
382 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time.
383 aura::Window* root_window = source->GetRootWindow();
385 // TODO(tdresser): Use gfx::PointF. See crbug.com/337824.
386 gfx::Point drag_location;
387 if (move_source == aura::client::WINDOW_MOVE_SOURCE_TOUCH &&
388 aura::Env::GetInstance()->is_touch_down()) {
389 gfx::PointF drag_location_f;
390 bool has_point = ui::GestureRecognizer::Get()->
391 GetLastTouchPointForTarget(source, &drag_location_f);
392 drag_location = gfx::ToFlooredPoint(drag_location_f);
395 drag_location = root_window->GetDispatcher()->GetLastMouseLocationInRoot();
396 aura::Window::ConvertPointToTarget(
397 root_window, source->parent(), &drag_location);
399 // Set the cursor before calling AttemptToStartDrag(), as that will
400 // eventually call LockCursor() and prevent the cursor from changing.
401 aura::client::CursorClient* cursor_client =
402 aura::client::GetCursorClient(root_window);
404 cursor_client->SetCursor(ui::kCursorPointer);
405 if (!AttemptToStartDrag(source, drag_location, HTCAPTION, move_source))
406 return aura::client::MOVE_CANCELED;
408 in_move_loop_ = true;
409 bool destroyed = false;
410 destroyed_ = &destroyed;
411 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
412 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
413 base::RunLoop run_loop;
414 quit_closure_ = run_loop.QuitClosure();
417 return aura::client::MOVE_CANCELED;
419 in_move_loop_ = false;
420 return drag_reverted_ ? aura::client::MOVE_CANCELED :
421 aura::client::MOVE_SUCCESSFUL;
424 void ToplevelWindowEventHandler::EndMoveLoop() {
426 CompleteDrag(DRAG_REVERT);
429 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
430 CompleteDrag(DRAG_REVERT);
433 bool ToplevelWindowEventHandler::AttemptToStartDrag(
434 aura::Window* window,
435 const gfx::Point& point_in_parent,
436 int window_component,
437 aura::client::WindowMoveSource source) {
438 if (window_resizer_.get())
440 WindowResizer* resizer = CreateWindowResizer(window, point_in_parent,
441 window_component, source).release();
445 window_resizer_.reset(new ScopedWindowResizer(this, resizer));
447 pre_drag_window_bounds_ = window->bounds();
448 in_gesture_drag_ = (source == aura::client::WINDOW_MOVE_SOURCE_TOUCH);
452 void ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status) {
453 scoped_ptr<ScopedWindowResizer> resizer(window_resizer_.release());
455 if (status == DRAG_COMPLETE)
456 resizer->resizer()->CompleteDrag();
458 resizer->resizer()->RevertDrag();
460 drag_reverted_ = (status == DRAG_REVERT);
462 first_finger_hittest_ = HTNOWHERE;
463 in_gesture_drag_ = false;
468 void ToplevelWindowEventHandler::HandleMousePressed(
469 aura::Window* target,
470 ui::MouseEvent* event) {
471 if (event->phase() != ui::EP_PRETARGET || !target->delegate())
474 // We also update the current window component here because for the
475 // mouse-drag-release-press case, where the mouse is released and
476 // pressed without mouse move event.
477 int component = GetWindowComponent(target, *event);
478 if ((event->flags() &
479 (ui::EF_IS_DOUBLE_CLICK | ui::EF_IS_TRIPLE_CLICK)) == 0 &&
480 WindowResizer::GetBoundsChangeForWindowComponent(component)) {
481 gfx::Point location_in_parent(
482 ConvertPointToParent(target, event->location()));
483 AttemptToStartDrag(target, location_in_parent, component,
484 aura::client::WINDOW_MOVE_SOURCE_MOUSE);
485 event->StopPropagation();
487 CompleteDrag(DRAG_COMPLETE);
491 void ToplevelWindowEventHandler::HandleMouseReleased(
492 aura::Window* target,
493 ui::MouseEvent* event) {
494 if (event->phase() != ui::EP_PRETARGET)
497 CompleteDrag(event->type() == ui::ET_MOUSE_RELEASED ?
498 DRAG_COMPLETE : DRAG_REVERT);
499 // Completing the drag may result in hiding the window. If this happens
500 // return true so no other handlers/observers see the event. Otherwise
501 // they see the event on a hidden window.
502 if (window_resizer_ &&
503 event->type() == ui::ET_MOUSE_CAPTURE_CHANGED &&
504 !target->IsVisible()) {
505 event->StopPropagation();
509 void ToplevelWindowEventHandler::HandleDrag(
510 aura::Window* target,
511 ui::LocatedEvent* event) {
512 // This function only be triggered to move window
513 // by mouse drag or touch move event.
514 DCHECK(event->type() == ui::ET_MOUSE_DRAGGED ||
515 event->type() == ui::ET_TOUCH_MOVED ||
516 event->type() == ui::ET_GESTURE_SCROLL_UPDATE);
518 // Drag actions are performed pre-target handling to prevent spurious mouse
519 // moves from the move/size operation from being sent to the target.
520 if (event->phase() != ui::EP_PRETARGET)
523 if (!window_resizer_)
525 window_resizer_->resizer()->Drag(
526 ConvertPointToParent(target, event->location()), event->flags());
527 event->StopPropagation();
530 void ToplevelWindowEventHandler::HandleMouseMoved(
531 aura::Window* target,
532 ui::LocatedEvent* event) {
533 // Shadow effects are applied after target handling. Note that we don't
534 // respect ER_HANDLED here right now since we have not had a reason to allow
535 // the target to cancel shadow rendering.
536 if (event->phase() != ui::EP_POSTTARGET || !target->delegate())
539 // TODO(jamescook): Move the resize cursor update code into here from
540 // CompoundEventFilter?
541 internal::ResizeShadowController* controller =
542 Shell::GetInstance()->resize_shadow_controller();
544 if (event->flags() & ui::EF_IS_NON_CLIENT) {
546 target->delegate()->GetNonClientComponent(event->location());
547 controller->ShowShadow(target, component);
549 controller->HideShadow(target);
554 void ToplevelWindowEventHandler::HandleMouseExited(
555 aura::Window* target,
556 ui::LocatedEvent* event) {
557 // Shadow effects are applied after target handling. Note that we don't
558 // respect ER_HANDLED here right now since we have not had a reason to allow
559 // the target to cancel shadow rendering.
560 if (event->phase() != ui::EP_POSTTARGET)
563 internal::ResizeShadowController* controller =
564 Shell::GetInstance()->resize_shadow_controller();
566 controller->HideShadow(target);
569 void ToplevelWindowEventHandler::SetWindowShowTypeFromGesture(
570 aura::Window* window,
571 wm::WindowShowType new_show_type) {
572 wm::WindowState* window_state = ash::wm::GetWindowState(window);
573 switch (new_show_type) {
574 case wm::SHOW_TYPE_MINIMIZED:
575 if (window_state->CanMinimize()) {
576 window_state->Minimize();
577 window_state->set_unminimize_to_restore_bounds(true);
578 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
581 case wm::SHOW_TYPE_MAXIMIZED:
582 if (window_state->CanMaximize()) {
583 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
584 window_state->Maximize();
587 case wm::SHOW_TYPE_LEFT_SNAPPED:
588 if (window_state->CanSnap()) {
589 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
590 internal::SnapSizer::SnapWindow(window_state,
591 internal::SnapSizer::LEFT_EDGE);
594 case wm::SHOW_TYPE_RIGHT_SNAPPED:
595 if (window_state->CanSnap()) {
596 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
597 internal::SnapSizer::SnapWindow(window_state,
598 internal::SnapSizer::RIGHT_EDGE);
606 void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
607 // We explicitly don't invoke RevertDrag() since that may do things to window.
608 // Instead we destroy the resizer.
609 window_resizer_.reset();
611 CompleteDrag(DRAG_REVERT);