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 virtual ~ScopedWindowResizer();
97 // Returns true if the drag moves the window and does not resize.
100 WindowResizer* resizer() { return resizer_.get(); }
102 // WindowObserver overrides:
103 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
105 // WindowStateObserver overrides:
106 virtual void OnPreWindowStateTypeChange(wm::WindowState* window_state,
107 wm::WindowStateType type) OVERRIDE;
110 ToplevelWindowEventHandler* handler_;
111 scoped_ptr<WindowResizer> resizer_;
113 DISALLOW_COPY_AND_ASSIGN(ScopedWindowResizer);
116 ToplevelWindowEventHandler::ScopedWindowResizer::ScopedWindowResizer(
117 ToplevelWindowEventHandler* handler,
118 WindowResizer* resizer)
121 resizer_->GetTarget()->AddObserver(this);
122 wm::GetWindowState(resizer_->GetTarget())->AddObserver(this);
125 ToplevelWindowEventHandler::ScopedWindowResizer::~ScopedWindowResizer() {
126 resizer_->GetTarget()->RemoveObserver(this);
127 wm::GetWindowState(resizer_->GetTarget())->RemoveObserver(this);
130 bool ToplevelWindowEventHandler::ScopedWindowResizer::IsMove() const {
131 return resizer_->details().bounds_change ==
132 WindowResizer::kBoundsChange_Repositions;
136 ToplevelWindowEventHandler::ScopedWindowResizer::OnPreWindowStateTypeChange(
137 wm::WindowState* window_state,
138 wm::WindowStateType old) {
139 handler_->CompleteDrag(DRAG_COMPLETE);
142 void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowDestroying(
143 aura::Window* window) {
144 DCHECK_EQ(resizer_->GetTarget(), window);
145 handler_->ResizerWindowDestroyed();
148 // ToplevelWindowEventHandler --------------------------------------------------
150 ToplevelWindowEventHandler::ToplevelWindowEventHandler()
151 : first_finger_hittest_(HTNOWHERE),
152 in_move_loop_(false),
153 in_gesture_drag_(false),
154 drag_reverted_(false),
156 Shell::GetInstance()->display_controller()->AddObserver(this);
159 ToplevelWindowEventHandler::~ToplevelWindowEventHandler() {
160 Shell::GetInstance()->display_controller()->RemoveObserver(this);
165 void ToplevelWindowEventHandler::OnKeyEvent(ui::KeyEvent* event) {
166 if (window_resizer_.get() && event->type() == ui::ET_KEY_PRESSED &&
167 event->key_code() == ui::VKEY_ESCAPE) {
168 CompleteDrag(DRAG_REVERT);
172 void ToplevelWindowEventHandler::OnMouseEvent(
173 ui::MouseEvent* event) {
174 if (event->handled())
176 if ((event->flags() &
177 (ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)) != 0)
180 if (in_gesture_drag_)
183 aura::Window* target = static_cast<aura::Window*>(event->target());
184 switch (event->type()) {
185 case ui::ET_MOUSE_PRESSED:
186 HandleMousePressed(target, event);
188 case ui::ET_MOUSE_DRAGGED:
189 HandleDrag(target, event);
191 case ui::ET_MOUSE_CAPTURE_CHANGED:
192 case ui::ET_MOUSE_RELEASED:
193 HandleMouseReleased(target, event);
195 case ui::ET_MOUSE_MOVED:
196 HandleMouseMoved(target, event);
198 case ui::ET_MOUSE_EXITED:
199 HandleMouseExited(target, event);
206 void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent* event) {
207 if (event->handled())
209 aura::Window* target = static_cast<aura::Window*>(event->target());
210 if (!target->delegate())
213 if (window_resizer_.get() && !in_gesture_drag_)
216 if (window_resizer_.get() &&
217 window_resizer_->resizer()->GetTarget() != target) {
221 if (event->details().touch_points() > 2) {
222 if (window_resizer_.get()) {
223 CompleteDrag(DRAG_COMPLETE);
224 event->StopPropagation();
229 switch (event->type()) {
230 case ui::ET_GESTURE_TAP_DOWN: {
231 int component = GetWindowComponent(target, *event);
232 if (!(WindowResizer::GetBoundsChangeForWindowComponent(component) &
233 WindowResizer::kBoundsChange_Resizes))
235 ResizeShadowController* controller =
236 Shell::GetInstance()->resize_shadow_controller();
238 controller->ShowShadow(target, component);
241 case ui::ET_GESTURE_END: {
242 ResizeShadowController* controller =
243 Shell::GetInstance()->resize_shadow_controller();
245 controller->HideShadow(target);
247 if (window_resizer_.get() &&
248 (event->details().touch_points() == 1 ||
249 !CanStartOneFingerDrag(first_finger_hittest_))) {
250 CompleteDrag(DRAG_COMPLETE);
251 event->StopPropagation();
255 case ui::ET_GESTURE_BEGIN: {
256 if (event->details().touch_points() == 1) {
257 first_finger_hittest_ = GetWindowComponent(target, *event);
258 } else if (window_resizer_.get()) {
259 if (!window_resizer_->IsMove()) {
260 // The transition from resizing with one finger to resizing with two
261 // fingers causes unintended resizing because the location of
262 // ET_GESTURE_SCROLL_UPDATE jumps from the position of the first
263 // finger to the position in the middle of the two fingers. For this
264 // reason two finger resizing is not supported.
265 CompleteDrag(DRAG_COMPLETE);
266 event->StopPropagation();
269 int second_finger_hittest = GetWindowComponent(target, *event);
270 if (CanStartTwoFingerMove(
271 target, first_finger_hittest_, second_finger_hittest)) {
272 gfx::Point location_in_parent =
273 event->details().bounding_box().CenterPoint();
274 AttemptToStartDrag(target, location_in_parent, HTCAPTION,
275 aura::client::WINDOW_MOVE_SOURCE_TOUCH);
276 event->StopPropagation();
281 case ui::ET_GESTURE_SCROLL_BEGIN: {
282 // The one finger drag is not started in ET_GESTURE_BEGIN to avoid the
283 // window jumping upon initiating a two finger drag. When a one finger
284 // drag is converted to a two finger drag, a jump occurs because the
285 // location of the ET_GESTURE_SCROLL_UPDATE event switches from the single
286 // finger's position to the position in the middle of the two fingers.
287 if (window_resizer_.get())
289 int component = GetWindowComponent(target, *event);
290 if (!CanStartOneFingerDrag(component))
292 gfx::Point location_in_parent(
293 ConvertPointToParent(target, event->location()));
294 AttemptToStartDrag(target, location_in_parent, component,
295 aura::client::WINDOW_MOVE_SOURCE_TOUCH);
296 event->StopPropagation();
303 if (!window_resizer_.get())
306 switch (event->type()) {
307 case ui::ET_GESTURE_SCROLL_UPDATE:
308 HandleDrag(target, event);
309 event->StopPropagation();
311 case ui::ET_GESTURE_SCROLL_END:
312 // We must complete the drag here instead of as a result of ET_GESTURE_END
313 // because otherwise the drag will be reverted when EndMoveLoop() is
315 // TODO(pkotwicz): Pass drag completion status to
316 // WindowMoveClient::EndMoveLoop().
317 CompleteDrag(DRAG_COMPLETE);
318 event->StopPropagation();
320 case ui::ET_SCROLL_FLING_START:
321 CompleteDrag(DRAG_COMPLETE);
323 // TODO(pkotwicz): Fix tests which inadvertantly start flings and check
324 // window_resizer_->IsMove() instead of the hittest component at |event|'s
326 if (GetWindowComponent(target, *event) != HTCAPTION ||
327 !wm::GetWindowState(target)->IsNormalOrSnapped()) {
331 if (event->details().velocity_y() > kMinVertVelocityForWindowMinimize) {
332 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED);
333 } else if (event->details().velocity_y() <
334 -kMinVertVelocityForWindowMinimize) {
335 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED);
336 } else if (event->details().velocity_x() >
337 kMinHorizVelocityForWindowSwipe) {
338 SetWindowStateTypeFromGesture(target,
339 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED);
340 } else if (event->details().velocity_x() <
341 -kMinHorizVelocityForWindowSwipe) {
342 SetWindowStateTypeFromGesture(target,
343 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED);
345 event->StopPropagation();
347 case ui::ET_GESTURE_SWIPE:
348 DCHECK_GT(event->details().touch_points(), 0);
349 if (event->details().touch_points() == 1)
351 if (!wm::GetWindowState(target)->IsNormalOrSnapped())
354 CompleteDrag(DRAG_COMPLETE);
356 if (event->details().swipe_down()) {
357 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED);
358 } else if (event->details().swipe_up()) {
359 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED);
360 } else if (event->details().swipe_right()) {
361 SetWindowStateTypeFromGesture(target,
362 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED);
364 SetWindowStateTypeFromGesture(target,
365 wm::WINDOW_STATE_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);
392 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
393 aura::Window::ConvertPointToTarget(
394 root_window, source->parent(), &drag_location);
396 // Set the cursor before calling AttemptToStartDrag(), as that will
397 // eventually call LockCursor() and prevent the cursor from changing.
398 aura::client::CursorClient* cursor_client =
399 aura::client::GetCursorClient(root_window);
401 cursor_client->SetCursor(ui::kCursorPointer);
402 if (!AttemptToStartDrag(source, drag_location, HTCAPTION, move_source))
403 return aura::client::MOVE_CANCELED;
405 in_move_loop_ = true;
406 bool destroyed = false;
407 destroyed_ = &destroyed;
408 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
409 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
410 base::RunLoop run_loop;
411 quit_closure_ = run_loop.QuitClosure();
414 return aura::client::MOVE_CANCELED;
416 in_move_loop_ = false;
417 return drag_reverted_ ? aura::client::MOVE_CANCELED :
418 aura::client::MOVE_SUCCESSFUL;
421 void ToplevelWindowEventHandler::EndMoveLoop() {
423 CompleteDrag(DRAG_REVERT);
426 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
427 CompleteDrag(DRAG_REVERT);
430 bool ToplevelWindowEventHandler::AttemptToStartDrag(
431 aura::Window* window,
432 const gfx::Point& point_in_parent,
433 int window_component,
434 aura::client::WindowMoveSource source) {
435 if (window_resizer_.get())
437 WindowResizer* resizer = CreateWindowResizer(window, point_in_parent,
438 window_component, source).release();
442 window_resizer_.reset(new ScopedWindowResizer(this, resizer));
444 pre_drag_window_bounds_ = window->bounds();
445 in_gesture_drag_ = (source == aura::client::WINDOW_MOVE_SOURCE_TOUCH);
449 void ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status) {
450 scoped_ptr<ScopedWindowResizer> resizer(window_resizer_.release());
452 if (status == DRAG_COMPLETE)
453 resizer->resizer()->CompleteDrag();
455 resizer->resizer()->RevertDrag();
457 drag_reverted_ = (status == DRAG_REVERT);
459 first_finger_hittest_ = HTNOWHERE;
460 in_gesture_drag_ = false;
465 void ToplevelWindowEventHandler::HandleMousePressed(
466 aura::Window* target,
467 ui::MouseEvent* event) {
468 if (event->phase() != ui::EP_PRETARGET || !target->delegate())
471 // We also update the current window component here because for the
472 // mouse-drag-release-press case, where the mouse is released and
473 // pressed without mouse move event.
474 int component = GetWindowComponent(target, *event);
475 if ((event->flags() &
476 (ui::EF_IS_DOUBLE_CLICK | ui::EF_IS_TRIPLE_CLICK)) == 0 &&
477 WindowResizer::GetBoundsChangeForWindowComponent(component)) {
478 gfx::Point location_in_parent(
479 ConvertPointToParent(target, event->location()));
480 AttemptToStartDrag(target, location_in_parent, component,
481 aura::client::WINDOW_MOVE_SOURCE_MOUSE);
482 // Set as handled so that other event handlers do no act upon the event
483 // but still receive it so that they receive both parts of each pressed/
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 // mark the event as handled so no other handlers/observers act upon the
501 // event. They should see the event on a hidden window, to determine targets
502 // of destructive actions such as hiding. They should not act upon them.
503 if (window_resizer_ &&
504 event->type() == ui::ET_MOUSE_CAPTURE_CHANGED &&
505 !target->IsVisible()) {
510 void ToplevelWindowEventHandler::HandleDrag(
511 aura::Window* target,
512 ui::LocatedEvent* event) {
513 // This function only be triggered to move window
514 // by mouse drag or touch move event.
515 DCHECK(event->type() == ui::ET_MOUSE_DRAGGED ||
516 event->type() == ui::ET_TOUCH_MOVED ||
517 event->type() == ui::ET_GESTURE_SCROLL_UPDATE);
519 // Drag actions are performed pre-target handling to prevent spurious mouse
520 // moves from the move/size operation from being sent to the target.
521 if (event->phase() != ui::EP_PRETARGET)
524 if (!window_resizer_)
526 window_resizer_->resizer()->Drag(
527 ConvertPointToParent(target, event->location()), event->flags());
528 event->StopPropagation();
531 void ToplevelWindowEventHandler::HandleMouseMoved(
532 aura::Window* target,
533 ui::LocatedEvent* event) {
534 // Shadow effects are applied after target handling. Note that we don't
535 // respect ER_HANDLED here right now since we have not had a reason to allow
536 // the target to cancel shadow rendering.
537 if (event->phase() != ui::EP_POSTTARGET || !target->delegate())
540 // TODO(jamescook): Move the resize cursor update code into here from
541 // CompoundEventFilter?
542 ResizeShadowController* controller =
543 Shell::GetInstance()->resize_shadow_controller();
545 if (event->flags() & ui::EF_IS_NON_CLIENT) {
547 target->delegate()->GetNonClientComponent(event->location());
548 controller->ShowShadow(target, component);
550 controller->HideShadow(target);
555 void ToplevelWindowEventHandler::HandleMouseExited(
556 aura::Window* target,
557 ui::LocatedEvent* event) {
558 // Shadow effects are applied after target handling. Note that we don't
559 // respect ER_HANDLED here right now since we have not had a reason to allow
560 // the target to cancel shadow rendering.
561 if (event->phase() != ui::EP_POSTTARGET)
564 ResizeShadowController* controller =
565 Shell::GetInstance()->resize_shadow_controller();
567 controller->HideShadow(target);
570 void ToplevelWindowEventHandler::SetWindowStateTypeFromGesture(
571 aura::Window* window,
572 wm::WindowStateType new_state_type) {
573 wm::WindowState* window_state = ash::wm::GetWindowState(window);
574 // TODO(oshima): Move extra logic (set_unminimize_to_restore_bounds,
575 // SetRestoreBoundsInParent) that modifies the window state
577 switch (new_state_type) {
578 case wm::WINDOW_STATE_TYPE_MINIMIZED:
579 if (window_state->CanMinimize()) {
580 window_state->Minimize();
581 window_state->set_unminimize_to_restore_bounds(true);
582 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
585 case wm::WINDOW_STATE_TYPE_MAXIMIZED:
586 if (window_state->CanMaximize()) {
587 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
588 window_state->Maximize();
591 case wm::WINDOW_STATE_TYPE_LEFT_SNAPPED:
592 if (window_state->CanSnap()) {
593 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
594 const wm::WMEvent event(wm::WM_EVENT_SNAP_LEFT);
595 window_state->OnWMEvent(&event);
598 case wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED:
599 if (window_state->CanSnap()) {
600 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
601 const wm::WMEvent event(wm::WM_EVENT_SNAP_RIGHT);
602 window_state->OnWMEvent(&event);
610 void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
611 // We explicitly don't invoke RevertDrag() since that may do things to window.
612 // Instead we destroy the resizer.
613 window_resizer_.reset();
615 CompleteDrag(DRAG_REVERT);