OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/corewm/cursor_manager.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "ui/views/corewm/native_cursor_manager.h" |
| 9 #include "ui/views/corewm/native_cursor_manager_delegate.h" |
| 10 |
| 11 namespace views { |
| 12 namespace corewm { |
| 13 |
| 14 namespace internal { |
| 15 |
| 16 // Represents the cursor state which is composed of cursor type, visibility, and |
| 17 // mouse events enable state. When mouse events are disabled, the cursor is |
| 18 // always invisible. |
| 19 class CursorState { |
| 20 public: |
| 21 CursorState() |
| 22 : cursor_(ui::kCursorNone), |
| 23 visible_(true), |
| 24 mouse_events_enabled_(true), |
| 25 visible_on_mouse_events_enabled_(true) { |
| 26 } |
| 27 |
| 28 gfx::NativeCursor cursor() const { return cursor_; } |
| 29 void set_cursor(gfx::NativeCursor cursor) { cursor_ = cursor; } |
| 30 |
| 31 bool visible() const { return visible_; } |
| 32 void SetVisible(bool visible) { |
| 33 if (mouse_events_enabled_) |
| 34 visible_ = visible; |
| 35 // Ignores the call when mouse events disabled. |
| 36 } |
| 37 |
| 38 bool mouse_events_enabled() const { return mouse_events_enabled_; } |
| 39 void SetMouseEventsEnabled(bool enabled) { |
| 40 if (mouse_events_enabled_ == enabled) |
| 41 return; |
| 42 mouse_events_enabled_ = enabled; |
| 43 |
| 44 // Restores the visibility when mouse events are enabled. |
| 45 if (enabled) { |
| 46 visible_ = visible_on_mouse_events_enabled_; |
| 47 } else { |
| 48 visible_on_mouse_events_enabled_ = visible_; |
| 49 visible_ = false; |
| 50 } |
| 51 } |
| 52 |
| 53 private: |
| 54 gfx::NativeCursor cursor_; |
| 55 bool visible_; |
| 56 bool mouse_events_enabled_; |
| 57 |
| 58 // The visibility to set when mouse events are enabled. |
| 59 bool visible_on_mouse_events_enabled_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(CursorState); |
| 62 }; |
| 63 |
| 64 } // namespace internal |
| 65 |
| 66 CursorManager::CursorManager(scoped_ptr<NativeCursorManager> delegate) |
| 67 : delegate_(delegate.Pass()), |
| 68 cursor_lock_count_(0), |
| 69 current_state_(new internal::CursorState), |
| 70 state_on_unlock_(new internal::CursorState) { |
| 71 } |
| 72 |
| 73 CursorManager::~CursorManager() { |
| 74 } |
| 75 |
| 76 void CursorManager::SetCursor(gfx::NativeCursor cursor) { |
| 77 state_on_unlock_->set_cursor(cursor); |
| 78 if (cursor_lock_count_ == 0 && |
| 79 GetCurrentCursor() != state_on_unlock_->cursor()) { |
| 80 delegate_->SetCursor(state_on_unlock_->cursor(), this); |
| 81 } |
| 82 } |
| 83 |
| 84 void CursorManager::ShowCursor() { |
| 85 state_on_unlock_->SetVisible(true); |
| 86 if (cursor_lock_count_ == 0 && |
| 87 IsCursorVisible() != state_on_unlock_->visible()) { |
| 88 delegate_->SetVisibility(state_on_unlock_->visible(), this); |
| 89 } |
| 90 } |
| 91 |
| 92 void CursorManager::HideCursor() { |
| 93 state_on_unlock_->SetVisible(false); |
| 94 if (cursor_lock_count_ == 0 && |
| 95 IsCursorVisible() != state_on_unlock_->visible()) { |
| 96 delegate_->SetVisibility(state_on_unlock_->visible(), this); |
| 97 } |
| 98 } |
| 99 |
| 100 bool CursorManager::IsCursorVisible() const { |
| 101 return current_state_->visible(); |
| 102 } |
| 103 |
| 104 void CursorManager::EnableMouseEvents() { |
| 105 state_on_unlock_->SetMouseEventsEnabled(true); |
| 106 if (cursor_lock_count_ == 0 && |
| 107 IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) { |
| 108 delegate_->SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled(), |
| 109 this); |
| 110 } |
| 111 } |
| 112 |
| 113 void CursorManager::DisableMouseEvents() { |
| 114 state_on_unlock_->SetMouseEventsEnabled(false); |
| 115 if (cursor_lock_count_ == 0 && |
| 116 IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) { |
| 117 delegate_->SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled(), |
| 118 this); |
| 119 } |
| 120 } |
| 121 |
| 122 bool CursorManager::IsMouseEventsEnabled() const { |
| 123 return current_state_->mouse_events_enabled(); |
| 124 } |
| 125 |
| 126 void CursorManager::SetDeviceScaleFactor(float device_scale_factor) { |
| 127 delegate_->SetDeviceScaleFactor(device_scale_factor, this); |
| 128 } |
| 129 |
| 130 void CursorManager::LockCursor() { |
| 131 cursor_lock_count_++; |
| 132 } |
| 133 |
| 134 void CursorManager::UnlockCursor() { |
| 135 cursor_lock_count_--; |
| 136 DCHECK_GE(cursor_lock_count_, 0); |
| 137 if (cursor_lock_count_ > 0) |
| 138 return; |
| 139 |
| 140 if (GetCurrentCursor() != state_on_unlock_->cursor()) { |
| 141 delegate_->SetCursor(state_on_unlock_->cursor(), this); |
| 142 } |
| 143 if (IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) { |
| 144 delegate_->SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled(), |
| 145 this); |
| 146 } |
| 147 if (IsCursorVisible() != state_on_unlock_->visible()) { |
| 148 delegate_->SetVisibility(state_on_unlock_->visible(), |
| 149 this); |
| 150 } |
| 151 } |
| 152 |
| 153 gfx::NativeCursor CursorManager::GetCurrentCursor() const { |
| 154 return current_state_->cursor(); |
| 155 } |
| 156 |
| 157 bool CursorManager::GetCurrentVisibility() const { |
| 158 return current_state_->visible(); |
| 159 } |
| 160 |
| 161 bool CursorManager::GetMouseEventsEnabled() const { |
| 162 return current_state_->mouse_events_enabled(); |
| 163 } |
| 164 |
| 165 void CursorManager::CommitCursor(gfx::NativeCursor cursor) { |
| 166 current_state_->set_cursor(cursor); |
| 167 } |
| 168 |
| 169 void CursorManager::CommitVisibility(bool visible) { |
| 170 current_state_->SetVisible(visible); |
| 171 } |
| 172 |
| 173 void CursorManager::CommitMouseEventsEnabled(bool enabled) { |
| 174 current_state_->SetMouseEventsEnabled(enabled); |
| 175 } |
| 176 |
| 177 } // namespace corewm |
| 178 } // namespace views |
OLD | NEW |