summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/panels/panel_mouse_watcher_timer.cc
blob: 029a8354b91539c1c91037a27c17e8b4e41ca851 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/time.h"
#include "base/timer.h"
#include "chrome/browser/ui/panels/panel_mouse_watcher.h"
#include "ui/gfx/screen.h"

// A timer based implementation of PanelMouseWatcher.  Currently used for Gtk
// and Mac panels implementations.
class PanelMouseWatcherTimer : public PanelMouseWatcher {
 public:
  PanelMouseWatcherTimer();
  virtual ~PanelMouseWatcherTimer();

 private:
  virtual void Start() OVERRIDE;
  virtual void Stop() OVERRIDE;
  virtual bool IsActive() const OVERRIDE;
  virtual gfx::Point GetMousePosition() const OVERRIDE;

  // Specifies the rate at which we want to sample the mouse position.
  static const int kMousePollingIntervalMs = 250;

  // Timer callback function.
  void DoWork();
  friend class base::RepeatingTimer<PanelMouseWatcherTimer>;

  // Timer used to track mouse movements. Some OSes do not provide an easy way
  // of tracking mouse movements across applications.  So we use a timer to
  // accomplish the same.  This could also be more efficient as you end up
  // getting a lot of notifications when tracking mouse movements.
  base::RepeatingTimer<PanelMouseWatcherTimer> timer_;

  DISALLOW_COPY_AND_ASSIGN(PanelMouseWatcherTimer);
};

// static
PanelMouseWatcher* PanelMouseWatcher::Create() {
  return new PanelMouseWatcherTimer();
}

PanelMouseWatcherTimer::PanelMouseWatcherTimer() {
}

PanelMouseWatcherTimer::~PanelMouseWatcherTimer() {
  DCHECK(!IsActive());
}

void PanelMouseWatcherTimer::Start() {
  DCHECK(!IsActive());
  timer_.Start(FROM_HERE,
               base::TimeDelta::FromMilliseconds(kMousePollingIntervalMs),
               this, &PanelMouseWatcherTimer::DoWork);
}

void PanelMouseWatcherTimer::Stop() {
  DCHECK(IsActive());
  timer_.Stop();
}

bool PanelMouseWatcherTimer::IsActive() const {
  return timer_.IsRunning();
}

gfx::Point PanelMouseWatcherTimer::GetMousePosition() const {
  // TODO(scottmg): NativeScreen is wrong. http://crbug.com/133312
  return gfx::Screen::GetNativeScreen()->GetCursorScreenPoint();
}

void PanelMouseWatcherTimer::DoWork() {
  NotifyMouseMovement(GetMousePosition());
}