blob: a126ae345f093734cee291ca51ba3cbfd8a5cc77 (
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
// 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/shell.h"
#include "ash/wm/image_cursors.h"
#include "base/logging.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/base/cursor/cursor.h"
namespace {
// The coordinate of the cursor used when the mouse events are disabled.
const int kDisabledCursorLocationX = -10000;
const int kDisabledCursorLocationY = -10000;
void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) {
ash::Shell::RootWindowList root_windows =
ash::Shell::GetInstance()->GetAllRootWindows();
for (ash::Shell::RootWindowList::iterator iter = root_windows.begin();
iter != root_windows.end(); ++iter)
(*iter)->SetCursor(cursor);
}
void NotifyCursorVisibilityChange(bool visible) {
ash::Shell::RootWindowList root_windows =
ash::Shell::GetInstance()->GetAllRootWindows();
for (ash::Shell::RootWindowList::iterator iter = root_windows.begin();
iter != root_windows.end(); ++iter)
(*iter)->OnCursorVisibilityChanged(visible);
}
void NotifyMouseEventsEnableStateChange(bool enabled) {
ash::Shell::RootWindowList root_windows =
ash::Shell::GetInstance()->GetAllRootWindows();
for (ash::Shell::RootWindowList::iterator iter = root_windows.begin();
iter != root_windows.end(); ++iter)
(*iter)->OnMouseEventsEnableStateChanged(enabled);
}
} // namespace
namespace ash {
namespace internal {
// Represents the cursor state which is composed of cursor type, visibility, and
// mouse events enable state. When mouse events are disabled, the cursor is
// always invisible.
class CursorState {
public:
CursorState()
: cursor_(ui::kCursorNone),
visible_(true),
mouse_events_enabled_(true),
visible_on_mouse_events_enabled_(true) {
}
gfx::NativeCursor cursor() const { return cursor_; }
void set_cursor(gfx::NativeCursor cursor) { cursor_ = cursor; }
bool visible() const { return visible_; }
void SetVisible(bool visible) {
if (mouse_events_enabled_)
visible_ = visible;
// Ignores the call when mouse events disabled.
}
bool mouse_events_enabled() const { return mouse_events_enabled_; }
void SetMouseEventsEnabled(bool enabled) {
if (mouse_events_enabled_ == enabled)
return;
mouse_events_enabled_ = enabled;
// Restores the visibility when mouse events are enabled.
if (enabled) {
visible_ = visible_on_mouse_events_enabled_;
} else {
visible_on_mouse_events_enabled_ = visible_;
visible_ = false;
}
}
private:
gfx::NativeCursor cursor_;
bool visible_;
bool mouse_events_enabled_;
// The visibility to set when mouse events are enabled.
bool visible_on_mouse_events_enabled_;
DISALLOW_COPY_AND_ASSIGN(CursorState);
};
} // namespace internal
CursorManager::CursorManager()
: cursor_lock_count_(0),
current_state_(new internal::CursorState),
state_on_unlock_(new internal::CursorState),
image_cursors_(new ImageCursors) {
}
CursorManager::~CursorManager() {
}
void CursorManager::SetCursor(gfx::NativeCursor cursor) {
state_on_unlock_->set_cursor(cursor);
if (cursor_lock_count_ == 0 &&
GetCurrentCursor() != state_on_unlock_->cursor()) {
SetCursorInternal(state_on_unlock_->cursor());
}
}
void CursorManager::ShowCursor() {
state_on_unlock_->SetVisible(true);
if (cursor_lock_count_ == 0 &&
IsCursorVisible() != state_on_unlock_->visible()) {
SetCursorVisibility(state_on_unlock_->visible());
}
}
void CursorManager::HideCursor() {
state_on_unlock_->SetVisible(false);
if (cursor_lock_count_ == 0 &&
IsCursorVisible() != state_on_unlock_->visible()) {
SetCursorVisibility(state_on_unlock_->visible());
}
}
bool CursorManager::IsCursorVisible() const {
return current_state_->visible();
}
void CursorManager::EnableMouseEvents() {
state_on_unlock_->SetMouseEventsEnabled(true);
if (cursor_lock_count_ == 0 &&
IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) {
SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled());
}
}
void CursorManager::DisableMouseEvents() {
state_on_unlock_->SetMouseEventsEnabled(false);
if (cursor_lock_count_ == 0 &&
IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) {
SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled());
}
}
bool CursorManager::IsMouseEventsEnabled() const {
return current_state_->mouse_events_enabled();
}
void CursorManager::SetDeviceScaleFactor(float device_scale_factor) {
if (image_cursors_->SetDeviceScaleFactor(device_scale_factor))
SetCursorInternal(GetCurrentCursor());
}
void CursorManager::LockCursor() {
cursor_lock_count_++;
}
void CursorManager::UnlockCursor() {
cursor_lock_count_--;
DCHECK_GE(cursor_lock_count_, 0);
if (cursor_lock_count_ > 0)
return;
if (GetCurrentCursor() != state_on_unlock_->cursor())
SetCursorInternal(state_on_unlock_->cursor());
if (IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled())
SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled());
if (IsCursorVisible() != state_on_unlock_->visible())
SetCursorVisibility(state_on_unlock_->visible());
}
void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) {
gfx::NativeCursor new_cursor = cursor;
image_cursors_->SetPlatformCursor(&new_cursor);
new_cursor.set_device_scale_factor(image_cursors_->GetDeviceScaleFactor());
current_state_->set_cursor(new_cursor);
if (IsCursorVisible())
SetCursorOnAllRootWindows(GetCurrentCursor());
}
void CursorManager::SetCursorVisibility(bool visible) {
current_state_->SetVisible(visible);
if (visible) {
SetCursorInternal(GetCurrentCursor());
} else {
gfx::NativeCursor invisible_cursor(ui::kCursorNone);
image_cursors_->SetPlatformCursor(&invisible_cursor);
SetCursorOnAllRootWindows(invisible_cursor);
}
NotifyCursorVisibilityChange(visible);
}
void CursorManager::SetMouseEventsEnabled(bool enabled) {
current_state_->SetMouseEventsEnabled(enabled);
if (enabled) {
aura::Env::GetInstance()->set_last_mouse_location(
disabled_cursor_location_);
} else {
disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location();
aura::Env::GetInstance()->set_last_mouse_location(
gfx::Point(kDisabledCursorLocationX, kDisabledCursorLocationY));
}
SetCursorVisibility(current_state_->visible());
NotifyMouseEventsEnableStateChange(enabled);
}
gfx::NativeCursor CursorManager::GetCurrentCursor() const {
return current_state_->cursor();
}
} // namespace ash
|