| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/macros.h" | |
| 6 #include "base/time/time.h" | |
| 7 #include "base/timer/timer.h" | |
| 8 #include "chrome/browser/ui/panels/panel_mouse_watcher.h" | |
| 9 #include "ui/display/screen.h" | |
| 10 #include "ui/gfx/geometry/point.h" | |
| 11 | |
| 12 // A timer based implementation of PanelMouseWatcher. Currently used for Gtk | |
| 13 // and Mac panels implementations. | |
| 14 class PanelMouseWatcherTimer : public PanelMouseWatcher { | |
| 15 public: | |
| 16 PanelMouseWatcherTimer(); | |
| 17 ~PanelMouseWatcherTimer() override; | |
| 18 | |
| 19 private: | |
| 20 void Start() override; | |
| 21 void Stop() override; | |
| 22 bool IsActive() const override; | |
| 23 gfx::Point GetMousePosition() const override; | |
| 24 | |
| 25 // Specifies the rate at which we want to sample the mouse position. | |
| 26 static const int kMousePollingIntervalMs = 250; | |
| 27 | |
| 28 // Timer callback function. | |
| 29 void DoWork(); | |
| 30 friend class base::RepeatingTimer; | |
| 31 | |
| 32 // Timer used to track mouse movements. Some OSes do not provide an easy way | |
| 33 // of tracking mouse movements across applications. So we use a timer to | |
| 34 // accomplish the same. This could also be more efficient as you end up | |
| 35 // getting a lot of notifications when tracking mouse movements. | |
| 36 base::RepeatingTimer timer_; | |
| 37 | |
| 38 DISALLOW_COPY_AND_ASSIGN(PanelMouseWatcherTimer); | |
| 39 }; | |
| 40 | |
| 41 // static | |
| 42 PanelMouseWatcher* PanelMouseWatcher::Create() { | |
| 43 return new PanelMouseWatcherTimer(); | |
| 44 } | |
| 45 | |
| 46 PanelMouseWatcherTimer::PanelMouseWatcherTimer() { | |
| 47 } | |
| 48 | |
| 49 PanelMouseWatcherTimer::~PanelMouseWatcherTimer() { | |
| 50 DCHECK(!IsActive()); | |
| 51 } | |
| 52 | |
| 53 void PanelMouseWatcherTimer::Start() { | |
| 54 DCHECK(!IsActive()); | |
| 55 timer_.Start(FROM_HERE, | |
| 56 base::TimeDelta::FromMilliseconds(kMousePollingIntervalMs), | |
| 57 this, &PanelMouseWatcherTimer::DoWork); | |
| 58 } | |
| 59 | |
| 60 void PanelMouseWatcherTimer::Stop() { | |
| 61 DCHECK(IsActive()); | |
| 62 timer_.Stop(); | |
| 63 } | |
| 64 | |
| 65 bool PanelMouseWatcherTimer::IsActive() const { | |
| 66 return timer_.IsRunning(); | |
| 67 } | |
| 68 | |
| 69 gfx::Point PanelMouseWatcherTimer::GetMousePosition() const { | |
| 70 return display::Screen::GetScreen()->GetCursorScreenPoint(); | |
| 71 } | |
| 72 | |
| 73 void PanelMouseWatcherTimer::DoWork() { | |
| 74 NotifyMouseMovement(GetMousePosition()); | |
| 75 } | |
| OLD | NEW |