OLD | NEW |
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/root_window.h" | 11 #include "ui/aura/root_window.h" |
11 #include "ui/base/cursor/cursor.h" | 12 #include "ui/base/cursor/cursor.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
| 16 // The coordinate of the cursor used when the mouse events are disabled. |
| 17 const int kDisabledCursorLocationX = -10000; |
| 18 const int kDisabledCursorLocationY = -10000; |
| 19 |
15 void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) { | 20 void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) { |
16 ash::Shell::RootWindowList root_windows = | 21 ash::Shell::RootWindowList root_windows = |
17 ash::Shell::GetInstance()->GetAllRootWindows(); | 22 ash::Shell::GetInstance()->GetAllRootWindows(); |
18 for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); | 23 for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); |
19 iter != root_windows.end(); ++iter) | 24 iter != root_windows.end(); ++iter) |
20 (*iter)->SetCursor(cursor); | 25 (*iter)->SetCursor(cursor); |
21 } | 26 } |
22 | 27 |
23 void NotifyCursorVisibilityChange(bool visible) { | 28 void NotifyCursorVisibilityChange(bool visible) { |
24 ash::Shell::RootWindowList root_windows = | 29 ash::Shell::RootWindowList root_windows = |
25 ash::Shell::GetInstance()->GetAllRootWindows(); | 30 ash::Shell::GetInstance()->GetAllRootWindows(); |
26 for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); | 31 for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); |
27 iter != root_windows.end(); ++iter) | 32 iter != root_windows.end(); ++iter) |
28 (*iter)->OnCursorVisibilityChanged(visible); | 33 (*iter)->OnCursorVisibilityChanged(visible); |
29 } | 34 } |
30 | 35 |
| 36 void NotifyMouseEventsEnableStateChange(bool enabled) { |
| 37 ash::Shell::RootWindowList root_windows = |
| 38 ash::Shell::GetInstance()->GetAllRootWindows(); |
| 39 for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); |
| 40 iter != root_windows.end(); ++iter) |
| 41 (*iter)->OnMouseEventsEnableStateChanged(enabled); |
| 42 } |
| 43 |
31 } // namespace | 44 } // namespace |
32 | 45 |
33 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 mouse_events_enabled_ = enabled; |
| 74 |
| 75 // Restores the visibility when mouse events are enabled. |
| 76 if (enabled) { |
| 77 visible_ = visible_on_mouse_events_enabled_; |
| 78 } else { |
| 79 visible_on_mouse_events_enabled_ = visible_; |
| 80 visible_ = false; |
| 81 } |
| 82 } |
| 83 |
| 84 |
| 85 private: |
| 86 gfx::NativeCursor cursor_; |
| 87 bool visible_; |
| 88 bool mouse_events_enabled_; |
| 89 |
| 90 // The visibility to set when mouse events are enabled. |
| 91 bool visible_on_mouse_events_enabled_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(CursorState); |
| 94 }; |
| 95 |
| 96 } // namespace internal |
34 | 97 |
35 CursorManager::CursorManager() | 98 CursorManager::CursorManager() |
36 : cursor_lock_count_(0), | 99 : cursor_lock_count_(0), |
37 did_cursor_change_(false), | 100 current_state_(new internal::CursorState), |
38 cursor_to_set_on_unlock_(0), | 101 state_on_unlock_(new internal::CursorState), |
39 did_visibility_change_(false), | |
40 show_on_unlock_(true), | |
41 cursor_visible_(true), | |
42 current_cursor_(ui::kCursorNone), | |
43 image_cursors_(new ImageCursors) { | 102 image_cursors_(new ImageCursors) { |
44 } | 103 } |
45 | 104 |
46 CursorManager::~CursorManager() { | 105 CursorManager::~CursorManager() { |
47 } | 106 } |
48 | 107 |
49 void CursorManager::SetCursor(gfx::NativeCursor cursor) { | 108 void CursorManager::SetCursor(gfx::NativeCursor cursor) { |
50 if (cursor_lock_count_ == 0) { | 109 state_on_unlock_->set_cursor(cursor); |
51 SetCursorInternal(cursor); | 110 if (cursor_lock_count_ == 0 && |
52 } else { | 111 GetCurrentCursor() != state_on_unlock_->cursor()) { |
53 cursor_to_set_on_unlock_ = cursor; | 112 SetCursorInternal(state_on_unlock_->cursor()); |
54 did_cursor_change_ = true; | |
55 } | 113 } |
56 } | 114 } |
57 | 115 |
58 void CursorManager::ShowCursor(bool show) { | 116 void CursorManager::ShowCursor() { |
59 if (cursor_lock_count_ == 0) { | 117 state_on_unlock_->SetVisible(true); |
60 ShowCursorInternal(show); | 118 if (cursor_lock_count_ == 0 && |
61 } else { | 119 IsCursorVisible() != state_on_unlock_->visible()) { |
62 show_on_unlock_ = show; | 120 SetCursorVisibility(state_on_unlock_->visible()); |
63 did_visibility_change_ = true; | 121 } |
| 122 } |
| 123 |
| 124 void CursorManager::HideCursor() { |
| 125 state_on_unlock_->SetVisible(false); |
| 126 if (cursor_lock_count_ == 0 && |
| 127 IsCursorVisible() != state_on_unlock_->visible()) { |
| 128 SetCursorVisibility(state_on_unlock_->visible()); |
64 } | 129 } |
65 } | 130 } |
66 | 131 |
67 bool CursorManager::IsCursorVisible() const { | 132 bool CursorManager::IsCursorVisible() const { |
68 return cursor_visible_; | 133 return current_state_->visible(); |
| 134 } |
| 135 |
| 136 void CursorManager::EnableMouseEvents() { |
| 137 state_on_unlock_->SetMouseEventsEnabled(true); |
| 138 if (cursor_lock_count_ == 0 && |
| 139 IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) { |
| 140 SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled()); |
| 141 } |
| 142 } |
| 143 |
| 144 void CursorManager::DisableMouseEvents() { |
| 145 state_on_unlock_->SetMouseEventsEnabled(false); |
| 146 if (cursor_lock_count_ == 0 && |
| 147 IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) { |
| 148 SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled()); |
| 149 } |
| 150 } |
| 151 |
| 152 bool CursorManager::IsMouseEventsEnabled() const { |
| 153 return current_state_->mouse_events_enabled(); |
69 } | 154 } |
70 | 155 |
71 void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { | 156 void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { |
72 if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) | 157 if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) |
73 SetCursorInternal(current_cursor_); | 158 SetCursorInternal(GetCurrentCursor()); |
74 } | 159 } |
75 | 160 |
76 void CursorManager::LockCursor() { | 161 void CursorManager::LockCursor() { |
77 cursor_lock_count_++; | 162 cursor_lock_count_++; |
78 } | 163 } |
79 | 164 |
80 void CursorManager::UnlockCursor() { | 165 void CursorManager::UnlockCursor() { |
81 cursor_lock_count_--; | 166 cursor_lock_count_--; |
82 DCHECK_GE(cursor_lock_count_, 0); | 167 DCHECK_GE(cursor_lock_count_, 0); |
83 if (cursor_lock_count_ > 0) | 168 if (cursor_lock_count_ > 0) |
84 return; | 169 return; |
85 | 170 |
86 if (did_cursor_change_) | 171 if (GetCurrentCursor() != state_on_unlock_->cursor()) |
87 SetCursorInternal(cursor_to_set_on_unlock_); | 172 SetCursorInternal(state_on_unlock_->cursor()); |
88 did_cursor_change_ = false; | 173 if (IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) |
89 cursor_to_set_on_unlock_ = gfx::kNullCursor; | 174 SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled()); |
90 | 175 if (IsCursorVisible() != state_on_unlock_->visible()) |
91 if (did_visibility_change_) | 176 SetCursorVisibility(state_on_unlock_->visible()); |
92 ShowCursorInternal(show_on_unlock_); | |
93 did_visibility_change_ = false; | |
94 } | 177 } |
95 | 178 |
96 void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { | 179 void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { |
97 current_cursor_ = cursor; | 180 gfx::NativeCursor new_cursor = cursor; |
98 image_cursors_->SetPlatformCursor(¤t_cursor_); | 181 image_cursors_->SetPlatformCursor(&new_cursor); |
99 current_cursor_.set_device_scale_factor( | 182 new_cursor.set_device_scale_factor(image_cursors_->GetDeviceScaleFactor()); |
100 image_cursors_->GetDeviceScaleFactor()); | 183 current_state_->set_cursor(new_cursor); |
101 | 184 |
102 if (cursor_visible_) | 185 if (IsCursorVisible()) |
103 SetCursorOnAllRootWindows(current_cursor_); | 186 SetCursorOnAllRootWindows(GetCurrentCursor()); |
104 } | 187 } |
105 | 188 |
106 void CursorManager::ShowCursorInternal(bool show) { | 189 void CursorManager::SetCursorVisibility(bool visible) { |
107 if (cursor_visible_ == show) | 190 current_state_->SetVisible(visible); |
108 return; | |
109 | 191 |
110 cursor_visible_ = show; | 192 if (visible) { |
111 | 193 SetCursorInternal(GetCurrentCursor()); |
112 if (show) { | |
113 SetCursorInternal(current_cursor_); | |
114 } else { | 194 } else { |
115 gfx::NativeCursor invisible_cursor(ui::kCursorNone); | 195 gfx::NativeCursor invisible_cursor(ui::kCursorNone); |
116 image_cursors_->SetPlatformCursor(&invisible_cursor); | 196 image_cursors_->SetPlatformCursor(&invisible_cursor); |
117 SetCursorOnAllRootWindows(invisible_cursor); | 197 SetCursorOnAllRootWindows(invisible_cursor); |
118 } | 198 } |
119 | 199 |
120 NotifyCursorVisibilityChange(show); | 200 NotifyCursorVisibilityChange(visible); |
| 201 } |
| 202 |
| 203 void CursorManager::SetMouseEventsEnabled(bool enabled) { |
| 204 current_state_->SetMouseEventsEnabled(enabled); |
| 205 |
| 206 if (enabled) { |
| 207 aura::Env::GetInstance()->set_last_mouse_location( |
| 208 disabled_cursor_location_); |
| 209 } else { |
| 210 disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location(); |
| 211 aura::Env::GetInstance()->set_last_mouse_location( |
| 212 gfx::Point(kDisabledCursorLocationX, kDisabledCursorLocationY)); |
| 213 } |
| 214 |
| 215 SetCursorVisibility(current_state_->visible()); |
| 216 NotifyMouseEventsEnableStateChange(enabled); |
| 217 } |
| 218 |
| 219 gfx::NativeCursor CursorManager::GetCurrentCursor() const { |
| 220 return current_state_->cursor(); |
121 } | 221 } |
122 | 222 |
123 } // namespace ash | 223 } // namespace ash |
OLD | NEW |