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/views/mouse_watcher.h"
8 #include "base/compiler_specific.h"
9 #include "base/event_types.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "ui/aura/env.h"
13 #include "ui/aura/window.h"
14 #include "ui/events/event.h"
15 #include "ui/events/event_constants.h"
16 #include "ui/events/event_handler.h"
17 #include "ui/events/event_utils.h"
18 #include "ui/gfx/screen.h"
22 // Amount of time between when the mouse moves outside the Host's zone and when
23 // the listener is notified.
24 const int kNotifyListenerTimeMs
= 300;
26 class MouseWatcher::Observer
: public ui::EventHandler
{
28 explicit Observer(MouseWatcher
* mouse_watcher
)
29 : mouse_watcher_(mouse_watcher
),
30 notify_listener_factory_(this) {
31 aura::Env::GetInstance()->AddPreTargetHandler(this);
35 aura::Env::GetInstance()->RemovePreTargetHandler(this);
38 // ui::EventHandler implementation:
39 virtual void OnMouseEvent(ui::MouseEvent
* event
) OVERRIDE
{
40 switch (event
->type()) {
41 case ui::ET_MOUSE_MOVED
:
42 case ui::ET_MOUSE_DRAGGED
:
43 HandleMouseEvent(MouseWatcherHost::MOUSE_MOVE
);
45 case ui::ET_MOUSE_EXITED
:
46 HandleMouseEvent(MouseWatcherHost::MOUSE_EXIT
);
54 MouseWatcherHost
* host() const { return mouse_watcher_
->host_
.get(); }
56 // Called when a mouse event we're interested is seen.
57 void HandleMouseEvent(MouseWatcherHost::MouseEventType event_type
) {
58 // It's safe to use last_mouse_location() here as this function is invoked
59 // during event dispatching.
60 if (!host()->Contains(aura::Env::GetInstance()->last_mouse_location(),
62 // Mouse moved outside the host's zone, start a timer to notify the
64 if (!notify_listener_factory_
.HasWeakPtrs()) {
65 base::MessageLoop::current()->PostDelayedTask(
67 base::Bind(&Observer::NotifyListener
,
68 notify_listener_factory_
.GetWeakPtr()),
69 event_type
== MouseWatcherHost::MOUSE_MOVE
70 ? base::TimeDelta::FromMilliseconds(kNotifyListenerTimeMs
)
71 : mouse_watcher_
->notify_on_exit_time_
);
74 // Mouse moved quickly out of the host and then into it again, so cancel
76 notify_listener_factory_
.InvalidateWeakPtrs();
80 void NotifyListener() {
81 mouse_watcher_
->NotifyListener();
82 // WARNING: we've been deleted.
86 MouseWatcher
* mouse_watcher_
;
88 // A factory that is used to construct a delayed callback to the listener.
89 base::WeakPtrFactory
<Observer
> notify_listener_factory_
;
91 DISALLOW_COPY_AND_ASSIGN(Observer
);
94 MouseWatcherListener::~MouseWatcherListener() {
97 MouseWatcherHost::~MouseWatcherHost() {
100 MouseWatcher::MouseWatcher(MouseWatcherHost
* host
,
101 MouseWatcherListener
* listener
)
104 notify_on_exit_time_(base::TimeDelta::FromMilliseconds(
105 kNotifyListenerTimeMs
)) {
108 MouseWatcher::~MouseWatcher() {
111 void MouseWatcher::Start() {
113 observer_
.reset(new Observer(this));
116 void MouseWatcher::Stop() {
117 observer_
.reset(NULL
);
120 void MouseWatcher::NotifyListener() {
121 observer_
.reset(NULL
);
122 listener_
->MouseMovedOutOfHost();