1 // Copyright (c) 2012 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 #ifndef UI_VIEWS_MOUSE_WATCHER_H_
6 #define UI_VIEWS_MOUSE_WATCHER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h"
11 #include "ui/gfx/insets.h"
12 #include "ui/views/views_export.h"
20 // MouseWatcherListener is notified when the mouse moves outside the host.
21 class VIEWS_EXPORT MouseWatcherListener
{
23 virtual void MouseMovedOutOfHost() = 0;
26 virtual ~MouseWatcherListener();
29 // The MouseWatcherHost determines what region is to be monitored.
30 class VIEWS_EXPORT MouseWatcherHost
{
32 // The type of mouse event.
39 virtual ~MouseWatcherHost();
41 // Return false when the mouse has moved outside the monitored region.
42 virtual bool Contains(const gfx::Point
& screen_point
,
43 MouseEventType type
) = 0;
46 // MouseWatcher is used to watch mouse movement and notify its listener when the
47 // mouse moves outside the bounds of a MouseWatcherHost.
48 class VIEWS_EXPORT MouseWatcher
{
50 // Creates a new MouseWatcher. The |listener| will be notified when the |host|
51 // determines that the mouse has moved outside its monitored region.
52 // |host| will be owned by the watcher and deleted upon completion.
53 MouseWatcher(MouseWatcherHost
* host
, MouseWatcherListener
* listener
);
56 // Sets the amount to delay before notifying the listener when the mouse exits
57 // the host by way of going to another window.
58 void set_notify_on_exit_time(base::TimeDelta time
) {
59 notify_on_exit_time_
= time
;
62 // Starts watching mouse movements. When the mouse moves outside the bounds of
63 // the host the listener is notified. |Start| may be invoked any number of
64 // times. If the mouse moves outside the bounds of the host the listener is
65 // notified and watcher stops watching events.
68 // Stops watching mouse events.
74 // Are we currently observing events?
75 bool is_observing() const { return observer_
.get() != NULL
; }
77 // Notifies the listener and stops watching events.
78 void NotifyListener();
80 // Host we're listening for events over.
81 scoped_ptr
<MouseWatcherHost
> host_
;
84 MouseWatcherListener
* listener_
;
86 // Does the actual work of listening for mouse events.
87 scoped_ptr
<Observer
> observer_
;
89 // See description above setter.
90 base::TimeDelta notify_on_exit_time_
;
92 DISALLOW_COPY_AND_ASSIGN(MouseWatcher
);
97 #endif // UI_VIEWS_MOUSE_WATCHER_H_