Chromium Code Reviews| 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/basictypes.h" | |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "ui/aura/env.h" | |
| 10 #include "ui/aura/root_window.h" | 12 #include "ui/aura/root_window.h" |
| 11 #include "ui/base/cursor/cursor.h" | 13 #include "ui/base/cursor/cursor.h" |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 17 // The coordinate of the cursor used when the cursor is disabled. | |
| 18 const int kDisabledCursorLocationX = -10000; | |
| 19 const int kDisabledCursorLocationY = -10000; | |
| 20 | |
| 15 void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) { | 21 void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) { |
| 16 ash::Shell::RootWindowList root_windows = | 22 ash::Shell::RootWindowList root_windows = |
| 17 ash::Shell::GetInstance()->GetAllRootWindows(); | 23 ash::Shell::GetInstance()->GetAllRootWindows(); |
| 18 for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); | 24 for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); |
| 19 iter != root_windows.end(); ++iter) | 25 iter != root_windows.end(); ++iter) |
| 20 (*iter)->SetCursor(cursor); | 26 (*iter)->SetCursor(cursor); |
| 21 } | 27 } |
| 22 | 28 |
| 23 void NotifyCursorVisibilityChange(bool visible) { | 29 void NotifyCursorEnableStateChange(bool visible) { |
| 24 ash::Shell::RootWindowList root_windows = | 30 ash::Shell::RootWindowList root_windows = |
| 25 ash::Shell::GetInstance()->GetAllRootWindows(); | 31 ash::Shell::GetInstance()->GetAllRootWindows(); |
| 26 for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); | 32 for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); |
| 27 iter != root_windows.end(); ++iter) | 33 iter != root_windows.end(); ++iter) |
| 28 (*iter)->OnCursorVisibilityChanged(visible); | 34 (*iter)->OnCursorEnableStateChanged(visible); |
| 29 } | 35 } |
| 30 | 36 |
| 31 } // namespace | 37 } // namespace |
| 32 | 38 |
| 33 namespace ash { | 39 namespace ash { |
| 40 namespace internal { | |
| 41 | |
| 42 // Represents the cursor state which is composed of cursor type, visibility, and | |
| 43 // enabled state. When the cursor is disabled, the cursor is always invisible. | |
| 44 // In other words, the cursor cannot be disabled and visible at the same time. | |
| 45 class CursorState { | |
| 46 public: | |
| 47 explicit CursorState(gfx::NativeCursor cursor) | |
| 48 : cursor_(cursor), | |
| 49 visible_(true), | |
| 50 enabled_(true), | |
| 51 visible_on_enabled_(true) { | |
| 52 } | |
| 53 | |
| 54 gfx::NativeCursor cursor() const { return cursor_; } | |
| 55 void set_cursor(gfx::NativeCursor cursor) { cursor_ = cursor; } | |
| 56 | |
| 57 bool visible() const { return visible_; } | |
| 58 void SetVisible(bool visible) { | |
| 59 if (enabled_) | |
| 60 visible_ = visible; | |
| 61 // Ignores the call when the cursor is disabled. | |
| 62 } | |
| 63 | |
| 64 bool enabled() const { return enabled_; } | |
| 65 void SetEnabled(bool enabled) { | |
| 66 enabled_ = enabled; | |
| 67 | |
| 68 // Restores the visibility when the cursor is enabled. | |
| 69 if (enabled) { | |
| 70 visible_ = visible_on_enabled_; | |
| 71 } else { | |
| 72 visible_on_enabled_ = visible_; | |
| 73 visible_ = false; | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 gfx::NativeCursor cursor_; | |
| 79 bool visible_; | |
| 80 bool enabled_; | |
| 81 | |
| 82 // The visibility to set when the cursor is enabled. | |
| 83 bool visible_on_enabled_; | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(CursorState); | |
| 86 }; | |
| 87 | |
| 88 } // namespace internal | |
| 34 | 89 |
| 35 CursorManager::CursorManager() | 90 CursorManager::CursorManager() |
| 36 : cursor_lock_count_(0), | 91 : cursor_lock_count_(0), |
| 37 did_cursor_change_(false), | 92 current_state_(new internal::CursorState(ui::kCursorNone)), |
| 38 cursor_to_set_on_unlock_(0), | 93 state_on_unlock_(new internal::CursorState(ui::kCursorNone)), |
| 39 did_visibility_change_(false), | |
| 40 show_on_unlock_(true), | |
| 41 cursor_visible_(true), | |
| 42 current_cursor_(ui::kCursorNone), | |
| 43 image_cursors_(new ImageCursors) { | 94 image_cursors_(new ImageCursors) { |
| 44 } | 95 } |
| 45 | 96 |
| 46 CursorManager::~CursorManager() { | 97 CursorManager::~CursorManager() { |
| 47 } | 98 } |
| 48 | 99 |
| 49 void CursorManager::SetCursor(gfx::NativeCursor cursor) { | 100 void CursorManager::SetCursor(gfx::NativeCursor cursor) { |
| 50 if (cursor_lock_count_ == 0) { | 101 state_on_unlock_->set_cursor(cursor); |
| 51 SetCursorInternal(cursor); | 102 if (cursor_lock_count_ == 0 && |
| 52 } else { | 103 GetCurrentCursor() != state_on_unlock_->cursor()) { |
| 53 cursor_to_set_on_unlock_ = cursor; | 104 SetCursorInternal(state_on_unlock_->cursor()); |
| 54 did_cursor_change_ = true; | |
| 55 } | 105 } |
| 56 } | 106 } |
| 57 | 107 |
| 108 bool CursorManager::IsCursorEnabled() const { | |
| 109 return current_state_->enabled(); | |
| 110 } | |
| 111 | |
| 58 void CursorManager::ShowCursor(bool show) { | 112 void CursorManager::ShowCursor(bool show) { |
| 59 if (cursor_lock_count_ == 0) { | 113 state_on_unlock_->SetVisible(show); |
| 60 ShowCursorInternal(show); | 114 if (cursor_lock_count_ == 0 && |
| 61 } else { | 115 IsCursorVisible() != state_on_unlock_->visible()) { |
| 62 show_on_unlock_ = show; | 116 ShowCursorInternal(state_on_unlock_->visible()); |
| 63 did_visibility_change_ = true; | |
| 64 } | 117 } |
| 65 } | 118 } |
| 66 | 119 |
| 67 bool CursorManager::IsCursorVisible() const { | 120 bool CursorManager::IsCursorVisible() const { |
| 68 return cursor_visible_; | 121 return current_state_->visible(); |
| 122 } | |
| 123 | |
| 124 void CursorManager::EnableCursor(bool enabled) { | |
| 125 state_on_unlock_->SetEnabled(enabled); | |
| 126 if (cursor_lock_count_ == 0 && | |
| 127 IsCursorEnabled() != state_on_unlock_->enabled()) { | |
| 128 EnableCursorInternal(state_on_unlock_->enabled()); | |
| 129 } | |
| 69 } | 130 } |
| 70 | 131 |
| 71 void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { | 132 void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { |
| 72 if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) | 133 if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) |
| 73 SetCursorInternal(current_cursor_); | 134 SetCursorInternal(GetCurrentCursor()); |
| 74 } | 135 } |
| 75 | 136 |
| 76 void CursorManager::LockCursor() { | 137 void CursorManager::LockCursor() { |
| 77 cursor_lock_count_++; | 138 cursor_lock_count_++; |
| 78 } | 139 } |
| 79 | 140 |
| 80 void CursorManager::UnlockCursor() { | 141 void CursorManager::UnlockCursor() { |
| 81 cursor_lock_count_--; | 142 cursor_lock_count_--; |
| 82 DCHECK_GE(cursor_lock_count_, 0); | 143 DCHECK_GE(cursor_lock_count_, 0); |
| 83 if (cursor_lock_count_ > 0) | 144 if (cursor_lock_count_ > 0) |
| 84 return; | 145 return; |
| 85 | 146 |
| 86 if (did_cursor_change_) | 147 if (GetCurrentCursor() != state_on_unlock_->enabled()) |
|
sky
2012/12/10 22:11:20
nit: move check to SetCursorInternal so you don't
mazda
2012/12/14 21:16:43
Moving the check to SetCursorInternal is not trivi
| |
| 87 SetCursorInternal(cursor_to_set_on_unlock_); | 148 SetCursorInternal(state_on_unlock_->cursor()); |
| 88 did_cursor_change_ = false; | |
| 89 cursor_to_set_on_unlock_ = gfx::kNullCursor; | |
| 90 | 149 |
| 91 if (did_visibility_change_) | 150 if (IsCursorEnabled() != state_on_unlock_->enabled()) |
|
sky
2012/12/10 22:11:20
nit: move this check to EnableCursorInternal so th
mazda
2012/12/14 21:16:43
Ditto.
| |
| 92 ShowCursorInternal(show_on_unlock_); | 151 EnableCursorInternal(state_on_unlock_->enabled()); |
| 93 did_visibility_change_ = false; | 152 |
| 153 if (IsCursorVisible() != state_on_unlock_->visible()) | |
|
sky
2012/12/10 22:11:20
same comment about moving if.
mazda
2012/12/14 21:16:43
Ditto.
| |
| 154 ShowCursorInternal(state_on_unlock_->visible()); | |
| 94 } | 155 } |
| 95 | 156 |
| 96 void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { | 157 void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { |
| 97 current_cursor_ = cursor; | 158 gfx::NativeCursor new_cursor = cursor; |
| 98 image_cursors_->SetPlatformCursor(¤t_cursor_); | 159 image_cursors_->SetPlatformCursor(&new_cursor); |
| 99 current_cursor_.set_device_scale_factor( | 160 new_cursor.set_device_scale_factor(image_cursors_->GetDeviceScaleFactor()); |
| 100 image_cursors_->GetDeviceScaleFactor()); | 161 current_state_->set_cursor(new_cursor); |
| 101 | 162 |
| 102 if (cursor_visible_) | 163 if (IsCursorVisible()) |
| 103 SetCursorOnAllRootWindows(current_cursor_); | 164 SetCursorOnAllRootWindows(GetCurrentCursor()); |
| 104 } | 165 } |
| 105 | 166 |
| 106 void CursorManager::ShowCursorInternal(bool show) { | 167 void CursorManager::ShowCursorInternal(bool show) { |
| 107 if (cursor_visible_ == show) | 168 current_state_->SetVisible(show); |
| 108 return; | |
| 109 | |
| 110 cursor_visible_ = show; | |
| 111 | 169 |
| 112 if (show) { | 170 if (show) { |
| 113 SetCursorInternal(current_cursor_); | 171 SetCursorInternal(GetCurrentCursor()); |
| 114 } else { | 172 } else { |
| 115 gfx::NativeCursor invisible_cursor(ui::kCursorNone); | 173 gfx::NativeCursor invisible_cursor(ui::kCursorNone); |
| 116 image_cursors_->SetPlatformCursor(&invisible_cursor); | 174 image_cursors_->SetPlatformCursor(&invisible_cursor); |
| 117 SetCursorOnAllRootWindows(invisible_cursor); | 175 SetCursorOnAllRootWindows(invisible_cursor); |
| 118 } | 176 } |
| 177 } | |
| 119 | 178 |
| 120 NotifyCursorVisibilityChange(show); | 179 void CursorManager::EnableCursorInternal(bool enabled) { |
| 180 current_state_->SetEnabled(enabled); | |
| 181 | |
| 182 if (enabled) { | |
| 183 aura::Env::GetInstance()->set_last_mouse_location( | |
| 184 disabled_cursor_location_); | |
| 185 } else { | |
| 186 disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location(); | |
| 187 aura::Env::GetInstance()->set_last_mouse_location( | |
| 188 gfx::Point(kDisabledCursorLocationX, kDisabledCursorLocationY)); | |
| 189 } | |
| 190 | |
| 191 ShowCursorInternal(current_state_->visible()); | |
| 192 NotifyCursorEnableStateChange(enabled); | |
| 193 } | |
| 194 | |
| 195 gfx::NativeCursor CursorManager::GetCurrentCursor() const { | |
| 196 return current_state_->cursor(); | |
| 121 } | 197 } |
| 122 | 198 |
| 123 } // namespace ash | 199 } // namespace ash |
| OLD | NEW |