forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem.cpp
393 lines (335 loc) · 9.83 KB
/
system.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Aseprite UI Library
// Copyright (C) 2018-2023 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ui/system.h"
#include "gfx/point.h"
#include "os/event.h"
#include "os/event_queue.h"
#include "os/surface.h"
#include "os/system.h"
#include "os/window.h"
#include "ui/clipboard_delegate.h"
#include "ui/cursor.h"
#include "ui/intern.h"
#include "ui/manager.h"
#include "ui/message.h"
#include "ui/overlay.h"
#include "ui/overlay_manager.h"
#include "ui/scale.h"
#include "ui/theme.h"
#include "ui/widget.h"
#include <thread>
namespace ui {
// This is used to check if calls to UI layer are made from the non-UI
// thread. (Which might be catastrofic.)
std::thread::id main_gui_thread;
// Multiple displays (create one os::Window for each ui::Window)
bool multi_displays = false;
// Current mouse cursor type.
static CursorType mouse_cursor_type = kOutsideDisplay;
static const Cursor* mouse_cursor_custom = nullptr;
static const Cursor* mouse_cursor = nullptr;
static Display* mouse_display = nullptr;
static OverlayRef mouse_cursor_overlay = nullptr;
static bool use_native_mouse_cursor = true;
static bool support_native_custom_cursor = false;
// Mouse information
static int mouse_cursor_scale = 1;
static int mouse_scares = 0;
static void update_mouse_overlay(const Cursor* cursor)
{
mouse_cursor = cursor;
if (mouse_cursor && mouse_scares == 0) {
if (!mouse_cursor_overlay) {
ASSERT(mouse_display);
mouse_cursor_overlay = base::make_ref<Overlay>(
mouse_display,
mouse_cursor->surface(),
mouse_display->nativeWindow()->pointFromScreen(get_mouse_position()),
Overlay::MouseZOrder);
OverlayManager::instance()->addOverlay(mouse_cursor_overlay);
}
else {
mouse_cursor_overlay->setSurface(mouse_cursor->surface());
update_cursor_overlay();
}
}
else if (mouse_cursor_overlay) {
OverlayManager::instance()->removeOverlay(mouse_cursor_overlay);
mouse_cursor_overlay->setSurface(nullptr);
mouse_cursor_overlay.reset();
}
}
static bool set_native_cursor_on_all_displays(Display* display,
const Cursor* cursor)
{
bool result = false;
while (display) {
os::Window* nativeWindow = display->nativeWindow();
if (cursor && cursor->surface()) {
// The cursor surface is already scaled by guiscale(), we scale
// the cursor by the os::Window scale and mouse scale.
const int scale = nativeWindow->scale() * mouse_cursor_scale;
if (auto osCursor = cursor->nativeCursor(scale))
result |= nativeWindow->setCursor(osCursor);
}
else if (mouse_cursor_type == kOutsideDisplay) {
result |= nativeWindow->setCursor(os::NativeCursor::Arrow);
}
else {
result |= nativeWindow->setCursor(os::NativeCursor::Hidden);
}
display = display->parentDisplay();
}
return result;
}
static bool set_native_cursor_on_all_displays(Display* display,
const os::NativeCursor cursor)
{
bool result = false;
while (display) {
os::Window* nativeWindow = display->nativeWindow();
if (mouse_cursor_type == kOutsideDisplay) {
result |= nativeWindow->setCursor(os::NativeCursor::Arrow);
}
else {
result |= nativeWindow->setCursor(cursor);
}
display = display->parentDisplay();
}
return result;
}
static bool update_custom_native_cursor(const Cursor* cursor)
{
bool result = false;
// Check if we can use a custom native mouse in this platform
if (support_native_custom_cursor &&
mouse_display) {
result = set_native_cursor_on_all_displays(mouse_display, cursor);
}
return result;
}
static void update_mouse_cursor()
{
os::NativeCursor nativeCursor = os::NativeCursor::Hidden;
const Cursor* cursor = nullptr;
if (use_native_mouse_cursor ||
mouse_cursor_type == kOutsideDisplay) {
switch (mouse_cursor_type) {
case ui::kOutsideDisplay:
nativeCursor = os::NativeCursor::Arrow;
break;
case ui::kNoCursor: break;
case ui::kArrowCursor:
case ui::kArrowPlusCursor:
nativeCursor = os::NativeCursor::Arrow;
break;
case ui::kCrosshairCursor:
nativeCursor = os::NativeCursor::Crosshair;
break;
case ui::kForbiddenCursor:
nativeCursor = os::NativeCursor::Forbidden;
break;
case ui::kHandCursor:
nativeCursor = os::NativeCursor::Link;
break;
case ui::kScrollCursor:
case ui::kMoveCursor:
nativeCursor = os::NativeCursor::Move;
break;
case ui::kSizeNSCursor: nativeCursor = os::NativeCursor::SizeNS; break;
case ui::kSizeWECursor: nativeCursor = os::NativeCursor::SizeWE; break;
case ui::kSizeNCursor: nativeCursor = os::NativeCursor::SizeN; break;
case ui::kSizeNECursor: nativeCursor = os::NativeCursor::SizeNE; break;
case ui::kSizeECursor: nativeCursor = os::NativeCursor::SizeE; break;
case ui::kSizeSECursor: nativeCursor = os::NativeCursor::SizeSE; break;
case ui::kSizeSCursor: nativeCursor = os::NativeCursor::SizeS; break;
case ui::kSizeSWCursor: nativeCursor = os::NativeCursor::SizeSW; break;
case ui::kSizeWCursor: nativeCursor = os::NativeCursor::SizeW; break;
case ui::kSizeNWCursor: nativeCursor = os::NativeCursor::SizeNW; break;
}
}
// Set native cursor
if (mouse_display) {
bool ok = set_native_cursor_on_all_displays(mouse_display, nativeCursor);
// It looks like the specific native cursor is not supported,
// so we can should use the internal overlay (even when we
// have use_native_mouse_cursor flag enabled).
if (!ok)
nativeCursor = os::NativeCursor::Hidden;
}
// Use a custom cursor
if (nativeCursor == os::NativeCursor::Hidden &&
mouse_cursor_type != ui::kOutsideDisplay) {
if (get_theme() && mouse_cursor_type != ui::kCustomCursor)
cursor = get_theme()->getStandardCursor(mouse_cursor_type);
else
cursor = mouse_cursor_custom;
}
// Try to use a custom native cursor if it's possible
if (mouse_display &&
nativeCursor == os::NativeCursor::Hidden &&
!update_custom_native_cursor(cursor)) {
// Or an overlay as last resource
update_mouse_overlay(cursor);
}
}
static UISystem* g_instance = nullptr;
// static
UISystem* UISystem::instance()
{
return g_instance;
}
UISystem::UISystem()
: m_clipboardDelegate(nullptr)
{
ASSERT(!g_instance);
g_instance = this;
main_gui_thread = std::this_thread::get_id();
mouse_cursor_type = kOutsideDisplay;
support_native_custom_cursor =
((os::instance() &&
(int(os::instance()->capabilities()) &
int(os::Capabilities::CustomMouseCursor))) ?
true: false);
details::initWidgets();
}
UISystem::~UISystem()
{
// finish theme
set_theme(nullptr, guiscale());
details::exitWidgets();
_internal_set_mouse_display(nullptr);
if (!update_custom_native_cursor(nullptr))
update_mouse_overlay(nullptr);
OverlayManager::destroyInstance();
ASSERT(g_instance == this);
g_instance = nullptr;
}
void _internal_set_mouse_display(Display* display)
{
if (display != mouse_display) {
mouse_display = display;
if (mouse_display)
update_mouse_cursor();
}
}
void set_multiple_displays(bool multi)
{
multi_displays = multi;
}
bool get_multiple_displays()
{
return multi_displays;
}
void set_clipboard_text(const std::string& text)
{
ASSERT(g_instance);
ClipboardDelegate* delegate = g_instance->clipboardDelegate();
if (delegate)
delegate->setClipboardText(text);
}
bool get_clipboard_text(std::string& text)
{
ASSERT(g_instance);
ClipboardDelegate* delegate = g_instance->clipboardDelegate();
if (delegate)
return delegate->getClipboardText(text);
else
return false;
}
void update_cursor_overlay()
{
if (mouse_cursor_overlay != nullptr && mouse_scares == 0) {
gfx::Point newPos =
mouse_display->nativeWindow()->pointFromScreen(get_mouse_position())
- mouse_cursor->focus();
if (newPos != mouse_cursor_overlay->position()) {
mouse_cursor_overlay->moveOverlay(newPos);
}
}
}
void set_use_native_cursors(bool state)
{
use_native_mouse_cursor = state;
update_mouse_cursor();
}
CursorType get_mouse_cursor()
{
return mouse_cursor_type;
}
void set_mouse_cursor(CursorType type, const Cursor* cursor)
{
if (mouse_cursor_type == type &&
mouse_cursor_custom == cursor)
return;
mouse_cursor_type = type;
mouse_cursor_custom = cursor;
update_mouse_cursor();
}
void set_mouse_cursor_scale(const int newScale)
{
mouse_cursor_scale = newScale;
update_mouse_cursor();
}
void set_mouse_cursor_reset_info()
{
mouse_cursor_type = kCustomCursor;
mouse_cursor_custom = nullptr;
}
void hide_mouse_cursor()
{
ASSERT(mouse_scares >= 0);
mouse_scares++;
}
void show_mouse_cursor()
{
ASSERT(mouse_scares > 0);
mouse_scares--;
if (mouse_scares == 0)
update_mouse_cursor();
}
void _internal_no_mouse_position()
{
if (!update_custom_native_cursor(nullptr))
update_mouse_overlay(nullptr);
}
gfx::Point get_mouse_position()
{
return os::instance()->mousePosition();
}
void set_mouse_position(const gfx::Point& newPos,
Display* display)
{
if (display && display != mouse_display)
_internal_set_mouse_display(display);
if (display)
display->nativeWindow()->setMousePosition(newPos);
else
os::instance()->setMousePosition(newPos);
}
void execute_from_ui_thread(std::function<void()>&& func)
{
// Queue the event
os::Event ev;
ev.setType(os::Event::Callback);
ev.setCallback(std::move(func));
os::queue_event(ev);
}
bool is_ui_thread()
{
return (main_gui_thread == std::this_thread::get_id());
}
#ifdef _DEBUG
void assert_ui_thread()
{
ASSERT(is_ui_thread());
}
#endif
} // namespace ui