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), | |
38 cursor_to_set_on_unlock_(0), | |
39 did_visibility_change_(false), | |
40 show_on_unlock_(true), | |
41 cursor_visible_(true), | 92 cursor_visible_(true), |
93 cursor_enabled_(true), | |
42 current_cursor_(ui::kCursorNone), | 94 current_cursor_(ui::kCursorNone), |
95 state_on_unlock_(new internal::CursorState(ui::kCursorNone)), | |
43 image_cursors_(new ImageCursors) { | 96 image_cursors_(new ImageCursors) { |
44 } | 97 } |
45 | 98 |
46 CursorManager::~CursorManager() { | 99 CursorManager::~CursorManager() { |
47 } | 100 } |
48 | 101 |
49 void CursorManager::SetCursor(gfx::NativeCursor cursor) { | 102 void CursorManager::SetCursor(gfx::NativeCursor cursor) { |
50 if (cursor_lock_count_ == 0) { | 103 state_on_unlock_->set_cursor(cursor); |
51 SetCursorInternal(cursor); | 104 if (cursor_lock_count_ == 0) |
52 } else { | 105 SetCursorInternal(state_on_unlock_->cursor()); |
53 cursor_to_set_on_unlock_ = cursor; | |
54 did_cursor_change_ = true; | |
55 } | |
56 } | 106 } |
57 | 107 |
58 void CursorManager::ShowCursor(bool show) { | 108 void CursorManager::ShowCursor(bool show) { |
59 if (cursor_lock_count_ == 0) { | 109 state_on_unlock_->SetVisible(show); |
60 ShowCursorInternal(show); | 110 if (cursor_lock_count_ == 0) |
61 } else { | 111 ShowCursorInternal(state_on_unlock_->visible()); |
62 show_on_unlock_ = show; | |
63 did_visibility_change_ = true; | |
64 } | |
65 } | 112 } |
66 | 113 |
67 bool CursorManager::IsCursorVisible() const { | 114 bool CursorManager::IsCursorVisible() const { |
68 return cursor_visible_; | 115 return cursor_visible_; |
69 } | 116 } |
70 | 117 |
118 void CursorManager::EnableCursor(bool enabled) { | |
119 state_on_unlock_->SetEnabled(enabled); | |
120 if (cursor_lock_count_ == 0) | |
121 EnableCursorInternal(state_on_unlock_->enabled()); | |
122 } | |
123 | |
71 void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { | 124 void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { |
72 if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) | 125 if (image_cursors_->SetDeviceScaleFactor(device_scale_factor)) |
73 SetCursorInternal(current_cursor_); | 126 SetCursorInternal(current_cursor_); |
74 } | 127 } |
75 | 128 |
76 void CursorManager::LockCursor() { | 129 void CursorManager::LockCursor() { |
77 cursor_lock_count_++; | 130 cursor_lock_count_++; |
78 } | 131 } |
79 | 132 |
80 void CursorManager::UnlockCursor() { | 133 void CursorManager::UnlockCursor() { |
81 cursor_lock_count_--; | 134 cursor_lock_count_--; |
82 DCHECK_GE(cursor_lock_count_, 0); | 135 DCHECK_GE(cursor_lock_count_, 0); |
83 if (cursor_lock_count_ > 0) | 136 if (cursor_lock_count_ > 0) |
84 return; | 137 return; |
85 | 138 |
86 if (did_cursor_change_) | 139 if (current_cursor_ != state_on_unlock_->cursor()) |
87 SetCursorInternal(cursor_to_set_on_unlock_); | 140 SetCursorInternal(state_on_unlock_->cursor()); |
88 did_cursor_change_ = false; | |
89 cursor_to_set_on_unlock_ = gfx::kNullCursor; | |
90 | 141 |
91 if (did_visibility_change_) | 142 if (cursor_enabled_ != state_on_unlock_->enabled()) |
92 ShowCursorInternal(show_on_unlock_); | 143 EnableCursorInternal(state_on_unlock_->enabled()); |
93 did_visibility_change_ = false; | 144 |
145 if (cursor_visible_ != state_on_unlock_->visible()) | |
146 ShowCursorInternal(state_on_unlock_->visible()); | |
oshima
2012/12/06 18:55:46
It'd be better to have "current_state_", and
"stat
mazda
2012/12/07 20:50:51
That's a good idea to have current_state_ and I ch
| |
94 } | 147 } |
95 | 148 |
96 void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { | 149 void CursorManager::SetCursorInternal(gfx::NativeCursor cursor) { |
97 current_cursor_ = cursor; | 150 current_cursor_ = cursor; |
98 image_cursors_->SetPlatformCursor(¤t_cursor_); | 151 image_cursors_->SetPlatformCursor(¤t_cursor_); |
99 current_cursor_.set_device_scale_factor( | 152 current_cursor_.set_device_scale_factor( |
100 image_cursors_->GetDeviceScaleFactor()); | 153 image_cursors_->GetDeviceScaleFactor()); |
101 | 154 |
102 if (cursor_visible_) | 155 if (cursor_visible_) |
103 SetCursorOnAllRootWindows(current_cursor_); | 156 SetCursorOnAllRootWindows(current_cursor_); |
104 } | 157 } |
105 | 158 |
106 void CursorManager::ShowCursorInternal(bool show) { | 159 void CursorManager::ShowCursorInternal(bool show) { |
107 if (cursor_visible_ == show) | 160 if (cursor_visible_ == show) |
108 return; | 161 return; |
109 | 162 |
110 cursor_visible_ = show; | 163 cursor_visible_ = show; |
111 | 164 |
112 if (show) { | 165 if (show) { |
113 SetCursorInternal(current_cursor_); | 166 SetCursorInternal(current_cursor_); |
114 } else { | 167 } else { |
115 gfx::NativeCursor invisible_cursor(ui::kCursorNone); | 168 gfx::NativeCursor invisible_cursor(ui::kCursorNone); |
116 image_cursors_->SetPlatformCursor(&invisible_cursor); | 169 image_cursors_->SetPlatformCursor(&invisible_cursor); |
117 SetCursorOnAllRootWindows(invisible_cursor); | 170 SetCursorOnAllRootWindows(invisible_cursor); |
118 } | 171 } |
119 | |
120 NotifyCursorVisibilityChange(show); | |
121 } | 172 } |
122 | 173 |
174 void CursorManager::EnableCursorInternal(bool enabled) { | |
175 if (cursor_enabled_ == enabled) | |
176 return; | |
177 | |
178 cursor_enabled_ = enabled; | |
179 | |
180 if (enabled) { | |
181 aura::Env::GetInstance()->set_last_mouse_location( | |
182 disabled_cursor_location_); | |
183 } else { | |
184 disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location(); | |
185 aura::Env::GetInstance()->set_last_mouse_location( | |
186 gfx::Point(kDisabledCursorLocationX, kDisabledCursorLocationY)); | |
187 } | |
188 ShowCursorInternal(state_on_unlock_->visible()); | |
189 NotifyCursorEnableStateChange(enabled); | |
190 } | |
191 | |
192 | |
123 } // namespace ash | 193 } // namespace ash |
OLD | NEW |