@windowkit/appkit 0.9.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +236 -4
- package/binding.gyp +2 -1
- package/package.json +2 -2
- package/prebuilds/darwin-arm64/calayers.node +0 -0
- package/prebuilds/darwin-x64/calayers.node +0 -0
- package/src/addon.mm +570 -224
- package/src/backend.mm +2394 -1110
- package/src/calendars.mm +69 -46
- package/src/channel.h +292 -0
- package/src/notifications.mm +99 -72
- package/src/permissions.mm +30 -21
- package/src/screencolor.mm +35 -25
- package/src/threaded.mm +1072 -0
package/src/backend.mm
CHANGED
|
@@ -23,11 +23,18 @@
|
|
|
23
23
|
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
|
|
24
24
|
#include <objc/runtime.h>
|
|
25
25
|
#include <dlfcn.h>
|
|
26
|
+
#include <pthread.h>
|
|
26
27
|
|
|
28
|
+
#include <atomic>
|
|
27
29
|
#include <cmath>
|
|
30
|
+
#include <memory>
|
|
31
|
+
#include <mutex>
|
|
28
32
|
#include <string>
|
|
33
|
+
#include <unordered_map>
|
|
29
34
|
#include <vector>
|
|
30
35
|
|
|
36
|
+
#include "channel.h"
|
|
37
|
+
|
|
31
38
|
// --- helpers (self-contained: addon.mm keeps its own copies) ---------------
|
|
32
39
|
|
|
33
40
|
static NSString* BToNSString(Napi::Value v) {
|
|
@@ -91,9 +98,42 @@ static NSMenu* gDockMenu = nil; // applicationDockMenu: answer; setDockMenu
|
|
|
91
98
|
|
|
92
99
|
static void BInstallAppDelegate(); // the app lifecycle section, below
|
|
93
100
|
static void BInstallAccessibilityObserver(); // the accessibility section, below
|
|
101
|
+
static void BPublishAppState(); // the published state section, below
|
|
102
|
+
static bool PolicyFromName(const std::string& s,
|
|
103
|
+
NSApplicationActivationPolicy* out); // app presence
|
|
104
|
+
static void PublishPolicy(NSApplicationActivationPolicy p); // published state
|
|
105
|
+
|
|
106
|
+
// Said before launch by initApp({ activationPolicy }), setActivationPolicy
|
|
107
|
+
// or runMain({ activationPolicy }): then the environment is not consulted.
|
|
108
|
+
static bool gPolicyChosen = false;
|
|
109
|
+
|
|
110
|
+
// APPKIT_ACTIVATION_POLICY=regular|accessory|prohibited — the policy to
|
|
111
|
+
// launch with when the code that would say so has not run yet
|
|
112
|
+
// (windowkit/appkit#64): a launcher's initApp() or runMain() launches the
|
|
113
|
+
// app before a worker has imported the app's entry, and a policy set after
|
|
114
|
+
// finishLaunching is too late for an agent app, whose Dock tile has
|
|
115
|
+
// already appeared. A bundled app says the same with LSUIElement. A name
|
|
116
|
+
// nobody knows is reported and the default kept.
|
|
117
|
+
static void PolicyFromEnvironment() {
|
|
118
|
+
const char* v = getenv("APPKIT_ACTIVATION_POLICY");
|
|
119
|
+
if (!v || !*v) return;
|
|
120
|
+
NSApplicationActivationPolicy p;
|
|
121
|
+
if (PolicyFromName(v, &p)) {
|
|
122
|
+
gActivationPolicy = p;
|
|
123
|
+
} else {
|
|
124
|
+
fprintf(stderr,
|
|
125
|
+
"@windowkit/appkit: APPKIT_ACTIVATION_POLICY=%s is not regular, "
|
|
126
|
+
"accessory or prohibited; launching as regular\n",
|
|
127
|
+
v);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
94
130
|
|
|
95
131
|
void BEnsureApp() {
|
|
96
132
|
if (gAppLaunched) return;
|
|
133
|
+
if (!gPolicyChosen) PolicyFromEnvironment();
|
|
134
|
+
// published as it is decided, not after finishLaunching: a launch can
|
|
135
|
+
// take longer than a worker takes to start and read it
|
|
136
|
+
PublishPolicy(gActivationPolicy);
|
|
97
137
|
@autoreleasepool {
|
|
98
138
|
[NSApplication sharedApplication];
|
|
99
139
|
[NSApp setActivationPolicy:gActivationPolicy];
|
|
@@ -110,6 +150,7 @@ void BEnsureApp() {
|
|
|
110
150
|
[NSApp finishLaunching];
|
|
111
151
|
}
|
|
112
152
|
gAppLaunched = true;
|
|
153
|
+
BPublishAppState();
|
|
113
154
|
}
|
|
114
155
|
|
|
115
156
|
// The top of the primary screen, for global coordinate flips. The primary
|
|
@@ -142,15 +183,303 @@ static Napi::FunctionReference gBackendCb;
|
|
|
142
183
|
// The guard only protects against dispatching with no callback installed.
|
|
143
184
|
static bool HasBackendCb() { return !gBackendCb.IsEmpty(); }
|
|
144
185
|
|
|
145
|
-
|
|
186
|
+
// Pump mode's delivery, which threaded.mm's CALEmit calls while the channel
|
|
187
|
+
// is closed: the record materialized in the callback's own environment and
|
|
188
|
+
// handed over inline, as every event always was. Producers everywhere build
|
|
189
|
+
// a CALEvent and call CALEmit (channel.h); nothing calls the callback
|
|
190
|
+
// directly any more. The call is raw, so a failed call is a status rather
|
|
191
|
+
// than node-addon-api's fatal error; an exception the callback throws is
|
|
192
|
+
// left pending. Inside pump2 (a JS call frame) it rethrows to pump2's
|
|
193
|
+
// caller, as it always has; the notification and calendar-change hops,
|
|
194
|
+
// which reach here from a threadsafe function's callback, make it the
|
|
195
|
+
// uncaught exception themselves (CALRaiseUncaughtIfPending).
|
|
196
|
+
void CALPumpDeliver(const CALEvent& ev) {
|
|
146
197
|
if (!HasBackendCb()) return;
|
|
147
|
-
gBackendCb.
|
|
198
|
+
Napi::Env env = gBackendCb.Env();
|
|
199
|
+
Napi::HandleScope scope(env);
|
|
200
|
+
napi_value undefined, arg = ev.ToObject(env);
|
|
201
|
+
if (napi_get_undefined(env, &undefined) != napi_ok) return;
|
|
202
|
+
napi_call_function(env, undefined, gBackendCb.Value(), 1, &arg, nullptr);
|
|
148
203
|
}
|
|
149
204
|
|
|
150
|
-
// The same callback, for the faces in other files: notifications.mm hands
|
|
151
|
-
// the UNUserNotificationCenter delegate's responses through here.
|
|
152
205
|
bool CALHasBackendCb() { return HasBackendCb(); }
|
|
153
|
-
|
|
206
|
+
|
|
207
|
+
// ---------------------------------------------------------------------------
|
|
208
|
+
// published state: what JS on a worker reads without a hop
|
|
209
|
+
// ---------------------------------------------------------------------------
|
|
210
|
+
//
|
|
211
|
+
// In threaded mode (threaded.mm) the renderer's JS never waits on the UI
|
|
212
|
+
// thread, so what it reads back synchronously is a copy the UI thread keeps
|
|
213
|
+
// as things change, under one lock: each window's content rect, visibility,
|
|
214
|
+
// occlusion, key state and scale; the screen list; the accessibility display
|
|
215
|
+
// options; the activation policy; the pasteboard's change count (polled, as
|
|
216
|
+
// nothing announces another app's write). listScreens,
|
|
217
|
+
// accessibilityDisplayOptions and pasteboardChangeCount answer from it when
|
|
218
|
+
// called off the main thread while runMain runs, and windowState and
|
|
219
|
+
// activationPolicy read it in either mode. The copies are kept in pump mode
|
|
220
|
+
// too — a few doubles per change — but every query pump mode already makes
|
|
221
|
+
// still asks AppKit, live, on the main thread.
|
|
222
|
+
|
|
223
|
+
struct PubWindow {
|
|
224
|
+
double x = 0, y = 0, width = 0, height = 0, scale = 1;
|
|
225
|
+
bool visible = false, occluded = false, key = false, ignoresMouseEvents = false;
|
|
226
|
+
bool liveResize = false; // inside a live resize (windowkit/appkit#63)
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
struct PubScreen {
|
|
230
|
+
double x, y, width, height; // the whole screen
|
|
231
|
+
double vx, vy, vwidth, vheight; // less the menu bar and the Dock
|
|
232
|
+
double scale, fps;
|
|
233
|
+
bool primary;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
struct A11yOptions {
|
|
237
|
+
bool reduceMotion, reduceTransparency, increaseContrast,
|
|
238
|
+
differentiateWithoutColor, invertColors;
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
// leaked, like threaded.mm's queues: a worker may still read while the
|
|
242
|
+
// process exits, after static destructors have begun
|
|
243
|
+
static std::mutex& gPubMu = *new std::mutex;
|
|
244
|
+
static std::unordered_map<long, PubWindow>& gPubWindows =
|
|
245
|
+
*new std::unordered_map<long, PubWindow>; // by windowNumber
|
|
246
|
+
static std::vector<PubScreen>& gPubScreens = *new std::vector<PubScreen>;
|
|
247
|
+
static A11yOptions gPubA11y = {};
|
|
248
|
+
static std::atomic<int> gPubPolicy{(int)NSApplicationActivationPolicyRegular};
|
|
249
|
+
static std::atomic<long> gPubPasteboardCount{0};
|
|
250
|
+
|
|
251
|
+
static void PublishPolicy(NSApplicationActivationPolicy p) { gPubPolicy = (int)p; }
|
|
252
|
+
|
|
253
|
+
// A call off the main thread — a worker's, threaded mode's — answers from
|
|
254
|
+
// the copy: AppKit's state is not that thread's to read.
|
|
255
|
+
static bool ReadPublished() { return !pthread_main_np(); }
|
|
256
|
+
|
|
257
|
+
// appInfo's part of the copy: the name LaunchServices has for the process,
|
|
258
|
+
// the Dock badge, whether the app is active.
|
|
259
|
+
static NSString* gPubName = nil; // gPubMu
|
|
260
|
+
static NSString* gPubBadge = nil; // gPubMu
|
|
261
|
+
static std::atomic<bool> gPubActive{false};
|
|
262
|
+
|
|
263
|
+
static void PublishName() {
|
|
264
|
+
NSString* name = NSRunningApplication.currentApplication.localizedName;
|
|
265
|
+
std::lock_guard<std::mutex> l(gPubMu);
|
|
266
|
+
gPubName = [name copy];
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
static void PublishBadge() {
|
|
270
|
+
NSString* badge = NSApp.dockTile.badgeLabel;
|
|
271
|
+
std::lock_guard<std::mutex> l(gPubMu);
|
|
272
|
+
gPubBadge = [badge copy];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
static bool WindowOnGlass(NSWindow* win) {
|
|
276
|
+
return (win.occlusionState & NSWindowOcclusionStateVisible) != 0;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// The content rect, top-left global coordinates, points.
|
|
280
|
+
static PubWindow WindowStateOf(NSWindow* win) {
|
|
281
|
+
NSRect content = ContentViewScreenRect(win);
|
|
282
|
+
PubWindow s;
|
|
283
|
+
s.x = content.origin.x;
|
|
284
|
+
s.y = PrimaryScreenTop() - (content.origin.y + content.size.height);
|
|
285
|
+
s.width = content.size.width;
|
|
286
|
+
s.height = content.size.height;
|
|
287
|
+
s.scale = win.backingScaleFactor;
|
|
288
|
+
s.visible = win.isVisible;
|
|
289
|
+
// visible but with no pixel on glass: fully behind another app's window
|
|
290
|
+
s.occluded = win.isVisible && !WindowOnGlass(win);
|
|
291
|
+
s.key = win.isKeyWindow;
|
|
292
|
+
s.ignoresMouseEvents = win.ignoresMouseEvents;
|
|
293
|
+
s.liveResize = win.inLiveResize;
|
|
294
|
+
return s;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// getWindowFrame's shape.
|
|
298
|
+
static Napi::Object WindowStateObject(Napi::Env env, const PubWindow& s) {
|
|
299
|
+
Napi::Object r = Napi::Object::New(env);
|
|
300
|
+
r.Set("x", s.x);
|
|
301
|
+
r.Set("y", s.y);
|
|
302
|
+
r.Set("width", s.width);
|
|
303
|
+
r.Set("height", s.height);
|
|
304
|
+
r.Set("scale", s.scale);
|
|
305
|
+
r.Set("visible", s.visible);
|
|
306
|
+
r.Set("occluded", s.occluded);
|
|
307
|
+
r.Set("key", s.key);
|
|
308
|
+
r.Set("ignoresMouseEvents", s.ignoresMouseEvents);
|
|
309
|
+
r.Set("liveResize", s.liveResize);
|
|
310
|
+
return r;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
static void PublishWindow(NSWindow* win) {
|
|
314
|
+
PubWindow s = WindowStateOf(win);
|
|
315
|
+
long number = (long)win.windowNumber;
|
|
316
|
+
std::lock_guard<std::mutex> l(gPubMu);
|
|
317
|
+
gPubWindows[number] = s;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// At a live resize's two ends, where the flag is said rather than read back:
|
|
321
|
+
// the notifications bracket the resize, and inLiveResize at exactly those
|
|
322
|
+
// instants is AppKit's to decide.
|
|
323
|
+
static void PublishWindowLiveResize(NSWindow* win, bool live) {
|
|
324
|
+
PubWindow s = WindowStateOf(win);
|
|
325
|
+
s.liveResize = live;
|
|
326
|
+
long number = (long)win.windowNumber;
|
|
327
|
+
std::lock_guard<std::mutex> l(gPubMu);
|
|
328
|
+
gPubWindows[number] = s;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
static void ForgetWindow(long number) {
|
|
332
|
+
std::lock_guard<std::mutex> l(gPubMu);
|
|
333
|
+
gPubWindows.erase(number);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
static bool PublishedWindow(long number, PubWindow* out) {
|
|
337
|
+
if (!number) return false;
|
|
338
|
+
std::lock_guard<std::mutex> l(gPubMu);
|
|
339
|
+
auto it = gPubWindows.find(number);
|
|
340
|
+
if (it == gPubWindows.end()) return false;
|
|
341
|
+
*out = it->second;
|
|
342
|
+
return true;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// For addon.mm's windowIsVisible on a worker's window.
|
|
346
|
+
bool CALWindowVisible(long number, bool* visible) {
|
|
347
|
+
PubWindow s;
|
|
348
|
+
if (!PublishedWindow(number, &s)) return false;
|
|
349
|
+
*visible = s.visible;
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// A worker's window handle, or nil for anything else (pump mode's window
|
|
354
|
+
// itself included).
|
|
355
|
+
static CALHandle* WindowHandleOf(id target) {
|
|
356
|
+
return [target isKindOfClass:[CALHandle class]] ? (CALHandle*)target : nil;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
static std::vector<PubScreen> ScreensNow() {
|
|
360
|
+
CGFloat top = PrimaryScreenTop();
|
|
361
|
+
std::vector<PubScreen> out;
|
|
362
|
+
NSArray<NSScreen*>* screens = NSScreen.screens;
|
|
363
|
+
for (NSUInteger i = 0; i < screens.count; i++) {
|
|
364
|
+
NSScreen* s = screens[i];
|
|
365
|
+
NSRect f = s.frame, v = s.visibleFrame;
|
|
366
|
+
// the panel's own refresh rate, so a renderer paces frames on the
|
|
367
|
+
// display's period instead of assuming 60Hz on a 120Hz ProMotion
|
|
368
|
+
// panel; 0 where the OS cannot say (before macOS 12)
|
|
369
|
+
double fps = 0;
|
|
370
|
+
if (@available(macOS 12.0, *)) fps = (double)s.maximumFramesPerSecond;
|
|
371
|
+
out.push_back({f.origin.x, top - (f.origin.y + f.size.height), f.size.width,
|
|
372
|
+
f.size.height, v.origin.x, top - (v.origin.y + v.size.height),
|
|
373
|
+
v.size.width, v.size.height, s.backingScaleFactor, fps, i == 0});
|
|
374
|
+
}
|
|
375
|
+
return out;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// listScreens' shape.
|
|
379
|
+
static Napi::Array ScreensArray(Napi::Env env, const std::vector<PubScreen>& screens) {
|
|
380
|
+
Napi::Array out = Napi::Array::New(env, screens.size());
|
|
381
|
+
for (size_t i = 0; i < screens.size(); i++) {
|
|
382
|
+
const PubScreen& s = screens[i];
|
|
383
|
+
Napi::Object o = Napi::Object::New(env);
|
|
384
|
+
o.Set("x", s.x);
|
|
385
|
+
o.Set("y", s.y);
|
|
386
|
+
o.Set("width", s.width);
|
|
387
|
+
o.Set("height", s.height);
|
|
388
|
+
Napi::Object work = Napi::Object::New(env);
|
|
389
|
+
work.Set("x", s.vx);
|
|
390
|
+
work.Set("y", s.vy);
|
|
391
|
+
work.Set("width", s.vwidth);
|
|
392
|
+
work.Set("height", s.vheight);
|
|
393
|
+
o.Set("visible", work);
|
|
394
|
+
o.Set("scale", s.scale);
|
|
395
|
+
o.Set("fps", s.fps);
|
|
396
|
+
o.Set("primary", s.primary);
|
|
397
|
+
out.Set((uint32_t)i, o);
|
|
398
|
+
}
|
|
399
|
+
return out;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
static void PublishScreens() {
|
|
403
|
+
std::vector<PubScreen> screens = ScreensNow();
|
|
404
|
+
std::lock_guard<std::mutex> l(gPubMu);
|
|
405
|
+
gPubScreens = std::move(screens);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
static A11yOptions A11yOptionsNow() {
|
|
409
|
+
NSWorkspace* ws = NSWorkspace.sharedWorkspace;
|
|
410
|
+
return {(bool)ws.accessibilityDisplayShouldReduceMotion,
|
|
411
|
+
(bool)ws.accessibilityDisplayShouldReduceTransparency,
|
|
412
|
+
(bool)ws.accessibilityDisplayShouldIncreaseContrast,
|
|
413
|
+
(bool)ws.accessibilityDisplayShouldDifferentiateWithoutColor,
|
|
414
|
+
(bool)ws.accessibilityDisplayShouldInvertColors};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
static void A11yFields(CALEvent& r, const A11yOptions& a) {
|
|
418
|
+
r.Bool("reduceMotion", a.reduceMotion);
|
|
419
|
+
r.Bool("reduceTransparency", a.reduceTransparency);
|
|
420
|
+
r.Bool("increaseContrast", a.increaseContrast);
|
|
421
|
+
r.Bool("differentiateWithoutColor", a.differentiateWithoutColor);
|
|
422
|
+
r.Bool("invertColors", a.invertColors);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
static void PublishA11y(const A11yOptions& a) {
|
|
426
|
+
std::lock_guard<std::mutex> l(gPubMu);
|
|
427
|
+
gPubA11y = a;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
static void PublishPasteboardCount() {
|
|
431
|
+
gPubPasteboardCount = (long)NSPasteboard.generalPasteboard.changeCount;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// Everything the app as a whole publishes, once it is launched (BEnsureApp);
|
|
435
|
+
// the screen list again whenever the arrangement or a resolution changes.
|
|
436
|
+
static id gScreensObserver = nil;
|
|
437
|
+
|
|
438
|
+
static id gActiveObserver = nil, gInactiveObserver = nil;
|
|
439
|
+
|
|
440
|
+
static void BPublishAppState() {
|
|
441
|
+
PublishScreens();
|
|
442
|
+
PublishA11y(A11yOptionsNow());
|
|
443
|
+
gPubPolicy = (int)NSApp.activationPolicy;
|
|
444
|
+
PublishPasteboardCount();
|
|
445
|
+
PublishName();
|
|
446
|
+
PublishBadge();
|
|
447
|
+
gPubActive = NSApp.isActive;
|
|
448
|
+
if (gScreensObserver) return;
|
|
449
|
+
NSNotificationCenter* nc = NSNotificationCenter.defaultCenter;
|
|
450
|
+
gScreensObserver =
|
|
451
|
+
[nc addObserverForName:NSApplicationDidChangeScreenParametersNotification
|
|
452
|
+
object:NSApp
|
|
453
|
+
queue:nil
|
|
454
|
+
usingBlock:^(NSNotification*) { PublishScreens(); }];
|
|
455
|
+
gActiveObserver =
|
|
456
|
+
[nc addObserverForName:NSApplicationDidBecomeActiveNotification
|
|
457
|
+
object:NSApp
|
|
458
|
+
queue:nil
|
|
459
|
+
usingBlock:^(NSNotification*) { gPubActive = true; }];
|
|
460
|
+
gInactiveObserver =
|
|
461
|
+
[nc addObserverForName:NSApplicationDidResignActiveNotification
|
|
462
|
+
object:NSApp
|
|
463
|
+
queue:nil
|
|
464
|
+
usingBlock:^(NSNotification*) { gPubActive = false; }];
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// windowState(windowNumber) -> { x, y, width, height, scale, visible,
|
|
468
|
+
// occluded, key, ignoresMouseEvents } — getWindowFrame's answer for a
|
|
469
|
+
// createWindow2 window, as the UI thread last published it; null for a
|
|
470
|
+
// number this bridge has no window for. Any thread.
|
|
471
|
+
static Napi::Value WindowStateFn(const Napi::CallbackInfo& info) {
|
|
472
|
+
Napi::Env env = info.Env();
|
|
473
|
+
if (!info[0].IsNumber()) {
|
|
474
|
+
Napi::TypeError::New(env, "windowState(windowNumber): a number is required")
|
|
475
|
+
.ThrowAsJavaScriptException();
|
|
476
|
+
return env.Undefined();
|
|
477
|
+
}
|
|
478
|
+
long number = (long)info[0].As<Napi::Number>().Int64Value();
|
|
479
|
+
PubWindow s;
|
|
480
|
+
if (!PublishedWindow(number, &s)) return env.Null();
|
|
481
|
+
return WindowStateObject(env, s);
|
|
482
|
+
}
|
|
154
483
|
|
|
155
484
|
// ---------------------------------------------------------------------------
|
|
156
485
|
// app lifecycle: what the OS asks the application as a whole
|
|
@@ -174,8 +503,8 @@ void CALEmitBackendEvent(Napi::Env env, Napi::Object ev) { EmitToJS(env, ev); }
|
|
|
174
503
|
// event inside finishLaunching, so the delegate goes in before that call
|
|
175
504
|
// (BEnsureApp). And initApp() runs before setBackendEventCallback(), so
|
|
176
505
|
// whatever arrives with nobody listening is held here and replayed on the
|
|
177
|
-
// first pump that has a listener
|
|
178
|
-
// NSEvents. Registering the scheme itself (CFBundleURLTypes) is an
|
|
506
|
+
// first pump that has a listener — or as runMain opens the channel — in
|
|
507
|
+
// arrival order, ahead of that pump's NSEvents. Registering the scheme itself (CFBundleURLTypes) is an
|
|
179
508
|
// Info.plist matter for the app bundle, not runtime code.
|
|
180
509
|
|
|
181
510
|
enum class AppEventKind { OpenURLs, Reopen, QuitRequest };
|
|
@@ -188,48 +517,40 @@ struct PendingAppEvent {
|
|
|
188
517
|
};
|
|
189
518
|
static std::vector<PendingAppEvent> gPendingAppEvents;
|
|
190
519
|
|
|
191
|
-
static
|
|
192
|
-
Napi::Object ev = Napi::Object::New(env);
|
|
520
|
+
static CALEvent AppEventRecord(const PendingAppEvent& p) {
|
|
193
521
|
switch (p.kind) {
|
|
194
522
|
case AppEventKind::OpenURLs: {
|
|
195
|
-
ev
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
523
|
+
CALEvent ev("app-open-urls");
|
|
524
|
+
ev.Strs("urls", p.urls);
|
|
525
|
+
return ev;
|
|
526
|
+
}
|
|
527
|
+
case AppEventKind::Reopen: {
|
|
528
|
+
CALEvent ev("app-reopen");
|
|
529
|
+
ev.Bool("hasVisibleWindows", p.hasVisibleWindows);
|
|
530
|
+
return ev;
|
|
201
531
|
}
|
|
202
|
-
case AppEventKind::Reopen:
|
|
203
|
-
ev.Set("type", "app-reopen");
|
|
204
|
-
ev.Set("hasVisibleWindows", p.hasVisibleWindows);
|
|
205
|
-
break;
|
|
206
532
|
case AppEventKind::QuitRequest:
|
|
207
|
-
ev.Set("type", "app-quit-request");
|
|
208
533
|
break;
|
|
209
534
|
}
|
|
210
|
-
return
|
|
535
|
+
return CALEvent("app-quit-request");
|
|
211
536
|
}
|
|
212
537
|
|
|
213
|
-
// Now if
|
|
538
|
+
// Now if anyone is listening, else held for the pump (or the run) that
|
|
539
|
+
// finds a listener.
|
|
214
540
|
static void EmitAppEvent(PendingAppEvent&& p) {
|
|
215
|
-
if (!
|
|
541
|
+
if (!CALListening()) {
|
|
216
542
|
gPendingAppEvents.push_back(std::move(p));
|
|
217
543
|
return;
|
|
218
544
|
}
|
|
219
|
-
|
|
220
|
-
Napi::HandleScope scope(env);
|
|
221
|
-
EmitToJS(env, AppEventObject(env, p));
|
|
545
|
+
CALEmit(AppEventRecord(p));
|
|
222
546
|
}
|
|
223
547
|
|
|
224
|
-
static void FlushPendingAppEvents(
|
|
225
|
-
if (gPendingAppEvents.empty() || !
|
|
548
|
+
static void FlushPendingAppEvents() {
|
|
549
|
+
if (gPendingAppEvents.empty() || !CALListening()) return;
|
|
226
550
|
// moved out first: a handler may pump, and pumping may hold more
|
|
227
551
|
std::vector<PendingAppEvent> held = std::move(gPendingAppEvents);
|
|
228
552
|
gPendingAppEvents.clear();
|
|
229
|
-
for (const PendingAppEvent& p : held)
|
|
230
|
-
Napi::HandleScope scope(env);
|
|
231
|
-
EmitToJS(env, AppEventObject(env, p));
|
|
232
|
-
}
|
|
553
|
+
for (const PendingAppEvent& p : held) CALEmit(AppEventRecord(p));
|
|
233
554
|
}
|
|
234
555
|
|
|
235
556
|
@interface CALAppDelegate : NSObject <NSApplicationDelegate>
|
|
@@ -258,12 +579,13 @@ static void FlushPendingAppEvents(Napi::Env env) {
|
|
|
258
579
|
}
|
|
259
580
|
// With nobody listening the OS default stands and the process ends here,
|
|
260
581
|
// as it always has. With a listener the request is theirs to act on, and
|
|
261
|
-
// quitting is a process.exit on their side
|
|
262
|
-
//
|
|
263
|
-
//
|
|
582
|
+
// quitting is a process.exit on their side (in threaded mode, requestExit
|
|
583
|
+
// and the main thread's process.exit). Never NSTerminateLater: AppKit would
|
|
584
|
+
// spin its own run loop waiting for the reply, and in pump mode this
|
|
585
|
+
// process is pumped from JS, not run by AppKit.
|
|
264
586
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)app {
|
|
265
587
|
(void)app;
|
|
266
|
-
if (!
|
|
588
|
+
if (!CALListening()) return NSTerminateNow;
|
|
267
589
|
EmitAppEvent(PendingAppEvent(AppEventKind::QuitRequest));
|
|
268
590
|
return NSTerminateCancel;
|
|
269
591
|
}
|
|
@@ -297,22 +619,19 @@ static void BInstallAppDelegate() {
|
|
|
297
619
|
// reduceMotion (react-x11: loops never start, transitions still run) is its
|
|
298
620
|
// own call.
|
|
299
621
|
|
|
300
|
-
static Napi::Object AccessibilityDisplayOptions(Napi::Env env) {
|
|
301
|
-
NSWorkspace* ws = NSWorkspace.sharedWorkspace;
|
|
302
|
-
Napi::Object o = Napi::Object::New(env);
|
|
303
|
-
o.Set("reduceMotion", (bool)ws.accessibilityDisplayShouldReduceMotion);
|
|
304
|
-
o.Set("reduceTransparency", (bool)ws.accessibilityDisplayShouldReduceTransparency);
|
|
305
|
-
o.Set("increaseContrast", (bool)ws.accessibilityDisplayShouldIncreaseContrast);
|
|
306
|
-
o.Set("differentiateWithoutColor",
|
|
307
|
-
(bool)ws.accessibilityDisplayShouldDifferentiateWithoutColor);
|
|
308
|
-
o.Set("invertColors", (bool)ws.accessibilityDisplayShouldInvertColors);
|
|
309
|
-
return o;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
622
|
// accessibilityDisplayOptions() -> { reduceMotion, reduceTransparency,
|
|
313
623
|
// increaseContrast, differentiateWithoutColor, invertColors }
|
|
314
624
|
static Napi::Value AccessibilityDisplayOptionsFn(const Napi::CallbackInfo& info) {
|
|
315
|
-
|
|
625
|
+
A11yOptions a;
|
|
626
|
+
if (ReadPublished()) {
|
|
627
|
+
std::lock_guard<std::mutex> l(gPubMu);
|
|
628
|
+
a = gPubA11y;
|
|
629
|
+
} else {
|
|
630
|
+
a = A11yOptionsNow();
|
|
631
|
+
}
|
|
632
|
+
CALEvent r;
|
|
633
|
+
A11yFields(r, a);
|
|
634
|
+
return r.ToObject(info.Env());
|
|
316
635
|
}
|
|
317
636
|
|
|
318
637
|
// The observer is installed once, with the app (BEnsureApp), so it is in
|
|
@@ -329,12 +648,13 @@ static void BInstallAccessibilityObserver() {
|
|
|
329
648
|
queue:NSOperationQueue.mainQueue
|
|
330
649
|
usingBlock:^(NSNotification* n) {
|
|
331
650
|
(void)n;
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
ev
|
|
337
|
-
|
|
651
|
+
A11yOptions a = A11yOptionsNow();
|
|
652
|
+
PublishA11y(a);
|
|
653
|
+
if (!CALListening()) return;
|
|
654
|
+
CALEvent ev;
|
|
655
|
+
A11yFields(ev, a);
|
|
656
|
+
ev.Str("type", "accessibility-display-changed");
|
|
657
|
+
CALEmit(std::move(ev));
|
|
338
658
|
}];
|
|
339
659
|
}
|
|
340
660
|
|
|
@@ -342,11 +662,16 @@ static void BInstallAccessibilityObserver() {
|
|
|
342
662
|
// posted from here through the same centre, so the observer path can be
|
|
343
663
|
// exercised without touching the user's settings. Test-only, like
|
|
344
664
|
// postAppleEvent; the values it carries are whatever the settings are.
|
|
665
|
+
// A command: the centre calls its observers on the posting thread, and
|
|
666
|
+
// AppKit's own (the menu bar's) rebuilds the main menu there — from a
|
|
667
|
+
// worker that is an NSInternalInconsistencyException abort.
|
|
345
668
|
static Napi::Value PostAccessibilityDisplayChange(const Napi::CallbackInfo& info) {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
669
|
+
CALOnUI(^{
|
|
670
|
+
BEnsureApp();
|
|
671
|
+
[NSWorkspace.sharedWorkspace.notificationCenter
|
|
672
|
+
postNotificationName:NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification
|
|
673
|
+
object:NSWorkspace.sharedWorkspace];
|
|
674
|
+
});
|
|
350
675
|
return info.Env().Undefined();
|
|
351
676
|
}
|
|
352
677
|
|
|
@@ -356,62 +681,100 @@ static Napi::Value PostAccessibilityDisplayChange(const Napi::CallbackInfo& info
|
|
|
356
681
|
|
|
357
682
|
@interface CALBackendDelegate : NSObject <NSWindowDelegate> {
|
|
358
683
|
@public
|
|
359
|
-
|
|
684
|
+
double handshakeMs_; // setResizeHandshake's budget; 0 is off
|
|
360
685
|
}
|
|
361
686
|
@end
|
|
362
687
|
|
|
363
|
-
|
|
364
|
-
|
|
688
|
+
// A worker's window carries its handle's id (createWindow2 off the main
|
|
689
|
+
// thread), and every event about it names the handle JS holds, so a
|
|
690
|
+
// renderer can key its windows on the handle. Pump mode's windows have
|
|
691
|
+
// none, and their events are what they always were.
|
|
692
|
+
static char kWindowHandleKey;
|
|
693
|
+
|
|
694
|
+
static void AddWindowHandle(CALEvent& ev, NSWindow* win) {
|
|
695
|
+
NSNumber* id = objc_getAssociatedObject(win, &kWindowHandleKey);
|
|
696
|
+
if (id) ev.HandleRef("handle", id.unsignedLongLongValue);
|
|
365
697
|
}
|
|
366
698
|
|
|
367
|
-
static
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
ev
|
|
371
|
-
ev.Set("windowNumber", (double)win.windowNumber);
|
|
699
|
+
static CALEvent WindowEvent(NSWindow* win, const char* type) {
|
|
700
|
+
CALEvent ev(type);
|
|
701
|
+
ev.Num("windowNumber", (double)win.windowNumber);
|
|
702
|
+
AddWindowHandle(ev, win);
|
|
372
703
|
return ev;
|
|
373
704
|
}
|
|
374
705
|
|
|
375
|
-
static void EmitWindowGeometry(
|
|
376
|
-
|
|
377
|
-
if (!
|
|
378
|
-
|
|
379
|
-
Napi::Object ev = WindowEvent(env, win, type);
|
|
706
|
+
static void EmitWindowGeometry(NSWindow* win, const char* type, bool live) {
|
|
707
|
+
PublishWindow(win);
|
|
708
|
+
if (!CALListening()) return;
|
|
709
|
+
CALEvent ev = WindowEvent(win, type);
|
|
380
710
|
NSRect content = ContentViewScreenRect(win);
|
|
381
|
-
ev.
|
|
382
|
-
ev.
|
|
383
|
-
ev.
|
|
384
|
-
ev.
|
|
385
|
-
ev.
|
|
386
|
-
|
|
711
|
+
ev.Num("width", content.size.width);
|
|
712
|
+
ev.Num("height", content.size.height);
|
|
713
|
+
ev.Num("x", content.origin.x);
|
|
714
|
+
ev.Num("y", PrimaryScreenTop() - (content.origin.y + content.size.height));
|
|
715
|
+
ev.Bool("live", live);
|
|
716
|
+
CALEmit(std::move(ev));
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
// The live-resize handshake (windowkit/appkit#53), after window-resize has
|
|
720
|
+
// gone out: wait, bounded, for the renderer's frame at the new size and
|
|
721
|
+
// apply it before AppKit commits the resize, then say how it went —
|
|
722
|
+
// resize-handshake { windowNumber, handle?, width, height, live, waited
|
|
723
|
+
// (ms), met } — which is the renderer's measure of whether its frames keep
|
|
724
|
+
// up with the edge.
|
|
725
|
+
static void AwaitFrameForResize(NSWindow* win, double waitMs) {
|
|
726
|
+
NSSize size = ContentViewScreenRect(win).size;
|
|
727
|
+
double waited = 0;
|
|
728
|
+
bool met = CALAwaitFrame(size.width, size.height, waitMs, &waited);
|
|
729
|
+
CALEvent ev = WindowEvent(win, "resize-handshake");
|
|
730
|
+
ev.Num("width", size.width).Num("height", size.height);
|
|
731
|
+
ev.Bool("live", win.inLiveResize).Num("waited", waited).Bool("met", met);
|
|
732
|
+
CALEmit(std::move(ev));
|
|
387
733
|
}
|
|
388
734
|
|
|
389
735
|
@implementation CALBackendDelegate
|
|
390
736
|
- (void)windowDidResize:(NSNotification*)n {
|
|
391
737
|
NSWindow* win = n.object;
|
|
392
|
-
EmitWindowGeometry(
|
|
738
|
+
EmitWindowGeometry(win, "window-resize", win.inLiveResize);
|
|
739
|
+
// threaded mode's: in pump mode the event above ran the renderer inside
|
|
740
|
+
// this very call, and its frame is already in this transaction
|
|
741
|
+
if (handshakeMs_ > 0 && CALThreaded()) AwaitFrameForResize(win, handshakeMs_);
|
|
393
742
|
}
|
|
394
743
|
- (void)windowDidMove:(NSNotification*)n {
|
|
395
|
-
EmitWindowGeometry(
|
|
396
|
-
|
|
744
|
+
EmitWindowGeometry((NSWindow*)n.object, "window-move", false);
|
|
745
|
+
}
|
|
746
|
+
// A live resize's two ends (windowkit/appkit#63). AppKit's tracking loop
|
|
747
|
+
// calls windowDidResize: once per pointer move and nothing when the pointer
|
|
748
|
+
// stops or lifts, so a renderer that defers its measured layout until the
|
|
749
|
+
// drag is over has to be told when it is: window-live-resize { phase:
|
|
750
|
+
// 'begin' | 'end' }, in both modes, and liveResize in the published state.
|
|
751
|
+
- (void)windowWillStartLiveResize:(NSNotification*)n {
|
|
752
|
+
NSWindow* win = n.object;
|
|
753
|
+
PublishWindowLiveResize(win, true);
|
|
754
|
+
CALEvent ev = WindowEvent(win, "window-live-resize");
|
|
755
|
+
ev.Str("phase", "begin");
|
|
756
|
+
CALEmit(std::move(ev));
|
|
757
|
+
}
|
|
758
|
+
- (void)windowDidEndLiveResize:(NSNotification*)n {
|
|
759
|
+
NSWindow* win = n.object;
|
|
760
|
+
PublishWindowLiveResize(win, false);
|
|
761
|
+
CALEvent ev = WindowEvent(win, "window-live-resize");
|
|
762
|
+
ev.Str("phase", "end");
|
|
763
|
+
CALEmit(std::move(ev));
|
|
397
764
|
}
|
|
398
765
|
- (BOOL)windowShouldClose:(NSWindow*)sender {
|
|
399
|
-
if (
|
|
400
|
-
Napi::Env env(env_);
|
|
401
|
-
Napi::HandleScope scope(env);
|
|
402
|
-
EmitToJS(env, WindowEvent(env, sender, "window-close-request"));
|
|
403
|
-
}
|
|
766
|
+
if (CALListening()) CALEmit(WindowEvent(sender, "window-close-request"));
|
|
404
767
|
return NO; // closing is the renderer's decision, never AppKit's
|
|
405
768
|
}
|
|
406
769
|
- (void)windowDidBecomeKey:(NSNotification*)n {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
770
|
+
NSWindow* win = n.object;
|
|
771
|
+
PublishWindow(win);
|
|
772
|
+
CALEmit(WindowEvent(win, "window-focus"));
|
|
410
773
|
}
|
|
411
774
|
- (void)windowDidResignKey:(NSNotification*)n {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
775
|
+
NSWindow* win = n.object;
|
|
776
|
+
PublishWindow(win);
|
|
777
|
+
CALEmit(WindowEvent(win, "window-blur"));
|
|
415
778
|
}
|
|
416
779
|
// A window entirely behind another application's window is still visible
|
|
417
780
|
// by isVisible's measure and still costs every frame its tree produces.
|
|
@@ -419,19 +782,17 @@ static void EmitWindowGeometry(Napi::Env env, NSWindow* win, const char* type,
|
|
|
419
782
|
// window is on glass, so a renderer can hold its frames until one is.
|
|
420
783
|
- (void)windowDidChangeOcclusionState:(NSNotification*)n {
|
|
421
784
|
NSWindow* win = n.object;
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
EmitToJS(env, ev);
|
|
785
|
+
PublishWindow(win);
|
|
786
|
+
CALEvent ev = WindowEvent(win, "window-occlusion");
|
|
787
|
+
ev.Bool("visible", WindowOnGlass(win));
|
|
788
|
+
CALEmit(std::move(ev));
|
|
427
789
|
}
|
|
428
790
|
- (void)windowDidChangeBackingProperties:(NSNotification*)n {
|
|
429
791
|
NSWindow* win = n.object;
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
ev
|
|
434
|
-
EmitToJS(env, ev);
|
|
792
|
+
PublishWindow(win);
|
|
793
|
+
CALEvent ev = WindowEvent(win, "window-scale");
|
|
794
|
+
ev.Num("scale", win.backingScaleFactor);
|
|
795
|
+
CALEmit(std::move(ev));
|
|
435
796
|
}
|
|
436
797
|
@end
|
|
437
798
|
|
|
@@ -445,10 +806,9 @@ static char kDelegateKey;
|
|
|
445
806
|
@interface CALBackendView : NSView {
|
|
446
807
|
@public
|
|
447
808
|
NSTrackingArea* tracking_;
|
|
448
|
-
// drag and drop (its own section below): the
|
|
449
|
-
//
|
|
450
|
-
//
|
|
451
|
-
napi_env env_;
|
|
809
|
+
// drag and drop (its own section below): the destination's standing
|
|
810
|
+
// answer, the source's masks and provider, and the press a session is
|
|
811
|
+
// begun from
|
|
452
812
|
bool dropAccept_;
|
|
453
813
|
NSString* dropOp_; // nil: the conventional operation for the source's mask
|
|
454
814
|
NSDragOperation sourceMask_, sourceMaskOutside_;
|
|
@@ -505,15 +865,63 @@ static char kDelegateKey;
|
|
|
505
865
|
// resizable, opaque, hasShadow, level, // level: 'normal'|'popup'|'floating'
|
|
506
866
|
// ignoresMouseEvents, // the pointer passes through (below)
|
|
507
867
|
// backgroundColor }) // [r,g,b,a] or absent
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
868
|
+
//
|
|
869
|
+
// On the main thread (pump mode) the window is made in the call and the
|
|
870
|
+
// window itself is the handle, as always. Off it (a worker, threaded mode)
|
|
871
|
+
// the options are read here, a handle is answered at once, and the window is
|
|
872
|
+
// made by a command; `window-created { handle, windowNumber }` follows, and
|
|
873
|
+
// every event about the window carries `handle`.
|
|
874
|
+
|
|
875
|
+
// createWindow2's options, read on the calling thread.
|
|
876
|
+
struct WindowSpec {
|
|
877
|
+
double w = 640, h = 480;
|
|
878
|
+
std::string kind = "normal", level;
|
|
879
|
+
bool resizable = true;
|
|
880
|
+
NSString* title = nil;
|
|
881
|
+
int hasShadow = -1, opaque = -1, ignoresMouseEvents = -1; // -1: not given
|
|
882
|
+
bool hasBackground = false;
|
|
883
|
+
double background[4] = {0, 0, 0, 1};
|
|
884
|
+
bool placed = false;
|
|
885
|
+
double x = 0, y = 0;
|
|
886
|
+
};
|
|
887
|
+
|
|
888
|
+
static int BTriOr(Napi::Object o, const char* k, bool d) {
|
|
889
|
+
return o.Has(k) ? (int)BBoolOr(o, k, d) : -1;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
static WindowSpec ParseWindowSpec(Napi::Object o) {
|
|
893
|
+
WindowSpec s;
|
|
894
|
+
s.w = BNumOr(o, "width", 640);
|
|
895
|
+
s.h = BNumOr(o, "height", 480);
|
|
896
|
+
if (o.Has("kind") && o.Get("kind").IsString())
|
|
897
|
+
s.kind = o.Get("kind").As<Napi::String>().Utf8Value();
|
|
898
|
+
s.resizable = BBoolOr(o, "resizable", true);
|
|
899
|
+
if (o.Has("title") && o.Get("title").IsString())
|
|
900
|
+
s.title = BToNSString(o.Get("title"));
|
|
901
|
+
if (o.Has("level") && o.Get("level").IsString())
|
|
902
|
+
s.level = o.Get("level").As<Napi::String>().Utf8Value();
|
|
903
|
+
s.hasShadow = BTriOr(o, "hasShadow", true);
|
|
904
|
+
s.opaque = BTriOr(o, "opaque", true);
|
|
905
|
+
s.ignoresMouseEvents = BTriOr(o, "ignoresMouseEvents", false);
|
|
906
|
+
if (o.Has("backgroundColor") && o.Get("backgroundColor").IsArray()) {
|
|
907
|
+
Napi::Array a = o.Get("backgroundColor").As<Napi::Array>();
|
|
908
|
+
s.hasBackground = true;
|
|
909
|
+
for (uint32_t i = 0; i < 4 && i < a.Length(); i++)
|
|
910
|
+
s.background[i] = a.Get(i).As<Napi::Number>().DoubleValue();
|
|
911
|
+
}
|
|
912
|
+
if (o.Has("x") && o.Get("x").IsNumber() && o.Has("y") && o.Get("y").IsNumber()) {
|
|
913
|
+
s.placed = true;
|
|
914
|
+
s.x = BNumOr(o, "x", 0);
|
|
915
|
+
s.y = BNumOr(o, "y", 0);
|
|
916
|
+
}
|
|
917
|
+
return s;
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
// On the UI thread, the app launched.
|
|
921
|
+
static NSWindow* BuildWindow(const WindowSpec& spec) {
|
|
922
|
+
double w = spec.w, h = spec.h;
|
|
923
|
+
const std::string& kind = spec.kind;
|
|
924
|
+
bool resizable = spec.resizable;
|
|
517
925
|
|
|
518
926
|
NSWindow* win;
|
|
519
927
|
@autoreleasepool {
|
|
@@ -548,16 +956,12 @@ static Napi::Value CreateWindow2(const Napi::CallbackInfo& info) {
|
|
|
548
956
|
// Never a tab bar, whatever an earlier process left in the defaults
|
|
549
957
|
// domain (see BEnsureApp).
|
|
550
958
|
win.tabbingMode = NSWindowTabbingModeDisallowed;
|
|
551
|
-
if (
|
|
552
|
-
|
|
553
|
-
if (
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
}
|
|
558
|
-
if (o.Has("hasShadow")) win.hasShadow = BBoolOr(o, "hasShadow", true);
|
|
559
|
-
if (o.Has("opaque")) {
|
|
560
|
-
win.opaque = BBoolOr(o, "opaque", true);
|
|
959
|
+
if (spec.title) win.title = spec.title;
|
|
960
|
+
if (spec.level == "popup") win.level = NSPopUpMenuWindowLevel;
|
|
961
|
+
else if (spec.level == "floating") win.level = NSFloatingWindowLevel;
|
|
962
|
+
if (spec.hasShadow >= 0) win.hasShadow = spec.hasShadow;
|
|
963
|
+
if (spec.opaque >= 0) {
|
|
964
|
+
win.opaque = spec.opaque;
|
|
561
965
|
if (!win.opaque) win.backgroundColor = NSColor.clearColor;
|
|
562
966
|
}
|
|
563
967
|
// A window the pointer passes through. The window server hit-tests
|
|
@@ -566,68 +970,115 @@ static Napi::Value CreateWindow2(const Napi::CallbackInfo& info) {
|
|
|
566
970
|
// types is not that: the window under the pointer is found first, an
|
|
567
971
|
// unregistered one is still the one found, and the drag then has no
|
|
568
972
|
// destination at all. Transparent pixels do not pass a hit either.
|
|
569
|
-
if (
|
|
570
|
-
win.ignoresMouseEvents = BBoolOr(o, "ignoresMouseEvents", false);
|
|
973
|
+
if (spec.ignoresMouseEvents >= 0) win.ignoresMouseEvents = spec.ignoresMouseEvents;
|
|
571
974
|
|
|
572
975
|
CALBackendView* view = [[CALBackendView alloc] initWithFrame:rect];
|
|
573
|
-
view->env_ = (napi_env)env;
|
|
574
976
|
CALayer* root = [CALayer layer];
|
|
575
977
|
root.geometryFlipped = YES;
|
|
576
978
|
[view setLayer:root];
|
|
577
979
|
[view setWantsLayer:YES];
|
|
578
980
|
win.contentView = view;
|
|
579
981
|
root.contentsScale = win.backingScaleFactor;
|
|
580
|
-
if (
|
|
581
|
-
CGColorRef c =
|
|
982
|
+
if (spec.hasBackground) {
|
|
983
|
+
CGColorRef c = CGColorCreateSRGB(spec.background[0], spec.background[1],
|
|
984
|
+
spec.background[2], spec.background[3]);
|
|
582
985
|
root.backgroundColor = c;
|
|
583
986
|
CGColorRelease(c);
|
|
584
987
|
}
|
|
585
988
|
|
|
586
989
|
// Placement: explicit top-left global coordinates, or centered.
|
|
587
|
-
if (
|
|
588
|
-
o.Get("y").IsNumber()) {
|
|
589
|
-
double x = BNumOr(o, "x", 0), y = BNumOr(o, "y", 0);
|
|
590
|
-
NSRect frame = [win frameRectForContentRect:NSMakeRect(0, 0, w, h)];
|
|
591
|
-
CGFloat titlebar = frame.size.height - h;
|
|
990
|
+
if (spec.placed) {
|
|
592
991
|
// y is the CONTENT's top edge in top-left global coordinates.
|
|
593
|
-
[win setFrameOrigin:NSMakePoint(x, PrimaryScreenTop() - y - h)];
|
|
594
|
-
(void)titlebar;
|
|
992
|
+
[win setFrameOrigin:NSMakePoint(spec.x, PrimaryScreenTop() - spec.y - h)];
|
|
595
993
|
} else {
|
|
596
994
|
[win center];
|
|
597
995
|
}
|
|
598
996
|
|
|
599
997
|
CALBackendDelegate* delegate = [[CALBackendDelegate alloc] init];
|
|
600
|
-
delegate->env_ = (napi_env)env;
|
|
601
998
|
win.delegate = delegate;
|
|
602
999
|
objc_setAssociatedObject(win, &kDelegateKey, delegate,
|
|
603
1000
|
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
604
1001
|
[win makeFirstResponder:view];
|
|
1002
|
+
PublishWindow(win);
|
|
605
1003
|
}
|
|
606
|
-
return
|
|
1004
|
+
return win;
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
static Napi::Value CreateWindow2(const Napi::CallbackInfo& info) {
|
|
1008
|
+
Napi::Env env = info.Env();
|
|
1009
|
+
WindowSpec spec = ParseWindowSpec(info[0].IsObject() ? info[0].As<Napi::Object>()
|
|
1010
|
+
: Napi::Object::New(env));
|
|
1011
|
+
if (pthread_main_np()) {
|
|
1012
|
+
BEnsureApp();
|
|
1013
|
+
NSWindow* win = BuildWindow(spec);
|
|
1014
|
+
CALUIObjectsChanged(+1);
|
|
1015
|
+
return BWrapRetained(env, win);
|
|
1016
|
+
}
|
|
1017
|
+
CALHandle* h = CALNewHandle();
|
|
1018
|
+
h->part_ = CALNewHandle(); // windowRootLayer's, bound with the window
|
|
1019
|
+
Napi::Value handle = CALWrapHandle(env, h, true);
|
|
1020
|
+
CALUIObjectsChanged(+1); // counted at the call: this thread's loop is held now
|
|
1021
|
+
CALOnUI(^{
|
|
1022
|
+
BEnsureApp();
|
|
1023
|
+
NSWindow* win = BuildWindow(spec);
|
|
1024
|
+
h->object_ = win;
|
|
1025
|
+
h->part_->object_ = win.contentView.layer;
|
|
1026
|
+
h->number_ = (long)win.windowNumber;
|
|
1027
|
+
objc_setAssociatedObject(win, &kWindowHandleKey, @(h->id_),
|
|
1028
|
+
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
1029
|
+
CALEvent ev("window-created");
|
|
1030
|
+
ev.HandleRef("handle", h->id_);
|
|
1031
|
+
ev.Num("windowNumber", (double)win.windowNumber);
|
|
1032
|
+
CALEmit(std::move(ev));
|
|
1033
|
+
});
|
|
1034
|
+
return handle;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
// Runs `body` on the UI thread with the window a verb's first argument
|
|
1038
|
+
// names: inline for pump mode's window on the main thread, as a command for
|
|
1039
|
+
// a worker's handle — resolved there, and a no-op before the window is made
|
|
1040
|
+
// or for anything that is not a window handle.
|
|
1041
|
+
static void OnWindow(Napi::Value v, void (^body)(NSWindow* win)) {
|
|
1042
|
+
id target = CALHandleTarget(v);
|
|
1043
|
+
if (!target) return;
|
|
1044
|
+
CALOnUI(^{
|
|
1045
|
+
NSWindow* win = CALResolve(target);
|
|
1046
|
+
if (win) body(win);
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
static double BNumArg(const Napi::CallbackInfo& info, size_t i) {
|
|
1051
|
+
return info.Length() > i && info[i].IsNumber()
|
|
1052
|
+
? info[i].As<Napi::Number>().DoubleValue()
|
|
1053
|
+
: NAN;
|
|
607
1054
|
}
|
|
608
1055
|
|
|
609
1056
|
// showWindow(win, activate) — map. Popups order front without activating.
|
|
610
1057
|
static Napi::Value ShowWindowFn(const Napi::CallbackInfo& info) {
|
|
611
|
-
NSWindow* win = BDeref<NSWindow*>(info[0]);
|
|
612
1058
|
bool activate = info.Length() > 1 && info[1].ToBoolean().Value();
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
1059
|
+
OnWindow(info[0], ^(NSWindow* win) {
|
|
1060
|
+
if (activate) {
|
|
1061
|
+
[win makeKeyAndOrderFront:nil];
|
|
1062
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
1063
|
+
} else {
|
|
1064
|
+
[win orderFrontRegardless];
|
|
1065
|
+
}
|
|
1066
|
+
PublishWindow(win);
|
|
1067
|
+
});
|
|
619
1068
|
return info.Env().Undefined();
|
|
620
1069
|
}
|
|
621
1070
|
|
|
622
1071
|
static Napi::Value HideWindowFn(const Napi::CallbackInfo& info) {
|
|
623
|
-
|
|
624
|
-
|
|
1072
|
+
OnWindow(info[0], ^(NSWindow* win) {
|
|
1073
|
+
[win orderOut:nil];
|
|
1074
|
+
PublishWindow(win);
|
|
1075
|
+
});
|
|
625
1076
|
return info.Env().Undefined();
|
|
626
1077
|
}
|
|
627
1078
|
|
|
628
1079
|
static Napi::Value SetWindowTitle(const Napi::CallbackInfo& info) {
|
|
629
|
-
|
|
630
|
-
win.title =
|
|
1080
|
+
NSString* title = BToNSString(info[1]);
|
|
1081
|
+
OnWindow(info[0], ^(NSWindow* win) { win.title = title; });
|
|
631
1082
|
return info.Env().Undefined();
|
|
632
1083
|
}
|
|
633
1084
|
|
|
@@ -635,49 +1086,47 @@ static Napi::Value SetWindowTitle(const Napi::CallbackInfo& info) {
|
|
|
635
1086
|
// same name, changed on a live window; the next hit the window server
|
|
636
1087
|
// resolves honours it.
|
|
637
1088
|
static Napi::Value SetWindowIgnoresMouseEvents(const Napi::CallbackInfo& info) {
|
|
638
|
-
|
|
639
|
-
|
|
1089
|
+
bool flag = info.Length() > 1 && info[1].ToBoolean().Value();
|
|
1090
|
+
OnWindow(info[0], ^(NSWindow* win) {
|
|
1091
|
+
win.ignoresMouseEvents = flag;
|
|
1092
|
+
PublishWindow(win);
|
|
1093
|
+
});
|
|
640
1094
|
return info.Env().Undefined();
|
|
641
1095
|
}
|
|
642
1096
|
|
|
643
1097
|
// setWindowFrame(win, x, y, w, h) — any argument may be null to keep it.
|
|
644
1098
|
// x/y are the content's top-left in global top-left coordinates, points.
|
|
645
1099
|
static Napi::Value SetWindowFrame(const Napi::CallbackInfo& info) {
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
[win setFrame:[win frameRectForContentRect:newContent] display:YES];
|
|
1100
|
+
double ax = BNumArg(info, 1), ay = BNumArg(info, 2);
|
|
1101
|
+
double aw = BNumArg(info, 3), ah = BNumArg(info, 4);
|
|
1102
|
+
OnWindow(info[0], ^(NSWindow* win) {
|
|
1103
|
+
NSRect content = ContentViewScreenRect(win);
|
|
1104
|
+
double topY = PrimaryScreenTop() - (content.origin.y + content.size.height);
|
|
1105
|
+
double x = std::isnan(ax) ? content.origin.x : ax;
|
|
1106
|
+
double y = std::isnan(ay) ? topY : ay;
|
|
1107
|
+
double w = std::isnan(aw) ? content.size.width : aw;
|
|
1108
|
+
double h = std::isnan(ah) ? content.size.height : ah;
|
|
1109
|
+
NSRect newContent = NSMakeRect(x, PrimaryScreenTop() - y - h, w, h);
|
|
1110
|
+
[win setFrame:[win frameRectForContentRect:newContent] display:YES];
|
|
1111
|
+
PublishWindow(win);
|
|
1112
|
+
});
|
|
660
1113
|
return info.Env().Undefined();
|
|
661
1114
|
}
|
|
662
1115
|
|
|
663
1116
|
// -> { x, y, width, height, scale, visible, occluded, key, ignoresMouseEvents }
|
|
664
|
-
// — content rect, top-left global coordinates, points.
|
|
1117
|
+
// — content rect, top-left global coordinates, points. (windowState reads
|
|
1118
|
+
// the same shape from the published copy, by window number.) From a worker
|
|
1119
|
+
// it is that copy: null until the window is made.
|
|
665
1120
|
static Napi::Value GetWindowFrame(const Napi::CallbackInfo& info) {
|
|
666
1121
|
Napi::Env env = info.Env();
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
r.Set("visible", (bool)win.isVisible);
|
|
676
|
-
// visible but with no pixel on glass: fully behind another app's window
|
|
677
|
-
r.Set("occluded", win.isVisible && !WindowOnGlass(win));
|
|
678
|
-
r.Set("key", (bool)win.isKeyWindow);
|
|
679
|
-
r.Set("ignoresMouseEvents", (bool)win.ignoresMouseEvents);
|
|
680
|
-
return r;
|
|
1122
|
+
id target = CALHandleTarget(info[0]);
|
|
1123
|
+
if (!pthread_main_np()) {
|
|
1124
|
+
CALHandle* h = WindowHandleOf(target);
|
|
1125
|
+
PubWindow s;
|
|
1126
|
+
if (!h || !PublishedWindow(h->number_.load(), &s)) return env.Null();
|
|
1127
|
+
return WindowStateObject(env, s);
|
|
1128
|
+
}
|
|
1129
|
+
return WindowStateObject(env, WindowStateOf(CALResolve(target)));
|
|
681
1130
|
}
|
|
682
1131
|
|
|
683
1132
|
// windowNumberAtPoint(x, y, belowWindowNumber?) -> number — the window the
|
|
@@ -687,6 +1136,7 @@ static Napi::Value GetWindowFrame(const Napi::CallbackInfo& info) {
|
|
|
687
1136
|
// window number in `belowWindowNumber` starts the search beneath that
|
|
688
1137
|
// window, so walking the answers back in finds what sits under another
|
|
689
1138
|
// application's window — a lock screen, a floating panel.
|
|
1139
|
+
// Off the main thread: windowNumberAtPoint(x, y, below?, cb).
|
|
690
1140
|
static Napi::Value WindowNumberAtPoint(const Napi::CallbackInfo& info) {
|
|
691
1141
|
Napi::Env env = info.Env();
|
|
692
1142
|
if (!info[0].IsNumber() || !info[1].IsNumber()) {
|
|
@@ -694,50 +1144,94 @@ static Napi::Value WindowNumberAtPoint(const Napi::CallbackInfo& info) {
|
|
|
694
1144
|
.ThrowAsJavaScriptException();
|
|
695
1145
|
return env.Undefined();
|
|
696
1146
|
}
|
|
697
|
-
BEnsureApp();
|
|
698
1147
|
double x = info[0].As<Napi::Number>().DoubleValue();
|
|
699
1148
|
double y = info[1].As<Napi::Number>().DoubleValue();
|
|
700
1149
|
NSInteger below = info.Length() > 2 && info[2].IsNumber()
|
|
701
1150
|
? (NSInteger)info[2].As<Napi::Number>().Int64Value()
|
|
702
1151
|
: 0;
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
1152
|
+
return CALAnswer(info, "windowNumberAtPoint", ^CALValueBlock {
|
|
1153
|
+
BEnsureApp();
|
|
1154
|
+
NSInteger hit =
|
|
1155
|
+
[NSWindow windowNumberAtPoint:NSMakePoint(x, PrimaryScreenTop() - y)
|
|
1156
|
+
belowWindowWithWindowNumber:below];
|
|
1157
|
+
return ^Napi::Value(Napi::Env e) { return Napi::Number::New(e, (double)hit); };
|
|
1158
|
+
});
|
|
707
1159
|
}
|
|
708
1160
|
|
|
709
1161
|
static Napi::Value SetWindowMinMax(const Napi::CallbackInfo& info) {
|
|
710
|
-
NSWindow* win = BDeref<NSWindow*>(info[0]);
|
|
711
1162
|
Napi::Object o = info[1].As<Napi::Object>();
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
1163
|
+
bool setMin = o.Has("minWidth") || o.Has("minHeight");
|
|
1164
|
+
bool setMax = o.Has("maxWidth") || o.Has("maxHeight");
|
|
1165
|
+
NSSize min = NSMakeSize(BNumOr(o, "minWidth", 0), BNumOr(o, "minHeight", 0));
|
|
1166
|
+
NSSize max = NSMakeSize(BNumOr(o, "maxWidth", 100000), BNumOr(o, "maxHeight", 100000));
|
|
1167
|
+
OnWindow(info[0], ^(NSWindow* win) {
|
|
1168
|
+
if (setMin) win.contentMinSize = min;
|
|
1169
|
+
if (setMax) win.contentMaxSize = max;
|
|
1170
|
+
});
|
|
718
1171
|
return info.Env().Undefined();
|
|
719
1172
|
}
|
|
720
1173
|
|
|
1174
|
+
// setResizeHandshake(win, { waitMs }) — the live-resize handshake
|
|
1175
|
+
// (windowkit/appkit#53). When the window's size changes, by a live resize or
|
|
1176
|
+
// a setWindowFrame, the UI thread sends window-resize and then waits, never
|
|
1177
|
+
// longer than waitMs, for a frame batch committed with that size
|
|
1178
|
+
// (txCommit({ width, height })), applying it in the same transaction as the
|
|
1179
|
+
// new size; resize-handshake reports each wait. 0 turns it off, the
|
|
1180
|
+
// default. Pump mode needs none — its window-resize runs the renderer inside
|
|
1181
|
+
// windowDidResize: itself — so it acts only while runMain runs.
|
|
1182
|
+
static Napi::Value SetResizeHandshake(const Napi::CallbackInfo& info) {
|
|
1183
|
+
Napi::Env env = info.Env();
|
|
1184
|
+
Napi::Value v = info[1].IsObject() ? info[1].As<Napi::Object>().Get("waitMs")
|
|
1185
|
+
: env.Undefined();
|
|
1186
|
+
double ms = v.IsNumber() ? v.As<Napi::Number>().DoubleValue() : NAN;
|
|
1187
|
+
if (!(ms >= 0) || !std::isfinite(ms)) {
|
|
1188
|
+
Napi::TypeError::New(env, "setResizeHandshake(win, { waitMs }): waitMs is a "
|
|
1189
|
+
"number of milliseconds, 0 for off")
|
|
1190
|
+
.ThrowAsJavaScriptException();
|
|
1191
|
+
return env.Undefined();
|
|
1192
|
+
}
|
|
1193
|
+
OnWindow(info[0], ^(NSWindow* win) {
|
|
1194
|
+
CALBackendDelegate* d = objc_getAssociatedObject(win, &kDelegateKey);
|
|
1195
|
+
if (d) d->handshakeMs_ = ms;
|
|
1196
|
+
});
|
|
1197
|
+
return env.Undefined();
|
|
1198
|
+
}
|
|
1199
|
+
|
|
721
1200
|
static void CancelPanelSheetsOn(NSWindow* win);
|
|
722
1201
|
|
|
723
1202
|
static Napi::Value DestroyWindow2(const Napi::CallbackInfo& info) {
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
[win
|
|
732
|
-
|
|
1203
|
+
id target = CALHandleTarget(info[0]);
|
|
1204
|
+
CALHandle* h = WindowHandleOf(target);
|
|
1205
|
+
if (h) {
|
|
1206
|
+
// a worker's window is counted out at the call, once
|
|
1207
|
+
if (h->released_.exchange(true)) return info.Env().Undefined();
|
|
1208
|
+
CALUIObjectsChanged(-1);
|
|
1209
|
+
}
|
|
1210
|
+
OnWindow(info[0], ^(NSWindow* win) {
|
|
1211
|
+
long number = (long)win.windowNumber;
|
|
1212
|
+
// a second destroy of the same window counts nothing
|
|
1213
|
+
bool live = objc_getAssociatedObject(win, &kDelegateKey) != nil;
|
|
1214
|
+
// A sheet whose owner goes away ends without telling its completion
|
|
1215
|
+
// handler; answer it as a cancel first so no callback is left waiting.
|
|
1216
|
+
CancelPanelSheetsOn(win);
|
|
1217
|
+
win.delegate = nil;
|
|
1218
|
+
objc_setAssociatedObject(win, &kDelegateKey, nil,
|
|
1219
|
+
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
1220
|
+
[win orderOut:nil];
|
|
1221
|
+
[win close];
|
|
1222
|
+
ForgetWindow(number);
|
|
1223
|
+
if (live && !h) CALUIObjectsChanged(-1);
|
|
1224
|
+
});
|
|
733
1225
|
return info.Env().Undefined();
|
|
734
1226
|
}
|
|
735
1227
|
|
|
736
1228
|
// initApp lives in the app presence section: it takes { activationPolicy }.
|
|
737
1229
|
|
|
738
1230
|
static Napi::Value ActivateApp(const Napi::CallbackInfo& info) {
|
|
739
|
-
|
|
740
|
-
|
|
1231
|
+
CALOnUI(^{
|
|
1232
|
+
BEnsureApp();
|
|
1233
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
1234
|
+
});
|
|
741
1235
|
return info.Env().Undefined();
|
|
742
1236
|
}
|
|
743
1237
|
|
|
@@ -753,77 +1247,84 @@ static Napi::Value ActivateApp(const Napi::CallbackInfo& info) {
|
|
|
753
1247
|
// Apple Event Manager, minus a spurious errAETimeout on kAEOpenDocuments,
|
|
754
1248
|
// whose AppKit handler suspends the event to reply later.) No Automation
|
|
755
1249
|
// permission is involved: nothing leaves the process.
|
|
1250
|
+
// A command from a worker, whose dispatch failure then goes unreported.
|
|
756
1251
|
static Napi::Value PostAppleEvent(const Napi::CallbackInfo& info) {
|
|
757
1252
|
Napi::Env env = info.Env();
|
|
758
|
-
BEnsureApp();
|
|
759
1253
|
if (!info[0].IsString()) {
|
|
760
1254
|
Napi::TypeError::New(env, "postAppleEvent: kind must be a string")
|
|
761
1255
|
.ThrowAsJavaScriptException();
|
|
762
1256
|
return env.Undefined();
|
|
763
1257
|
}
|
|
764
1258
|
std::string kind = info[0].As<Napi::String>().Utf8Value();
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
NSAppleEventDescriptor* direct = nil;
|
|
771
|
-
if (kind == "open-url") {
|
|
772
|
-
if (!info[1].IsString()) {
|
|
773
|
-
Napi::TypeError::New(env, "postAppleEvent('open-url', url): url must be a string")
|
|
774
|
-
.ThrowAsJavaScriptException();
|
|
775
|
-
return env.Undefined();
|
|
776
|
-
}
|
|
777
|
-
cls = kInternetEventClass;
|
|
778
|
-
id = kAEGetURL;
|
|
779
|
-
direct = [NSAppleEventDescriptor descriptorWithString:BToNSString(info[1])];
|
|
780
|
-
} else if (kind == "open-documents") {
|
|
781
|
-
if (!info[1].IsArray()) {
|
|
782
|
-
Napi::TypeError::New(env, "postAppleEvent('open-documents', paths): paths must be an array")
|
|
783
|
-
.ThrowAsJavaScriptException();
|
|
784
|
-
return env.Undefined();
|
|
785
|
-
}
|
|
786
|
-
cls = kCoreEventClass;
|
|
787
|
-
id = kAEOpenDocuments;
|
|
788
|
-
direct = [NSAppleEventDescriptor listDescriptor];
|
|
789
|
-
Napi::Array paths = info[1].As<Napi::Array>();
|
|
790
|
-
for (uint32_t i = 0; i < paths.Length(); i++) {
|
|
791
|
-
NSURL* url = [NSURL fileURLWithPath:BToNSString(paths.Get(i))];
|
|
792
|
-
[direct insertDescriptor:[NSAppleEventDescriptor descriptorWithFileURL:url]
|
|
793
|
-
atIndex:0]; // 0 appends
|
|
794
|
-
}
|
|
795
|
-
} else if (kind == "reopen") {
|
|
796
|
-
cls = kCoreEventClass;
|
|
797
|
-
id = kAEReopenApplication;
|
|
798
|
-
} else if (kind == "quit") {
|
|
799
|
-
cls = kCoreEventClass;
|
|
800
|
-
id = kAEQuitApplication;
|
|
801
|
-
} else {
|
|
802
|
-
Napi::TypeError::New(env, "postAppleEvent: unknown kind '" + kind + "'")
|
|
1259
|
+
NSString* url = nil;
|
|
1260
|
+
NSMutableArray<NSString*>* paths = nil;
|
|
1261
|
+
if (kind == "open-url") {
|
|
1262
|
+
if (!info[1].IsString()) {
|
|
1263
|
+
Napi::TypeError::New(env, "postAppleEvent('open-url', url): url must be a string")
|
|
803
1264
|
.ThrowAsJavaScriptException();
|
|
804
1265
|
return env.Undefined();
|
|
805
1266
|
}
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
returnID:kAutoGenerateReturnID
|
|
811
|
-
transactionID:kAnyTransactionID];
|
|
812
|
-
if (direct) [ev setParamDescriptor:direct forKeyword:keyDirectObject];
|
|
813
|
-
AppleEvent reply = {typeNull, NULL};
|
|
814
|
-
// the refCon reaches raw C handlers only; AppKit's are Objective-C
|
|
815
|
-
// methods looked up by class and id, but the parameter is non-null
|
|
816
|
-
static char refCon;
|
|
817
|
-
OSErr err = [[NSAppleEventManager sharedAppleEventManager]
|
|
818
|
-
dispatchRawAppleEvent:ev.aeDesc
|
|
819
|
-
withRawReply:&reply
|
|
820
|
-
handlerRefCon:(SRefCon)&refCon];
|
|
821
|
-
AEDisposeDesc(&reply);
|
|
822
|
-
if (err != noErr) {
|
|
823
|
-
Napi::Error::New(env, "postAppleEvent: dispatch failed (" +
|
|
824
|
-
std::to_string((int)err) + ")")
|
|
1267
|
+
url = BToNSString(info[1]);
|
|
1268
|
+
} else if (kind == "open-documents") {
|
|
1269
|
+
if (!info[1].IsArray()) {
|
|
1270
|
+
Napi::TypeError::New(env, "postAppleEvent('open-documents', paths): paths must be an array")
|
|
825
1271
|
.ThrowAsJavaScriptException();
|
|
1272
|
+
return env.Undefined();
|
|
1273
|
+
}
|
|
1274
|
+
paths = [NSMutableArray array];
|
|
1275
|
+
Napi::Array a = info[1].As<Napi::Array>();
|
|
1276
|
+
for (uint32_t i = 0; i < a.Length(); i++) [paths addObject:BToNSString(a.Get(i))];
|
|
1277
|
+
} else if (kind != "reopen" && kind != "quit") {
|
|
1278
|
+
Napi::TypeError::New(env, "postAppleEvent: unknown kind '" + kind + "'")
|
|
1279
|
+
.ThrowAsJavaScriptException();
|
|
1280
|
+
return env.Undefined();
|
|
1281
|
+
}
|
|
1282
|
+
__block OSErr err = noErr;
|
|
1283
|
+
CALOnUI(^{
|
|
1284
|
+
BEnsureApp();
|
|
1285
|
+
@autoreleasepool {
|
|
1286
|
+
NSAppleEventDescriptor* target =
|
|
1287
|
+
[NSAppleEventDescriptor currentProcessDescriptor];
|
|
1288
|
+
AEEventClass cls = kCoreEventClass;
|
|
1289
|
+
AEEventID id = kAEQuitApplication;
|
|
1290
|
+
NSAppleEventDescriptor* direct = nil;
|
|
1291
|
+
if (url) {
|
|
1292
|
+
cls = kInternetEventClass;
|
|
1293
|
+
id = kAEGetURL;
|
|
1294
|
+
direct = [NSAppleEventDescriptor descriptorWithString:url];
|
|
1295
|
+
} else if (paths) {
|
|
1296
|
+
id = kAEOpenDocuments;
|
|
1297
|
+
direct = [NSAppleEventDescriptor listDescriptor];
|
|
1298
|
+
for (NSString* p in paths) {
|
|
1299
|
+
NSURL* u = [NSURL fileURLWithPath:p];
|
|
1300
|
+
[direct insertDescriptor:[NSAppleEventDescriptor descriptorWithFileURL:u]
|
|
1301
|
+
atIndex:0]; // 0 appends
|
|
1302
|
+
}
|
|
1303
|
+
} else if (kind == "reopen") {
|
|
1304
|
+
id = kAEReopenApplication;
|
|
1305
|
+
}
|
|
1306
|
+
NSAppleEventDescriptor* ev =
|
|
1307
|
+
[NSAppleEventDescriptor appleEventWithEventClass:cls
|
|
1308
|
+
eventID:id
|
|
1309
|
+
targetDescriptor:target
|
|
1310
|
+
returnID:kAutoGenerateReturnID
|
|
1311
|
+
transactionID:kAnyTransactionID];
|
|
1312
|
+
if (direct) [ev setParamDescriptor:direct forKeyword:keyDirectObject];
|
|
1313
|
+
AppleEvent reply = {typeNull, NULL};
|
|
1314
|
+
// the refCon reaches raw C handlers only; AppKit's are Objective-C
|
|
1315
|
+
// methods looked up by class and id, but the parameter is non-null
|
|
1316
|
+
static char refCon;
|
|
1317
|
+
err = [[NSAppleEventManager sharedAppleEventManager]
|
|
1318
|
+
dispatchRawAppleEvent:ev.aeDesc
|
|
1319
|
+
withRawReply:&reply
|
|
1320
|
+
handlerRefCon:(SRefCon)&refCon];
|
|
1321
|
+
AEDisposeDesc(&reply);
|
|
826
1322
|
}
|
|
1323
|
+
});
|
|
1324
|
+
if (pthread_main_np() && err != noErr) {
|
|
1325
|
+
Napi::Error::New(env, "postAppleEvent: dispatch failed (" +
|
|
1326
|
+
std::to_string((int)err) + ")")
|
|
1327
|
+
.ThrowAsJavaScriptException();
|
|
827
1328
|
}
|
|
828
1329
|
return env.Undefined();
|
|
829
1330
|
}
|
|
@@ -834,8 +1335,14 @@ static Napi::Value PostAppleEvent(const Napi::CallbackInfo& info) {
|
|
|
834
1335
|
|
|
835
1336
|
static bool StatusWindowEvent(NSEvent* e);
|
|
836
1337
|
|
|
837
|
-
|
|
838
|
-
|
|
1338
|
+
// One NSEvent as a backend event. In pump mode pump2 calls this for each
|
|
1339
|
+
// event it dequeues, before [NSApp sendEvent:]; in threaded mode [NSApp run]
|
|
1340
|
+
// dequeues, and a local event monitor calls it from the top of that same
|
|
1341
|
+
// sendEvent: (measured: a monitor fires inside sendEvent:, not at dequeue).
|
|
1342
|
+
// So both modes see the events AppKit dispatches, and neither sees what a
|
|
1343
|
+
// nested tracking loop dequeues for itself.
|
|
1344
|
+
static void DispatchEvent2(NSEvent* e) {
|
|
1345
|
+
if (!CALListening()) return;
|
|
839
1346
|
const char* type = nullptr;
|
|
840
1347
|
bool mouse = false, key = false, wheel = false, crossing = false;
|
|
841
1348
|
switch (e.type) {
|
|
@@ -867,30 +1374,31 @@ static void DispatchEvent2(Napi::Env env, NSEvent* e) {
|
|
|
867
1374
|
((CALBackendView*)e.window.contentView)->lastPress_ = e;
|
|
868
1375
|
}
|
|
869
1376
|
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
1377
|
+
CALEvent ev(type);
|
|
1378
|
+
if (e.window) ev.Num("windowNumber", (double)e.window.windowNumber);
|
|
1379
|
+
if (e.window) AddWindowHandle(ev, e.window);
|
|
1380
|
+
ev.Num("time", e.timestamp * 1000.0);
|
|
1381
|
+
// queued behind another move in the same window, it replaces it
|
|
1382
|
+
if (strcmp(type, "mousemove") == 0) ev.FoldBy((double)e.window.windowNumber);
|
|
875
1383
|
|
|
876
1384
|
NSEventModifierFlags f = e.modifierFlags;
|
|
877
|
-
ev.
|
|
878
|
-
ev.
|
|
879
|
-
ev.
|
|
880
|
-
ev.
|
|
881
|
-
ev.
|
|
1385
|
+
ev.Bool("shift", (bool)(f & NSEventModifierFlagShift));
|
|
1386
|
+
ev.Bool("control", (bool)(f & NSEventModifierFlagControl));
|
|
1387
|
+
ev.Bool("option", (bool)(f & NSEventModifierFlagOption));
|
|
1388
|
+
ev.Bool("command", (bool)(f & NSEventModifierFlagCommand));
|
|
1389
|
+
ev.Bool("capsLock", (bool)(f & NSEventModifierFlagCapsLock));
|
|
882
1390
|
|
|
883
1391
|
if ((mouse || crossing) && e.window) {
|
|
884
1392
|
NSView* v = e.window.contentView;
|
|
885
1393
|
NSPoint p = [v convertPoint:e.locationInWindow fromView:nil];
|
|
886
|
-
ev.
|
|
887
|
-
ev.
|
|
1394
|
+
ev.Num("x", p.x);
|
|
1395
|
+
ev.Num("y", v.isFlipped ? p.y : v.bounds.size.height - p.y);
|
|
888
1396
|
// and the same point in global top-left coordinates, for popups
|
|
889
1397
|
NSRect r = [e.window
|
|
890
1398
|
convertRectToScreen:NSMakeRect(e.locationInWindow.x,
|
|
891
1399
|
e.locationInWindow.y, 0, 0)];
|
|
892
|
-
ev.
|
|
893
|
-
ev.
|
|
1400
|
+
ev.Num("gx", r.origin.x);
|
|
1401
|
+
ev.Num("gy", PrimaryScreenTop() - r.origin.y);
|
|
894
1402
|
}
|
|
895
1403
|
if (mouse && !wheel && !crossing) {
|
|
896
1404
|
// 0 left, 1 right, 2 middle in AppKit; X buttons are 1 left, 2 middle,
|
|
@@ -903,35 +1411,35 @@ static void DispatchEvent2(Napi::Env env, NSEvent* e) {
|
|
|
903
1411
|
e.type == NSEventTypeOtherMouseDragged) {
|
|
904
1412
|
xbutton = 0;
|
|
905
1413
|
}
|
|
906
|
-
ev.
|
|
1414
|
+
ev.Num("button", (double)xbutton);
|
|
907
1415
|
if (e.type == NSEventTypeLeftMouseDown ||
|
|
908
1416
|
e.type == NSEventTypeRightMouseDown ||
|
|
909
1417
|
e.type == NSEventTypeOtherMouseDown ||
|
|
910
1418
|
e.type == NSEventTypeLeftMouseUp ||
|
|
911
1419
|
e.type == NSEventTypeRightMouseUp ||
|
|
912
1420
|
e.type == NSEventTypeOtherMouseUp) {
|
|
913
|
-
ev.
|
|
1421
|
+
ev.Num("clickCount", (double)e.clickCount);
|
|
914
1422
|
}
|
|
915
1423
|
}
|
|
916
1424
|
if (wheel) {
|
|
917
|
-
ev.
|
|
918
|
-
ev.
|
|
919
|
-
ev.
|
|
920
|
-
ev.
|
|
1425
|
+
ev.Num("dx", e.scrollingDeltaX);
|
|
1426
|
+
ev.Num("dy", e.scrollingDeltaY);
|
|
1427
|
+
ev.Bool("precise", (bool)e.hasPreciseScrollingDeltas);
|
|
1428
|
+
ev.Bool("momentum", e.momentumPhase != NSEventPhaseNone);
|
|
921
1429
|
}
|
|
922
1430
|
if (key && e.type != NSEventTypeFlagsChanged) {
|
|
923
|
-
ev.
|
|
1431
|
+
ev.Num("keyCode", (double)e.keyCode);
|
|
924
1432
|
NSString* chars = e.characters;
|
|
925
1433
|
NSString* ignoring = e.charactersIgnoringModifiers;
|
|
926
|
-
if (chars) ev.
|
|
927
|
-
if (ignoring) ev.
|
|
1434
|
+
if (chars) ev.Str("chars", chars.UTF8String);
|
|
1435
|
+
if (ignoring) ev.Str("charsShifted", ignoring.UTF8String);
|
|
928
1436
|
if (@available(macOS 10.15, *)) {
|
|
929
1437
|
NSString* base = [e charactersByApplyingModifiers:0];
|
|
930
|
-
if (base) ev.
|
|
1438
|
+
if (base) ev.Str("charsBase", base.UTF8String);
|
|
931
1439
|
}
|
|
932
|
-
ev.
|
|
1440
|
+
ev.Bool("repeat", (bool)e.isARepeat);
|
|
933
1441
|
}
|
|
934
|
-
|
|
1442
|
+
CALEmit(std::move(ev));
|
|
935
1443
|
}
|
|
936
1444
|
|
|
937
1445
|
static Napi::Value SetBackendEventCallback(const Napi::CallbackInfo& info) {
|
|
@@ -949,20 +1457,31 @@ static Napi::Value SetBackendEventCallback(const Napi::CallbackInfo& info) {
|
|
|
949
1457
|
}
|
|
950
1458
|
|
|
951
1459
|
// notifications.mm: responses that arrived before a listener was installed
|
|
952
|
-
void CALNotificationsReplayHeld(
|
|
1460
|
+
void CALNotificationsReplayHeld();
|
|
953
1461
|
// calendars.mm: an EventKit store change from before there was a listener
|
|
954
|
-
void CALCalendarsReplayHeld(
|
|
1462
|
+
void CALCalendarsReplayHeld();
|
|
1463
|
+
|
|
1464
|
+
// What came before anyone listened, ahead of the input that follows so the
|
|
1465
|
+
// renderer hears of it first: the launch's URL (or a Dock click), then a
|
|
1466
|
+
// notification acted on before the callback existed (one that launched the
|
|
1467
|
+
// app, say), then a calendar store that changed under it. pump2 calls this
|
|
1468
|
+
// at the top of every tick; runMain as it opens the channel.
|
|
1469
|
+
void CALReplayHeldEvents() {
|
|
1470
|
+
FlushPendingAppEvents();
|
|
1471
|
+
CALNotificationsReplayHeld();
|
|
1472
|
+
CALCalendarsReplayHeld();
|
|
1473
|
+
}
|
|
955
1474
|
|
|
956
1475
|
static Napi::Value Pump2(const Napi::CallbackInfo& info) {
|
|
957
1476
|
Napi::Env env = info.Env();
|
|
1477
|
+
if (!pthread_main_np()) {
|
|
1478
|
+
Napi::Error::New(env, "pump2: the main thread's — in threaded mode [NSApp run] "
|
|
1479
|
+
"dispatches, and a worker listens through connect()")
|
|
1480
|
+
.ThrowAsJavaScriptException();
|
|
1481
|
+
return env.Undefined();
|
|
1482
|
+
}
|
|
958
1483
|
BEnsureApp();
|
|
959
|
-
|
|
960
|
-
// renderer hears of it first: the launch's URL (or a Dock click), then a
|
|
961
|
-
// notification acted on before the callback existed (one that launched
|
|
962
|
-
// the app, say), then a calendar store that changed under it
|
|
963
|
-
FlushPendingAppEvents(env);
|
|
964
|
-
CALNotificationsReplayHeld(env);
|
|
965
|
-
CALCalendarsReplayHeld(env);
|
|
1484
|
+
CALReplayHeldEvents();
|
|
966
1485
|
@autoreleasepool {
|
|
967
1486
|
while (true) {
|
|
968
1487
|
NSEvent* e = [NSApp nextEventMatchingMask:NSEventMaskAny
|
|
@@ -970,7 +1489,7 @@ static Napi::Value Pump2(const Napi::CallbackInfo& info) {
|
|
|
970
1489
|
inMode:NSDefaultRunLoopMode
|
|
971
1490
|
dequeue:YES];
|
|
972
1491
|
if (!e) break;
|
|
973
|
-
DispatchEvent2(
|
|
1492
|
+
DispatchEvent2(e);
|
|
974
1493
|
[NSApp sendEvent:e];
|
|
975
1494
|
}
|
|
976
1495
|
[CATransaction flush];
|
|
@@ -978,6 +1497,37 @@ static Napi::Value Pump2(const Napi::CallbackInfo& info) {
|
|
|
978
1497
|
return env.Undefined();
|
|
979
1498
|
}
|
|
980
1499
|
|
|
1500
|
+
// Threaded mode's side of the same stream (threaded.mm's runMain brackets
|
|
1501
|
+
// [NSApp run] with these): the local monitor that hands each dispatched
|
|
1502
|
+
// event to DispatchEvent2, and the pasteboard's change count polled into the
|
|
1503
|
+
// published state, since nothing announces another app's write. Neither is
|
|
1504
|
+
// installed in pump mode, where pump2's own loop dispatches.
|
|
1505
|
+
static id gEventMonitor = nil;
|
|
1506
|
+
static NSTimer* gPasteboardPoll = nil;
|
|
1507
|
+
|
|
1508
|
+
void CALBeginRunMode() {
|
|
1509
|
+
gEventMonitor =
|
|
1510
|
+
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskAny
|
|
1511
|
+
handler:^NSEvent*(NSEvent* e) {
|
|
1512
|
+
DispatchEvent2(e);
|
|
1513
|
+
return e;
|
|
1514
|
+
}];
|
|
1515
|
+
gPasteboardPoll = [NSTimer timerWithTimeInterval:0.25
|
|
1516
|
+
repeats:YES
|
|
1517
|
+
block:^(NSTimer*) {
|
|
1518
|
+
PublishPasteboardCount();
|
|
1519
|
+
}];
|
|
1520
|
+
gPasteboardPoll.tolerance = 0.1;
|
|
1521
|
+
[NSRunLoop.mainRunLoop addTimer:gPasteboardPoll forMode:NSRunLoopCommonModes];
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
void CALEndRunMode() {
|
|
1525
|
+
if (gEventMonitor) [NSEvent removeMonitor:gEventMonitor];
|
|
1526
|
+
gEventMonitor = nil;
|
|
1527
|
+
[gPasteboardPoll invalidate];
|
|
1528
|
+
gPasteboardPoll = nil;
|
|
1529
|
+
}
|
|
1530
|
+
|
|
981
1531
|
// ---------------------------------------------------------------------------
|
|
982
1532
|
// surfaces: CGBitmapContext with canvas-shaped drawing
|
|
983
1533
|
// ---------------------------------------------------------------------------
|
|
@@ -1381,21 +1931,17 @@ static NSString* BStrOr(Napi::Object o, const char* k, NSString* d);
|
|
|
1381
1931
|
|
|
1382
1932
|
@interface CALMenuTarget : NSObject {
|
|
1383
1933
|
@public
|
|
1384
|
-
|
|
1385
|
-
const char* source_; // "main" | "dock": which menu tree the item is in
|
|
1934
|
+
const char* source_; // "main" | "dock" | "status": which menu tree the item is in
|
|
1386
1935
|
}
|
|
1387
1936
|
- (void)activate:(NSMenuItem*)sender;
|
|
1388
1937
|
@end
|
|
1389
1938
|
@implementation CALMenuTarget
|
|
1390
1939
|
- (void)activate:(NSMenuItem*)sender {
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
ev.
|
|
1395
|
-
|
|
1396
|
-
// ids are the caller's; the two trees may allocate them independently
|
|
1397
|
-
ev.Set("menu", source_);
|
|
1398
|
-
EmitToJS(env, ev);
|
|
1940
|
+
CALEvent ev("menu-activate");
|
|
1941
|
+
ev.Num("id", (double)sender.tag);
|
|
1942
|
+
// ids are the caller's; the trees may allocate them independently
|
|
1943
|
+
ev.Str("menu", source_);
|
|
1944
|
+
CALEmit(std::move(ev));
|
|
1399
1945
|
}
|
|
1400
1946
|
@end
|
|
1401
1947
|
|
|
@@ -1404,34 +1950,85 @@ static CALMenuTarget* gMenuTarget = nil; // NSApp.mainMenu
|
|
|
1404
1950
|
static CALMenuTarget* gDockMenuTarget = nil; // the Dock menu
|
|
1405
1951
|
static CALMenuTarget* gStatusMenuTarget = nil; // status-item (tray) menus
|
|
1406
1952
|
|
|
1407
|
-
static CALMenuTarget* MenuTargetFor(
|
|
1408
|
-
CALMenuTarget* __strong* slot,
|
|
1953
|
+
static CALMenuTarget* MenuTargetFor(CALMenuTarget* __strong* slot,
|
|
1409
1954
|
const char* source) {
|
|
1410
1955
|
if (!*slot) *slot = [CALMenuTarget new];
|
|
1411
|
-
(*slot)->env_ = env;
|
|
1412
1956
|
(*slot)->source_ = source;
|
|
1413
1957
|
return *slot;
|
|
1414
1958
|
}
|
|
1415
1959
|
|
|
1416
|
-
|
|
1417
|
-
|
|
1960
|
+
// A menu spec, read on the calling thread and built into NSMenus on the UI
|
|
1961
|
+
// thread — in the call in pump mode, by a command from a worker — since an
|
|
1962
|
+
// NSMenu touched off the main thread (the main menu's above all) is an
|
|
1963
|
+
// NSInternalInconsistencyException.
|
|
1964
|
+
struct MenuItemSpec {
|
|
1965
|
+
bool separator = false;
|
|
1966
|
+
NSString* title = @"";
|
|
1967
|
+
NSInteger tag = 0;
|
|
1968
|
+
bool enabled = true, hidden = false, checked = false;
|
|
1969
|
+
NSString* key = @"";
|
|
1970
|
+
NSUInteger modifiers = NSEventModifierFlagCommand;
|
|
1971
|
+
NSString* iconName = @"";
|
|
1972
|
+
NSData* iconData = nil;
|
|
1973
|
+
bool hasItems = false; // a non-empty `items`: a submenu, not an action
|
|
1974
|
+
std::vector<MenuItemSpec> items;
|
|
1975
|
+
};
|
|
1976
|
+
|
|
1977
|
+
static std::vector<MenuItemSpec> ParseMenuItems(Napi::Array items);
|
|
1978
|
+
|
|
1979
|
+
static MenuItemSpec ParseMenuItem(Napi::Object o) {
|
|
1980
|
+
MenuItemSpec s;
|
|
1981
|
+
s.separator = BBoolOr(o, "separator", false);
|
|
1982
|
+
if (s.separator) return s;
|
|
1983
|
+
s.title = BStrOr(o, "title", @"");
|
|
1984
|
+
s.tag = (NSInteger)BNumOr(o, "id", 0);
|
|
1985
|
+
s.enabled = BBoolOr(o, "enabled", true);
|
|
1986
|
+
s.hidden = BBoolOr(o, "hidden", false);
|
|
1987
|
+
s.checked = BBoolOr(o, "checked", false);
|
|
1988
|
+
s.key = BStrOr(o, "key", @"");
|
|
1989
|
+
s.modifiers = (NSUInteger)BNumOr(o, "modifiers", NSEventModifierFlagCommand);
|
|
1990
|
+
s.iconName = BStrOr(o, "iconName", @"");
|
|
1991
|
+
if (o.Has("iconData")) {
|
|
1992
|
+
Napi::Value v = o.Get("iconData");
|
|
1993
|
+
if (v.IsBuffer()) {
|
|
1994
|
+
Napi::Buffer<uint8_t> buf = v.As<Napi::Buffer<uint8_t>>();
|
|
1995
|
+
s.iconData = [NSData dataWithBytes:buf.Data() length:buf.Length()];
|
|
1996
|
+
}
|
|
1997
|
+
}
|
|
1998
|
+
if (o.Has("items")) {
|
|
1999
|
+
Napi::Value v = o.Get("items");
|
|
2000
|
+
if (v.IsArray() && v.As<Napi::Array>().Length() > 0) {
|
|
2001
|
+
s.hasItems = true;
|
|
2002
|
+
s.items = ParseMenuItems(v.As<Napi::Array>());
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
return s;
|
|
2006
|
+
}
|
|
2007
|
+
|
|
2008
|
+
static std::vector<MenuItemSpec> ParseMenuItems(Napi::Array items) {
|
|
2009
|
+
std::vector<MenuItemSpec> out;
|
|
2010
|
+
for (uint32_t i = 0; i < items.Length(); i++) {
|
|
2011
|
+
Napi::Value v = items.Get(i);
|
|
2012
|
+
if (v.IsObject()) out.push_back(ParseMenuItem(v.As<Napi::Object>()));
|
|
2013
|
+
}
|
|
2014
|
+
return out;
|
|
2015
|
+
}
|
|
2016
|
+
|
|
2017
|
+
static NSMenu* BuildMenu(const std::vector<MenuItemSpec>& items,
|
|
2018
|
+
CALMenuTarget* target);
|
|
1418
2019
|
|
|
1419
|
-
static NSMenuItem* BuildMenuItem(
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
NSMenuItem* it = [[NSMenuItem alloc] initWithTitle:BStrOr(o, "title", @"")
|
|
2020
|
+
static NSMenuItem* BuildMenuItem(const MenuItemSpec& s, CALMenuTarget* target) {
|
|
2021
|
+
if (s.separator) return [NSMenuItem separatorItem];
|
|
2022
|
+
NSMenuItem* it = [[NSMenuItem alloc] initWithTitle:s.title
|
|
1423
2023
|
action:nil
|
|
1424
2024
|
keyEquivalent:@""];
|
|
1425
|
-
it.tag =
|
|
1426
|
-
it.enabled =
|
|
1427
|
-
it.hidden =
|
|
1428
|
-
it.state =
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
it.keyEquivalent = key;
|
|
1433
|
-
it.keyEquivalentModifierMask =
|
|
1434
|
-
(NSUInteger)BNumOr(o, "modifiers", NSEventModifierFlagCommand);
|
|
2025
|
+
it.tag = s.tag;
|
|
2026
|
+
it.enabled = s.enabled;
|
|
2027
|
+
it.hidden = s.hidden;
|
|
2028
|
+
it.state = s.checked ? NSControlStateValueOn : NSControlStateValueOff;
|
|
2029
|
+
if (s.key.length) {
|
|
2030
|
+
it.keyEquivalent = s.key;
|
|
2031
|
+
it.keyEquivalentModifierMask = s.modifiers;
|
|
1435
2032
|
}
|
|
1436
2033
|
// Icons, the serialisable pair from the dbusmenu vocabulary. `iconName`
|
|
1437
2034
|
// is read in the platform's own icon theme — SF Symbols — which renders
|
|
@@ -1439,50 +2036,34 @@ static NSMenuItem* BuildMenuItem(Napi::Env env, Napi::Object o,
|
|
|
1439
2036
|
// symbol catalogue does not know simply misses (a freedesktop name on
|
|
1440
2037
|
// its way to a Linux panel does the same in reverse). `iconData` is
|
|
1441
2038
|
// literal pixels (PNG bytes on the bus) and is the fallback.
|
|
1442
|
-
NSString* iconName = BStrOr(o, "iconName", @"");
|
|
1443
2039
|
NSImage* icon = nil;
|
|
1444
|
-
if (iconName.length) {
|
|
1445
|
-
icon = [NSImage imageWithSystemSymbolName:iconName
|
|
2040
|
+
if (s.iconName.length) {
|
|
2041
|
+
icon = [NSImage imageWithSystemSymbolName:s.iconName
|
|
1446
2042
|
accessibilityDescription:nil];
|
|
1447
2043
|
}
|
|
1448
|
-
if (!icon &&
|
|
1449
|
-
|
|
1450
|
-
if (
|
|
1451
|
-
Napi::Buffer<uint8_t> buf = v.As<Napi::Buffer<uint8_t>>();
|
|
1452
|
-
NSData* bytes = [NSData dataWithBytes:buf.Data() length:buf.Length()];
|
|
1453
|
-
icon = [[NSImage alloc] initWithData:bytes];
|
|
1454
|
-
if (icon) icon.size = NSMakeSize(16, 16);
|
|
1455
|
-
}
|
|
2044
|
+
if (!icon && s.iconData) {
|
|
2045
|
+
icon = [[NSImage alloc] initWithData:s.iconData];
|
|
2046
|
+
if (icon) icon.size = NSMakeSize(16, 16);
|
|
1456
2047
|
}
|
|
1457
2048
|
if (icon) it.image = icon;
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
sub.title = it.title;
|
|
1464
|
-
it.submenu = sub;
|
|
1465
|
-
hasChildren = true;
|
|
1466
|
-
}
|
|
1467
|
-
}
|
|
1468
|
-
if (!hasChildren) {
|
|
2049
|
+
if (s.hasItems) {
|
|
2050
|
+
NSMenu* sub = BuildMenu(s.items, target);
|
|
2051
|
+
sub.title = it.title;
|
|
2052
|
+
it.submenu = sub;
|
|
2053
|
+
} else {
|
|
1469
2054
|
it.target = target;
|
|
1470
2055
|
it.action = @selector(activate:);
|
|
1471
2056
|
}
|
|
1472
2057
|
return it;
|
|
1473
2058
|
}
|
|
1474
2059
|
|
|
1475
|
-
static NSMenu*
|
|
1476
|
-
|
|
2060
|
+
static NSMenu* BuildMenu(const std::vector<MenuItemSpec>& items,
|
|
2061
|
+
CALMenuTarget* target) {
|
|
1477
2062
|
NSMenu* m = [[NSMenu alloc] initWithTitle:@""];
|
|
1478
2063
|
// we own enabled/hidden; AppKit's validation would grey everything whose
|
|
1479
2064
|
// target it cannot interrogate
|
|
1480
2065
|
m.autoenablesItems = NO;
|
|
1481
|
-
for (
|
|
1482
|
-
Napi::Value v = items.Get(i);
|
|
1483
|
-
if (!v.IsObject()) continue;
|
|
1484
|
-
[m addItem:BuildMenuItem(env, v.As<Napi::Object>(), target)];
|
|
1485
|
-
}
|
|
2066
|
+
for (const MenuItemSpec& s : items) [m addItem:BuildMenuItem(s, target)];
|
|
1486
2067
|
return m;
|
|
1487
2068
|
}
|
|
1488
2069
|
|
|
@@ -1490,86 +2071,144 @@ static NSMenu* BuildMenuFrom(Napi::Env env, Napi::Array items,
|
|
|
1490
2071
|
// app menu (macOS shows the process name for its title regardless).
|
|
1491
2072
|
static Napi::Value SetMainMenuFn(const Napi::CallbackInfo& info) {
|
|
1492
2073
|
Napi::Env env = info.Env();
|
|
1493
|
-
BEnsureApp();
|
|
1494
|
-
CALMenuTarget* target = MenuTargetFor(env, &gMenuTarget, "main");
|
|
1495
2074
|
Napi::Array spec = info[0].As<Napi::Array>();
|
|
1496
|
-
|
|
1497
|
-
main.autoenablesItems = NO;
|
|
2075
|
+
std::vector<std::pair<NSString*, std::vector<MenuItemSpec>>> menus;
|
|
1498
2076
|
for (uint32_t i = 0; i < spec.Length(); i++) {
|
|
1499
2077
|
Napi::Value v = spec.Get(i);
|
|
1500
2078
|
if (!v.IsObject()) continue;
|
|
1501
2079
|
Napi::Object m = v.As<Napi::Object>();
|
|
1502
|
-
NSString* title = BStrOr(m, "title", @"");
|
|
1503
|
-
NSMenuItem* holder = [[NSMenuItem alloc] initWithTitle:title
|
|
1504
|
-
action:nil
|
|
1505
|
-
keyEquivalent:@""];
|
|
1506
2080
|
Napi::Value items = m.Get("items");
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
2081
|
+
menus.emplace_back(BStrOr(m, "title", @""),
|
|
2082
|
+
items.IsArray() ? ParseMenuItems(items.As<Napi::Array>())
|
|
2083
|
+
: std::vector<MenuItemSpec>());
|
|
2084
|
+
}
|
|
2085
|
+
CALOnUI(^{
|
|
2086
|
+
BEnsureApp();
|
|
2087
|
+
CALMenuTarget* target = MenuTargetFor(&gMenuTarget, "main");
|
|
2088
|
+
NSMenu* main = [[NSMenu alloc] initWithTitle:@"MainMenu"];
|
|
2089
|
+
main.autoenablesItems = NO;
|
|
2090
|
+
for (const auto& m : menus) {
|
|
2091
|
+
NSMenuItem* holder = [[NSMenuItem alloc] initWithTitle:m.first
|
|
2092
|
+
action:nil
|
|
2093
|
+
keyEquivalent:@""];
|
|
2094
|
+
NSMenu* sub = BuildMenu(m.second, target);
|
|
2095
|
+
sub.autoenablesItems = NO;
|
|
2096
|
+
sub.title = m.first;
|
|
2097
|
+
holder.submenu = sub;
|
|
2098
|
+
[main addItem:holder];
|
|
2099
|
+
}
|
|
2100
|
+
[NSApp setMainMenu:main];
|
|
2101
|
+
});
|
|
1516
2102
|
return env.Undefined();
|
|
1517
2103
|
}
|
|
1518
2104
|
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
2105
|
+
// A menu tree as plain data, read on the UI thread and made into JS on the
|
|
2106
|
+
// caller's: mainMenuInfo's shape.
|
|
2107
|
+
struct MenuInfoData {
|
|
2108
|
+
struct Item {
|
|
2109
|
+
std::string title, key;
|
|
2110
|
+
double id;
|
|
2111
|
+
bool enabled, hidden, separator, checked, hasImage;
|
|
2112
|
+
std::shared_ptr<MenuInfoData> submenu;
|
|
2113
|
+
};
|
|
2114
|
+
std::string title;
|
|
2115
|
+
std::vector<Item> items;
|
|
2116
|
+
};
|
|
2117
|
+
|
|
2118
|
+
static std::shared_ptr<MenuInfoData> MenuInfoOf(NSMenu* menu) {
|
|
2119
|
+
auto out = std::make_shared<MenuInfoData>();
|
|
2120
|
+
out->title = menu.title.UTF8String ?: "";
|
|
1523
2121
|
for (NSInteger i = 0; i < menu.numberOfItems; i++) {
|
|
1524
2122
|
NSMenuItem* it = [menu itemAtIndex:i];
|
|
2123
|
+
MenuInfoData::Item io;
|
|
2124
|
+
io.title = it.title.UTF8String ?: "";
|
|
2125
|
+
io.id = (double)it.tag;
|
|
2126
|
+
io.enabled = it.enabled;
|
|
2127
|
+
io.hidden = it.hidden;
|
|
2128
|
+
io.separator = it.separatorItem;
|
|
2129
|
+
io.checked = it.state == NSControlStateValueOn;
|
|
2130
|
+
io.hasImage = it.image != nil;
|
|
2131
|
+
io.key = it.keyEquivalent.UTF8String ?: "";
|
|
2132
|
+
if (it.submenu) io.submenu = MenuInfoOf(it.submenu);
|
|
2133
|
+
out->items.push_back(std::move(io));
|
|
2134
|
+
}
|
|
2135
|
+
return out;
|
|
2136
|
+
}
|
|
2137
|
+
|
|
2138
|
+
static Napi::Object MenuInfoValue(Napi::Env env, const MenuInfoData& m) {
|
|
2139
|
+
Napi::Object out = Napi::Object::New(env);
|
|
2140
|
+
out.Set("title", m.title);
|
|
2141
|
+
Napi::Array arr = Napi::Array::New(env, m.items.size());
|
|
2142
|
+
for (size_t i = 0; i < m.items.size(); i++) {
|
|
2143
|
+
const MenuInfoData::Item& it = m.items[i];
|
|
1525
2144
|
Napi::Object io = Napi::Object::New(env);
|
|
1526
|
-
io.Set("title",
|
|
1527
|
-
io.Set("id",
|
|
1528
|
-
io.Set("enabled",
|
|
1529
|
-
io.Set("hidden",
|
|
1530
|
-
io.Set("separator",
|
|
1531
|
-
io.Set("checked", it.
|
|
1532
|
-
io.Set("hasImage", it.
|
|
1533
|
-
io.Set("key",
|
|
1534
|
-
if (it.submenu) io.Set("submenu",
|
|
2145
|
+
io.Set("title", it.title);
|
|
2146
|
+
io.Set("id", it.id);
|
|
2147
|
+
io.Set("enabled", it.enabled);
|
|
2148
|
+
io.Set("hidden", it.hidden);
|
|
2149
|
+
io.Set("separator", it.separator);
|
|
2150
|
+
io.Set("checked", it.checked);
|
|
2151
|
+
io.Set("hasImage", it.hasImage);
|
|
2152
|
+
io.Set("key", it.key);
|
|
2153
|
+
if (it.submenu) io.Set("submenu", MenuInfoValue(env, *it.submenu));
|
|
1535
2154
|
arr.Set((uint32_t)i, io);
|
|
1536
2155
|
}
|
|
1537
2156
|
out.Set("items", arr);
|
|
1538
2157
|
return out;
|
|
1539
2158
|
}
|
|
1540
2159
|
|
|
1541
|
-
//
|
|
2160
|
+
// What a menu read answers: the tree as data, or null for no menu.
|
|
2161
|
+
static CALValueBlock MenuInfoAnswer(NSMenu* menu) {
|
|
2162
|
+
std::shared_ptr<MenuInfoData> data = menu ? MenuInfoOf(menu) : nullptr;
|
|
2163
|
+
return ^Napi::Value(Napi::Env e) {
|
|
2164
|
+
return data ? Napi::Value(MenuInfoValue(e, *data)) : Napi::Value(e.Null());
|
|
2165
|
+
};
|
|
2166
|
+
}
|
|
2167
|
+
|
|
2168
|
+
// mainMenuInfo(cb?) — the installed menu bar as data, for tests.
|
|
1542
2169
|
static Napi::Value MainMenuInfoFn(const Napi::CallbackInfo& info) {
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
2170
|
+
return CALAnswer(info, "mainMenuInfo", ^CALValueBlock {
|
|
2171
|
+
return MenuInfoAnswer([NSApp mainMenu]);
|
|
2172
|
+
});
|
|
2173
|
+
}
|
|
2174
|
+
|
|
2175
|
+
// An index path into a menu tree, read on the calling thread.
|
|
2176
|
+
static std::vector<NSInteger> MenuPathArg(Napi::Value v) {
|
|
2177
|
+
std::vector<NSInteger> path;
|
|
2178
|
+
if (!v.IsArray()) return path;
|
|
2179
|
+
Napi::Array a = v.As<Napi::Array>();
|
|
2180
|
+
for (uint32_t i = 0; i < a.Length(); i++)
|
|
2181
|
+
path.push_back((NSInteger)a.Get(i).As<Napi::Number>().Int64Value());
|
|
2182
|
+
return path;
|
|
1547
2183
|
}
|
|
1548
2184
|
|
|
1549
2185
|
// Walk a menu tree by index and fire the leaf's action the way tracking
|
|
1550
2186
|
// would (shared by the main menu and status item test hooks).
|
|
1551
|
-
static bool ActivateInMenu(NSMenu* menu,
|
|
1552
|
-
if (!menu || path.
|
|
1553
|
-
for (
|
|
1554
|
-
NSInteger i =
|
|
2187
|
+
static bool ActivateInMenu(NSMenu* menu, const std::vector<NSInteger>& path) {
|
|
2188
|
+
if (!menu || path.empty()) return false;
|
|
2189
|
+
for (size_t d = 0; d + 1 < path.size(); d++) {
|
|
2190
|
+
NSInteger i = path[d];
|
|
1555
2191
|
if (i < 0 || i >= menu.numberOfItems) return false;
|
|
1556
2192
|
menu = [menu itemAtIndex:i].submenu;
|
|
1557
2193
|
if (!menu) return false;
|
|
1558
2194
|
}
|
|
1559
|
-
NSInteger leaf = (
|
|
1560
|
-
path.Get(path.Length() - 1).As<Napi::Number>().Int64Value();
|
|
2195
|
+
NSInteger leaf = path.back();
|
|
1561
2196
|
if (leaf < 0 || leaf >= menu.numberOfItems) return false;
|
|
1562
2197
|
[menu performActionForItemAtIndex:leaf];
|
|
1563
2198
|
return true;
|
|
1564
2199
|
}
|
|
1565
2200
|
|
|
1566
|
-
|
|
1567
|
-
|
|
2201
|
+
static CALValueBlock BoolAnswer(bool ok) {
|
|
2202
|
+
return ^Napi::Value(Napi::Env e) { return Napi::Boolean::New(e, ok); };
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2205
|
+
// activateMenuItem([i, j, ...], cb?) -> bool — walk the installed bar by
|
|
2206
|
+
// index and fire the leaf's action, the way tracking would. For tests.
|
|
1568
2207
|
static Napi::Value ActivateMenuItemFn(const Napi::CallbackInfo& info) {
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
2208
|
+
std::vector<NSInteger> path = MenuPathArg(info[0]);
|
|
2209
|
+
return CALAnswer(info, "activateMenuItem", ^CALValueBlock {
|
|
2210
|
+
return BoolAnswer(ActivateInMenu([NSApp mainMenu], path));
|
|
2211
|
+
});
|
|
1573
2212
|
}
|
|
1574
2213
|
|
|
1575
2214
|
// ---------------------------------------------------------------------------
|
|
@@ -1590,10 +2229,10 @@ static Napi::Value ActivateMenuItemFn(const Napi::CallbackInfo& info) {
|
|
|
1590
2229
|
|
|
1591
2230
|
@interface CALStatusItemTarget : NSObject {
|
|
1592
2231
|
@public
|
|
1593
|
-
napi_env env_;
|
|
1594
2232
|
// the JS handle: events carry this very object, so `ev.statusItem ===
|
|
1595
2233
|
// item` holds and a renderer can key a Map on it
|
|
1596
2234
|
Napi::Reference<Napi::Value> handle_;
|
|
2235
|
+
uint64_t handleId_; // a worker's item: its CALHandle's id instead (0: none)
|
|
1597
2236
|
NSStatusItem* __weak item_;
|
|
1598
2237
|
}
|
|
1599
2238
|
- (void)click:(id)sender;
|
|
@@ -1616,35 +2255,47 @@ static void RememberStatusWindow(NSWindow* win, NSStatusItem* item) {
|
|
|
1616
2255
|
[gStatusWindows setObject:item forKey:@(win.windowNumber)];
|
|
1617
2256
|
}
|
|
1618
2257
|
|
|
1619
|
-
// The button's rect in global top-left coordinates, points
|
|
1620
|
-
|
|
1621
|
-
|
|
2258
|
+
// The button's rect in global top-left coordinates, points: x, y, width,
|
|
2259
|
+
// height. False while the button has no window.
|
|
2260
|
+
static bool StatusButtonFrame(NSStatusBarButton* btn, double out[4]) {
|
|
2261
|
+
if (!btn.window) return false;
|
|
1622
2262
|
NSRect r = [btn.window convertRectToScreen:[btn convertRect:btn.bounds
|
|
1623
2263
|
toView:nil]];
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
2264
|
+
out[0] = r.origin.x;
|
|
2265
|
+
out[1] = PrimaryScreenTop() - (r.origin.y + r.size.height);
|
|
2266
|
+
out[2] = r.size.width;
|
|
2267
|
+
out[3] = r.size.height;
|
|
2268
|
+
return true;
|
|
1628
2269
|
}
|
|
1629
2270
|
|
|
1630
2271
|
static void EmitStatusItemClick(CALStatusItemTarget* t, const char* kind,
|
|
1631
2272
|
NSEvent* e) {
|
|
1632
2273
|
NSStatusItem* item = t->item_;
|
|
1633
|
-
if (!item || !
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
ev.
|
|
2274
|
+
if (!item || !CALListening()) return;
|
|
2275
|
+
CALEvent ev("status-item-click");
|
|
2276
|
+
// The item's own handle, looked up as the event is materialized, so
|
|
2277
|
+
// `ev.statusItem === item` holds in the environment that created it.
|
|
2278
|
+
// Weak: an event still in flight never keeps a removed item's target.
|
|
2279
|
+
__weak CALStatusItemTarget* weak = t;
|
|
2280
|
+
if (t->handleId_) ev.HandleRef("statusItem", t->handleId_);
|
|
2281
|
+
else ev.Handle("statusItem", ^Napi::Value(Napi::Env env) {
|
|
2282
|
+
CALStatusItemTarget* target = weak;
|
|
2283
|
+
if (!target || target->handle_.IsEmpty() ||
|
|
2284
|
+
(napi_env)target->handle_.Env() != (napi_env)env)
|
|
2285
|
+
return env.Null();
|
|
2286
|
+
return target->handle_.Value();
|
|
2287
|
+
});
|
|
2288
|
+
ev.Str("kind", kind);
|
|
1640
2289
|
NSEventModifierFlags f = e ? e.modifierFlags : NSEvent.modifierFlags;
|
|
1641
|
-
ev.
|
|
1642
|
-
ev.
|
|
1643
|
-
ev.
|
|
1644
|
-
ev.
|
|
1645
|
-
ev.
|
|
1646
|
-
|
|
1647
|
-
|
|
2290
|
+
ev.Bool("shift", (bool)(f & NSEventModifierFlagShift));
|
|
2291
|
+
ev.Bool("control", (bool)(f & NSEventModifierFlagControl));
|
|
2292
|
+
ev.Bool("option", (bool)(f & NSEventModifierFlagOption));
|
|
2293
|
+
ev.Bool("command", (bool)(f & NSEventModifierFlagCommand));
|
|
2294
|
+
ev.Num("clickCount", (double)(e ? e.clickCount : 1));
|
|
2295
|
+
double r[4];
|
|
2296
|
+
if (StatusButtonFrame(item.button, r))
|
|
2297
|
+
ev.Num("x", r[0]).Num("y", r[1]).Num("width", r[2]).Num("height", r[3]);
|
|
2298
|
+
CALEmit(std::move(ev));
|
|
1648
2299
|
}
|
|
1649
2300
|
|
|
1650
2301
|
// Called by the pump for every mouse event that has a window: true when
|
|
@@ -1677,65 +2328,108 @@ static bool StatusWindowEvent(NSEvent* e) {
|
|
|
1677
2328
|
}
|
|
1678
2329
|
@end
|
|
1679
2330
|
|
|
1680
|
-
|
|
2331
|
+
// A length: points, 'square' or 'variable'; NAN for anything else (the
|
|
2332
|
+
// caller keeps what it had).
|
|
2333
|
+
static double StatusLengthFrom(Napi::Value v) {
|
|
1681
2334
|
if (v.IsNumber()) return v.As<Napi::Number>().DoubleValue();
|
|
1682
2335
|
if (v.IsString()) {
|
|
1683
2336
|
std::string s = v.As<Napi::String>().Utf8Value();
|
|
1684
2337
|
if (s == "square") return NSSquareStatusItemLength;
|
|
1685
2338
|
if (s == "variable") return NSVariableStatusItemLength;
|
|
1686
2339
|
}
|
|
1687
|
-
return
|
|
2340
|
+
return NAN;
|
|
2341
|
+
}
|
|
2342
|
+
|
|
2343
|
+
// A status item's props, read on the calling thread and applied on the UI
|
|
2344
|
+
// thread; only the keys present are touched, so setStatusItem's patch is a
|
|
2345
|
+
// create with fewer keys. `image`: an SF Symbol name (a template by nature —
|
|
2346
|
+
// it follows the bar's light/dark), a surface handle (its bitmap at its
|
|
2347
|
+
// scale, copied as it is at the call), or encoded image bytes (PNG and
|
|
2348
|
+
// friends). Template by default so a bitmap icon adapts the way a symbol
|
|
2349
|
+
// does; `imageTemplate: false` keeps its colours. `imageSize: [w, h]` sets
|
|
2350
|
+
// the size in points.
|
|
2351
|
+
struct StatusItemSpec {
|
|
2352
|
+
bool hasImage = false;
|
|
2353
|
+
NSString* symbol = nil;
|
|
2354
|
+
NSData* bytes = nil;
|
|
2355
|
+
id bitmap = nil; // a CGImage, owned by ARC through the bridge
|
|
2356
|
+
double bitmapW = 0, bitmapH = 0;
|
|
2357
|
+
bool hasImageSize = false;
|
|
2358
|
+
double imageW = 0, imageH = 0;
|
|
2359
|
+
bool imageTemplate = true;
|
|
2360
|
+
bool hasTitle = false, hasTooltip = false, hasLength = false;
|
|
2361
|
+
NSString* title = @"";
|
|
2362
|
+
NSString* tooltip = @"";
|
|
2363
|
+
double length = NAN;
|
|
2364
|
+
int visible = -1; // -1: not given
|
|
2365
|
+
};
|
|
2366
|
+
|
|
2367
|
+
// False with a TypeError pending (a released surface as the image).
|
|
2368
|
+
static bool ParseStatusItemSpec(Napi::Object o, StatusItemSpec* s) {
|
|
2369
|
+
if (o.Has("image")) {
|
|
2370
|
+
s->hasImage = true;
|
|
2371
|
+
Napi::Value v = o.Get("image");
|
|
2372
|
+
if (v.IsString()) {
|
|
2373
|
+
s->symbol = BToNSString(v);
|
|
2374
|
+
} else if (v.IsBuffer()) {
|
|
2375
|
+
Napi::Buffer<uint8_t> buf = v.As<Napi::Buffer<uint8_t>>();
|
|
2376
|
+
s->bytes = [NSData dataWithBytes:buf.Data() length:buf.Length()];
|
|
2377
|
+
} else if (v.IsExternal()) {
|
|
2378
|
+
CALSurface* surf = SurfaceFrom(v);
|
|
2379
|
+
if (!surf) return false; // released: the error is pending
|
|
2380
|
+
s->bitmap = (__bridge_transfer id)CGBitmapContextCreateImage(surf->ctx);
|
|
2381
|
+
s->bitmapW = surf->width / surf->scale;
|
|
2382
|
+
s->bitmapH = surf->height / surf->scale;
|
|
2383
|
+
}
|
|
2384
|
+
if (o.Has("imageSize") && o.Get("imageSize").IsArray()) {
|
|
2385
|
+
Napi::Array sz = o.Get("imageSize").As<Napi::Array>();
|
|
2386
|
+
if (sz.Length() >= 2) {
|
|
2387
|
+
s->hasImageSize = true;
|
|
2388
|
+
s->imageW = sz.Get(0u).As<Napi::Number>().DoubleValue();
|
|
2389
|
+
s->imageH = sz.Get(1u).As<Napi::Number>().DoubleValue();
|
|
2390
|
+
}
|
|
2391
|
+
}
|
|
2392
|
+
s->imageTemplate = BBoolOr(o, "imageTemplate", true);
|
|
2393
|
+
}
|
|
2394
|
+
if (o.Has("title")) {
|
|
2395
|
+
s->hasTitle = true;
|
|
2396
|
+
s->title = BStrOr(o, "title", @"");
|
|
2397
|
+
}
|
|
2398
|
+
if (o.Has("tooltip")) {
|
|
2399
|
+
s->hasTooltip = true;
|
|
2400
|
+
s->tooltip = BStrOr(o, "tooltip", @"");
|
|
2401
|
+
}
|
|
2402
|
+
if (o.Has("length")) {
|
|
2403
|
+
s->hasLength = true;
|
|
2404
|
+
s->length = StatusLengthFrom(o.Get("length"));
|
|
2405
|
+
}
|
|
2406
|
+
if (o.Has("visible")) s->visible = BBoolOr(o, "visible", true);
|
|
2407
|
+
return true;
|
|
1688
2408
|
}
|
|
1689
2409
|
|
|
1690
|
-
|
|
1691
|
-
// light/dark), a surface handle (its bitmap, at its scale), or encoded
|
|
1692
|
-
// image bytes (PNG and friends). Template by default so a bitmap icon
|
|
1693
|
-
// adapts the way a symbol does; `imageTemplate: false` keeps its colours.
|
|
1694
|
-
// `imageSize: [w, h]` sets the size in points.
|
|
1695
|
-
static NSImage* StatusImageFrom(Napi::Object o) {
|
|
1696
|
-
Napi::Value v = o.Get("image");
|
|
2410
|
+
static NSImage* StatusImageOf(const StatusItemSpec& s) {
|
|
1697
2411
|
NSImage* img = nil;
|
|
1698
|
-
if (
|
|
1699
|
-
img = [NSImage imageWithSystemSymbolName:
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
img = [[NSImage alloc]
|
|
1704
|
-
|
|
1705
|
-
} else if (v.IsExternal()) {
|
|
1706
|
-
CALSurface* s = SurfaceFrom(v);
|
|
1707
|
-
if (!s) return nil; // released: the error is pending
|
|
1708
|
-
CGImageRef cg = CGBitmapContextCreateImage(s->ctx);
|
|
1709
|
-
img = [[NSImage alloc]
|
|
1710
|
-
initWithCGImage:cg
|
|
1711
|
-
size:NSMakeSize(s->width / s->scale, s->height / s->scale)];
|
|
1712
|
-
CGImageRelease(cg);
|
|
1713
|
-
} else {
|
|
1714
|
-
return nil;
|
|
2412
|
+
if (s.symbol) {
|
|
2413
|
+
img = [NSImage imageWithSystemSymbolName:s.symbol accessibilityDescription:nil];
|
|
2414
|
+
} else if (s.bytes) {
|
|
2415
|
+
img = [[NSImage alloc] initWithData:s.bytes];
|
|
2416
|
+
} else if (s.bitmap) {
|
|
2417
|
+
img = [[NSImage alloc] initWithCGImage:(__bridge CGImageRef)s.bitmap
|
|
2418
|
+
size:NSMakeSize(s.bitmapW, s.bitmapH)];
|
|
1715
2419
|
}
|
|
1716
2420
|
if (!img) return nil;
|
|
1717
|
-
if (
|
|
1718
|
-
|
|
1719
|
-
if (sz.Length() >= 2)
|
|
1720
|
-
img.size = NSMakeSize(sz.Get(0u).As<Napi::Number>().DoubleValue(),
|
|
1721
|
-
sz.Get(1u).As<Napi::Number>().DoubleValue());
|
|
1722
|
-
}
|
|
1723
|
-
[img setTemplate:BBoolOr(o, "imageTemplate", true)];
|
|
2421
|
+
if (s.hasImageSize) img.size = NSMakeSize(s.imageW, s.imageH);
|
|
2422
|
+
[img setTemplate:s.imageTemplate];
|
|
1724
2423
|
return img;
|
|
1725
2424
|
}
|
|
1726
2425
|
|
|
1727
|
-
|
|
1728
|
-
// share this, so a patch is a create with fewer keys.
|
|
1729
|
-
static void ApplyStatusItemProps(NSStatusItem* item, Napi::Object o) {
|
|
2426
|
+
static void ApplyStatusItemSpec(NSStatusItem* item, const StatusItemSpec& s) {
|
|
1730
2427
|
NSStatusBarButton* btn = item.button;
|
|
1731
|
-
if (
|
|
1732
|
-
if (
|
|
1733
|
-
if (
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
}
|
|
1737
|
-
if (o.Has("length")) item.length = StatusLengthFrom(o.Get("length"), item.length);
|
|
1738
|
-
if (o.Has("visible")) item.visible = BBoolOr(o, "visible", true);
|
|
2428
|
+
if (s.hasImage) btn.image = StatusImageOf(s);
|
|
2429
|
+
if (s.hasTitle) btn.title = s.title;
|
|
2430
|
+
if (s.hasTooltip) btn.toolTip = s.tooltip.length ? s.tooltip : nil;
|
|
2431
|
+
if (s.hasLength && !std::isnan(s.length)) item.length = s.length;
|
|
2432
|
+
if (s.visible >= 0) item.visible = s.visible;
|
|
1739
2433
|
// image alone, title alone, or the image leading the title
|
|
1740
2434
|
bool hasImage = btn.image != nil, hasTitle = btn.title.length > 0;
|
|
1741
2435
|
btn.imagePosition = hasImage && hasTitle ? NSImageLeft
|
|
@@ -1743,65 +2437,100 @@ static void ApplyStatusItemProps(NSStatusItem* item, Napi::Object o) {
|
|
|
1743
2437
|
: NSNoImage;
|
|
1744
2438
|
}
|
|
1745
2439
|
|
|
2440
|
+
// On the UI thread, the app launched: the item, its target, and its props.
|
|
2441
|
+
static NSStatusItem* MakeStatusItem(const StatusItemSpec& spec) {
|
|
2442
|
+
CGFloat len = spec.hasLength && !std::isnan(spec.length)
|
|
2443
|
+
? spec.length
|
|
2444
|
+
: NSVariableStatusItemLength;
|
|
2445
|
+
NSStatusItem* item = [[NSStatusBar systemStatusBar] statusItemWithLength:len];
|
|
2446
|
+
CALStatusItemTarget* target = [CALStatusItemTarget new];
|
|
2447
|
+
target->item_ = item;
|
|
2448
|
+
objc_setAssociatedObject(item, &kStatusTargetKey, target,
|
|
2449
|
+
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
2450
|
+
NSStatusBarButton* btn = item.button;
|
|
2451
|
+
btn.target = target;
|
|
2452
|
+
btn.action = @selector(click:);
|
|
2453
|
+
[btn sendActionOn:(NSEventMaskLeftMouseUp | NSEventMaskRightMouseUp)];
|
|
2454
|
+
RememberStatusWindow(btn.window, item);
|
|
2455
|
+
// AppKit remembers an item's visibility across launches in user defaults
|
|
2456
|
+
// under an autosave name it generates by creation order ("Item-0"), so
|
|
2457
|
+
// an item hidden when the last process ended would come back hidden.
|
|
2458
|
+
// Visibility is the renderer's to decide: assert it at creation.
|
|
2459
|
+
item.visible = spec.visible != 0;
|
|
2460
|
+
ApplyStatusItemSpec(item, spec);
|
|
2461
|
+
return item;
|
|
2462
|
+
}
|
|
2463
|
+
|
|
1746
2464
|
// createStatusItem({ image, title, tooltip, length, visible,
|
|
1747
2465
|
// imageTemplate, imageSize }) -> handle
|
|
1748
|
-
// length: 'variable' (default) | 'square' | points.
|
|
2466
|
+
// length: 'variable' (default) | 'square' | points. From a worker the
|
|
2467
|
+
// handle is answered at the call and the item made by a command; its clicks
|
|
2468
|
+
// carry that handle, and it is held until removeStatusItem.
|
|
1749
2469
|
static Napi::Value CreateStatusItem(const Napi::CallbackInfo& info) {
|
|
1750
2470
|
Napi::Env env = info.Env();
|
|
1751
|
-
BEnsureApp();
|
|
1752
2471
|
Napi::Object o = info.Length() > 0 && info[0].IsObject()
|
|
1753
2472
|
? info[0].As<Napi::Object>()
|
|
1754
2473
|
: Napi::Object::New(env);
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
handle = BWrapRetained(env, item);
|
|
1779
|
-
target->handle_ = Napi::Reference<Napi::Value>::New(handle, 1);
|
|
1780
|
-
}
|
|
2474
|
+
StatusItemSpec spec;
|
|
2475
|
+
if (!ParseStatusItemSpec(o, &spec)) return env.Undefined();
|
|
2476
|
+
if (pthread_main_np()) {
|
|
2477
|
+
BEnsureApp();
|
|
2478
|
+
Napi::Value handle;
|
|
2479
|
+
@autoreleasepool {
|
|
2480
|
+
NSStatusItem* item = MakeStatusItem(spec);
|
|
2481
|
+
handle = BWrapRetained(env, item);
|
|
2482
|
+
StatusTargetOf(item)->handle_ = Napi::Reference<Napi::Value>::New(handle, 1);
|
|
2483
|
+
}
|
|
2484
|
+
CALUIObjectsChanged(+1);
|
|
2485
|
+
return handle;
|
|
2486
|
+
}
|
|
2487
|
+
CALHandle* h = CALNewHandle();
|
|
2488
|
+
Napi::Value handle = CALWrapHandle(env, h, true);
|
|
2489
|
+
CALPinHandle(env, h->id_, true);
|
|
2490
|
+
CALUIObjectsChanged(+1);
|
|
2491
|
+
CALOnUI(^{
|
|
2492
|
+
BEnsureApp();
|
|
2493
|
+
NSStatusItem* item = MakeStatusItem(spec);
|
|
2494
|
+
StatusTargetOf(item)->handleId_ = h->id_;
|
|
2495
|
+
h->object_ = item;
|
|
2496
|
+
});
|
|
1781
2497
|
return handle;
|
|
1782
2498
|
}
|
|
1783
2499
|
|
|
1784
|
-
//
|
|
1785
|
-
//
|
|
1786
|
-
static
|
|
2500
|
+
// A status item verb's first argument, captured for its command (a
|
|
2501
|
+
// TypeError, and nil, for anything that is not a handle).
|
|
2502
|
+
static id StatusTargetArg(Napi::Value v) {
|
|
1787
2503
|
if (!v.IsExternal()) {
|
|
1788
2504
|
Napi::TypeError::New(v.Env(), "expected a status item handle")
|
|
1789
2505
|
.ThrowAsJavaScriptException();
|
|
1790
2506
|
return nil;
|
|
1791
2507
|
}
|
|
1792
|
-
|
|
1793
|
-
|
|
2508
|
+
return CALHandleTarget(v);
|
|
2509
|
+
}
|
|
2510
|
+
|
|
2511
|
+
// On the UI thread: the item a captured target names, or nil when it was
|
|
2512
|
+
// removed (or not made yet) — every verb after removeStatusItem is a no-op,
|
|
2513
|
+
// never a crash.
|
|
2514
|
+
static NSStatusItem* ResolveStatusItem(id target) {
|
|
2515
|
+
NSStatusItem* item = CALResolve(target);
|
|
2516
|
+
return item && StatusTargetOf(item) ? item : nil;
|
|
1794
2517
|
}
|
|
1795
2518
|
|
|
1796
2519
|
// setStatusItem(item, { image, title, tooltip, length, visible, ... })
|
|
1797
2520
|
static Napi::Value SetStatusItem(const Napi::CallbackInfo& info) {
|
|
1798
2521
|
Napi::Env env = info.Env();
|
|
1799
|
-
|
|
1800
|
-
if (!
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
2522
|
+
id target = StatusTargetArg(info[0]);
|
|
2523
|
+
if (!target || !info[1].IsObject()) return env.Undefined();
|
|
2524
|
+
StatusItemSpec spec;
|
|
2525
|
+
if (!ParseStatusItemSpec(info[1].As<Napi::Object>(), &spec)) return env.Undefined();
|
|
2526
|
+
CALOnUI(^{
|
|
2527
|
+
NSStatusItem* item = ResolveStatusItem(target);
|
|
2528
|
+
if (!item) return;
|
|
2529
|
+
@autoreleasepool {
|
|
2530
|
+
ApplyStatusItemSpec(item, spec);
|
|
2531
|
+
RememberStatusWindow(item.button.window, item);
|
|
2532
|
+
}
|
|
2533
|
+
});
|
|
1805
2534
|
return env.Undefined();
|
|
1806
2535
|
}
|
|
1807
2536
|
|
|
@@ -1812,74 +2541,96 @@ static Napi::Value SetStatusItem(const Napi::CallbackInfo& info) {
|
|
|
1812
2541
|
// returns the item to click events.
|
|
1813
2542
|
static Napi::Value SetStatusItemMenu(const Napi::CallbackInfo& info) {
|
|
1814
2543
|
Napi::Env env = info.Env();
|
|
1815
|
-
|
|
1816
|
-
if (!
|
|
2544
|
+
id target = StatusTargetArg(info[0]);
|
|
2545
|
+
if (!target) return env.Undefined();
|
|
1817
2546
|
Napi::Value spec = info.Length() > 1 ? info[1] : env.Null();
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
2547
|
+
bool hasMenu = spec.IsArray();
|
|
2548
|
+
std::vector<MenuItemSpec> items;
|
|
2549
|
+
if (hasMenu) items = ParseMenuItems(spec.As<Napi::Array>());
|
|
2550
|
+
CALOnUI(^{
|
|
2551
|
+
NSStatusItem* item = ResolveStatusItem(target);
|
|
2552
|
+
if (!item) return;
|
|
2553
|
+
@autoreleasepool {
|
|
2554
|
+
item.menu = hasMenu ? BuildMenu(items, MenuTargetFor(&gStatusMenuTarget, "status"))
|
|
2555
|
+
: nil;
|
|
1825
2556
|
}
|
|
1826
|
-
}
|
|
2557
|
+
});
|
|
1827
2558
|
return env.Undefined();
|
|
1828
2559
|
}
|
|
1829
2560
|
|
|
1830
2561
|
static Napi::Value RemoveStatusItem(const Napi::CallbackInfo& info) {
|
|
1831
2562
|
Napi::Env env = info.Env();
|
|
1832
|
-
|
|
1833
|
-
if (!
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
2563
|
+
id target = StatusTargetArg(info[0]);
|
|
2564
|
+
if (!target) return env.Undefined();
|
|
2565
|
+
CALHandle* h = WindowHandleOf(target); // any handle: a worker's item
|
|
2566
|
+
if (h) {
|
|
2567
|
+
// counted out and let go at the call, once
|
|
2568
|
+
if (h->released_.exchange(true)) return env.Undefined();
|
|
2569
|
+
CALUIObjectsChanged(-1);
|
|
2570
|
+
CALPinHandle(env, h->id_, false);
|
|
2571
|
+
}
|
|
2572
|
+
CALOnUI(^{
|
|
2573
|
+
NSStatusItem* item = ResolveStatusItem(target);
|
|
2574
|
+
if (!item) return;
|
|
2575
|
+
@autoreleasepool {
|
|
2576
|
+
CALStatusItemTarget* t = StatusTargetOf(item);
|
|
2577
|
+
item.button.target = nil;
|
|
2578
|
+
item.button.action = nil;
|
|
2579
|
+
item.menu = nil;
|
|
2580
|
+
[[NSStatusBar systemStatusBar] removeStatusItem:item];
|
|
2581
|
+
objc_setAssociatedObject(item, &kStatusTargetKey, nil,
|
|
2582
|
+
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
2583
|
+
t->item_ = nil;
|
|
2584
|
+
t->handle_.Reset(); // pump mode's handle may now be collected
|
|
2585
|
+
}
|
|
2586
|
+
if (!h) CALUIObjectsChanged(-1);
|
|
2587
|
+
});
|
|
1845
2588
|
return env.Undefined();
|
|
1846
2589
|
}
|
|
1847
2590
|
|
|
1848
|
-
// statusItemInfo(item) — the item as data, for tests: what the bar
|
|
1849
|
-
// its menu in mainMenuInfo's shape, and the button's screen rect.
|
|
1850
|
-
// once removed.
|
|
2591
|
+
// statusItemInfo(item, cb?) — the item as data, for tests: what the bar
|
|
2592
|
+
// shows, its menu in mainMenuInfo's shape, and the button's screen rect.
|
|
2593
|
+
// Null once removed.
|
|
1851
2594
|
static Napi::Value StatusItemInfo(const Napi::CallbackInfo& info) {
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
r.
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
2595
|
+
id target = StatusTargetArg(info[0]);
|
|
2596
|
+
if (!target) return info.Env().Undefined();
|
|
2597
|
+
return CALAnswer(info, "statusItemInfo", ^CALValueBlock {
|
|
2598
|
+
NSStatusItem* item = ResolveStatusItem(target);
|
|
2599
|
+
if (!item) return ^Napi::Value(Napi::Env e) { return e.Null(); };
|
|
2600
|
+
NSStatusBarButton* btn = item.button;
|
|
2601
|
+
CALEvent r;
|
|
2602
|
+
r.Str("title", btn.title.UTF8String);
|
|
2603
|
+
r.Str("tooltip", btn.toolTip ? btn.toolTip.UTF8String : "");
|
|
2604
|
+
r.Bool("visible", item.visible);
|
|
2605
|
+
r.Bool("hasImage", btn.image != nil);
|
|
2606
|
+
r.Bool("imageTemplate", btn.image != nil && [btn.image isTemplate]);
|
|
2607
|
+
if (btn.image) {
|
|
2608
|
+
r.Num("imageWidth", btn.image.size.width);
|
|
2609
|
+
r.Num("imageHeight", btn.image.size.height);
|
|
2610
|
+
}
|
|
2611
|
+
if (item.length == NSVariableStatusItemLength) r.Str("length", "variable");
|
|
2612
|
+
else if (item.length == NSSquareStatusItemLength) r.Str("length", "square");
|
|
2613
|
+
else r.Num("length", item.length);
|
|
2614
|
+
r.Handle("menu", MenuInfoAnswer(item.menu));
|
|
2615
|
+
if (btn.window) r.Num("windowNumber", (double)btn.window.windowNumber);
|
|
2616
|
+
double f[4];
|
|
2617
|
+
if (StatusButtonFrame(btn, f))
|
|
2618
|
+
r.Num("x", f[0]).Num("y", f[1]).Num("width", f[2]).Num("height", f[3]);
|
|
2619
|
+
return ^Napi::Value(Napi::Env e) { return r.ToObject(e); };
|
|
2620
|
+
});
|
|
1873
2621
|
}
|
|
1874
2622
|
|
|
1875
|
-
// activateStatusItemMenuItem(item, [i, j, ...]) — for tests; a real
|
|
1876
|
-
// would track the menu in a modal loop nobody can dismiss from a
|
|
2623
|
+
// activateStatusItemMenuItem(item, [i, j, ...], cb?) — for tests; a real
|
|
2624
|
+
// click would track the menu in a modal loop nobody can dismiss from a
|
|
2625
|
+
// script.
|
|
1877
2626
|
static Napi::Value ActivateStatusItemMenuItem(const Napi::CallbackInfo& info) {
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
return
|
|
1882
|
-
|
|
2627
|
+
id target = StatusTargetArg(info[0]);
|
|
2628
|
+
if (!target) return info.Env().Undefined();
|
|
2629
|
+
std::vector<NSInteger> path = MenuPathArg(info[1]);
|
|
2630
|
+
return CALAnswer(info, "activateStatusItemMenuItem", ^CALValueBlock {
|
|
2631
|
+
NSStatusItem* item = ResolveStatusItem(target);
|
|
2632
|
+
return BoolAnswer(item && ActivateInMenu(item.menu, path));
|
|
2633
|
+
});
|
|
1883
2634
|
}
|
|
1884
2635
|
|
|
1885
2636
|
// Two things have to happen before a mouse event posted into an item's
|
|
@@ -1910,82 +2661,93 @@ static bool WaitForStatusWindow(NSWindow* win, NSTimeInterval timeout) {
|
|
|
1910
2661
|
// what fires. Declines a left or right click while a menu is set: that
|
|
1911
2662
|
// click would open the menu and never return. Pump afterwards; the event
|
|
1912
2663
|
// arrives through the backend callback like a user's click.
|
|
2664
|
+
// Off the main thread: clickStatusItem(item, kind, cb). It waits for the
|
|
2665
|
+
// status window in a run loop of its own, so a queued one is a callout of
|
|
2666
|
+
// its own too (CALAnswer's `nested`).
|
|
1913
2667
|
static Napi::Value ClickStatusItem(const Napi::CallbackInfo& info) {
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
if (!item) return Napi::Boolean::New(env, false);
|
|
2668
|
+
id target = StatusTargetArg(info[0]);
|
|
2669
|
+
if (!target) return info.Env().Undefined();
|
|
1917
2670
|
std::string kind = info.Length() > 1 && info[1].IsString()
|
|
1918
2671
|
? info[1].As<Napi::String>().Utf8Value()
|
|
1919
2672
|
: "left";
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
NSTimeInterval now = [NSProcessInfo processInfo].systemUptime;
|
|
1935
|
-
for (NSEventType t : {down, up}) {
|
|
1936
|
-
NSEvent* e = [NSEvent mouseEventWithType:t
|
|
1937
|
-
location:p
|
|
1938
|
-
modifierFlags:0
|
|
1939
|
-
timestamp:now
|
|
1940
|
-
windowNumber:win.windowNumber
|
|
1941
|
-
context:nil
|
|
1942
|
-
eventNumber:0
|
|
1943
|
-
clickCount:1
|
|
1944
|
-
pressure:t == down ? 1 : 0];
|
|
1945
|
-
if (kind == "middle") {
|
|
1946
|
-
// mouseEventWithType: leaves the button number at 0 for the "other"
|
|
1947
|
-
// types; the middle button is number 2, which only the CGEvent
|
|
1948
|
-
// underneath can say
|
|
1949
|
-
CGEventRef cg = CGEventCreateCopy(e.CGEvent);
|
|
1950
|
-
CGEventSetIntegerValueField(cg, kCGMouseEventButtonNumber, 2);
|
|
1951
|
-
NSEvent* e2 = [NSEvent eventWithCGEvent:cg];
|
|
1952
|
-
CFRelease(cg);
|
|
1953
|
-
if (e2) e = e2;
|
|
2673
|
+
return CALAnswer(info, "clickStatusItem", ^CALValueBlock {
|
|
2674
|
+
NSStatusItem* item = ResolveStatusItem(target);
|
|
2675
|
+
if (!item) return BoolAnswer(false);
|
|
2676
|
+
if (item.menu && kind != "middle") return BoolAnswer(false);
|
|
2677
|
+
NSStatusBarButton* btn = item.button;
|
|
2678
|
+
NSWindow* win = btn.window;
|
|
2679
|
+
if (!win) return BoolAnswer(false);
|
|
2680
|
+
WaitForStatusWindow(win, 2.0);
|
|
2681
|
+
RememberStatusWindow(win, item);
|
|
2682
|
+
NSEventType down = NSEventTypeLeftMouseDown, up = NSEventTypeLeftMouseUp;
|
|
2683
|
+
if (kind == "right") {
|
|
2684
|
+
down = NSEventTypeRightMouseDown; up = NSEventTypeRightMouseUp;
|
|
2685
|
+
} else if (kind == "middle") {
|
|
2686
|
+
down = NSEventTypeOtherMouseDown; up = NSEventTypeOtherMouseUp;
|
|
1954
2687
|
}
|
|
1955
|
-
[
|
|
1956
|
-
|
|
1957
|
-
|
|
2688
|
+
NSRect r = [btn convertRect:btn.bounds toView:nil];
|
|
2689
|
+
NSPoint p = NSMakePoint(NSMidX(r), NSMidY(r));
|
|
2690
|
+
NSTimeInterval now = [NSProcessInfo processInfo].systemUptime;
|
|
2691
|
+
for (NSEventType t : {down, up}) {
|
|
2692
|
+
NSEvent* e = [NSEvent mouseEventWithType:t
|
|
2693
|
+
location:p
|
|
2694
|
+
modifierFlags:0
|
|
2695
|
+
timestamp:now
|
|
2696
|
+
windowNumber:win.windowNumber
|
|
2697
|
+
context:nil
|
|
2698
|
+
eventNumber:0
|
|
2699
|
+
clickCount:1
|
|
2700
|
+
pressure:t == down ? 1 : 0];
|
|
2701
|
+
if (kind == "middle") {
|
|
2702
|
+
// mouseEventWithType: leaves the button number at 0 for the "other"
|
|
2703
|
+
// types; the middle button is number 2, which only the CGEvent
|
|
2704
|
+
// underneath can say
|
|
2705
|
+
CGEventRef cg = CGEventCreateCopy(e.CGEvent);
|
|
2706
|
+
CGEventSetIntegerValueField(cg, kCGMouseEventButtonNumber, 2);
|
|
2707
|
+
NSEvent* e2 = [NSEvent eventWithCGEvent:cg];
|
|
2708
|
+
CFRelease(cg);
|
|
2709
|
+
if (e2) e = e2;
|
|
2710
|
+
}
|
|
2711
|
+
[NSApp postEvent:e atStart:NO];
|
|
2712
|
+
}
|
|
2713
|
+
return BoolAnswer(true);
|
|
2714
|
+
}, true);
|
|
1958
2715
|
}
|
|
1959
2716
|
|
|
1960
|
-
// snapshotStatusItem(item, file) — the item's window as the
|
|
1961
|
-
// composited it, to PNG (our own window: no screen-recording
|
|
1962
|
-
// needed, unlike `screencapture -l` from another process). For
|
|
2717
|
+
// snapshotStatusItem(item, file, cb?) — the item's window as the
|
|
2718
|
+
// WindowServer composited it, to PNG (our own window: no screen-recording
|
|
2719
|
+
// permission needed, unlike `screencapture -l` from another process). For
|
|
2720
|
+
// tests.
|
|
1963
2721
|
static Napi::Value SnapshotStatusItem(const Napi::CallbackInfo& info) {
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
2722
|
+
id target = StatusTargetArg(info[0]);
|
|
2723
|
+
if (!target) return info.Env().Undefined();
|
|
2724
|
+
NSString* path = info[1].IsString() ? BToNSString(info[1]) : nil;
|
|
2725
|
+
return CALAnswer(info, "snapshotStatusItem", ^CALValueBlock {
|
|
2726
|
+
NSStatusItem* item = ResolveStatusItem(target);
|
|
2727
|
+
NSWindow* win = item ? item.button.window : nil;
|
|
2728
|
+
if (!win || !path) return BoolAnswer(false);
|
|
2729
|
+
WaitForStatusWindow(win, 2.0);
|
|
1969
2730
|
#pragma clang diagnostic push
|
|
1970
2731
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
2732
|
+
CGImageRef img = CGWindowListCreateImage(
|
|
2733
|
+
CGRectNull, kCGWindowListOptionIncludingWindow,
|
|
2734
|
+
(CGWindowID)win.windowNumber,
|
|
2735
|
+
(CGWindowImageOption)(kCGWindowImageBoundsIgnoreFraming |
|
|
2736
|
+
kCGWindowImageBestResolution));
|
|
1976
2737
|
#pragma clang diagnostic pop
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
2738
|
+
if (!img) return BoolAnswer(false);
|
|
2739
|
+
NSURL* url = [NSURL fileURLWithPath:path];
|
|
2740
|
+
CGImageDestinationRef dst = CGImageDestinationCreateWithURL(
|
|
2741
|
+
(__bridge CFURLRef)url, CFSTR("public.png"), 1, NULL);
|
|
2742
|
+
bool ok = false;
|
|
2743
|
+
if (dst) {
|
|
2744
|
+
CGImageDestinationAddImage(dst, img, NULL);
|
|
2745
|
+
ok = CGImageDestinationFinalize(dst);
|
|
2746
|
+
CFRelease(dst);
|
|
2747
|
+
}
|
|
2748
|
+
CGImageRelease(img);
|
|
2749
|
+
return BoolAnswer(ok);
|
|
2750
|
+
}, true);
|
|
1989
2751
|
}
|
|
1990
2752
|
|
|
1991
2753
|
// setDockMenu(items | null) — the menu behind a right-click (or a press-
|
|
@@ -1997,19 +2759,19 @@ static Napi::Value SnapshotStatusItem(const Napi::CallbackInfo& info) {
|
|
|
1997
2759
|
// removes the menu (the Dock then shows only its own entries).
|
|
1998
2760
|
static Napi::Value SetDockMenuFn(const Napi::CallbackInfo& info) {
|
|
1999
2761
|
Napi::Env env = info.Env();
|
|
2000
|
-
BEnsureApp();
|
|
2001
2762
|
Napi::Value v = info[0];
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
return env.Undefined();
|
|
2005
|
-
}
|
|
2006
|
-
if (!v.IsArray()) {
|
|
2763
|
+
bool clear = v.IsNull() || v.IsUndefined();
|
|
2764
|
+
if (!clear && !v.IsArray()) {
|
|
2007
2765
|
Napi::TypeError::New(env, "setDockMenu: expected an array of items or null")
|
|
2008
2766
|
.ThrowAsJavaScriptException();
|
|
2009
2767
|
return env.Undefined();
|
|
2010
2768
|
}
|
|
2011
|
-
|
|
2012
|
-
|
|
2769
|
+
std::vector<MenuItemSpec> items;
|
|
2770
|
+
if (!clear) items = ParseMenuItems(v.As<Napi::Array>());
|
|
2771
|
+
CALOnUI(^{
|
|
2772
|
+
BEnsureApp();
|
|
2773
|
+
gDockMenu = clear ? nil : BuildMenu(items, MenuTargetFor(&gDockMenuTarget, "dock"));
|
|
2774
|
+
});
|
|
2013
2775
|
return env.Undefined();
|
|
2014
2776
|
}
|
|
2015
2777
|
|
|
@@ -2023,20 +2785,19 @@ static NSMenu* DockMenuViaDelegate() {
|
|
|
2023
2785
|
|
|
2024
2786
|
// dockMenuInfo() — the installed Dock menu as data (null when none). Tests.
|
|
2025
2787
|
static Napi::Value DockMenuInfoFn(const Napi::CallbackInfo& info) {
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
return MenuInfo(env, m);
|
|
2788
|
+
return CALAnswer(info, "dockMenuInfo", ^CALValueBlock {
|
|
2789
|
+
BEnsureApp();
|
|
2790
|
+
return MenuInfoAnswer(DockMenuViaDelegate());
|
|
2791
|
+
});
|
|
2031
2792
|
}
|
|
2032
2793
|
|
|
2033
|
-
// activateDockMenuItem([i, j, ...]) — through the Dock menu.
|
|
2794
|
+
// activateDockMenuItem([i, j, ...], cb?) — through the Dock menu.
|
|
2034
2795
|
static Napi::Value ActivateDockMenuItemFn(const Napi::CallbackInfo& info) {
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2796
|
+
std::vector<NSInteger> path = MenuPathArg(info[0]);
|
|
2797
|
+
return CALAnswer(info, "activateDockMenuItem", ^CALValueBlock {
|
|
2798
|
+
BEnsureApp();
|
|
2799
|
+
return BoolAnswer(ActivateInMenu(DockMenuViaDelegate(), path));
|
|
2800
|
+
});
|
|
2040
2801
|
}
|
|
2041
2802
|
|
|
2042
2803
|
// ---------------------------------------------------------------------------
|
|
@@ -2069,27 +2830,52 @@ static const char* PolicyName(NSApplicationActivationPolicy p) {
|
|
|
2069
2830
|
static bool ApplyActivationPolicy(NSApplicationActivationPolicy p) {
|
|
2070
2831
|
if (!gAppLaunched) {
|
|
2071
2832
|
gActivationPolicy = p;
|
|
2072
|
-
|
|
2833
|
+
gPolicyChosen = true; // over APPKIT_ACTIVATION_POLICY
|
|
2834
|
+
BEnsureApp(); // publishes it
|
|
2073
2835
|
return true;
|
|
2074
2836
|
}
|
|
2075
|
-
|
|
2837
|
+
bool ok = [NSApp setActivationPolicy:p];
|
|
2838
|
+
gPubPolicy = (int)NSApp.activationPolicy;
|
|
2839
|
+
return ok;
|
|
2076
2840
|
}
|
|
2077
2841
|
|
|
2078
|
-
// Reads `activationPolicy` off an options object
|
|
2079
|
-
// Returns false when it threw
|
|
2080
|
-
|
|
2842
|
+
// Reads `activationPolicy` off an options object on the calling thread;
|
|
2843
|
+
// throws on an unknown name. Returns false when it threw; *has says whether
|
|
2844
|
+
// there was one to apply.
|
|
2845
|
+
static bool ParsePolicyOption(Napi::Env env, Napi::Value v, bool* has,
|
|
2846
|
+
NSApplicationActivationPolicy* p) {
|
|
2847
|
+
*has = false;
|
|
2081
2848
|
if (v.IsUndefined() || v.IsNull()) return true;
|
|
2082
|
-
|
|
2083
|
-
if (!v.IsString() || !PolicyFromName(v.As<Napi::String>().Utf8Value(), &p)) {
|
|
2849
|
+
if (!v.IsString() || !PolicyFromName(v.As<Napi::String>().Utf8Value(), p)) {
|
|
2084
2850
|
Napi::RangeError::New(
|
|
2085
2851
|
env, "activationPolicy: expected 'regular' | 'accessory' | 'prohibited'")
|
|
2086
2852
|
.ThrowAsJavaScriptException();
|
|
2087
2853
|
return false;
|
|
2088
2854
|
}
|
|
2089
|
-
|
|
2855
|
+
*has = true;
|
|
2090
2856
|
return true;
|
|
2091
2857
|
}
|
|
2092
2858
|
|
|
2859
|
+
// runMain({ activationPolicy }) (threaded.mm, windowkit/appkit#64), on the
|
|
2860
|
+
// main thread: before launch it is the policy the app launches with; after,
|
|
2861
|
+
// a live switch, as setActivationPolicy is. False with a RangeError pending
|
|
2862
|
+
// for a name nobody knows.
|
|
2863
|
+
bool CALApplyActivationPolicyOption(Napi::Env env, Napi::Value v) {
|
|
2864
|
+
bool has = false;
|
|
2865
|
+
NSApplicationActivationPolicy p = NSApplicationActivationPolicyRegular;
|
|
2866
|
+
if (!ParsePolicyOption(env, v, &has, &p)) return false;
|
|
2867
|
+
if (has) ApplyActivationPolicy(p);
|
|
2868
|
+
return true;
|
|
2869
|
+
}
|
|
2870
|
+
|
|
2871
|
+
// A command's answer, for the verbs that return what AppKit said: the
|
|
2872
|
+
// answer in pump mode, where the block ran in the call; undefined from a
|
|
2873
|
+
// worker, whose command has not run yet (JS never waits on the UI thread).
|
|
2874
|
+
static Napi::Value BCommandAnswer(Napi::Env env, bool ok) {
|
|
2875
|
+
return pthread_main_np() ? Napi::Value(Napi::Boolean::New(env, ok))
|
|
2876
|
+
: env.Undefined();
|
|
2877
|
+
}
|
|
2878
|
+
|
|
2093
2879
|
// initApp({ activationPolicy? }) — the NSApplication, its activation policy
|
|
2094
2880
|
// and the app delegate (open-URL / open-file, reopen, quit and Dock-menu
|
|
2095
2881
|
// routing; see the app lifecycle section). Idempotent; every other entry
|
|
@@ -2099,11 +2885,17 @@ static bool ApplyPolicyOption(Napi::Env env, Napi::Value v) {
|
|
|
2099
2885
|
// how the app launches, afterwards it switches live.
|
|
2100
2886
|
static Napi::Value InitAppFn(const Napi::CallbackInfo& info) {
|
|
2101
2887
|
Napi::Env env = info.Env();
|
|
2888
|
+
bool has = false;
|
|
2889
|
+
NSApplicationActivationPolicy p = NSApplicationActivationPolicyRegular;
|
|
2102
2890
|
if (info.Length() > 0 && info[0].IsObject()) {
|
|
2103
2891
|
Napi::Object o = info[0].As<Napi::Object>();
|
|
2104
|
-
if (!
|
|
2892
|
+
if (!ParsePolicyOption(env, o.Get("activationPolicy"), &has, &p))
|
|
2893
|
+
return env.Undefined();
|
|
2105
2894
|
}
|
|
2106
|
-
|
|
2895
|
+
CALOnUI(^{
|
|
2896
|
+
if (has) ApplyActivationPolicy(p);
|
|
2897
|
+
BEnsureApp();
|
|
2898
|
+
});
|
|
2107
2899
|
return env.Undefined();
|
|
2108
2900
|
}
|
|
2109
2901
|
|
|
@@ -2121,7 +2913,16 @@ static Napi::Value SetActivationPolicyFn(const Napi::CallbackInfo& info) {
|
|
|
2121
2913
|
.ThrowAsJavaScriptException();
|
|
2122
2914
|
return env.Undefined();
|
|
2123
2915
|
}
|
|
2124
|
-
|
|
2916
|
+
__block bool ok = false;
|
|
2917
|
+
CALOnUI(^{ ok = ApplyActivationPolicy(p); });
|
|
2918
|
+
return BCommandAnswer(env, ok);
|
|
2919
|
+
}
|
|
2920
|
+
|
|
2921
|
+
// activationPolicy() -> 'regular' | 'accessory' | 'prohibited' — as the UI
|
|
2922
|
+
// thread last published it (the published state section). Any thread.
|
|
2923
|
+
static Napi::Value ActivationPolicyFn(const Napi::CallbackInfo& info) {
|
|
2924
|
+
return Napi::String::New(
|
|
2925
|
+
info.Env(), PolicyName((NSApplicationActivationPolicy)gPubPolicy.load()));
|
|
2125
2926
|
}
|
|
2126
2927
|
|
|
2127
2928
|
// setDockBadge(label | null) — NSDockTile.badgeLabel: the red pill on the
|
|
@@ -2129,16 +2930,20 @@ static Napi::Value SetActivationPolicyFn(const Napi::CallbackInfo& info) {
|
|
|
2129
2930
|
// undefined clears. Shows only while the tile exists (policy 'regular').
|
|
2130
2931
|
static Napi::Value SetDockBadgeFn(const Napi::CallbackInfo& info) {
|
|
2131
2932
|
Napi::Env env = info.Env();
|
|
2132
|
-
BEnsureApp();
|
|
2133
2933
|
Napi::Value v = info[0];
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
} else {
|
|
2934
|
+
NSString* label = nil;
|
|
2935
|
+
if (v.IsString() || v.IsNumber()) {
|
|
2936
|
+
label = BToNSString(v.ToString());
|
|
2937
|
+
} else if (!v.IsNull() && !v.IsUndefined()) {
|
|
2139
2938
|
Napi::TypeError::New(env, "setDockBadge: expected a string, a number or null")
|
|
2140
2939
|
.ThrowAsJavaScriptException();
|
|
2940
|
+
return env.Undefined();
|
|
2141
2941
|
}
|
|
2942
|
+
CALOnUI(^{
|
|
2943
|
+
BEnsureApp();
|
|
2944
|
+
NSApp.dockTile.badgeLabel = label;
|
|
2945
|
+
PublishBadge();
|
|
2946
|
+
});
|
|
2142
2947
|
return env.Undefined();
|
|
2143
2948
|
}
|
|
2144
2949
|
|
|
@@ -2148,9 +2953,15 @@ static Napi::Value SetDockBadgeFn(const Napi::CallbackInfo& info) {
|
|
|
2148
2953
|
// activated. AppKit ignores the request while the app is active (the id it
|
|
2149
2954
|
// returns then is still safe to cancel; appInfo().active says which case a
|
|
2150
2955
|
// caller is in). Anything else as the type is a RangeError.
|
|
2956
|
+
//
|
|
2957
|
+
// From a worker the id is the bridge's own, allocated at the call from a
|
|
2958
|
+
// range AppKit's never reaches (2^40 up) and mapped to AppKit's when the
|
|
2959
|
+
// command runs; cancelUserAttention takes either kind.
|
|
2960
|
+
static std::atomic<int64_t> gNextAttentionId{(int64_t)1 << 40};
|
|
2961
|
+
static NSMutableDictionary<NSNumber*, NSNumber*>* gAttention = nil; // UI thread
|
|
2962
|
+
|
|
2151
2963
|
static Napi::Value RequestUserAttentionFn(const Napi::CallbackInfo& info) {
|
|
2152
2964
|
Napi::Env env = info.Env();
|
|
2153
|
-
BEnsureApp();
|
|
2154
2965
|
NSRequestUserAttentionType type;
|
|
2155
2966
|
std::string s = info[0].IsString() ? info[0].As<Napi::String>().Utf8Value() : "";
|
|
2156
2967
|
if (s == "informational") type = NSInformationalRequest;
|
|
@@ -2161,7 +2972,17 @@ static Napi::Value RequestUserAttentionFn(const Napi::CallbackInfo& info) {
|
|
|
2161
2972
|
.ThrowAsJavaScriptException();
|
|
2162
2973
|
return env.Undefined();
|
|
2163
2974
|
}
|
|
2164
|
-
|
|
2975
|
+
if (pthread_main_np()) {
|
|
2976
|
+
BEnsureApp();
|
|
2977
|
+
return Napi::Number::New(env, (double)[NSApp requestUserAttention:type]);
|
|
2978
|
+
}
|
|
2979
|
+
int64_t id = gNextAttentionId++;
|
|
2980
|
+
CALOnUI(^{
|
|
2981
|
+
BEnsureApp();
|
|
2982
|
+
if (!gAttention) gAttention = [NSMutableDictionary dictionary];
|
|
2983
|
+
gAttention[@(id)] = @([NSApp requestUserAttention:type]);
|
|
2984
|
+
});
|
|
2985
|
+
return Napi::Number::New(env, (double)id);
|
|
2165
2986
|
}
|
|
2166
2987
|
|
|
2167
2988
|
// cancelUserAttention(requestId) — stop a bounce early (the message was
|
|
@@ -2173,8 +2994,13 @@ static Napi::Value CancelUserAttentionFn(const Napi::CallbackInfo& info) {
|
|
|
2173
2994
|
.ThrowAsJavaScriptException();
|
|
2174
2995
|
return env.Undefined();
|
|
2175
2996
|
}
|
|
2176
|
-
|
|
2177
|
-
|
|
2997
|
+
int64_t id = info[0].As<Napi::Number>().Int64Value();
|
|
2998
|
+
CALOnUI(^{
|
|
2999
|
+
BEnsureApp();
|
|
3000
|
+
NSNumber* mapped = gAttention[@(id)];
|
|
3001
|
+
if (mapped) [gAttention removeObjectForKey:@(id)];
|
|
3002
|
+
[NSApp cancelUserAttentionRequest:mapped ? mapped.integerValue : (NSInteger)id];
|
|
3003
|
+
});
|
|
2178
3004
|
return env.Undefined();
|
|
2179
3005
|
}
|
|
2180
3006
|
|
|
@@ -2226,17 +3052,39 @@ static Napi::Value SetAppNameFn(const Napi::CallbackInfo& info) {
|
|
|
2226
3052
|
.ThrowAsJavaScriptException();
|
|
2227
3053
|
return env.Undefined();
|
|
2228
3054
|
}
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
3055
|
+
NSString* name = BToNSString(info[0]);
|
|
3056
|
+
__block bool ok = false;
|
|
3057
|
+
CALOnUI(^{
|
|
3058
|
+
BEnsureApp();
|
|
3059
|
+
ok = !BundleDeclaresName() && SetLaunchServicesDisplayName(name);
|
|
3060
|
+
PublishName();
|
|
3061
|
+
});
|
|
3062
|
+
return BCommandAnswer(env, ok);
|
|
2233
3063
|
}
|
|
2234
3064
|
|
|
2235
3065
|
// appInfo() -> { activationPolicy, name, dockBadge, active } — the presence
|
|
2236
3066
|
// state read back from AppKit and LaunchServices, for tests and for a
|
|
2237
|
-
// renderer deciding whether a bounce would even be seen.
|
|
3067
|
+
// renderer deciding whether a bounce would even be seen. From a worker it is
|
|
3068
|
+
// the published copy.
|
|
2238
3069
|
static Napi::Value AppInfoFn(const Napi::CallbackInfo& info) {
|
|
2239
3070
|
Napi::Env env = info.Env();
|
|
3071
|
+
if (ReadPublished()) {
|
|
3072
|
+
NSString *name, *badge;
|
|
3073
|
+
{
|
|
3074
|
+
std::lock_guard<std::mutex> l(gPubMu);
|
|
3075
|
+
name = gPubName;
|
|
3076
|
+
badge = gPubBadge;
|
|
3077
|
+
}
|
|
3078
|
+
Napi::Object r = Napi::Object::New(env);
|
|
3079
|
+
r.Set("activationPolicy",
|
|
3080
|
+
PolicyName((NSApplicationActivationPolicy)gPubPolicy.load()));
|
|
3081
|
+
if (name) r.Set("name", name.UTF8String);
|
|
3082
|
+
else r.Set("name", env.Null());
|
|
3083
|
+
if (badge.length) r.Set("dockBadge", badge.UTF8String);
|
|
3084
|
+
else r.Set("dockBadge", env.Null());
|
|
3085
|
+
r.Set("active", gPubActive.load());
|
|
3086
|
+
return r;
|
|
3087
|
+
}
|
|
2240
3088
|
BEnsureApp();
|
|
2241
3089
|
Napi::Object r = Napi::Object::New(env);
|
|
2242
3090
|
r.Set("activationPolicy", PolicyName(NSApp.activationPolicy));
|
|
@@ -2264,14 +3112,17 @@ static Napi::Value AppInfoFn(const Napi::CallbackInfo& info) {
|
|
|
2264
3112
|
// behalf so nothing is dropped the way AppleScript's `of type` dropped MIME.
|
|
2265
3113
|
// ---------------------------------------------------------------------------
|
|
2266
3114
|
|
|
2267
|
-
// What a presented panel owes JS:
|
|
2268
|
-
//
|
|
2269
|
-
// that has already answered.
|
|
3115
|
+
// What a presented panel owes JS: pump mode's callback and the env to call
|
|
3116
|
+
// it with, or a worker's threadsafe function. Hung on the panel itself so
|
|
3117
|
+
// cancelPanel can tell an open panel from one that has already answered.
|
|
2270
3118
|
@interface CALPanelPending : NSObject {
|
|
2271
3119
|
@public
|
|
2272
3120
|
napi_env env_;
|
|
2273
3121
|
Napi::FunctionReference cb_;
|
|
2274
|
-
bool
|
|
3122
|
+
bool threaded_;
|
|
3123
|
+
CALTsfn tsfn_;
|
|
3124
|
+
bool open_; // NSOpenPanel answers paths[], NSSavePanel answers a path
|
|
3125
|
+
bool modal_; // app-modal (runModal) rather than a sheet
|
|
2275
3126
|
}
|
|
2276
3127
|
@end
|
|
2277
3128
|
@implementation CALPanelPending
|
|
@@ -2279,19 +3130,26 @@ static Napi::Value AppInfoFn(const Napi::CallbackInfo& info) {
|
|
|
2279
3130
|
|
|
2280
3131
|
static char kPanelPendingKey;
|
|
2281
3132
|
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
3133
|
+
// The answer, read off the panel on the UI thread: paths[] for an open
|
|
3134
|
+
// panel, a path for a save panel, null for a cancel.
|
|
3135
|
+
static CALValueBlock PanelAnswer(NSSavePanel* panel, bool open, NSModalResponse r) {
|
|
3136
|
+
if (r != NSModalResponseOK) return ^Napi::Value(Napi::Env e) { return e.Null(); };
|
|
2285
3137
|
if (open) {
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
a
|
|
2290
|
-
|
|
3138
|
+
std::vector<std::string> paths;
|
|
3139
|
+
for (NSURL* u in ((NSOpenPanel*)panel).URLs) paths.push_back(u.path.UTF8String);
|
|
3140
|
+
return ^Napi::Value(Napi::Env e) {
|
|
3141
|
+
Napi::Array a = Napi::Array::New(e, paths.size());
|
|
3142
|
+
for (size_t i = 0; i < paths.size(); i++)
|
|
3143
|
+
a.Set((uint32_t)i, Napi::String::New(e, paths[i]));
|
|
3144
|
+
return a;
|
|
3145
|
+
};
|
|
2291
3146
|
}
|
|
2292
3147
|
NSURL* u = panel.URL;
|
|
2293
|
-
|
|
2294
|
-
|
|
3148
|
+
bool has = u != nil;
|
|
3149
|
+
std::string path = has ? u.path.UTF8String : "";
|
|
3150
|
+
return ^Napi::Value(Napi::Env e) {
|
|
3151
|
+
return has ? Napi::Value(Napi::String::New(e, path)) : Napi::Value(e.Null());
|
|
3152
|
+
};
|
|
2295
3153
|
}
|
|
2296
3154
|
|
|
2297
3155
|
// The one place a panel answers. The pending record comes off first, so a
|
|
@@ -2301,9 +3159,14 @@ static void FinishPanel(NSSavePanel* panel, NSModalResponse r) {
|
|
|
2301
3159
|
if (!p) return;
|
|
2302
3160
|
objc_setAssociatedObject(panel, &kPanelPendingKey, nil,
|
|
2303
3161
|
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
3162
|
+
CALValueBlock answer = PanelAnswer(panel, p->open_, r);
|
|
3163
|
+
if (p->threaded_) {
|
|
3164
|
+
CALReply(p->tsfn_, answer);
|
|
3165
|
+
return;
|
|
3166
|
+
}
|
|
2304
3167
|
Napi::Env env(p->env_);
|
|
2305
3168
|
Napi::HandleScope scope(env);
|
|
2306
|
-
Napi::Value result =
|
|
3169
|
+
Napi::Value result = answer(env);
|
|
2307
3170
|
Napi::FunctionReference cb = std::move(p->cb_);
|
|
2308
3171
|
cb.Call({result});
|
|
2309
3172
|
}
|
|
@@ -2316,34 +3179,74 @@ static NSURL* PanelURLArg(Napi::Value v) {
|
|
|
2316
3179
|
return s.length ? [NSURL fileURLWithPath:s] : nil;
|
|
2317
3180
|
}
|
|
2318
3181
|
|
|
2319
|
-
//
|
|
2320
|
-
|
|
3182
|
+
// A panel's spec, read on the calling thread; the panel itself is made on
|
|
3183
|
+
// the UI thread (MakePanel).
|
|
3184
|
+
struct PanelSpec {
|
|
3185
|
+
NSString *title = nil, *message = nil, *prompt = nil, *name = nil;
|
|
3186
|
+
NSURL* directoryURL = nil;
|
|
3187
|
+
int canCreateDirectories = -1; // -1: AppKit's default
|
|
3188
|
+
NSArray<NSString*>* typeIds = nil;
|
|
3189
|
+
bool directory = false, multiple = false;
|
|
3190
|
+
};
|
|
3191
|
+
|
|
3192
|
+
static PanelSpec ParsePanelSpec(Napi::Object o, bool open) {
|
|
3193
|
+
PanelSpec s;
|
|
2321
3194
|
if (o.Has("title") && o.Get("title").IsString())
|
|
2322
|
-
|
|
3195
|
+
s.title = BToNSString(o.Get("title"));
|
|
2323
3196
|
if (o.Has("message") && o.Get("message").IsString())
|
|
2324
|
-
|
|
3197
|
+
s.message = BToNSString(o.Get("message"));
|
|
2325
3198
|
if (o.Has("prompt") && o.Get("prompt").IsString())
|
|
2326
|
-
|
|
2327
|
-
if (o.Has("directoryURL"))
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
}
|
|
2331
|
-
if (o.Has("canCreateDirectories"))
|
|
2332
|
-
panel.canCreateDirectories =
|
|
2333
|
-
BBoolOr(o, "canCreateDirectories", panel.canCreateDirectories);
|
|
3199
|
+
s.prompt = BToNSString(o.Get("prompt"));
|
|
3200
|
+
if (o.Has("directoryURL")) s.directoryURL = PanelURLArg(o.Get("directoryURL"));
|
|
3201
|
+
if (o.Has("canCreateDirectories") && o.Get("canCreateDirectories").IsBoolean())
|
|
3202
|
+
s.canCreateDirectories = o.Get("canCreateDirectories").As<Napi::Boolean>().Value();
|
|
2334
3203
|
if (o.Has("allowedContentTypes") && o.Get("allowedContentTypes").IsArray()) {
|
|
2335
3204
|
Napi::Array ids = o.Get("allowedContentTypes").As<Napi::Array>();
|
|
2336
|
-
NSMutableArray<
|
|
3205
|
+
NSMutableArray<NSString*>* strs = [NSMutableArray array];
|
|
2337
3206
|
for (uint32_t i = 0; i < ids.Length(); i++) {
|
|
2338
3207
|
Napi::Value v = ids.Get(i);
|
|
2339
|
-
if (
|
|
2340
|
-
|
|
3208
|
+
if (v.IsString()) [strs addObject:BToNSString(v)];
|
|
3209
|
+
}
|
|
3210
|
+
s.typeIds = strs;
|
|
3211
|
+
}
|
|
3212
|
+
if (open) {
|
|
3213
|
+
s.directory = BBoolOr(o, "directory", false);
|
|
3214
|
+
s.multiple = BBoolOr(o, "multiple", false);
|
|
3215
|
+
} else if (o.Has("nameFieldStringValue") && o.Get("nameFieldStringValue").IsString()) {
|
|
3216
|
+
s.name = BToNSString(o.Get("nameFieldStringValue"));
|
|
3217
|
+
}
|
|
3218
|
+
return s;
|
|
3219
|
+
}
|
|
3220
|
+
|
|
3221
|
+
// On the UI thread.
|
|
3222
|
+
static NSSavePanel* MakePanel(const PanelSpec& s, bool open) {
|
|
3223
|
+
NSSavePanel* panel;
|
|
3224
|
+
if (open) {
|
|
3225
|
+
NSOpenPanel* op = [NSOpenPanel openPanel];
|
|
3226
|
+
op.canChooseDirectories = s.directory;
|
|
3227
|
+
op.canChooseFiles = !s.directory;
|
|
3228
|
+
op.allowsMultipleSelection = s.multiple;
|
|
3229
|
+
panel = op;
|
|
3230
|
+
} else {
|
|
3231
|
+
panel = [NSSavePanel savePanel];
|
|
3232
|
+
if (s.name) panel.nameFieldStringValue = s.name;
|
|
3233
|
+
}
|
|
3234
|
+
if (s.title) panel.title = s.title;
|
|
3235
|
+
if (s.message) panel.message = s.message;
|
|
3236
|
+
if (s.prompt) panel.prompt = s.prompt;
|
|
3237
|
+
if (s.directoryURL) panel.directoryURL = s.directoryURL;
|
|
3238
|
+
if (s.canCreateDirectories >= 0) panel.canCreateDirectories = s.canCreateDirectories;
|
|
3239
|
+
if (s.typeIds) {
|
|
3240
|
+
NSMutableArray<UTType*>* types = [NSMutableArray array];
|
|
3241
|
+
for (NSString* id in s.typeIds) {
|
|
3242
|
+
UTType* t = [UTType typeWithIdentifier:id];
|
|
2341
3243
|
if (t) [types addObject:t];
|
|
2342
3244
|
}
|
|
2343
3245
|
// A list the OS recognises nothing of is no filter at all, the same as
|
|
2344
3246
|
// passing none: a panel that admits nothing helps nobody.
|
|
2345
3247
|
if (types.count) panel.allowedContentTypes = types;
|
|
2346
3248
|
}
|
|
3249
|
+
return panel;
|
|
2347
3250
|
}
|
|
2348
3251
|
|
|
2349
3252
|
// openPanel(spec, cb) / savePanel(spec, cb) -> panel handle
|
|
@@ -2364,52 +3267,78 @@ static Napi::Value PresentPanel(const Napi::CallbackInfo& info, bool open) {
|
|
|
2364
3267
|
return env.Undefined();
|
|
2365
3268
|
}
|
|
2366
3269
|
Napi::Object o = info[0].As<Napi::Object>();
|
|
2367
|
-
|
|
3270
|
+
id owner = nil;
|
|
2368
3271
|
if (o.Has("window")) {
|
|
2369
3272
|
Napi::Value w = o.Get("window");
|
|
2370
3273
|
if (w.IsExternal()) {
|
|
2371
|
-
owner =
|
|
3274
|
+
owner = CALHandleTarget(w);
|
|
2372
3275
|
} else if (!w.IsNull() && !w.IsUndefined()) {
|
|
2373
3276
|
Napi::TypeError::New(env, "window must be a window handle")
|
|
2374
3277
|
.ThrowAsJavaScriptException();
|
|
2375
3278
|
return env.Undefined();
|
|
2376
3279
|
}
|
|
2377
3280
|
}
|
|
2378
|
-
|
|
3281
|
+
PanelSpec spec = ParsePanelSpec(o, open);
|
|
2379
3282
|
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
3283
|
+
if (pthread_main_np()) {
|
|
3284
|
+
BEnsureApp();
|
|
3285
|
+
NSSavePanel* panel = MakePanel(spec, open);
|
|
3286
|
+
CALPanelPending* p = [CALPanelPending new];
|
|
3287
|
+
p->env_ = (napi_env)env;
|
|
3288
|
+
p->cb_ = Napi::Persistent(info[1].As<Napi::Function>());
|
|
3289
|
+
p->threaded_ = false;
|
|
3290
|
+
p->open_ = open;
|
|
3291
|
+
objc_setAssociatedObject(panel, &kPanelPendingKey, p,
|
|
3292
|
+
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
3293
|
+
Napi::Value handle = BWrapRetained(env, panel);
|
|
3294
|
+
NSWindow* ownerWin = owner ? CALResolve(owner) : nil;
|
|
3295
|
+
p->modal_ = !ownerWin;
|
|
3296
|
+
if (ownerWin) {
|
|
3297
|
+
[panel beginSheetModalForWindow:ownerWin
|
|
3298
|
+
completionHandler:^(NSModalResponse r) {
|
|
3299
|
+
FinishPanel(panel, r);
|
|
3300
|
+
}];
|
|
3301
|
+
} else {
|
|
3302
|
+
NSModalResponse r = [panel runModal];
|
|
3303
|
+
FinishPanel(panel, r);
|
|
3304
|
+
}
|
|
3305
|
+
return handle;
|
|
2393
3306
|
}
|
|
2394
|
-
ConfigurePanel(panel, o);
|
|
2395
3307
|
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
|
|
3308
|
+
// From a worker: the handle now, the panel from a command — an app-modal
|
|
3309
|
+
// one from a callout of its own, runModal being a nested loop — and the
|
|
3310
|
+
// answer through a threadsafe function in this environment.
|
|
3311
|
+
CALHandle* h = CALNewHandle();
|
|
3312
|
+
Napi::Value handle = CALWrapHandle(env, h, false);
|
|
3313
|
+
CALTsfn tsfn = CALReplyTo(
|
|
3314
|
+
env, info[1].As<Napi::Function>(), open ? "appkit:openPanel" : "appkit:savePanel");
|
|
3315
|
+
dispatch_block_t present = ^{
|
|
3316
|
+
BEnsureApp();
|
|
3317
|
+
NSSavePanel* panel = MakePanel(spec, open);
|
|
3318
|
+
CALPanelPending* p = [CALPanelPending new];
|
|
3319
|
+
p->threaded_ = true;
|
|
3320
|
+
p->tsfn_ = tsfn;
|
|
3321
|
+
p->open_ = open;
|
|
3322
|
+
objc_setAssociatedObject(panel, &kPanelPendingKey, p,
|
|
3323
|
+
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
|
3324
|
+
h->object_ = panel;
|
|
3325
|
+
p->modal_ = !owner;
|
|
3326
|
+
if (!owner) {
|
|
3327
|
+
FinishPanel(panel, [panel runModal]);
|
|
3328
|
+
return;
|
|
3329
|
+
}
|
|
3330
|
+
NSWindow* ownerWin = CALResolve(owner);
|
|
3331
|
+
if (!ownerWin) { // the window it was to sit on is gone
|
|
3332
|
+
FinishPanel(panel, NSModalResponseCancel);
|
|
3333
|
+
return;
|
|
3334
|
+
}
|
|
3335
|
+
[panel beginSheetModalForWindow:ownerWin
|
|
2406
3336
|
completionHandler:^(NSModalResponse r) {
|
|
2407
3337
|
FinishPanel(panel, r);
|
|
2408
3338
|
}];
|
|
2409
|
-
}
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
}
|
|
3339
|
+
};
|
|
3340
|
+
if (owner) CALOnUI(present);
|
|
3341
|
+
else CALOnUIModal(present);
|
|
2413
3342
|
return handle;
|
|
2414
3343
|
}
|
|
2415
3344
|
|
|
@@ -2421,10 +3350,12 @@ static Napi::Value SavePanelFn(const Napi::CallbackInfo& info) {
|
|
|
2421
3350
|
return PresentPanel(info, false);
|
|
2422
3351
|
}
|
|
2423
3352
|
|
|
2424
|
-
// cancelPanel(panel) -> bool — dismiss a
|
|
2425
|
-
// callback then gets null. False when the panel has already answered.
|
|
2426
|
-
//
|
|
2427
|
-
//
|
|
3353
|
+
// cancelPanel(panel) -> bool — dismiss a panel that is still up; its
|
|
3354
|
+
// callback then gets null. False when the panel has already answered. In
|
|
3355
|
+
// pump mode only a sheet can be reached (the thread that would call this
|
|
3356
|
+
// for an app-modal panel is inside runModal); from a worker either can, the
|
|
3357
|
+
// command queue draining inside runModal too — and there the answer is
|
|
3358
|
+
// undefined, the command not having run yet.
|
|
2428
3359
|
static Napi::Value CancelPanelFn(const Napi::CallbackInfo& info) {
|
|
2429
3360
|
Napi::Env env = info.Env();
|
|
2430
3361
|
if (!info[0].IsExternal()) {
|
|
@@ -2432,11 +3363,24 @@ static Napi::Value CancelPanelFn(const Napi::CallbackInfo& info) {
|
|
|
2432
3363
|
.ThrowAsJavaScriptException();
|
|
2433
3364
|
return env.Undefined();
|
|
2434
3365
|
}
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
3366
|
+
id target = CALHandleTarget(info[0]);
|
|
3367
|
+
__block bool ok = false;
|
|
3368
|
+
CALOnUI(^{
|
|
3369
|
+
NSSavePanel* panel = CALResolve(target);
|
|
3370
|
+
CALPanelPending* p = panel ? objc_getAssociatedObject(panel, &kPanelPendingKey) : nil;
|
|
3371
|
+
if (!p) return;
|
|
3372
|
+
if (p->modal_) {
|
|
3373
|
+
// the panel is hosted out of process, and its cancel: ends a sheet but
|
|
3374
|
+
// not the modal session runModal is in: that is ended as its Cancel
|
|
3375
|
+
// button would, and runModal answers the cancel
|
|
3376
|
+
[NSApp stopModalWithCode:NSModalResponseCancel];
|
|
3377
|
+
CALPostWakeEvent();
|
|
3378
|
+
} else {
|
|
3379
|
+
[panel cancel:nil];
|
|
3380
|
+
}
|
|
3381
|
+
ok = true;
|
|
3382
|
+
});
|
|
3383
|
+
return BCommandAnswer(env, ok);
|
|
2440
3384
|
}
|
|
2441
3385
|
|
|
2442
3386
|
// Every panel sheet still up on `win`, answered null. Called before the
|
|
@@ -2502,16 +3446,51 @@ struct BezelControl {
|
|
|
2502
3446
|
NSControl* view = nil;
|
|
2503
3447
|
};
|
|
2504
3448
|
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
3449
|
+
// A bezel's parameters, read on the calling thread. The cell or control is
|
|
3450
|
+
// made on the UI thread (windowkit/appkit#54): an NSView or NSControl made
|
|
3451
|
+
// on a worker drew correct pixels and then crashed the process at exit.
|
|
3452
|
+
struct BezelSpec {
|
|
3453
|
+
NSString* kind = @"push";
|
|
3454
|
+
NSString* title = @"";
|
|
3455
|
+
bool pressed = false, enabled = true, isDefault = false;
|
|
3456
|
+
int state = 0; // 0 off, 1 on
|
|
3457
|
+
double value = 0.5;
|
|
3458
|
+
NSString* controlSize = @"regular";
|
|
3459
|
+
NSString* appearance = @"system";
|
|
3460
|
+
};
|
|
3461
|
+
|
|
3462
|
+
// False with an error pending: not an object, or a kind nobody draws.
|
|
3463
|
+
static bool ParseBezelSpec(Napi::Env env, Napi::Value v, BezelSpec* s) {
|
|
3464
|
+
if (!v.IsObject()) {
|
|
3465
|
+
Napi::TypeError::New(env, "expected the control's parameters, an object")
|
|
3466
|
+
.ThrowAsJavaScriptException();
|
|
3467
|
+
return false;
|
|
3468
|
+
}
|
|
3469
|
+
Napi::Object o = v.As<Napi::Object>();
|
|
3470
|
+
s->kind = BStrOr(o, "kind", @"push");
|
|
3471
|
+
if (![@[ @"push", @"checkbox", @"radio", @"popup", @"slider", @"switch" ]
|
|
3472
|
+
containsObject:s->kind]) {
|
|
3473
|
+
Napi::Error::New(env, "unknown control kind").ThrowAsJavaScriptException();
|
|
3474
|
+
return false;
|
|
3475
|
+
}
|
|
3476
|
+
s->title = BStrOr(o, "title", @"");
|
|
3477
|
+
s->pressed = BBoolOr(o, "pressed", false);
|
|
3478
|
+
s->enabled = BBoolOr(o, "enabled", true);
|
|
3479
|
+
s->isDefault = BBoolOr(o, "isDefault", false);
|
|
3480
|
+
s->state = (int)BNumOr(o, "state", 0);
|
|
3481
|
+
s->value = BNumOr(o, "value", 0.5);
|
|
3482
|
+
s->controlSize = BStrOr(o, "controlSize", @"regular");
|
|
3483
|
+
s->appearance = BStrOr(o, "appearance", @"system");
|
|
3484
|
+
return true;
|
|
3485
|
+
}
|
|
2511
3486
|
|
|
3487
|
+
// On the UI thread.
|
|
3488
|
+
static BezelControl BuildBezel(const BezelSpec& s) {
|
|
3489
|
+
BezelControl out;
|
|
3490
|
+
NSString* kind = s.kind;
|
|
2512
3491
|
if ([kind isEqualToString:@"checkbox"] || [kind isEqualToString:@"radio"] ||
|
|
2513
3492
|
[kind isEqualToString:@"push"]) {
|
|
2514
|
-
NSButtonCell* c = [[NSButtonCell alloc] initTextCell:
|
|
3493
|
+
NSButtonCell* c = [[NSButtonCell alloc] initTextCell:s.title];
|
|
2515
3494
|
if ([kind isEqualToString:@"checkbox"]) {
|
|
2516
3495
|
c.buttonType = NSButtonTypeSwitch;
|
|
2517
3496
|
} else if ([kind isEqualToString:@"radio"]) {
|
|
@@ -2521,30 +3500,27 @@ static BezelControl BuildBezel(Napi::Env env, Napi::Object o) {
|
|
|
2521
3500
|
c.bezelStyle = NSBezelStylePush;
|
|
2522
3501
|
// the Return key equivalent is what makes AppKit fill it with the
|
|
2523
3502
|
// user's accent — the "default button" look
|
|
2524
|
-
if (
|
|
3503
|
+
if (s.isDefault) c.keyEquivalent = @"\r";
|
|
2525
3504
|
}
|
|
2526
|
-
c.state = state == 1 ? NSControlStateValueOn : NSControlStateValueOff;
|
|
3505
|
+
c.state = s.state == 1 ? NSControlStateValueOn : NSControlStateValueOff;
|
|
2527
3506
|
out.cell = c;
|
|
2528
3507
|
} else if ([kind isEqualToString:@"popup"]) {
|
|
2529
3508
|
NSPopUpButtonCell* c = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:NO];
|
|
2530
|
-
[c addItemWithTitle:
|
|
3509
|
+
[c addItemWithTitle:s.title];
|
|
2531
3510
|
out.cell = c;
|
|
2532
3511
|
} else if ([kind isEqualToString:@"slider"]) {
|
|
2533
|
-
NSSlider*
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
out.view =
|
|
2538
|
-
} else
|
|
2539
|
-
NSSwitch*
|
|
2540
|
-
|
|
2541
|
-
out.view =
|
|
2542
|
-
}
|
|
2543
|
-
|
|
2544
|
-
|
|
2545
|
-
}
|
|
2546
|
-
|
|
2547
|
-
NSString* sz = BStrOr(o, "controlSize", @"regular");
|
|
3512
|
+
NSSlider* sl = [[NSSlider alloc] init];
|
|
3513
|
+
sl.minValue = 0;
|
|
3514
|
+
sl.maxValue = 1;
|
|
3515
|
+
sl.doubleValue = s.value;
|
|
3516
|
+
out.view = sl;
|
|
3517
|
+
} else { // switch: ParseBezelSpec admits nothing else
|
|
3518
|
+
NSSwitch* sw = [[NSSwitch alloc] init];
|
|
3519
|
+
sw.state = s.state == 1 ? NSControlStateValueOn : NSControlStateValueOff;
|
|
3520
|
+
out.view = sw;
|
|
3521
|
+
}
|
|
3522
|
+
|
|
3523
|
+
NSString* sz = s.controlSize;
|
|
2548
3524
|
NSControlSize csize = NSControlSizeRegular;
|
|
2549
3525
|
if ([sz isEqualToString:@"small"]) csize = NSControlSizeSmall;
|
|
2550
3526
|
else if ([sz isEqualToString:@"mini"]) csize = NSControlSizeMini;
|
|
@@ -2554,17 +3530,16 @@ static BezelControl BuildBezel(Napi::Env env, Napi::Object o) {
|
|
|
2554
3530
|
out.cell.controlSize = csize;
|
|
2555
3531
|
out.cell.font =
|
|
2556
3532
|
[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:csize]];
|
|
2557
|
-
out.cell.enabled = enabled;
|
|
2558
|
-
out.cell.highlighted = pressed;
|
|
3533
|
+
out.cell.enabled = s.enabled;
|
|
3534
|
+
out.cell.highlighted = s.pressed;
|
|
2559
3535
|
} else if (out.view) {
|
|
2560
3536
|
out.view.controlSize = csize;
|
|
2561
|
-
out.view.enabled = enabled;
|
|
3537
|
+
out.view.enabled = s.enabled;
|
|
2562
3538
|
}
|
|
2563
3539
|
return out;
|
|
2564
3540
|
}
|
|
2565
3541
|
|
|
2566
|
-
static NSAppearance* BezelAppearance(
|
|
2567
|
-
NSString* name = BStrOr(o, "appearance", @"system");
|
|
3542
|
+
static NSAppearance* BezelAppearance(NSString* name) {
|
|
2568
3543
|
if ([name isEqualToString:@"dark"])
|
|
2569
3544
|
return [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
|
|
2570
3545
|
if ([name isEqualToString:@"light"])
|
|
@@ -2572,73 +3547,92 @@ static NSAppearance* BezelAppearance(Napi::Object o) {
|
|
|
2572
3547
|
return NSApp.effectiveAppearance;
|
|
2573
3548
|
}
|
|
2574
3549
|
|
|
2575
|
-
// measureControl({kind, controlSize, title?}) -> {width, height} in
|
|
2576
|
-
// the control's natural size, which is the size the bezel is
|
|
2577
|
-
// stretching a checkbox distorts it, so layout adopts these.
|
|
3550
|
+
// measureControl({kind, controlSize, title?}, cb?) -> {width, height} in
|
|
3551
|
+
// points — the control's natural size, which is the size the bezel is
|
|
3552
|
+
// *designed* at: stretching a checkbox distorts it, so layout adopts these.
|
|
3553
|
+
// On the main thread without a callback it answers in the call, as always;
|
|
3554
|
+
// with one — the only way off the main thread — the cell is measured on the
|
|
3555
|
+
// UI thread and cb({ width, height }) follows.
|
|
2578
3556
|
static Napi::Value MeasureControl(const Napi::CallbackInfo& info) {
|
|
2579
3557
|
Napi::Env env = info.Env();
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2596
|
-
|
|
2597
|
-
|
|
3558
|
+
BezelSpec spec;
|
|
3559
|
+
if (!ParseBezelSpec(env, info[0], &spec)) return env.Undefined();
|
|
3560
|
+
return CALAnswer(info, "measureControl", ^CALValueBlock {
|
|
3561
|
+
BEnsureApp();
|
|
3562
|
+
BezelControl c = BuildBezel(spec);
|
|
3563
|
+
double w = 0, h = 0;
|
|
3564
|
+
if (c.cell) {
|
|
3565
|
+
NSSize natural = c.cell.cellSize;
|
|
3566
|
+
w = ceil(natural.width);
|
|
3567
|
+
h = ceil(natural.height);
|
|
3568
|
+
} else if (c.view) {
|
|
3569
|
+
NSSize natural = c.view.intrinsicContentSize;
|
|
3570
|
+
w = natural.width > 0 ? ceil(natural.width) : 100;
|
|
3571
|
+
h = natural.height > 0 ? ceil(natural.height) : 22;
|
|
3572
|
+
}
|
|
3573
|
+
return ^Napi::Value(Napi::Env e) {
|
|
3574
|
+
Napi::Object r = Napi::Object::New(e);
|
|
3575
|
+
r.Set("width", w);
|
|
3576
|
+
r.Set("height", h);
|
|
3577
|
+
return r;
|
|
3578
|
+
};
|
|
3579
|
+
});
|
|
2598
3580
|
}
|
|
2599
3581
|
|
|
2600
|
-
// drawControlIntoSurface(surface, params) — render the bezel to fill
|
|
2601
|
-
// whole surface (surface px / scale = the frame in points). Clears
|
|
2602
|
-
// bezels are alpha-composited art, not opaque tiles.
|
|
3582
|
+
// drawControlIntoSurface(surface, params, cb?) — render the bezel to fill
|
|
3583
|
+
// the whole surface (surface px / scale = the frame in points). Clears
|
|
3584
|
+
// first: bezels are alpha-composited art, not opaque tiles. With a callback
|
|
3585
|
+
// (the only way off the main thread) AppKit draws on the UI thread straight
|
|
3586
|
+
// into the caller's bitmap, nothing copied, and cb() says it is done: until
|
|
3587
|
+
// then the renderer must leave the surface alone.
|
|
2603
3588
|
static Napi::Value DrawControlIntoSurface(const Napi::CallbackInfo& info) {
|
|
2604
3589
|
Napi::Env env = info.Env();
|
|
2605
|
-
BEnsureApp();
|
|
2606
3590
|
CALSurface* s = SurfaceFrom(info[0]);
|
|
2607
3591
|
if (!s) return info.Env().Undefined();
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
3592
|
+
BezelSpec spec;
|
|
3593
|
+
if (!ParseBezelSpec(env, info[1], &spec)) return env.Undefined();
|
|
3594
|
+
// held until the bezel is drawn: a release in the meantime must not free
|
|
3595
|
+
// the bitmap under AppKit
|
|
3596
|
+
id ctxKeep = (__bridge id)s->ctx;
|
|
3597
|
+
id ioKeep = s->iosurface ? (__bridge id)s->iosurface : nil;
|
|
3598
|
+
size_t pw = s->width, ph = s->height;
|
|
2612
3599
|
double scale = s->scale > 0 ? s->scale : 1;
|
|
2613
|
-
double w = s->width / scale, h = s->height / scale;
|
|
2614
3600
|
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
[
|
|
2631
|
-
[
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
3601
|
+
return CALAnswer(info, "drawControlIntoSurface", ^CALValueBlock {
|
|
3602
|
+
(void)ioKeep;
|
|
3603
|
+
BEnsureApp();
|
|
3604
|
+
CGContextRef ctx = (__bridge CGContextRef)ctxKeep;
|
|
3605
|
+
BezelControl c = BuildBezel(spec);
|
|
3606
|
+
double w = pw / scale, h = ph / scale;
|
|
3607
|
+
|
|
3608
|
+
CGContextSaveGState(ctx);
|
|
3609
|
+
// the surface's base CTM is already top-left-origin device pixels;
|
|
3610
|
+
// clear in that space, then move to points for AppKit
|
|
3611
|
+
CGContextClearRect(ctx, CGRectMake(0, 0, (CGFloat)pw, (CGFloat)ph));
|
|
3612
|
+
CGContextScaleCTM(ctx, scale, scale);
|
|
3613
|
+
|
|
3614
|
+
NSGraphicsContext* g =
|
|
3615
|
+
[NSGraphicsContext graphicsContextWithCGContext:ctx flipped:YES];
|
|
3616
|
+
[NSGraphicsContext saveGraphicsState];
|
|
3617
|
+
[NSGraphicsContext setCurrentContext:g];
|
|
3618
|
+
|
|
3619
|
+
NSAppearance* ap = BezelAppearance(spec.appearance);
|
|
3620
|
+
if (c.view) {
|
|
3621
|
+
c.view.frame = NSMakeRect(0, 0, w, h);
|
|
3622
|
+
c.view.appearance = ap;
|
|
3623
|
+
[c.view layoutSubtreeIfNeeded];
|
|
3624
|
+
[c.view displayRectIgnoringOpacity:c.view.bounds inContext:g];
|
|
3625
|
+
} else if (c.cell) {
|
|
3626
|
+
NSCell* cell = c.cell;
|
|
3627
|
+
[ap performAsCurrentDrawingAppearance:^{
|
|
3628
|
+
[cell drawWithFrame:NSMakeRect(0, 0, w, h) inView:BezelDrawView()];
|
|
3629
|
+
}];
|
|
3630
|
+
}
|
|
2638
3631
|
|
|
2639
|
-
|
|
2640
|
-
|
|
2641
|
-
|
|
3632
|
+
[NSGraphicsContext restoreGraphicsState];
|
|
3633
|
+
CGContextRestoreGState(ctx);
|
|
3634
|
+
return ^Napi::Value(Napi::Env e) { return e.Undefined(); };
|
|
3635
|
+
});
|
|
2642
3636
|
}
|
|
2643
3637
|
|
|
2644
3638
|
// --- drawing verbs. All take the surface handle first. ---------------------
|
|
@@ -3117,20 +4111,39 @@ static Napi::Value CtxGetImageData(const Napi::CallbackInfo& info) {
|
|
|
3117
4111
|
|
|
3118
4112
|
// surfaceToLayer(surface, layer) — hand the bitmap to a layer as contents.
|
|
3119
4113
|
// CGBitmapContextCreateImage is copy-on-write, so this is cheap per frame.
|
|
4114
|
+
// The image is taken in the call, on the thread that paints the surface, so
|
|
4115
|
+
// what a worker's frame shows is the bitmap as it was then, whatever is
|
|
4116
|
+
// drawn into it before the frame applies.
|
|
3120
4117
|
static Napi::Value SurfaceToLayer(const Napi::CallbackInfo& info) {
|
|
3121
4118
|
CALSurface* s = SurfaceFrom(info[0]);
|
|
3122
4119
|
if (!s) return info.Env().Undefined();
|
|
3123
|
-
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
3128
|
-
|
|
3129
|
-
|
|
3130
|
-
|
|
4120
|
+
id target = CALHandleTarget(info[1]);
|
|
4121
|
+
id img = CFBridgingRelease(CGBitmapContextCreateImage(s->ctx));
|
|
4122
|
+
double scale = s->scale;
|
|
4123
|
+
CALOnLayers(^{
|
|
4124
|
+
CALayer* L = CALResolve(target);
|
|
4125
|
+
if (!L) return;
|
|
4126
|
+
[CATransaction begin];
|
|
4127
|
+
[CATransaction setDisableActions:YES];
|
|
4128
|
+
CALNoteContentsReplaced(L, img);
|
|
4129
|
+
L.contents = img;
|
|
4130
|
+
L.contentsScale = scale;
|
|
4131
|
+
[CATransaction commit];
|
|
4132
|
+
});
|
|
3131
4133
|
return info.Env().Undefined();
|
|
3132
4134
|
}
|
|
3133
4135
|
|
|
4136
|
+
// surfaceIsInUse(surface) -> bool — whether anything (the render server
|
|
4137
|
+
// scanning it out, most often) still reads an IOSurface-backed surface:
|
|
4138
|
+
// IOSurfaceIsInUse, which is thread-safe, so a worker's renderer can poll
|
|
4139
|
+
// it before drawing into a buffer it handed a layer. False for a surface
|
|
4140
|
+
// that is not IOSurface-backed.
|
|
4141
|
+
static Napi::Value SurfaceIsInUse(const Napi::CallbackInfo& info) {
|
|
4142
|
+
CALSurface* s = SurfaceFrom(info[0]);
|
|
4143
|
+
if (!s) return info.Env().Undefined();
|
|
4144
|
+
return Napi::Boolean::New(info.Env(), s->iosurface && IOSurfaceIsInUse(s->iosurface));
|
|
4145
|
+
}
|
|
4146
|
+
|
|
3134
4147
|
// scrollSurface(surface, x, y, w, h, dx, dy) — scroll the pixels WITHIN
|
|
3135
4148
|
// the rect by (dx, dy), ntk Window.scrollRegion's exact contract: the
|
|
3136
4149
|
// destination band is rect ∩ (rect + delta), so nothing is ever written
|
|
@@ -3588,7 +4601,7 @@ static Napi::Value FontWithSize(const Napi::CallbackInfo& info) {
|
|
|
3588
4601
|
});
|
|
3589
4602
|
}
|
|
3590
4603
|
|
|
3591
|
-
// fontShapeText(font, text)
|
|
4604
|
+
// fontShapeText(font, text, { letterSpacing }?)
|
|
3592
4605
|
// -> { width, runs: [{ font: handle | null, glyphs: Uint16Array,
|
|
3593
4606
|
// positions: Float64Array x0,y0,x1,y1,…,
|
|
3594
4607
|
// advances: Float64Array }] }
|
|
@@ -3602,7 +4615,9 @@ static Napi::Value FontWithSize(const Napi::CallbackInfo& info) {
|
|
|
3602
4615
|
// right. The typesetter's whole answer for a cluster — a base with its
|
|
3603
4616
|
// marks positioned, an emoji sequence joined, a variation selector honoured
|
|
3604
4617
|
// — as ids a caller can hand to ctxDrawGlyphs beside the ids it looked up
|
|
3605
|
-
// itself; nothing here decides where a cluster goes.
|
|
4618
|
+
// itself; nothing here decides where a cluster goes. `letterSpacing` is
|
|
4619
|
+
// the points added after every character, as createLayout's span option
|
|
4620
|
+
// adds them, so a run shaped here and a paragraph laid out there agree.
|
|
3606
4621
|
static Napi::Value FontShapeText(const Napi::CallbackInfo& info) {
|
|
3607
4622
|
Napi::Env env = info.Env();
|
|
3608
4623
|
if (!BCheckFontArg(info, "fontShapeText")) return env.Undefined();
|
|
@@ -3611,9 +4626,15 @@ static Napi::Value FontShapeText(const Napi::CallbackInfo& info) {
|
|
|
3611
4626
|
Napi::Object out = Napi::Object::New(env);
|
|
3612
4627
|
Napi::Array runsOut = Napi::Array::New(env);
|
|
3613
4628
|
double width = 0;
|
|
4629
|
+
double spacing = info.Length() > 2 && info[2].IsObject()
|
|
4630
|
+
? BNumOr(info[2].As<Napi::Object>(), "letterSpacing", 0)
|
|
4631
|
+
: 0;
|
|
3614
4632
|
if (text.length > 0) {
|
|
3615
|
-
|
|
3616
|
-
|
|
4633
|
+
NSMutableDictionary* attrs = [NSMutableDictionary dictionary];
|
|
4634
|
+
attrs[(__bridge id)kCTFontAttributeName] = (__bridge id)font;
|
|
4635
|
+
if (spacing != 0 && std::isfinite(spacing)) {
|
|
4636
|
+
attrs[(__bridge id)kCTKernAttributeName] = @(spacing);
|
|
4637
|
+
}
|
|
3617
4638
|
NSAttributedString* as =
|
|
3618
4639
|
[[NSAttributedString alloc] initWithString:text attributes:attrs];
|
|
3619
4640
|
CTLineRef line =
|
|
@@ -3790,6 +4811,57 @@ static Napi::Value FontApplyVariations(const Napi::CallbackInfo& info) {
|
|
|
3790
4811
|
});
|
|
3791
4812
|
}
|
|
3792
4813
|
|
|
4814
|
+
// fontApplyFeatures(font, ['tnum'] | { tnum: true, liga: false, salt: 2 })
|
|
4815
|
+
// -> CTFont
|
|
4816
|
+
// OpenType features by tag (kCTFontOpenTypeFeatureTag, macOS 10.13): an
|
|
4817
|
+
// array turns each tag on, an object sets each tag to its value — true or
|
|
4818
|
+
// false, or a number for a feature that selects among alternates. The
|
|
4819
|
+
// settings replace any the font already carries, the way a style names its
|
|
4820
|
+
// features whole. Answers the font it was given when there is nothing to
|
|
4821
|
+
// set, so a caller can apply it unconditionally.
|
|
4822
|
+
static Napi::Value FontApplyFeatures(const Napi::CallbackInfo& info) {
|
|
4823
|
+
Napi::Env env = info.Env();
|
|
4824
|
+
if (!BCheckFontArg(info, "fontApplyFeatures")) return env.Undefined();
|
|
4825
|
+
CTFontRef base = BFontFrom(info[0]);
|
|
4826
|
+
NSMutableArray* settings = [NSMutableArray array];
|
|
4827
|
+
auto add = [&](const std::string& tag, long value) {
|
|
4828
|
+
if (tag.size() != 4) return;
|
|
4829
|
+
[settings addObject:@{
|
|
4830
|
+
(__bridge id)kCTFontOpenTypeFeatureTag :
|
|
4831
|
+
[NSString stringWithUTF8String:tag.c_str()],
|
|
4832
|
+
(__bridge id)kCTFontOpenTypeFeatureValue : @(value),
|
|
4833
|
+
}];
|
|
4834
|
+
};
|
|
4835
|
+
if (info.Length() > 1 && info[1].IsArray()) {
|
|
4836
|
+
Napi::Array tags = info[1].As<Napi::Array>();
|
|
4837
|
+
for (uint32_t i = 0; i < tags.Length(); i++) {
|
|
4838
|
+
if (tags.Get(i).IsString()) {
|
|
4839
|
+
add(tags.Get(i).As<Napi::String>().Utf8Value(), 1);
|
|
4840
|
+
}
|
|
4841
|
+
}
|
|
4842
|
+
} else if (info.Length() > 1 && info[1].IsObject()) {
|
|
4843
|
+
Napi::Object o = info[1].As<Napi::Object>();
|
|
4844
|
+
Napi::Array names = o.GetPropertyNames();
|
|
4845
|
+
for (uint32_t i = 0; i < names.Length(); i++) {
|
|
4846
|
+
std::string tag = names.Get(i).As<Napi::String>().Utf8Value();
|
|
4847
|
+
Napi::Value v = o.Get(tag.c_str());
|
|
4848
|
+
if (v.IsBoolean()) add(tag, v.As<Napi::Boolean>().Value() ? 1 : 0);
|
|
4849
|
+
else if (v.IsNumber()) add(tag, (long)v.As<Napi::Number>().Int64Value());
|
|
4850
|
+
}
|
|
4851
|
+
}
|
|
4852
|
+
if (settings.count == 0) return info[0];
|
|
4853
|
+
CTFontDescriptorRef d = CTFontDescriptorCreateWithAttributes(
|
|
4854
|
+
(__bridge CFDictionaryRef)
|
|
4855
|
+
@{(__bridge id)kCTFontFeatureSettingsAttribute : settings});
|
|
4856
|
+
CTFontRef ct =
|
|
4857
|
+
CTFontCreateCopyWithAttributes(base, CTFontGetSize(base), NULL, d);
|
|
4858
|
+
CFRelease(d);
|
|
4859
|
+
if (!ct) return info[0];
|
|
4860
|
+
return Napi::External<void>::New(env, (void*)ct, [](Napi::Env, void* d2) {
|
|
4861
|
+
CFRelease(d2);
|
|
4862
|
+
});
|
|
4863
|
+
}
|
|
4864
|
+
|
|
3793
4865
|
// listFonts({ family? , limit? }) -> [{ postScriptName, familyName,
|
|
3794
4866
|
// styleName, path }]. With a family: that family's faces, in CoreText's
|
|
3795
4867
|
// matching order. Without: every installed face (bounded by limit).
|
|
@@ -3938,6 +5010,12 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
|
|
|
3938
5010
|
NSMutableDictionary* attrs = [NSMutableDictionary dictionary];
|
|
3939
5011
|
attrs[(__bridge id)kCTFontAttributeName] = font;
|
|
3940
5012
|
attrs[NSParagraphStyleAttributeName] = para;
|
|
5013
|
+
// CSS's letter-spacing, in points: added after every character, the
|
|
5014
|
+
// last on a line included — what kCTKernAttributeName does
|
|
5015
|
+
double spacing = BNumOr(span, "letterSpacing", 0);
|
|
5016
|
+
if (spacing != 0 && std::isfinite(spacing)) {
|
|
5017
|
+
attrs[(__bridge id)kCTKernAttributeName] = @(spacing);
|
|
5018
|
+
}
|
|
3941
5019
|
if (span.Has("color") && span.Get("color").IsArray()) {
|
|
3942
5020
|
CGColorRef color = BMakeColor(span.Get("color"));
|
|
3943
5021
|
attrs[(__bridge id)kCTForegroundColorAttributeName] =
|
|
@@ -4310,26 +5388,43 @@ static Napi::Value LayoutCaret(const Napi::CallbackInfo& info) {
|
|
|
4310
5388
|
// pasteboard
|
|
4311
5389
|
// ---------------------------------------------------------------------------
|
|
4312
5390
|
|
|
5391
|
+
// The general pasteboard is the UI thread's: writes are commands, and a
|
|
5392
|
+
// read off the main thread answers through a callback.
|
|
4313
5393
|
static Napi::Value PbWriteTextFn(const Napi::CallbackInfo& info) {
|
|
4314
|
-
|
|
4315
|
-
|
|
4316
|
-
|
|
5394
|
+
NSString* text = BToNSString(info[0]);
|
|
5395
|
+
CALOnUI(^{
|
|
5396
|
+
NSPasteboard* pb = NSPasteboard.generalPasteboard;
|
|
5397
|
+
[pb clearContents];
|
|
5398
|
+
[pb setString:text forType:NSPasteboardTypeString];
|
|
5399
|
+
PublishPasteboardCount();
|
|
5400
|
+
});
|
|
4317
5401
|
return info.Env().Undefined();
|
|
4318
5402
|
}
|
|
4319
5403
|
|
|
5404
|
+
// pasteboardReadText(cb?) -> string | null
|
|
4320
5405
|
static Napi::Value PbReadTextFn(const Napi::CallbackInfo& info) {
|
|
4321
|
-
|
|
4322
|
-
|
|
4323
|
-
|
|
4324
|
-
|
|
5406
|
+
return CALAnswer(info, "pasteboardReadText", ^CALValueBlock {
|
|
5407
|
+
NSString* s =
|
|
5408
|
+
[NSPasteboard.generalPasteboard stringForType:NSPasteboardTypeString];
|
|
5409
|
+
std::string text = s ? s.UTF8String : "";
|
|
5410
|
+
bool has = s != nil;
|
|
5411
|
+
return ^Napi::Value(Napi::Env e) {
|
|
5412
|
+
return has ? Napi::Value(Napi::String::New(e, text)) : Napi::Value(e.Null());
|
|
5413
|
+
};
|
|
5414
|
+
});
|
|
4325
5415
|
}
|
|
4326
5416
|
|
|
4327
5417
|
static Napi::Value PbClearFn(const Napi::CallbackInfo& info) {
|
|
4328
|
-
|
|
5418
|
+
CALOnUI(^{
|
|
5419
|
+
[NSPasteboard.generalPasteboard clearContents];
|
|
5420
|
+
PublishPasteboardCount();
|
|
5421
|
+
});
|
|
4329
5422
|
return info.Env().Undefined();
|
|
4330
5423
|
}
|
|
4331
5424
|
|
|
4332
5425
|
static Napi::Value PbChangeCountFn(const Napi::CallbackInfo& info) {
|
|
5426
|
+
if (ReadPublished())
|
|
5427
|
+
return Napi::Number::New(info.Env(), (double)gPubPasteboardCount.load());
|
|
4333
5428
|
return Napi::Number::New(info.Env(),
|
|
4334
5429
|
(double)NSPasteboard.generalPasteboard.changeCount);
|
|
4335
5430
|
}
|
|
@@ -4340,35 +5435,16 @@ static Napi::Value PbChangeCountFn(const Napi::CallbackInfo& info) {
|
|
|
4340
5435
|
|
|
4341
5436
|
static Napi::Value ListScreens(const Napi::CallbackInfo& info) {
|
|
4342
5437
|
Napi::Env env = info.Env();
|
|
4343
|
-
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
|
|
4347
|
-
|
|
4348
|
-
|
|
4349
|
-
|
|
4350
|
-
NSRect f = s.frame, v = s.visibleFrame;
|
|
4351
|
-
o.Set("x", f.origin.x);
|
|
4352
|
-
o.Set("y", top - (f.origin.y + f.size.height));
|
|
4353
|
-
o.Set("width", f.size.width);
|
|
4354
|
-
o.Set("height", f.size.height);
|
|
4355
|
-
Napi::Object work = Napi::Object::New(env);
|
|
4356
|
-
work.Set("x", v.origin.x);
|
|
4357
|
-
work.Set("y", top - (v.origin.y + v.size.height));
|
|
4358
|
-
work.Set("width", v.size.width);
|
|
4359
|
-
work.Set("height", v.size.height);
|
|
4360
|
-
o.Set("visible", work);
|
|
4361
|
-
o.Set("scale", s.backingScaleFactor);
|
|
4362
|
-
// the panel's own refresh rate, so a renderer paces frames on the
|
|
4363
|
-
// display's period instead of assuming 60Hz on a 120Hz ProMotion
|
|
4364
|
-
// panel; 0 where the OS cannot say (before macOS 12)
|
|
4365
|
-
double fps = 0;
|
|
4366
|
-
if (@available(macOS 12.0, *)) fps = (double)s.maximumFramesPerSecond;
|
|
4367
|
-
o.Set("fps", fps);
|
|
4368
|
-
o.Set("primary", i == 0);
|
|
4369
|
-
out.Set((uint32_t)i, o);
|
|
5438
|
+
if (ReadPublished()) {
|
|
5439
|
+
std::vector<PubScreen> screens;
|
|
5440
|
+
{
|
|
5441
|
+
std::lock_guard<std::mutex> l(gPubMu);
|
|
5442
|
+
screens = gPubScreens;
|
|
5443
|
+
}
|
|
5444
|
+
return ScreensArray(env, screens);
|
|
4370
5445
|
}
|
|
4371
|
-
|
|
5446
|
+
BEnsureApp();
|
|
5447
|
+
return ScreensArray(env, ScreensNow());
|
|
4372
5448
|
}
|
|
4373
5449
|
|
|
4374
5450
|
static Napi::Value SetCursorFn(const Napi::CallbackInfo& info) {
|
|
@@ -4385,14 +5461,13 @@ static Napi::Value SetCursorFn(const Napi::CallbackInfo& info) {
|
|
|
4385
5461
|
c = NSCursor.resizeUpDownCursor;
|
|
4386
5462
|
else if (name == "not-allowed") c = NSCursor.operationNotAllowedCursor;
|
|
4387
5463
|
else c = NSCursor.arrowCursor;
|
|
4388
|
-
[c set];
|
|
5464
|
+
CALOnUI(^{ [c set]; });
|
|
4389
5465
|
return info.Env().Undefined();
|
|
4390
5466
|
}
|
|
4391
5467
|
|
|
4392
5468
|
// postKeyEvent(win, down, keyCode, chars, modifiers) — synthetic keys for
|
|
4393
5469
|
// tests, through the real pump like postMouseEvent.
|
|
4394
5470
|
static Napi::Value PostKeyEvent(const Napi::CallbackInfo& info) {
|
|
4395
|
-
NSWindow* win = BDeref<NSWindow*>(info[0]);
|
|
4396
5471
|
bool down = info[1].ToBoolean().Value();
|
|
4397
5472
|
unsigned short keyCode = (unsigned short)info[2].As<Napi::Number>().Uint32Value();
|
|
4398
5473
|
NSString* chars = info.Length() > 3 && info[3].IsString()
|
|
@@ -4406,17 +5481,19 @@ static Napi::Value PostKeyEvent(const Napi::CallbackInfo& info) {
|
|
|
4406
5481
|
if (BBoolOr(m, "option", false)) flags |= NSEventModifierFlagOption;
|
|
4407
5482
|
if (BBoolOr(m, "command", false)) flags |= NSEventModifierFlagCommand;
|
|
4408
5483
|
}
|
|
4409
|
-
|
|
4410
|
-
|
|
4411
|
-
|
|
4412
|
-
|
|
4413
|
-
|
|
4414
|
-
|
|
4415
|
-
|
|
4416
|
-
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
5484
|
+
OnWindow(info[0], ^(NSWindow* win) {
|
|
5485
|
+
NSEvent* e = [NSEvent keyEventWithType:down ? NSEventTypeKeyDown : NSEventTypeKeyUp
|
|
5486
|
+
location:NSMakePoint(0, 0)
|
|
5487
|
+
modifierFlags:flags
|
|
5488
|
+
timestamp:NSProcessInfo.processInfo.systemUptime
|
|
5489
|
+
windowNumber:win.windowNumber
|
|
5490
|
+
context:nil
|
|
5491
|
+
characters:chars
|
|
5492
|
+
charactersIgnoringModifiers:chars
|
|
5493
|
+
isARepeat:NO
|
|
5494
|
+
keyCode:keyCode];
|
|
5495
|
+
[NSApp postEvent:e atStart:NO];
|
|
5496
|
+
});
|
|
4420
5497
|
return info.Env().Undefined();
|
|
4421
5498
|
}
|
|
4422
5499
|
|
|
@@ -4426,8 +5503,7 @@ static Napi::Value PostKeyEvent(const Napi::CallbackInfo& info) {
|
|
|
4426
5503
|
// automatically, so a popup presented after its map keeps whatever shape
|
|
4427
5504
|
// AppKit guessed first (a full-frame dark square). Call after presenting.
|
|
4428
5505
|
static Napi::Value InvalidateWindowShadow(const Napi::CallbackInfo& info) {
|
|
4429
|
-
NSWindow* win
|
|
4430
|
-
[win invalidateShadow];
|
|
5506
|
+
OnWindow(info[0], ^(NSWindow* win) { [win invalidateShadow]; });
|
|
4431
5507
|
return info.Env().Undefined();
|
|
4432
5508
|
}
|
|
4433
5509
|
|
|
@@ -4480,11 +5556,10 @@ static NSDragOperation DragMaskFrom(Napi::Value v) {
|
|
|
4480
5556
|
return mask;
|
|
4481
5557
|
}
|
|
4482
5558
|
|
|
4483
|
-
static
|
|
4484
|
-
|
|
4485
|
-
uint32_t n = 0;
|
|
5559
|
+
static std::vector<std::string> DragOpsOfMask(NSDragOperation mask) {
|
|
5560
|
+
std::vector<std::string> out;
|
|
4486
5561
|
for (const auto& d : kDragOps)
|
|
4487
|
-
if (mask & d.op) out.
|
|
5562
|
+
if (mask & d.op) out.push_back(d.name);
|
|
4488
5563
|
return out;
|
|
4489
5564
|
}
|
|
4490
5565
|
|
|
@@ -4594,10 +5669,48 @@ static void WritePasteboardValue(NSPasteboardItem* item, NSString* type,
|
|
|
4594
5669
|
// shape; a drag of three files is three entries of one public.file-url
|
|
4595
5670
|
// each, which is how Finder reads them). A bare object is one item. null
|
|
4596
5671
|
// promises the representation through `provide`; without a provider it is
|
|
4597
|
-
// dropped.
|
|
4598
|
-
|
|
4599
|
-
|
|
4600
|
-
|
|
5672
|
+
// dropped. Read on the calling thread into plain data; the NSPasteboardItems
|
|
5673
|
+
// are made on the UI thread.
|
|
5674
|
+
struct PbRep {
|
|
5675
|
+
NSString* type = nil;
|
|
5676
|
+
NSString* string = nil;
|
|
5677
|
+
NSData* data = nil;
|
|
5678
|
+
bool lazy = false;
|
|
5679
|
+
};
|
|
5680
|
+
typedef std::vector<std::vector<PbRep>> PbItemsSpec;
|
|
5681
|
+
|
|
5682
|
+
// WritePasteboardValue's reading half: false for null / undefined.
|
|
5683
|
+
static bool ParsePbValue(Napi::Value v, PbRep* r) {
|
|
5684
|
+
if (v.IsNull() || v.IsUndefined()) return false;
|
|
5685
|
+
if (v.IsString()) {
|
|
5686
|
+
r->string = BToNSString(v);
|
|
5687
|
+
return true;
|
|
5688
|
+
}
|
|
5689
|
+
const void* bytes = nullptr;
|
|
5690
|
+
size_t len = 0;
|
|
5691
|
+
if (v.IsBuffer()) {
|
|
5692
|
+
Napi::Buffer<uint8_t> b = v.As<Napi::Buffer<uint8_t>>();
|
|
5693
|
+
bytes = b.Data();
|
|
5694
|
+
len = b.Length();
|
|
5695
|
+
} else if (v.IsTypedArray()) {
|
|
5696
|
+
Napi::TypedArray a = v.As<Napi::TypedArray>();
|
|
5697
|
+
bytes = (const uint8_t*)a.ArrayBuffer().Data() + a.ByteOffset();
|
|
5698
|
+
len = a.ByteLength();
|
|
5699
|
+
} else if (v.IsArrayBuffer()) {
|
|
5700
|
+
Napi::ArrayBuffer a = v.As<Napi::ArrayBuffer>();
|
|
5701
|
+
bytes = a.Data();
|
|
5702
|
+
len = a.ByteLength();
|
|
5703
|
+
} else {
|
|
5704
|
+
r->string = BToNSString(v.ToString());
|
|
5705
|
+
return true;
|
|
5706
|
+
}
|
|
5707
|
+
r->data = [NSData dataWithBytes:bytes length:len];
|
|
5708
|
+
return true;
|
|
5709
|
+
}
|
|
5710
|
+
|
|
5711
|
+
static PbItemsSpec ParsePasteboardItems(Napi::Env env, Napi::Value spec,
|
|
5712
|
+
bool lazyAllowed) {
|
|
5713
|
+
PbItemsSpec out;
|
|
4601
5714
|
Napi::Array arr;
|
|
4602
5715
|
if (spec.IsArray()) {
|
|
4603
5716
|
arr = spec.As<Napi::Array>();
|
|
@@ -4609,18 +5722,38 @@ static NSArray<NSPasteboardItem*>* BuildPasteboardItems(
|
|
|
4609
5722
|
Napi::Value v = arr.Get(i);
|
|
4610
5723
|
if (!v.IsObject()) continue;
|
|
4611
5724
|
Napi::Object o = v.As<Napi::Object>();
|
|
4612
|
-
|
|
4613
|
-
NSMutableArray<NSString*>* lazy = [NSMutableArray array];
|
|
5725
|
+
std::vector<PbRep> reps;
|
|
4614
5726
|
Napi::Array keys = o.GetPropertyNames();
|
|
4615
5727
|
for (uint32_t k = 0; k < keys.Length(); k++) {
|
|
4616
5728
|
Napi::Value key = keys.Get(k);
|
|
4617
5729
|
if (!key.IsString()) continue;
|
|
4618
|
-
|
|
4619
|
-
|
|
4620
|
-
if (
|
|
4621
|
-
if (
|
|
4622
|
-
|
|
4623
|
-
|
|
5730
|
+
PbRep r;
|
|
5731
|
+
r.type = BToNSString(key);
|
|
5732
|
+
if (!ParsePbValue(o.Get(key), &r)) {
|
|
5733
|
+
if (!lazyAllowed) continue;
|
|
5734
|
+
r.lazy = true;
|
|
5735
|
+
}
|
|
5736
|
+
reps.push_back(r);
|
|
5737
|
+
}
|
|
5738
|
+
out.push_back(std::move(reps));
|
|
5739
|
+
}
|
|
5740
|
+
return out;
|
|
5741
|
+
}
|
|
5742
|
+
|
|
5743
|
+
// On the UI thread.
|
|
5744
|
+
static NSArray<NSPasteboardItem*>* BuildPasteboardItems(const PbItemsSpec& spec,
|
|
5745
|
+
CALDragProvider* provider) {
|
|
5746
|
+
NSMutableArray<NSPasteboardItem*>* items = [NSMutableArray array];
|
|
5747
|
+
for (const std::vector<PbRep>& reps : spec) {
|
|
5748
|
+
NSPasteboardItem* item = [[NSPasteboardItem alloc] init];
|
|
5749
|
+
NSMutableArray<NSString*>* lazy = [NSMutableArray array];
|
|
5750
|
+
for (const PbRep& r : reps) {
|
|
5751
|
+
if (r.lazy) {
|
|
5752
|
+
if (provider) [lazy addObject:r.type];
|
|
5753
|
+
} else if (r.string) {
|
|
5754
|
+
[item setString:r.string forType:r.type];
|
|
5755
|
+
} else if (r.data) {
|
|
5756
|
+
[item setData:r.data forType:r.type];
|
|
4624
5757
|
}
|
|
4625
5758
|
}
|
|
4626
5759
|
if (lazy.count) [item setDataProvider:provider forTypes:lazy];
|
|
@@ -4630,22 +5763,27 @@ static NSArray<NSPasteboardItem*>* BuildPasteboardItems(
|
|
|
4630
5763
|
}
|
|
4631
5764
|
|
|
4632
5765
|
// The drag image, from either bitmap this addon deals in: `surface`, a
|
|
4633
|
-
// surface handle (the renderer's own paint, its scale known
|
|
4634
|
-
//
|
|
4635
|
-
// text.render / controls.render
|
|
4636
|
-
|
|
5766
|
+
// surface handle (the renderer's own paint, its scale known, its pixels
|
|
5767
|
+
// copied as they are at the call), or `image`, a CGImage External — or the
|
|
5768
|
+
// {image, width, height, scale} object text.render / controls.render
|
|
5769
|
+
// answer, taken whole. False with an error pending.
|
|
5770
|
+
struct DragImageSpec {
|
|
5771
|
+
id image = nil; // a CGImage, owned by ARC through the bridge
|
|
5772
|
+
double w = 0, h = 0;
|
|
5773
|
+
};
|
|
5774
|
+
|
|
5775
|
+
static bool ParseDragImage(Napi::Object o, DragImageSpec* d) {
|
|
4637
5776
|
Napi::Value sv = o.Get("surface");
|
|
4638
5777
|
if (sv.IsExternal()) {
|
|
4639
5778
|
CALSurface* s = SurfaceFrom(sv); // a released handle throws, as everywhere
|
|
4640
|
-
if (!s) return
|
|
5779
|
+
if (!s) return false;
|
|
4641
5780
|
double scale = s->scale > 0 ? s->scale : 1;
|
|
4642
5781
|
CGImageRef cg = CGBitmapContextCreateImage(s->ctx);
|
|
4643
|
-
if (!cg) return
|
|
4644
|
-
|
|
4645
|
-
|
|
4646
|
-
|
|
4647
|
-
|
|
4648
|
-
return img;
|
|
5782
|
+
if (!cg) return true;
|
|
5783
|
+
d->image = (__bridge_transfer id)cg;
|
|
5784
|
+
d->w = s->width / scale;
|
|
5785
|
+
d->h = s->height / scale;
|
|
5786
|
+
return true;
|
|
4649
5787
|
}
|
|
4650
5788
|
Napi::Value iv = o.Get("image");
|
|
4651
5789
|
double scale = BNumOr(o, "imageScale", 1);
|
|
@@ -4654,11 +5792,33 @@ static NSImage* DragImageFrom(Napi::Object o, double* w, double* h) {
|
|
|
4654
5792
|
scale = BNumOr(r, "scale", scale);
|
|
4655
5793
|
iv = r.Get("image");
|
|
4656
5794
|
}
|
|
4657
|
-
if (!iv.IsExternal()) return
|
|
5795
|
+
if (!iv.IsExternal()) return true;
|
|
4658
5796
|
CGImageRef cg = (CGImageRef)iv.As<Napi::External<void>>().Data();
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
5797
|
+
d->image = (__bridge id)cg;
|
|
5798
|
+
d->w = CGImageGetWidth(cg) / scale;
|
|
5799
|
+
d->h = CGImageGetHeight(cg) / scale;
|
|
5800
|
+
return true;
|
|
5801
|
+
}
|
|
5802
|
+
|
|
5803
|
+
// With threaded mode's channel open, the drop carries the cheap forms of
|
|
5804
|
+
// its payload itself — each item's types, and the strings of its text and
|
|
5805
|
+
// URL types — since a worker cannot read the drag pasteboard back inside
|
|
5806
|
+
// the destination callback, and after it the source may withdraw it.
|
|
5807
|
+
static std::string DragItemsJson(NSPasteboard* pb) {
|
|
5808
|
+
NSMutableArray* out = [NSMutableArray array];
|
|
5809
|
+
for (NSPasteboardItem* item in pb.pasteboardItems) {
|
|
5810
|
+
NSMutableDictionary* strings = [NSMutableDictionary dictionary];
|
|
5811
|
+
for (NSString* t in item.types) {
|
|
5812
|
+
UTType* ut = [UTType typeWithIdentifier:t];
|
|
5813
|
+
if (!ut || !([ut conformsToType:UTTypeText] || [ut conformsToType:UTTypeURL]))
|
|
5814
|
+
continue;
|
|
5815
|
+
NSString* s = [item stringForType:t];
|
|
5816
|
+
if (s) strings[t] = s;
|
|
5817
|
+
}
|
|
5818
|
+
[out addObject:@{@"types" : item.types ?: @[], @"strings" : strings}];
|
|
5819
|
+
}
|
|
5820
|
+
NSData* d = [NSJSONSerialization dataWithJSONObject:out options:0 error:nil];
|
|
5821
|
+
return d ? std::string((const char*)d.bytes, d.length) : "[]";
|
|
4662
5822
|
}
|
|
4663
5823
|
|
|
4664
5824
|
// The shared payload of the destination events: where (content view,
|
|
@@ -4668,56 +5828,54 @@ static NSImage* DragImageFrom(Napi::Object o, double* w, double* h) {
|
|
|
4668
5828
|
// a Finder drag of three files is three items of one public.file-url each,
|
|
4669
5829
|
// read per item through dragItemString. `local` is a drag begun by one of
|
|
4670
5830
|
// our own windows (beginDrag), whose number then follows.
|
|
4671
|
-
static void EmitDragInfo(
|
|
4672
|
-
|
|
4673
|
-
if (!
|
|
4674
|
-
Napi::HandleScope scope(env);
|
|
5831
|
+
static void EmitDragInfo(CALBackendView* view, const char* type,
|
|
5832
|
+
id<NSDraggingInfo> info) {
|
|
5833
|
+
if (!CALListening()) return;
|
|
4675
5834
|
NSWindow* win = view.window;
|
|
4676
|
-
|
|
5835
|
+
CALEvent ev = WindowEvent(win, type);
|
|
4677
5836
|
if (info) {
|
|
4678
5837
|
NSPoint loc = info.draggingLocation;
|
|
4679
5838
|
NSPoint p = [view convertPoint:loc fromView:nil]; // the view is flipped
|
|
4680
|
-
ev.
|
|
4681
|
-
ev.
|
|
5839
|
+
ev.Num("x", p.x);
|
|
5840
|
+
ev.Num("y", p.y);
|
|
4682
5841
|
NSRect r = [win convertRectToScreen:NSMakeRect(loc.x, loc.y, 0, 0)];
|
|
4683
|
-
ev.
|
|
4684
|
-
ev.
|
|
5842
|
+
ev.Num("gx", r.origin.x);
|
|
5843
|
+
ev.Num("gy", PrimaryScreenTop() - r.origin.y);
|
|
4685
5844
|
NSPasteboard* pb = info.draggingPasteboard;
|
|
4686
|
-
|
|
4687
|
-
|
|
4688
|
-
|
|
4689
|
-
ev.
|
|
4690
|
-
ev.Set("itemCount", (double)pb.pasteboardItems.count);
|
|
5845
|
+
std::vector<std::string> types;
|
|
5846
|
+
for (NSString* t in pb.types) types.push_back(t.UTF8String);
|
|
5847
|
+
ev.Strs("types", std::move(types));
|
|
5848
|
+
ev.Num("itemCount", (double)pb.pasteboardItems.count);
|
|
4691
5849
|
NSDragOperation mask = info.draggingSourceOperationMask;
|
|
4692
|
-
ev.
|
|
4693
|
-
ev.
|
|
5850
|
+
ev.Num("sourceMask", (double)mask);
|
|
5851
|
+
ev.Strs("operations", DragOpsOfMask(mask));
|
|
4694
5852
|
id src = info.draggingSource;
|
|
4695
5853
|
bool local = [src isKindOfClass:[CALBackendView class]];
|
|
4696
|
-
ev.
|
|
5854
|
+
ev.Bool("local", local);
|
|
4697
5855
|
if (local)
|
|
4698
|
-
ev.
|
|
5856
|
+
ev.Num("sourceWindowNumber",
|
|
4699
5857
|
(double)((CALBackendView*)src).window.windowNumber);
|
|
4700
|
-
ev.
|
|
5858
|
+
ev.Num("sequence", (double)info.draggingSequenceNumber);
|
|
5859
|
+
if (CALChannelOpen() && strcmp(type, "drag-perform") == 0)
|
|
5860
|
+
ev.Json("items", DragItemsJson(pb));
|
|
4701
5861
|
}
|
|
4702
|
-
|
|
5862
|
+
CALEmit(std::move(ev));
|
|
4703
5863
|
}
|
|
4704
5864
|
|
|
4705
5865
|
// The source side's events: the pointer in global top-left coordinates,
|
|
4706
5866
|
// and for the end, the operation the destination performed ('none' when
|
|
4707
5867
|
// nothing took the drop) with `dropped` as its boolean.
|
|
4708
|
-
static void EmitDragSession(
|
|
4709
|
-
const char*
|
|
4710
|
-
|
|
4711
|
-
|
|
4712
|
-
|
|
4713
|
-
|
|
4714
|
-
ev.Set("x", screenPoint.x);
|
|
4715
|
-
ev.Set("y", PrimaryScreenTop() - screenPoint.y);
|
|
5868
|
+
static void EmitDragSession(CALBackendView* view, const char* type,
|
|
5869
|
+
NSPoint screenPoint, const char* operation) {
|
|
5870
|
+
if (!CALListening()) return;
|
|
5871
|
+
CALEvent ev = WindowEvent(view.window, type);
|
|
5872
|
+
ev.Num("x", screenPoint.x);
|
|
5873
|
+
ev.Num("y", PrimaryScreenTop() - screenPoint.y);
|
|
4716
5874
|
if (operation) {
|
|
4717
|
-
ev.
|
|
4718
|
-
ev.
|
|
5875
|
+
ev.Str("operation", operation);
|
|
5876
|
+
ev.Bool("dropped", strcmp(operation, "none") != 0);
|
|
4719
5877
|
}
|
|
4720
|
-
|
|
5878
|
+
CALEmit(std::move(ev));
|
|
4721
5879
|
}
|
|
4722
5880
|
|
|
4723
5881
|
@interface CALBackendView (DragAndDrop) <NSDraggingSource>
|
|
@@ -4736,16 +5894,16 @@ static void EmitDragSession(Napi::Env env, CALBackendView* view,
|
|
|
4736
5894
|
// a yes left over from the previous drag must not answer this one
|
|
4737
5895
|
dropAccept_ = false;
|
|
4738
5896
|
dropOp_ = nil;
|
|
4739
|
-
EmitDragInfo(
|
|
5897
|
+
EmitDragInfo(self, "drag-enter", sender);
|
|
4740
5898
|
return [self dropAnswer:sender];
|
|
4741
5899
|
}
|
|
4742
5900
|
- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender {
|
|
4743
5901
|
gDragPasteboard = sender.draggingPasteboard;
|
|
4744
|
-
EmitDragInfo(
|
|
5902
|
+
EmitDragInfo(self, "drag-over", sender);
|
|
4745
5903
|
return [self dropAnswer:sender];
|
|
4746
5904
|
}
|
|
4747
5905
|
- (void)draggingExited:(nullable id<NSDraggingInfo>)sender {
|
|
4748
|
-
EmitDragInfo(
|
|
5906
|
+
EmitDragInfo(self, "drag-exit", sender);
|
|
4749
5907
|
}
|
|
4750
5908
|
- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender {
|
|
4751
5909
|
(void)sender;
|
|
@@ -4753,7 +5911,7 @@ static void EmitDragSession(Napi::Env env, CALBackendView* view,
|
|
|
4753
5911
|
}
|
|
4754
5912
|
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
|
|
4755
5913
|
gDragPasteboard = sender.draggingPasteboard;
|
|
4756
|
-
EmitDragInfo(
|
|
5914
|
+
EmitDragInfo(self, "drag-perform", sender);
|
|
4757
5915
|
return dropAccept_; // JS may withdraw it, having looked at the payload
|
|
4758
5916
|
}
|
|
4759
5917
|
// One drag-over per pointer position, not a timer's worth.
|
|
@@ -4774,20 +5932,18 @@ static void EmitDragSession(Napi::Env env, CALBackendView* view,
|
|
|
4774
5932
|
- (void)draggingSession:(NSDraggingSession*)session
|
|
4775
5933
|
willBeginAtPoint:(NSPoint)screenPoint {
|
|
4776
5934
|
(void)session;
|
|
4777
|
-
EmitDragSession(
|
|
4778
|
-
nullptr);
|
|
5935
|
+
EmitDragSession(self, "drag-session-began", screenPoint, nullptr);
|
|
4779
5936
|
}
|
|
4780
5937
|
- (void)draggingSession:(NSDraggingSession*)session
|
|
4781
5938
|
movedToPoint:(NSPoint)screenPoint {
|
|
4782
5939
|
(void)session;
|
|
4783
|
-
EmitDragSession(
|
|
4784
|
-
nullptr);
|
|
5940
|
+
EmitDragSession(self, "drag-session-moved", screenPoint, nullptr);
|
|
4785
5941
|
}
|
|
4786
5942
|
- (void)draggingSession:(NSDraggingSession*)session
|
|
4787
5943
|
endedAtPoint:(NSPoint)screenPoint
|
|
4788
5944
|
operation:(NSDragOperation)operation {
|
|
4789
5945
|
(void)session;
|
|
4790
|
-
EmitDragSession(
|
|
5946
|
+
EmitDragSession(self, "drag-session-ended", screenPoint,
|
|
4791
5947
|
DragOpName(operation));
|
|
4792
5948
|
}
|
|
4793
5949
|
@end
|
|
@@ -4796,20 +5952,51 @@ static void EmitDragSession(Napi::Env env, CALBackendView* view,
|
|
|
4796
5952
|
// view; an empty list unregisters. Until this is called a window takes no
|
|
4797
5953
|
// drops and sees no drag events: AppKit routes a drag only to views
|
|
4798
5954
|
// registered for a type it carries.
|
|
5955
|
+
// The hosting view of a verb's window, for a command: pump mode's checked
|
|
5956
|
+
// in the call (a TypeError for anything else, as always), a worker's when
|
|
5957
|
+
// its command runs (skipped then if it is not a createWindow2 window).
|
|
5958
|
+
static bool OnBackendView(Napi::Value v, const char* fn,
|
|
5959
|
+
void (^body)(CALBackendView* view)) {
|
|
5960
|
+
if (pthread_main_np()) {
|
|
5961
|
+
CALBackendView* view = BackendViewArg(v, fn);
|
|
5962
|
+
if (!view) return false;
|
|
5963
|
+
body(view);
|
|
5964
|
+
return true;
|
|
5965
|
+
}
|
|
5966
|
+
if (!v.IsExternal()) {
|
|
5967
|
+
Napi::TypeError::New(v.Env(), std::string(fn) + ": expected a createWindow2 window")
|
|
5968
|
+
.ThrowAsJavaScriptException();
|
|
5969
|
+
return false;
|
|
5970
|
+
}
|
|
5971
|
+
id target = CALHandleTarget(v);
|
|
5972
|
+
CALOnUI(^{
|
|
5973
|
+
NSWindow* win = CALResolve(target);
|
|
5974
|
+
if ([win.contentView isKindOfClass:[CALBackendView class]])
|
|
5975
|
+
body((CALBackendView*)win.contentView);
|
|
5976
|
+
});
|
|
5977
|
+
return true;
|
|
5978
|
+
}
|
|
5979
|
+
|
|
5980
|
+
// The same resolution inside a command already on the UI thread.
|
|
5981
|
+
static CALBackendView* BackendViewOf(id target) {
|
|
5982
|
+
NSWindow* win = CALResolve(target);
|
|
5983
|
+
return [win.contentView isKindOfClass:[CALBackendView class]]
|
|
5984
|
+
? (CALBackendView*)win.contentView
|
|
5985
|
+
: nil;
|
|
5986
|
+
}
|
|
5987
|
+
|
|
4799
5988
|
static Napi::Value RegisterDropTypes(const Napi::CallbackInfo& info) {
|
|
4800
|
-
Napi::Env env = info.Env();
|
|
4801
|
-
CALBackendView* view = BackendViewArg(info[0], "registerDropTypes");
|
|
4802
|
-
if (!view) return env.Undefined();
|
|
4803
|
-
view->env_ = env;
|
|
4804
5989
|
NSMutableArray<NSString*>* types = [NSMutableArray array];
|
|
4805
5990
|
if (info[1].IsArray()) {
|
|
4806
5991
|
Napi::Array a = info[1].As<Napi::Array>();
|
|
4807
5992
|
for (uint32_t i = 0; i < a.Length(); i++)
|
|
4808
5993
|
if (a.Get(i).IsString()) [types addObject:BToNSString(a.Get(i))];
|
|
4809
5994
|
}
|
|
4810
|
-
[
|
|
4811
|
-
|
|
4812
|
-
|
|
5995
|
+
OnBackendView(info[0], "registerDropTypes", ^(CALBackendView* view) {
|
|
5996
|
+
[view unregisterDraggedTypes]; // replace, never accumulate
|
|
5997
|
+
if (types.count) [view registerForDraggedTypes:types];
|
|
5998
|
+
});
|
|
5999
|
+
return info.Env().Undefined();
|
|
4813
6000
|
}
|
|
4814
6001
|
|
|
4815
6002
|
// setDropResponse(win, { accept, operation? }) — the view's answer for the
|
|
@@ -4819,15 +6006,20 @@ static Napi::Value RegisterDropTypes(const Napi::CallbackInfo& info) {
|
|
|
4819
6006
|
// drag-perform, `accept: false` withdraws the drop. `operation` is one of
|
|
4820
6007
|
// copy | move | link | generic | private | delete; absent, the conventional
|
|
4821
6008
|
// choice among what the source allows.
|
|
6009
|
+
// From a worker it is a command, so it cannot answer the draggingEntered:
|
|
6010
|
+
// that is running as the event crosses: it is the standing answer for the
|
|
6011
|
+
// drag-over questions that follow (the rule for what AppKit asks
|
|
6012
|
+
// synchronously: push it ahead).
|
|
4822
6013
|
static Napi::Value SetDropResponse(const Napi::CallbackInfo& info) {
|
|
4823
6014
|
Napi::Env env = info.Env();
|
|
4824
|
-
CALBackendView* view = BackendViewArg(info[0], "setDropResponse");
|
|
4825
|
-
if (!view) return env.Undefined();
|
|
4826
6015
|
Napi::Object o = info[1].IsObject() ? info[1].As<Napi::Object>()
|
|
4827
6016
|
: Napi::Object::New(env);
|
|
4828
|
-
|
|
6017
|
+
bool accept = BBoolOr(o, "accept", false);
|
|
4829
6018
|
NSString* op = BStrOr(o, "operation", nil);
|
|
4830
|
-
|
|
6019
|
+
OnBackendView(info[0], "setDropResponse", ^(CALBackendView* view) {
|
|
6020
|
+
view->dropAccept_ = accept;
|
|
6021
|
+
view->dropOp_ = op.length ? op : nil;
|
|
6022
|
+
});
|
|
4831
6023
|
return env.Undefined();
|
|
4832
6024
|
}
|
|
4833
6025
|
|
|
@@ -4842,45 +6034,68 @@ static NSPasteboard* CurrentDragPasteboard() {
|
|
|
4842
6034
|
// type) -> string | null read one. Read during the drag-perform callback:
|
|
4843
6035
|
// the payload is the source's promise, and a source is free to withdraw it
|
|
4844
6036
|
// once its session has ended.
|
|
6037
|
+
// Off the main thread each takes a callback (its last argument); in
|
|
6038
|
+
// threaded mode the drop's own `items` usually makes the call unnecessary.
|
|
4845
6039
|
static Napi::Value DragItems(const Napi::CallbackInfo& info) {
|
|
4846
|
-
|
|
4847
|
-
|
|
4848
|
-
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
6040
|
+
return CALAnswer(info, "dragItems", ^CALValueBlock {
|
|
6041
|
+
std::vector<std::vector<std::string>> all;
|
|
6042
|
+
for (NSPasteboardItem* item in CurrentDragPasteboard().pasteboardItems) {
|
|
6043
|
+
std::vector<std::string> types;
|
|
6044
|
+
for (NSString* t in item.types) types.push_back(t.UTF8String);
|
|
6045
|
+
all.push_back(std::move(types));
|
|
6046
|
+
}
|
|
6047
|
+
return ^Napi::Value(Napi::Env e) {
|
|
6048
|
+
Napi::Array out = Napi::Array::New(e);
|
|
6049
|
+
uint32_t n = 0;
|
|
6050
|
+
for (const std::vector<std::string>& types : all) {
|
|
6051
|
+
Napi::Object o = Napi::Object::New(e);
|
|
6052
|
+
Napi::Array ta = Napi::Array::New(e);
|
|
6053
|
+
uint32_t k = 0;
|
|
6054
|
+
for (const std::string& t : types) ta.Set(k++, t);
|
|
6055
|
+
o.Set("types", ta);
|
|
6056
|
+
out.Set(n++, o);
|
|
6057
|
+
}
|
|
6058
|
+
return out;
|
|
6059
|
+
};
|
|
6060
|
+
});
|
|
4858
6061
|
}
|
|
4859
6062
|
|
|
4860
|
-
|
|
6063
|
+
// On the UI thread: the item at an index read on the calling thread.
|
|
6064
|
+
static NSPasteboardItem* DragItemAt(double i) {
|
|
4861
6065
|
NSArray<NSPasteboardItem*>* items = CurrentDragPasteboard().pasteboardItems;
|
|
4862
|
-
if (!v.IsNumber()) return nil;
|
|
4863
|
-
double i = v.As<Napi::Number>().DoubleValue();
|
|
4864
6066
|
if (!(i >= 0 && i < (double)items.count)) return nil;
|
|
4865
6067
|
return items[(NSUInteger)i];
|
|
4866
6068
|
}
|
|
4867
6069
|
|
|
6070
|
+
static double DragIndexArg(Napi::Value v) {
|
|
6071
|
+
return v.IsNumber() ? v.As<Napi::Number>().DoubleValue() : -1;
|
|
6072
|
+
}
|
|
6073
|
+
|
|
4868
6074
|
static Napi::Value DragItemData(const Napi::CallbackInfo& info) {
|
|
4869
|
-
|
|
4870
|
-
|
|
4871
|
-
|
|
4872
|
-
|
|
4873
|
-
|
|
4874
|
-
|
|
6075
|
+
double index = DragIndexArg(info[0]);
|
|
6076
|
+
NSString* type = info[1].IsString() ? BToNSString(info[1]) : nil;
|
|
6077
|
+
return CALAnswer(info, "dragItemData", ^CALValueBlock {
|
|
6078
|
+
NSPasteboardItem* item = DragItemAt(index);
|
|
6079
|
+
NSData* d = item && type ? [item dataForType:type] : nil;
|
|
6080
|
+
return ^Napi::Value(Napi::Env e) {
|
|
6081
|
+
return d ? Napi::Value(Napi::Buffer<uint8_t>::Copy(e, (const uint8_t*)d.bytes, d.length))
|
|
6082
|
+
: Napi::Value(e.Null());
|
|
6083
|
+
};
|
|
6084
|
+
});
|
|
4875
6085
|
}
|
|
4876
6086
|
|
|
4877
6087
|
static Napi::Value DragItemString(const Napi::CallbackInfo& info) {
|
|
4878
|
-
|
|
4879
|
-
|
|
4880
|
-
|
|
4881
|
-
|
|
4882
|
-
|
|
4883
|
-
|
|
6088
|
+
double index = DragIndexArg(info[0]);
|
|
6089
|
+
NSString* type = info[1].IsString() ? BToNSString(info[1]) : nil;
|
|
6090
|
+
return CALAnswer(info, "dragItemString", ^CALValueBlock {
|
|
6091
|
+
NSPasteboardItem* item = DragItemAt(index);
|
|
6092
|
+
NSString* s = item && type ? [item stringForType:type] : nil;
|
|
6093
|
+
bool has = s != nil;
|
|
6094
|
+
std::string text = has ? s.UTF8String : "";
|
|
6095
|
+
return ^Napi::Value(Napi::Env e) {
|
|
6096
|
+
return has ? Napi::Value(Napi::String::New(e, text)) : Napi::Value(e.Null());
|
|
6097
|
+
};
|
|
6098
|
+
});
|
|
4884
6099
|
}
|
|
4885
6100
|
|
|
4886
6101
|
// beginDrag(win, { x, y, items, provide?, operations?, operationsOutside?,
|
|
@@ -4902,95 +6117,141 @@ static Napi::Value DragItemString(const Napi::CallbackInfo& info) {
|
|
|
4902
6117
|
// hears drag-session-began / -moved / -ended, and the pointer's own
|
|
4903
6118
|
// mousemove/mouseup do not arrive while it runs — the ended event is the
|
|
4904
6119
|
// release.
|
|
6120
|
+
//
|
|
6121
|
+
// From a worker the session is begun by a modal command and the call
|
|
6122
|
+
// answers undefined: the answer is the drag-session-began event, or a
|
|
6123
|
+
// drag-session-ended with nothing dropped when no session began (no press
|
|
6124
|
+
// in flight and no x/y given included). `provide` answers AppKit
|
|
6125
|
+
// synchronously, which a worker cannot: there it is a TypeError, and every
|
|
6126
|
+
// representation's value is given up front.
|
|
4905
6127
|
static Napi::Value BeginDrag(const Napi::CallbackInfo& info) {
|
|
4906
6128
|
Napi::Env env = info.Env();
|
|
4907
|
-
|
|
4908
|
-
if (
|
|
6129
|
+
bool main = pthread_main_np();
|
|
6130
|
+
if (main) {
|
|
6131
|
+
if (!BackendViewArg(info[0], "beginDrag")) return env.Undefined();
|
|
6132
|
+
} else if (!info[0].IsExternal()) {
|
|
6133
|
+
Napi::TypeError::New(env, "beginDrag: expected a createWindow2 window")
|
|
6134
|
+
.ThrowAsJavaScriptException();
|
|
6135
|
+
return env.Undefined();
|
|
6136
|
+
}
|
|
4909
6137
|
if (!info[1].IsObject()) {
|
|
4910
6138
|
Napi::TypeError::New(env, "beginDrag: expected an options object")
|
|
4911
6139
|
.ThrowAsJavaScriptException();
|
|
4912
6140
|
return env.Undefined();
|
|
4913
6141
|
}
|
|
4914
6142
|
Napi::Object o = info[1].As<Napi::Object>();
|
|
4915
|
-
view->env_ = env;
|
|
4916
|
-
|
|
4917
|
-
NSEvent* press = view->lastPress_;
|
|
4918
|
-
if (press && press.window != view.window) press = nil;
|
|
4919
6143
|
double x = BNumOr(o, "x", NAN), y = BNumOr(o, "y", NAN);
|
|
4920
|
-
if (std::isnan(x) || std::isnan(y)) {
|
|
4921
|
-
if (!press) {
|
|
4922
|
-
Napi::TypeError::New(env, "beginDrag: x and y are required without a press in flight")
|
|
4923
|
-
.ThrowAsJavaScriptException();
|
|
4924
|
-
return env.Undefined();
|
|
4925
|
-
}
|
|
4926
|
-
NSPoint p = [view convertPoint:press.locationInWindow fromView:nil];
|
|
4927
|
-
x = p.x;
|
|
4928
|
-
y = p.y;
|
|
4929
|
-
}
|
|
4930
|
-
if (!press) {
|
|
4931
|
-
NSPoint wp = [view convertPoint:NSMakePoint(x, y) toView:nil];
|
|
4932
|
-
press = [NSEvent mouseEventWithType:NSEventTypeLeftMouseDown
|
|
4933
|
-
location:wp
|
|
4934
|
-
modifierFlags:0
|
|
4935
|
-
timestamp:NSProcessInfo.processInfo.systemUptime
|
|
4936
|
-
windowNumber:view.window.windowNumber
|
|
4937
|
-
context:nil
|
|
4938
|
-
eventNumber:0
|
|
4939
|
-
clickCount:1
|
|
4940
|
-
pressure:1];
|
|
4941
|
-
}
|
|
4942
6144
|
|
|
4943
6145
|
CALDragProvider* provider = nil;
|
|
4944
6146
|
Napi::Value provide = o.Get("provide");
|
|
4945
6147
|
if (provide.IsFunction()) {
|
|
6148
|
+
if (!main) {
|
|
6149
|
+
Napi::TypeError::New(env, "beginDrag: `provide` answers AppKit synchronously, "
|
|
6150
|
+
"which a worker cannot — give every value up front")
|
|
6151
|
+
.ThrowAsJavaScriptException();
|
|
6152
|
+
return env.Undefined();
|
|
6153
|
+
}
|
|
4946
6154
|
provider = [[CALDragProvider alloc] init];
|
|
4947
6155
|
provider->env_ = env;
|
|
4948
6156
|
provider->fn_ = Napi::Persistent(provide.As<Napi::Function>());
|
|
4949
6157
|
}
|
|
4950
|
-
|
|
4951
|
-
|
|
4952
|
-
if (provider) provider->items_ = pbItems;
|
|
4953
|
-
if (pbItems.count == 0) {
|
|
6158
|
+
PbItemsSpec items = ParsePasteboardItems(env, o.Get("items"), provider != nil);
|
|
6159
|
+
if (items.empty()) {
|
|
4954
6160
|
Napi::TypeError::New(env, "beginDrag: items must name at least one item")
|
|
4955
6161
|
.ThrowAsJavaScriptException();
|
|
4956
6162
|
return env.Undefined();
|
|
4957
6163
|
}
|
|
6164
|
+
DragImageSpec image;
|
|
6165
|
+
if (!ParseDragImage(o, &image)) return env.Undefined();
|
|
6166
|
+
double imageW = o.Has("imageWidth") ? BNumOr(o, "imageWidth", NAN) : NAN;
|
|
6167
|
+
double imageH = o.Has("imageHeight") ? BNumOr(o, "imageHeight", NAN) : NAN;
|
|
6168
|
+
double imageX = BNumOr(o, "imageX", NAN), imageY = BNumOr(o, "imageY", NAN);
|
|
6169
|
+
NSDragOperation mask = DragMaskFrom(o.Get("operations"));
|
|
6170
|
+
NSDragOperation maskOutside =
|
|
6171
|
+
o.Has("operationsOutside") ? DragMaskFrom(o.Get("operationsOutside")) : mask;
|
|
6172
|
+
bool ignoreModifiers = BBoolOr(o, "ignoreModifiers", false);
|
|
6173
|
+
bool slideBack = BBoolOr(o, "slideBack", true);
|
|
6174
|
+
id target = CALHandleTarget(info[0]);
|
|
6175
|
+
|
|
6176
|
+
__block bool began = false, needXY = false;
|
|
6177
|
+
CALOnUIModal(^{
|
|
6178
|
+
CALBackendView* view = BackendViewOf(target);
|
|
6179
|
+
if (!view) return;
|
|
6180
|
+
NSEvent* press = view->lastPress_;
|
|
6181
|
+
if (press && press.window != view.window) press = nil;
|
|
6182
|
+
double px = x, py = y;
|
|
6183
|
+
if (std::isnan(px) || std::isnan(py)) {
|
|
6184
|
+
if (!press) {
|
|
6185
|
+
needXY = true;
|
|
6186
|
+
if (!main)
|
|
6187
|
+
EmitDragSession(view, "drag-session-ended", NSEvent.mouseLocation, "none");
|
|
6188
|
+
return;
|
|
6189
|
+
}
|
|
6190
|
+
NSPoint p = [view convertPoint:press.locationInWindow fromView:nil];
|
|
6191
|
+
px = p.x;
|
|
6192
|
+
py = p.y;
|
|
6193
|
+
}
|
|
6194
|
+
if (!press) {
|
|
6195
|
+
NSPoint wp = [view convertPoint:NSMakePoint(px, py) toView:nil];
|
|
6196
|
+
press = [NSEvent mouseEventWithType:NSEventTypeLeftMouseDown
|
|
6197
|
+
location:wp
|
|
6198
|
+
modifierFlags:0
|
|
6199
|
+
timestamp:NSProcessInfo.processInfo.systemUptime
|
|
6200
|
+
windowNumber:view.window.windowNumber
|
|
6201
|
+
context:nil
|
|
6202
|
+
eventNumber:0
|
|
6203
|
+
clickCount:1
|
|
6204
|
+
pressure:1];
|
|
6205
|
+
}
|
|
6206
|
+
NSArray<NSPasteboardItem*>* pbItems = BuildPasteboardItems(items, provider);
|
|
6207
|
+
if (provider) provider->items_ = pbItems;
|
|
6208
|
+
|
|
6209
|
+
double w = image.w, h = image.h;
|
|
6210
|
+
NSImage* img = image.image ? [[NSImage alloc] initWithCGImage:(__bridge CGImageRef)image.image
|
|
6211
|
+
size:NSMakeSize(w, h)]
|
|
6212
|
+
: nil;
|
|
6213
|
+
if (!std::isnan(imageW)) w = imageW;
|
|
6214
|
+
if (!std::isnan(imageH)) h = imageH;
|
|
6215
|
+
double ix = std::isnan(imageX) ? px - w / 2 : imageX;
|
|
6216
|
+
double iy = std::isnan(imageY) ? py - h / 2 : imageY;
|
|
6217
|
+
if (!img) {
|
|
6218
|
+
img = [[NSImage alloc] initWithSize:NSMakeSize(1, 1)];
|
|
6219
|
+
w = h = 1;
|
|
6220
|
+
ix = px;
|
|
6221
|
+
iy = py;
|
|
6222
|
+
}
|
|
6223
|
+
// the view is flipped, so a top-left frame is what setDraggingFrame: takes
|
|
6224
|
+
NSRect frame = NSMakeRect(ix, iy, w, h);
|
|
6225
|
+
NSMutableArray<NSDraggingItem*>* dragItems = [NSMutableArray array];
|
|
6226
|
+
for (NSPasteboardItem* item in pbItems) {
|
|
6227
|
+
NSDraggingItem* di = [[NSDraggingItem alloc] initWithPasteboardWriter:item];
|
|
6228
|
+
[di setDraggingFrame:frame contents:img];
|
|
6229
|
+
[dragItems addObject:di];
|
|
6230
|
+
}
|
|
4958
6231
|
|
|
4959
|
-
|
|
4960
|
-
|
|
4961
|
-
|
|
4962
|
-
|
|
4963
|
-
|
|
4964
|
-
|
|
4965
|
-
|
|
4966
|
-
|
|
4967
|
-
|
|
4968
|
-
|
|
4969
|
-
|
|
4970
|
-
|
|
4971
|
-
|
|
4972
|
-
|
|
4973
|
-
|
|
4974
|
-
|
|
4975
|
-
|
|
4976
|
-
|
|
4977
|
-
|
|
4978
|
-
|
|
4979
|
-
|
|
4980
|
-
|
|
4981
|
-
|
|
4982
|
-
? DragMaskFrom(o.Get("operationsOutside"))
|
|
4983
|
-
: view->sourceMask_;
|
|
4984
|
-
view->ignoreModifiers_ = BBoolOr(o, "ignoreModifiers", false);
|
|
4985
|
-
view->dragProvider_ = provider; // alive for as long as the pasteboard may ask
|
|
4986
|
-
|
|
4987
|
-
NSDraggingSession* session = [view beginDraggingSessionWithItems:dragItems
|
|
4988
|
-
event:press
|
|
4989
|
-
source:view];
|
|
4990
|
-
if (!session) return Napi::Boolean::New(env, false);
|
|
4991
|
-
session.animatesToStartingPositionsOnCancelOrFail =
|
|
4992
|
-
BBoolOr(o, "slideBack", true);
|
|
4993
|
-
return Napi::Boolean::New(env, true);
|
|
6232
|
+
view->sourceMask_ = mask;
|
|
6233
|
+
view->sourceMaskOutside_ = maskOutside;
|
|
6234
|
+
view->ignoreModifiers_ = ignoreModifiers;
|
|
6235
|
+
view->dragProvider_ = provider; // alive for as long as the pasteboard may ask
|
|
6236
|
+
|
|
6237
|
+
NSDraggingSession* session = [view beginDraggingSessionWithItems:dragItems
|
|
6238
|
+
event:press
|
|
6239
|
+
source:view];
|
|
6240
|
+
if (!session) {
|
|
6241
|
+
if (!main)
|
|
6242
|
+
EmitDragSession(view, "drag-session-ended", NSEvent.mouseLocation, "none");
|
|
6243
|
+
return;
|
|
6244
|
+
}
|
|
6245
|
+
session.animatesToStartingPositionsOnCancelOrFail = slideBack;
|
|
6246
|
+
began = true;
|
|
6247
|
+
});
|
|
6248
|
+
if (!main) return env.Undefined();
|
|
6249
|
+
if (needXY) {
|
|
6250
|
+
Napi::TypeError::New(env, "beginDrag: x and y are required without a press in flight")
|
|
6251
|
+
.ThrowAsJavaScriptException();
|
|
6252
|
+
return env.Undefined();
|
|
6253
|
+
}
|
|
6254
|
+
return Napi::Boolean::New(env, began);
|
|
4994
6255
|
}
|
|
4995
6256
|
|
|
4996
6257
|
// A dragging info of our own, for postDragEvent: what AppKit would build
|
|
@@ -5059,56 +6320,74 @@ static Napi::Value BeginDrag(const Napi::CallbackInfo& info) {
|
|
|
5059
6320
|
// again on any later phase that names it), `operations` is the pretend
|
|
5060
6321
|
// source's mask (copy default), and `local: true` names this window's own
|
|
5061
6322
|
// view as the source.
|
|
6323
|
+
// Off the main thread: postDragEvent(win, phase, opts, cb), the answer
|
|
6324
|
+
// through cb.
|
|
6325
|
+
static NSInteger gPostedDragSequence = 0; // the UI thread's
|
|
6326
|
+
|
|
5062
6327
|
static Napi::Value PostDragEvent(const Napi::CallbackInfo& info) {
|
|
5063
6328
|
Napi::Env env = info.Env();
|
|
5064
|
-
|
|
5065
|
-
|
|
5066
|
-
|
|
6329
|
+
if (pthread_main_np()) {
|
|
6330
|
+
if (!BackendViewArg(info[0], "postDragEvent")) return env.Undefined();
|
|
6331
|
+
} else if (!info[0].IsExternal()) {
|
|
6332
|
+
Napi::TypeError::New(env, "postDragEvent: expected a createWindow2 window")
|
|
6333
|
+
.ThrowAsJavaScriptException();
|
|
6334
|
+
return env.Undefined();
|
|
6335
|
+
}
|
|
5067
6336
|
std::string phase = info[1].IsString() ? info[1].As<Napi::String>().Utf8Value() : "";
|
|
6337
|
+
if (phase != "enter" && phase != "over" && phase != "exit" && phase != "drop") {
|
|
6338
|
+
Napi::TypeError::New(env, "postDragEvent: phase must be enter | over | exit | drop")
|
|
6339
|
+
.ThrowAsJavaScriptException();
|
|
6340
|
+
return env.Undefined();
|
|
6341
|
+
}
|
|
5068
6342
|
Napi::Object o = info.Length() > 2 && info[2].IsObject()
|
|
5069
6343
|
? info[2].As<Napi::Object>()
|
|
5070
6344
|
: Napi::Object::New(env);
|
|
5071
|
-
|
|
5072
|
-
|
|
5073
|
-
|
|
5074
|
-
|
|
5075
|
-
|
|
5076
|
-
|
|
5077
|
-
|
|
5078
|
-
|
|
5079
|
-
|
|
5080
|
-
|
|
5081
|
-
|
|
5082
|
-
|
|
5083
|
-
|
|
5084
|
-
|
|
5085
|
-
|
|
5086
|
-
|
|
5087
|
-
|
|
5088
|
-
|
|
5089
|
-
|
|
5090
|
-
|
|
5091
|
-
|
|
5092
|
-
|
|
5093
|
-
|
|
5094
|
-
|
|
5095
|
-
|
|
5096
|
-
|
|
5097
|
-
|
|
5098
|
-
|
|
5099
|
-
|
|
5100
|
-
|
|
5101
|
-
|
|
5102
|
-
|
|
5103
|
-
|
|
6345
|
+
bool hasItems = o.Has("items");
|
|
6346
|
+
PbItemsSpec items = ParsePasteboardItems(env, o.Get("items"), false);
|
|
6347
|
+
double x = BNumOr(o, "x", 0), y = BNumOr(o, "y", 0);
|
|
6348
|
+
NSDragOperation mask = DragMaskFrom(o.Get("operations"));
|
|
6349
|
+
bool local = BBoolOr(o, "local", false);
|
|
6350
|
+
id target = CALHandleTarget(info[0]);
|
|
6351
|
+
|
|
6352
|
+
return CALAnswer(info, "postDragEvent", ^CALValueBlock {
|
|
6353
|
+
CALBackendView* view = BackendViewOf(target);
|
|
6354
|
+
if (!view) return ^Napi::Value(Napi::Env e) { return e.Undefined(); };
|
|
6355
|
+
// one pasteboard per drag: a new one on enter, its contents whatever
|
|
6356
|
+
// `items` the latest phase named (a drag's payload is fixed in AppKit,
|
|
6357
|
+
// but a test may want to say it once, at the drop)
|
|
6358
|
+
if (phase == "enter" || !gPostedPasteboard) {
|
|
6359
|
+
if (gPostedPasteboard) [gPostedPasteboard releaseGlobally];
|
|
6360
|
+
gPostedPasteboard = [NSPasteboard pasteboardWithUniqueName];
|
|
6361
|
+
gPostedDragSequence++;
|
|
6362
|
+
}
|
|
6363
|
+
if (phase == "enter" || hasItems) {
|
|
6364
|
+
[gPostedPasteboard clearContents];
|
|
6365
|
+
[gPostedPasteboard writeObjects:BuildPasteboardItems(items, nil)];
|
|
6366
|
+
}
|
|
6367
|
+
CALPostedDragInfo* di = [[CALPostedDragInfo alloc] init];
|
|
6368
|
+
di->window_ = view.window;
|
|
6369
|
+
di->pasteboard_ = gPostedPasteboard;
|
|
6370
|
+
di->location_ = [view convertPoint:NSMakePoint(x, y) toView:nil];
|
|
6371
|
+
di->mask_ = mask;
|
|
6372
|
+
di->source_ = local ? view : nil;
|
|
6373
|
+
di->sequence_ = gPostedDragSequence;
|
|
6374
|
+
di->formation_ = NSDraggingFormationDefault;
|
|
6375
|
+
di->valid_ = (NSInteger)gPostedPasteboard.pasteboardItems.count;
|
|
6376
|
+
|
|
6377
|
+
if (phase == "enter" || phase == "over") {
|
|
6378
|
+
std::string op = DragOpName(phase == "enter" ? [view draggingEntered:di]
|
|
6379
|
+
: [view draggingUpdated:di]);
|
|
6380
|
+
return ^Napi::Value(Napi::Env e) { return Napi::String::New(e, op); };
|
|
6381
|
+
}
|
|
6382
|
+
if (phase == "exit") {
|
|
6383
|
+
[view draggingExited:di];
|
|
6384
|
+
return ^Napi::Value(Napi::Env e) { return e.Undefined(); };
|
|
6385
|
+
}
|
|
5104
6386
|
bool taken = [view prepareForDragOperation:di] && [view performDragOperation:di];
|
|
5105
6387
|
if (taken && [view respondsToSelector:@selector(concludeDragOperation:)])
|
|
5106
6388
|
[view concludeDragOperation:di];
|
|
5107
|
-
return
|
|
5108
|
-
}
|
|
5109
|
-
Napi::TypeError::New(env, "postDragEvent: phase must be enter | over | exit | drop")
|
|
5110
|
-
.ThrowAsJavaScriptException();
|
|
5111
|
-
return env.Undefined();
|
|
6389
|
+
return BoolAnswer(taken);
|
|
6390
|
+
});
|
|
5112
6391
|
}
|
|
5113
6392
|
|
|
5114
6393
|
// pasteboardTypeForMIME(mime) -> UTI — the OS's own MIME <-> UTI table
|
|
@@ -5164,7 +6443,9 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
|
|
|
5164
6443
|
BFN("windowNumberAtPoint", WindowNumberAtPoint);
|
|
5165
6444
|
BFN("setWindowFrame", SetWindowFrame);
|
|
5166
6445
|
BFN("getWindowFrame", GetWindowFrame);
|
|
6446
|
+
BFN("windowState", WindowStateFn);
|
|
5167
6447
|
BFN("setWindowMinMax", SetWindowMinMax);
|
|
6448
|
+
BFN("setResizeHandshake", SetResizeHandshake);
|
|
5168
6449
|
BFN("destroyWindow2", DestroyWindow2);
|
|
5169
6450
|
BFN("invalidateWindowShadow", InvalidateWindowShadow);
|
|
5170
6451
|
BFN("activateApp", ActivateApp);
|
|
@@ -5179,6 +6460,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
|
|
|
5179
6460
|
BFN("copySurfaceRegion", CopySurfaceRegion);
|
|
5180
6461
|
BFN("blitSurface", BlitSurface);
|
|
5181
6462
|
BFN("surfaceSize", SurfaceSize);
|
|
6463
|
+
BFN("surfaceIsInUse", SurfaceIsInUse);
|
|
5182
6464
|
BFN("ctxSave", CtxSave);
|
|
5183
6465
|
BFN("ctxRestore", CtxRestore);
|
|
5184
6466
|
BFN("ctxTranslate", CtxTranslate);
|
|
@@ -5228,6 +6510,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
|
|
|
5228
6510
|
BFN("cgFontWithSize", CgFontWithSize);
|
|
5229
6511
|
BFN("fontByPostScriptName", FontByPostScriptName);
|
|
5230
6512
|
BFN("fontApplyVariations", FontApplyVariations);
|
|
6513
|
+
BFN("fontApplyFeatures", FontApplyFeatures);
|
|
5231
6514
|
BFN("drawLayoutGradient", DrawLayoutGradient);
|
|
5232
6515
|
BFN("ctxSetShadow", CtxSetShadow);
|
|
5233
6516
|
BFN("listFonts", ListFonts);
|
|
@@ -5266,6 +6549,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
|
|
|
5266
6549
|
BFN("cancelUserAttention", CancelUserAttentionFn);
|
|
5267
6550
|
BFN("setAppName", SetAppNameFn);
|
|
5268
6551
|
BFN("appInfo", AppInfoFn);
|
|
6552
|
+
BFN("activationPolicy", ActivationPolicyFn);
|
|
5269
6553
|
BFN("measureControl", MeasureControl);
|
|
5270
6554
|
BFN("drawControlIntoSurface", DrawControlIntoSurface);
|
|
5271
6555
|
BFN("listScreens", ListScreens);
|