| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/events/event_constants.h" | 12 #include "ui/events/event_constants.h" |
| 13 #include "ui/events/event_utils.h" | 13 #include "ui/events/event_utils.h" |
| 14 #include "ui/gfx/screen.h" | 14 #include "ui/gfx/screen.h" |
| 15 | 15 |
| 16 #if defined(USE_AURA) | 16 #if defined(USE_AURA) |
| 17 #include "ui/aura/env.h" | 17 #include "ui/aura/env.h" |
| 18 #include "ui/aura/window.h" | 18 #include "ui/aura/window.h" |
| 19 #include "ui/events/event.h" | 19 #include "ui/events/event.h" |
| 20 #include "ui/events/event_handler.h" | 20 #include "ui/events/event_handler.h" |
| 21 #endif | 21 #endif |
| 22 | 22 |
| 23 namespace views { | 23 namespace views { |
| 24 | 24 |
| 25 // Amount of time between when the mouse moves outside the Host's zone and when | 25 // Amount of time between when the mouse moves outside the Host's zone and when |
| 26 // the listener is notified. | 26 // the listener is notified. |
| 27 const int kNotifyListenerTimeMs = 300; | 27 const int kNotifyListenerTimeMs = 300; |
| 28 | 28 |
| 29 #if defined(OS_WIN) && !defined(USE_AURA) | |
| 30 class MouseWatcher::Observer : public base::MessageLoopForUI::Observer { | |
| 31 public: | |
| 32 explicit Observer(MouseWatcher* mouse_watcher) | |
| 33 : mouse_watcher_(mouse_watcher), | |
| 34 notify_listener_factory_(this) { | |
| 35 base::MessageLoopForUI::current()->AddObserver(this); | |
| 36 } | |
| 37 | |
| 38 virtual ~Observer() { | |
| 39 base::MessageLoopForUI::current()->RemoveObserver(this); | |
| 40 } | |
| 41 | |
| 42 // MessageLoop::Observer implementation: | |
| 43 virtual base::EventStatus WillProcessEvent( | |
| 44 const base::NativeEvent& event) OVERRIDE { | |
| 45 return base::EVENT_CONTINUE; | |
| 46 } | |
| 47 | |
| 48 virtual void DidProcessEvent(const base::NativeEvent& event) OVERRIDE { | |
| 49 // We spy on three different Windows messages here to see if the mouse has | |
| 50 // moved out of the bounds of the current view. The messages are: | |
| 51 // | |
| 52 // WM_MOUSEMOVE: | |
| 53 // For when the mouse moves from the view into the rest of the browser UI, | |
| 54 // i.e. within the bounds of the same windows HWND. | |
| 55 // WM_MOUSELEAVE: | |
| 56 // For when the mouse moves out of the bounds of the view's HWND. | |
| 57 // WM_NCMOUSELEAVE: | |
| 58 // For notification when the mouse leaves the _non-client_ area. | |
| 59 // | |
| 60 switch (event.message) { | |
| 61 case WM_MOUSEMOVE: | |
| 62 HandleGlobalMouseMoveEvent(MouseWatcherHost::MOUSE_MOVE); | |
| 63 break; | |
| 64 case WM_MOUSELEAVE: | |
| 65 case WM_NCMOUSELEAVE: | |
| 66 HandleGlobalMouseMoveEvent(MouseWatcherHost::MOUSE_EXIT); | |
| 67 break; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 private: | |
| 72 MouseWatcherHost* host() const { return mouse_watcher_->host_.get(); } | |
| 73 | |
| 74 // Called from the message loop observer when a mouse movement has occurred. | |
| 75 void HandleGlobalMouseMoveEvent(MouseWatcherHost::MouseEventType event_type) { | |
| 76 bool contained = host()->Contains( | |
| 77 // TODO(scottmg): Native is wrong http://crbug.com/133312 | |
| 78 gfx::Screen::GetNativeScreen()->GetCursorScreenPoint(), | |
| 79 event_type); | |
| 80 if (!contained) { | |
| 81 // Mouse moved outside the host's zone, start a timer to notify the | |
| 82 // listener. | |
| 83 if (!notify_listener_factory_.HasWeakPtrs()) { | |
| 84 base::MessageLoop::current()->PostDelayedTask( | |
| 85 FROM_HERE, | |
| 86 base::Bind(&Observer::NotifyListener, | |
| 87 notify_listener_factory_.GetWeakPtr()), | |
| 88 event_type == MouseWatcherHost::MOUSE_MOVE | |
| 89 ? base::TimeDelta::FromMilliseconds(kNotifyListenerTimeMs) | |
| 90 : mouse_watcher_->notify_on_exit_time_); | |
| 91 } | |
| 92 } else { | |
| 93 // Mouse moved quickly out of the host and then into it again, so cancel | |
| 94 // the timer. | |
| 95 notify_listener_factory_.InvalidateWeakPtrs(); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void NotifyListener() { | |
| 100 mouse_watcher_->NotifyListener(); | |
| 101 // WARNING: we've been deleted. | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 MouseWatcher* mouse_watcher_; | |
| 106 | |
| 107 // A factory that is used to construct a delayed callback to the listener. | |
| 108 base::WeakPtrFactory<Observer> notify_listener_factory_; | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(Observer); | |
| 111 }; | |
| 112 #else | |
| 113 class MouseWatcher::Observer : public ui::EventHandler { | 29 class MouseWatcher::Observer : public ui::EventHandler { |
| 114 public: | 30 public: |
| 115 explicit Observer(MouseWatcher* mouse_watcher) | 31 explicit Observer(MouseWatcher* mouse_watcher) |
| 116 : mouse_watcher_(mouse_watcher), | 32 : mouse_watcher_(mouse_watcher), |
| 117 notify_listener_factory_(this) { | 33 notify_listener_factory_(this) { |
| 118 aura::Env::GetInstance()->AddPreTargetHandler(this); | 34 aura::Env::GetInstance()->AddPreTargetHandler(this); |
| 119 } | 35 } |
| 120 | 36 |
| 121 virtual ~Observer() { | 37 virtual ~Observer() { |
| 122 aura::Env::GetInstance()->RemovePreTargetHandler(this); | 38 aura::Env::GetInstance()->RemovePreTargetHandler(this); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 } | 86 } |
| 171 | 87 |
| 172 private: | 88 private: |
| 173 MouseWatcher* mouse_watcher_; | 89 MouseWatcher* mouse_watcher_; |
| 174 | 90 |
| 175 // A factory that is used to construct a delayed callback to the listener. | 91 // A factory that is used to construct a delayed callback to the listener. |
| 176 base::WeakPtrFactory<Observer> notify_listener_factory_; | 92 base::WeakPtrFactory<Observer> notify_listener_factory_; |
| 177 | 93 |
| 178 DISALLOW_COPY_AND_ASSIGN(Observer); | 94 DISALLOW_COPY_AND_ASSIGN(Observer); |
| 179 }; | 95 }; |
| 180 #endif | |
| 181 | 96 |
| 182 MouseWatcherListener::~MouseWatcherListener() { | 97 MouseWatcherListener::~MouseWatcherListener() { |
| 183 } | 98 } |
| 184 | 99 |
| 185 MouseWatcherHost::~MouseWatcherHost() { | 100 MouseWatcherHost::~MouseWatcherHost() { |
| 186 } | 101 } |
| 187 | 102 |
| 188 MouseWatcher::MouseWatcher(MouseWatcherHost* host, | 103 MouseWatcher::MouseWatcher(MouseWatcherHost* host, |
| 189 MouseWatcherListener* listener) | 104 MouseWatcherListener* listener) |
| 190 : host_(host), | 105 : host_(host), |
| (...skipping 13 matching lines...) Expand all Loading... |
| 204 void MouseWatcher::Stop() { | 119 void MouseWatcher::Stop() { |
| 205 observer_.reset(NULL); | 120 observer_.reset(NULL); |
| 206 } | 121 } |
| 207 | 122 |
| 208 void MouseWatcher::NotifyListener() { | 123 void MouseWatcher::NotifyListener() { |
| 209 observer_.reset(NULL); | 124 observer_.reset(NULL); |
| 210 listener_->MouseMovedOutOfHost(); | 125 listener_->MouseMovedOutOfHost(); |
| 211 } | 126 } |
| 212 | 127 |
| 213 } // namespace views | 128 } // namespace views |
| OLD | NEW |