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
)
143 Window
* target
= GetGestureTarget(event
);
145 event
->ConvertLocationToTarget(window(), target
);
146 DispatchDetails details
= DispatchEvent(target
, event
);
147 if (details
.dispatcher_destroyed
)
152 DispatchDetails
WindowEventDispatcher::DispatchMouseExitAtPoint(
153 const gfx::Point
& point
) {
154 ui::MouseEvent
event(ui::ET_MOUSE_EXITED
, point
, point
, ui::EF_NONE
,
156 return DispatchMouseEnterOrExit(event
, ui::ET_MOUSE_EXITED
);
159 void WindowEventDispatcher::ProcessedTouchEvent(ui::TouchEvent
* event
,
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
)) {
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
)
179 void WindowEventDispatcher::HoldPointerMoves() {
180 if (!move_hold_count_
)
181 held_event_factory_
.InvalidateWeakPtrs();
183 TRACE_EVENT_ASYNC_BEGIN0("ui", "WindowEventDispatcher::HoldPointerMoves",
187 void WindowEventDispatcher::ReleasePointerMoves() {
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()));
200 TRACE_EVENT_ASYNC_END0("ui", "WindowEventDispatcher::HoldPointerMoves", this);
203 gfx::Point
WindowEventDispatcher::GetLastMouseLocationInRoot() const {
204 gfx::Point location
= Env::GetInstance()->last_mouse_location();
205 client::ScreenPositionClient
* client
=
206 client::GetScreenPositionClient(window());
208 client
->ConvertPointFromScreen(window(), &location
);
212 void WindowEventDispatcher::OnHostLostMouseGrab() {
213 mouse_pressed_handler_
= NULL
;
214 mouse_moved_handler_
= NULL
;
217 void WindowEventDispatcher::OnCursorMovedToRootLocation(
218 const gfx::Point
& root_location
) {
219 SetLastMouseLocation(window(), root_location
);
220 synthesize_mouse_move_
= false;
223 void WindowEventDispatcher::OnPostNotifiedWindowDestroying(Window
* window
) {
224 OnWindowHidden(window
, WINDOW_DESTROYED
);
227 ////////////////////////////////////////////////////////////////////////////////
228 // WindowEventDispatcher, private:
230 Window
* WindowEventDispatcher::window() {
231 return host_
->window();
234 const Window
* WindowEventDispatcher::window() const {
235 return host_
->window();
238 void WindowEventDispatcher::TransformEventForDeviceScaleFactor(
239 ui::LocatedEvent
* event
) {
240 event
->UpdateForRootTransform(host_
->GetInverseRootTransform());
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
)
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());
266 if (!mouse_moved_handler_
|| !mouse_moved_handler_
->delegate() ||
267 !window()->Contains(mouse_moved_handler_
))
268 return DispatchDetails();
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());
278 ui::MouseEvent
translated_event(event
,
280 mouse_moved_handler_
,
282 event
.flags() | ui::EF_IS_SYNTHESIZED
);
283 return DispatchEvent(mouse_moved_handler_
, &translated_event
);
286 ui::EventDispatchDetails
WindowEventDispatcher::ProcessGestures(
287 ui::GestureRecognizer::Gestures
* gestures
) {
288 DispatchDetails details
;
289 if (!gestures
|| gestures
->empty())
292 Window
* target
= GetGestureTarget(gestures
->get().at(0));
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
)
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
;
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
319 if (invisible
->Contains(old_dispatch_target_
))
320 old_dispatch_target_
= NULL
;
322 invisible
->CleanupGestureState();
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
;
337 if (invisible
->Contains(event_dispatch_target_
))
338 event_dispatch_target_
= NULL
;
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
343 if (invisible
->Contains(capture_window
) && invisible
!= window())
344 capture_window
->ReleaseCapture();
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());
356 target
= ConsumerToWindow(
357 ui::GestureRecognizer::Get()->GetTargetForGestureEvent(*event
));
363 ////////////////////////////////////////////////////////////////////////////////
364 // WindowEventDispatcher, aura::client::CaptureDelegate implementation:
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
371 if (mouse_moved_handler_
&& !window()->Contains(mouse_moved_handler_
))
372 mouse_moved_handler_
= NULL
;
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(),
380 DispatchDetails details
= DispatchEvent(old_capture
, &event
);
381 if (details
.dispatcher_destroyed
)
384 if (!details
.target_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::OnEventProcessingStarted(ui::Event
* event
) {
440 // The held events are already in |window()|'s coordinate system. So it is
441 // not necessary to apply the transform to convert from the host's
442 // coordinate system to |window()|'s coordinate system.
443 if (event
->IsLocatedEvent() && !dispatching_held_event_
)
444 TransformEventForDeviceScaleFactor(static_cast<ui::LocatedEvent
*>(event
));
447 ////////////////////////////////////////////////////////////////////////////////
448 // WindowEventDispatcher, ui::EventDispatcherDelegate implementation:
450 bool WindowEventDispatcher::CanDispatchToTarget(ui::EventTarget
* target
) {
451 return event_dispatch_target_
== target
;
454 ui::EventDispatchDetails
WindowEventDispatcher::PreDispatchEvent(
455 ui::EventTarget
* target
,
457 Window
* target_window
= static_cast<Window
*>(target
);
458 CHECK(window()->Contains(target_window
));
460 if (!dispatching_held_event_
) {
461 bool can_be_held
= IsEventCandidateForHold(*event
);
462 if (!move_hold_count_
|| !can_be_held
) {
464 held_move_event_
.reset();
465 DispatchDetails details
= DispatchHeldEvents();
466 if (details
.dispatcher_destroyed
|| details
.target_destroyed
)
471 if (event
->IsMouseEvent()) {
472 PreDispatchMouseEvent(target_window
, static_cast<ui::MouseEvent
*>(event
));
473 } else if (event
->IsScrollEvent()) {
474 PreDispatchLocatedEvent(target_window
,
475 static_cast<ui::ScrollEvent
*>(event
));
476 } else if (event
->IsTouchEvent()) {
477 PreDispatchTouchEvent(target_window
, static_cast<ui::TouchEvent
*>(event
));
479 old_dispatch_target_
= event_dispatch_target_
;
480 event_dispatch_target_
= static_cast<Window
*>(target
);
481 return DispatchDetails();
484 ui::EventDispatchDetails
WindowEventDispatcher::PostDispatchEvent(
485 ui::EventTarget
* target
,
486 const ui::Event
& event
) {
487 DispatchDetails details
;
488 if (!target
|| target
!= event_dispatch_target_
)
489 details
.target_destroyed
= true;
490 event_dispatch_target_
= old_dispatch_target_
;
491 old_dispatch_target_
= NULL
;
493 DCHECK(!event_dispatch_target_
|| window()->Contains(event_dispatch_target_
));
496 if (event
.IsTouchEvent() && !details
.target_destroyed
) {
497 // Do not let 'held' touch events contribute to any gestures unless it is
499 if (dispatching_held_event_
|| !held_move_event_
||
500 !held_move_event_
->IsTouchEvent()) {
502 // Once we've fully migrated to the eager gesture detector, we won't
503 // need to pass an event here.
504 ui::TouchEvent
orig_event(static_cast<const ui::TouchEvent
&>(event
),
505 static_cast<Window
*>(event
.target()),
508 if (event
.result() & ui::ER_CONSUMED
)
509 orig_event
.StopPropagation();
511 // TODO(tdresser): Move this to PreDispatchTouchEvent, to enable eager
512 // gesture detection. See crbug.com/410280.
513 if (!ui::GestureRecognizer::Get()
514 ->ProcessTouchEventPreDispatch(orig_event
,
515 static_cast<Window
*>(target
))) {
519 scoped_ptr
<ui::GestureRecognizer::Gestures
> gestures
;
522 ui::GestureRecognizer::Get()->ProcessTouchEventPostDispatch(
523 orig_event
, event
.result(), static_cast<Window
*>(target
)));
525 return ProcessGestures(gestures
.get());
532 ////////////////////////////////////////////////////////////////////////////////
533 // WindowEventDispatcher, ui::GestureEventHelper implementation:
535 bool WindowEventDispatcher::CanDispatchToConsumer(
536 ui::GestureConsumer
* consumer
) {
537 Window
* consumer_window
= ConsumerToWindow(consumer
);
538 return (consumer_window
&& consumer_window
->GetRootWindow() == window());
541 void WindowEventDispatcher::DispatchCancelTouchEvent(ui::TouchEvent
* event
) {
542 // The touchcancel event's location is based on the last known location of
543 // the pointer, in dips. OnEventFromSource expects events with co-ordinates
544 // in raw pixels, so we convert back to raw pixels here.
545 event
->UpdateForRootTransform(host_
->GetRootTransform());
546 DispatchDetails details
= OnEventFromSource(event
);
547 if (details
.dispatcher_destroyed
)
551 ////////////////////////////////////////////////////////////////////////////////
552 // WindowEventDispatcher, WindowObserver implementation:
554 void WindowEventDispatcher::OnWindowDestroying(Window
* window
) {
555 if (!host_
->window()->Contains(window
))
558 DispatchMouseExitToHidingWindow(window
);
559 SynthesizeMouseMoveAfterChangeToWindow(window
);
562 void WindowEventDispatcher::OnWindowDestroyed(Window
* window
) {
563 // We observe all windows regardless of what root Window (if any) they're
565 observer_manager_
.Remove(window
);
568 void WindowEventDispatcher::OnWindowAddedToRootWindow(Window
* attached
) {
569 if (!observer_manager_
.IsObserving(attached
))
570 observer_manager_
.Add(attached
);
572 if (!host_
->window()->Contains(attached
))
575 SynthesizeMouseMoveAfterChangeToWindow(attached
);
578 void WindowEventDispatcher::OnWindowRemovingFromRootWindow(Window
* detached
,
580 if (!host_
->window()->Contains(detached
))
583 DCHECK(client::GetCaptureWindow(window()) != window());
585 DispatchMouseExitToHidingWindow(detached
);
586 SynthesizeMouseMoveAfterChangeToWindow(detached
);
588 // Hiding the window releases capture which can implicitly destroy the window
589 // so the window may no longer be valid after this call.
590 OnWindowHidden(detached
, new_root
? WINDOW_MOVING
: WINDOW_HIDDEN
);
593 void WindowEventDispatcher::OnWindowVisibilityChanging(Window
* window
,
595 if (!host_
->window()->Contains(window
))
598 DispatchMouseExitToHidingWindow(window
);
601 void WindowEventDispatcher::OnWindowVisibilityChanged(Window
* window
,
603 if (!host_
->window()->Contains(window
))
606 if (window
->ContainsPointInRoot(GetLastMouseLocationInRoot()))
607 PostSynthesizeMouseMove();
609 // Hiding the window releases capture which can implicitly destroy the window
610 // so the window may no longer be valid after this call.
612 OnWindowHidden(window
, WINDOW_HIDDEN
);
615 void WindowEventDispatcher::OnWindowBoundsChanged(Window
* window
,
616 const gfx::Rect
& old_bounds
,
617 const gfx::Rect
& new_bounds
) {
618 if (!host_
->window()->Contains(window
))
621 if (window
== host_
->window()) {
622 TRACE_EVENT1("ui", "WindowEventDispatcher::OnWindowBoundsChanged(root)",
623 "size", new_bounds
.size().ToString());
625 DispatchDetails details
= DispatchHeldEvents();
626 if (details
.dispatcher_destroyed
)
629 synthesize_mouse_move_
= false;
632 if (window
->IsVisible() && !window
->ignore_events()) {
633 gfx::Rect old_bounds_in_root
= old_bounds
, new_bounds_in_root
= new_bounds
;
634 Window::ConvertRectToTarget(window
->parent(), host_
->window(),
635 &old_bounds_in_root
);
636 Window::ConvertRectToTarget(window
->parent(), host_
->window(),
637 &new_bounds_in_root
);
638 gfx::Point last_mouse_location
= GetLastMouseLocationInRoot();
639 if (old_bounds_in_root
.Contains(last_mouse_location
) !=
640 new_bounds_in_root
.Contains(last_mouse_location
)) {
641 PostSynthesizeMouseMove();
646 void WindowEventDispatcher::OnWindowTransforming(Window
* window
) {
647 if (!host_
->window()->Contains(window
))
650 SynthesizeMouseMoveAfterChangeToWindow(window
);
653 void WindowEventDispatcher::OnWindowTransformed(Window
* window
) {
654 if (!host_
->window()->Contains(window
))
657 SynthesizeMouseMoveAfterChangeToWindow(window
);
660 ///////////////////////////////////////////////////////////////////////////////
661 // WindowEventDispatcher, EnvObserver implementation:
663 void WindowEventDispatcher::OnWindowInitialized(Window
* window
) {
664 observer_manager_
.Add(window
);
667 ////////////////////////////////////////////////////////////////////////////////
668 // WindowEventDispatcher, private:
670 ui::EventDispatchDetails
WindowEventDispatcher::DispatchHeldEvents() {
671 if (!held_repostable_event_
&& !held_move_event_
)
672 return DispatchDetails();
674 CHECK(!dispatching_held_event_
);
675 dispatching_held_event_
= true;
677 DispatchDetails dispatch_details
;
678 if (held_repostable_event_
) {
679 if (held_repostable_event_
->type() == ui::ET_MOUSE_PRESSED
) {
680 scoped_ptr
<ui::MouseEvent
> mouse_event(
681 static_cast<ui::MouseEvent
*>(held_repostable_event_
.release()));
682 dispatch_details
= OnEventFromSource(mouse_event
.get());
684 // TODO(rbyers): GESTURE_TAP_DOWN not yet supported: crbug.com/170987.
687 if (dispatch_details
.dispatcher_destroyed
)
688 return dispatch_details
;
691 if (held_move_event_
) {
692 // If a mouse move has been synthesized, the target location is suspect,
693 // so drop the held mouse event.
694 if (held_move_event_
->IsTouchEvent() ||
695 (held_move_event_
->IsMouseEvent() && !synthesize_mouse_move_
)) {
696 dispatch_details
= OnEventFromSource(held_move_event_
.get());
698 if (!dispatch_details
.dispatcher_destroyed
)
699 held_move_event_
.reset();
702 if (!dispatch_details
.dispatcher_destroyed
)
703 dispatching_held_event_
= false;
704 return dispatch_details
;
707 void WindowEventDispatcher::PostSynthesizeMouseMove() {
708 if (synthesize_mouse_move_
)
710 synthesize_mouse_move_
= true;
711 base::MessageLoop::current()->PostNonNestableTask(
713 base::Bind(base::IgnoreResult(
714 &WindowEventDispatcher::SynthesizeMouseMoveEvent
),
715 held_event_factory_
.GetWeakPtr()));
718 void WindowEventDispatcher::SynthesizeMouseMoveAfterChangeToWindow(
720 if (window
->IsVisible() &&
721 window
->ContainsPointInRoot(GetLastMouseLocationInRoot())) {
722 PostSynthesizeMouseMove();
726 ui::EventDispatchDetails
WindowEventDispatcher::SynthesizeMouseMoveEvent() {
727 DispatchDetails details
;
728 if (!synthesize_mouse_move_
)
730 synthesize_mouse_move_
= false;
732 // If one of the mouse buttons is currently down, then do not synthesize a
733 // mouse-move event. In such cases, aura could synthesize a DRAGGED event
734 // instead of a MOVED event, but in multi-display/multi-host scenarios, the
735 // DRAGGED event can be synthesized in the incorrect host. So avoid
736 // synthesizing any events at all.
737 if (Env::GetInstance()->mouse_button_flags())
740 gfx::Point root_mouse_location
= GetLastMouseLocationInRoot();
741 if (!window()->bounds().Contains(root_mouse_location
))
743 gfx::Point host_mouse_location
= root_mouse_location
;
744 host_
->ConvertPointToHost(&host_mouse_location
);
745 ui::MouseEvent
event(ui::ET_MOUSE_MOVED
,
748 ui::EF_IS_SYNTHESIZED
,
750 return OnEventFromSource(&event
);
753 void WindowEventDispatcher::PreDispatchLocatedEvent(Window
* target
,
754 ui::LocatedEvent
* event
) {
755 int flags
= event
->flags();
756 if (IsNonClientLocation(target
, event
->location()))
757 flags
|= ui::EF_IS_NON_CLIENT
;
758 event
->set_flags(flags
);
760 if (!dispatching_held_event_
&&
761 (event
->IsMouseEvent() || event
->IsScrollEvent()) &&
762 !(event
->flags() & ui::EF_IS_SYNTHESIZED
)) {
763 if (event
->type() != ui::ET_MOUSE_CAPTURE_CHANGED
)
764 SetLastMouseLocation(window(), event
->root_location());
765 synthesize_mouse_move_
= false;
769 void WindowEventDispatcher::PreDispatchMouseEvent(Window
* target
,
770 ui::MouseEvent
* event
) {
771 client::CursorClient
* cursor_client
= client::GetCursorClient(window());
772 // We allow synthesized mouse exit events through even if mouse events are
773 // disabled. This ensures that hover state, etc on controls like buttons is
776 !cursor_client
->IsMouseEventsEnabled() &&
777 (event
->flags() & ui::EF_IS_SYNTHESIZED
) &&
778 (event
->type() != ui::ET_MOUSE_EXITED
)) {
783 if (IsEventCandidateForHold(*event
) && !dispatching_held_event_
) {
784 if (move_hold_count_
) {
785 if (!(event
->flags() & ui::EF_IS_SYNTHESIZED
) &&
786 event
->type() != ui::ET_MOUSE_CAPTURE_CHANGED
) {
787 SetLastMouseLocation(window(), event
->root_location());
789 held_move_event_
.reset(new ui::MouseEvent(*event
, target
, window()));
793 // We may have a held event for a period between the time move_hold_count_
794 // fell to 0 and the DispatchHeldEvents executes. Since we're going to
795 // dispatch the new event directly below, we can reset the old one.
796 held_move_event_
.reset();
800 const int kMouseButtonFlagMask
= ui::EF_LEFT_MOUSE_BUTTON
|
801 ui::EF_MIDDLE_MOUSE_BUTTON
|
802 ui::EF_RIGHT_MOUSE_BUTTON
;
803 switch (event
->type()) {
804 case ui::ET_MOUSE_EXITED
:
805 if (!target
|| target
== window()) {
806 DispatchDetails details
=
807 DispatchMouseEnterOrExit(*event
, ui::ET_MOUSE_EXITED
);
808 if (details
.dispatcher_destroyed
) {
812 mouse_moved_handler_
= NULL
;
815 case ui::ET_MOUSE_MOVED
:
816 // Send an exit to the current |mouse_moved_handler_| and an enter to
817 // |target|. Take care that both us and |target| aren't destroyed during
819 if (target
!= mouse_moved_handler_
) {
820 aura::Window
* old_mouse_moved_handler
= mouse_moved_handler_
;
821 WindowTracker live_window
;
822 live_window
.Add(target
);
823 DispatchDetails details
=
824 DispatchMouseEnterOrExit(*event
, ui::ET_MOUSE_EXITED
);
825 if (details
.dispatcher_destroyed
) {
829 // If the |mouse_moved_handler_| changes out from under us, assume a
830 // nested message loop ran and we don't need to do anything.
831 if (mouse_moved_handler_
!= old_mouse_moved_handler
) {
835 if (!live_window
.Contains(target
) || details
.target_destroyed
) {
836 mouse_moved_handler_
= NULL
;
840 live_window
.Remove(target
);
842 mouse_moved_handler_
= target
;
843 details
= DispatchMouseEnterOrExit(*event
, ui::ET_MOUSE_ENTERED
);
844 if (details
.dispatcher_destroyed
|| details
.target_destroyed
) {
850 case ui::ET_MOUSE_PRESSED
:
851 // Don't set the mouse pressed handler for non client mouse down events.
852 // These are only sent by Windows and are not always followed with non
853 // client mouse up events which causes subsequent mouse events to be
854 // sent to the wrong target.
855 if (!(event
->flags() & ui::EF_IS_NON_CLIENT
) && !mouse_pressed_handler_
)
856 mouse_pressed_handler_
= target
;
857 Env::GetInstance()->set_mouse_button_flags(
858 event
->flags() & kMouseButtonFlagMask
);
860 case ui::ET_MOUSE_RELEASED
:
861 mouse_pressed_handler_
= NULL
;
862 Env::GetInstance()->set_mouse_button_flags(event
->flags() &
863 kMouseButtonFlagMask
& ~event
->changed_button_flags());
869 PreDispatchLocatedEvent(target
, event
);
872 void WindowEventDispatcher::PreDispatchTouchEvent(Window
* target
,
873 ui::TouchEvent
* event
) {
874 switch (event
->type()) {
875 case ui::ET_TOUCH_PRESSED
:
876 touch_ids_down_
|= (1 << event
->touch_id());
877 Env::GetInstance()->set_touch_down(touch_ids_down_
!= 0);
880 // Handle ET_TOUCH_CANCELLED only if it has a native event.
881 case ui::ET_TOUCH_CANCELLED
:
882 if (!event
->HasNativeEvent())
885 case ui::ET_TOUCH_RELEASED
:
886 touch_ids_down_
= (touch_ids_down_
| (1 << event
->touch_id())) ^
887 (1 << event
->touch_id());
888 Env::GetInstance()->set_touch_down(touch_ids_down_
!= 0);
891 case ui::ET_TOUCH_MOVED
:
892 if (move_hold_count_
&& !dispatching_held_event_
) {
893 held_move_event_
.reset(new ui::TouchEvent(*event
, target
, window()));
904 PreDispatchLocatedEvent(target
, event
);