Fix translation extraction failure.
[chromium-blink-merge.git] / ash / wm / toplevel_window_event_handler.cc
blobecb4b3be9886db56d78befdec9b5b7d66d916d9b
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 gfx::Point drag_location;
399 if (move_source == aura::client::WINDOW_MOVE_SOURCE_TOUCH &&
400 aura::Env::GetInstance()->is_touch_down()) {
401 gfx::PointF drag_location_f;
402 bool has_point = ui::GestureRecognizer::Get()->
403 GetLastTouchPointForTarget(source, &drag_location_f);
404 drag_location = gfx::ToFlooredPoint(drag_location_f);
405 DCHECK(has_point);
406 } else {
407 drag_location =
408 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
409 aura::Window::ConvertPointToTarget(
410 root_window, source->parent(), &drag_location);
412 // Set the cursor before calling AttemptToStartDrag(), as that will
413 // eventually call LockCursor() and prevent the cursor from changing.
414 aura::client::CursorClient* cursor_client =
415 aura::client::GetCursorClient(root_window);
416 if (cursor_client)
417 cursor_client->SetCursor(ui::kCursorPointer);
418 if (!AttemptToStartDrag(source, drag_location, HTCAPTION, move_source))
419 return aura::client::MOVE_CANCELED;
421 in_move_loop_ = true;
422 bool destroyed = false;
423 destroyed_ = &destroyed;
424 base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
425 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
426 base::RunLoop run_loop;
427 quit_closure_ = run_loop.QuitClosure();
428 run_loop.Run();
429 if (destroyed)
430 return aura::client::MOVE_CANCELED;
431 destroyed_ = NULL;
432 in_move_loop_ = false;
433 return drag_reverted_ ? aura::client::MOVE_CANCELED :
434 aura::client::MOVE_SUCCESSFUL;
437 void ToplevelWindowEventHandler::EndMoveLoop() {
438 if (in_move_loop_)
439 CompleteDrag(DRAG_REVERT);
442 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
443 CompleteDrag(DRAG_REVERT);
446 bool ToplevelWindowEventHandler::AttemptToStartDrag(
447 aura::Window* window,
448 const gfx::Point& point_in_parent,
449 int window_component,
450 aura::client::WindowMoveSource source) {
451 if (window_resizer_.get())
452 return false;
453 WindowResizer* resizer = CreateWindowResizer(window, point_in_parent,
454 window_component, source).release();
455 if (!resizer)
456 return false;
458 window_resizer_.reset(new ScopedWindowResizer(this, resizer));
460 pre_drag_window_bounds_ = window->bounds();
461 in_gesture_drag_ = (source == aura::client::WINDOW_MOVE_SOURCE_TOUCH);
462 return true;
465 bool ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status) {
466 if (!window_resizer_)
467 return false;
469 scoped_ptr<ScopedWindowResizer> resizer(window_resizer_.release());
470 switch (status) {
471 case DRAG_COMPLETE:
472 resizer->resizer()->CompleteDrag();
473 break;
474 case DRAG_REVERT:
475 resizer->resizer()->RevertDrag();
476 break;
477 case DRAG_RESIZER_WINDOW_DESTROYED:
478 // We explicitly do not invoke RevertDrag() since that may do things to
479 // WindowResizer::GetTarget() which was destroyed.
480 break;
482 drag_reverted_ = (status != DRAG_COMPLETE);
484 first_finger_hittest_ = HTNOWHERE;
485 in_gesture_drag_ = false;
486 if (in_move_loop_)
487 quit_closure_.Run();
488 return true;
491 void ToplevelWindowEventHandler::HandleMousePressed(
492 aura::Window* target,
493 ui::MouseEvent* event) {
494 if (event->phase() != ui::EP_PRETARGET || !target->delegate())
495 return;
497 // We also update the current window component here because for the
498 // mouse-drag-release-press case, where the mouse is released and
499 // pressed without mouse move event.
500 int component = GetWindowComponent(target, *event);
501 if ((event->flags() &
502 (ui::EF_IS_DOUBLE_CLICK | ui::EF_IS_TRIPLE_CLICK)) == 0 &&
503 WindowResizer::GetBoundsChangeForWindowComponent(component)) {
504 gfx::Point location_in_parent(
505 ConvertPointToParent(target, event->location()));
506 AttemptToStartDrag(target, location_in_parent, component,
507 aura::client::WINDOW_MOVE_SOURCE_MOUSE);
508 // Set as handled so that other event handlers do no act upon the event
509 // but still receive it so that they receive both parts of each pressed/
510 // released pair.
511 event->SetHandled();
512 } else {
513 CompleteDrag(DRAG_COMPLETE);
517 void ToplevelWindowEventHandler::HandleMouseReleased(
518 aura::Window* target,
519 ui::MouseEvent* event) {
520 if (event->phase() == ui::EP_PRETARGET)
521 CompleteDrag(DRAG_COMPLETE);
524 void ToplevelWindowEventHandler::HandleDrag(
525 aura::Window* target,
526 ui::LocatedEvent* event) {
527 // This function only be triggered to move window
528 // by mouse drag or touch move event.
529 DCHECK(event->type() == ui::ET_MOUSE_DRAGGED ||
530 event->type() == ui::ET_TOUCH_MOVED ||
531 event->type() == ui::ET_GESTURE_SCROLL_UPDATE);
533 // Drag actions are performed pre-target handling to prevent spurious mouse
534 // moves from the move/size operation from being sent to the target.
535 if (event->phase() != ui::EP_PRETARGET)
536 return;
538 if (!window_resizer_)
539 return;
540 window_resizer_->resizer()->Drag(
541 ConvertPointToParent(target, event->location()), event->flags());
542 event->StopPropagation();
545 void ToplevelWindowEventHandler::HandleMouseMoved(
546 aura::Window* target,
547 ui::LocatedEvent* event) {
548 // Shadow effects are applied after target handling. Note that we don't
549 // respect ER_HANDLED here right now since we have not had a reason to allow
550 // the target to cancel shadow rendering.
551 if (event->phase() != ui::EP_POSTTARGET || !target->delegate())
552 return;
554 // TODO(jamescook): Move the resize cursor update code into here from
555 // CompoundEventFilter?
556 ResizeShadowController* controller =
557 Shell::GetInstance()->resize_shadow_controller();
558 if (controller) {
559 if (event->flags() & ui::EF_IS_NON_CLIENT) {
560 int component =
561 target->delegate()->GetNonClientComponent(event->location());
562 controller->ShowShadow(target, component);
563 } else {
564 controller->HideShadow(target);
569 void ToplevelWindowEventHandler::HandleMouseExited(
570 aura::Window* target,
571 ui::LocatedEvent* event) {
572 // Shadow effects are applied after target handling. Note that we don't
573 // respect ER_HANDLED here right now since we have not had a reason to allow
574 // the target to cancel shadow rendering.
575 if (event->phase() != ui::EP_POSTTARGET)
576 return;
578 ResizeShadowController* controller =
579 Shell::GetInstance()->resize_shadow_controller();
580 if (controller)
581 controller->HideShadow(target);
584 void ToplevelWindowEventHandler::HandleCaptureLost(ui::LocatedEvent* event) {
585 if (event->phase() == ui::EP_PRETARGET)
586 CompleteDrag(DRAG_REVERT);
589 void ToplevelWindowEventHandler::SetWindowStateTypeFromGesture(
590 aura::Window* window,
591 wm::WindowStateType new_state_type) {
592 wm::WindowState* window_state = ash::wm::GetWindowState(window);
593 // TODO(oshima): Move extra logic (set_unminimize_to_restore_bounds,
594 // SetRestoreBoundsInParent) that modifies the window state
595 // into WindowState.
596 switch (new_state_type) {
597 case wm::WINDOW_STATE_TYPE_MINIMIZED:
598 if (window_state->CanMinimize()) {
599 window_state->Minimize();
600 window_state->set_unminimize_to_restore_bounds(true);
601 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
603 break;
604 case wm::WINDOW_STATE_TYPE_MAXIMIZED:
605 if (window_state->CanMaximize()) {
606 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
607 window_state->Maximize();
609 break;
610 case wm::WINDOW_STATE_TYPE_LEFT_SNAPPED:
611 if (window_state->CanSnap()) {
612 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
613 const wm::WMEvent event(wm::WM_EVENT_SNAP_LEFT);
614 window_state->OnWMEvent(&event);
616 break;
617 case wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED:
618 if (window_state->CanSnap()) {
619 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
620 const wm::WMEvent event(wm::WM_EVENT_SNAP_RIGHT);
621 window_state->OnWMEvent(&event);
623 break;
624 default:
625 NOTREACHED();
629 void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
630 CompleteDrag(DRAG_RESIZER_WINDOW_DESTROYED);
633 } // namespace ash