summaryrefslogtreecommitdiffstats
path: root/ash/wm/cursor_manager.cc
blob: 3afee4b20d848e9d1b29b0ecce034e2d228d4523 (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
75
76
77
78
79
80
81
82
// 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 "ash/wm/cursor_manager.h"

#include "ash/wm/cursor_delegate.h"
#include "ash/wm/image_cursors.h"
#include "base/logging.h"
#include "ui/aura/env.h"
#include "ui/base/cursor/cursor.h"

namespace ash {

CursorManager::CursorManager()
    : delegate_(NULL),
      cursor_lock_count_(0),
      did_cursor_change_(false),
      cursor_to_set_on_unlock_(0),
      cursor_visible_(true),
      current_cursor_(ui::kCursorNone),
      image_cursors_(new ImageCursors) {
}

CursorManager::~CursorManager() {
}

void CursorManager::LockCursor() {
  cursor_lock_count_++;
}

void CursorManager::UnlockCursor() {
  cursor_lock_count_--;
  DCHECK_GE(cursor_lock_count_, 0);
  if (cursor_lock_count_ == 0) {
    if (did_cursor_change_) {
      did_cursor_change_ = false;
      if (delegate_)
        delegate_->SetCursor(cursor_to_set_on_unlock_);
    }
    did_cursor_change_ = false;
    cursor_to_set_on_unlock_ = gfx::kNullCursor;
  }
}

void CursorManager::SetCursor(gfx::NativeCursor cursor) {
  if (cursor_lock_count_ == 0) {
    if (delegate_)
      SetCursorInternal(cursor);
  } else {
    cursor_to_set_on_unlock_ = cursor;
    did_cursor_change_ = true;
  }
}

void CursorManager::ShowCursor(bool show) {
  cursor_visible_ = show;
  if (delegate_)
    delegate_->ShowCursor(show);
}

bool CursorManager::IsCursorVisible() const {
  return cursor_visible_;
}

void CursorManager::SetDeviceScaleFactor(float device_scale_factor) {
  if (image_cursors_->GetDeviceScaleFactor() == device_scale_factor)
    return;
  image_cursors_->SetDeviceScaleFactor(device_scale_factor);
  SetCursorInternal(current_cursor_);
}

void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) {
  DCHECK(delegate_);
  current_cursor_ = cursor;
  image_cursors_->SetPlatformCursor(&current_cursor_);
  current_cursor_.set_device_scale_factor(
      image_cursors_->GetDeviceScaleFactor());
  delegate_->SetCursor(current_cursor_);
}

}  // namespace ash