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_controller.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace views { |
| 10 namespace corewm { |
| 11 |
| 12 namespace internal { |
| 13 |
| 14 // Represents the cursor state which is composed of cursor type, visibility, and |
| 15 // mouse events enable state. When mouse events are disabled, the cursor is |
| 16 // always invisible. |
| 17 class CursorState { |
| 18 public: |
| 19 CursorState() |
| 20 : cursor_(ui::kCursorNone), |
| 21 visible_(true), |
| 22 mouse_events_enabled_(true), |
| 23 visible_on_mouse_events_enabled_(true) { |
| 24 } |
| 25 |
| 26 gfx::NativeCursor cursor() const { return cursor_; } |
| 27 void set_cursor(gfx::NativeCursor cursor) { cursor_ = cursor; } |
| 28 |
| 29 bool visible() const { return visible_; } |
| 30 void SetVisible(bool visible) { |
| 31 if (mouse_events_enabled_) |
| 32 visible_ = visible; |
| 33 // Ignores the call when mouse events disabled. |
| 34 } |
| 35 |
| 36 bool mouse_events_enabled() const { return mouse_events_enabled_; } |
| 37 void SetMouseEventsEnabled(bool enabled) { |
| 38 if (mouse_events_enabled_ == enabled) |
| 39 return; |
| 40 mouse_events_enabled_ = enabled; |
| 41 |
| 42 // Restores the visibility when mouse events are enabled. |
| 43 if (enabled) { |
| 44 visible_ = visible_on_mouse_events_enabled_; |
| 45 } else { |
| 46 visible_on_mouse_events_enabled_ = visible_; |
| 47 visible_ = false; |
| 48 } |
| 49 } |
| 50 |
| 51 private: |
| 52 gfx::NativeCursor cursor_; |
| 53 bool visible_; |
| 54 bool mouse_events_enabled_; |
| 55 |
| 56 // The visibility to set when mouse events are enabled. |
| 57 bool visible_on_mouse_events_enabled_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(CursorState); |
| 60 }; |
| 61 |
| 62 } // namespace internal |
| 63 |
| 64 CursorController::CursorController() |
| 65 : cursor_lock_count_(0), |
| 66 current_state_(new internal::CursorState), |
| 67 state_on_unlock_(new internal::CursorState) { |
| 68 } |
| 69 |
| 70 CursorController::~CursorController() { |
| 71 } |
| 72 |
| 73 void CursorController::SetCursor(gfx::NativeCursor cursor) { |
| 74 state_on_unlock_->set_cursor(cursor); |
| 75 if (cursor_lock_count_ == 0 && |
| 76 GetCurrentCursor() != state_on_unlock_->cursor()) { |
| 77 SetCursorInternal(state_on_unlock_->cursor()); |
| 78 } |
| 79 } |
| 80 |
| 81 void CursorController::ShowCursor() { |
| 82 state_on_unlock_->SetVisible(true); |
| 83 if (cursor_lock_count_ == 0 && |
| 84 IsCursorVisible() != state_on_unlock_->visible()) { |
| 85 SetCursorVisibility(state_on_unlock_->visible()); |
| 86 } |
| 87 } |
| 88 |
| 89 void CursorController::HideCursor() { |
| 90 state_on_unlock_->SetVisible(false); |
| 91 if (cursor_lock_count_ == 0 && |
| 92 IsCursorVisible() != state_on_unlock_->visible()) { |
| 93 SetCursorVisibility(state_on_unlock_->visible()); |
| 94 } |
| 95 } |
| 96 |
| 97 bool CursorController::IsCursorVisible() const { |
| 98 return current_state_->visible(); |
| 99 } |
| 100 |
| 101 void CursorController::EnableMouseEvents() { |
| 102 state_on_unlock_->SetMouseEventsEnabled(true); |
| 103 if (cursor_lock_count_ == 0 && |
| 104 IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) { |
| 105 SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled()); |
| 106 } |
| 107 } |
| 108 |
| 109 void CursorController::DisableMouseEvents() { |
| 110 state_on_unlock_->SetMouseEventsEnabled(false); |
| 111 if (cursor_lock_count_ == 0 && |
| 112 IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) { |
| 113 SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled()); |
| 114 } |
| 115 } |
| 116 |
| 117 bool CursorController::IsMouseEventsEnabled() const { |
| 118 return current_state_->mouse_events_enabled(); |
| 119 } |
| 120 |
| 121 void CursorController::LockCursor() { |
| 122 cursor_lock_count_++; |
| 123 } |
| 124 |
| 125 void CursorController::UnlockCursor() { |
| 126 cursor_lock_count_--; |
| 127 DCHECK_GE(cursor_lock_count_, 0); |
| 128 if (cursor_lock_count_ > 0) |
| 129 return; |
| 130 |
| 131 if (GetCurrentCursor() != state_on_unlock_->cursor()) |
| 132 SetCursorInternal(state_on_unlock_->cursor()); |
| 133 if (IsMouseEventsEnabled() != state_on_unlock_->mouse_events_enabled()) |
| 134 SetMouseEventsEnabled(state_on_unlock_->mouse_events_enabled()); |
| 135 if (IsCursorVisible() != state_on_unlock_->visible()) |
| 136 SetCursorVisibility(state_on_unlock_->visible()); |
| 137 } |
| 138 |
| 139 gfx::NativeCursor CursorController::GetCurrentCursor() const { |
| 140 return current_state_->cursor(); |
| 141 } |
| 142 |
| 143 void CursorController::SetCursorInternal(gfx::NativeCursor cursor) { |
| 144 current_state_->set_cursor(cursor); |
| 145 } |
| 146 |
| 147 void CursorController::SetCursorVisibility(bool visible) { |
| 148 current_state_->SetVisible(visible); |
| 149 } |
| 150 |
| 151 void CursorController::SetMouseEventsEnabled(bool enabled) { |
| 152 current_state_->SetMouseEventsEnabled(enabled); |
| 153 } |
| 154 |
| 155 } // namespace corewm |
| 156 } // namespace views |
OLD | NEW |