• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "ash/wm/toplevel_window_event_handler.h"
6 
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"
31 
32 namespace {
33 const double kMinHorizVelocityForWindowSwipe = 1100;
34 const double kMinVertVelocityForWindowMinimize = 1000;
35 }
36 
37 namespace ash {
38 
39 namespace {
40 
41 // Returns whether |window| can be moved via a two finger drag given
42 // the hittest results of the two fingers.
CanStartTwoFingerMove(aura::Window * window,int window_component1,int window_component2)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;
54   }
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;
61 }
62 
63 // Returns whether |window| can be moved or resized via one finger given
64 // |window_component|.
CanStartOneFingerDrag(int window_component)65 bool CanStartOneFingerDrag(int window_component) {
66   return WindowResizer::GetBoundsChangeForWindowComponent(
67       window_component) != 0;
68 }
69 
ConvertPointToParent(aura::Window * window,const gfx::Point & point)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;
75 }
76 
77 // Returns the window component containing |event|'s location.
GetWindowComponent(aura::Window * window,const ui::LocatedEvent & event)78 int GetWindowComponent(aura::Window* window, const ui::LocatedEvent& event) {
79   return window->delegate()->GetNonClientComponent(event.location());
80 }
81 
82 }  // namespace
83 
84 // ScopedWindowResizer ---------------------------------------------------------
85 
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   virtual ~ScopedWindowResizer();
96 
97   // Returns true if the drag moves the window and does not resize.
98   bool IsMove() const;
99 
resizer()100   WindowResizer* resizer() { return resizer_.get(); }
101 
102   // WindowObserver overrides:
103   virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
104 
105   // WindowStateObserver overrides:
106   virtual void OnPreWindowStateTypeChange(wm::WindowState* window_state,
107                                           wm::WindowStateType type) OVERRIDE;
108 
109  private:
110   ToplevelWindowEventHandler* handler_;
111   scoped_ptr<WindowResizer> resizer_;
112 
113   DISALLOW_COPY_AND_ASSIGN(ScopedWindowResizer);
114 };
115 
ScopedWindowResizer(ToplevelWindowEventHandler * handler,WindowResizer * resizer)116 ToplevelWindowEventHandler::ScopedWindowResizer::ScopedWindowResizer(
117     ToplevelWindowEventHandler* handler,
118     WindowResizer* resizer)
119     : handler_(handler),
120       resizer_(resizer) {
121   resizer_->GetTarget()->AddObserver(this);
122   wm::GetWindowState(resizer_->GetTarget())->AddObserver(this);
123 }
124 
~ScopedWindowResizer()125 ToplevelWindowEventHandler::ScopedWindowResizer::~ScopedWindowResizer() {
126   resizer_->GetTarget()->RemoveObserver(this);
127   wm::GetWindowState(resizer_->GetTarget())->RemoveObserver(this);
128 }
129 
IsMove() const130 bool ToplevelWindowEventHandler::ScopedWindowResizer::IsMove() const {
131   return resizer_->details().bounds_change ==
132       WindowResizer::kBoundsChange_Repositions;
133 }
134 
135 void
OnPreWindowStateTypeChange(wm::WindowState * window_state,wm::WindowStateType old)136 ToplevelWindowEventHandler::ScopedWindowResizer::OnPreWindowStateTypeChange(
137     wm::WindowState* window_state,
138     wm::WindowStateType old) {
139   handler_->CompleteDrag(DRAG_COMPLETE);
140 }
141 
OnWindowDestroying(aura::Window * window)142 void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowDestroying(
143     aura::Window* window) {
144   DCHECK_EQ(resizer_->GetTarget(), window);
145   handler_->ResizerWindowDestroyed();
146 }
147 
148 // ToplevelWindowEventHandler --------------------------------------------------
149 
ToplevelWindowEventHandler()150 ToplevelWindowEventHandler::ToplevelWindowEventHandler()
151     : first_finger_hittest_(HTNOWHERE),
152       in_move_loop_(false),
153       in_gesture_drag_(false),
154       drag_reverted_(false),
155       destroyed_(NULL) {
156   Shell::GetInstance()->display_controller()->AddObserver(this);
157 }
158 
~ToplevelWindowEventHandler()159 ToplevelWindowEventHandler::~ToplevelWindowEventHandler() {
160   Shell::GetInstance()->display_controller()->RemoveObserver(this);
161   if (destroyed_)
162     *destroyed_ = true;
163 }
164 
OnKeyEvent(ui::KeyEvent * event)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);
169   }
170 }
171 
OnMouseEvent(ui::MouseEvent * event)172 void ToplevelWindowEventHandler::OnMouseEvent(
173     ui::MouseEvent* event) {
174   if (event->handled())
175     return;
176   if ((event->flags() &
177       (ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)) != 0)
178     return;
179 
180   if (in_gesture_drag_)
181     return;
182 
183   aura::Window* target = static_cast<aura::Window*>(event->target());
184   switch (event->type()) {
185     case ui::ET_MOUSE_PRESSED:
186       HandleMousePressed(target, event);
187       break;
188     case ui::ET_MOUSE_DRAGGED:
189       HandleDrag(target, event);
190       break;
191     case ui::ET_MOUSE_CAPTURE_CHANGED:
192     case ui::ET_MOUSE_RELEASED:
193       HandleMouseReleased(target, event);
194       break;
195     case ui::ET_MOUSE_MOVED:
196       HandleMouseMoved(target, event);
197       break;
198     case ui::ET_MOUSE_EXITED:
199       HandleMouseExited(target, event);
200       break;
201     default:
202       break;
203   }
204 }
205 
OnGestureEvent(ui::GestureEvent * event)206 void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent* event) {
207   if (event->handled())
208     return;
209   aura::Window* target = static_cast<aura::Window*>(event->target());
210   if (!target->delegate())
211     return;
212 
213   if (window_resizer_.get() && !in_gesture_drag_)
214     return;
215 
216   if (window_resizer_.get() &&
217       window_resizer_->resizer()->GetTarget() != target) {
218     return;
219   }
220 
221   if (event->details().touch_points() > 2) {
222     if (window_resizer_.get()) {
223       CompleteDrag(DRAG_COMPLETE);
224       event->StopPropagation();
225     }
226     return;
227   }
228 
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))
234         return;
235       ResizeShadowController* controller =
236           Shell::GetInstance()->resize_shadow_controller();
237       if (controller)
238         controller->ShowShadow(target, component);
239       return;
240     }
241     case ui::ET_GESTURE_END: {
242       ResizeShadowController* controller =
243           Shell::GetInstance()->resize_shadow_controller();
244       if (controller)
245         controller->HideShadow(target);
246 
247       if (window_resizer_.get() &&
248           (event->details().touch_points() == 1 ||
249            !CanStartOneFingerDrag(first_finger_hittest_))) {
250         CompleteDrag(DRAG_COMPLETE);
251         event->StopPropagation();
252       }
253       return;
254     }
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();
267         }
268       } else {
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();
277         }
278       }
279       return;
280     }
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())
288         return;
289       int component = GetWindowComponent(target, *event);
290       if (!CanStartOneFingerDrag(component))
291         return;
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();
297       return;
298     }
299     default:
300       break;
301   }
302 
303   if (!window_resizer_.get())
304     return;
305 
306   switch (event->type()) {
307     case ui::ET_GESTURE_SCROLL_UPDATE:
308       HandleDrag(target, event);
309       event->StopPropagation();
310       return;
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
314       // called.
315       // TODO(pkotwicz): Pass drag completion status to
316       // WindowMoveClient::EndMoveLoop().
317       CompleteDrag(DRAG_COMPLETE);
318       event->StopPropagation();
319       return;
320     case ui::ET_SCROLL_FLING_START:
321       CompleteDrag(DRAG_COMPLETE);
322 
323       // TODO(pkotwicz): Fix tests which inadvertantly start flings and check
324       // window_resizer_->IsMove() instead of the hittest component at |event|'s
325       // location.
326       if (GetWindowComponent(target, *event) != HTCAPTION ||
327           !wm::GetWindowState(target)->IsNormalOrSnapped()) {
328         return;
329       }
330 
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);
344       }
345       event->StopPropagation();
346       return;
347     case ui::ET_GESTURE_SWIPE:
348       DCHECK_GT(event->details().touch_points(), 0);
349       if (event->details().touch_points() == 1)
350         return;
351       if (!wm::GetWindowState(target)->IsNormalOrSnapped())
352         return;
353 
354       CompleteDrag(DRAG_COMPLETE);
355 
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);
363       } else {
364         SetWindowStateTypeFromGesture(target,
365                                       wm::WINDOW_STATE_TYPE_LEFT_SNAPPED);
366       }
367       event->StopPropagation();
368       return;
369     default:
370       return;
371   }
372 }
373 
RunMoveLoop(aura::Window * source,const gfx::Vector2d & drag_offset,aura::client::WindowMoveSource move_source)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();
380   DCHECK(root_window);
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);
389     DCHECK(has_point);
390   } else {
391     drag_location =
392         root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
393     aura::Window::ConvertPointToTarget(
394         root_window, source->parent(), &drag_location);
395   }
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);
400   if (cursor_client)
401     cursor_client->SetCursor(ui::kCursorPointer);
402   if (!AttemptToStartDrag(source, drag_location, HTCAPTION, move_source))
403     return aura::client::MOVE_CANCELED;
404 
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();
412   run_loop.Run();
413   if (destroyed)
414     return aura::client::MOVE_CANCELED;
415   destroyed_ = NULL;
416   in_move_loop_ = false;
417   return drag_reverted_ ? aura::client::MOVE_CANCELED :
418       aura::client::MOVE_SUCCESSFUL;
419 }
420 
EndMoveLoop()421 void ToplevelWindowEventHandler::EndMoveLoop() {
422   if (in_move_loop_)
423     CompleteDrag(DRAG_REVERT);
424 }
425 
OnDisplayConfigurationChanging()426 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
427   CompleteDrag(DRAG_REVERT);
428 }
429 
AttemptToStartDrag(aura::Window * window,const gfx::Point & point_in_parent,int window_component,aura::client::WindowMoveSource source)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())
436     return false;
437   WindowResizer* resizer = CreateWindowResizer(window, point_in_parent,
438       window_component, source).release();
439   if (!resizer)
440     return false;
441 
442   window_resizer_.reset(new ScopedWindowResizer(this, resizer));
443 
444   pre_drag_window_bounds_ = window->bounds();
445   in_gesture_drag_ = (source == aura::client::WINDOW_MOVE_SOURCE_TOUCH);
446   return true;
447 }
448 
CompleteDrag(DragCompletionStatus status)449 void ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status) {
450   scoped_ptr<ScopedWindowResizer> resizer(window_resizer_.release());
451   if (resizer) {
452     if (status == DRAG_COMPLETE)
453       resizer->resizer()->CompleteDrag();
454     else
455       resizer->resizer()->RevertDrag();
456   }
457   drag_reverted_ = (status == DRAG_REVERT);
458 
459   first_finger_hittest_ = HTNOWHERE;
460   in_gesture_drag_ = false;
461   if (in_move_loop_)
462     quit_closure_.Run();
463 }
464 
HandleMousePressed(aura::Window * target,ui::MouseEvent * event)465 void ToplevelWindowEventHandler::HandleMousePressed(
466     aura::Window* target,
467     ui::MouseEvent* event) {
468   if (event->phase() != ui::EP_PRETARGET || !target->delegate())
469     return;
470 
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/
484     // released pair.
485     event->SetHandled();
486   } else {
487     CompleteDrag(DRAG_COMPLETE);
488   }
489 }
490 
HandleMouseReleased(aura::Window * target,ui::MouseEvent * event)491 void ToplevelWindowEventHandler::HandleMouseReleased(
492     aura::Window* target,
493     ui::MouseEvent* event) {
494   if (event->phase() != ui::EP_PRETARGET)
495     return;
496 
497   if (window_resizer_) {
498     CompleteDrag(event->type() == ui::ET_MOUSE_RELEASED ?
499                      DRAG_COMPLETE : DRAG_REVERT);
500   }
501 
502   // Completing the drag may result in hiding the window. If this happens
503   // mark the event as handled so no other handlers/observers act upon the
504   // event. They should see the event on a hidden window, to determine targets
505   // of destructive actions such as hiding. They should not act upon them.
506   if (window_resizer_ &&
507       event->type() == ui::ET_MOUSE_CAPTURE_CHANGED &&
508       !target->IsVisible()) {
509     event->SetHandled();
510   }
511 }
512 
HandleDrag(aura::Window * target,ui::LocatedEvent * event)513 void ToplevelWindowEventHandler::HandleDrag(
514     aura::Window* target,
515     ui::LocatedEvent* event) {
516   // This function only be triggered to move window
517   // by mouse drag or touch move event.
518   DCHECK(event->type() == ui::ET_MOUSE_DRAGGED ||
519          event->type() == ui::ET_TOUCH_MOVED ||
520          event->type() == ui::ET_GESTURE_SCROLL_UPDATE);
521 
522   // Drag actions are performed pre-target handling to prevent spurious mouse
523   // moves from the move/size operation from being sent to the target.
524   if (event->phase() != ui::EP_PRETARGET)
525     return;
526 
527   if (!window_resizer_)
528     return;
529   window_resizer_->resizer()->Drag(
530       ConvertPointToParent(target, event->location()), event->flags());
531   event->StopPropagation();
532 }
533 
HandleMouseMoved(aura::Window * target,ui::LocatedEvent * event)534 void ToplevelWindowEventHandler::HandleMouseMoved(
535     aura::Window* target,
536     ui::LocatedEvent* event) {
537   // Shadow effects are applied after target handling. Note that we don't
538   // respect ER_HANDLED here right now since we have not had a reason to allow
539   // the target to cancel shadow rendering.
540   if (event->phase() != ui::EP_POSTTARGET || !target->delegate())
541     return;
542 
543   // TODO(jamescook): Move the resize cursor update code into here from
544   // CompoundEventFilter?
545   ResizeShadowController* controller =
546       Shell::GetInstance()->resize_shadow_controller();
547   if (controller) {
548     if (event->flags() & ui::EF_IS_NON_CLIENT) {
549       int component =
550           target->delegate()->GetNonClientComponent(event->location());
551       controller->ShowShadow(target, component);
552     } else {
553       controller->HideShadow(target);
554     }
555   }
556 }
557 
HandleMouseExited(aura::Window * target,ui::LocatedEvent * event)558 void ToplevelWindowEventHandler::HandleMouseExited(
559     aura::Window* target,
560     ui::LocatedEvent* event) {
561   // Shadow effects are applied after target handling. Note that we don't
562   // respect ER_HANDLED here right now since we have not had a reason to allow
563   // the target to cancel shadow rendering.
564   if (event->phase() != ui::EP_POSTTARGET)
565     return;
566 
567   ResizeShadowController* controller =
568       Shell::GetInstance()->resize_shadow_controller();
569   if (controller)
570     controller->HideShadow(target);
571 }
572 
SetWindowStateTypeFromGesture(aura::Window * window,wm::WindowStateType new_state_type)573 void ToplevelWindowEventHandler::SetWindowStateTypeFromGesture(
574     aura::Window* window,
575     wm::WindowStateType new_state_type) {
576   wm::WindowState* window_state = ash::wm::GetWindowState(window);
577   // TODO(oshima): Move extra logic (set_unminimize_to_restore_bounds,
578   // SetRestoreBoundsInParent) that modifies the window state
579   // into WindowState.
580   switch (new_state_type) {
581     case wm::WINDOW_STATE_TYPE_MINIMIZED:
582       if (window_state->CanMinimize()) {
583         window_state->Minimize();
584         window_state->set_unminimize_to_restore_bounds(true);
585         window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
586       }
587       break;
588     case wm::WINDOW_STATE_TYPE_MAXIMIZED:
589       if (window_state->CanMaximize()) {
590         window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
591         window_state->Maximize();
592       }
593       break;
594     case wm::WINDOW_STATE_TYPE_LEFT_SNAPPED:
595       if (window_state->CanSnap()) {
596         window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
597         const wm::WMEvent event(wm::WM_EVENT_SNAP_LEFT);
598         window_state->OnWMEvent(&event);
599       }
600       break;
601     case wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED:
602       if (window_state->CanSnap()) {
603         window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
604         const wm::WMEvent event(wm::WM_EVENT_SNAP_RIGHT);
605         window_state->OnWMEvent(&event);
606       }
607       break;
608     default:
609       NOTREACHED();
610   }
611 }
612 
ResizerWindowDestroyed()613 void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
614   // We explicitly don't invoke RevertDrag() since that may do things to window.
615   // Instead we destroy the resizer.
616   window_resizer_.reset();
617 
618   CompleteDrag(DRAG_REVERT);
619 }
620 
621 }  // namespace ash
622