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/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 coordinates of the cursor which is set to when the cursor is disabled. | |
|
oshima
2012/12/05 23:46:45
The coordinate of the cursor used when the cursor
mazda
2012/12/06 01:36:50
Done.
| |
| 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 NotifyCursorEnableStateChange(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)->OnCursorEnableStateChanged(visible); |
| 29 } | 34 } |
| 30 | 35 |
| 31 } // namespace | 36 } // namespace |
| 32 | 37 |
| 33 namespace ash { | 38 namespace ash { |
| 34 | 39 |
| 40 // Represents the cursor state which is composed of cursor type, visibility, and | |
| 41 // enabled state. When the cursor is disabled, the cursor is always invisible. | |
| 42 // In other words, the cursor cannot be disabled and visible at the same time. | |
| 43 class CursorState { | |
|
oshima
2012/12/05 23:46:45
put this in internal namespace.
mazda
2012/12/06 01:36:50
Done.
| |
| 44 public: | |
| 45 CursorState(gfx::NativeCursor cursor) | |
|
oshima
2012/12/05 23:46:45
explicit
mazda
2012/12/06 01:36:50
Done.
| |
| 46 : cursor_(cursor), | |
| 47 visible_(true), | |
| 48 enabled_(true), | |
| 49 visible_on_enabled_(true) { | |
| 50 } | |
| 51 | |
| 52 gfx::NativeCursor cursor() const { return cursor_; } | |
| 53 void set_cursor(gfx::NativeCursor cursor) { cursor_ = cursor; } | |
| 54 | |
| 55 bool visible() const { return visible_; } | |
| 56 void SetVisible(bool visible) { | |
| 57 if (enabled_) | |
| 58 visible_ = visible; | |
| 59 // Ignores the call when the cursor is disabled. | |
| 60 } | |
| 61 | |
| 62 bool enabled() const { return enabled_; } | |
| 63 void SetEnabled(bool enabled) { | |
| 64 enabled_ = enabled; | |
| 65 | |
| 66 // Restores the visibility when the cursor is enabled. | |
| 67 if (enabled) { | |
| 68 visible_ = visible_on_enabled_; | |
| 69 } else { | |
| 70 visible_on_enabled_ = visible_; | |
| 71 visible_ = false; | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 gfx::NativeCursor cursor_; | |
| 77 bool visible_; | |
| 78 bool enabled_; | |
| 79 | |
| 80 // The visibility to set when the cursor is enabled. | |
| 81 bool visible_on_enabled_; | |
| 82 }; | |
|
oshima
2012/12/05 23:46:45
DISALLOW_COPY_AND_ASSIGN
mazda
2012/12/06 01:36:50
Done.
| |
| 83 | |
| 35 CursorManager::CursorManager() | 84 CursorManager::CursorManager() |
| 36 : cursor_lock_count_(0), | 85 : cursor_lock_count_(0), |
| 37 did_cursor_change_(false), | |
| 38 cursor_to_set_on_unlock_(0), | |
| 39 did_visibility_change_(false), | |
| 40 show_on_unlock_(true), | |
| 41 cursor_visible_(true), | 86 cursor_visible_(true), |
| 87 cursor_enabled_(true), | |
| 42 current_cursor_(ui::kCursorNone), | 88 current_cursor_(ui::kCursorNone), |
| 89 state_on_unlock_(new CursorState(ui::kCursorNone)), | |
| 43 image_cursors_(new ImageCursors) { | 90 image_cursors_(new ImageCursors) { |
| 44 } | 91 } |
| 45 | 92 |
| 46 CursorManager::~CursorManager() { | 93 CursorManager::~CursorManager() { |
| 47 } | 94 } |
| 48 | 95 |
| 49 void CursorManager::SetCursor(gfx::NativeCursor cursor) { | 96 void CursorManager::SetCursor(gfx::NativeCursor cursor) { |
| 50 if (cursor_lock_count_ == 0) { | 97 state_on_unlock_->set_cursor(cursor); |
| 51 SetCursorInternal(cursor); | 98 if (cursor_lock_count_ == 0) |
| 52 } else { | 99 SetCursorInternal(state_on_unlock_->cursor()); |
| 53 cursor_to_set_on_unlock_ = cursor; | |
| 54 did_cursor_change_ = true; | |
| 55 } | |
| 56 } | 100 } |
| 57 | 101 |
| 58 void CursorManager::ShowCursor(bool show) { | 102 void CursorManager::ShowCursor(bool show) { |
| 59 if (cursor_lock_count_ == 0) { | 103 state_on_unlock_->SetVisible(show); |
| 60 ShowCursorInternal(show); | 104 if (cursor_lock_count_ == 0) |
| 61 } else { | 105 ShowCursorInternal(state_on_unlock_->visible()); |
| 62 show_on_unlock_ = show; | |
| 63 did_visibility_change_ = true; | |
| 64 } | |
| 65 } | 106 } |
| 66 | 107 |
| 67 bool CursorManager::IsCursorVisible() const { | 108 bool CursorManager::IsCursorVisible() const { |
| 68 return cursor_visible_; | 109 return cursor_visible_; |
| 69 } | 110 } |
| 70 | 111 |
| 112 void CursorManager::EnableCursor(bool enabled) { | |
| 113 state_on_unlock_->SetEnabled(enabled); | |
| 114 if (cursor_lock_count_ == 0) | |
| 115 EnableCursorInternal(state_on_unlock_->enabled()); | |
| 116 } | |
| 117 | |
| 71 void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { | 118 void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { |
| 72 if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) | 119 if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) |
| 73 SetCursorInternal(current_cursor_); | 120 SetCursorInternal(current_cursor_); |
| 74 } | 121 } |
| 75 | 122 |
| 76 void CursorManager::LockCursor() { | 123 void CursorManager::LockCursor() { |
| 77 cursor_lock_count_++; | 124 cursor_lock_count_++; |
| 78 } | 125 } |
| 79 | 126 |
| 80 void CursorManager::UnlockCursor() { | 127 void CursorManager::UnlockCursor() { |
| 81 cursor_lock_count_--; | 128 cursor_lock_count_--; |
| 82 DCHECK_GE(cursor_lock_count_, 0); | 129 DCHECK_GE(cursor_lock_count_, 0); |
| 83 if (cursor_lock_count_ > 0) | 130 if (cursor_lock_count_ > 0) |
| 84 return; | 131 return; |
| 85 | 132 |
| 86 if (did_cursor_change_) | 133 if (current_cursor_ != state_on_unlock_->cursor()) |
| 87 SetCursorInternal(cursor_to_set_on_unlock_); | 134 SetCursorInternal(state_on_unlock_->cursor()); |
| 88 did_cursor_change_ = false; | |
| 89 cursor_to_set_on_unlock_ = gfx::kNullCursor; | |
| 90 | 135 |
| 91 if (did_visibility_change_) | 136 if (cursor_enabled_ != state_on_unlock_->enabled()) |
| 92 ShowCursorInternal(show_on_unlock_); | 137 EnableCursorInternal(state_on_unlock_->enabled()); |
| 93 did_visibility_change_ = false; | 138 |
| 139 if (cursor_visible_ != state_on_unlock_->visible()) | |
| 140 ShowCursorInternal(state_on_unlock_->visible()); | |
| 94 } | 141 } |
| 95 | 142 |
| 96 void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { | 143 void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { |
| 97 current_cursor_ = cursor; | 144 current_cursor_ = cursor; |
| 98 image_cursors_->SetPlatformCursor(¤t_cursor_); | 145 image_cursors_->SetPlatformCursor(¤t_cursor_); |
| 99 current_cursor_.set_device_scale_factor( | 146 current_cursor_.set_device_scale_factor( |
| 100 image_cursors_->GetDeviceScaleFactor()); | 147 image_cursors_->GetDeviceScaleFactor()); |
| 101 | 148 |
| 102 if (cursor_visible_) | 149 if (cursor_visible_) |
| 103 SetCursorOnAllRootWindows(current_cursor_); | 150 SetCursorOnAllRootWindows(current_cursor_); |
| 104 } | 151 } |
| 105 | 152 |
| 106 void CursorManager::ShowCursorInternal(bool show) { | 153 void CursorManager::ShowCursorInternal(bool show) { |
| 107 if (cursor_visible_ == show) | 154 if (cursor_visible_ == show) |
| 108 return; | 155 return; |
| 109 | 156 |
| 110 cursor_visible_ = show; | 157 cursor_visible_ = show; |
| 111 | 158 |
| 112 if (show) { | 159 if (show) { |
| 113 SetCursorInternal(current_cursor_); | 160 SetCursorInternal(current_cursor_); |
| 114 } else { | 161 } else { |
| 115 gfx::NativeCursor invisible_cursor(ui::kCursorNone); | 162 gfx::NativeCursor invisible_cursor(ui::kCursorNone); |
| 116 image_cursors_->SetPlatformCursor(&invisible_cursor); | 163 image_cursors_->SetPlatformCursor(&invisible_cursor); |
| 117 SetCursorOnAllRootWindows(invisible_cursor); | 164 SetCursorOnAllRootWindows(invisible_cursor); |
| 118 } | 165 } |
| 119 | |
| 120 NotifyCursorVisibilityChange(show); | |
| 121 } | 166 } |
| 122 | 167 |
| 168 void CursorManager::EnableCursorInternal(bool enabled) { | |
| 169 if (cursor_enabled_ == enabled) | |
| 170 return; | |
| 171 | |
| 172 cursor_enabled_ = enabled; | |
| 173 | |
| 174 if (enabled) { | |
| 175 aura::Env::GetInstance()->set_last_mouse_location( | |
| 176 disabled_cursor_location_); | |
| 177 } else { | |
| 178 disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location(); | |
| 179 aura::Env::GetInstance()->set_last_mouse_location( | |
| 180 gfx::Point(kDisabledCursorLocationX, kDisabledCursorLocationY)); | |
| 181 } | |
| 182 ShowCursorInternal(state_on_unlock_->visible()); | |
| 183 NotifyCursorEnableStateChange(enabled); | |
| 184 } | |
| 185 | |
| 186 | |
| 123 } // namespace ash | 187 } // namespace ash |
| OLD | NEW |