| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "services/ui/ws/cursor_state.h" | 5 #include "services/ui/ws/cursor_state.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "services/ui/ws/display.h" | 8 #include "services/ui/ws/display.h" |
| 9 #include "services/ui/ws/display_manager.h" | 9 #include "services/ui/ws/display_manager.h" |
| 10 #include "ui/base/cursor/cursor.h" | 10 #include "ui/base/cursor/cursor.h" |
| 11 | 11 |
| 12 namespace ui { | 12 namespace ui { |
| 13 namespace ws { | 13 namespace ws { |
| 14 | 14 |
| 15 class CursorState::StateSnapshot { | 15 class CursorState::StateSnapshot { |
| 16 public: | 16 public: |
| 17 StateSnapshot() | 17 StateSnapshot() = default; |
| 18 : cursor_data_(ui::CursorData(ui::CursorType::kNull)), visible_(true) {} | |
| 19 StateSnapshot(const StateSnapshot& rhs) = default; | 18 StateSnapshot(const StateSnapshot& rhs) = default; |
| 20 ~StateSnapshot() {} | 19 ~StateSnapshot() = default; |
| 21 | 20 |
| 22 const base::Optional<ui::CursorData>& global_override_cursor() const { | 21 const base::Optional<ui::CursorData>& global_override_cursor() const { |
| 23 return global_override_cursor_; | 22 return global_override_cursor_; |
| 24 } | 23 } |
| 25 void SetGlobalOverrideCursor(const base::Optional<ui::CursorData>& cursor) { | 24 void SetGlobalOverrideCursor(const base::Optional<ui::CursorData>& cursor) { |
| 26 global_override_cursor_ = cursor; | 25 global_override_cursor_ = cursor; |
| 27 } | 26 } |
| 28 | 27 |
| 29 const ui::CursorData& cursor_data() const { return cursor_data_; } | 28 const ui::CursorData& cursor_data() const { return cursor_data_; } |
| 30 void SetCursorData(const ui::CursorData& data) { cursor_data_ = data; } | 29 void SetCursorData(const ui::CursorData& data) { cursor_data_ = data; } |
| 31 | 30 |
| 32 bool visible() const { return visible_; } | 31 bool visible() const { return visible_; } |
| 33 void set_visible(bool visible) { visible_ = visible; } | 32 void set_visible(bool visible) { visible_ = visible; } |
| 34 | 33 |
| 34 ui::CursorSize cursor_size() const { return cursor_size_; } |
| 35 void set_cursor_size(ui::CursorSize cursor_size) { |
| 36 cursor_size_ = cursor_size; |
| 37 } |
| 38 |
| 35 private: | 39 private: |
| 36 // An optional cursor set by the window manager which overrides per-window | 40 // An optional cursor set by the window manager which overrides per-window |
| 37 // requests. | 41 // requests. |
| 38 base::Optional<ui::CursorData> global_override_cursor_; | 42 base::Optional<ui::CursorData> global_override_cursor_; |
| 39 | 43 |
| 40 // The last cursor set. Used to track whether we need to change the cursor. | 44 // The last cursor set. Used to track whether we need to change the cursor. |
| 41 ui::CursorData cursor_data_; | 45 ui::CursorData cursor_data_ = ui::CursorData(ui::CursorType::kNull); |
| 46 |
| 47 // Which cursor set to use. |
| 48 ui::CursorSize cursor_size_ = CursorSize::kNormal; |
| 42 | 49 |
| 43 // Whether the cursor is visible. | 50 // Whether the cursor is visible. |
| 44 bool visible_; | 51 bool visible_ = true; |
| 45 }; | 52 }; |
| 46 | 53 |
| 47 CursorState::CursorState(DisplayManager* display_manager) | 54 CursorState::CursorState(DisplayManager* display_manager) |
| 48 : display_manager_(display_manager), | 55 : display_manager_(display_manager), |
| 49 current_state_(base::MakeUnique<StateSnapshot>()), | 56 current_state_(base::MakeUnique<StateSnapshot>()), |
| 50 state_on_unlock_(base::MakeUnique<StateSnapshot>()) {} | 57 state_on_unlock_(base::MakeUnique<StateSnapshot>()) {} |
| 51 | 58 |
| 52 CursorState::~CursorState() {} | 59 CursorState::~CursorState() {} |
| 53 | 60 |
| 54 void CursorState::SetCurrentWindowCursor(const ui::CursorData& cursor) { | 61 void CursorState::SetCurrentWindowCursor(const ui::CursorData& cursor) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 66 cursor_lock_count_++; | 73 cursor_lock_count_++; |
| 67 } | 74 } |
| 68 | 75 |
| 69 void CursorState::UnlockCursor() { | 76 void CursorState::UnlockCursor() { |
| 70 cursor_lock_count_--; | 77 cursor_lock_count_--; |
| 71 DCHECK_GE(cursor_lock_count_, 0); | 78 DCHECK_GE(cursor_lock_count_, 0); |
| 72 if (cursor_lock_count_ > 0) | 79 if (cursor_lock_count_ > 0) |
| 73 return; | 80 return; |
| 74 | 81 |
| 75 *current_state_ = *state_on_unlock_; | 82 *current_state_ = *state_on_unlock_; |
| 83 SetPlatformCursorSize(); |
| 76 SetPlatformCursor(); | 84 SetPlatformCursor(); |
| 77 } | 85 } |
| 78 | 86 |
| 79 void CursorState::SetCursorVisible(bool visible) { | 87 void CursorState::SetCursorVisible(bool visible) { |
| 80 state_on_unlock_->set_visible(visible); | 88 state_on_unlock_->set_visible(visible); |
| 81 if (cursor_lock_count_ == 0 && | 89 if (cursor_lock_count_ == 0 && |
| 82 current_state_->visible() != state_on_unlock_->visible()) { | 90 current_state_->visible() != state_on_unlock_->visible()) { |
| 83 current_state_->set_visible(visible); | 91 current_state_->set_visible(visible); |
| 84 SetPlatformCursor(); | 92 SetPlatformCursor(); |
| 85 } | 93 } |
| 86 } | 94 } |
| 87 | 95 |
| 88 void CursorState::SetGlobalOverrideCursor( | 96 void CursorState::SetGlobalOverrideCursor( |
| 89 const base::Optional<ui::CursorData>& cursor) { | 97 const base::Optional<ui::CursorData>& cursor) { |
| 90 state_on_unlock_->SetGlobalOverrideCursor(cursor); | 98 state_on_unlock_->SetGlobalOverrideCursor(cursor); |
| 91 if (cursor_lock_count_ == 0) { | 99 if (cursor_lock_count_ == 0) { |
| 92 current_state_->SetGlobalOverrideCursor(cursor); | 100 current_state_->SetGlobalOverrideCursor(cursor); |
| 93 SetPlatformCursor(); | 101 SetPlatformCursor(); |
| 94 } | 102 } |
| 95 } | 103 } |
| 96 | 104 |
| 105 void CursorState::SetCursorSize(ui::CursorSize cursor_size) { |
| 106 state_on_unlock_->set_cursor_size(cursor_size); |
| 107 if (cursor_lock_count_ == 0 && |
| 108 current_state_->cursor_size() != state_on_unlock_->cursor_size()) { |
| 109 current_state_->set_cursor_size(cursor_size); |
| 110 SetPlatformCursorSize(); |
| 111 SetPlatformCursor(); |
| 112 } |
| 113 } |
| 114 |
| 115 void CursorState::SetPlatformCursorSize() { |
| 116 DisplayManager* manager = display_manager_; |
| 117 for (Display* display : manager->displays()) |
| 118 display->SetNativeCursorSize(current_state_->cursor_size()); |
| 119 } |
| 120 |
| 97 void CursorState::SetPlatformCursor() { | 121 void CursorState::SetPlatformCursor() { |
| 98 DisplayManager* manager = display_manager_; | 122 DisplayManager* manager = display_manager_; |
| 99 auto set_on_all = [manager](const ui::CursorData& cursor) { | 123 auto set_on_all = [manager](const ui::CursorData& cursor) { |
| 100 for (Display* display : manager->displays()) | 124 for (Display* display : manager->displays()) |
| 101 display->SetNativeCursor(cursor); | 125 display->SetNativeCursor(cursor); |
| 102 }; | 126 }; |
| 103 | 127 |
| 104 if (current_state_->visible()) { | 128 if (current_state_->visible()) { |
| 105 if (current_state_->global_override_cursor().has_value()) | 129 if (current_state_->global_override_cursor().has_value()) |
| 106 set_on_all(current_state_->global_override_cursor().value()); | 130 set_on_all(current_state_->global_override_cursor().value()); |
| 107 else | 131 else |
| 108 set_on_all(current_state_->cursor_data()); | 132 set_on_all(current_state_->cursor_data()); |
| 109 } else { | 133 } else { |
| 110 set_on_all(ui::CursorData(ui::CursorType::kNone)); | 134 set_on_all(ui::CursorData(ui::CursorType::kNone)); |
| 111 } | 135 } |
| 112 } | 136 } |
| 113 | 137 |
| 114 } // namespace ws | 138 } // namespace ws |
| 115 } // namespace ui | 139 } // namespace ui |
| OLD | NEW |