This source file includes following definitions.
- notify_listener_factory_
- Observer
- OnMouseEvent
- host
- HandleMouseEvent
- NotifyListener
- notify_on_exit_time_
- Start
- Stop
- NotifyListener
#include "ui/views/mouse_watcher.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/event_types.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/event_handler.h"
#include "ui/events/event_utils.h"
#include "ui/gfx/screen.h"
namespace views {
const int kNotifyListenerTimeMs = 300;
class MouseWatcher::Observer : public ui::EventHandler {
public:
explicit Observer(MouseWatcher* mouse_watcher)
: mouse_watcher_(mouse_watcher),
notify_listener_factory_(this) {
aura::Env::GetInstance()->AddPreTargetHandler(this);
}
virtual ~Observer() {
aura::Env::GetInstance()->RemovePreTargetHandler(this);
}
virtual void OnMouseEvent(ui::MouseEvent* event) OVERRIDE {
switch (event->type()) {
case ui::ET_MOUSE_MOVED:
case ui::ET_MOUSE_DRAGGED:
HandleMouseEvent(MouseWatcherHost::MOUSE_MOVE);
break;
case ui::ET_MOUSE_EXITED:
HandleMouseEvent(MouseWatcherHost::MOUSE_EXIT);
break;
default:
break;
}
}
private:
MouseWatcherHost* host() const { return mouse_watcher_->host_.get(); }
void HandleMouseEvent(MouseWatcherHost::MouseEventType event_type) {
if (!host()->Contains(aura::Env::GetInstance()->last_mouse_location(),
event_type)) {
if (!notify_listener_factory_.HasWeakPtrs()) {
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&Observer::NotifyListener,
notify_listener_factory_.GetWeakPtr()),
event_type == MouseWatcherHost::MOUSE_MOVE
? base::TimeDelta::FromMilliseconds(kNotifyListenerTimeMs)
: mouse_watcher_->notify_on_exit_time_);
}
} else {
notify_listener_factory_.InvalidateWeakPtrs();
}
}
void NotifyListener() {
mouse_watcher_->NotifyListener();
}
private:
MouseWatcher* mouse_watcher_;
base::WeakPtrFactory<Observer> notify_listener_factory_;
DISALLOW_COPY_AND_ASSIGN(Observer);
};
MouseWatcherListener::~MouseWatcherListener() {
}
MouseWatcherHost::~MouseWatcherHost() {
}
MouseWatcher::MouseWatcher(MouseWatcherHost* host,
MouseWatcherListener* listener)
: host_(host),
listener_(listener),
notify_on_exit_time_(base::TimeDelta::FromMilliseconds(
kNotifyListenerTimeMs)) {
}
MouseWatcher::~MouseWatcher() {
}
void MouseWatcher::Start() {
if (!is_observing())
observer_.reset(new Observer(this));
}
void MouseWatcher::Stop() {
observer_.reset(NULL);
}
void MouseWatcher::NotifyListener() {
observer_.reset(NULL);
listener_->MouseMovedOutOfHost();
}
}