Chromium Code Reviews| Index: ash/wm/cursor_manager.cc |
| diff --git a/ash/wm/cursor_manager.cc b/ash/wm/cursor_manager.cc |
| index b25105172d2e9011cadedc603ee297e5479a770a..9081c316fe47d97a343b8e27f61792aabe56b838 100644 |
| --- a/ash/wm/cursor_manager.cc |
| +++ b/ash/wm/cursor_manager.cc |
| @@ -7,11 +7,16 @@ |
| #include "ash/shell.h" |
| #include "ash/wm/image_cursors.h" |
| #include "base/logging.h" |
| +#include "ui/aura/env.h" |
| #include "ui/aura/root_window.h" |
| #include "ui/base/cursor/cursor.h" |
| namespace { |
| +// 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.
|
| +const int kDisabledCursorLocationX = -10000; |
| +const int kDisabledCursorLocationY = -10000; |
| + |
| void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) { |
| ash::Shell::RootWindowList root_windows = |
| ash::Shell::GetInstance()->GetAllRootWindows(); |
| @@ -20,26 +25,68 @@ void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) { |
| (*iter)->SetCursor(cursor); |
| } |
| -void NotifyCursorVisibilityChange(bool visible) { |
| +void NotifyCursorEnableStateChange(bool visible) { |
| ash::Shell::RootWindowList root_windows = |
| ash::Shell::GetInstance()->GetAllRootWindows(); |
| for (ash::Shell::RootWindowList::iterator iter = root_windows.begin(); |
| iter != root_windows.end(); ++iter) |
| - (*iter)->OnCursorVisibilityChanged(visible); |
| + (*iter)->OnCursorEnableStateChanged(visible); |
| } |
| } // namespace |
| namespace ash { |
| +// Represents the cursor state which is composed of cursor type, visibility, and |
| +// enabled state. When the cursor is disabled, the cursor is always invisible. |
| +// In other words, the cursor cannot be disabled and visible at the same time. |
| +class CursorState { |
|
oshima
2012/12/05 23:46:45
put this in internal namespace.
mazda
2012/12/06 01:36:50
Done.
|
| + public: |
| + CursorState(gfx::NativeCursor cursor) |
|
oshima
2012/12/05 23:46:45
explicit
mazda
2012/12/06 01:36:50
Done.
|
| + : cursor_(cursor), |
| + visible_(true), |
| + enabled_(true), |
| + visible_on_enabled_(true) { |
| + } |
| + |
| + gfx::NativeCursor cursor() const { return cursor_; } |
| + void set_cursor(gfx::NativeCursor cursor) { cursor_ = cursor; } |
| + |
| + bool visible() const { return visible_; } |
| + void SetVisible(bool visible) { |
| + if (enabled_) |
| + visible_ = visible; |
| + // Ignores the call when the cursor is disabled. |
| + } |
| + |
| + bool enabled() const { return enabled_; } |
| + void SetEnabled(bool enabled) { |
| + enabled_ = enabled; |
| + |
| + // Restores the visibility when the cursor is enabled. |
| + if (enabled) { |
| + visible_ = visible_on_enabled_; |
| + } else { |
| + visible_on_enabled_ = visible_; |
| + visible_ = false; |
| + } |
| + } |
| + |
| + private: |
| + gfx::NativeCursor cursor_; |
| + bool visible_; |
| + bool enabled_; |
| + |
| + // The visibility to set when the cursor is enabled. |
| + bool visible_on_enabled_; |
| +}; |
|
oshima
2012/12/05 23:46:45
DISALLOW_COPY_AND_ASSIGN
mazda
2012/12/06 01:36:50
Done.
|
| + |
| CursorManager::CursorManager() |
| : cursor_lock_count_(0), |
| - did_cursor_change_(false), |
| - cursor_to_set_on_unlock_(0), |
| - did_visibility_change_(false), |
| - show_on_unlock_(true), |
| cursor_visible_(true), |
| + cursor_enabled_(true), |
| current_cursor_(ui::kCursorNone), |
| + state_on_unlock_(new CursorState(ui::kCursorNone)), |
| image_cursors_(new ImageCursors) { |
| } |
| @@ -47,27 +94,27 @@ CursorManager::~CursorManager() { |
| } |
| void CursorManager::SetCursor(gfx::NativeCursor cursor) { |
| - if (cursor_lock_count_ == 0) { |
| - SetCursorInternal(cursor); |
| - } else { |
| - cursor_to_set_on_unlock_ = cursor; |
| - did_cursor_change_ = true; |
| - } |
| + state_on_unlock_->set_cursor(cursor); |
| + if (cursor_lock_count_ == 0) |
| + SetCursorInternal(state_on_unlock_->cursor()); |
| } |
| void CursorManager::ShowCursor(bool show) { |
| - if (cursor_lock_count_ == 0) { |
| - ShowCursorInternal(show); |
| - } else { |
| - show_on_unlock_ = show; |
| - did_visibility_change_ = true; |
| - } |
| + state_on_unlock_->SetVisible(show); |
| + if (cursor_lock_count_ == 0) |
| + ShowCursorInternal(state_on_unlock_->visible()); |
| } |
| bool CursorManager::IsCursorVisible() const { |
| return cursor_visible_; |
| } |
| +void CursorManager::EnableCursor(bool enabled) { |
| + state_on_unlock_->SetEnabled(enabled); |
| + if (cursor_lock_count_ == 0) |
| + EnableCursorInternal(state_on_unlock_->enabled()); |
| +} |
| + |
| void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { |
| if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) |
| SetCursorInternal(current_cursor_); |
| @@ -83,14 +130,14 @@ void CursorManager::UnlockCursor() { |
| if (cursor_lock_count_ > 0) |
| return; |
| - if (did_cursor_change_) |
| - SetCursorInternal(cursor_to_set_on_unlock_); |
| - did_cursor_change_ = false; |
| - cursor_to_set_on_unlock_ = gfx::kNullCursor; |
| + if (current_cursor_ != state_on_unlock_->cursor()) |
| + SetCursorInternal(state_on_unlock_->cursor()); |
| + |
| + if (cursor_enabled_ != state_on_unlock_->enabled()) |
| + EnableCursorInternal(state_on_unlock_->enabled()); |
| - if (did_visibility_change_) |
| - ShowCursorInternal(show_on_unlock_); |
| - did_visibility_change_ = false; |
| + if (cursor_visible_ != state_on_unlock_->visible()) |
| + ShowCursorInternal(state_on_unlock_->visible()); |
| } |
| void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { |
| @@ -116,8 +163,25 @@ void CursorManager::ShowCursorInternal(bool show) { |
| image_cursors_->SetPlatformCursor(&invisible_cursor); |
| SetCursorOnAllRootWindows(invisible_cursor); |
| } |
| +} |
| - NotifyCursorVisibilityChange(show); |
| +void CursorManager::EnableCursorInternal(bool enabled) { |
| + if (cursor_enabled_ == enabled) |
| + return; |
| + |
| + cursor_enabled_ = enabled; |
| + |
| + if (enabled) { |
| + aura::Env::GetInstance()->set_last_mouse_location( |
| + disabled_cursor_location_); |
| + } else { |
| + disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location(); |
| + aura::Env::GetInstance()->set_last_mouse_location( |
| + gfx::Point(kDisabledCursorLocationX, kDisabledCursorLocationY)); |
| + } |
| + ShowCursorInternal(state_on_unlock_->visible()); |
| + NotifyCursorEnableStateChange(enabled); |
| } |
| + |
| } // namespace ash |