Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / ash / wm / toplevel_window_event_handler.cc
blob884efd40b51b74108ab8609c3a3d9a37eec4407c
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"
7 #include "ash/shell.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"
32 namespace {
33 const double kMinHorizVelocityForWindowSwipe = 1100;
34 const double kMinVertVelocityForWindowMinimize = 1000;
37 namespace ash {
39 namespace {
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
50 // web contents.
51 if (!wm::GetWindowState(window)->IsNormalOrSnapped() ||
52 window->type() != ui::wm::WINDOW_TYPE_NORMAL) {
53 return false;
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);
74 return 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());
82 } // namespace
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 {
92 public:
93 ScopedWindowResizer(ToplevelWindowEventHandler* handler,
94 WindowResizer* resizer);
95 ~ScopedWindowResizer() override;
97 // Returns true if the drag moves the window and does not resize.
98 bool IsMove() const;
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;
109 private:
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)
122 : handler_(handler),
123 resizer_(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;
148 void
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),
168 destroyed_(NULL) {
169 Shell::GetInstance()->window_tree_host_manager()->AddObserver(this);
172 ToplevelWindowEventHandler::~ToplevelWindowEventHandler() {
173 Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this);
174 if (destroyed_)
175 *destroyed_ = true;
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())
188 return;
189 if ((event->flags() &
190 (ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)) != 0)
191 return;
193 if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED) {
194 // Capture is grabbed when both gesture and mouse drags start. Handle
195 // capture loss regardless of which type of drag is in progress.
196 HandleCaptureLost(event);
197 return;
200 if (in_gesture_drag_)
201 return;
203 aura::Window* target = static_cast<aura::Window*>(event->target());
204 switch (event->type()) {
205 case ui::ET_MOUSE_PRESSED:
206 HandleMousePressed(target, event);
207 break;
208 case ui::ET_MOUSE_DRAGGED:
209 HandleDrag(target, event);
210 break;
211 case ui::ET_MOUSE_RELEASED:
212 HandleMouseReleased(target, event);
213 break;
214 case ui::ET_MOUSE_MOVED:
215 HandleMouseMoved(target, event);
216 break;
217 case ui::ET_MOUSE_EXITED:
218 HandleMouseExited(target, event);
219 break;
220 default:
221 break;
225 void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent* event) {
226 if (event->handled())
227 return;
228 aura::Window* target = static_cast<aura::Window*>(event->target());
229 if (!target->delegate())
230 return;
232 if (window_resizer_.get() && !in_gesture_drag_)
233 return;
235 if (window_resizer_.get() &&
236 window_resizer_->resizer()->GetTarget() != target) {
237 return;
240 if (event->details().touch_points() > 2) {
241 if (CompleteDrag(DRAG_COMPLETE))
242 event->StopPropagation();
243 return;
246 switch (event->type()) {
247 case ui::ET_GESTURE_TAP_DOWN: {
248 int component = GetWindowComponent(target, *event);
249 if (!(WindowResizer::GetBoundsChangeForWindowComponent(component) &
250 WindowResizer::kBoundsChange_Resizes))
251 return;
252 ResizeShadowController* controller =
253 Shell::GetInstance()->resize_shadow_controller();
254 if (controller)
255 controller->ShowShadow(target, component);
256 return;
258 case ui::ET_GESTURE_END: {
259 ResizeShadowController* controller =
260 Shell::GetInstance()->resize_shadow_controller();
261 if (controller)
262 controller->HideShadow(target);
264 if (window_resizer_.get() &&
265 (event->details().touch_points() == 1 ||
266 !CanStartOneFingerDrag(first_finger_hittest_))) {
267 CompleteDrag(DRAG_COMPLETE);
268 event->StopPropagation();
270 return;
272 case ui::ET_GESTURE_BEGIN: {
273 if (event->details().touch_points() == 1) {
274 first_finger_hittest_ = GetWindowComponent(target, *event);
275 } else if (window_resizer_.get()) {
276 if (!window_resizer_->IsMove()) {
277 // The transition from resizing with one finger to resizing with two
278 // fingers causes unintended resizing because the location of
279 // ET_GESTURE_SCROLL_UPDATE jumps from the position of the first
280 // finger to the position in the middle of the two fingers. For this
281 // reason two finger resizing is not supported.
282 CompleteDrag(DRAG_COMPLETE);
283 event->StopPropagation();
285 } else {
286 int second_finger_hittest = GetWindowComponent(target, *event);
287 if (CanStartTwoFingerMove(
288 target, first_finger_hittest_, second_finger_hittest)) {
289 gfx::Point location_in_parent =
290 event->details().bounding_box().CenterPoint();
291 AttemptToStartDrag(target, location_in_parent, HTCAPTION,
292 aura::client::WINDOW_MOVE_SOURCE_TOUCH);
293 event->StopPropagation();
296 return;
298 case ui::ET_GESTURE_SCROLL_BEGIN: {
299 // The one finger drag is not started in ET_GESTURE_BEGIN to avoid the
300 // window jumping upon initiating a two finger drag. When a one finger
301 // drag is converted to a two finger drag, a jump occurs because the
302 // location of the ET_GESTURE_SCROLL_UPDATE event switches from the single
303 // finger's position to the position in the middle of the two fingers.
304 if (window_resizer_.get())
305 return;
306 int component = GetWindowComponent(target, *event);
307 if (!CanStartOneFingerDrag(component))
308 return;
309 gfx::Point location_in_parent(
310 ConvertPointToParent(target, event->location()));
311 AttemptToStartDrag(target, location_in_parent, component,
312 aura::client::WINDOW_MOVE_SOURCE_TOUCH);
313 event->StopPropagation();
314 return;
316 default:
317 break;
320 if (!window_resizer_.get())
321 return;
323 switch (event->type()) {
324 case ui::ET_GESTURE_SCROLL_UPDATE:
325 HandleDrag(target, event);
326 event->StopPropagation();
327 return;
328 case ui::ET_GESTURE_SCROLL_END:
329 // We must complete the drag here instead of as a result of ET_GESTURE_END
330 // because otherwise the drag will be reverted when EndMoveLoop() is
331 // called.
332 // TODO(pkotwicz): Pass drag completion status to
333 // WindowMoveClient::EndMoveLoop().
334 CompleteDrag(DRAG_COMPLETE);
335 event->StopPropagation();
336 return;
337 case ui::ET_SCROLL_FLING_START:
338 CompleteDrag(DRAG_COMPLETE);
340 // TODO(pkotwicz): Fix tests which inadvertantly start flings and check
341 // window_resizer_->IsMove() instead of the hittest component at |event|'s
342 // location.
343 if (GetWindowComponent(target, *event) != HTCAPTION ||
344 !wm::GetWindowState(target)->IsNormalOrSnapped()) {
345 return;
348 if (event->details().velocity_y() > kMinVertVelocityForWindowMinimize) {
349 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED);
350 } else if (event->details().velocity_y() <
351 -kMinVertVelocityForWindowMinimize) {
352 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED);
353 } else if (event->details().velocity_x() >
354 kMinHorizVelocityForWindowSwipe) {
355 SetWindowStateTypeFromGesture(target,
356 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED);
357 } else if (event->details().velocity_x() <
358 -kMinHorizVelocityForWindowSwipe) {
359 SetWindowStateTypeFromGesture(target,
360 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED);
362 event->StopPropagation();
363 return;
364 case ui::ET_GESTURE_SWIPE:
365 DCHECK_GT(event->details().touch_points(), 0);
366 if (event->details().touch_points() == 1)
367 return;
368 if (!wm::GetWindowState(target)->IsNormalOrSnapped())
369 return;
371 CompleteDrag(DRAG_COMPLETE);
373 if (event->details().swipe_down()) {
374 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED);
375 } else if (event->details().swipe_up()) {
376 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED);
377 } else if (event->details().swipe_right()) {
378 SetWindowStateTypeFromGesture(target,
379 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED);
380 } else {
381 SetWindowStateTypeFromGesture(target,
382 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED);
384 event->StopPropagation();
385 return;
386 default:
387 return;
391 aura::client::WindowMoveResult ToplevelWindowEventHandler::RunMoveLoop(
392 aura::Window* source,
393 const gfx::Vector2d& drag_offset,
394 aura::client::WindowMoveSource move_source) {
395 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time.
396 aura::Window* root_window = source->GetRootWindow();
397 DCHECK(root_window);
398 // TODO(tdresser): Use gfx::PointF. See crbug.com/337824.
399 gfx::Point drag_location;
400 if (move_source == aura::client::WINDOW_MOVE_SOURCE_TOUCH &&
401 aura::Env::GetInstance()->is_touch_down()) {
402 gfx::PointF drag_location_f;
403 bool has_point = ui::GestureRecognizer::Get()->
404 GetLastTouchPointForTarget(source, &drag_location_f);
405 drag_location = gfx::ToFlooredPoint(drag_location_f);
406 DCHECK(has_point);
407 } else {
408 drag_location =
409 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
410 aura::Window::ConvertPointToTarget(
411 root_window, source->parent(), &drag_location);
413 // Set the cursor before calling AttemptToStartDrag(), as that will
414 // eventually call LockCursor() and prevent the cursor from changing.
415 aura::client::CursorClient* cursor_client =
416 aura::client::GetCursorClient(root_window);
417 if (cursor_client)
418 cursor_client->SetCursor(ui::kCursorPointer);
419 if (!AttemptToStartDrag(source, drag_location, HTCAPTION, move_source))
420 return aura::client::MOVE_CANCELED;
422 in_move_loop_ = true;
423 bool destroyed = false;
424 destroyed_ = &destroyed;
425 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
426 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
427 base::RunLoop run_loop;
428 quit_closure_ = run_loop.QuitClosure();
429 run_loop.Run();
430 if (destroyed)
431 return aura::client::MOVE_CANCELED;
432 destroyed_ = NULL;
433 in_move_loop_ = false;
434 return drag_reverted_ ? aura::client::MOVE_CANCELED :
435 aura::client::MOVE_SUCCESSFUL;
438 void ToplevelWindowEventHandler::EndMoveLoop() {
439 if (in_move_loop_)
440 CompleteDrag(DRAG_REVERT);
443 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
444 CompleteDrag(DRAG_REVERT);
447 bool ToplevelWindowEventHandler::AttemptToStartDrag(
448 aura::Window* window,
449 const gfx::Point& point_in_parent,
450 int window_component,
451 aura::client::WindowMoveSource source) {
452 if (window_resizer_.get())
453 return false;
454 WindowResizer* resizer = CreateWindowResizer(window, point_in_parent,
455 window_component, source).release();
456 if (!resizer)
457 return false;
459 window_resizer_.reset(new ScopedWindowResizer(this, resizer));
461 pre_drag_window_bounds_ = window->bounds();
462 in_gesture_drag_ = (source == aura::client::WINDOW_MOVE_SOURCE_TOUCH);
463 return true;
466 bool ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status) {
467 if (!window_resizer_)
468 return false;
470 scoped_ptr<ScopedWindowResizer> resizer(window_resizer_.release());
471 switch (status) {
472 case DRAG_COMPLETE:
473 resizer->resizer()->CompleteDrag();
474 break;
475 case DRAG_REVERT:
476 resizer->resizer()->RevertDrag();
477 break;
478 case DRAG_RESIZER_WINDOW_DESTROYED:
479 // We explicitly do not invoke RevertDrag() since that may do things to
480 // WindowResizer::GetTarget() which was destroyed.
481 break;
483 drag_reverted_ = (status != DRAG_COMPLETE);
485 first_finger_hittest_ = HTNOWHERE;
486 in_gesture_drag_ = false;
487 if (in_move_loop_)
488 quit_closure_.Run();
489 return true;
492 void ToplevelWindowEventHandler::HandleMousePressed(
493 aura::Window* target,
494 ui::MouseEvent* event) {
495 if (event->phase() != ui::EP_PRETARGET || !target->delegate())
496 return;
498 // We also update the current window component here because for the
499 // mouse-drag-release-press case, where the mouse is released and
500 // pressed without mouse move event.
501 int component = GetWindowComponent(target, *event);
502 if ((event->flags() &
503 (ui::EF_IS_DOUBLE_CLICK | ui::EF_IS_TRIPLE_CLICK)) == 0 &&
504 WindowResizer::GetBoundsChangeForWindowComponent(component)) {
505 gfx::Point location_in_parent(
506 ConvertPointToParent(target, event->location()));
507 AttemptToStartDrag(target, location_in_parent, component,
508 aura::client::WINDOW_MOVE_SOURCE_MOUSE);
509 // Set as handled so that other event handlers do no act upon the event
510 // but still receive it so that they receive both parts of each pressed/
511 // released pair.
512 event->SetHandled();
513 } else {
514 CompleteDrag(DRAG_COMPLETE);
518 void ToplevelWindowEventHandler::HandleMouseReleased(
519 aura::Window* target,
520 ui::MouseEvent* event) {
521 if (event->phase() == ui::EP_PRETARGET)
522 CompleteDrag(DRAG_COMPLETE);
525 void ToplevelWindowEventHandler::HandleDrag(
526 aura::Window* target,
527 ui::LocatedEvent* event) {
528 // This function only be triggered to move window
529 // by mouse drag or touch move event.
530 DCHECK(event->type() == ui::ET_MOUSE_DRAGGED ||
531 event->type() == ui::ET_TOUCH_MOVED ||
532 event->type() == ui::ET_GESTURE_SCROLL_UPDATE);
534 // Drag actions are performed pre-target handling to prevent spurious mouse
535 // moves from the move/size operation from being sent to the target.
536 if (event->phase() != ui::EP_PRETARGET)
537 return;
539 if (!window_resizer_)
540 return;
541 window_resizer_->resizer()->Drag(
542 ConvertPointToParent(target, event->location()), event->flags());
543 event->StopPropagation();
546 void ToplevelWindowEventHandler::HandleMouseMoved(
547 aura::Window* target,
548 ui::LocatedEvent* event) {
549 // Shadow effects are applied after target handling. Note that we don't
550 // respect ER_HANDLED here right now since we have not had a reason to allow
551 // the target to cancel shadow rendering.
552 if (event->phase() != ui::EP_POSTTARGET || !target->delegate())
553 return;
555 // TODO(jamescook): Move the resize cursor update code into here from
556 // CompoundEventFilter?
557 ResizeShadowController* controller =
558 Shell::GetInstance()->resize_shadow_controller();
559 if (controller) {
560 if (event->flags() & ui::EF_IS_NON_CLIENT) {
561 int component =
562 target->delegate()->GetNonClientComponent(event->location());
563 controller->ShowShadow(target, component);
564 } else {
565 controller->HideShadow(target);
570 void ToplevelWindowEventHandler::HandleMouseExited(
571 aura::Window* target,
572 ui::LocatedEvent* event) {
573 // Shadow effects are applied after target handling. Note that we don't
574 // respect ER_HANDLED here right now since we have not had a reason to allow
575 // the target to cancel shadow rendering.
576 if (event->phase() != ui::EP_POSTTARGET)
577 return;
579 ResizeShadowController* controller =
580 Shell::GetInstance()->resize_shadow_controller();
581 if (controller)
582 controller->HideShadow(target);
585 void ToplevelWindowEventHandler::HandleCaptureLost(ui::LocatedEvent* event) {
586 if (event->phase() == ui::EP_PRETARGET)
587 CompleteDrag(DRAG_REVERT);
590 void ToplevelWindowEventHandler::SetWindowStateTypeFromGesture(
591 aura::Window* window,
592 wm::WindowStateType new_state_type) {
593 wm::WindowState* window_state = ash::wm::GetWindowState(window);
594 // TODO(oshima): Move extra logic (set_unminimize_to_restore_bounds,
595 // SetRestoreBoundsInParent) that modifies the window state
596 // into WindowState.
597 switch (new_state_type) {
598 case wm::WINDOW_STATE_TYPE_MINIMIZED:
599 if (window_state->CanMinimize()) {
600 window_state->Minimize();
601 window_state->set_unminimize_to_restore_bounds(true);
602 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
604 break;
605 case wm::WINDOW_STATE_TYPE_MAXIMIZED:
606 if (window_state->CanMaximize()) {
607 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
608 window_state->Maximize();
610 break;
611 case wm::WINDOW_STATE_TYPE_LEFT_SNAPPED:
612 if (window_state->CanSnap()) {
613 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
614 const wm::WMEvent event(wm::WM_EVENT_SNAP_LEFT);
615 window_state->OnWMEvent(&event);
617 break;
618 case wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED:
619 if (window_state->CanSnap()) {
620 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
621 const wm::WMEvent event(wm::WM_EVENT_SNAP_RIGHT);
622 window_state->OnWMEvent(&event);
624 break;
625 default:
626 NOTREACHED();
630 void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
631 CompleteDrag(DRAG_RESIZER_WINDOW_DESTROYED);
634 } // namespace ash