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"
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;
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())
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);
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);
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)
63 if (event.type() == ui::ET_MOUSE_DRAGGED)
65 if (event.IsMouseEvent() && (event.flags() & ui::EF_IS_SYNTHESIZED))
72 ////////////////////////////////////////////////////////////////////////////////
73 // WindowEventDispatcher, public:
75 WindowEventDispatcher::WindowEventDispatcher(WindowTreeHost* host)
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),
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(
106 static_cast<const ui::MouseEvent&>(event),
107 static_cast<aura::Window*>(event.target()),
109 base::MessageLoop::current()->PostNonNestableTask(
110 FROM_HERE, base::Bind(
111 base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents),
112 repost_event_factory_.GetWeakPtr()));
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)
139 void WindowEventDispatcher::DispatchGestureEvent(ui::GestureEvent* event) {
140 DispatchDetails details = DispatchHeldEvents();
141 if (details.dispatcher_destroyed)
144 Window* target = GetGestureTarget(event);
146 event->ConvertLocationToTarget(window(), target);
147 DispatchDetails details = DispatchEvent(target, event);
148 if (details.dispatcher_destroyed)
153 DispatchDetails WindowEventDispatcher::DispatchMouseExitAtPoint(
154 const gfx::Point& point) {
155 ui::MouseEvent event(ui::ET_MOUSE_EXITED, point, point, ui::EF_NONE,
157 return DispatchMouseEnterOrExit(event, ui::ET_MOUSE_EXITED);
160 void WindowEventDispatcher::ProcessedTouchEvent(ui::TouchEvent* event,
162 ui::EventResult result) {
163 // TODO(tdresser): Move this to PreDispatchTouchEvent, to enable eager
164 // gesture detection. See crbug.com/410280.
165 if (!ui::GestureRecognizer::Get()
166 ->ProcessTouchEventPreDispatch(*event, window)) {
170 // Once we've fully migrated to the eager gesture detector, we won't need to
171 // pass an event here.
172 scoped_ptr<ui::GestureRecognizer::Gestures> gestures(
173 ui::GestureRecognizer::Get()->ProcessTouchEventOnAsyncAck(
174 *event, result, window));
175 DispatchDetails details = ProcessGestures(gestures.get());
176 if (details.dispatcher_destroyed)
180 void WindowEventDispatcher::HoldPointerMoves() {
181 if (!move_hold_count_)
182 held_event_factory_.InvalidateWeakPtrs();
184 TRACE_EVENT_ASYNC_BEGIN0("ui", "WindowEventDispatcher::HoldPointerMoves",
188 void WindowEventDispatcher::ReleasePointerMoves() {
190 DCHECK_GE(move_hold_count_, 0);
191 if (!move_hold_count_ && held_move_event_) {
192 // We don't want to call DispatchHeldEvents directly, because this might be
193 // called from a deep stack while another event, in which case dispatching
194 // another one may not be safe/expected. Instead we post a task, that we
195 // may cancel if HoldPointerMoves is called again before it executes.
196 base::MessageLoop::current()->PostNonNestableTask(
197 FROM_HERE, base::Bind(
198 base::IgnoreResult(&WindowEventDispatcher::DispatchHeldEvents),
199 held_event_factory_.GetWeakPtr()));
201 TRACE_EVENT_ASYNC_END0("ui", "WindowEventDispatcher::HoldPointerMoves", this);
204 gfx::Point WindowEventDispatcher::GetLastMouseLocationInRoot() const {
205 gfx::Point location = Env::GetInstance()->last_mouse_location();
206 client::ScreenPositionClient* client =
207 client::GetScreenPositionClient(window());
209 client->ConvertPointFromScreen(window(), &location);
213 void WindowEventDispatcher::OnHostLostMouseGrab() {
214 mouse_pressed_handler_ = NULL;
215 mouse_moved_handler_ = NULL;
218 void WindowEventDispatcher::OnCursorMovedToRootLocation(
219 const gfx::Point& root_location) {
220 SetLastMouseLocation(window(), root_location);
221 synthesize_mouse_move_ = false;
224 void WindowEventDispatcher::OnPostNotifiedWindowDestroying(Window* window) {
225 OnWindowHidden(window, WINDOW_DESTROYED);
228 ////////////////////////////////////////////////////////////////////////////////
229 // WindowEventDispatcher, private:
231 Window* WindowEventDispatcher::window() {
232 return host_->window();
235 const Window* WindowEventDispatcher::window() const {
236 return host_->window();
239 void WindowEventDispatcher::TransformEventForDeviceScaleFactor(
240 ui::LocatedEvent* event) {
241 event->UpdateForRootTransform(host_->GetInverseRootTransform());
244 void WindowEventDispatcher::DispatchMouseExitToHidingWindow(Window* window) {
245 // The mouse capture is intentionally ignored. Think that a mouse enters
246 // to a window, the window sets the capture, the mouse exits the window,
247 // and then it releases the capture. In that case OnMouseExited won't
248 // be called. So it is natural not to emit OnMouseExited even though
249 // |window| is the capture window.
250 gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
251 if (window->Contains(mouse_moved_handler_) &&
252 window->ContainsPointInRoot(last_mouse_location)) {
253 DispatchDetails details = DispatchMouseExitAtPoint(last_mouse_location);
254 if (details.dispatcher_destroyed)
259 ui::EventDispatchDetails WindowEventDispatcher::DispatchMouseEnterOrExit(
260 const ui::MouseEvent& event,
261 ui::EventType type) {
262 if (event.type() != ui::ET_MOUSE_CAPTURE_CHANGED &&
263 !(event.flags() & ui::EF_IS_SYNTHESIZED)) {
264 SetLastMouseLocation(window(), event.root_location());
267 if (!mouse_moved_handler_ || !mouse_moved_handler_->delegate() ||
268 !window()->Contains(mouse_moved_handler_))
269 return DispatchDetails();
271 // |event| may be an event in the process of being dispatched to a target (in
272 // which case its locations will be in the event's target's coordinate
273 // system), or a synthetic event created in root-window (in which case, the
274 // event's target will be NULL, and the event will be in the root-window's
275 // coordinate system.
276 aura::Window* target = static_cast<Window*>(event.target());
279 ui::MouseEvent translated_event(event,
281 mouse_moved_handler_,
283 event.flags() | ui::EF_IS_SYNTHESIZED);
284 return DispatchEvent(mouse_moved_handler_, &translated_event);
287 ui::EventDispatchDetails WindowEventDispatcher::ProcessGestures(
288 ui::GestureRecognizer::Gestures* gestures) {
289 DispatchDetails details;
290 if (!gestures || gestures->empty())
293 Window* target = GetGestureTarget(gestures->get().at(0));
297 for (size_t i = 0; i < gestures->size(); ++i) {
298 ui::GestureEvent* event = gestures->get().at(i);
299 event->ConvertLocationToTarget(window(), target);
300 details = DispatchEvent(target, event);
301 if (details.dispatcher_destroyed || details.target_destroyed)
307 void WindowEventDispatcher::OnWindowHidden(Window* invisible,
308 WindowHiddenReason reason) {
309 // If the window the mouse was pressed in becomes invisible, it should no
310 // longer receive mouse events.
311 if (invisible->Contains(mouse_pressed_handler_))
312 mouse_pressed_handler_ = NULL;
313 if (invisible->Contains(mouse_moved_handler_))
314 mouse_moved_handler_ = NULL;
316 // If events are being dispatched from a nested message-loop, and the target
317 // of the outer loop is hidden or moved to another dispatcher during
318 // dispatching events in the inner loop, then reset the target for the outer
320 if (invisible->Contains(old_dispatch_target_))
321 old_dispatch_target_ = NULL;
323 invisible->CleanupGestureState();
325 // Do not clear the capture, and the |event_dispatch_target_| if the
326 // window is moving across hosts, because the target itself is actually still
327 // visible and clearing them stops further event processing, which can cause
328 // unexpected behaviors. See crbug.com/157583
329 if (reason != WINDOW_MOVING) {
330 // We don't ask |invisible| here, because invisible may have been removed
331 // from the window hierarchy already by the time this function is called
332 // (OnWindowDestroyed).
333 client::CaptureClient* capture_client =
334 client::GetCaptureClient(host_->window());
335 Window* capture_window =
336 capture_client ? capture_client->GetCaptureWindow() : NULL;
338 if (invisible->Contains(event_dispatch_target_))
339 event_dispatch_target_ = NULL;
341 // If the ancestor of the capture window is hidden, release the capture.
342 // Note that this may delete the window so do not use capture_window
344 if (invisible->Contains(capture_window) && invisible != window())
345 capture_window->ReleaseCapture();
349 Window* WindowEventDispatcher::GetGestureTarget(ui::GestureEvent* event) {
350 Window* target = NULL;
351 if (!event->IsEndingEvent()) {
352 // The window that received the start event (e.g. scroll begin) needs to
353 // receive the end event (e.g. scroll end).
354 target = client::GetCaptureWindow(window());
357 target = ConsumerToWindow(
358 ui::GestureRecognizer::Get()->GetTargetForGestureEvent(*event));
364 ////////////////////////////////////////////////////////////////////////////////
365 // WindowEventDispatcher, aura::client::CaptureDelegate implementation:
367 void WindowEventDispatcher::UpdateCapture(Window* old_capture,
368 Window* new_capture) {
369 // |mouse_moved_handler_| may have been set to a Window in a different root
370 // (see below). Clear it here to ensure we don't end up referencing a stale
372 if (mouse_moved_handler_ && !window()->Contains(mouse_moved_handler_))
373 mouse_moved_handler_ = NULL;
375 if (old_capture && old_capture->GetRootWindow() == window() &&
376 old_capture->delegate()) {
377 // Send a capture changed event with bogus location data.
378 ui::MouseEvent event(ui::ET_MOUSE_CAPTURE_CHANGED, gfx::Point(),
381 DispatchDetails details = DispatchEvent(old_capture, &event);
382 if (details.dispatcher_destroyed)
385 old_capture->delegate()->OnCaptureLost();
389 // Make all subsequent mouse events go to the capture window. We shouldn't
390 // need to send an event here as OnCaptureLost() should take care of that.
391 if (mouse_moved_handler_ || Env::GetInstance()->IsMouseButtonDown())
392 mouse_moved_handler_ = new_capture;
394 // Make sure mouse_moved_handler gets updated.
395 DispatchDetails details = SynthesizeMouseMoveEvent();
396 if (details.dispatcher_destroyed)
399 mouse_pressed_handler_ = NULL;
402 void WindowEventDispatcher::OnOtherRootGotCapture() {
403 // Windows provides the TrackMouseEvents API which allows us to rely on the
404 // OS to send us the mouse exit events (WM_MOUSELEAVE). Additionally on
405 // desktop Windows, every top level window could potentially have its own
406 // root window, in which case this function will get called whenever those
407 // windows grab mouse capture. Sending mouse exit messages in these cases
408 // causes subtle bugs like (crbug.com/394672).
410 if (mouse_moved_handler_) {
411 // Dispatch a mouse exit to reset any state associated with hover. This is
412 // important when going from no window having capture to a window having
413 // capture because we do not dispatch ET_MOUSE_CAPTURE_CHANGED in this case.
414 DispatchDetails details = DispatchMouseExitAtPoint(
415 GetLastMouseLocationInRoot());
416 if (details.dispatcher_destroyed)
421 mouse_moved_handler_ = NULL;
422 mouse_pressed_handler_ = NULL;
425 void WindowEventDispatcher::SetNativeCapture() {
429 void WindowEventDispatcher::ReleaseNativeCapture() {
430 host_->ReleaseCapture();
433 ////////////////////////////////////////////////////////////////////////////////
434 // WindowEventDispatcher, ui::EventProcessor implementation:
435 ui::EventTarget* WindowEventDispatcher::GetRootTarget() {
439 void WindowEventDispatcher::PrepareEventForDispatch(ui::Event* event) {
440 if (dispatching_held_event_) {
441 // The held events are already in |window()|'s coordinate system. So it is
442 // not necessary to apply the transform to convert from the host's
443 // coordinate system to |window()|'s coordinate system.
446 if (event->IsMouseEvent() ||
447 event->IsScrollEvent() ||
448 event->IsTouchEvent() ||
449 event->IsGestureEvent()) {
450 TransformEventForDeviceScaleFactor(static_cast<ui::LocatedEvent*>(event));
454 ////////////////////////////////////////////////////////////////////////////////
455 // WindowEventDispatcher, ui::EventDispatcherDelegate implementation:
457 bool WindowEventDispatcher::CanDispatchToTarget(ui::EventTarget* target) {
458 return event_dispatch_target_ == target;
461 ui::EventDispatchDetails WindowEventDispatcher::PreDispatchEvent(
462 ui::EventTarget* target,
464 Window* target_window = static_cast<Window*>(target);
465 CHECK(window()->Contains(target_window));
467 if (!dispatching_held_event_) {
468 bool can_be_held = IsEventCandidateForHold(*event);
469 if (!move_hold_count_ || !can_be_held) {
471 held_move_event_.reset();
472 DispatchDetails details = DispatchHeldEvents();
473 if (details.dispatcher_destroyed || details.target_destroyed)
478 if (event->IsMouseEvent()) {
479 PreDispatchMouseEvent(target_window, static_cast<ui::MouseEvent*>(event));
480 } else if (event->IsScrollEvent()) {
481 PreDispatchLocatedEvent(target_window,
482 static_cast<ui::ScrollEvent*>(event));
483 } else if (event->IsTouchEvent()) {
484 PreDispatchTouchEvent(target_window, static_cast<ui::TouchEvent*>(event));
486 old_dispatch_target_ = event_dispatch_target_;
487 event_dispatch_target_ = static_cast<Window*>(target);
488 return DispatchDetails();
491 ui::EventDispatchDetails WindowEventDispatcher::PostDispatchEvent(
492 ui::EventTarget* target,
493 const ui::Event& event) {
494 DispatchDetails details;
495 if (!target || target != event_dispatch_target_)
496 details.target_destroyed = true;
497 event_dispatch_target_ = old_dispatch_target_;
498 old_dispatch_target_ = NULL;
500 DCHECK(!event_dispatch_target_ || window()->Contains(event_dispatch_target_));
503 if (event.IsTouchEvent() && !details.target_destroyed) {
504 // Do not let 'held' touch events contribute to any gestures unless it is
506 if (dispatching_held_event_ || !held_move_event_ ||
507 !held_move_event_->IsTouchEvent()) {
509 // Once we've fully migrated to the eager gesture detector, we won't
510 // need to pass an event here.
511 ui::TouchEvent orig_event(static_cast<const ui::TouchEvent&>(event),
512 static_cast<Window*>(event.target()),
515 if (event.result() & ui::ER_CONSUMED)
516 orig_event.StopPropagation();
518 // TODO(tdresser): Move this to PreDispatchTouchEvent, to enable eager
519 // gesture detection. See crbug.com/410280.
520 if (!ui::GestureRecognizer::Get()
521 ->ProcessTouchEventPreDispatch(orig_event,
522 static_cast<Window*>(target))) {
526 scoped_ptr<ui::GestureRecognizer::Gestures> gestures;
529 ui::GestureRecognizer::Get()->ProcessTouchEventPostDispatch(
530 orig_event, event.result(), static_cast<Window*>(target)));
532 return ProcessGestures(gestures.get());
539 ////////////////////////////////////////////////////////////////////////////////
540 // WindowEventDispatcher, ui::GestureEventHelper implementation:
542 bool WindowEventDispatcher::CanDispatchToConsumer(
543 ui::GestureConsumer* consumer) {
544 Window* consumer_window = ConsumerToWindow(consumer);
545 return (consumer_window && consumer_window->GetRootWindow() == window());
548 void WindowEventDispatcher::DispatchCancelTouchEvent(ui::TouchEvent* event) {
549 DispatchDetails details = OnEventFromSource(event);
550 if (details.dispatcher_destroyed)
554 ////////////////////////////////////////////////////////////////////////////////
555 // WindowEventDispatcher, WindowObserver implementation:
557 void WindowEventDispatcher::OnWindowDestroying(Window* window) {
558 if (!host_->window()->Contains(window))
561 DispatchMouseExitToHidingWindow(window);
562 SynthesizeMouseMoveAfterChangeToWindow(window);
565 void WindowEventDispatcher::OnWindowDestroyed(Window* window) {
566 // We observe all windows regardless of what root Window (if any) they're
568 observer_manager_.Remove(window);
571 void WindowEventDispatcher::OnWindowAddedToRootWindow(Window* attached) {
572 if (!observer_manager_.IsObserving(attached))
573 observer_manager_.Add(attached);
575 if (!host_->window()->Contains(attached))
578 SynthesizeMouseMoveAfterChangeToWindow(attached);
581 void WindowEventDispatcher::OnWindowRemovingFromRootWindow(Window* detached,
583 if (!host_->window()->Contains(detached))
586 DCHECK(client::GetCaptureWindow(window()) != window());
588 DispatchMouseExitToHidingWindow(detached);
589 SynthesizeMouseMoveAfterChangeToWindow(detached);
591 // Hiding the window releases capture which can implicitly destroy the window
592 // so the window may no longer be valid after this call.
593 OnWindowHidden(detached, new_root ? WINDOW_MOVING : WINDOW_HIDDEN);
596 void WindowEventDispatcher::OnWindowVisibilityChanging(Window* window,
598 if (!host_->window()->Contains(window))
601 DispatchMouseExitToHidingWindow(window);
604 void WindowEventDispatcher::OnWindowVisibilityChanged(Window* window,
606 if (!host_->window()->Contains(window))
609 if (window->ContainsPointInRoot(GetLastMouseLocationInRoot()))
610 PostSynthesizeMouseMove();
612 // Hiding the window releases capture which can implicitly destroy the window
613 // so the window may no longer be valid after this call.
615 OnWindowHidden(window, WINDOW_HIDDEN);
618 void WindowEventDispatcher::OnWindowBoundsChanged(Window* window,
619 const gfx::Rect& old_bounds,
620 const gfx::Rect& new_bounds) {
621 if (!host_->window()->Contains(window))
624 if (window == host_->window()) {
625 TRACE_EVENT1("ui", "WindowEventDispatcher::OnWindowBoundsChanged(root)",
626 "size", new_bounds.size().ToString());
628 DispatchDetails details = DispatchHeldEvents();
629 if (details.dispatcher_destroyed)
632 synthesize_mouse_move_ = false;
635 if (window->IsVisible() && !window->ignore_events()) {
636 gfx::Rect old_bounds_in_root = old_bounds, new_bounds_in_root = new_bounds;
637 Window::ConvertRectToTarget(window->parent(), host_->window(),
638 &old_bounds_in_root);
639 Window::ConvertRectToTarget(window->parent(), host_->window(),
640 &new_bounds_in_root);
641 gfx::Point last_mouse_location = GetLastMouseLocationInRoot();
642 if (old_bounds_in_root.Contains(last_mouse_location) !=
643 new_bounds_in_root.Contains(last_mouse_location)) {
644 PostSynthesizeMouseMove();
649 void WindowEventDispatcher::OnWindowTransforming(Window* window) {
650 if (!host_->window()->Contains(window))
653 SynthesizeMouseMoveAfterChangeToWindow(window);
656 void WindowEventDispatcher::OnWindowTransformed(Window* window) {
657 if (!host_->window()->Contains(window))
660 SynthesizeMouseMoveAfterChangeToWindow(window);
663 ///////////////////////////////////////////////////////////////////////////////
664 // WindowEventDispatcher, EnvObserver implementation:
666 void WindowEventDispatcher::OnWindowInitialized(Window* window) {
667 observer_manager_.Add(window);
670 ////////////////////////////////////////////////////////////////////////////////
671 // WindowEventDispatcher, private:
673 ui::EventDispatchDetails WindowEventDispatcher::DispatchHeldEvents() {
674 if (!held_repostable_event_ && !held_move_event_)
675 return DispatchDetails();
677 CHECK(!dispatching_held_event_);
678 dispatching_held_event_ = true;
680 DispatchDetails dispatch_details;
681 if (held_repostable_event_) {
682 if (held_repostable_event_->type() == ui::ET_MOUSE_PRESSED) {
683 scoped_ptr<ui::MouseEvent> mouse_event(
684 static_cast<ui::MouseEvent*>(held_repostable_event_.release()));
685 dispatch_details = OnEventFromSource(mouse_event.get());
687 // TODO(rbyers): GESTURE_TAP_DOWN not yet supported: crbug.com/170987.
690 if (dispatch_details.dispatcher_destroyed)
691 return dispatch_details;
694 if (held_move_event_) {
695 // If a mouse move has been synthesized, the target location is suspect,
696 // so drop the held mouse event.
697 if (held_move_event_->IsTouchEvent() ||
698 (held_move_event_->IsMouseEvent() && !synthesize_mouse_move_)) {
699 dispatch_details = OnEventFromSource(held_move_event_.get());
701 if (!dispatch_details.dispatcher_destroyed)
702 held_move_event_.reset();
705 if (!dispatch_details.dispatcher_destroyed)
706 dispatching_held_event_ = false;
707 return dispatch_details;
710 void WindowEventDispatcher::PostSynthesizeMouseMove() {
711 if (synthesize_mouse_move_)
713 synthesize_mouse_move_ = true;
714 base::MessageLoop::current()->PostNonNestableTask(
716 base::Bind(base::IgnoreResult(
717 &WindowEventDispatcher::SynthesizeMouseMoveEvent),
718 held_event_factory_.GetWeakPtr()));
721 void WindowEventDispatcher::SynthesizeMouseMoveAfterChangeToWindow(
723 if (window->IsVisible() &&
724 window->ContainsPointInRoot(GetLastMouseLocationInRoot())) {
725 PostSynthesizeMouseMove();
729 ui::EventDispatchDetails WindowEventDispatcher::SynthesizeMouseMoveEvent() {
730 DispatchDetails details;
731 if (!synthesize_mouse_move_)
733 synthesize_mouse_move_ = false;
735 // If one of the mouse buttons is currently down, then do not synthesize a
736 // mouse-move event. In such cases, aura could synthesize a DRAGGED event
737 // instead of a MOVED event, but in multi-display/multi-host scenarios, the
738 // DRAGGED event can be synthesized in the incorrect host. So avoid
739 // synthesizing any events at all.
740 if (Env::GetInstance()->mouse_button_flags())
743 gfx::Point root_mouse_location = GetLastMouseLocationInRoot();
744 if (!window()->bounds().Contains(root_mouse_location))
746 gfx::Point host_mouse_location = root_mouse_location;
747 host_->ConvertPointToHost(&host_mouse_location);
748 ui::MouseEvent event(ui::ET_MOUSE_MOVED,
751 ui::EF_IS_SYNTHESIZED,
753 return OnEventFromSource(&event);
756 void WindowEventDispatcher::PreDispatchLocatedEvent(Window* target,
757 ui::LocatedEvent* event) {
758 int flags = event->flags();
759 if (IsNonClientLocation(target, event->location()))
760 flags |= ui::EF_IS_NON_CLIENT;
761 event->set_flags(flags);
763 if (!dispatching_held_event_ &&
764 (event->IsMouseEvent() || event->IsScrollEvent()) &&
765 !(event->flags() & ui::EF_IS_SYNTHESIZED)) {
766 if (event->type() != ui::ET_MOUSE_CAPTURE_CHANGED)
767 SetLastMouseLocation(window(), event->root_location());
768 synthesize_mouse_move_ = false;
772 void WindowEventDispatcher::PreDispatchMouseEvent(Window* target,
773 ui::MouseEvent* event) {
774 client::CursorClient* cursor_client = client::GetCursorClient(window());
775 // We allow synthesized mouse exit events through even if mouse events are
776 // disabled. This ensures that hover state, etc on controls like buttons is
779 !cursor_client->IsMouseEventsEnabled() &&
780 (event->flags() & ui::EF_IS_SYNTHESIZED) &&
781 (event->type() != ui::ET_MOUSE_EXITED)) {
786 if (IsEventCandidateForHold(*event) && !dispatching_held_event_) {
787 if (move_hold_count_) {
788 if (!(event->flags() & ui::EF_IS_SYNTHESIZED) &&
789 event->type() != ui::ET_MOUSE_CAPTURE_CHANGED) {
790 SetLastMouseLocation(window(), event->root_location());
792 held_move_event_.reset(new ui::MouseEvent(*event, target, window()));
796 // We may have a held event for a period between the time move_hold_count_
797 // fell to 0 and the DispatchHeldEvents executes. Since we're going to
798 // dispatch the new event directly below, we can reset the old one.
799 held_move_event_.reset();
803 const int kMouseButtonFlagMask = ui::EF_LEFT_MOUSE_BUTTON |
804 ui::EF_MIDDLE_MOUSE_BUTTON |
805 ui::EF_RIGHT_MOUSE_BUTTON;
806 switch (event->type()) {
807 case ui::ET_MOUSE_EXITED:
808 if (!target || target == window()) {
809 DispatchDetails details =
810 DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_EXITED);
811 if (details.dispatcher_destroyed) {
815 mouse_moved_handler_ = NULL;
818 case ui::ET_MOUSE_MOVED:
819 // Send an exit to the current |mouse_moved_handler_| and an enter to
820 // |target|. Take care that both us and |target| aren't destroyed during
822 if (target != mouse_moved_handler_) {
823 aura::Window* old_mouse_moved_handler = mouse_moved_handler_;
824 WindowTracker live_window;
825 live_window.Add(target);
826 DispatchDetails details =
827 DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_EXITED);
828 if (details.dispatcher_destroyed) {
832 // If the |mouse_moved_handler_| changes out from under us, assume a
833 // nested message loop ran and we don't need to do anything.
834 if (mouse_moved_handler_ != old_mouse_moved_handler) {
838 if (!live_window.Contains(target) || details.target_destroyed) {
839 mouse_moved_handler_ = NULL;
843 live_window.Remove(target);
845 mouse_moved_handler_ = target;
846 details = DispatchMouseEnterOrExit(*event, ui::ET_MOUSE_ENTERED);
847 if (details.dispatcher_destroyed || details.target_destroyed) {
853 case ui::ET_MOUSE_PRESSED:
854 // Don't set the mouse pressed handler for non client mouse down events.
855 // These are only sent by Windows and are not always followed with non
856 // client mouse up events which causes subsequent mouse events to be
857 // sent to the wrong target.
858 if (!(event->flags() & ui::EF_IS_NON_CLIENT) && !mouse_pressed_handler_)
859 mouse_pressed_handler_ = target;
860 Env::GetInstance()->set_mouse_button_flags(
861 event->flags() & kMouseButtonFlagMask);
863 case ui::ET_MOUSE_RELEASED:
864 mouse_pressed_handler_ = NULL;
865 Env::GetInstance()->set_mouse_button_flags(event->flags() &
866 kMouseButtonFlagMask & ~event->changed_button_flags());
872 PreDispatchLocatedEvent(target, event);
875 void WindowEventDispatcher::PreDispatchTouchEvent(Window* target,
876 ui::TouchEvent* event) {
877 switch (event->type()) {
878 case ui::ET_TOUCH_PRESSED:
879 touch_ids_down_ |= (1 << event->touch_id());
880 Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
883 // Handle ET_TOUCH_CANCELLED only if it has a native event.
884 case ui::ET_TOUCH_CANCELLED:
885 if (!event->HasNativeEvent())
888 case ui::ET_TOUCH_RELEASED:
889 touch_ids_down_ = (touch_ids_down_ | (1 << event->touch_id())) ^
890 (1 << event->touch_id());
891 Env::GetInstance()->set_touch_down(touch_ids_down_ != 0);
894 case ui::ET_TOUCH_MOVED:
895 if (move_hold_count_ && !dispatching_held_event_) {
896 held_move_event_.reset(new ui::TouchEvent(*event, target, window()));
907 PreDispatchLocatedEvent(target, event);