Index: ash/wm/cursor_manager.cc |
diff --git a/ash/wm/cursor_manager.cc b/ash/wm/cursor_manager.cc |
index b25105172d2e9011cadedc603ee297e5479a770a..b24b8637600d564009ae417de055d47dbff11ae0 100644 |
--- a/ash/wm/cursor_manager.cc |
+++ b/ash/wm/cursor_manager.cc |
@@ -6,12 +6,18 @@ |
#include "ash/shell.h" |
#include "ash/wm/image_cursors.h" |
+#include "base/basictypes.h" |
#include "base/logging.h" |
+#include "ui/aura/env.h" |
#include "ui/aura/root_window.h" |
#include "ui/base/cursor/cursor.h" |
namespace { |
+// The coordinate of the cursor used when the cursor is disabled. |
+const int kDisabledCursorLocationX = -10000; |
+const int kDisabledCursorLocationY = -10000; |
+ |
void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) { |
ash::Shell::RootWindowList root_windows = |
ash::Shell::GetInstance()->GetAllRootWindows(); |
@@ -20,26 +26,71 @@ 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 { |
+namespace internal { |
+ |
+// 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 { |
+ public: |
+ explicit CursorState(gfx::NativeCursor cursor) |
+ : 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_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(CursorState); |
+}; |
+ |
+} // namespace internal |
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), |
- current_cursor_(ui::kCursorNone), |
+ current_state_(new internal::CursorState(ui::kCursorNone)), |
+ state_on_unlock_(new internal::CursorState(ui::kCursorNone)), |
image_cursors_(new ImageCursors) { |
} |
@@ -47,30 +98,40 @@ 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 && |
+ GetCurrentCursor() != state_on_unlock_->cursor()) { |
+ SetCursorInternal(state_on_unlock_->cursor()); |
} |
} |
+bool CursorManager::IsCursorEnabled() const { |
+ return current_state_->enabled(); |
+} |
+ |
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 && |
+ IsCursorVisible() != state_on_unlock_->visible()) { |
+ ShowCursorInternal(state_on_unlock_->visible()); |
} |
} |
bool CursorManager::IsCursorVisible() const { |
- return cursor_visible_; |
+ return current_state_->visible(); |
+} |
+ |
+void CursorManager::EnableCursor(bool enabled) { |
+ state_on_unlock_->SetEnabled(enabled); |
+ if (cursor_lock_count_ == 0 && |
+ IsCursorEnabled() != state_on_unlock_->enabled()) { |
+ EnableCursorInternal(state_on_unlock_->enabled()); |
+ } |
} |
void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { |
if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) |
- SetCursorInternal(current_cursor_); |
+ SetCursorInternal(GetCurrentCursor()); |
} |
void CursorManager::LockCursor() { |
@@ -83,41 +144,56 @@ 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 (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
|
+ SetCursorInternal(state_on_unlock_->cursor()); |
- if (did_visibility_change_) |
- ShowCursorInternal(show_on_unlock_); |
- did_visibility_change_ = false; |
+ 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.
|
+ EnableCursorInternal(state_on_unlock_->enabled()); |
+ |
+ 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.
|
+ ShowCursorInternal(state_on_unlock_->visible()); |
} |
void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { |
- current_cursor_ = cursor; |
- image_cursors_->SetPlatformCursor(¤t_cursor_); |
- current_cursor_.set_device_scale_factor( |
- image_cursors_->GetDeviceScaleFactor()); |
+ gfx::NativeCursor new_cursor = cursor; |
+ image_cursors_->SetPlatformCursor(&new_cursor); |
+ new_cursor.set_device_scale_factor(image_cursors_->GetDeviceScaleFactor()); |
+ current_state_->set_cursor(new_cursor); |
- if (cursor_visible_) |
- SetCursorOnAllRootWindows(current_cursor_); |
+ if (IsCursorVisible()) |
+ SetCursorOnAllRootWindows(GetCurrentCursor()); |
} |
void CursorManager::ShowCursorInternal(bool show) { |
- if (cursor_visible_ == show) |
- return; |
- |
- cursor_visible_ = show; |
+ current_state_->SetVisible(show); |
if (show) { |
- SetCursorInternal(current_cursor_); |
+ SetCursorInternal(GetCurrentCursor()); |
} else { |
gfx::NativeCursor invisible_cursor(ui::kCursorNone); |
image_cursors_->SetPlatformCursor(&invisible_cursor); |
SetCursorOnAllRootWindows(invisible_cursor); |
} |
+} |
+ |
+void CursorManager::EnableCursorInternal(bool enabled) { |
+ current_state_->SetEnabled(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(current_state_->visible()); |
+ NotifyCursorEnableStateChange(enabled); |
+} |
- NotifyCursorVisibilityChange(show); |
+gfx::NativeCursor CursorManager::GetCurrentCursor() const { |
+ return current_state_->cursor(); |
} |
} // namespace ash |