base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / ui / aura / window_event_dispatcher.cc
blob27f1ff6a74d0589eaa3ad5b6d49357479301968f
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.
5 #include "ui/aura/window_event_dispatcher.h"
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"
28 typedef ui::EventDispatchDetails DispatchDetails;
30 namespace aura {
32 namespace {
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;
43 Window* ConsumerToWindow(ui::GestureConsumer* consumer) {
44 return consumer ? static_cast<Window*>(consumer) : NULL;
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);
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;
70 } // namespace
72 ////////////////////////////////////////////////////////////////////////////////
73 // WindowEventDispatcher, public:
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);
92 WindowEventDispatcher::~WindowEventDispatcher() {
93 TRACE_EVENT0("shutdown", "WindowEventDispatcher::Destructor");
94 Env::GetInstance()->RemoveObserver(this);
95 ui::GestureRecognizer::Get()->RemoveGestureEventHelper(this);
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.
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.
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;
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;
152 DispatchDetails WindowEventDispatcher::DispatchMouseExitAtPoint(
153 Window* window,
154 const gfx::Point& point) {
155 ui::MouseEvent event(ui::ET_MOUSE_EXITED, point, point, ui::EF_NONE,
156 ui::EF_NONE);
157 return DispatchMouseEnterOrExit(window, event, ui::ET_MOUSE_EXITED);
160 void WindowEventDispatcher::ProcessedTouchEvent(ui::TouchEvent* event,
161 Window* window,
162 ui::EventResult result) {
163 // Once we've fully migrated to the eager gesture detector, we won't need to
164 // pass an event here.
165 scoped_ptr<ui::GestureRecognizer::Gestures> gestures(
166 ui::GestureRecognizer::Get()->AckAsyncTouchEvent(
167 *event, result, window));
168 DispatchDetails details = ProcessGestures(gestures.get());
169 if (details.dispatcher_destroyed)
170 return;
173 void WindowEventDispatcher::HoldPointerMoves() {
174 if (!move_hold_count_)
175 held_event_factory_.InvalidateWeakPtrs();
176 ++move_hold_count_;
177 TRACE_EVENT_ASYNC_BEGIN0("ui", "WindowEventDispatcher::HoldPointerMoves",
178 this);
181 void WindowEventDispatcher::ReleasePointerMoves() {
182 --move_hold_count_;
183 DCHECK_GE(move_hold_count_, 0);
184 if (!move_hold_count_ && held_move_event_) {
185 // We don't want to call DispatchHeldEvents directly, because this might be
186 // called from a deep stack while another event, in which case dispatching
187 // another one may not be safe/expected. Instead we post a task, that we
188 // may cancel if HoldPointerMoves is called again before it executes.
189 base::MessageLoop::current()->PostNonNestableTask(
190 FROM_HERE, base::Bind(
191 base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents),
192 held_event_factory_.GetWeakPtr()));
194 TRACE_EVENT_ASYNC_END0("ui", "WindowEventDispatcher::HoldPointerMoves", this);
197 gfx::Point WindowEventDispatcher::GetLastMouseLocationInRoot() const {
198 gfx::Point location = Env::GetInstance()->last_mouse_location();
199 client::ScreenPositionClient* client =
200 client::GetScreenPositionClient(window());
201 if (client)
202 client->ConvertPointFromScreen(window(), &location);
203 return location;
206 void WindowEventDispatcher::OnHostLostMouseGrab() {
207 mouse_pressed_handler_ = NULL;
208 mouse_moved_handler_ = NULL;
211 void WindowEventDispatcher::OnCursorMovedToRootLocation(
212 const gfx::Point& root_location) {
213 SetLastMouseLocation(window(), root_location);
214 synthesize_mouse_move_ = false;
217 void WindowEventDispatcher::OnPostNotifiedWindowDestroying(Window* window) {
218 OnWindowHidden(window, WINDOW_DESTROYED);
221 ////////////////////////////////////////////////////////////////////////////////
222 // WindowEventDispatcher, private:
224 Window* WindowEventDispatcher::window() {
225 return host_->window();
228 const Window* WindowEventDispatcher::window() const {
229 return host_->window();
232 void WindowEventDispatcher::TransformEventForDeviceScaleFactor(
233 ui::LocatedEvent* event) {
234 event->UpdateForRootTransform(host_->GetInverseRootTransform());
237 void WindowEventDispatcher::DispatchMouseExitToHidingWindow(Window* window) {
238 // The mouse capture is intentionally ignored. Think that a mouse enters
239 // to a window, the window sets the capture, the mouse exits the window,
240 // and then it releases the capture. In that case OnMouseExited won't
241 // be called. So it is natural not to emit OnMouseExited even though
242 // |window| is the capture window.
243 gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
244 if (window->Contains(mouse_moved_handler_) &&
245 window->ContainsPointInRoot(last_mouse_location)) {
246 DispatchDetails details =
247 DispatchMouseExitAtPoint(window, last_mouse_location);
248 if (details.dispatcher_destroyed)
249 return;
253 ui::EventDispatchDetails WindowEventDispatcher::DispatchMouseEnterOrExit(
254 Window* target,
255 const ui::MouseEvent& event,
256 ui::EventType type) {
257 if (event.type() != ui::ET_MOUSE_CAPTURE_CHANGED &&
258 !(event.flags() & ui::EF_IS_SYNTHESIZED)) {
259 SetLastMouseLocation(window(), event.root_location());
262 if (!mouse_moved_handler_ || !mouse_moved_handler_->delegate() ||
263 !window()->Contains(mouse_moved_handler_))
264 return DispatchDetails();
266 // |event| may be an event in the process of being dispatched to a target (in
267 // which case its locations will be in the event's target's coordinate
268 // system), or a synthetic event created in root-window (in which case, the
269 // event's target will be NULL, and the event will be in the root-window's
270 // coordinate system.
271 if (!target)
272 target = window();
273 ui::MouseEvent translated_event(event,
274 target,
275 mouse_moved_handler_,
276 type,
277 event.flags() | ui::EF_IS_SYNTHESIZED);
278 return DispatchEvent(mouse_moved_handler_, &translated_event);
281 ui::EventDispatchDetails WindowEventDispatcher::ProcessGestures(
282 ui::GestureRecognizer::Gestures* gestures) {
283 DispatchDetails details;
284 if (!gestures || gestures->empty())
285 return details;
287 Window* target = GetGestureTarget(gestures->get().at(0));
288 if (!target)
289 return details;
291 for (size_t i = 0; i < gestures->size(); ++i) {
292 ui::GestureEvent* event = gestures->get().at(i);
293 event->ConvertLocationToTarget(window(), target);
294 details = DispatchEvent(target, event);
295 if (details.dispatcher_destroyed || details.target_destroyed)
296 break;
298 return details;
301 void WindowEventDispatcher::OnWindowHidden(Window* invisible,
302 WindowHiddenReason reason) {
303 // If the window the mouse was pressed in becomes invisible, it should no
304 // longer receive mouse events.
305 if (invisible->Contains(mouse_pressed_handler_))
306 mouse_pressed_handler_ = NULL;
307 if (invisible->Contains(mouse_moved_handler_))
308 mouse_moved_handler_ = NULL;
310 // If events are being dispatched from a nested message-loop, and the target
311 // of the outer loop is hidden or moved to another dispatcher during
312 // dispatching events in the inner loop, then reset the target for the outer
313 // loop.
314 if (invisible->Contains(old_dispatch_target_))
315 old_dispatch_target_ = NULL;
317 invisible->CleanupGestureState();
319 // Do not clear the capture, and the |event_dispatch_target_| if the
320 // window is moving across hosts, because the target itself is actually still
321 // visible and clearing them stops further event processing, which can cause
322 // unexpected behaviors. See crbug.com/157583
323 if (reason != WINDOW_MOVING) {
324 // We don't ask |invisible| here, because invisible may have been removed
325 // from the window hierarchy already by the time this function is called
326 // (OnWindowDestroyed).
327 client::CaptureClient* capture_client =
328 client::GetCaptureClient(host_->window());
329 Window* capture_window =
330 capture_client ? capture_client->GetCaptureWindow() : NULL;
332 if (invisible->Contains(event_dispatch_target_))
333 event_dispatch_target_ = NULL;
335 // If the ancestor of the capture window is hidden, release the capture.
336 // Note that this may delete the window so do not use capture_window
337 // after this.
338 if (invisible->Contains(capture_window) && invisible != window())
339 capture_window->ReleaseCapture();
343 Window* WindowEventDispatcher::GetGestureTarget(ui::GestureEvent* event) {
344 Window* target = NULL;
345 if (!event->IsEndingEvent()) {
346 // The window that received the start event (e.g. scroll begin) needs to
347 // receive the end event (e.g. scroll end).
348 target = client::GetCaptureWindow(window());
350 if (!target) {
351 target = ConsumerToWindow(
352 ui::GestureRecognizer::Get()->GetTargetForGestureEvent(*event));
355 return target;
358 ////////////////////////////////////////////////////////////////////////////////
359 // WindowEventDispatcher, aura::client::CaptureDelegate implementation:
361 void WindowEventDispatcher::UpdateCapture(Window* old_capture,
362 Window* new_capture) {
363 // |mouse_moved_handler_| may have been set to a Window in a different root
364 // (see below). Clear it here to ensure we don't end up referencing a stale
365 // Window.
366 if (mouse_moved_handler_ && !window()->Contains(mouse_moved_handler_))
367 mouse_moved_handler_ = NULL;
369 if (old_capture && old_capture->GetRootWindow() == window() &&
370 old_capture->delegate()) {
371 // Send a capture changed event with bogus location data.
372 ui::MouseEvent event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(),
373 gfx::Point(), 0, 0);
375 DispatchDetails details = DispatchEvent(old_capture, &event);
376 if (details.dispatcher_destroyed)
377 return;
379 if (!details.target_destroyed)
380 old_capture->delegate()->OnCaptureLost();
383 if (new_capture) {
384 // Make all subsequent mouse events go to the capture window. We shouldn't
385 // need to send an event here as OnCaptureLost() should take care of that.
386 if (mouse_moved_handler_ || Env::GetInstance()->IsMouseButtonDown())
387 mouse_moved_handler_ = new_capture;
388 } else {
389 // Make sure mouse_moved_handler gets updated.
390 DispatchDetails details = SynthesizeMouseMoveEvent();
391 if (details.dispatcher_destroyed)
392 return;
394 mouse_pressed_handler_ = NULL;
397 void WindowEventDispatcher::OnOtherRootGotCapture() {
398 // Windows provides the TrackMouseEvents API which allows us to rely on the
399 // OS to send us the mouse exit events (WM_MOUSELEAVE). Additionally on
400 // desktop Windows, every top level window could potentially have its own
401 // root window, in which case this function will get called whenever those
402 // windows grab mouse capture. Sending mouse exit messages in these cases
403 // causes subtle bugs like (crbug.com/394672).
404 #if !defined(OS_WIN)
405 if (mouse_moved_handler_) {
406 // Dispatch a mouse exit to reset any state associated with hover. This is
407 // important when going from no window having capture to a window having
408 // capture because we do not dispatch ET_MOUSE_CAPTURE_CHANGED in this case.
409 DispatchDetails details =
410 DispatchMouseExitAtPoint(nullptr, GetLastMouseLocationInRoot());
411 if (details.dispatcher_destroyed)
412 return;
414 #endif
416 mouse_moved_handler_ = NULL;
417 mouse_pressed_handler_ = NULL;
420 void WindowEventDispatcher::SetNativeCapture() {
421 host_->SetCapture();
424 void WindowEventDispatcher::ReleaseNativeCapture() {
425 host_->ReleaseCapture();
428 ////////////////////////////////////////////////////////////////////////////////
429 // WindowEventDispatcher, ui::EventProcessor implementation:
430 ui::EventTarget* WindowEventDispatcher::GetRootTarget() {
431 return window();
434 void WindowEventDispatcher::OnEventProcessingStarted(ui::Event* event) {
435 // The held events are already in |window()|'s coordinate system. So it is
436 // not necessary to apply the transform to convert from the host's
437 // coordinate system to |window()|'s coordinate system.
438 if (event->IsLocatedEvent() && !dispatching_held_event_)
439 TransformEventForDeviceScaleFactor(static_cast<ui::LocatedEvent*>(event));
442 ////////////////////////////////////////////////////////////////////////////////
443 // WindowEventDispatcher, ui::EventDispatcherDelegate implementation:
445 bool WindowEventDispatcher::CanDispatchToTarget(ui::EventTarget* target) {
446 return event_dispatch_target_ == target;
449 ui::EventDispatchDetails WindowEventDispatcher::PreDispatchEvent(
450 ui::EventTarget* target,
451 ui::Event* event) {
452 Window* target_window = static_cast<Window*>(target);
453 CHECK(window()->Contains(target_window));
455 if (!dispatching_held_event_) {
456 bool can_be_held = IsEventCandidateForHold(*event);
457 if (!move_hold_count_ || !can_be_held) {
458 if (can_be_held)
459 held_move_event_.reset();
460 DispatchDetails details = DispatchHeldEvents();
461 if (details.dispatcher_destroyed || details.target_destroyed)
462 return details;
466 if (event->IsMouseEvent()) {
467 PreDispatchMouseEvent(target_window, static_cast<ui::MouseEvent*>(event));
468 } else if (event->IsScrollEvent()) {
469 PreDispatchLocatedEvent(target_window,
470 static_cast<ui::ScrollEvent*>(event));
471 } else if (event->IsTouchEvent()) {
472 PreDispatchTouchEvent(target_window, static_cast<ui::TouchEvent*>(event));
474 old_dispatch_target_ = event_dispatch_target_;
475 event_dispatch_target_ = static_cast<Window*>(target);
476 return DispatchDetails();
479 ui::EventDispatchDetails WindowEventDispatcher::PostDispatchEvent(
480 ui::EventTarget* target,
481 const ui::Event& event) {
482 DispatchDetails details;
483 if (!target || target != event_dispatch_target_)
484 details.target_destroyed = true;
485 event_dispatch_target_ = old_dispatch_target_;
486 old_dispatch_target_ = NULL;
487 #ifndef NDEBUG
488 DCHECK(!event_dispatch_target_ || window()->Contains(event_dispatch_target_));
489 #endif
491 if (event.IsTouchEvent() && !details.target_destroyed) {
492 // Do not let 'held' touch events contribute to any gestures unless it is
493 // being dispatched.
494 if (dispatching_held_event_ || !held_move_event_ ||
495 !held_move_event_->IsTouchEvent()) {
496 const ui::TouchEvent& touchevent =
497 static_cast<const ui::TouchEvent&>(event);
499 if (!touchevent.synchronous_handling_disabled()) {
500 scoped_ptr<ui::GestureRecognizer::Gestures> gestures;
502 // Once we've fully migrated to the eager gesture detector, we won't
503 // need to pass an event here.
504 gestures.reset(ui::GestureRecognizer::Get()->AckSyncTouchEvent(
505 touchevent.unique_event_id(), event.result(),
506 static_cast<Window*>(target)));
508 return ProcessGestures(gestures.get());
513 return details;
516 ////////////////////////////////////////////////////////////////////////////////
517 // WindowEventDispatcher, ui::GestureEventHelper implementation:
519 bool WindowEventDispatcher::CanDispatchToConsumer(
520 ui::GestureConsumer* consumer) {
521 Window* consumer_window = ConsumerToWindow(consumer);
522 return (consumer_window && consumer_window->GetRootWindow() == window());
525 void WindowEventDispatcher::DispatchCancelTouchEvent(ui::TouchEvent* event) {
526 // The touchcancel event's location is based on the last known location of
527 // the pointer, in dips. OnEventFromSource expects events with co-ordinates
528 // in raw pixels, so we convert back to raw pixels here.
529 event->UpdateForRootTransform(host_->GetRootTransform());
530 DispatchDetails details = OnEventFromSource(event);
531 if (details.dispatcher_destroyed)
532 return;
535 ////////////////////////////////////////////////////////////////////////////////
536 // WindowEventDispatcher, WindowObserver implementation:
538 void WindowEventDispatcher::OnWindowDestroying(Window* window) {
539 if (!host_->window()->Contains(window))
540 return;
542 SynthesizeMouseMoveAfterChangeToWindow(window);
545 void WindowEventDispatcher::OnWindowDestroyed(Window* window) {
546 // We observe all windows regardless of what root Window (if any) they're
547 // attached to.
548 observer_manager_.Remove(window);
551 void WindowEventDispatcher::OnWindowAddedToRootWindow(Window* attached) {
552 if (!observer_manager_.IsObserving(attached))
553 observer_manager_.Add(attached);
555 if (!host_->window()->Contains(attached))
556 return;
558 SynthesizeMouseMoveAfterChangeToWindow(attached);
561 void WindowEventDispatcher::OnWindowRemovingFromRootWindow(Window* detached,
562 Window* new_root) {
563 if (!host_->window()->Contains(detached))
564 return;
566 DCHECK(client::GetCaptureWindow(window()) != window());
568 DispatchMouseExitToHidingWindow(detached);
569 SynthesizeMouseMoveAfterChangeToWindow(detached);
571 // Hiding the window releases capture which can implicitly destroy the window
572 // so the window may no longer be valid after this call.
573 OnWindowHidden(detached, new_root ? WINDOW_MOVING : WINDOW_HIDDEN);
576 void WindowEventDispatcher::OnWindowVisibilityChanging(Window* window,
577 bool visible) {
578 if (!host_->window()->Contains(window))
579 return;
581 DispatchMouseExitToHidingWindow(window);
584 void WindowEventDispatcher::OnWindowVisibilityChanged(Window* window,
585 bool visible) {
586 if (!host_->window()->Contains(window))
587 return;
589 if (window->ContainsPointInRoot(GetLastMouseLocationInRoot()))
590 PostSynthesizeMouseMove();
592 // Hiding the window releases capture which can implicitly destroy the window
593 // so the window may no longer be valid after this call.
594 if (!visible)
595 OnWindowHidden(window, WINDOW_HIDDEN);
598 void WindowEventDispatcher::OnWindowBoundsChanged(Window* window,
599 const gfx::Rect& old_bounds,
600 const gfx::Rect& new_bounds) {
601 if (!host_->window()->Contains(window))
602 return;
604 if (window == host_->window()) {
605 TRACE_EVENT1("ui", "WindowEventDispatcher::OnWindowBoundsChanged(root)",
606 "size", new_bounds.size().ToString());
608 DispatchDetails details = DispatchHeldEvents();
609 if (details.dispatcher_destroyed)
610 return;
612 synthesize_mouse_move_ = false;
615 if (window->IsVisible() && !window->ignore_events()) {
616 gfx::Rect old_bounds_in_root = old_bounds, new_bounds_in_root = new_bounds;
617 Window::ConvertRectToTarget(window->parent(), host_->window(),
618 &old_bounds_in_root);
619 Window::ConvertRectToTarget(window->parent(), host_->window(),
620 &new_bounds_in_root);
621 gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
622 if (old_bounds_in_root.Contains(last_mouse_location) !=
623 new_bounds_in_root.Contains(last_mouse_location)) {
624 PostSynthesizeMouseMove();
629 void WindowEventDispatcher::OnWindowTransforming(Window* window) {
630 if (!host_->window()->Contains(window))
631 return;
633 SynthesizeMouseMoveAfterChangeToWindow(window);
636 void WindowEventDispatcher::OnWindowTransformed(Window* window) {
637 if (!host_->window()->Contains(window))
638 return;
640 SynthesizeMouseMoveAfterChangeToWindow(window);
643 ///////////////////////////////////////////////////////////////////////////////
644 // WindowEventDispatcher, EnvObserver implementation:
646 void WindowEventDispatcher::OnWindowInitialized(Window* window) {
647 observer_manager_.Add(window);
650 ////////////////////////////////////////////////////////////////////////////////
651 // WindowEventDispatcher, private:
653 ui::EventDispatchDetails WindowEventDispatcher::DispatchHeldEvents() {
654 if (!held_repostable_event_ && !held_move_event_)
655 return DispatchDetails();
657 CHECK(!dispatching_held_event_);
658 dispatching_held_event_ = true;
660 DispatchDetails dispatch_details;
661 if (held_repostable_event_) {
662 if (held_repostable_event_->type() == ui::ET_MOUSE_PRESSED) {
663 scoped_ptr<ui::MouseEvent> mouse_event(
664 static_cast<ui::MouseEvent*>(held_repostable_event_.release()));
665 dispatch_details = OnEventFromSource(mouse_event.get());
666 } else {
667 // TODO(rbyers): GESTURE_TAP_DOWN not yet supported: crbug.com/170987.
668 NOTREACHED();
670 if (dispatch_details.dispatcher_destroyed)
671 return dispatch_details;
674 if (held_move_event_) {
675 // If a mouse move has been synthesized, the target location is suspect,
676 // so drop the held mouse event.
677 if (held_move_event_->IsTouchEvent() ||
678 (held_move_event_->IsMouseEvent() && !synthesize_mouse_move_)) {
679 dispatch_details = OnEventFromSource(held_move_event_.get());
681 if (!dispatch_details.dispatcher_destroyed)
682 held_move_event_.reset();
685 if (!dispatch_details.dispatcher_destroyed)
686 dispatching_held_event_ = false;
687 return dispatch_details;
690 void WindowEventDispatcher::PostSynthesizeMouseMove() {
691 if (synthesize_mouse_move_)
692 return;
693 synthesize_mouse_move_ = true;
694 base::MessageLoop::current()->PostNonNestableTask(
695 FROM_HERE,
696 base::Bind(base::IgnoreResult(
697 &WindowEventDispatcher::SynthesizeMouseMoveEvent),
698 held_event_factory_.GetWeakPtr()));
701 void WindowEventDispatcher::SynthesizeMouseMoveAfterChangeToWindow(
702 Window* window) {
703 if (window->IsVisible() &&
704 window->ContainsPointInRoot(GetLastMouseLocationInRoot())) {
705 PostSynthesizeMouseMove();
709 ui::EventDispatchDetails WindowEventDispatcher::SynthesizeMouseMoveEvent() {
710 DispatchDetails details;
711 if (!synthesize_mouse_move_)
712 return details;
713 synthesize_mouse_move_ = false;
715 // If one of the mouse buttons is currently down, then do not synthesize a
716 // mouse-move event. In such cases, aura could synthesize a DRAGGED event
717 // instead of a MOVED event, but in multi-display/multi-host scenarios, the
718 // DRAGGED event can be synthesized in the incorrect host. So avoid
719 // synthesizing any events at all.
720 if (Env::GetInstance()->mouse_button_flags())
721 return details;
723 gfx::Point root_mouse_location = GetLastMouseLocationInRoot();
724 if (!window()->bounds().Contains(root_mouse_location))
725 return details;
726 gfx::Point host_mouse_location = root_mouse_location;
727 host_->ConvertPointToHost(&host_mouse_location);
728 ui::MouseEvent event(ui::ET_MOUSE_MOVED,
729 host_mouse_location,
730 host_mouse_location,
731 ui::EF_IS_SYNTHESIZED,
733 return OnEventFromSource(&event);
736 void WindowEventDispatcher::PreDispatchLocatedEvent(Window* target,
737 ui::LocatedEvent* event) {
738 int flags = event->flags();
739 if (IsNonClientLocation(target, event->location()))
740 flags |= ui::EF_IS_NON_CLIENT;
741 event->set_flags(flags);
743 if (!dispatching_held_event_ &&
744 (event->IsMouseEvent() || event->IsScrollEvent()) &&
745 !(event->flags() & ui::EF_IS_SYNTHESIZED)) {
746 if (event->type() != ui::ET_MOUSE_CAPTURE_CHANGED)
747 SetLastMouseLocation(window(), event->root_location());
748 synthesize_mouse_move_ = false;
752 void WindowEventDispatcher::PreDispatchMouseEvent(Window* target,
753 ui::MouseEvent* event) {
754 client::CursorClient* cursor_client = client::GetCursorClient(window());
755 // We allow synthesized mouse exit events through even if mouse events are
756 // disabled. This ensures that hover state, etc on controls like buttons is
757 // cleared.
758 if (cursor_client &&
759 !cursor_client->IsMouseEventsEnabled() &&
760 (event->flags() & ui::EF_IS_SYNTHESIZED) &&
761 (event->type() != ui::ET_MOUSE_EXITED)) {
762 event->SetHandled();
763 return;
766 if (IsEventCandidateForHold(*event) && !dispatching_held_event_) {
767 if (move_hold_count_) {
768 if (!(event->flags() & ui::EF_IS_SYNTHESIZED) &&
769 event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) {
770 SetLastMouseLocation(window(), event->root_location());
772 held_move_event_.reset(new ui::MouseEvent(*event, target, window()));
773 event->SetHandled();
774 return;
775 } else {
776 // We may have a held event for a period between the time move_hold_count_
777 // fell to 0 and the DispatchHeldEvents executes. Since we're going to
778 // dispatch the new event directly below, we can reset the old one.
779 held_move_event_.reset();
783 const int kMouseButtonFlagMask = ui::EF_LEFT_MOUSE_BUTTON |
784 ui::EF_MIDDLE_MOUSE_BUTTON |
785 ui::EF_RIGHT_MOUSE_BUTTON;
786 switch (event->type()) {
787 case ui::ET_MOUSE_EXITED:
788 if (!target || target == window()) {
789 DispatchDetails details =
790 DispatchMouseEnterOrExit(target, *event, ui::ET_MOUSE_EXITED);
791 if (details.dispatcher_destroyed) {
792 event->SetHandled();
793 return;
795 mouse_moved_handler_ = NULL;
797 break;
798 case ui::ET_MOUSE_MOVED:
799 // Send an exit to the current |mouse_moved_handler_| and an enter to
800 // |target|. Take care that both us and |target| aren't destroyed during
801 // dispatch.
802 if (target != mouse_moved_handler_) {
803 aura::Window* old_mouse_moved_handler = mouse_moved_handler_;
804 WindowTracker live_window;
805 live_window.Add(target);
806 DispatchDetails details =
807 DispatchMouseEnterOrExit(target, *event, ui::ET_MOUSE_EXITED);
808 if (details.dispatcher_destroyed) {
809 event->SetHandled();
810 return;
812 // If the |mouse_moved_handler_| changes out from under us, assume a
813 // nested message loop ran and we don't need to do anything.
814 if (mouse_moved_handler_ != old_mouse_moved_handler) {
815 event->SetHandled();
816 return;
818 if (!live_window.Contains(target) || details.target_destroyed) {
819 mouse_moved_handler_ = NULL;
820 event->SetHandled();
821 return;
823 live_window.Remove(target);
825 mouse_moved_handler_ = target;
826 details =
827 DispatchMouseEnterOrExit(target, *event, ui::ET_MOUSE_ENTERED);
828 if (details.dispatcher_destroyed || details.target_destroyed) {
829 event->SetHandled();
830 return;
833 break;
834 case ui::ET_MOUSE_PRESSED:
835 // Don't set the mouse pressed handler for non client mouse down events.
836 // These are only sent by Windows and are not always followed with non
837 // client mouse up events which causes subsequent mouse events to be
838 // sent to the wrong target.
839 if (!(event->flags() & ui::EF_IS_NON_CLIENT) && !mouse_pressed_handler_)
840 mouse_pressed_handler_ = target;
841 Env::GetInstance()->set_mouse_button_flags(
842 event->flags() & kMouseButtonFlagMask);
843 break;
844 case ui::ET_MOUSE_RELEASED:
845 mouse_pressed_handler_ = NULL;
846 Env::GetInstance()->set_mouse_button_flags(event->flags() &
847 kMouseButtonFlagMask & ~event->changed_button_flags());
848 break;
849 default:
850 break;
853 PreDispatchLocatedEvent(target, event);
856 void WindowEventDispatcher::PreDispatchTouchEvent(Window* target,
857 ui::TouchEvent* event) {
858 switch (event->type()) {
859 case ui::ET_TOUCH_PRESSED:
860 touch_ids_down_ |= (1 << event->touch_id());
861 Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
862 break;
864 // Handle ET_TOUCH_CANCELLED only if it has a native event.
865 case ui::ET_TOUCH_CANCELLED:
866 if (!event->HasNativeEvent())
867 break;
868 // fallthrough
869 case ui::ET_TOUCH_RELEASED:
870 touch_ids_down_ = (touch_ids_down_ | (1 << event->touch_id())) ^
871 (1 << event->touch_id());
872 Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
873 break;
875 case ui::ET_TOUCH_MOVED:
876 if (move_hold_count_ && !dispatching_held_event_) {
877 held_move_event_.reset(new ui::TouchEvent(*event, target, window()));
878 event->SetHandled();
879 return;
881 break;
883 default:
884 NOTREACHED();
885 break;
888 ui::TouchEvent orig_event(*event, target, window());
889 if (!ui::GestureRecognizer::Get()->ProcessTouchEventPreDispatch(&orig_event,
890 target)) {
891 // The event is invalid - ignore it.
892 event->StopPropagation();
893 event->DisableSynchronousHandling();
894 return;
897 // This flag is set depending on the gestures recognized in the call above,
898 // and needs to propagate with the forwarded event.
899 event->set_may_cause_scrolling(orig_event.may_cause_scrolling());
901 PreDispatchLocatedEvent(target, event);
904 } // namespace aura