| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/wm/toplevel_window_event_handler.h" | 5 #include "ash/wm/toplevel_window_event_handler.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ash/wm/aura/wm_window_aura.h" | 8 #include "ash/wm/aura/wm_window_aura.h" |
| 9 #include "ash/wm/common/window_resizer.h" | |
| 10 #include "ash/wm/common/window_state.h" | |
| 11 #include "ash/wm/common/window_state_observer.h" | |
| 12 #include "ash/wm/common/wm_event.h" | |
| 13 #include "ash/wm/common/wm_window_observer.h" | |
| 14 #include "ash/wm/resize_shadow_controller.h" | |
| 15 #include "ash/wm/window_state_aura.h" | |
| 16 #include "ash/wm/window_util.h" | |
| 17 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 18 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 19 #include "ui/aura/client/cursor_client.h" | 11 #include "ui/aura/client/cursor_client.h" |
| 20 #include "ui/aura/env.h" | 12 #include "ui/aura/env.h" |
| 21 #include "ui/aura/window.h" | 13 #include "ui/aura/window.h" |
| 22 #include "ui/aura/window_delegate.h" | |
| 23 #include "ui/aura/window_event_dispatcher.h" | 14 #include "ui/aura/window_event_dispatcher.h" |
| 24 #include "ui/aura/window_observer.h" | |
| 25 #include "ui/aura/window_tree_host.h" | 15 #include "ui/aura/window_tree_host.h" |
| 26 #include "ui/base/cursor/cursor.h" | 16 #include "ui/base/cursor/cursor.h" |
| 27 #include "ui/base/hit_test.h" | 17 #include "ui/base/hit_test.h" |
| 28 #include "ui/base/ui_base_types.h" | |
| 29 #include "ui/events/event.h" | 18 #include "ui/events/event.h" |
| 30 #include "ui/events/event_utils.h" | 19 #include "ui/events/event_utils.h" |
| 31 #include "ui/events/gestures/gesture_recognizer.h" | 20 #include "ui/events/gestures/gesture_recognizer.h" |
| 32 #include "ui/gfx/geometry/point_conversions.h" | |
| 33 | |
| 34 namespace { | |
| 35 const double kMinHorizVelocityForWindowSwipe = 1100; | |
| 36 const double kMinVertVelocityForWindowMinimize = 1000; | |
| 37 } | |
| 38 | 21 |
| 39 namespace ash { | 22 namespace ash { |
| 40 | 23 |
| 41 namespace { | 24 ToplevelWindowEventHandler::ToplevelWindowEventHandler(wm::WmGlobals* globals) |
| 25 : wm_toplevel_window_event_handler_(globals), weak_factory_(this) {} |
| 42 | 26 |
| 43 // Returns whether |window| can be moved via a two finger drag given | 27 ToplevelWindowEventHandler::~ToplevelWindowEventHandler() {} |
| 44 // the hittest results of the two fingers. | 28 |
| 45 bool CanStartTwoFingerMove(aura::Window* window, | 29 void ToplevelWindowEventHandler::OnKeyEvent(ui::KeyEvent* event) { |
| 46 int window_component1, | 30 wm_toplevel_window_event_handler_.OnKeyEvent(event); |
| 47 int window_component2) { | |
| 48 // We allow moving a window via two fingers when the hittest components are | |
| 49 // HTCLIENT. This is done so that a window can be dragged via two fingers when | |
| 50 // the tab strip is full and hitting the caption area is difficult. We check | |
| 51 // the window type and the state type so that we do not steal touches from the | |
| 52 // web contents. | |
| 53 if (!wm::GetWindowState(window)->IsNormalOrSnapped() || | |
| 54 window->type() != ui::wm::WINDOW_TYPE_NORMAL) { | |
| 55 return false; | |
| 56 } | |
| 57 int component1_behavior = | |
| 58 WindowResizer::GetBoundsChangeForWindowComponent(window_component1); | |
| 59 int component2_behavior = | |
| 60 WindowResizer::GetBoundsChangeForWindowComponent(window_component2); | |
| 61 return (component1_behavior & WindowResizer::kBoundsChange_Resizes) == 0 && | |
| 62 (component2_behavior & WindowResizer::kBoundsChange_Resizes) == 0; | |
| 63 } | 31 } |
| 64 | 32 |
| 65 // Returns whether |window| can be moved or resized via one finger given | 33 void ToplevelWindowEventHandler::OnMouseEvent(ui::MouseEvent* event) { |
| 66 // |window_component|. | |
| 67 bool CanStartOneFingerDrag(int window_component) { | |
| 68 return WindowResizer::GetBoundsChangeForWindowComponent( | |
| 69 window_component) != 0; | |
| 70 } | |
| 71 | |
| 72 gfx::Point ConvertPointToParent(aura::Window* window, | |
| 73 const gfx::Point& point) { | |
| 74 gfx::Point result(point); | |
| 75 aura::Window::ConvertPointToTarget(window, window->parent(), &result); | |
| 76 return result; | |
| 77 } | |
| 78 | |
| 79 // Returns the window component containing |event|'s location. | |
| 80 int GetWindowComponent(aura::Window* window, const ui::LocatedEvent& event) { | |
| 81 return window->delegate()->GetNonClientComponent(event.location()); | |
| 82 } | |
| 83 | |
| 84 } // namespace | |
| 85 | |
| 86 // ScopedWindowResizer --------------------------------------------------------- | |
| 87 | |
| 88 // Wraps a WindowResizer and installs an observer on its target window. When | |
| 89 // the window is destroyed ResizerWindowDestroyed() is invoked back on the | |
| 90 // ToplevelWindowEventHandler to clean up. | |
| 91 class ToplevelWindowEventHandler::ScopedWindowResizer | |
| 92 : public wm::WmWindowObserver, | |
| 93 public wm::WindowStateObserver { | |
| 94 public: | |
| 95 ScopedWindowResizer(ToplevelWindowEventHandler* handler, | |
| 96 WindowResizer* resizer); | |
| 97 ~ScopedWindowResizer() override; | |
| 98 | |
| 99 // Returns true if the drag moves the window and does not resize. | |
| 100 bool IsMove() const; | |
| 101 | |
| 102 WindowResizer* resizer() { return resizer_.get(); } | |
| 103 | |
| 104 // WindowObserver overrides: | |
| 105 void OnWindowDestroying(wm::WmWindow* window) override; | |
| 106 | |
| 107 // WindowStateObserver overrides: | |
| 108 void OnPreWindowStateTypeChange(wm::WindowState* window_state, | |
| 109 wm::WindowStateType type) override; | |
| 110 | |
| 111 private: | |
| 112 ToplevelWindowEventHandler* handler_; | |
| 113 std::unique_ptr<WindowResizer> resizer_; | |
| 114 | |
| 115 // Whether ScopedWindowResizer grabbed capture. | |
| 116 bool grabbed_capture_; | |
| 117 | |
| 118 DISALLOW_COPY_AND_ASSIGN(ScopedWindowResizer); | |
| 119 }; | |
| 120 | |
| 121 ToplevelWindowEventHandler::ScopedWindowResizer::ScopedWindowResizer( | |
| 122 ToplevelWindowEventHandler* handler, | |
| 123 WindowResizer* resizer) | |
| 124 : handler_(handler), | |
| 125 resizer_(resizer), | |
| 126 grabbed_capture_(false) { | |
| 127 wm::WmWindow* target = resizer_->GetTarget(); | |
| 128 target->AddObserver(this); | |
| 129 target->GetWindowState()->AddObserver(this); | |
| 130 | |
| 131 if (!target->HasCapture()) { | |
| 132 grabbed_capture_ = true; | |
| 133 target->SetCapture(); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 ToplevelWindowEventHandler::ScopedWindowResizer::~ScopedWindowResizer() { | |
| 138 wm::WmWindow* target = resizer_->GetTarget(); | |
| 139 target->RemoveObserver(this); | |
| 140 target->GetWindowState()->RemoveObserver(this); | |
| 141 if (grabbed_capture_) | |
| 142 target->ReleaseCapture(); | |
| 143 } | |
| 144 | |
| 145 bool ToplevelWindowEventHandler::ScopedWindowResizer::IsMove() const { | |
| 146 return resizer_->details().bounds_change == | |
| 147 WindowResizer::kBoundsChange_Repositions; | |
| 148 } | |
| 149 | |
| 150 void | |
| 151 ToplevelWindowEventHandler::ScopedWindowResizer::OnPreWindowStateTypeChange( | |
| 152 wm::WindowState* window_state, | |
| 153 wm::WindowStateType old) { | |
| 154 handler_->CompleteDrag(DRAG_COMPLETE); | |
| 155 } | |
| 156 | |
| 157 void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowDestroying( | |
| 158 wm::WmWindow* window) { | |
| 159 DCHECK_EQ(resizer_->GetTarget(), window); | |
| 160 handler_->ResizerWindowDestroyed(); | |
| 161 } | |
| 162 | |
| 163 // ToplevelWindowEventHandler -------------------------------------------------- | |
| 164 | |
| 165 ToplevelWindowEventHandler::ToplevelWindowEventHandler() | |
| 166 : first_finger_hittest_(HTNOWHERE), | |
| 167 in_move_loop_(false), | |
| 168 in_gesture_drag_(false), | |
| 169 drag_reverted_(false), | |
| 170 destroyed_(NULL) { | |
| 171 Shell::GetInstance()->window_tree_host_manager()->AddObserver(this); | |
| 172 } | |
| 173 | |
| 174 ToplevelWindowEventHandler::~ToplevelWindowEventHandler() { | |
| 175 Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this); | |
| 176 if (destroyed_) | |
| 177 *destroyed_ = true; | |
| 178 } | |
| 179 | |
| 180 void ToplevelWindowEventHandler::OnKeyEvent(ui::KeyEvent* event) { | |
| 181 if (window_resizer_.get() && event->type() == ui::ET_KEY_PRESSED && | |
| 182 event->key_code() == ui::VKEY_ESCAPE) { | |
| 183 CompleteDrag(DRAG_REVERT); | |
| 184 } | |
| 185 } | |
| 186 | |
| 187 void ToplevelWindowEventHandler::OnMouseEvent( | |
| 188 ui::MouseEvent* event) { | |
| 189 if (event->handled()) | |
| 190 return; | |
| 191 if ((event->flags() & | |
| 192 (ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)) != 0) | |
| 193 return; | |
| 194 | |
| 195 if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED) { | |
| 196 // Capture is grabbed when both gesture and mouse drags start. Handle | |
| 197 // capture loss regardless of which type of drag is in progress. | |
| 198 HandleCaptureLost(event); | |
| 199 return; | |
| 200 } | |
| 201 | |
| 202 if (in_gesture_drag_) | |
| 203 return; | |
| 204 | |
| 205 aura::Window* target = static_cast<aura::Window*>(event->target()); | 34 aura::Window* target = static_cast<aura::Window*>(event->target()); |
| 206 switch (event->type()) { | 35 wm_toplevel_window_event_handler_.OnMouseEvent(event, |
| 207 case ui::ET_MOUSE_PRESSED: | 36 wm::WmWindowAura::Get(target)); |
| 208 HandleMousePressed(target, event); | |
| 209 break; | |
| 210 case ui::ET_MOUSE_DRAGGED: | |
| 211 HandleDrag(target, event); | |
| 212 break; | |
| 213 case ui::ET_MOUSE_RELEASED: | |
| 214 HandleMouseReleased(target, event); | |
| 215 break; | |
| 216 case ui::ET_MOUSE_MOVED: | |
| 217 HandleMouseMoved(target, event); | |
| 218 break; | |
| 219 case ui::ET_MOUSE_EXITED: | |
| 220 HandleMouseExited(target, event); | |
| 221 break; | |
| 222 default: | |
| 223 break; | |
| 224 } | |
| 225 } | 37 } |
| 226 | 38 |
| 227 void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent* event) { | 39 void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent* event) { |
| 228 if (event->handled()) | |
| 229 return; | |
| 230 aura::Window* target = static_cast<aura::Window*>(event->target()); | 40 aura::Window* target = static_cast<aura::Window*>(event->target()); |
| 231 if (!target->delegate()) | 41 wm_toplevel_window_event_handler_.OnGestureEvent( |
| 232 return; | 42 event, wm::WmWindowAura::Get(target)); |
| 233 | |
| 234 if (window_resizer_.get() && !in_gesture_drag_) | |
| 235 return; | |
| 236 | |
| 237 if (window_resizer_.get() && | |
| 238 wm::WmWindowAura::GetAuraWindow( | |
| 239 window_resizer_->resizer()->GetTarget()) != target) { | |
| 240 return; | |
| 241 } | |
| 242 | |
| 243 if (event->details().touch_points() > 2) { | |
| 244 if (CompleteDrag(DRAG_COMPLETE)) | |
| 245 event->StopPropagation(); | |
| 246 return; | |
| 247 } | |
| 248 | |
| 249 switch (event->type()) { | |
| 250 case ui::ET_GESTURE_TAP_DOWN: { | |
| 251 int component = GetWindowComponent(target, *event); | |
| 252 if (!(WindowResizer::GetBoundsChangeForWindowComponent(component) & | |
| 253 WindowResizer::kBoundsChange_Resizes)) | |
| 254 return; | |
| 255 ResizeShadowController* controller = | |
| 256 Shell::GetInstance()->resize_shadow_controller(); | |
| 257 if (controller) | |
| 258 controller->ShowShadow(target, component); | |
| 259 return; | |
| 260 } | |
| 261 case ui::ET_GESTURE_END: { | |
| 262 ResizeShadowController* controller = | |
| 263 Shell::GetInstance()->resize_shadow_controller(); | |
| 264 if (controller) | |
| 265 controller->HideShadow(target); | |
| 266 | |
| 267 if (window_resizer_.get() && | |
| 268 (event->details().touch_points() == 1 || | |
| 269 !CanStartOneFingerDrag(first_finger_hittest_))) { | |
| 270 CompleteDrag(DRAG_COMPLETE); | |
| 271 event->StopPropagation(); | |
| 272 } | |
| 273 return; | |
| 274 } | |
| 275 case ui::ET_GESTURE_BEGIN: { | |
| 276 if (event->details().touch_points() == 1) { | |
| 277 first_finger_hittest_ = GetWindowComponent(target, *event); | |
| 278 } else if (window_resizer_.get()) { | |
| 279 if (!window_resizer_->IsMove()) { | |
| 280 // The transition from resizing with one finger to resizing with two | |
| 281 // fingers causes unintended resizing because the location of | |
| 282 // ET_GESTURE_SCROLL_UPDATE jumps from the position of the first | |
| 283 // finger to the position in the middle of the two fingers. For this | |
| 284 // reason two finger resizing is not supported. | |
| 285 CompleteDrag(DRAG_COMPLETE); | |
| 286 event->StopPropagation(); | |
| 287 } | |
| 288 } else { | |
| 289 int second_finger_hittest = GetWindowComponent(target, *event); | |
| 290 if (CanStartTwoFingerMove( | |
| 291 target, first_finger_hittest_, second_finger_hittest)) { | |
| 292 gfx::Point location_in_parent = | |
| 293 event->details().bounding_box().CenterPoint(); | |
| 294 AttemptToStartDrag(target, location_in_parent, HTCAPTION, | |
| 295 aura::client::WINDOW_MOVE_SOURCE_TOUCH); | |
| 296 event->StopPropagation(); | |
| 297 } | |
| 298 } | |
| 299 return; | |
| 300 } | |
| 301 case ui::ET_GESTURE_SCROLL_BEGIN: { | |
| 302 // The one finger drag is not started in ET_GESTURE_BEGIN to avoid the | |
| 303 // window jumping upon initiating a two finger drag. When a one finger | |
| 304 // drag is converted to a two finger drag, a jump occurs because the | |
| 305 // location of the ET_GESTURE_SCROLL_UPDATE event switches from the single | |
| 306 // finger's position to the position in the middle of the two fingers. | |
| 307 if (window_resizer_.get()) | |
| 308 return; | |
| 309 int component = GetWindowComponent(target, *event); | |
| 310 if (!CanStartOneFingerDrag(component)) | |
| 311 return; | |
| 312 gfx::Point location_in_parent( | |
| 313 ConvertPointToParent(target, event->location())); | |
| 314 AttemptToStartDrag(target, location_in_parent, component, | |
| 315 aura::client::WINDOW_MOVE_SOURCE_TOUCH); | |
| 316 event->StopPropagation(); | |
| 317 return; | |
| 318 } | |
| 319 default: | |
| 320 break; | |
| 321 } | |
| 322 | |
| 323 if (!window_resizer_.get()) | |
| 324 return; | |
| 325 | |
| 326 switch (event->type()) { | |
| 327 case ui::ET_GESTURE_SCROLL_UPDATE: | |
| 328 HandleDrag(target, event); | |
| 329 event->StopPropagation(); | |
| 330 return; | |
| 331 case ui::ET_GESTURE_SCROLL_END: | |
| 332 // We must complete the drag here instead of as a result of ET_GESTURE_END | |
| 333 // because otherwise the drag will be reverted when EndMoveLoop() is | |
| 334 // called. | |
| 335 // TODO(pkotwicz): Pass drag completion status to | |
| 336 // WindowMoveClient::EndMoveLoop(). | |
| 337 CompleteDrag(DRAG_COMPLETE); | |
| 338 event->StopPropagation(); | |
| 339 return; | |
| 340 case ui::ET_SCROLL_FLING_START: | |
| 341 CompleteDrag(DRAG_COMPLETE); | |
| 342 | |
| 343 // TODO(pkotwicz): Fix tests which inadvertantly start flings and check | |
| 344 // window_resizer_->IsMove() instead of the hittest component at |event|'s | |
| 345 // location. | |
| 346 if (GetWindowComponent(target, *event) != HTCAPTION || | |
| 347 !wm::GetWindowState(target)->IsNormalOrSnapped()) { | |
| 348 return; | |
| 349 } | |
| 350 | |
| 351 if (event->details().velocity_y() > kMinVertVelocityForWindowMinimize) { | |
| 352 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED); | |
| 353 } else if (event->details().velocity_y() < | |
| 354 -kMinVertVelocityForWindowMinimize) { | |
| 355 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED); | |
| 356 } else if (event->details().velocity_x() > | |
| 357 kMinHorizVelocityForWindowSwipe) { | |
| 358 SetWindowStateTypeFromGesture(target, | |
| 359 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED); | |
| 360 } else if (event->details().velocity_x() < | |
| 361 -kMinHorizVelocityForWindowSwipe) { | |
| 362 SetWindowStateTypeFromGesture(target, | |
| 363 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED); | |
| 364 } | |
| 365 event->StopPropagation(); | |
| 366 return; | |
| 367 case ui::ET_GESTURE_SWIPE: | |
| 368 DCHECK_GT(event->details().touch_points(), 0); | |
| 369 if (event->details().touch_points() == 1) | |
| 370 return; | |
| 371 if (!wm::GetWindowState(target)->IsNormalOrSnapped()) | |
| 372 return; | |
| 373 | |
| 374 CompleteDrag(DRAG_COMPLETE); | |
| 375 | |
| 376 if (event->details().swipe_down()) { | |
| 377 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED); | |
| 378 } else if (event->details().swipe_up()) { | |
| 379 SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED); | |
| 380 } else if (event->details().swipe_right()) { | |
| 381 SetWindowStateTypeFromGesture(target, | |
| 382 wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED); | |
| 383 } else { | |
| 384 SetWindowStateTypeFromGesture(target, | |
| 385 wm::WINDOW_STATE_TYPE_LEFT_SNAPPED); | |
| 386 } | |
| 387 event->StopPropagation(); | |
| 388 return; | |
| 389 default: | |
| 390 return; | |
| 391 } | |
| 392 } | 43 } |
| 393 | 44 |
| 394 aura::client::WindowMoveResult ToplevelWindowEventHandler::RunMoveLoop( | 45 aura::client::WindowMoveResult ToplevelWindowEventHandler::RunMoveLoop( |
| 395 aura::Window* source, | 46 aura::Window* source, |
| 396 const gfx::Vector2d& drag_offset, | 47 const gfx::Vector2d& drag_offset, |
| 397 aura::client::WindowMoveSource move_source) { | 48 aura::client::WindowMoveSource move_source) { |
| 398 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. | 49 DCHECK(!in_move_loop_); // Can only handle one nested loop at a time. |
| 399 aura::Window* root_window = source->GetRootWindow(); | 50 aura::Window* root_window = source->GetRootWindow(); |
| 400 DCHECK(root_window); | 51 DCHECK(root_window); |
| 401 gfx::Point drag_location; | 52 gfx::Point drag_location; |
| 402 if (move_source == aura::client::WINDOW_MOVE_SOURCE_TOUCH && | 53 if (move_source == aura::client::WINDOW_MOVE_SOURCE_TOUCH && |
| 403 aura::Env::GetInstance()->is_touch_down()) { | 54 aura::Env::GetInstance()->is_touch_down()) { |
| 404 gfx::PointF drag_location_f; | 55 gfx::PointF drag_location_f; |
| 405 bool has_point = ui::GestureRecognizer::Get()-> | 56 bool has_point = ui::GestureRecognizer::Get()-> |
| 406 GetLastTouchPointForTarget(source, &drag_location_f); | 57 GetLastTouchPointForTarget(source, &drag_location_f); |
| 407 drag_location = gfx::ToFlooredPoint(drag_location_f); | 58 drag_location = gfx::ToFlooredPoint(drag_location_f); |
| 408 DCHECK(has_point); | 59 DCHECK(has_point); |
| 409 } else { | 60 } else { |
| 410 drag_location = | 61 drag_location = |
| 411 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot(); | 62 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot(); |
| 412 aura::Window::ConvertPointToTarget( | 63 aura::Window::ConvertPointToTarget( |
| 413 root_window, source->parent(), &drag_location); | 64 root_window, source->parent(), &drag_location); |
| 414 } | 65 } |
| 415 // Set the cursor before calling AttemptToStartDrag(), as that will | 66 // Set the cursor before calling AttemptToStartDrag(), as that will |
| 416 // eventually call LockCursor() and prevent the cursor from changing. | 67 // eventually call LockCursor() and prevent the cursor from changing. |
| 417 aura::client::CursorClient* cursor_client = | 68 aura::client::CursorClient* cursor_client = |
| 418 aura::client::GetCursorClient(root_window); | 69 aura::client::GetCursorClient(root_window); |
| 419 if (cursor_client) | 70 if (cursor_client) |
| 420 cursor_client->SetCursor(ui::kCursorPointer); | 71 cursor_client->SetCursor(ui::kCursorPointer); |
| 421 if (!AttemptToStartDrag(source, drag_location, HTCAPTION, move_source)) | 72 |
| 73 base::RunLoop run_loop; |
| 74 |
| 75 wm::WmToplevelWindowEventHandler::DragResult result = |
| 76 wm::WmToplevelWindowEventHandler::DragResult::SUCCESS; |
| 77 if (!wm_toplevel_window_event_handler_.AttemptToStartDrag( |
| 78 wm::WmWindowAura::Get(source), drag_location, HTCAPTION, move_source, |
| 79 base::Bind(&ToplevelWindowEventHandler::OnDragCompleted, |
| 80 weak_factory_.GetWeakPtr(), &result, &run_loop))) { |
| 81 return aura::client::MOVE_CANCELED; |
| 82 } |
| 83 |
| 84 in_move_loop_ = true; |
| 85 base::WeakPtr<ToplevelWindowEventHandler> weak_ptr( |
| 86 weak_factory_.GetWeakPtr()); |
| 87 base::MessageLoop* loop = base::MessageLoop::current(); |
| 88 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); |
| 89 |
| 90 run_loop.Run(); |
| 91 |
| 92 if (!weak_ptr) |
| 422 return aura::client::MOVE_CANCELED; | 93 return aura::client::MOVE_CANCELED; |
| 423 | 94 |
| 424 in_move_loop_ = true; | |
| 425 bool destroyed = false; | |
| 426 destroyed_ = &destroyed; | |
| 427 base::MessageLoop* loop = base::MessageLoop::current(); | |
| 428 base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop); | |
| 429 base::RunLoop run_loop; | |
| 430 quit_closure_ = run_loop.QuitClosure(); | |
| 431 run_loop.Run(); | |
| 432 if (destroyed) | |
| 433 return aura::client::MOVE_CANCELED; | |
| 434 destroyed_ = NULL; | |
| 435 in_move_loop_ = false; | 95 in_move_loop_ = false; |
| 436 return drag_reverted_ ? aura::client::MOVE_CANCELED : | 96 return result == wm::WmToplevelWindowEventHandler::DragResult::SUCCESS |
| 437 aura::client::MOVE_SUCCESSFUL; | 97 ? aura::client::MOVE_SUCCESSFUL |
| 98 : aura::client::MOVE_CANCELED; |
| 438 } | 99 } |
| 439 | 100 |
| 440 void ToplevelWindowEventHandler::EndMoveLoop() { | 101 void ToplevelWindowEventHandler::EndMoveLoop() { |
| 441 if (in_move_loop_) | 102 if (in_move_loop_) |
| 442 CompleteDrag(DRAG_REVERT); | 103 wm_toplevel_window_event_handler_.RevertDrag(); |
| 443 } | 104 } |
| 444 | 105 |
| 445 void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() { | 106 void ToplevelWindowEventHandler::OnDragCompleted( |
| 446 CompleteDrag(DRAG_REVERT); | 107 wm::WmToplevelWindowEventHandler::DragResult* result_return_value, |
| 447 } | 108 base::RunLoop* run_loop, |
| 448 | 109 wm::WmToplevelWindowEventHandler::DragResult result) { |
| 449 bool ToplevelWindowEventHandler::AttemptToStartDrag( | 110 *result_return_value = result; |
| 450 aura::Window* window, | 111 run_loop->Quit(); |
| 451 const gfx::Point& point_in_parent, | |
| 452 int window_component, | |
| 453 aura::client::WindowMoveSource source) { | |
| 454 if (window_resizer_.get()) | |
| 455 return false; | |
| 456 WindowResizer* resizer = | |
| 457 CreateWindowResizer(wm::WmWindowAura::Get(window), point_in_parent, | |
| 458 window_component, source) | |
| 459 .release(); | |
| 460 if (!resizer) | |
| 461 return false; | |
| 462 | |
| 463 window_resizer_.reset(new ScopedWindowResizer(this, resizer)); | |
| 464 | |
| 465 pre_drag_window_bounds_ = window->bounds(); | |
| 466 in_gesture_drag_ = (source == aura::client::WINDOW_MOVE_SOURCE_TOUCH); | |
| 467 return true; | |
| 468 } | |
| 469 | |
| 470 bool ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status) { | |
| 471 if (!window_resizer_) | |
| 472 return false; | |
| 473 | |
| 474 std::unique_ptr<ScopedWindowResizer> resizer(window_resizer_.release()); | |
| 475 switch (status) { | |
| 476 case DRAG_COMPLETE: | |
| 477 resizer->resizer()->CompleteDrag(); | |
| 478 break; | |
| 479 case DRAG_REVERT: | |
| 480 resizer->resizer()->RevertDrag(); | |
| 481 break; | |
| 482 case DRAG_RESIZER_WINDOW_DESTROYED: | |
| 483 // We explicitly do not invoke RevertDrag() since that may do things to | |
| 484 // WindowResizer::GetAuraTarget() which was destroyed. | |
| 485 break; | |
| 486 } | |
| 487 drag_reverted_ = (status != DRAG_COMPLETE); | |
| 488 | |
| 489 first_finger_hittest_ = HTNOWHERE; | |
| 490 in_gesture_drag_ = false; | |
| 491 if (in_move_loop_) | |
| 492 quit_closure_.Run(); | |
| 493 return true; | |
| 494 } | |
| 495 | |
| 496 void ToplevelWindowEventHandler::HandleMousePressed( | |
| 497 aura::Window* target, | |
| 498 ui::MouseEvent* event) { | |
| 499 if (event->phase() != ui::EP_PRETARGET || !target->delegate()) | |
| 500 return; | |
| 501 | |
| 502 // We also update the current window component here because for the | |
| 503 // mouse-drag-release-press case, where the mouse is released and | |
| 504 // pressed without mouse move event. | |
| 505 int component = GetWindowComponent(target, *event); | |
| 506 if ((event->flags() & | |
| 507 (ui::EF_IS_DOUBLE_CLICK | ui::EF_IS_TRIPLE_CLICK)) == 0 && | |
| 508 WindowResizer::GetBoundsChangeForWindowComponent(component)) { | |
| 509 gfx::Point location_in_parent( | |
| 510 ConvertPointToParent(target, event->location())); | |
| 511 AttemptToStartDrag(target, location_in_parent, component, | |
| 512 aura::client::WINDOW_MOVE_SOURCE_MOUSE); | |
| 513 // Set as handled so that other event handlers do no act upon the event | |
| 514 // but still receive it so that they receive both parts of each pressed/ | |
| 515 // released pair. | |
| 516 event->SetHandled(); | |
| 517 } else { | |
| 518 CompleteDrag(DRAG_COMPLETE); | |
| 519 } | |
| 520 } | |
| 521 | |
| 522 void ToplevelWindowEventHandler::HandleMouseReleased( | |
| 523 aura::Window* target, | |
| 524 ui::MouseEvent* event) { | |
| 525 if (event->phase() == ui::EP_PRETARGET) | |
| 526 CompleteDrag(DRAG_COMPLETE); | |
| 527 } | |
| 528 | |
| 529 void ToplevelWindowEventHandler::HandleDrag( | |
| 530 aura::Window* target, | |
| 531 ui::LocatedEvent* event) { | |
| 532 // This function only be triggered to move window | |
| 533 // by mouse drag or touch move event. | |
| 534 DCHECK(event->type() == ui::ET_MOUSE_DRAGGED || | |
| 535 event->type() == ui::ET_TOUCH_MOVED || | |
| 536 event->type() == ui::ET_GESTURE_SCROLL_UPDATE); | |
| 537 | |
| 538 // Drag actions are performed pre-target handling to prevent spurious mouse | |
| 539 // moves from the move/size operation from being sent to the target. | |
| 540 if (event->phase() != ui::EP_PRETARGET) | |
| 541 return; | |
| 542 | |
| 543 if (!window_resizer_) | |
| 544 return; | |
| 545 window_resizer_->resizer()->Drag( | |
| 546 ConvertPointToParent(target, event->location()), event->flags()); | |
| 547 event->StopPropagation(); | |
| 548 } | |
| 549 | |
| 550 void ToplevelWindowEventHandler::HandleMouseMoved( | |
| 551 aura::Window* target, | |
| 552 ui::LocatedEvent* event) { | |
| 553 // Shadow effects are applied after target handling. Note that we don't | |
| 554 // respect ER_HANDLED here right now since we have not had a reason to allow | |
| 555 // the target to cancel shadow rendering. | |
| 556 if (event->phase() != ui::EP_POSTTARGET || !target->delegate()) | |
| 557 return; | |
| 558 | |
| 559 // TODO(jamescook): Move the resize cursor update code into here from | |
| 560 // CompoundEventFilter? | |
| 561 ResizeShadowController* controller = | |
| 562 Shell::GetInstance()->resize_shadow_controller(); | |
| 563 if (controller) { | |
| 564 if (event->flags() & ui::EF_IS_NON_CLIENT) { | |
| 565 int component = | |
| 566 target->delegate()->GetNonClientComponent(event->location()); | |
| 567 controller->ShowShadow(target, component); | |
| 568 } else { | |
| 569 controller->HideShadow(target); | |
| 570 } | |
| 571 } | |
| 572 } | |
| 573 | |
| 574 void ToplevelWindowEventHandler::HandleMouseExited( | |
| 575 aura::Window* target, | |
| 576 ui::LocatedEvent* event) { | |
| 577 // Shadow effects are applied after target handling. Note that we don't | |
| 578 // respect ER_HANDLED here right now since we have not had a reason to allow | |
| 579 // the target to cancel shadow rendering. | |
| 580 if (event->phase() != ui::EP_POSTTARGET) | |
| 581 return; | |
| 582 | |
| 583 ResizeShadowController* controller = | |
| 584 Shell::GetInstance()->resize_shadow_controller(); | |
| 585 if (controller) | |
| 586 controller->HideShadow(target); | |
| 587 } | |
| 588 | |
| 589 void ToplevelWindowEventHandler::HandleCaptureLost(ui::LocatedEvent* event) { | |
| 590 if (event->phase() == ui::EP_PRETARGET) { | |
| 591 // We complete the drag instead of reverting it, as reverting it will result | |
| 592 // in a weird behavior when a dragged tab produces a modal dialog while the | |
| 593 // drag is in progress. crbug.com/558201. | |
| 594 CompleteDrag(DRAG_COMPLETE); | |
| 595 } | |
| 596 } | |
| 597 | |
| 598 void ToplevelWindowEventHandler::SetWindowStateTypeFromGesture( | |
| 599 aura::Window* window, | |
| 600 wm::WindowStateType new_state_type) { | |
| 601 wm::WindowState* window_state = ash::wm::GetWindowState(window); | |
| 602 // TODO(oshima): Move extra logic (set_unminimize_to_restore_bounds, | |
| 603 // SetRestoreBoundsInParent) that modifies the window state | |
| 604 // into WindowState. | |
| 605 switch (new_state_type) { | |
| 606 case wm::WINDOW_STATE_TYPE_MINIMIZED: | |
| 607 if (window_state->CanMinimize()) { | |
| 608 window_state->Minimize(); | |
| 609 window_state->set_unminimize_to_restore_bounds(true); | |
| 610 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_); | |
| 611 } | |
| 612 break; | |
| 613 case wm::WINDOW_STATE_TYPE_MAXIMIZED: | |
| 614 if (window_state->CanMaximize()) { | |
| 615 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_); | |
| 616 window_state->Maximize(); | |
| 617 } | |
| 618 break; | |
| 619 case wm::WINDOW_STATE_TYPE_LEFT_SNAPPED: | |
| 620 if (window_state->CanSnap()) { | |
| 621 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_); | |
| 622 const wm::WMEvent event(wm::WM_EVENT_SNAP_LEFT); | |
| 623 window_state->OnWMEvent(&event); | |
| 624 } | |
| 625 break; | |
| 626 case wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED: | |
| 627 if (window_state->CanSnap()) { | |
| 628 window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_); | |
| 629 const wm::WMEvent event(wm::WM_EVENT_SNAP_RIGHT); | |
| 630 window_state->OnWMEvent(&event); | |
| 631 } | |
| 632 break; | |
| 633 default: | |
| 634 NOTREACHED(); | |
| 635 } | |
| 636 } | |
| 637 | |
| 638 void ToplevelWindowEventHandler::ResizerWindowDestroyed() { | |
| 639 CompleteDrag(DRAG_RESIZER_WINDOW_DESTROYED); | |
| 640 } | 112 } |
| 641 | 113 |
| 642 } // namespace ash | 114 } // namespace ash |
| OLD | NEW |