[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4b8003c | 2012-07-26 00:54:19 | [diff] [blame] | 5 | #include "ash/wm/cursor_manager.h" |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame] | 6 | |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 7 | #include "ash/shell.h" |
[email protected] | 151ffdff | 2012-09-11 20:18:35 | [diff] [blame] | 8 | #include "ash/wm/image_cursors.h" |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame] | 9 | #include "base/logging.h" |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 10 | #include "ui/aura/env.h" |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 11 | #include "ui/aura/root_window.h" |
[email protected] | 151ffdff | 2012-09-11 20:18:35 | [diff] [blame] | 12 | #include "ui/base/cursor/cursor.h" |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame] | 13 | |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 14 | namespace { |
| 15 | |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 16 | // The coordinate of the cursor used when the mouse events are disabled. |
| 17 | const int kDisabledCursorLocationX = -10000; |
| 18 | const int kDisabledCursorLocationY = -10000; |
| 19 | |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 20 | void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) { |
| 21 | ash::Shell::RootWindowList root_windows = |
| 22 | ash::Shell::GetInstance()->GetAllRootWindows(); |
| 23 | for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); |
| 24 | iter != root_windows.end(); ++iter) |
| 25 | (*iter)->SetCursor(cursor); |
| 26 | } |
| 27 | |
| 28 | void NotifyCursorVisibilityChange(bool visible) { |
| 29 | ash::Shell::RootWindowList root_windows = |
| 30 | ash::Shell::GetInstance()->GetAllRootWindows(); |
| 31 | for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); |
| 32 | iter != root_windows.end(); ++iter) |
| 33 | (*iter)->OnCursorVisibilityChanged(visible); |
| 34 | } |
| 35 | |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 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 | |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 44 | } // namespace |
| 45 | |
[email protected] | 4b8003c | 2012-07-26 00:54:19 | [diff] [blame] | 46 | namespace ash { |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 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 |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame] | 97 | |
| 98 | CursorManager::CursorManager() |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 99 | : cursor_lock_count_(0), |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 100 | current_state_(new internal::CursorState), |
| 101 | state_on_unlock_(new internal::CursorState), |
[email protected] | 151ffdff | 2012-09-11 20:18:35 | [diff] [blame] | 102 | image_cursors_(new ImageCursors) { |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | CursorManager::~CursorManager() { |
| 106 | } |
| 107 | |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame] | 108 | void CursorManager::SetCursor(gfx::NativeCursor cursor) { |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 109 | state_on_unlock_->set_cursor(cursor); |
| 110 | if (cursor_lock_count_ == 0 && |
| 111 | GetCurrentCursor() != state_on_unlock_->cursor()) { |
| 112 | SetCursorInternal(state_on_unlock_->cursor()); |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 116 | void CursorManager::ShowCursor() { |
| 117 | state_on_unlock_->SetVisible(true); |
| 118 | if (cursor_lock_count_ == 0 && |
| 119 | IsCursorVisible() != state_on_unlock_->visible()) { |
| 120 | SetCursorVisibility(state_on_unlock_->visible()); |
| 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()); |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 129 | } |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 4b8003c | 2012-07-26 00:54:19 | [diff] [blame] | 132 | bool CursorManager::IsCursorVisible() const { |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 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(); |
[email protected] | 4b8003c | 2012-07-26 00:54:19 | [diff] [blame] | 154 | } |
| 155 | |
[email protected] | 151ffdff | 2012-09-11 20:18:35 | [diff] [blame] | 156 | void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { |
[email protected] | 2216f23 | 2012-10-22 23:45:05 | [diff] [blame] | 157 | if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 158 | SetCursorInternal(GetCurrentCursor()); |
[email protected] | 151ffdff | 2012-09-11 20:18:35 | [diff] [blame] | 159 | } |
| 160 | |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 161 | void CursorManager::LockCursor() { |
| 162 | cursor_lock_count_++; |
| 163 | } |
| 164 | |
| 165 | void CursorManager::UnlockCursor() { |
| 166 | cursor_lock_count_--; |
| 167 | DCHECK_GE(cursor_lock_count_, 0); |
| 168 | if (cursor_lock_count_ > 0) |
| 169 | return; |
| 170 | |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 171 | if (GetCurrentCursor() != state_on_unlock_->cursor()) |
| 172 | SetCursorInternal(state_on_unlock_->cursor()); |
| 173 | if (IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) |
| 174 | SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled()); |
| 175 | if (IsCursorVisible() != state_on_unlock_->visible()) |
| 176 | SetCursorVisibility(state_on_unlock_->visible()); |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 151ffdff | 2012-09-11 20:18:35 | [diff] [blame] | 179 | void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 180 | gfx::NativeCursor new_cursor = cursor; |
| 181 | image_cursors_->SetPlatformCursor(&new_cursor); |
| 182 | new_cursor.set_device_scale_factor(image_cursors_->GetDeviceScaleFactor()); |
| 183 | current_state_->set_cursor(new_cursor); |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 184 | |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 185 | if (IsCursorVisible()) |
| 186 | SetCursorOnAllRootWindows(GetCurrentCursor()); |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 187 | } |
| 188 | |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 189 | void CursorManager::SetCursorVisibility(bool visible) { |
| 190 | current_state_->SetVisible(visible); |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 191 | |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 192 | if (visible) { |
| 193 | SetCursorInternal(GetCurrentCursor()); |
[email protected] | 60c2620 | 2012-10-12 18:55:23 | [diff] [blame] | 194 | } else { |
| 195 | gfx::NativeCursor invisible_cursor(ui::kCursorNone); |
| 196 | image_cursors_->SetPlatformCursor(&invisible_cursor); |
| 197 | SetCursorOnAllRootWindows(invisible_cursor); |
| 198 | } |
| 199 | |
[email protected] | 166ccde | 2012-12-19 16:43:53 | [diff] [blame^] | 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(); |
[email protected] | 151ffdff | 2012-09-11 20:18:35 | [diff] [blame] | 221 | } |
| 222 | |
[email protected] | 4b8003c | 2012-07-26 00:54:19 | [diff] [blame] | 223 | } // namespace ash |