forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative_cursor_manager_ash_mus.cc
209 lines (172 loc) · 6.91 KB
/
native_cursor_manager_ash_mus.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/wm/native_cursor_manager_ash_mus.h"
#include "ash/display/cursor_window_controller.h"
#include "ash/display/window_tree_host_manager.h"
#include "ash/shell.h"
#include "ui/aura/env.h"
#include "ui/aura/mus/window_manager_delegate.h"
#include "ui/aura/mus/window_port_mus.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/cursor/image_cursors.h"
#include "ui/base/layout.h"
#include "ui/wm/core/cursor_manager.h"
#if defined(USE_OZONE)
#include "ui/base/cursor/ozone/cursor_data_factory_ozone.h"
#endif
namespace ash {
namespace {
// We want to forward these things to the window tree client.
void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) {
ui::CursorData mojo_cursor;
if (cursor.platform()) {
#if defined(USE_OZONE)
mojo_cursor = ui::CursorDataFactoryOzone::GetCursorData(cursor.platform());
#else
NOTIMPLEMENTED()
<< "Can't pass native platform cursors on non-ozone platforms";
mojo_cursor = ui::CursorData(ui::CursorType::kPointer);
#endif
} else {
mojo_cursor = ui::CursorData(cursor.native_type());
}
// As the window manager, tell mus to use |mojo_cursor| everywhere. We do
// this instead of trying to set per-window because otherwise we run into the
// event targeting issue.
Shell::window_manager_client()->SetGlobalOverrideCursor(mojo_cursor);
// Make sure the local state is set properly, so that local queries show that
// we set the cursor.
for (aura::Window* root : Shell::Get()->GetAllRootWindows())
root->GetHost()->SetCursor(cursor);
Shell::Get()
->window_tree_host_manager()
->cursor_window_controller()
->SetCursor(cursor);
}
void NotifyCursorVisibilityChange(bool visible) {
// Communicate the cursor visibility state to the mus server.
Shell::window_manager_client()->SetCursorVisible(visible);
// Communicate the cursor visibility change to our local root window objects.
aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
for (aura::Window::Windows::iterator iter = root_windows.begin();
iter != root_windows.end(); ++iter)
(*iter)->GetHost()->OnCursorVisibilityChanged(visible);
Shell::Get()
->window_tree_host_manager()
->cursor_window_controller()
->SetVisibility(visible);
}
void NotifyMouseEventsEnableStateChange(bool enabled) {
aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
for (aura::Window::Windows::iterator iter = root_windows.begin();
iter != root_windows.end(); ++iter)
(*iter)->GetHost()->dispatcher()->OnMouseEventsEnableStateChanged(enabled);
// Mirror window never process events.
}
} // namespace
NativeCursorManagerAshMus::NativeCursorManagerAshMus() {
#if defined(USE_OZONE)
// If we're in a mus client, we aren't going to have all of ozone initialized
// even though we're in an ozone build. All the hard coded USE_OZONE ifdefs
// that handle cursor code expect that there will be a CursorFactoryOzone
// instance. Partially initialize the ozone cursor internals here, like we
// partially initialize other ozone subsystems in
// ChromeBrowserMainExtraPartsViews.
cursor_factory_ozone_ = base::MakeUnique<ui::CursorDataFactoryOzone>();
image_cursors_ = base::MakeUnique<ui::ImageCursors>();
#endif
}
NativeCursorManagerAshMus::~NativeCursorManagerAshMus() = default;
void NativeCursorManagerAshMus::SetNativeCursorEnabled(bool enabled) {
native_cursor_enabled_ = enabled;
::wm::CursorManager* cursor_manager = Shell::Get()->cursor_manager();
SetCursor(cursor_manager->GetCursor(), cursor_manager);
}
float NativeCursorManagerAshMus::GetScale() const {
return image_cursors_->GetScale();
}
display::Display::Rotation NativeCursorManagerAshMus::GetRotation() const {
return image_cursors_->GetRotation();
}
void NativeCursorManagerAshMus::SetDisplay(
const display::Display& display,
::wm::NativeCursorManagerDelegate* delegate) {
DCHECK(display.is_valid());
// Use the platform's device scale factor instead of the display's, which
// might have been adjusted for the UI scale.
const float original_scale = Shell::Get()
->display_manager()
->GetDisplayInfo(display.id())
.device_scale_factor();
// And use the nearest resource scale factor.
const float cursor_scale =
ui::GetScaleForScaleFactor(ui::GetSupportedScaleFactor(original_scale));
if (image_cursors_->SetDisplay(display, cursor_scale))
SetCursor(delegate->GetCursor(), delegate);
Shell::Get()
->window_tree_host_manager()
->cursor_window_controller()
->SetDisplay(display);
}
void NativeCursorManagerAshMus::SetCursor(
gfx::NativeCursor cursor,
::wm::NativeCursorManagerDelegate* delegate) {
if (image_cursors_) {
if (native_cursor_enabled_) {
image_cursors_->SetPlatformCursor(&cursor);
} else {
gfx::NativeCursor invisible_cursor(ui::CursorType::kNone);
image_cursors_->SetPlatformCursor(&invisible_cursor);
if (cursor == ui::CursorType::kCustom) {
// Fall back to the default pointer cursor for now. (crbug.com/476078)
// TODO(oshima): support custom cursor.
cursor = ui::CursorType::kPointer;
} else {
cursor.SetPlatformCursor(invisible_cursor.platform());
}
}
}
cursor.set_device_scale_factor(image_cursors_->GetScale());
delegate->CommitCursor(cursor);
if (delegate->IsCursorVisible())
SetCursorOnAllRootWindows(cursor);
}
void NativeCursorManagerAshMus::SetVisibility(
bool visible,
::wm::NativeCursorManagerDelegate* delegate) {
delegate->CommitVisibility(visible);
if (visible) {
SetCursor(delegate->GetCursor(), delegate);
} else {
gfx::NativeCursor invisible_cursor(ui::CursorType::kNone);
image_cursors_->SetPlatformCursor(&invisible_cursor);
SetCursorOnAllRootWindows(invisible_cursor);
}
NotifyCursorVisibilityChange(visible);
}
void NativeCursorManagerAshMus::SetCursorSize(
ui::CursorSize cursor_size,
::wm::NativeCursorManagerDelegate* delegate) {
delegate->CommitCursorSize(cursor_size);
Shell::window_manager_client()->SetCursorSize(cursor_size);
Shell::Get()
->window_tree_host_manager()
->cursor_window_controller()
->SetCursorSize(cursor_size);
}
void NativeCursorManagerAshMus::SetMouseEventsEnabled(
bool enabled,
::wm::NativeCursorManagerDelegate* delegate) {
delegate->CommitMouseEventsEnabled(enabled);
if (enabled) {
aura::Env::GetInstance()->set_last_mouse_location(
disabled_cursor_location_);
} else {
disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location();
}
SetVisibility(delegate->IsCursorVisible(), delegate);
NotifyMouseEventsEnableStateChange(enabled);
}
} // namespace ash