OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "services/ui/ws/cursor_state.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "services/ui/ws/display.h" |
| 9 #include "services/ui/ws/display_manager.h" |
| 10 #include "ui/base/cursor/cursor.h" |
| 11 |
| 12 namespace ui { |
| 13 namespace ws { |
| 14 |
| 15 class CursorState::StateSnapshot { |
| 16 public: |
| 17 StateSnapshot() |
| 18 : cursor_data_(ui::CursorData(ui::CursorType::kNull)), visible_(true) {} |
| 19 StateSnapshot(const StateSnapshot& rhs) = default; |
| 20 ~StateSnapshot() {} |
| 21 |
| 22 const base::Optional<ui::CursorData>& global_override_cursor() const { |
| 23 return global_override_cursor_; |
| 24 } |
| 25 void SetGlobalOverrideCursor(const base::Optional<ui::CursorData>& cursor) { |
| 26 global_override_cursor_ = cursor; |
| 27 } |
| 28 |
| 29 const ui::CursorData& cursor_data() const { return cursor_data_; } |
| 30 void SetCursorData(const ui::CursorData& data) { cursor_data_ = data; } |
| 31 |
| 32 bool visible() const { return visible_; } |
| 33 void set_visible(bool visible) { visible_ = visible; } |
| 34 |
| 35 private: |
| 36 // An optional cursor set by the window manager which overrides per-window |
| 37 // requests. |
| 38 base::Optional<ui::CursorData> global_override_cursor_; |
| 39 |
| 40 // The last cursor set. Used to track whether we need to change the cursor. |
| 41 ui::CursorData cursor_data_; |
| 42 |
| 43 // Whether the cursor is visible. |
| 44 bool visible_; |
| 45 }; |
| 46 |
| 47 CursorState::CursorState(DisplayManager* display_manager) |
| 48 : display_manager_(display_manager), |
| 49 current_state_(base::MakeUnique<StateSnapshot>()), |
| 50 state_on_unlock_(base::MakeUnique<StateSnapshot>()) {} |
| 51 |
| 52 CursorState::~CursorState() {} |
| 53 |
| 54 void CursorState::SetCurrentWindowCursor(const ui::CursorData& cursor) { |
| 55 if (!state_on_unlock_->cursor_data().IsSameAs(cursor)) |
| 56 state_on_unlock_->SetCursorData(cursor); |
| 57 |
| 58 if (cursor_lock_count_ == 0 && |
| 59 !current_state_->cursor_data().IsSameAs(cursor)) { |
| 60 current_state_->SetCursorData(cursor); |
| 61 SetPlatformCursor(); |
| 62 } |
| 63 } |
| 64 |
| 65 void CursorState::LockCursor() { |
| 66 cursor_lock_count_++; |
| 67 } |
| 68 |
| 69 void CursorState::UnlockCursor() { |
| 70 cursor_lock_count_--; |
| 71 DCHECK_GE(cursor_lock_count_, 0); |
| 72 if (cursor_lock_count_ > 0) |
| 73 return; |
| 74 |
| 75 *current_state_ = *state_on_unlock_; |
| 76 SetPlatformCursor(); |
| 77 } |
| 78 |
| 79 void CursorState::SetCursorVisible(bool visible) { |
| 80 state_on_unlock_->set_visible(visible); |
| 81 if (cursor_lock_count_ == 0 && |
| 82 current_state_->visible() != state_on_unlock_->visible()) { |
| 83 current_state_->set_visible(visible); |
| 84 SetPlatformCursor(); |
| 85 } |
| 86 } |
| 87 |
| 88 void CursorState::SetGlobalOverrideCursor( |
| 89 const base::Optional<ui::CursorData>& cursor) { |
| 90 state_on_unlock_->SetGlobalOverrideCursor(cursor); |
| 91 if (cursor_lock_count_ == 0) { |
| 92 current_state_->SetGlobalOverrideCursor(cursor); |
| 93 SetPlatformCursor(); |
| 94 } |
| 95 } |
| 96 |
| 97 void CursorState::SetPlatformCursor() { |
| 98 DisplayManager* manager = display_manager_; |
| 99 auto set_on_all = [manager](const ui::CursorData& cursor) { |
| 100 for (Display* display : manager->displays()) |
| 101 display->SetNativeCursor(cursor); |
| 102 }; |
| 103 |
| 104 if (current_state_->visible()) { |
| 105 if (current_state_->global_override_cursor().has_value()) |
| 106 set_on_all(current_state_->global_override_cursor().value()); |
| 107 else |
| 108 set_on_all(current_state_->cursor_data()); |
| 109 } else { |
| 110 set_on_all(ui::CursorData(ui::CursorType::kNone)); |
| 111 } |
| 112 } |
| 113 |
| 114 } // namespace ws |
| 115 } // namespace ui |
OLD | NEW |