1 // Copyright (c) 2013 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.
5 #include "ui/views/corewm/cursor_manager.h"
7 #include "base/logging.h"
8 #include "ui/aura/client/cursor_client_observer.h"
9 #include "ui/views/corewm/native_cursor_manager.h"
10 #include "ui/views/corewm/native_cursor_manager_delegate.h"
17 // Represents the cursor state which is composed of cursor type, visibility, and
18 // mouse events enable state. When mouse events are disabled, the cursor is
23 : cursor_(ui::kCursorNone),
26 cursor_set_(ui::CURSOR_SET_NORMAL),
27 mouse_events_enabled_(true),
28 visible_on_mouse_events_enabled_(true) {
31 gfx::NativeCursor cursor() const { return cursor_; }
32 void set_cursor(gfx::NativeCursor cursor) { cursor_ = cursor; }
34 bool visible() const { return visible_; }
35 void SetVisible(bool visible) {
36 if (mouse_events_enabled_)
38 // Ignores the call when mouse events disabled.
41 float scale() const { return scale_; }
42 void set_scale(float scale) {
46 ui::CursorSetType cursor_set() const { return cursor_set_; }
47 void set_cursor_set(ui::CursorSetType cursor_set) {
48 cursor_set_ = cursor_set;
51 bool mouse_events_enabled() const { return mouse_events_enabled_; }
52 void SetMouseEventsEnabled(bool enabled) {
53 if (mouse_events_enabled_ == enabled)
55 mouse_events_enabled_ = enabled;
57 // Restores the visibility when mouse events are enabled.
59 visible_ = visible_on_mouse_events_enabled_;
61 visible_on_mouse_events_enabled_ = visible_;
67 gfx::NativeCursor cursor_;
70 ui::CursorSetType cursor_set_;
71 bool mouse_events_enabled_;
73 // The visibility to set when mouse events are enabled.
74 bool visible_on_mouse_events_enabled_;
76 DISALLOW_COPY_AND_ASSIGN(CursorState);
79 } // namespace internal
81 CursorManager::CursorManager(scoped_ptr<NativeCursorManager> delegate)
82 : delegate_(delegate.Pass()),
83 cursor_lock_count_(0),
84 current_state_(new internal::CursorState),
85 state_on_unlock_(new internal::CursorState) {
88 CursorManager::~CursorManager() {
91 void CursorManager::SetCursor(gfx::NativeCursor cursor) {
92 state_on_unlock_->set_cursor(cursor);
93 if (cursor_lock_count_ == 0 &&
94 GetCurrentCursor() != state_on_unlock_->cursor()) {
95 delegate_->SetCursor(state_on_unlock_->cursor(), this);
99 void CursorManager::ShowCursor() {
100 state_on_unlock_->SetVisible(true);
101 if (cursor_lock_count_ == 0 &&
102 IsCursorVisible() != state_on_unlock_->visible()) {
103 delegate_->SetVisibility(state_on_unlock_->visible(), this);
104 FOR_EACH_OBSERVER(aura::client::CursorClientObserver, observers_,
105 OnCursorVisibilityChanged(true));
109 void CursorManager::HideCursor() {
110 state_on_unlock_->SetVisible(false);
111 if (cursor_lock_count_ == 0 &&
112 IsCursorVisible() != state_on_unlock_->visible()) {
113 delegate_->SetVisibility(state_on_unlock_->visible(), this);
114 FOR_EACH_OBSERVER(aura::client::CursorClientObserver, observers_,
115 OnCursorVisibilityChanged(false));
119 bool CursorManager::IsCursorVisible() const {
120 return current_state_->visible();
123 void CursorManager::SetScale(float scale) {
124 state_on_unlock_->set_scale(scale);
125 if (GetCurrentScale() != state_on_unlock_->scale())
126 delegate_->SetScale(state_on_unlock_->scale(), this);
129 void CursorManager::SetCursorSet(ui::CursorSetType cursor_set) {
130 state_on_unlock_->set_cursor_set(cursor_set);
131 if (GetCurrentCursorSet() != state_on_unlock_->cursor_set())
132 delegate_->SetCursorSet(state_on_unlock_->cursor_set(), this);
135 float CursorManager::GetCurrentScale() const {
136 return current_state_->scale();
139 ui::CursorSetType CursorManager::GetCurrentCursorSet() const {
140 return current_state_->cursor_set();
143 void CursorManager::EnableMouseEvents() {
144 state_on_unlock_->SetMouseEventsEnabled(true);
145 if (cursor_lock_count_ == 0 &&
146 IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) {
147 delegate_->SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled(),
152 void CursorManager::DisableMouseEvents() {
153 state_on_unlock_->SetMouseEventsEnabled(false);
154 if (cursor_lock_count_ == 0 &&
155 IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) {
156 delegate_->SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled(),
161 bool CursorManager::IsMouseEventsEnabled() const {
162 return current_state_->mouse_events_enabled();
165 void CursorManager::SetDisplay(const gfx::Display& display) {
166 delegate_->SetDisplay(display, this);
169 void CursorManager::LockCursor() {
170 cursor_lock_count_++;
173 void CursorManager::UnlockCursor() {
174 cursor_lock_count_--;
175 DCHECK_GE(cursor_lock_count_, 0);
176 if (cursor_lock_count_ > 0)
179 if (GetCurrentCursor() != state_on_unlock_->cursor()) {
180 delegate_->SetCursor(state_on_unlock_->cursor(), this);
182 if (IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) {
183 delegate_->SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled(),
186 if (IsCursorVisible() != state_on_unlock_->visible()) {
187 delegate_->SetVisibility(state_on_unlock_->visible(),
192 bool CursorManager::IsCursorLocked() const {
193 return cursor_lock_count_ > 0;
196 void CursorManager::AddObserver(
197 aura::client::CursorClientObserver* observer) {
198 observers_.AddObserver(observer);
201 void CursorManager::RemoveObserver(
202 aura::client::CursorClientObserver* observer) {
203 observers_.RemoveObserver(observer);
206 gfx::NativeCursor CursorManager::GetCurrentCursor() const {
207 return current_state_->cursor();
210 bool CursorManager::GetCurrentVisibility() const {
211 return current_state_->visible();
214 bool CursorManager::GetMouseEventsEnabled() const {
215 return current_state_->mouse_events_enabled();
218 void CursorManager::CommitCursor(gfx::NativeCursor cursor) {
219 current_state_->set_cursor(cursor);
222 void CursorManager::CommitVisibility(bool visible) {
223 FOR_EACH_OBSERVER(aura::client::CursorClientObserver, observers_,
224 OnCursorVisibilityChanged(visible));
225 current_state_->SetVisible(visible);
228 void CursorManager::CommitScale(float scale) {
229 current_state_->set_scale(scale);
232 void CursorManager::CommitCursorSet(ui::CursorSetType cursor_set) {
233 current_state_->set_cursor_set(cursor_set);
236 void CursorManager::CommitMouseEventsEnabled(bool enabled) {
237 current_state_->SetMouseEventsEnabled(enabled);
240 } // namespace corewm