Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: ash/wm/cursor_manager.cc

Issue 12263050: Rework ash::CursorManager into a corewm object, to share code with desktop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/wm/cursor_manager.h" 5 #include "ash/wm/cursor_manager.h"
6 6
7 #include "ash/shell.h" 7 #include "ash/shell.h"
8 #include "ash/wm/image_cursors.h" 8 #include "ash/wm/image_cursors.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "ui/aura/env.h" 10 #include "ui/aura/env.h"
(...skipping 26 matching lines...) Expand all
37 ash::Shell::RootWindowList root_windows = 37 ash::Shell::RootWindowList root_windows =
38 ash::Shell::GetInstance()->GetAllRootWindows(); 38 ash::Shell::GetInstance()->GetAllRootWindows();
39 for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); 39 for (ash::Shell::RootWindowList::iterator iter = root_windows.begin();
40 iter != root_windows.end(); ++iter) 40 iter != root_windows.end(); ++iter)
41 (*iter)->OnMouseEventsEnableStateChanged(enabled); 41 (*iter)->OnMouseEventsEnableStateChanged(enabled);
42 } 42 }
43 43
44 } // namespace 44 } // namespace
45 45
46 namespace ash { 46 namespace ash {
47 namespace internal {
48
49 // Represents the cursor state which is composed of cursor type, visibility, and
50 // mouse events enable state. When mouse events are disabled, the cursor is
51 // always invisible.
52 class CursorState {
53 public:
54 CursorState()
55 : cursor_(ui::kCursorNone),
56 visible_(true),
57 mouse_events_enabled_(true),
58 visible_on_mouse_events_enabled_(true) {
59 }
60
61 gfx::NativeCursor cursor() const { return cursor_; }
62 void set_cursor(gfx::NativeCursor cursor) { cursor_ = cursor; }
63
64 bool visible() const { return visible_; }
65 void SetVisible(bool visible) {
66 if (mouse_events_enabled_)
67 visible_ = visible;
68 // Ignores the call when mouse events disabled.
69 }
70
71 bool mouse_events_enabled() const { return mouse_events_enabled_; }
72 void SetMouseEventsEnabled(bool enabled) {
73 if (mouse_events_enabled_ == enabled)
74 return;
75 mouse_events_enabled_ = enabled;
76
77 // Restores the visibility when mouse events are enabled.
78 if (enabled) {
79 visible_ = visible_on_mouse_events_enabled_;
80 } else {
81 visible_on_mouse_events_enabled_ = visible_;
82 visible_ = false;
83 }
84 }
85
86 private:
87 gfx::NativeCursor cursor_;
88 bool visible_;
89 bool mouse_events_enabled_;
90
91 // The visibility to set when mouse events are enabled.
92 bool visible_on_mouse_events_enabled_;
93
94 DISALLOW_COPY_AND_ASSIGN(CursorState);
95 };
96
97 } // namespace internal
98 47
99 CursorManager::CursorManager() 48 CursorManager::CursorManager()
100 : cursor_lock_count_(0), 49 : image_cursors_(new ImageCursors) {
101 current_state_(new internal::CursorState),
102 state_on_unlock_(new internal::CursorState),
103 image_cursors_(new ImageCursors) {
104 } 50 }
105 51
106 CursorManager::~CursorManager() { 52 CursorManager::~CursorManager() {
107 } 53 }
108 54
109 void CursorManager::SetCursor(gfx::NativeCursor cursor) {
110 state_on_unlock_->set_cursor(cursor);
111 if (cursor_lock_count_ == 0 &&
112 GetCurrentCursor() != state_on_unlock_->cursor()) {
113 SetCursorInternal(state_on_unlock_->cursor());
114 }
115 }
116
117 void CursorManager::ShowCursor() {
118 state_on_unlock_->SetVisible(true);
119 if (cursor_lock_count_ == 0 &&
120 IsCursorVisible() != state_on_unlock_->visible()) {
121 SetCursorVisibility(state_on_unlock_->visible());
122 }
123 }
124
125 void CursorManager::HideCursor() {
126 state_on_unlock_->SetVisible(false);
127 if (cursor_lock_count_ == 0 &&
128 IsCursorVisible() != state_on_unlock_->visible()) {
129 SetCursorVisibility(state_on_unlock_->visible());
130 }
131 }
132
133 bool CursorManager::IsCursorVisible() const {
134 return current_state_->visible();
135 }
136
137 void CursorManager::EnableMouseEvents() {
138 state_on_unlock_->SetMouseEventsEnabled(true);
139 if (cursor_lock_count_ == 0 &&
140 IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) {
141 SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled());
142 }
143 }
144
145 void CursorManager::DisableMouseEvents() {
146 state_on_unlock_->SetMouseEventsEnabled(false);
147 if (cursor_lock_count_ == 0 &&
148 IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) {
149 SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled());
150 }
151 }
152
153 bool CursorManager::IsMouseEventsEnabled() const {
154 return current_state_->mouse_events_enabled();
155 }
156
157 void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { 55 void CursorManager::SetDeviceScaleFactor(float device_scale_factor) {
158 if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) 56 if (image_cursors_->SetDeviceScaleFactor(device_scale_factor))
159 SetCursorInternal(GetCurrentCursor()); 57 SetCursorInternal(GetCurrentCursor());
160 } 58 }
161 59
162 void CursorManager::LockCursor() {
163 cursor_lock_count_++;
164 }
165
166 void CursorManager::UnlockCursor() {
167 cursor_lock_count_--;
168 DCHECK_GE(cursor_lock_count_, 0);
169 if (cursor_lock_count_ > 0)
170 return;
171
172 if (GetCurrentCursor() != state_on_unlock_->cursor())
173 SetCursorInternal(state_on_unlock_->cursor());
174 if (IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled())
175 SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled());
176 if (IsCursorVisible() != state_on_unlock_->visible())
177 SetCursorVisibility(state_on_unlock_->visible());
178 }
179
180 void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { 60 void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) {
181 gfx::NativeCursor new_cursor = cursor; 61 gfx::NativeCursor new_cursor = cursor;
182 image_cursors_->SetPlatformCursor(&new_cursor); 62 image_cursors_->SetPlatformCursor(&new_cursor);
183 new_cursor.set_device_scale_factor(image_cursors_->GetDeviceScaleFactor()); 63 new_cursor.set_device_scale_factor(image_cursors_->GetDeviceScaleFactor());
184 current_state_->set_cursor(new_cursor); 64
65 CursorController::SetCursorInternal(new_cursor);
185 66
186 if (IsCursorVisible()) 67 if (IsCursorVisible())
187 SetCursorOnAllRootWindows(GetCurrentCursor()); 68 SetCursorOnAllRootWindows(GetCurrentCursor());
188 } 69 }
189 70
190 void CursorManager::SetCursorVisibility(bool visible) { 71 void CursorManager::SetCursorVisibility(bool visible) {
191 current_state_->SetVisible(visible); 72 CursorController::SetCursorVisibility(visible);
192 73
193 if (visible) { 74 if (visible) {
194 SetCursorInternal(GetCurrentCursor()); 75 SetCursorInternal(GetCurrentCursor());
195 } else { 76 } else {
196 gfx::NativeCursor invisible_cursor(ui::kCursorNone); 77 gfx::NativeCursor invisible_cursor(ui::kCursorNone);
197 image_cursors_->SetPlatformCursor(&invisible_cursor); 78 image_cursors_->SetPlatformCursor(&invisible_cursor);
198 SetCursorOnAllRootWindows(invisible_cursor); 79 SetCursorOnAllRootWindows(invisible_cursor);
199 } 80 }
200 81
201 NotifyCursorVisibilityChange(visible); 82 NotifyCursorVisibilityChange(visible);
202 } 83 }
203 84
204 void CursorManager::SetMouseEventsEnabled(bool enabled) { 85 void CursorManager::SetMouseEventsEnabled(bool enabled) {
205 current_state_->SetMouseEventsEnabled(enabled); 86 CursorController::SetMouseEventsEnabled(enabled);
206 87
207 if (enabled) { 88 if (enabled) {
208 aura::Env::GetInstance()->set_last_mouse_location( 89 aura::Env::GetInstance()->set_last_mouse_location(
209 disabled_cursor_location_); 90 disabled_cursor_location_);
210 } else { 91 } else {
211 disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location(); 92 disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location();
212 aura::Env::GetInstance()->set_last_mouse_location( 93 aura::Env::GetInstance()->set_last_mouse_location(
213 gfx::Point(kDisabledCursorLocationX, kDisabledCursorLocationY)); 94 gfx::Point(kDisabledCursorLocationX, kDisabledCursorLocationY));
214 } 95 }
215 96
216 SetCursorVisibility(current_state_->visible()); 97 SetCursorVisibility(IsCursorVisible());
217 NotifyMouseEventsEnableStateChange(enabled); 98 NotifyMouseEventsEnableStateChange(enabled);
218 } 99 }
219 100
220 gfx::NativeCursor CursorManager::GetCurrentCursor() const {
221 return current_state_->cursor();
222 }
223
224 } // namespace ash 101 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698