| 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::CursorSet cursor_set() const { return cursor_set_; } |
| 35 void set_cursor_set(ui::CursorSet cursor_set) { cursor_set_ = cursor_set; } |
| 36 |
| 35 private: | 37 private: |
| 36 // An optional cursor set by the window manager which overrides per-window | 38 // An optional cursor set by the window manager which overrides per-window |
| 37 // requests. | 39 // requests. |
| 38 base::Optional<ui::CursorData> global_override_cursor_; | 40 base::Optional<ui::CursorData> global_override_cursor_; |
| 39 | 41 |
| 40 // The last cursor set. Used to track whether we need to change the cursor. | 42 // The last cursor set. Used to track whether we need to change the cursor. |
| 41 ui::CursorData cursor_data_; | 43 ui::CursorData cursor_data_ = ui::CursorData(ui::CursorType::kNull); |
| 44 |
| 45 // Which cursor set to use. |
| 46 ui::CursorSet cursor_set_ = CursorSet::kNormal; |
| 42 | 47 |
| 43 // Whether the cursor is visible. | 48 // Whether the cursor is visible. |
| 44 bool visible_; | 49 bool visible_ = true; |
| 45 }; | 50 }; |
| 46 | 51 |
| 47 CursorState::CursorState(DisplayManager* display_manager) | 52 CursorState::CursorState(DisplayManager* display_manager) |
| 48 : display_manager_(display_manager), | 53 : display_manager_(display_manager), |
| 49 current_state_(base::MakeUnique<StateSnapshot>()), | 54 current_state_(base::MakeUnique<StateSnapshot>()), |
| 50 state_on_unlock_(base::MakeUnique<StateSnapshot>()) {} | 55 state_on_unlock_(base::MakeUnique<StateSnapshot>()) {} |
| 51 | 56 |
| 52 CursorState::~CursorState() {} | 57 CursorState::~CursorState() {} |
| 53 | 58 |
| 54 void CursorState::SetCurrentWindowCursor(const ui::CursorData& cursor) { | 59 void CursorState::SetCurrentWindowCursor(const ui::CursorData& cursor) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 66 cursor_lock_count_++; | 71 cursor_lock_count_++; |
| 67 } | 72 } |
| 68 | 73 |
| 69 void CursorState::UnlockCursor() { | 74 void CursorState::UnlockCursor() { |
| 70 cursor_lock_count_--; | 75 cursor_lock_count_--; |
| 71 DCHECK_GE(cursor_lock_count_, 0); | 76 DCHECK_GE(cursor_lock_count_, 0); |
| 72 if (cursor_lock_count_ > 0) | 77 if (cursor_lock_count_ > 0) |
| 73 return; | 78 return; |
| 74 | 79 |
| 75 *current_state_ = *state_on_unlock_; | 80 *current_state_ = *state_on_unlock_; |
| 81 SetPlatformCursorSet(); |
| 76 SetPlatformCursor(); | 82 SetPlatformCursor(); |
| 77 } | 83 } |
| 78 | 84 |
| 79 void CursorState::SetCursorVisible(bool visible) { | 85 void CursorState::SetCursorVisible(bool visible) { |
| 80 state_on_unlock_->set_visible(visible); | 86 state_on_unlock_->set_visible(visible); |
| 81 if (cursor_lock_count_ == 0 && | 87 if (cursor_lock_count_ == 0 && |
| 82 current_state_->visible() != state_on_unlock_->visible()) { | 88 current_state_->visible() != state_on_unlock_->visible()) { |
| 83 current_state_->set_visible(visible); | 89 current_state_->set_visible(visible); |
| 84 SetPlatformCursor(); | 90 SetPlatformCursor(); |
| 85 } | 91 } |
| 86 } | 92 } |
| 87 | 93 |
| 88 void CursorState::SetGlobalOverrideCursor( | 94 void CursorState::SetGlobalOverrideCursor( |
| 89 const base::Optional<ui::CursorData>& cursor) { | 95 const base::Optional<ui::CursorData>& cursor) { |
| 90 state_on_unlock_->SetGlobalOverrideCursor(cursor); | 96 state_on_unlock_->SetGlobalOverrideCursor(cursor); |
| 91 if (cursor_lock_count_ == 0) { | 97 if (cursor_lock_count_ == 0) { |
| 92 current_state_->SetGlobalOverrideCursor(cursor); | 98 current_state_->SetGlobalOverrideCursor(cursor); |
| 93 SetPlatformCursor(); | 99 SetPlatformCursor(); |
| 94 } | 100 } |
| 95 } | 101 } |
| 96 | 102 |
| 103 void CursorState::SetCursorSet(ui::CursorSet cursor_set) { |
| 104 state_on_unlock_->set_cursor_set(cursor_set); |
| 105 if (cursor_lock_count_ == 0 && |
| 106 current_state_->cursor_set() != state_on_unlock_->cursor_set()) { |
| 107 current_state_->set_cursor_set(cursor_set); |
| 108 SetPlatformCursorSet(); |
| 109 SetPlatformCursor(); |
| 110 } |
| 111 } |
| 112 |
| 113 void CursorState::SetPlatformCursorSet() { |
| 114 DisplayManager* manager = display_manager_; |
| 115 for (Display* display : manager->displays()) |
| 116 display->SetNativeCursorSet(current_state_->cursor_set()); |
| 117 } |
| 118 |
| 97 void CursorState::SetPlatformCursor() { | 119 void CursorState::SetPlatformCursor() { |
| 98 DisplayManager* manager = display_manager_; | 120 DisplayManager* manager = display_manager_; |
| 99 auto set_on_all = [manager](const ui::CursorData& cursor) { | 121 auto set_on_all = [manager](const ui::CursorData& cursor) { |
| 100 for (Display* display : manager->displays()) | 122 for (Display* display : manager->displays()) |
| 101 display->SetNativeCursor(cursor); | 123 display->SetNativeCursor(cursor); |
| 102 }; | 124 }; |
| 103 | 125 |
| 104 if (current_state_->visible()) { | 126 if (current_state_->visible()) { |
| 105 if (current_state_->global_override_cursor().has_value()) | 127 if (current_state_->global_override_cursor().has_value()) |
| 106 set_on_all(current_state_->global_override_cursor().value()); | 128 set_on_all(current_state_->global_override_cursor().value()); |
| 107 else | 129 else |
| 108 set_on_all(current_state_->cursor_data()); | 130 set_on_all(current_state_->cursor_data()); |
| 109 } else { | 131 } else { |
| 110 set_on_all(ui::CursorData(ui::CursorType::kNone)); | 132 set_on_all(ui::CursorData(ui::CursorType::kNone)); |
| 111 } | 133 } |
| 112 } | 134 } |
| 113 | 135 |
| 114 } // namespace ws | 136 } // namespace ws |
| 115 } // namespace ui | 137 } // namespace ui |
| OLD | NEW |