| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/mouse_watcher.h" | 5 #include "ui/views/mouse_watcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/event_types.h" | 9 #include "base/event_types.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/message_loop/message_loop.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" | 12 #include "ui/events/event.h" |
| 15 #include "ui/events/event_constants.h" | 13 #include "ui/events/event_constants.h" |
| 16 #include "ui/events/event_handler.h" | 14 #include "ui/events/event_handler.h" |
| 17 #include "ui/events/event_utils.h" | 15 #include "ui/events/event_utils.h" |
| 18 #include "ui/gfx/screen.h" | 16 #include "ui/views/event_monitor.h" |
| 19 | 17 |
| 20 namespace views { | 18 namespace views { |
| 21 | 19 |
| 22 // Amount of time between when the mouse moves outside the Host's zone and when | 20 // Amount of time between when the mouse moves outside the Host's zone and when |
| 23 // the listener is notified. | 21 // the listener is notified. |
| 24 const int kNotifyListenerTimeMs = 300; | 22 const int kNotifyListenerTimeMs = 300; |
| 25 | 23 |
| 26 class MouseWatcher::Observer : public ui::EventHandler { | 24 class MouseWatcher::Observer : public ui::EventHandler { |
| 27 public: | 25 public: |
| 28 explicit Observer(MouseWatcher* mouse_watcher) | 26 explicit Observer(MouseWatcher* mouse_watcher) |
| 29 : mouse_watcher_(mouse_watcher), | 27 : mouse_watcher_(mouse_watcher), |
| 28 event_monitor_(views::EventMonitor::Create(this)), |
| 30 notify_listener_factory_(this) { | 29 notify_listener_factory_(this) { |
| 31 aura::Env::GetInstance()->AddPreTargetHandler(this); | |
| 32 } | |
| 33 | |
| 34 virtual ~Observer() { | |
| 35 aura::Env::GetInstance()->RemovePreTargetHandler(this); | |
| 36 } | 30 } |
| 37 | 31 |
| 38 // ui::EventHandler implementation: | 32 // ui::EventHandler implementation: |
| 39 virtual void OnMouseEvent(ui::MouseEvent* event) override { | 33 virtual void OnMouseEvent(ui::MouseEvent* event) override { |
| 40 switch (event->type()) { | 34 switch (event->type()) { |
| 41 case ui::ET_MOUSE_MOVED: | 35 case ui::ET_MOUSE_MOVED: |
| 42 case ui::ET_MOUSE_DRAGGED: | 36 case ui::ET_MOUSE_DRAGGED: |
| 43 HandleMouseEvent(MouseWatcherHost::MOUSE_MOVE); | 37 HandleMouseEvent(MouseWatcherHost::MOUSE_MOVE); |
| 44 break; | 38 break; |
| 45 case ui::ET_MOUSE_EXITED: | 39 case ui::ET_MOUSE_EXITED: |
| 46 HandleMouseEvent(MouseWatcherHost::MOUSE_EXIT); | 40 HandleMouseEvent(MouseWatcherHost::MOUSE_EXIT); |
| 47 break; | 41 break; |
| 48 default: | 42 default: |
| 49 break; | 43 break; |
| 50 } | 44 } |
| 51 } | 45 } |
| 52 | 46 |
| 53 private: | 47 private: |
| 54 MouseWatcherHost* host() const { return mouse_watcher_->host_.get(); } | 48 MouseWatcherHost* host() const { return mouse_watcher_->host_.get(); } |
| 55 | 49 |
| 56 // Called when a mouse event we're interested is seen. | 50 // Called when a mouse event we're interested is seen. |
| 57 void HandleMouseEvent(MouseWatcherHost::MouseEventType event_type) { | 51 void HandleMouseEvent(MouseWatcherHost::MouseEventType event_type) { |
| 58 // It's safe to use last_mouse_location() here as this function is invoked | 52 // It's safe to use last_mouse_location() here as this function is invoked |
| 59 // during event dispatching. | 53 // during event dispatching. |
| 60 if (!host()->Contains(aura::Env::GetInstance()->last_mouse_location(), | 54 if (!host()->Contains(EventMonitor::GetLastMouseLocation(), event_type)) { |
| 61 event_type)) { | |
| 62 // Mouse moved outside the host's zone, start a timer to notify the | 55 // Mouse moved outside the host's zone, start a timer to notify the |
| 63 // listener. | 56 // listener. |
| 64 if (!notify_listener_factory_.HasWeakPtrs()) { | 57 if (!notify_listener_factory_.HasWeakPtrs()) { |
| 65 base::MessageLoop::current()->PostDelayedTask( | 58 base::MessageLoop::current()->PostDelayedTask( |
| 66 FROM_HERE, | 59 FROM_HERE, |
| 67 base::Bind(&Observer::NotifyListener, | 60 base::Bind(&Observer::NotifyListener, |
| 68 notify_listener_factory_.GetWeakPtr()), | 61 notify_listener_factory_.GetWeakPtr()), |
| 69 event_type == MouseWatcherHost::MOUSE_MOVE | 62 event_type == MouseWatcherHost::MOUSE_MOVE |
| 70 ? base::TimeDelta::FromMilliseconds(kNotifyListenerTimeMs) | 63 ? base::TimeDelta::FromMilliseconds(kNotifyListenerTimeMs) |
| 71 : mouse_watcher_->notify_on_exit_time_); | 64 : mouse_watcher_->notify_on_exit_time_); |
| 72 } | 65 } |
| 73 } else { | 66 } else { |
| 74 // Mouse moved quickly out of the host and then into it again, so cancel | 67 // Mouse moved quickly out of the host and then into it again, so cancel |
| 75 // the timer. | 68 // the timer. |
| 76 notify_listener_factory_.InvalidateWeakPtrs(); | 69 notify_listener_factory_.InvalidateWeakPtrs(); |
| 77 } | 70 } |
| 78 } | 71 } |
| 79 | 72 |
| 80 void NotifyListener() { | 73 void NotifyListener() { |
| 81 mouse_watcher_->NotifyListener(); | 74 mouse_watcher_->NotifyListener(); |
| 82 // WARNING: we've been deleted. | 75 // WARNING: we've been deleted. |
| 83 } | 76 } |
| 84 | 77 |
| 85 private: | 78 private: |
| 86 MouseWatcher* mouse_watcher_; | 79 MouseWatcher* mouse_watcher_; |
| 80 scoped_ptr<views::EventMonitor> event_monitor_; |
| 87 | 81 |
| 88 // A factory that is used to construct a delayed callback to the listener. | 82 // A factory that is used to construct a delayed callback to the listener. |
| 89 base::WeakPtrFactory<Observer> notify_listener_factory_; | 83 base::WeakPtrFactory<Observer> notify_listener_factory_; |
| 90 | 84 |
| 91 DISALLOW_COPY_AND_ASSIGN(Observer); | 85 DISALLOW_COPY_AND_ASSIGN(Observer); |
| 92 }; | 86 }; |
| 93 | 87 |
| 94 MouseWatcherListener::~MouseWatcherListener() { | 88 MouseWatcherListener::~MouseWatcherListener() { |
| 95 } | 89 } |
| 96 | 90 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 116 void MouseWatcher::Stop() { | 110 void MouseWatcher::Stop() { |
| 117 observer_.reset(NULL); | 111 observer_.reset(NULL); |
| 118 } | 112 } |
| 119 | 113 |
| 120 void MouseWatcher::NotifyListener() { | 114 void MouseWatcher::NotifyListener() { |
| 121 observer_.reset(NULL); | 115 observer_.reset(NULL); |
| 122 listener_->MouseMovedOutOfHost(); | 116 listener_->MouseMovedOutOfHost(); |
| 123 } | 117 } |
| 124 | 118 |
| 125 } // namespace views | 119 } // namespace views |
| OLD | NEW |