@windowkit/appkit 0.7.0 → 0.9.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 +228 -7
- package/binding.gyp +5 -2
- package/index.js +116 -7
- 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 +8 -0
- package/src/backend.mm +4 -1
- package/src/calendars.mm +1706 -0
- package/src/permissions.mm +181 -31
- package/src/screencolor.mm +146 -0
package/src/permissions.mm
CHANGED
|
@@ -20,13 +20,25 @@
|
|
|
20
20
|
// location CLLocationManager.authorizationStatus /
|
|
21
21
|
// requestWhenInUseAuthorization, answered through the
|
|
22
22
|
// delegate on the main run loop — i.e. during the pump
|
|
23
|
+
// calendars, reminders EKEventStore authorizationStatusForEntityType: /
|
|
24
|
+
// requestFullAccessToEventsWithCompletion:,
|
|
25
|
+
// requestWriteOnlyAccessToEventsWithCompletion:,
|
|
26
|
+
// requestFullAccessToRemindersWithCompletion: on 14+
|
|
27
|
+
// (requestAccessToEntityType:completion: before), on
|
|
28
|
+
// the process's one EKEventStore; the completion lands
|
|
29
|
+
// on an arbitrary queue like AVFoundation's
|
|
23
30
|
//
|
|
24
31
|
// Every status that crosses to JS is one of 'authorized' | 'denied' |
|
|
25
|
-
// 'restricted' | 'notDetermined'
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
32
|
+
// 'restricted' | 'notDetermined' — plus 'writeOnly', which only calendars
|
|
33
|
+
// and reminders can answer: macOS 14's partial grant (the app may save items
|
|
34
|
+
// it cannot read back). It crosses as its own word rather than as either
|
|
35
|
+
// side, because whether it counts as granted depends on what the consumer
|
|
36
|
+
// wanted to do — a writer has what it needs, a reader does not — and that is
|
|
37
|
+
// the renderer's decision. Screen recording and accessibility only ever
|
|
38
|
+
// answer with a bool, so they never say notDetermined; their "request" posts
|
|
39
|
+
// the system's own go-to-Settings dialog and returns at once — the user
|
|
40
|
+
// finishes the grant in Settings, which is what openPrivacySettings is for.
|
|
41
|
+
// The folder grants (Desktop, Documents, Downloads) need nothing here:
|
|
30
42
|
// reading the directory is the prompt, and EPERM is the denial.
|
|
31
43
|
//
|
|
32
44
|
// Every request answers asynchronously, once, through the callback — never
|
|
@@ -37,16 +49,23 @@
|
|
|
37
49
|
#import <Cocoa/Cocoa.h>
|
|
38
50
|
#import <AVFoundation/AVFoundation.h>
|
|
39
51
|
#import <CoreLocation/CoreLocation.h>
|
|
52
|
+
#import <EventKit/EventKit.h>
|
|
40
53
|
#import <ApplicationServices/ApplicationServices.h>
|
|
41
54
|
#import <IOKit/hidsystem/IOHIDLib.h>
|
|
42
55
|
|
|
43
56
|
#include <cstring>
|
|
44
57
|
#include <string>
|
|
45
58
|
|
|
59
|
+
// src/calendars.mm — the change observer the process's store gets as it is
|
|
60
|
+
// created, so EKEventStoreChangedNotification is watched from the store's
|
|
61
|
+
// first moment whichever verb made it.
|
|
62
|
+
void CALCalendarsObserveStore(EKEventStore* store);
|
|
63
|
+
|
|
46
64
|
static const char* const kAuthorized = "authorized";
|
|
47
65
|
static const char* const kDenied = "denied";
|
|
48
66
|
static const char* const kRestricted = "restricted";
|
|
49
67
|
static const char* const kNotDetermined = "notDetermined";
|
|
68
|
+
static const char* const kWriteOnly = "writeOnly";
|
|
50
69
|
|
|
51
70
|
enum class Kind {
|
|
52
71
|
Camera,
|
|
@@ -56,11 +75,13 @@ enum class Kind {
|
|
|
56
75
|
InputMonitoring,
|
|
57
76
|
Automation,
|
|
58
77
|
Location,
|
|
78
|
+
Calendars,
|
|
79
|
+
Reminders,
|
|
59
80
|
};
|
|
60
81
|
|
|
61
82
|
static const char* const kKindNames =
|
|
62
83
|
"camera, microphone, screen-recording, accessibility, input-monitoring, "
|
|
63
|
-
"automation, location";
|
|
84
|
+
"automation, location, calendars, reminders";
|
|
64
85
|
|
|
65
86
|
static bool ParseKind(const std::string& s, Kind* out) {
|
|
66
87
|
if (s == "camera") *out = Kind::Camera;
|
|
@@ -70,14 +91,26 @@ static bool ParseKind(const std::string& s, Kind* out) {
|
|
|
70
91
|
else if (s == "input-monitoring") *out = Kind::InputMonitoring;
|
|
71
92
|
else if (s == "automation") *out = Kind::Automation;
|
|
72
93
|
else if (s == "location") *out = Kind::Location;
|
|
94
|
+
else if (s == "calendars") *out = Kind::Calendars;
|
|
95
|
+
else if (s == "reminders") *out = Kind::Reminders;
|
|
73
96
|
else return false;
|
|
74
97
|
return true;
|
|
75
98
|
}
|
|
76
99
|
|
|
77
|
-
// (kind, opts?)
|
|
78
|
-
// from opts.target
|
|
100
|
+
// The parsed (kind, opts?) of a status or request call: the kind, for
|
|
101
|
+
// 'automation' the target bundle id from opts.target, and for 'calendars' the
|
|
102
|
+
// level from opts.access — 'full' (the default) or 'write-only', macOS 14's
|
|
103
|
+
// partial grant. Reminders have no write-only grant, so asking for one there
|
|
104
|
+
// is a bad shape, not a request that would silently ask for something else.
|
|
105
|
+
struct KindArgs {
|
|
106
|
+
Kind kind;
|
|
107
|
+
NSString* target = nil;
|
|
108
|
+
bool writeOnly = false;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
// Throws a TypeError and returns false on a bad shape.
|
|
79
112
|
static bool ParseKindArgs(const char* fn, const Napi::CallbackInfo& info,
|
|
80
|
-
|
|
113
|
+
KindArgs* out) {
|
|
81
114
|
Napi::Env env = info.Env();
|
|
82
115
|
if (info.Length() < 1 || !info[0].IsString()) {
|
|
83
116
|
Napi::TypeError::New(env, std::string(fn) + ": expected a kind string (" +
|
|
@@ -86,18 +119,21 @@ static bool ParseKindArgs(const char* fn, const Napi::CallbackInfo& info,
|
|
|
86
119
|
return false;
|
|
87
120
|
}
|
|
88
121
|
std::string name = info[0].As<Napi::String>().Utf8Value();
|
|
89
|
-
if (!ParseKind(name, kind)) {
|
|
122
|
+
if (!ParseKind(name, &out->kind)) {
|
|
90
123
|
Napi::TypeError::New(env, std::string(fn) + ": unknown kind '" + name +
|
|
91
124
|
"' (" + kKindNames + ")")
|
|
92
125
|
.ThrowAsJavaScriptException();
|
|
93
126
|
return false;
|
|
94
127
|
}
|
|
95
|
-
|
|
96
|
-
if (
|
|
128
|
+
Napi::Object opts;
|
|
129
|
+
if (info.Length() > 1 && info[1].IsObject() && !info[1].IsFunction()) {
|
|
130
|
+
opts = info[1].As<Napi::Object>();
|
|
131
|
+
}
|
|
132
|
+
out->target = nil;
|
|
133
|
+
out->writeOnly = false;
|
|
134
|
+
if (out->kind == Kind::Automation) {
|
|
97
135
|
Napi::Value t;
|
|
98
|
-
if (
|
|
99
|
-
t = info[1].As<Napi::Object>().Get("target");
|
|
100
|
-
}
|
|
136
|
+
if (!opts.IsEmpty()) t = opts.Get("target");
|
|
101
137
|
if (t.IsEmpty() || !t.IsString()) {
|
|
102
138
|
Napi::TypeError::New(env, std::string(fn) +
|
|
103
139
|
": 'automation' needs { target: <bundle "
|
|
@@ -106,7 +142,29 @@ static bool ParseKindArgs(const char* fn, const Napi::CallbackInfo& info,
|
|
|
106
142
|
return false;
|
|
107
143
|
}
|
|
108
144
|
std::string s = t.As<Napi::String>().Utf8Value();
|
|
109
|
-
|
|
145
|
+
out->target = [NSString stringWithUTF8String:s.c_str()];
|
|
146
|
+
}
|
|
147
|
+
if (out->kind == Kind::Calendars || out->kind == Kind::Reminders) {
|
|
148
|
+
Napi::Value a;
|
|
149
|
+
if (!opts.IsEmpty()) a = opts.Get("access");
|
|
150
|
+
if (!a.IsEmpty() && !a.IsUndefined() && !a.IsNull()) {
|
|
151
|
+
std::string s = a.IsString() ? a.As<Napi::String>().Utf8Value() : "";
|
|
152
|
+
if (s == "write-only" && out->kind == Kind::Calendars) {
|
|
153
|
+
out->writeOnly = true;
|
|
154
|
+
} else if (s == "write-only") {
|
|
155
|
+
Napi::TypeError::New(env, std::string(fn) +
|
|
156
|
+
": 'reminders' has no write-only grant; "
|
|
157
|
+
"access must be 'full'")
|
|
158
|
+
.ThrowAsJavaScriptException();
|
|
159
|
+
return false;
|
|
160
|
+
} else if (s != "full") {
|
|
161
|
+
Napi::TypeError::New(env, std::string(fn) + ": '" + name +
|
|
162
|
+
"' access must be 'full' or "
|
|
163
|
+
"'write-only'")
|
|
164
|
+
.ThrowAsJavaScriptException();
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
110
168
|
}
|
|
111
169
|
return true;
|
|
112
170
|
}
|
|
@@ -151,6 +209,49 @@ static CLLocationManager* LocationStatusManager() {
|
|
|
151
209
|
return m;
|
|
152
210
|
}
|
|
153
211
|
|
|
212
|
+
static EKEntityType EntityOf(Kind kind) {
|
|
213
|
+
return kind == Kind::Reminders ? EKEntityTypeReminder : EKEntityTypeEvent;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// EventKit's grant for one entity type, Apple's word as it is. FullAccess
|
|
217
|
+
// (14+) has the raw value of the deprecated Authorized, so the one case reads
|
|
218
|
+
// both; WriteOnly (14+) is the partial grant and crosses as its own word.
|
|
219
|
+
static const char* EKStatus(EKEntityType type) {
|
|
220
|
+
EKAuthorizationStatus st =
|
|
221
|
+
[EKEventStore authorizationStatusForEntityType:type];
|
|
222
|
+
if (@available(macOS 14.0, *)) {
|
|
223
|
+
if (st == EKAuthorizationStatusWriteOnly) return kWriteOnly;
|
|
224
|
+
}
|
|
225
|
+
switch (st) {
|
|
226
|
+
case EKAuthorizationStatusAuthorized: return kAuthorized;
|
|
227
|
+
case EKAuthorizationStatusDenied: return kDenied;
|
|
228
|
+
case EKAuthorizationStatusRestricted: return kRestricted;
|
|
229
|
+
case EKAuthorizationStatusNotDetermined:
|
|
230
|
+
default: return kNotDetermined;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// The process's one EKEventStore, created by the first EventKit verb and
|
|
235
|
+
// never released: a store is inert until a grant and creating one never
|
|
236
|
+
// prompts — only the request does — and a grant refreshes it in place (it
|
|
237
|
+
// posts EKEventStoreChangedNotification), so the store verbs that read
|
|
238
|
+
// calendars and reminders share this instance rather than making their own.
|
|
239
|
+
// Taken on the JS thread only: the fetches are thread-safe, the lazy
|
|
240
|
+
// creation is not.
|
|
241
|
+
EKEventStore* CALEventStore() {
|
|
242
|
+
static EKEventStore* store = nil;
|
|
243
|
+
if (!store) {
|
|
244
|
+
store = [EKEventStore new];
|
|
245
|
+
CALCalendarsObserveStore(store);
|
|
246
|
+
}
|
|
247
|
+
return store;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// The grant calendars.mm reads before a fetch, so a store that may not be
|
|
251
|
+
// read answers an error naming the status rather than an empty list. Apple's
|
|
252
|
+
// mapping stays here, in the one place that knows it.
|
|
253
|
+
const char* CALEventsAuthorizationStatus() { return EKStatus(EKEntityTypeEvent); }
|
|
254
|
+
|
|
154
255
|
// TCC's answer for sending Apple Events to `bundleId`. With `ask`, the call
|
|
155
256
|
// blocks while the consent dialog is up. procNotFound means the target is
|
|
156
257
|
// not running — TCC only answers for a running target, so the caller turns
|
|
@@ -185,19 +286,32 @@ static bool ThrowIfTargetNotRunning(Napi::Env env, const char* fn,
|
|
|
185
286
|
|
|
186
287
|
// --- the answer path -------------------------------------------------------
|
|
187
288
|
|
|
188
|
-
// Deliver one
|
|
289
|
+
// Deliver one answer to the request's callback as cb(granted, status) on the
|
|
189
290
|
// JS thread, from any thread, then let the thread-safe function go. The
|
|
190
291
|
// status strings are the static literals above, so a bare pointer travels.
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
292
|
+
struct Reply {
|
|
293
|
+
const char* status;
|
|
294
|
+
bool granted;
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
static void Answer(Napi::ThreadSafeFunction tsfn, const char* status,
|
|
298
|
+
bool granted) {
|
|
299
|
+
Reply* r = new Reply{status, granted};
|
|
300
|
+
napi_status st = tsfn.BlockingCall(
|
|
301
|
+
r, [](Napi::Env env, Napi::Function cb, Reply* r) {
|
|
302
|
+
cb.Call({Napi::Boolean::New(env, r->granted),
|
|
303
|
+
Napi::String::New(env, r->status)});
|
|
304
|
+
delete r;
|
|
197
305
|
});
|
|
306
|
+
if (st != napi_ok) delete r;
|
|
198
307
|
tsfn.Release();
|
|
199
308
|
}
|
|
200
309
|
|
|
310
|
+
// For every kind but the EventKit pair, granted is the one full grant.
|
|
311
|
+
static void Answer(Napi::ThreadSafeFunction tsfn, const char* status) {
|
|
312
|
+
Answer(tsfn, status, strcmp(status, kAuthorized) == 0);
|
|
313
|
+
}
|
|
314
|
+
|
|
201
315
|
// A location request: its own CLLocationManager whose delegate reports the
|
|
202
316
|
// decision. The delegate is also told the current status right after it is
|
|
203
317
|
// set, which is notDetermined while the prompt is still up — that echo is
|
|
@@ -238,13 +352,15 @@ static void StartLocationRequest(Napi::ThreadSafeFunction tsfn) {
|
|
|
238
352
|
// --- the natives -----------------------------------------------------------
|
|
239
353
|
|
|
240
354
|
// authorizationStatus(kind, opts?) -> 'authorized' | 'denied' | 'restricted'
|
|
241
|
-
// | 'notDetermined'. Never
|
|
355
|
+
// | 'notDetermined' | 'writeOnly' (calendars and reminders only). Never
|
|
356
|
+
// prompts.
|
|
242
357
|
static Napi::Value AuthorizationStatus(const Napi::CallbackInfo& info) {
|
|
243
358
|
Napi::Env env = info.Env();
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
if (!ParseKindArgs("authorizationStatus", info, &kind, &target))
|
|
359
|
+
KindArgs args;
|
|
360
|
+
if (!ParseKindArgs("authorizationStatus", info, &args))
|
|
247
361
|
return env.Undefined();
|
|
362
|
+
Kind kind = args.kind;
|
|
363
|
+
NSString* target = args.target;
|
|
248
364
|
@autoreleasepool {
|
|
249
365
|
const char* s = kNotDetermined;
|
|
250
366
|
switch (kind) {
|
|
@@ -265,19 +381,25 @@ static Napi::Value AuthorizationStatus(const Napi::CallbackInfo& info) {
|
|
|
265
381
|
break;
|
|
266
382
|
}
|
|
267
383
|
case Kind::Location: s = LocationStatusOf(LocationStatusManager()); break;
|
|
384
|
+
case Kind::Calendars:
|
|
385
|
+
case Kind::Reminders: s = EKStatus(EntityOf(kind)); break;
|
|
268
386
|
}
|
|
269
387
|
return Napi::String::New(env, s);
|
|
270
388
|
}
|
|
271
389
|
}
|
|
272
390
|
|
|
273
391
|
// requestAuthorization(kind, opts?, cb) — raises the system prompt where the
|
|
274
|
-
// framework offers one; cb(granted, status) once, asynchronously.
|
|
392
|
+
// framework offers one; cb(granted, status) once, asynchronously. granted is
|
|
393
|
+
// whether the level asked for is held afterwards: for every kind the full
|
|
394
|
+
// grant, and for a { access: 'write-only' } calendars request write-only or
|
|
395
|
+
// full (full access can save too).
|
|
275
396
|
static Napi::Value RequestAuthorization(const Napi::CallbackInfo& info) {
|
|
276
397
|
Napi::Env env = info.Env();
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if (!ParseKindArgs("requestAuthorization", info, &kind, &target))
|
|
398
|
+
KindArgs args;
|
|
399
|
+
if (!ParseKindArgs("requestAuthorization", info, &args))
|
|
280
400
|
return env.Undefined();
|
|
401
|
+
Kind kind = args.kind;
|
|
402
|
+
NSString* target = args.target;
|
|
281
403
|
size_t cbAt = info.Length() > 1 && info[1].IsFunction() ? 1 : 2;
|
|
282
404
|
if (info.Length() <= cbAt || !info[cbAt].IsFunction()) {
|
|
283
405
|
Napi::TypeError::New(env, "requestAuthorization: expected a callback "
|
|
@@ -339,6 +461,32 @@ static Napi::Value RequestAuthorization(const Napi::CallbackInfo& info) {
|
|
|
339
461
|
break;
|
|
340
462
|
}
|
|
341
463
|
case Kind::Location: StartLocationRequest(tsfn); break;
|
|
464
|
+
case Kind::Calendars:
|
|
465
|
+
case Kind::Reminders: {
|
|
466
|
+
EKEntityType type = EntityOf(kind);
|
|
467
|
+
bool writeOnly = args.writeOnly;
|
|
468
|
+
// the status after the request is the answer (a refusal reads back
|
|
469
|
+
// as denied, a prompt nobody saw leaves notDetermined); the
|
|
470
|
+
// completion's own flag is not consulted, as for AVFoundation
|
|
471
|
+
EKEventStoreRequestAccessCompletionHandler done = ^(BOOL, NSError*) {
|
|
472
|
+
const char* s = EKStatus(type);
|
|
473
|
+
Answer(tsfn, s, s == kAuthorized || (writeOnly && s == kWriteOnly));
|
|
474
|
+
};
|
|
475
|
+
EKEventStore* store = CALEventStore();
|
|
476
|
+
if (@available(macOS 14.0, *)) {
|
|
477
|
+
if (type == EKEntityTypeReminder)
|
|
478
|
+
[store requestFullAccessToRemindersWithCompletion:done];
|
|
479
|
+
else if (writeOnly)
|
|
480
|
+
[store requestWriteOnlyAccessToEventsWithCompletion:done];
|
|
481
|
+
else
|
|
482
|
+
[store requestFullAccessToEventsWithCompletion:done];
|
|
483
|
+
} else {
|
|
484
|
+
// 11–13 have one grant per entity and no write-only level, so a
|
|
485
|
+
// write-only request asks for the only grant there is
|
|
486
|
+
[store requestAccessToEntityType:type completion:done];
|
|
487
|
+
}
|
|
488
|
+
break;
|
|
489
|
+
}
|
|
342
490
|
}
|
|
343
491
|
}
|
|
344
492
|
return env.Undefined();
|
|
@@ -366,6 +514,8 @@ static Napi::Value OpenPrivacySettings(const Napi::CallbackInfo& info) {
|
|
|
366
514
|
else if (kind == "input-monitoring") anchor = "Privacy_ListenEvent";
|
|
367
515
|
else if (kind == "automation") anchor = "Privacy_Automation";
|
|
368
516
|
else if (kind == "location") anchor = "Privacy_LocationServices";
|
|
517
|
+
else if (kind == "calendars") anchor = "Privacy_Calendars";
|
|
518
|
+
else if (kind == "reminders") anchor = "Privacy_Reminders";
|
|
369
519
|
else if (kind == "files-and-folders") anchor = "Privacy_FilesAndFolders";
|
|
370
520
|
else if (kind == "full-disk-access") anchor = "Privacy_AllFiles";
|
|
371
521
|
if (!anchor) {
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// @windowkit/appkit screencolor.mm — one colour off the screen, through
|
|
2
|
+
// NSColorSampler: the eyedropper a colour picker's dropper button calls.
|
|
3
|
+
//
|
|
4
|
+
// The system draws the loupe out of process and hands back the one colour the
|
|
5
|
+
// user clicked, so nothing here reads the screen and the app needs no Screen
|
|
6
|
+
// Recording grant of its own. That is why this is the rung that belongs on
|
|
7
|
+
// top of react-x11's ladder rather than a fallback under the desktop
|
|
8
|
+
// portal's PickColor (windowkit/appkit#46): it is the same shape — the
|
|
9
|
+
// desktop samples, the app is told the answer — and the rung below it, a
|
|
10
|
+
// pointer grab and a 1×1 GetImage on the root window, has neither a pointer
|
|
11
|
+
// to grab nor a root window to read on Cocoa.
|
|
12
|
+
//
|
|
13
|
+
// One verb, mechanism only:
|
|
14
|
+
//
|
|
15
|
+
// sampleScreenColor(cb) cb(null, { r, g, b }) — sRGB, 0–1 floats, the
|
|
16
|
+
// portal's (ddd) shape — or cb(null, null) when the
|
|
17
|
+
// user dismissed the sampler without picking. A
|
|
18
|
+
// cancel is an ordinary outcome, not an error.
|
|
19
|
+
//
|
|
20
|
+
// What the colour means past that — the hex string a component paints with,
|
|
21
|
+
// whether a cancel resolves null or rejects — stays in the renderer.
|
|
22
|
+
//
|
|
23
|
+
// One at a time, the way NSColorSampler is: a show "begins or attaches to an
|
|
24
|
+
// existing color sampling session", its header says, so a second call while a
|
|
25
|
+
// loupe is up must not stack a second one. The bridge keeps that promise
|
|
26
|
+
// itself rather than leaving it to the framework — one session, and every
|
|
27
|
+
// caller waiting on it answered once with the same colour — because a
|
|
28
|
+
// callback that never fired would hold node's loop open forever.
|
|
29
|
+
//
|
|
30
|
+
// The answer is asynchronous, always: AppKit calls the handler on the main
|
|
31
|
+
// thread when the session ends, which is during the pump (the location grant
|
|
32
|
+
// in permissions.mm arrives the same way), and it crosses to JS through a
|
|
33
|
+
// thread-safe function, so a sampler still up holds the event loop open like
|
|
34
|
+
// pending I/O. Nothing dismisses it from code — AppKit offers no such call —
|
|
35
|
+
// so the session ends when the user picks a colour or presses Escape.
|
|
36
|
+
|
|
37
|
+
#include <napi.h>
|
|
38
|
+
#import <Cocoa/Cocoa.h>
|
|
39
|
+
|
|
40
|
+
#include <string>
|
|
41
|
+
#include <vector>
|
|
42
|
+
|
|
43
|
+
// src/backend.mm — NSApplication set up exactly once, whichever verb comes
|
|
44
|
+
// first. The sampler is AppKit UI like a panel, so it wants an app.
|
|
45
|
+
void BEnsureApp();
|
|
46
|
+
|
|
47
|
+
// What one finished session owes every caller waiting on it.
|
|
48
|
+
struct ColorAnswer {
|
|
49
|
+
bool cancelled = false; // dismissed without picking
|
|
50
|
+
double r = 0, g = 0, b = 0; // sRGB, 0–1
|
|
51
|
+
std::string error; // set instead when the colour could not be read
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// The callers of the session that is up, in the order they asked; empty when
|
|
55
|
+
// none is. Touched on the JS thread only: the verb runs there, and so does
|
|
56
|
+
// AppKit's handler — node's main thread is the process main thread, and the
|
|
57
|
+
// handler arrives on it inside the pump.
|
|
58
|
+
static std::vector<Napi::ThreadSafeFunction> gWaiting;
|
|
59
|
+
|
|
60
|
+
// cb(err) | cb(null, null) | cb(null, { r, g, b }) on the JS thread, then the
|
|
61
|
+
// thread-safe function goes — which is what held the loop open meanwhile.
|
|
62
|
+
static void Deliver(Napi::ThreadSafeFunction tsfn, const ColorAnswer& answer) {
|
|
63
|
+
ColorAnswer* a = new ColorAnswer(answer);
|
|
64
|
+
napi_status st = tsfn.BlockingCall(
|
|
65
|
+
a, [](Napi::Env env, Napi::Function cb, ColorAnswer* a) {
|
|
66
|
+
if (!a->error.empty()) {
|
|
67
|
+
cb.Call({Napi::Error::New(env, a->error).Value()});
|
|
68
|
+
} else if (a->cancelled) {
|
|
69
|
+
cb.Call({env.Null(), env.Null()});
|
|
70
|
+
} else {
|
|
71
|
+
Napi::Object c = Napi::Object::New(env);
|
|
72
|
+
c.Set("r", Napi::Number::New(env, a->r));
|
|
73
|
+
c.Set("g", Napi::Number::New(env, a->g));
|
|
74
|
+
c.Set("b", Napi::Number::New(env, a->b));
|
|
75
|
+
cb.Call({env.Null(), c});
|
|
76
|
+
}
|
|
77
|
+
delete a;
|
|
78
|
+
});
|
|
79
|
+
if (st != napi_ok) delete a;
|
|
80
|
+
tsfn.Release();
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The one place a session answers. The waiting list comes off first, so a
|
|
84
|
+
// sampleScreenColor called from inside a callback opens a fresh session
|
|
85
|
+
// rather than joining the one that has just ended (the file panels' rule).
|
|
86
|
+
static void FinishSession(NSColor* picked) {
|
|
87
|
+
ColorAnswer a;
|
|
88
|
+
if (!picked) {
|
|
89
|
+
a.cancelled = true;
|
|
90
|
+
} else {
|
|
91
|
+
// Whatever space the sampler read the pixel in — a display's, wide-gamut
|
|
92
|
+
// on most Macs now — in the one every colour crosses this bridge in (the
|
|
93
|
+
// 0.5.1 rule). ColorSync gamut-maps on the way, so a Display P3 red
|
|
94
|
+
// arrives as sRGB (1, 0, 0) rather than as components outside 0–1.
|
|
95
|
+
NSColor* srgb = [picked colorUsingColorSpace:NSColorSpace.sRGBColorSpace];
|
|
96
|
+
if (srgb) {
|
|
97
|
+
a.r = srgb.redComponent;
|
|
98
|
+
a.g = srgb.greenComponent;
|
|
99
|
+
a.b = srgb.blueComponent;
|
|
100
|
+
} else {
|
|
101
|
+
// Nothing the sampler can hand back should land here (a pattern colour
|
|
102
|
+
// is the shape with no sRGB form), but a colour that cannot be read is
|
|
103
|
+
// not a cancel, so it is said rather than answered as one.
|
|
104
|
+
a.error =
|
|
105
|
+
"sampleScreenColor: the sampler answered a colour with no sRGB form";
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
std::vector<Napi::ThreadSafeFunction> waiting;
|
|
109
|
+
waiting.swap(gWaiting);
|
|
110
|
+
for (Napi::ThreadSafeFunction& tsfn : waiting) Deliver(tsfn, a);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// sampleScreenColor(cb) — show the system sampler and answer once,
|
|
114
|
+
// asynchronously: cb(null, { r, g, b }) in sRGB, or cb(null, null) when the
|
|
115
|
+
// user dismissed it. A second call while a sampler is up joins that session
|
|
116
|
+
// instead of showing another, and both callbacks get the same answer.
|
|
117
|
+
static Napi::Value SampleScreenColor(const Napi::CallbackInfo& info) {
|
|
118
|
+
Napi::Env env = info.Env();
|
|
119
|
+
if (info.Length() < 1 || !info[0].IsFunction()) {
|
|
120
|
+
Napi::TypeError::New(env, "sampleScreenColor: expected a callback (cb)")
|
|
121
|
+
.ThrowAsJavaScriptException();
|
|
122
|
+
return env.Undefined();
|
|
123
|
+
}
|
|
124
|
+
@autoreleasepool {
|
|
125
|
+
BEnsureApp();
|
|
126
|
+
bool sessionUp = !gWaiting.empty();
|
|
127
|
+
gWaiting.push_back(Napi::ThreadSafeFunction::New(
|
|
128
|
+
env, info[0].As<Napi::Function>(), "appkit:sampleScreenColor", 0, 1));
|
|
129
|
+
if (sessionUp) return env.Undefined();
|
|
130
|
+
// Nothing here keeps the sampler alive: AppKit retains it for as long as
|
|
131
|
+
// the session lasts, which its header promises in as many words.
|
|
132
|
+
[[NSColorSampler new] showSamplerWithSelectionHandler:^(NSColor* c) {
|
|
133
|
+
FinishSession(c);
|
|
134
|
+
}];
|
|
135
|
+
}
|
|
136
|
+
return env.Undefined();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
// registration (called from addon.mm's Init)
|
|
141
|
+
// ---------------------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
void InitScreenColor(Napi::Env env, Napi::Object exports) {
|
|
144
|
+
exports.Set("sampleScreenColor",
|
|
145
|
+
Napi::Function::New(env, SampleScreenColor));
|
|
146
|
+
}
|