| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "ui/aura/window_event_dispatcher.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/debug/trace_event.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "ui/aura/client/capture_client.h" | |
| 12 #include "ui/aura/client/cursor_client.h" | |
| 13 #include "ui/aura/client/event_client.h" | |
| 14 #include "ui/aura/client/focus_client.h" | |
| 15 #include "ui/aura/client/screen_position_client.h" | |
| 16 #include "ui/aura/env.h" | |
| 17 #include "ui/aura/window.h" | |
| 18 #include "ui/aura/window_delegate.h" | |
| 19 #include "ui/aura/window_targeter.h" | |
| 20 #include "ui/aura/window_tracker.h" | |
| 21 #include "ui/aura/window_tree_host.h" | |
| 22 #include "ui/base/hit_test.h" | |
| 23 #include "ui/compositor/dip_util.h" | |
| 24 #include "ui/events/event.h" | |
| 25 #include "ui/events/gestures/gesture_recognizer.h" | |
| 26 #include "ui/events/gestures/gesture_types.h" | |
| 27 | |
| 28 typedef ui::EventDispatchDetails DispatchDetails; | |
| 29 | |
| 30 namespace aura { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 // Returns true if |target| has a non-client (frame) component at |location|, | |
| 35 // in window coordinates. | |
| 36 bool IsNonClientLocation(Window* target, const gfx::Point& location) { | |
| 37 if (!target->delegate()) | |
| 38 return false; | |
| 39 int hit_test_code = target->delegate()->GetNonClientComponent(location); | |
| 40 return hit_test_code != HTCLIENT && hit_test_code != HTNOWHERE; | |
| 41 } | |
| 42 | |
| 43 Window* ConsumerToWindow(ui::GestureConsumer* consumer) { | |
| 44 return consumer ? static_cast<Window*>(consumer) : NULL; | |
| 45 } | |
| 46 | |
| 47 void SetLastMouseLocation(const Window* root_window, | |
| 48 const gfx::Point& location_in_root) { | |
| 49 client::ScreenPositionClient* client = | |
| 50 client::GetScreenPositionClient(root_window); | |
| 51 if (client) { | |
| 52 gfx::Point location_in_screen = location_in_root; | |
| 53 client->ConvertPointToScreen(root_window, &location_in_screen); | |
| 54 Env::GetInstance()->set_last_mouse_location(location_in_screen); | |
| 55 } else { | |
| 56 Env::GetInstance()->set_last_mouse_location(location_in_root); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 bool IsEventCandidateForHold(const ui::Event& event) { | |
| 61 if (event.type() == ui::ET_TOUCH_MOVED) | |
| 62 return true; | |
| 63 if (event.type() == ui::ET_MOUSE_DRAGGED) | |
| 64 return true; | |
| 65 if (event.IsMouseEvent() && (event.flags() & ui::EF_IS_SYNTHESIZED)) | |
| 66 return true; | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 //////////////////////////////////////////////////////////////////////////////// | |
| 73 // WindowEventDispatcher, public: | |
| 74 | |
| 75 WindowEventDispatcher::WindowEventDispatcher(WindowTreeHost* host) | |
| 76 : host_(host), | |
| 77 touch_ids_down_(0), | |
| 78 mouse_pressed_handler_(NULL), | |
| 79 mouse_moved_handler_(NULL), | |
| 80 event_dispatch_target_(NULL), | |
| 81 old_dispatch_target_(NULL), | |
| 82 synthesize_mouse_move_(false), | |
| 83 move_hold_count_(0), | |
| 84 dispatching_held_event_(false), | |
| 85 observer_manager_(this), | |
| 86 repost_event_factory_(this), | |
| 87 held_event_factory_(this) { | |
| 88 ui::GestureRecognizer::Get()->AddGestureEventHelper(this); | |
| 89 Env::GetInstance()->AddObserver(this); | |
| 90 } | |
| 91 | |
| 92 WindowEventDispatcher::~WindowEventDispatcher() { | |
| 93 TRACE_EVENT0("shutdown", "WindowEventDispatcher::Destructor"); | |
| 94 Env::GetInstance()->RemoveObserver(this); | |
| 95 ui::GestureRecognizer::Get()->RemoveGestureEventHelper(this); | |
| 96 } | |
| 97 | |
| 98 void WindowEventDispatcher::RepostEvent(const ui::LocatedEvent& event) { | |
| 99 DCHECK(event.type() == ui::ET_MOUSE_PRESSED || | |
| 100 event.type() == ui::ET_GESTURE_TAP_DOWN); | |
| 101 // We allow for only one outstanding repostable event. This is used | |
| 102 // in exiting context menus. A dropped repost request is allowed. | |
| 103 if (event.type() == ui::ET_MOUSE_PRESSED) { | |
| 104 held_repostable_event_.reset( | |
| 105 new ui::MouseEvent( | |
| 106 static_cast<const ui::MouseEvent&>(event), | |
| 107 static_cast<aura::Window*>(event.target()), | |
| 108 window())); | |
| 109 base::MessageLoop::current()->PostNonNestableTask( | |
| 110 FROM_HERE, base::Bind( | |
| 111 base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents), | |
| 112 repost_event_factory_.GetWeakPtr())); | |
| 113 } else { | |
| 114 DCHECK(event.type() == ui::ET_GESTURE_TAP_DOWN); | |
| 115 held_repostable_event_.reset(); | |
| 116 // TODO(rbyers): Reposing of gestures is tricky to get | |
| 117 // right, so it's not yet supported. crbug.com/170987. | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void WindowEventDispatcher::OnMouseEventsEnableStateChanged(bool enabled) { | |
| 122 // Send entered / exited so that visual state can be updated to match | |
| 123 // mouse events state. | |
| 124 PostSynthesizeMouseMove(); | |
| 125 // TODO(mazda): Add code to disable mouse events when |enabled| == false. | |
| 126 } | |
| 127 | |
| 128 void WindowEventDispatcher::DispatchCancelModeEvent() { | |
| 129 ui::CancelModeEvent event; | |
| 130 Window* focused_window = client::GetFocusClient(window())->GetFocusedWindow(); | |
| 131 if (focused_window && !window()->Contains(focused_window)) | |
| 132 focused_window = NULL; | |
| 133 DispatchDetails details = | |
| 134 DispatchEvent(focused_window ? focused_window : window(), &event); | |
| 135 if (details.dispatcher_destroyed) | |
| 136 return; | |
| 137 } | |
| 138 | |
| 139 void WindowEventDispatcher::DispatchGestureEvent(ui::GestureEvent* event) { | |
| 140 DispatchDetails details = DispatchHeldEvents(); | |
| 141 if (details.dispatcher_destroyed) | |
| 142 return; | |
| 143 Window* target = GetGestureTarget(event); | |
| 144 if (target) { | |
| 145 event->ConvertLocationToTarget(window(), target); | |
| 146 DispatchDetails details = DispatchEvent(target, event); | |
| 147 if (details.dispatcher_destroyed) | |
| 148 return; | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 DispatchDetails WindowEventDispatcher::DispatchMouseExitAtPoint( | |
| 153 const gfx::Point& point) { | |
| 154 ui::MouseEvent event(ui::ET_MOUSE_EXITED, point, point, ui::EF_NONE, | |
| 155 ui::EF_NONE); | |
| 156 return DispatchMouseEnterOrExit(event, ui::ET_MOUSE_EXITED); | |
| 157 } | |
| 158 | |
| 159 void WindowEventDispatcher::ProcessedTouchEvent(ui::TouchEvent* event, | |
| 160 Window* window, | |
| 161 ui::EventResult result) { | |
| 162 // TODO(tdresser): Move this to PreDispatchTouchEvent, to enable eager | |
| 163 // gesture detection. See crbug.com/410280. | |
| 164 if (!ui::GestureRecognizer::Get() | |
| 165 ->ProcessTouchEventPreDispatch(*event, window)) { | |
| 166 return; | |
| 167 } | |
| 168 | |
| 169 // Once we've fully migrated to the eager gesture detector, we won't need to | |
| 170 // pass an event here. | |
| 171 scoped_ptr<ui::GestureRecognizer::Gestures> gestures( | |
| 172 ui::GestureRecognizer::Get()->ProcessTouchEventOnAsyncAck( | |
| 173 *event, result, window)); | |
| 174 DispatchDetails details = ProcessGestures(gestures.get()); | |
| 175 if (details.dispatcher_destroyed) | |
| 176 return; | |
| 177 } | |
| 178 | |
| 179 void WindowEventDispatcher::HoldPointerMoves() { | |
| 180 if (!move_hold_count_) | |
| 181 held_event_factory_.InvalidateWeakPtrs(); | |
| 182 ++move_hold_count_; | |
| 183 TRACE_EVENT_ASYNC_BEGIN0("ui", "WindowEventDispatcher::HoldPointerMoves", | |
| 184 this); | |
| 185 } | |
| 186 | |
| 187 void WindowEventDispatcher::ReleasePointerMoves() { | |
| 188 --move_hold_count_; | |
| 189 DCHECK_GE(move_hold_count_, 0); | |
| 190 if (!move_hold_count_ && held_move_event_) { | |
| 191 // We don't want to call DispatchHeldEvents directly, because this might be | |
| 192 // called from a deep stack while another event, in which case dispatching | |
| 193 // another one may not be safe/expected. Instead we post a task, that we | |
| 194 // may cancel if HoldPointerMoves is called again before it executes. | |
| 195 base::MessageLoop::current()->PostNonNestableTask( | |
| 196 FROM_HERE, base::Bind( | |
| 197 base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents), | |
| 198 held_event_factory_.GetWeakPtr())); | |
| 199 } | |
| 200 TRACE_EVENT_ASYNC_END0("ui", "WindowEventDispatcher::HoldPointerMoves", this); | |
| 201 } | |
| 202 | |
| 203 gfx::Point WindowEventDispatcher::GetLastMouseLocationInRoot() const { | |
| 204 gfx::Point location = Env::GetInstance()->last_mouse_location(); | |
| 205 client::ScreenPositionClient* client = | |
| 206 client::GetScreenPositionClient(window()); | |
| 207 if (client) | |
| 208 client->ConvertPointFromScreen(window(), &location); | |
| 209 return location; | |
| 210 } | |
| 211 | |
| 212 void WindowEventDispatcher::OnHostLostMouseGrab() { | |
| 213 mouse_pressed_handler_ = NULL; | |
| 214 mouse_moved_handler_ = NULL; | |
| 215 } | |
| 216 | |
| 217 void WindowEventDispatcher::OnCursorMovedToRootLocation( | |
| 218 const gfx::Point& root_location) { | |
| 219 SetLastMouseLocation(window(), root_location); | |
| 220 synthesize_mouse_move_ = false; | |
| 221 } | |
| 222 | |
| 223 void WindowEventDispatcher::OnPostNotifiedWindowDestroying(Window* window) { | |
| 224 OnWindowHidden(window, WINDOW_DESTROYED); | |
| 225 } | |
| 226 | |
| 227 //////////////////////////////////////////////////////////////////////////////// | |
| 228 // WindowEventDispatcher, private: | |
| 229 | |
| 230 Window* WindowEventDispatcher::window() { | |
| 231 return host_->window(); | |
| 232 } | |
| 233 | |
| 234 const Window* WindowEventDispatcher::window() const { | |
| 235 return host_->window(); | |
| 236 } | |
| 237 | |
| 238 void WindowEventDispatcher::TransformEventForDeviceScaleFactor( | |
| 239 ui::LocatedEvent* event) { | |
| 240 event->UpdateForRootTransform(host_->GetInverseRootTransform()); | |
| 241 } | |
| 242 | |
| 243 void WindowEventDispatcher::DispatchMouseExitToHidingWindow(Window* window) { | |
| 244 // The mouse capture is intentionally ignored. Think that a mouse enters | |
| 245 // to a window, the window sets the capture, the mouse exits the window, | |
| 246 // and then it releases the capture. In that case OnMouseExited won't | |
| 247 // be called. So it is natural not to emit OnMouseExited even though | |
| 248 // |window| is the capture window. | |
| 249 gfx::Point last_mouse_location = GetLastMouseLocationInRoot(); | |
| 250 if (window->Contains(mouse_moved_handler_) && | |
| 251 window->ContainsPointInRoot(last_mouse_location)) { | |
| 252 DispatchDetails details = DispatchMouseExitAtPoint(last_mouse_location); | |
| 253 if (details.dispatcher_destroyed) | |
| 254 return; | |
| 255 } | |
| 256 } | |
| 257 | |
| 258 ui::EventDispatchDetails WindowEventDispatcher::DispatchMouseEnterOrExit( | |
| 259 const ui::MouseEvent& event, | |
| 260 ui::EventType type) { | |
| 261 if (event.type() != ui::ET_MOUSE_CAPTURE_CHANGED && | |
| 262 !(event.flags() & ui::EF_IS_SYNTHESIZED)) { | |
| 263 SetLastMouseLocation(window(), event.root_location()); | |
| 264 } | |
| 265 | |
| 266 if (!mouse_moved_handler_ || !mouse_moved_handler_->delegate() || | |
| 267 !window()->Contains(mouse_moved_handler_)) | |
| 268 return DispatchDetails(); | |
| 269 | |
| 270 // |event| may be an event in the process of being dispatched to a target (in | |
| 271 // which case its locations will be in the event's target's coordinate | |
| 272 // system), or a synthetic event created in root-window (in which case, the | |
| 273 // event's target will be NULL, and the event will be in the root-window's | |
| 274 // coordinate system. | |
| 275 aura::Window* target = static_cast<Window*>(event.target()); | |
| 276 if (!target) | |
| 277 target = window(); | |
| 278 ui::MouseEvent translated_event(event, | |
| 279 target, | |
| 280 mouse_moved_handler_, | |
| 281 type, | |
| 282 event.flags() | ui::EF_IS_SYNTHESIZED); | |
| 283 return DispatchEvent(mouse_moved_handler_, &translated_event); | |
| 284 } | |
| 285 | |
| 286 ui::EventDispatchDetails WindowEventDispatcher::ProcessGestures( | |
| 287 ui::GestureRecognizer::Gestures* gestures) { | |
| 288 DispatchDetails details; | |
| 289 if (!gestures || gestures->empty()) | |
| 290 return details; | |
| 291 | |
| 292 Window* target = GetGestureTarget(gestures->get().at(0)); | |
| 293 if (!target) | |
| 294 return details; | |
| 295 | |
| 296 for (size_t i = 0; i < gestures->size(); ++i) { | |
| 297 ui::GestureEvent* event = gestures->get().at(i); | |
| 298 event->ConvertLocationToTarget(window(), target); | |
| 299 details = DispatchEvent(target, event); | |
| 300 if (details.dispatcher_destroyed || details.target_destroyed) | |
| 301 break; | |
| 302 } | |
| 303 return details; | |
| 304 } | |
| 305 | |
| 306 void WindowEventDispatcher::OnWindowHidden(Window* invisible, | |
| 307 WindowHiddenReason reason) { | |
| 308 // If the window the mouse was pressed in becomes invisible, it should no | |
| 309 // longer receive mouse events. | |
| 310 if (invisible->Contains(mouse_pressed_handler_)) | |
| 311 mouse_pressed_handler_ = NULL; | |
| 312 if (invisible->Contains(mouse_moved_handler_)) | |
| 313 mouse_moved_handler_ = NULL; | |
| 314 | |
| 315 // If events are being dispatched from a nested message-loop, and the target | |
| 316 // of the outer loop is hidden or moved to another dispatcher during | |
| 317 // dispatching events in the inner loop, then reset the target for the outer | |
| 318 // loop. | |
| 319 if (invisible->Contains(old_dispatch_target_)) | |
| 320 old_dispatch_target_ = NULL; | |
| 321 | |
| 322 invisible->CleanupGestureState(); | |
| 323 | |
| 324 // Do not clear the capture, and the |event_dispatch_target_| if the | |
| 325 // window is moving across hosts, because the target itself is actually still | |
| 326 // visible and clearing them stops further event processing, which can cause | |
| 327 // unexpected behaviors. See crbug.com/157583 | |
| 328 if (reason != WINDOW_MOVING) { | |
| 329 // We don't ask |invisible| here, because invisible may have been removed | |
| 330 // from the window hierarchy already by the time this function is called | |
| 331 // (OnWindowDestroyed). | |
| 332 client::CaptureClient* capture_client = | |
| 333 client::GetCaptureClient(host_->window()); | |
| 334 Window* capture_window = | |
| 335 capture_client ? capture_client->GetCaptureWindow() : NULL; | |
| 336 | |
| 337 if (invisible->Contains(event_dispatch_target_)) | |
| 338 event_dispatch_target_ = NULL; | |
| 339 | |
| 340 // If the ancestor of the capture window is hidden, release the capture. | |
| 341 // Note that this may delete the window so do not use capture_window | |
| 342 // after this. | |
| 343 if (invisible->Contains(capture_window) && invisible != window()) | |
| 344 capture_window->ReleaseCapture(); | |
| 345 } | |
| 346 } | |
| 347 | |
| 348 Window* WindowEventDispatcher::GetGestureTarget(ui::GestureEvent* event) { | |
| 349 Window* target = NULL; | |
| 350 if (!event->IsEndingEvent()) { | |
| 351 // The window that received the start event (e.g. scroll begin) needs to | |
| 352 // receive the end event (e.g. scroll end). | |
| 353 target = client::GetCaptureWindow(window()); | |
| 354 } | |
| 355 if (!target) { | |
| 356 target = ConsumerToWindow( | |
| 357 ui::GestureRecognizer::Get()->GetTargetForGestureEvent(*event)); | |
| 358 } | |
| 359 | |
| 360 return target; | |
| 361 } | |
| 362 | |
| 363 //////////////////////////////////////////////////////////////////////////////// | |
| 364 // WindowEventDispatcher, aura::client::CaptureDelegate implementation: | |
| 365 | |
| 366 void WindowEventDispatcher::UpdateCapture(Window* old_capture, | |
| 367 Window* new_capture) { | |
| 368 // |mouse_moved_handler_| may have been set to a Window in a different root | |
| 369 // (see below). Clear it here to ensure we don't end up referencing a stale | |
| 370 // Window. | |
| 371 if (mouse_moved_handler_ && !window()->Contains(mouse_moved_handler_)) | |
| 372 mouse_moved_handler_ = NULL; | |
| 373 | |
| 374 if (old_capture && old_capture->GetRootWindow() == window() && | |
| 375 old_capture->delegate()) { | |
| 376 // Send a capture changed event with bogus location data. | |
| 377 ui::MouseEvent event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(), | |
| 378 gfx::Point(), 0, 0); | |
| 379 | |
| 380 DispatchDetails details = DispatchEvent(old_capture, &event); | |
| 381 if (details.dispatcher_destroyed) | |
| 382 return; | |
| 383 | |
| 384 old_capture->delegate()->OnCaptureLost(); | |
| 385 } | |
| 386 | |
| 387 if (new_capture) { | |
| 388 // Make all subsequent mouse events go to the capture window. We shouldn't | |
| 389 // need to send an event here as OnCaptureLost() should take care of that. | |
| 390 if (mouse_moved_handler_ || Env::GetInstance()->IsMouseButtonDown()) | |
| 391 mouse_moved_handler_ = new_capture; | |
| 392 } else { | |
| 393 // Make sure mouse_moved_handler gets updated. | |
| 394 DispatchDetails details = SynthesizeMouseMoveEvent(); | |
| 395 if (details.dispatcher_destroyed) | |
| 396 return; | |
| 397 } | |
| 398 mouse_pressed_handler_ = NULL; | |
| 399 } | |
| 400 | |
| 401 void WindowEventDispatcher::OnOtherRootGotCapture() { | |
| 402 // Windows provides the TrackMouseEvents API which allows us to rely on the | |
| 403 // OS to send us the mouse exit events (WM_MOUSELEAVE). Additionally on | |
| 404 // desktop Windows, every top level window could potentially have its own | |
| 405 // root window, in which case this function will get called whenever those | |
| 406 // windows grab mouse capture. Sending mouse exit messages in these cases | |
| 407 // causes subtle bugs like (crbug.com/394672). | |
| 408 #if !defined(OS_WIN) | |
| 409 if (mouse_moved_handler_) { | |
| 410 // Dispatch a mouse exit to reset any state associated with hover. This is | |
| 411 // important when going from no window having capture to a window having | |
| 412 // capture because we do not dispatch ET_MOUSE_CAPTURE_CHANGED in this case. | |
| 413 DispatchDetails details = DispatchMouseExitAtPoint( | |
| 414 GetLastMouseLocationInRoot()); | |
| 415 if (details.dispatcher_destroyed) | |
| 416 return; | |
| 417 } | |
| 418 #endif | |
| 419 | |
| 420 mouse_moved_handler_ = NULL; | |
| 421 mouse_pressed_handler_ = NULL; | |
| 422 } | |
| 423 | |
| 424 void WindowEventDispatcher::SetNativeCapture() { | |
| 425 host_->SetCapture(); | |
| 426 } | |
| 427 | |
| 428 void WindowEventDispatcher::ReleaseNativeCapture() { | |
| 429 host_->ReleaseCapture(); | |
| 430 } | |
| 431 | |
| 432 //////////////////////////////////////////////////////////////////////////////// | |
| 433 // WindowEventDispatcher, ui::EventProcessor implementation: | |
| 434 ui::EventTarget* WindowEventDispatcher::GetRootTarget() { | |
| 435 return window(); | |
| 436 } | |
| 437 | |
| 438 void WindowEventDispatcher::OnEventProcessingStarted(ui::Event* event) { | |
| 439 // The held events are already in |window()|'s coordinate system. So it is | |
| 440 // not necessary to apply the transform to convert from the host's | |
| 441 // coordinate system to |window()|'s coordinate system. | |
| 442 if (event->IsLocatedEvent() && !dispatching_held_event_) | |
| 443 TransformEventForDeviceScaleFactor(static_cast<ui::LocatedEvent*>(event)); | |
| 444 } | |
| 445 | |
| 446 //////////////////////////////////////////////////////////////////////////////// | |
| 447 // WindowEventDispatcher, ui::EventDispatcherDelegate implementation: | |
| 448 | |
| 449 bool WindowEventDispatcher::CanDispatchToTarget(ui::EventTarget* target) { | |
| 450 return event_dispatch_target_ == target; | |
| 451 } | |
| 452 | |
| 453 ui::EventDispatchDetails WindowEventDispatcher::PreDispatchEvent( | |
| 454 ui::EventTarget* target, | |
| 455 ui::Event* event) { | |
| 456 Window* target_window = static_cast<Window*>(target); | |
| 457 CHECK(window()->Contains(target_window)); | |
| 458 | |
| 459 if (!dispatching_held_event_) { | |
| 460 bool can_be_held = IsEventCandidateForHold(*event); | |
| 461 if (!move_hold_count_ || !can_be_held) { | |
| 462 if (can_be_held) | |
| 463 held_move_event_.reset(); | |
| 464 DispatchDetails details = DispatchHeldEvents(); | |
| 465 if (details.dispatcher_destroyed || details.target_destroyed) | |
| 466 return details; | |
| 467 } | |
| 468 } | |
| 469 | |
| 470 if (event->IsMouseEvent()) { | |
| 471 PreDispatchMouseEvent(target_window, static_cast<ui::MouseEvent*>(event)); | |
| 472 } else if (event->IsScrollEvent()) { | |
| 473 PreDispatchLocatedEvent(target_window, | |
| 474 static_cast<ui::ScrollEvent*>(event)); | |
| 475 } else if (event->IsTouchEvent()) { | |
| 476 PreDispatchTouchEvent(target_window, static_cast<ui::TouchEvent*>(event)); | |
| 477 } | |
| 478 old_dispatch_target_ = event_dispatch_target_; | |
| 479 event_dispatch_target_ = static_cast<Window*>(target); | |
| 480 return DispatchDetails(); | |
| 481 } | |
| 482 | |
| 483 ui::EventDispatchDetails WindowEventDispatcher::PostDispatchEvent( | |
| 484 ui::EventTarget* target, | |
| 485 const ui::Event& event) { | |
| 486 DispatchDetails details; | |
| 487 if (!target || target != event_dispatch_target_) | |
| 488 details.target_destroyed = true; | |
| 489 event_dispatch_target_ = old_dispatch_target_; | |
| 490 old_dispatch_target_ = NULL; | |
| 491 #ifndef NDEBUG | |
| 492 DCHECK(!event_dispatch_target_ || window()->Contains(event_dispatch_target_)); | |
| 493 #endif | |
| 494 | |
| 495 if (event.IsTouchEvent() && !details.target_destroyed) { | |
| 496 // Do not let 'held' touch events contribute to any gestures unless it is | |
| 497 // being dispatched. | |
| 498 if (dispatching_held_event_ || !held_move_event_ || | |
| 499 !held_move_event_->IsTouchEvent()) { | |
| 500 | |
| 501 // Once we've fully migrated to the eager gesture detector, we won't | |
| 502 // need to pass an event here. | |
| 503 ui::TouchEvent orig_event(static_cast<const ui::TouchEvent&>(event), | |
| 504 static_cast<Window*>(event.target()), | |
| 505 window()); | |
| 506 | |
| 507 if (event.result() & ui::ER_CONSUMED) | |
| 508 orig_event.StopPropagation(); | |
| 509 | |
| 510 // TODO(tdresser): Move this to PreDispatchTouchEvent, to enable eager | |
| 511 // gesture detection. See crbug.com/410280. | |
| 512 if (!ui::GestureRecognizer::Get() | |
| 513 ->ProcessTouchEventPreDispatch(orig_event, | |
| 514 static_cast<Window*>(target))) { | |
| 515 return details; | |
| 516 } | |
| 517 | |
| 518 scoped_ptr<ui::GestureRecognizer::Gestures> gestures; | |
| 519 | |
| 520 gestures.reset( | |
| 521 ui::GestureRecognizer::Get()->ProcessTouchEventPostDispatch( | |
| 522 orig_event, event.result(), static_cast<Window*>(target))); | |
| 523 | |
| 524 return ProcessGestures(gestures.get()); | |
| 525 } | |
| 526 } | |
| 527 | |
| 528 return details; | |
| 529 } | |
| 530 | |
| 531 //////////////////////////////////////////////////////////////////////////////// | |
| 532 // WindowEventDispatcher, ui::GestureEventHelper implementation: | |
| 533 | |
| 534 bool WindowEventDispatcher::CanDispatchToConsumer( | |
| 535 ui::GestureConsumer* consumer) { | |
| 536 Window* consumer_window = ConsumerToWindow(consumer); | |
| 537 return (consumer_window && consumer_window->GetRootWindow() == window()); | |
| 538 } | |
| 539 | |
| 540 void WindowEventDispatcher::DispatchCancelTouchEvent(ui::TouchEvent* event) { | |
| 541 // The touchcancel event's location is based on the last known location of | |
| 542 // the pointer, in dips. OnEventFromSource expects events with co-ordinates | |
| 543 // in raw pixels, so we convert back to raw pixels here. | |
| 544 event->UpdateForRootTransform(host_->GetRootTransform()); | |
| 545 DispatchDetails details = OnEventFromSource(event); | |
| 546 if (details.dispatcher_destroyed) | |
| 547 return; | |
| 548 } | |
| 549 | |
| 550 //////////////////////////////////////////////////////////////////////////////// | |
| 551 // WindowEventDispatcher, WindowObserver implementation: | |
| 552 | |
| 553 void WindowEventDispatcher::OnWindowDestroying(Window* window) { | |
| 554 if (!host_->window()->Contains(window)) | |
| 555 return; | |
| 556 | |
| 557 DispatchMouseExitToHidingWindow(window); | |
| 558 SynthesizeMouseMoveAfterChangeToWindow(window); | |
| 559 } | |
| 560 | |
| 561 void WindowEventDispatcher::OnWindowDestroyed(Window* window) { | |
| 562 // We observe all windows regardless of what root Window (if any) they're | |
| 563 // attached to. | |
| 564 observer_manager_.Remove(window); | |
| 565 } | |
| 566 | |
| 567 void WindowEventDispatcher::OnWindowAddedToRootWindow(Window* attached) { | |
| 568 if (!observer_manager_.IsObserving(attached)) | |
| 569 observer_manager_.Add(attached); | |
| 570 | |
| 571 if (!host_->window()->Contains(attached)) | |
| 572 return; | |
| 573 | |
| 574 SynthesizeMouseMoveAfterChangeToWindow(attached); | |
| 575 } | |
| 576 | |
| 577 void WindowEventDispatcher::OnWindowRemovingFromRootWindow(Window* detached, | |
| 578 Window* new_root) { | |
| 579 if (!host_->window()->Contains(detached)) | |
| 580 return; | |
| 581 | |
| 582 DCHECK(client::GetCaptureWindow(window()) != window()); | |
| 583 | |
| 584 DispatchMouseExitToHidingWindow(detached); | |
| 585 SynthesizeMouseMoveAfterChangeToWindow(detached); | |
| 586 | |
| 587 // Hiding the window releases capture which can implicitly destroy the window | |
| 588 // so the window may no longer be valid after this call. | |
| 589 OnWindowHidden(detached, new_root ? WINDOW_MOVING : WINDOW_HIDDEN); | |
| 590 } | |
| 591 | |
| 592 void WindowEventDispatcher::OnWindowVisibilityChanging(Window* window, | |
| 593 bool visible) { | |
| 594 if (!host_->window()->Contains(window)) | |
| 595 return; | |
| 596 | |
| 597 DispatchMouseExitToHidingWindow(window); | |
| 598 } | |
| 599 | |
| 600 void WindowEventDispatcher::OnWindowVisibilityChanged(Window* window, | |
| 601 bool visible) { | |
| 602 if (!host_->window()->Contains(window)) | |
| 603 return; | |
| 604 | |
| 605 if (window->ContainsPointInRoot(GetLastMouseLocationInRoot())) | |
| 606 PostSynthesizeMouseMove(); | |
| 607 | |
| 608 // Hiding the window releases capture which can implicitly destroy the window | |
| 609 // so the window may no longer be valid after this call. | |
| 610 if (!visible) | |
| 611 OnWindowHidden(window, WINDOW_HIDDEN); | |
| 612 } | |
| 613 | |
| 614 void WindowEventDispatcher::OnWindowBoundsChanged(Window* window, | |
| 615 const gfx::Rect& old_bounds, | |
| 616 const gfx::Rect& new_bounds) { | |
| 617 if (!host_->window()->Contains(window)) | |
| 618 return; | |
| 619 | |
| 620 if (window == host_->window()) { | |
| 621 TRACE_EVENT1("ui", "WindowEventDispatcher::OnWindowBoundsChanged(root)", | |
| 622 "size", new_bounds.size().ToString()); | |
| 623 | |
| 624 DispatchDetails details = DispatchHeldEvents(); | |
| 625 if (details.dispatcher_destroyed) | |
| 626 return; | |
| 627 | |
| 628 synthesize_mouse_move_ = false; | |
| 629 } | |
| 630 | |
| 631 if (window->IsVisible() && !window->ignore_events()) { | |
| 632 gfx::Rect old_bounds_in_root = old_bounds, new_bounds_in_root = new_bounds; | |
| 633 Window::ConvertRectToTarget(window->parent(), host_->window(), | |
| 634 &old_bounds_in_root); | |
| 635 Window::ConvertRectToTarget(window->parent(), host_->window(), | |
| 636 &new_bounds_in_root); | |
| 637 gfx::Point last_mouse_location = GetLastMouseLocationInRoot(); | |
| 638 if (old_bounds_in_root.Contains(last_mouse_location) != | |
| 639 new_bounds_in_root.Contains(last_mouse_location)) { | |
| 640 PostSynthesizeMouseMove(); | |
| 641 } | |
| 642 } | |
| 643 } | |
| 644 | |
| 645 void WindowEventDispatcher::OnWindowTransforming(Window* window) { | |
| 646 if (!host_->window()->Contains(window)) | |
| 647 return; | |
| 648 | |
| 649 SynthesizeMouseMoveAfterChangeToWindow(window); | |
| 650 } | |
| 651 | |
| 652 void WindowEventDispatcher::OnWindowTransformed(Window* window) { | |
| 653 if (!host_->window()->Contains(window)) | |
| 654 return; | |
| 655 | |
| 656 SynthesizeMouseMoveAfterChangeToWindow(window); | |
| 657 } | |
| 658 | |
| 659 /////////////////////////////////////////////////////////////////////////////// | |
| 660 // WindowEventDispatcher, EnvObserver implementation: | |
| 661 | |
| 662 void WindowEventDispatcher::OnWindowInitialized(Window* window) { | |
| 663 observer_manager_.Add(window); | |
| 664 } | |
| 665 | |
| 666 //////////////////////////////////////////////////////////////////////////////// | |
| 667 // WindowEventDispatcher, private: | |
| 668 | |
| 669 ui::EventDispatchDetails WindowEventDispatcher::DispatchHeldEvents() { | |
| 670 if (!held_repostable_event_ && !held_move_event_) | |
| 671 return DispatchDetails(); | |
| 672 | |
| 673 CHECK(!dispatching_held_event_); | |
| 674 dispatching_held_event_ = true; | |
| 675 | |
| 676 DispatchDetails dispatch_details; | |
| 677 if (held_repostable_event_) { | |
| 678 if (held_repostable_event_->type() == ui::ET_MOUSE_PRESSED) { | |
| 679 scoped_ptr<ui::MouseEvent> mouse_event( | |
| 680 static_cast<ui::MouseEvent*>(held_repostable_event_.release())); | |
| 681 dispatch_details = OnEventFromSource(mouse_event.get()); | |
| 682 } else { | |
| 683 // TODO(rbyers): GESTURE_TAP_DOWN not yet supported: crbug.com/170987. | |
| 684 NOTREACHED(); | |
| 685 } | |
| 686 if (dispatch_details.dispatcher_destroyed) | |
| 687 return dispatch_details; | |
| 688 } | |
| 689 | |
| 690 if (held_move_event_) { | |
| 691 // If a mouse move has been synthesized, the target location is suspect, | |
| 692 // so drop the held mouse event. | |
| 693 if (held_move_event_->IsTouchEvent() || | |
| 694 (held_move_event_->IsMouseEvent() && !synthesize_mouse_move_)) { | |
| 695 dispatch_details = OnEventFromSource(held_move_event_.get()); | |
| 696 } | |
| 697 if (!dispatch_details.dispatcher_destroyed) | |
| 698 held_move_event_.reset(); | |
| 699 } | |
| 700 | |
| 701 if (!dispatch_details.dispatcher_destroyed) | |
| 702 dispatching_held_event_ = false; | |
| 703 return dispatch_details; | |
| 704 } | |
| 705 | |
| 706 void WindowEventDispatcher::PostSynthesizeMouseMove() { | |
| 707 if (synthesize_mouse_move_) | |
| 708 return; | |
| 709 synthesize_mouse_move_ = true; | |
| 710 base::MessageLoop::current()->PostNonNestableTask( | |
| 711 FROM_HERE, | |
| 712 base::Bind(base::IgnoreResult( | |
| 713 &WindowEventDispatcher::SynthesizeMouseMoveEvent), | |
| 714 held_event_factory_.GetWeakPtr())); | |
| 715 } | |
| 716 | |
| 717 void WindowEventDispatcher::SynthesizeMouseMoveAfterChangeToWindow( | |
| 718 Window* window) { | |
| 719 if (window->IsVisible() && | |
| 720 window->ContainsPointInRoot(GetLastMouseLocationInRoot())) { | |
| 721 PostSynthesizeMouseMove(); | |
| 722 } | |
| 723 } | |
| 724 | |
| 725 ui::EventDispatchDetails WindowEventDispatcher::SynthesizeMouseMoveEvent() { | |
| 726 DispatchDetails details; | |
| 727 if (!synthesize_mouse_move_) | |
| 728 return details; | |
| 729 synthesize_mouse_move_ = false; | |
| 730 | |
| 731 // If one of the mouse buttons is currently down, then do not synthesize a | |
| 732 // mouse-move event. In such cases, aura could synthesize a DRAGGED event | |
| 733 // instead of a MOVED event, but in multi-display/multi-host scenarios, the | |
| 734 // DRAGGED event can be synthesized in the incorrect host. So avoid | |
| 735 // synthesizing any events at all. | |
| 736 if (Env::GetInstance()->mouse_button_flags()) | |
| 737 return details; | |
| 738 | |
| 739 gfx::Point root_mouse_location = GetLastMouseLocationInRoot(); | |
| 740 if (!window()->bounds().Contains(root_mouse_location)) | |
| 741 return details; | |
| 742 gfx::Point host_mouse_location = root_mouse_location; | |
| 743 host_->ConvertPointToHost(&host_mouse_location); | |
| 744 ui::MouseEvent event(ui::ET_MOUSE_MOVED, | |
| 745 host_mouse_location, | |
| 746 host_mouse_location, | |
| 747 ui::EF_IS_SYNTHESIZED, | |
| 748 0); | |
| 749 return OnEventFromSource(&event); | |
| 750 } | |
| 751 | |
| 752 void WindowEventDispatcher::PreDispatchLocatedEvent(Window* target, | |
| 753 ui::LocatedEvent* event) { | |
| 754 int flags = event->flags(); | |
| 755 if (IsNonClientLocation(target, event->location())) | |
| 756 flags |= ui::EF_IS_NON_CLIENT; | |
| 757 event->set_flags(flags); | |
| 758 | |
| 759 if (!dispatching_held_event_ && | |
| 760 (event->IsMouseEvent() || event->IsScrollEvent()) && | |
| 761 !(event->flags() & ui::EF_IS_SYNTHESIZED)) { | |
| 762 if (event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) | |
| 763 SetLastMouseLocation(window(), event->root_location()); | |
| 764 synthesize_mouse_move_ = false; | |
| 765 } | |
| 766 } | |
| 767 | |
| 768 void WindowEventDispatcher::PreDispatchMouseEvent(Window* target, | |
| 769 ui::MouseEvent* event) { | |
| 770 client::CursorClient* cursor_client = client::GetCursorClient(window()); | |
| 771 // We allow synthesized mouse exit events through even if mouse events are | |
| 772 // disabled. This ensures that hover state, etc on controls like buttons is | |
| 773 // cleared. | |
| 774 if (cursor_client && | |
| 775 !cursor_client->IsMouseEventsEnabled() && | |
| 776 (event->flags() & ui::EF_IS_SYNTHESIZED) && | |
| 777 (event->type() != ui::ET_MOUSE_EXITED)) { | |
| 778 event->SetHandled(); | |
| 779 return; | |
| 780 } | |
| 781 | |
| 782 if (IsEventCandidateForHold(*event) && !dispatching_held_event_) { | |
| 783 if (move_hold_count_) { | |
| 784 if (!(event->flags() & ui::EF_IS_SYNTHESIZED) && | |
| 785 event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) { | |
| 786 SetLastMouseLocation(window(), event->root_location()); | |
| 787 } | |
| 788 held_move_event_.reset(new ui::MouseEvent(*event, target, window())); | |
| 789 event->SetHandled(); | |
| 790 return; | |
| 791 } else { | |
| 792 // We may have a held event for a period between the time move_hold_count_ | |
| 793 // fell to 0 and the DispatchHeldEvents executes. Since we're going to | |
| 794 // dispatch the new event directly below, we can reset the old one. | |
| 795 held_move_event_.reset(); | |
| 796 } | |
| 797 } | |
| 798 | |
| 799 const int kMouseButtonFlagMask = ui::EF_LEFT_MOUSE_BUTTON | | |
| 800 ui::EF_MIDDLE_MOUSE_BUTTON | | |
| 801 ui::EF_RIGHT_MOUSE_BUTTON; | |
| 802 switch (event->type()) { | |
| 803 case ui::ET_MOUSE_EXITED: | |
| 804 if (!target || target == window()) { | |
| 805 DispatchDetails details = | |
| 806 DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_EXITED); | |
| 807 if (details.dispatcher_destroyed) { | |
| 808 event->SetHandled(); | |
| 809 return; | |
| 810 } | |
| 811 mouse_moved_handler_ = NULL; | |
| 812 } | |
| 813 break; | |
| 814 case ui::ET_MOUSE_MOVED: | |
| 815 // Send an exit to the current |mouse_moved_handler_| and an enter to | |
| 816 // |target|. Take care that both us and |target| aren't destroyed during | |
| 817 // dispatch. | |
| 818 if (target != mouse_moved_handler_) { | |
| 819 aura::Window* old_mouse_moved_handler = mouse_moved_handler_; | |
| 820 WindowTracker live_window; | |
| 821 live_window.Add(target); | |
| 822 DispatchDetails details = | |
| 823 DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_EXITED); | |
| 824 if (details.dispatcher_destroyed) { | |
| 825 event->SetHandled(); | |
| 826 return; | |
| 827 } | |
| 828 // If the |mouse_moved_handler_| changes out from under us, assume a | |
| 829 // nested message loop ran and we don't need to do anything. | |
| 830 if (mouse_moved_handler_ != old_mouse_moved_handler) { | |
| 831 event->SetHandled(); | |
| 832 return; | |
| 833 } | |
| 834 if (!live_window.Contains(target) || details.target_destroyed) { | |
| 835 mouse_moved_handler_ = NULL; | |
| 836 event->SetHandled(); | |
| 837 return; | |
| 838 } | |
| 839 live_window.Remove(target); | |
| 840 | |
| 841 mouse_moved_handler_ = target; | |
| 842 details = DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_ENTERED); | |
| 843 if (details.dispatcher_destroyed || details.target_destroyed) { | |
| 844 event->SetHandled(); | |
| 845 return; | |
| 846 } | |
| 847 } | |
| 848 break; | |
| 849 case ui::ET_MOUSE_PRESSED: | |
| 850 // Don't set the mouse pressed handler for non client mouse down events. | |
| 851 // These are only sent by Windows and are not always followed with non | |
| 852 // client mouse up events which causes subsequent mouse events to be | |
| 853 // sent to the wrong target. | |
| 854 if (!(event->flags() & ui::EF_IS_NON_CLIENT) && !mouse_pressed_handler_) | |
| 855 mouse_pressed_handler_ = target; | |
| 856 Env::GetInstance()->set_mouse_button_flags( | |
| 857 event->flags() & kMouseButtonFlagMask); | |
| 858 break; | |
| 859 case ui::ET_MOUSE_RELEASED: | |
| 860 mouse_pressed_handler_ = NULL; | |
| 861 Env::GetInstance()->set_mouse_button_flags(event->flags() & | |
| 862 kMouseButtonFlagMask & ~event->changed_button_flags()); | |
| 863 break; | |
| 864 default: | |
| 865 break; | |
| 866 } | |
| 867 | |
| 868 PreDispatchLocatedEvent(target, event); | |
| 869 } | |
| 870 | |
| 871 void WindowEventDispatcher::PreDispatchTouchEvent(Window* target, | |
| 872 ui::TouchEvent* event) { | |
| 873 switch (event->type()) { | |
| 874 case ui::ET_TOUCH_PRESSED: | |
| 875 touch_ids_down_ |= (1 << event->touch_id()); | |
| 876 Env::GetInstance()->set_touch_down(touch_ids_down_ != 0); | |
| 877 break; | |
| 878 | |
| 879 // Handle ET_TOUCH_CANCELLED only if it has a native event. | |
| 880 case ui::ET_TOUCH_CANCELLED: | |
| 881 if (!event->HasNativeEvent()) | |
| 882 break; | |
| 883 // fallthrough | |
| 884 case ui::ET_TOUCH_RELEASED: | |
| 885 touch_ids_down_ = (touch_ids_down_ | (1 << event->touch_id())) ^ | |
| 886 (1 << event->touch_id()); | |
| 887 Env::GetInstance()->set_touch_down(touch_ids_down_ != 0); | |
| 888 break; | |
| 889 | |
| 890 case ui::ET_TOUCH_MOVED: | |
| 891 if (move_hold_count_ && !dispatching_held_event_) { | |
| 892 held_move_event_.reset(new ui::TouchEvent(*event, target, window())); | |
| 893 event->SetHandled(); | |
| 894 return; | |
| 895 } | |
| 896 break; | |
| 897 | |
| 898 default: | |
| 899 NOTREACHED(); | |
| 900 break; | |
| 901 } | |
| 902 | |
| 903 PreDispatchLocatedEvent(target, event); | |
| 904 } | |
| 905 | |
| 906 } // namespace aura | |
| OLD | NEW |