| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "ash/wm/cursor_manager.h" | |
| 6 | |
| 7 #include "ash/shell.h" | |
| 8 #include "ash/wm/image_cursors.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "ui/aura/env.h" | |
| 11 #include "ui/aura/root_window.h" | |
| 12 #include "ui/base/cursor/cursor.h" | |
| 13 | |
| 14 namespace { | |
| 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 | |
| 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 | |
| 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 | |
| 44 } // namespace | |
| 45 | |
| 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 | |
| 99 CursorManager::CursorManager() | |
| 100 : cursor_lock_count_(0), | |
| 101 current_state_(new internal::CursorState), | |
| 102 state_on_unlock_(new internal::CursorState), | |
| 103 image_cursors_(new ImageCursors) { | |
| 104 } | |
| 105 | |
| 106 CursorManager::~CursorManager() { | |
| 107 } | |
| 108 | |
| 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) { | |
| 158 if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) | |
| 159 SetCursorInternal(GetCurrentCursor()); | |
| 160 } | |
| 161 | |
| 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) { | |
| 181 gfx::NativeCursor new_cursor = cursor; | |
| 182 image_cursors_->SetPlatformCursor(&new_cursor); | |
| 183 new_cursor.set_device_scale_factor(image_cursors_->GetDeviceScaleFactor()); | |
| 184 current_state_->set_cursor(new_cursor); | |
| 185 | |
| 186 if (IsCursorVisible()) | |
| 187 SetCursorOnAllRootWindows(GetCurrentCursor()); | |
| 188 } | |
| 189 | |
| 190 void CursorManager::SetCursorVisibility(bool visible) { | |
| 191 current_state_->SetVisible(visible); | |
| 192 | |
| 193 if (visible) { | |
| 194 SetCursorInternal(GetCurrentCursor()); | |
| 195 } else { | |
| 196 gfx::NativeCursor invisible_cursor(ui::kCursorNone); | |
| 197 image_cursors_->SetPlatformCursor(&invisible_cursor); | |
| 198 SetCursorOnAllRootWindows(invisible_cursor); | |
| 199 } | |
| 200 | |
| 201 NotifyCursorVisibilityChange(visible); | |
| 202 } | |
| 203 | |
| 204 void CursorManager::SetMouseEventsEnabled(bool enabled) { | |
| 205 current_state_->SetMouseEventsEnabled(enabled); | |
| 206 | |
| 207 if (enabled) { | |
| 208 aura::Env::GetInstance()->set_last_mouse_location( | |
| 209 disabled_cursor_location_); | |
| 210 } else { | |
| 211 disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location(); | |
| 212 aura::Env::GetInstance()->set_last_mouse_location( | |
| 213 gfx::Point(kDisabledCursorLocationX, kDisabledCursorLocationY)); | |
| 214 } | |
| 215 | |
| 216 SetCursorVisibility(current_state_->visible()); | |
| 217 NotifyMouseEventsEnableStateChange(enabled); | |
| 218 } | |
| 219 | |
| 220 gfx::NativeCursor CursorManager::GetCurrentCursor() const { | |
| 221 return current_state_->cursor(); | |
| 222 } | |
| 223 | |
| 224 } // namespace ash | |
| OLD | NEW |