@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/notifications.mm
CHANGED
|
@@ -60,9 +60,8 @@
|
|
|
60
60
|
#include <string>
|
|
61
61
|
#include <vector>
|
|
62
62
|
|
|
63
|
-
// backend.mm: the one backend event
|
|
64
|
-
|
|
65
|
-
void CALEmitBackendEvent(Napi::Env env, Napi::Object ev);
|
|
63
|
+
// backend.mm / threaded.mm: the one backend event path
|
|
64
|
+
#include "channel.h"
|
|
66
65
|
|
|
67
66
|
static NSString* const kDefaultCategory = @"appkit.default";
|
|
68
67
|
static NSString* const kUserInfoKey = @"appkit.userInfo";
|
|
@@ -158,62 +157,71 @@ struct NotifEvent {
|
|
|
158
157
|
|
|
159
158
|
static void CallJsEvent(Napi::Env env, Napi::Function, void*, NotifEvent* ev);
|
|
160
159
|
using EventTsfn = Napi::TypedThreadSafeFunction<void, NotifEvent, CallJsEvent>;
|
|
161
|
-
|
|
160
|
+
// Made in the first environment to reach the centre (ProbeCenter) and called
|
|
161
|
+
// from the centre's own thread, so through CALTsfn: once that environment
|
|
162
|
+
// has begun to end, an event is dropped rather than sent into it.
|
|
163
|
+
static CALTsfn gEvents;
|
|
162
164
|
static std::vector<NotifEvent*> gHeld;
|
|
163
165
|
|
|
164
|
-
static
|
|
165
|
-
|
|
166
|
-
o.
|
|
167
|
-
o.
|
|
168
|
-
if (!ev.
|
|
169
|
-
if (
|
|
170
|
-
o.
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (ev.hasUserInfo)
|
|
174
|
-
|
|
175
|
-
if (env.IsExceptionPending()) {
|
|
176
|
-
// not our own output after all; hand it over as it is rather than fail
|
|
177
|
-
(void)env.GetAndClearPendingException();
|
|
178
|
-
v = Napi::String::New(env, ev.userInfoJson);
|
|
179
|
-
}
|
|
180
|
-
o.Set("userInfo", v);
|
|
181
|
-
}
|
|
182
|
-
if (ev.hasUserText) o.Set("userText", ev.userText);
|
|
166
|
+
static CALEvent EventRecord(const NotifEvent& ev) {
|
|
167
|
+
CALEvent o(ev.type.c_str());
|
|
168
|
+
o.Str("identifier", ev.identifier);
|
|
169
|
+
if (!ev.actionId.empty()) o.Str("actionId", ev.actionId);
|
|
170
|
+
if (!ev.reason.empty()) o.Str("reason", ev.reason);
|
|
171
|
+
if (ev.categoryId.empty()) o.Null("categoryId");
|
|
172
|
+
else o.Str("categoryId", ev.categoryId);
|
|
173
|
+
// parsed as it crosses; text that is not our own output after all is
|
|
174
|
+
// handed over as it is rather than fail
|
|
175
|
+
if (ev.hasUserInfo) o.Json("userInfo", ev.userInfoJson);
|
|
176
|
+
if (ev.hasUserText) o.Str("userText", ev.userText);
|
|
183
177
|
return o;
|
|
184
178
|
}
|
|
185
179
|
|
|
186
|
-
static void EmitOrHold(
|
|
187
|
-
if (!
|
|
180
|
+
static void EmitOrHold(NotifEvent* ev) {
|
|
181
|
+
if (!CALListening()) {
|
|
188
182
|
gHeld.push_back(ev);
|
|
189
183
|
return;
|
|
190
184
|
}
|
|
191
|
-
|
|
192
|
-
CALEmitBackendEvent(env, EventObject(env, *ev));
|
|
185
|
+
CALEmit(EventRecord(*ev));
|
|
193
186
|
delete ev;
|
|
194
187
|
}
|
|
195
188
|
|
|
196
|
-
// Called at the start of pump2
|
|
197
|
-
// goes out ahead of that
|
|
198
|
-
|
|
199
|
-
|
|
189
|
+
// Called at the start of pump2, and as runMain opens the channel
|
|
190
|
+
// (backend.mm): what arrived before a listener goes out ahead of that
|
|
191
|
+
// tick's input, in arrival order.
|
|
192
|
+
void CALNotificationsReplayHeld() {
|
|
193
|
+
if (gHeld.empty() || !CALListening()) return;
|
|
200
194
|
std::vector<NotifEvent*> held;
|
|
201
195
|
held.swap(gHeld);
|
|
202
|
-
for (NotifEvent* ev : held) EmitOrHold(
|
|
196
|
+
for (NotifEvent* ev : held) EmitOrHold(ev);
|
|
203
197
|
}
|
|
204
198
|
|
|
205
199
|
static void CallJsEvent(Napi::Env env, Napi::Function, void*, NotifEvent* ev) {
|
|
206
|
-
|
|
200
|
+
// the function is being torn down, or the environment is (channel.h)
|
|
201
|
+
if ((napi_env)env == nullptr || !CALCanCallIntoJS(env)) {
|
|
207
202
|
delete ev;
|
|
208
203
|
return;
|
|
209
204
|
}
|
|
210
|
-
CALNotificationsReplayHeld(
|
|
211
|
-
EmitOrHold(
|
|
205
|
+
CALNotificationsReplayHeld(); // keep order behind anything still held
|
|
206
|
+
EmitOrHold(ev);
|
|
207
|
+
// pump mode's inline delivery leaves a listener's exception pending; from
|
|
208
|
+
// a threadsafe function's callback it is the uncaught exception (#62)
|
|
209
|
+
CALRaiseUncaughtIfPending(env);
|
|
212
210
|
}
|
|
213
211
|
|
|
214
|
-
// From any thread: onto node's loop, then to the listener or the held list
|
|
212
|
+
// From any thread: onto node's loop, then to the listener or the held list
|
|
213
|
+
// — in pump mode. With threaded mode's channel open the record goes straight
|
|
214
|
+
// into it from the centre's own thread.
|
|
215
215
|
static void QueueEvent(NotifEvent* ev) {
|
|
216
|
-
if (
|
|
216
|
+
if (CALChannelOpen()) {
|
|
217
|
+
CALEmit(EventRecord(*ev));
|
|
218
|
+
delete ev;
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
bool queued = gEvents.Use(false, [ev](napi_threadsafe_function f) {
|
|
222
|
+
return EventTsfn(f).NonBlockingCall(ev);
|
|
223
|
+
});
|
|
224
|
+
if (!queued) delete ev;
|
|
217
225
|
}
|
|
218
226
|
|
|
219
227
|
static void QueueResponse(UNNotificationResponse* r) {
|
|
@@ -285,8 +293,9 @@ static UNNotificationCategory* DefaultCategory() {
|
|
|
285
293
|
static void ProbeCenter(Napi::Env env) {
|
|
286
294
|
if (gProbed) return;
|
|
287
295
|
gProbed = true;
|
|
288
|
-
|
|
289
|
-
|
|
296
|
+
EventTsfn events = EventTsfn::New(env, "appkit:notifications", 0, 1);
|
|
297
|
+
events.Unref(env); // events never hold the loop open by themselves
|
|
298
|
+
gEvents = CALTsfn::Watch(env, events);
|
|
290
299
|
@autoreleasepool {
|
|
291
300
|
NSBundle* main = NSBundle.mainBundle;
|
|
292
301
|
if (!main.bundleIdentifier) {
|
|
@@ -411,24 +420,26 @@ static Napi::Value NotificationSettings(const Napi::CallbackInfo& info) {
|
|
|
411
420
|
Napi::Env env = info.Env();
|
|
412
421
|
if (!CallbackArg(info, 0, "notificationSettings", "(cb)"))
|
|
413
422
|
return env.Undefined();
|
|
414
|
-
|
|
415
|
-
|
|
423
|
+
CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
|
|
424
|
+
"appkit:notificationSettings");
|
|
416
425
|
if (!gCenter) {
|
|
417
|
-
tsfn.
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
426
|
+
tsfn.Answer((void*)nullptr, [](Napi::Env env, Napi::Function cb, void*) {
|
|
427
|
+
// an environment on its way out gets no answer (channel.h)
|
|
428
|
+
if (!CALCanCallIntoJS(env)) return;
|
|
429
|
+
CALCallJS(env, cb, {UnavailableObject(env)});
|
|
430
|
+
});
|
|
422
431
|
return env.Undefined();
|
|
423
432
|
}
|
|
424
433
|
[gCenter getNotificationSettingsWithCompletionHandler:^(
|
|
425
434
|
UNNotificationSettings* s) {
|
|
426
435
|
void* p = (void*)CFBridgingRetain(s);
|
|
427
|
-
tsfn.
|
|
436
|
+
bool queued = tsfn.Answer(p, [](Napi::Env env, Napi::Function cb, void* p) {
|
|
428
437
|
UNNotificationSettings* s = CFBridgingRelease(p);
|
|
429
|
-
|
|
438
|
+
// an environment on its way out gets no answer (channel.h)
|
|
439
|
+
if (!CALCanCallIntoJS(env)) return;
|
|
440
|
+
CALCallJS(env, cb, {SettingsObject(env, s)});
|
|
430
441
|
});
|
|
431
|
-
|
|
442
|
+
if (!queued) CFRelease(p);
|
|
432
443
|
}];
|
|
433
444
|
return env.Undefined();
|
|
434
445
|
}
|
|
@@ -493,23 +504,30 @@ static Napi::Value RequestNotificationAuthorization(
|
|
|
493
504
|
if (!CallbackArg(info, 1, fn, "(options, cb)")) return env.Undefined();
|
|
494
505
|
UNUserNotificationCenter* center = CenterOrThrow(env, fn);
|
|
495
506
|
if (!center) return env.Undefined();
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
0, 1);
|
|
507
|
+
CALTsfn tsfn = CALTsfn::New(env, info[1].As<Napi::Function>(),
|
|
508
|
+
"appkit:requestNotificationAuthorization");
|
|
499
509
|
[center requestAuthorizationWithOptions:opts
|
|
500
510
|
completionHandler:^(BOOL granted, NSError* error) {
|
|
501
511
|
Outcome* out = new Outcome{(bool)granted, error};
|
|
502
|
-
tsfn.
|
|
512
|
+
bool queued = tsfn.Answer(
|
|
503
513
|
out, [](Napi::Env env, Napi::Function cb,
|
|
504
514
|
Outcome* out) {
|
|
505
515
|
if (out->ok && gCenter && gCategories) {
|
|
506
516
|
[gCenter setNotificationCategories:gCategories];
|
|
507
517
|
}
|
|
508
|
-
|
|
509
|
-
|
|
518
|
+
// an environment on its way out gets no
|
|
519
|
+
// answer (channel.h); the categories are the
|
|
520
|
+
// process's, re-applied either way
|
|
521
|
+
if (!CALCanCallIntoJS(env)) {
|
|
522
|
+
delete out;
|
|
523
|
+
return;
|
|
524
|
+
}
|
|
525
|
+
CALCallJS(env, cb,
|
|
526
|
+
{Napi::Boolean::New(env, out->ok),
|
|
527
|
+
ErrorValue(env, out->error)});
|
|
510
528
|
delete out;
|
|
511
529
|
});
|
|
512
|
-
|
|
530
|
+
if (!queued) delete out;
|
|
513
531
|
}];
|
|
514
532
|
return env.Undefined();
|
|
515
533
|
}
|
|
@@ -639,19 +657,21 @@ static Napi::Value NotificationCategories(const Napi::CallbackInfo& info) {
|
|
|
639
657
|
if (!CallbackArg(info, 0, fn, "(cb)")) return env.Undefined();
|
|
640
658
|
UNUserNotificationCenter* center = CenterOrThrow(env, fn);
|
|
641
659
|
if (!center) return env.Undefined();
|
|
642
|
-
|
|
643
|
-
|
|
660
|
+
CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
|
|
661
|
+
"appkit:notificationCategories");
|
|
644
662
|
[center getNotificationCategoriesWithCompletionHandler:^(
|
|
645
663
|
NSSet<UNNotificationCategory*>* cats) {
|
|
646
664
|
void* p = (void*)CFBridgingRetain(cats);
|
|
647
|
-
tsfn.
|
|
665
|
+
bool queued = tsfn.Answer(p, [](Napi::Env env, Napi::Function cb, void* p) {
|
|
648
666
|
NSSet<UNNotificationCategory*>* cats = CFBridgingRelease(p);
|
|
667
|
+
// an environment on its way out gets no answer (channel.h)
|
|
668
|
+
if (!CALCanCallIntoJS(env)) return;
|
|
649
669
|
Napi::Array out = Napi::Array::New(env);
|
|
650
670
|
uint32_t n = 0;
|
|
651
671
|
for (UNNotificationCategory* c in cats) out.Set(n++, CategoryObject(env, c));
|
|
652
|
-
cb
|
|
672
|
+
CALCallJS(env, cb, {out});
|
|
653
673
|
});
|
|
654
|
-
|
|
674
|
+
if (!queued) CFRelease(p);
|
|
655
675
|
}];
|
|
656
676
|
return env.Undefined();
|
|
657
677
|
}
|
|
@@ -744,18 +764,23 @@ static Napi::Value Post(const Napi::CallbackInfo& info, size_t propsAt,
|
|
|
744
764
|
if (!hasCb) {
|
|
745
765
|
[center addNotificationRequest:req withCompletionHandler:nil];
|
|
746
766
|
} else {
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
1);
|
|
767
|
+
CALTsfn tsfn = CALTsfn::New(env, info[cbAt].As<Napi::Function>(),
|
|
768
|
+
"appkit:postNotification");
|
|
750
769
|
[center addNotificationRequest:req
|
|
751
770
|
withCompletionHandler:^(NSError* error) {
|
|
752
771
|
Outcome* out = new Outcome{error == nil, error};
|
|
753
|
-
tsfn.
|
|
772
|
+
bool queued = tsfn.Answer(
|
|
754
773
|
out, [](Napi::Env env, Napi::Function cb, Outcome* out) {
|
|
755
|
-
|
|
774
|
+
// an environment on its way out gets no answer
|
|
775
|
+
// (channel.h)
|
|
776
|
+
if (!CALCanCallIntoJS(env)) {
|
|
777
|
+
delete out;
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
CALCallJS(env, cb, {ErrorValue(env, out->error)});
|
|
756
781
|
delete out;
|
|
757
782
|
});
|
|
758
|
-
|
|
783
|
+
if (!queued) delete out;
|
|
759
784
|
}];
|
|
760
785
|
}
|
|
761
786
|
return Napi::String::New(env, req.identifier.UTF8String);
|
|
@@ -819,13 +844,15 @@ static Napi::Value DeliveredNotifications(const Napi::CallbackInfo& info) {
|
|
|
819
844
|
if (!CallbackArg(info, 0, fn, "(cb)")) return env.Undefined();
|
|
820
845
|
UNUserNotificationCenter* center = CenterOrThrow(env, fn);
|
|
821
846
|
if (!center) return env.Undefined();
|
|
822
|
-
|
|
823
|
-
|
|
847
|
+
CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
|
|
848
|
+
"appkit:deliveredNotifications");
|
|
824
849
|
[center getDeliveredNotificationsWithCompletionHandler:^(
|
|
825
850
|
NSArray<UNNotification*>* list) {
|
|
826
851
|
void* p = (void*)CFBridgingRetain(list);
|
|
827
|
-
tsfn.
|
|
852
|
+
bool queued = tsfn.Answer(p, [](Napi::Env env, Napi::Function cb, void* p) {
|
|
828
853
|
NSArray<UNNotification*>* list = CFBridgingRelease(p);
|
|
854
|
+
// an environment on its way out gets no answer (channel.h)
|
|
855
|
+
if (!CALCanCallIntoJS(env)) return;
|
|
829
856
|
Napi::Array out = Napi::Array::New(env);
|
|
830
857
|
uint32_t n = 0;
|
|
831
858
|
for (UNNotification* note in list) {
|
|
@@ -854,9 +881,9 @@ static Napi::Value DeliveredNotifications(const Napi::CallbackInfo& info) {
|
|
|
854
881
|
o.Set("date", note.date.timeIntervalSince1970 * 1000.0);
|
|
855
882
|
out.Set(n++, o);
|
|
856
883
|
}
|
|
857
|
-
cb
|
|
884
|
+
CALCallJS(env, cb, {out});
|
|
858
885
|
});
|
|
859
|
-
|
|
886
|
+
if (!queued) CFRelease(p);
|
|
860
887
|
}];
|
|
861
888
|
return env.Undefined();
|
|
862
889
|
}
|
package/src/permissions.mm
CHANGED
|
@@ -56,6 +56,8 @@
|
|
|
56
56
|
#include <cstring>
|
|
57
57
|
#include <string>
|
|
58
58
|
|
|
59
|
+
#include "channel.h" // CALOnUI, CALCanCallIntoJS
|
|
60
|
+
|
|
59
61
|
// src/calendars.mm — the change observer the process's store gets as it is
|
|
60
62
|
// created, so EKEventStoreChangedNotification is watched from the store's
|
|
61
63
|
// first moment whichever verb made it.
|
|
@@ -294,21 +296,24 @@ struct Reply {
|
|
|
294
296
|
bool granted;
|
|
295
297
|
};
|
|
296
298
|
|
|
297
|
-
static void Answer(
|
|
298
|
-
bool granted) {
|
|
299
|
+
static void Answer(const CALTsfn& tsfn, const char* status, bool granted) {
|
|
299
300
|
Reply* r = new Reply{status, granted};
|
|
300
|
-
|
|
301
|
+
bool queued = tsfn.Answer(
|
|
301
302
|
r, [](Napi::Env env, Napi::Function cb, Reply* r) {
|
|
302
|
-
|
|
303
|
-
|
|
303
|
+
// an environment on its way out gets no answer (channel.h)
|
|
304
|
+
if (!CALCanCallIntoJS(env)) {
|
|
305
|
+
delete r;
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
CALCallJS(env, cb, {Napi::Boolean::New(env, r->granted),
|
|
309
|
+
Napi::String::New(env, r->status)});
|
|
304
310
|
delete r;
|
|
305
311
|
});
|
|
306
|
-
if (
|
|
307
|
-
tsfn.Release();
|
|
312
|
+
if (!queued) delete r;
|
|
308
313
|
}
|
|
309
314
|
|
|
310
315
|
// For every kind but the EventKit pair, granted is the one full grant.
|
|
311
|
-
static void Answer(
|
|
316
|
+
static void Answer(const CALTsfn& tsfn, const char* status) {
|
|
312
317
|
Answer(tsfn, status, strcmp(status, kAuthorized) == 0);
|
|
313
318
|
}
|
|
314
319
|
|
|
@@ -319,7 +324,7 @@ static void Answer(Napi::ThreadSafeFunction tsfn, const char* status) {
|
|
|
319
324
|
@interface CALLocationRequest : NSObject <CLLocationManagerDelegate> {
|
|
320
325
|
@public
|
|
321
326
|
CLLocationManager* mgr_;
|
|
322
|
-
|
|
327
|
+
CALTsfn tsfn_;
|
|
323
328
|
bool done_;
|
|
324
329
|
}
|
|
325
330
|
@end
|
|
@@ -338,15 +343,20 @@ static NSMutableArray<CALLocationRequest*>* gLocationRequests = nil;
|
|
|
338
343
|
}
|
|
339
344
|
@end
|
|
340
345
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
346
|
+
// The manager reports on the run loop of the thread that made it, and a
|
|
347
|
+
// worker has none: it is made on the UI thread (inline in pump mode, a
|
|
348
|
+
// command in threaded mode), where the delegate call then arrives.
|
|
349
|
+
static void StartLocationRequest(CALTsfn tsfn) { // by value: the block copies it
|
|
350
|
+
CALOnUI(^{
|
|
351
|
+
CALLocationRequest* r = [CALLocationRequest new];
|
|
352
|
+
r->tsfn_ = tsfn;
|
|
353
|
+
r->done_ = false;
|
|
354
|
+
r->mgr_ = [CLLocationManager new];
|
|
355
|
+
r->mgr_.delegate = r;
|
|
356
|
+
if (!gLocationRequests) gLocationRequests = [NSMutableArray new];
|
|
357
|
+
[gLocationRequests addObject:r];
|
|
358
|
+
[r->mgr_ requestWhenInUseAuthorization];
|
|
359
|
+
});
|
|
350
360
|
}
|
|
351
361
|
|
|
352
362
|
// --- the natives -----------------------------------------------------------
|
|
@@ -408,9 +418,8 @@ static Napi::Value RequestAuthorization(const Napi::CallbackInfo& info) {
|
|
|
408
418
|
return env.Undefined();
|
|
409
419
|
}
|
|
410
420
|
@autoreleasepool {
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
0, 1);
|
|
421
|
+
CALTsfn tsfn = CALTsfn::New(env, info[cbAt].As<Napi::Function>(),
|
|
422
|
+
"appkit:requestAuthorization");
|
|
414
423
|
switch (kind) {
|
|
415
424
|
case Kind::Camera:
|
|
416
425
|
case Kind::Microphone: {
|
package/src/screencolor.mm
CHANGED
|
@@ -40,6 +40,8 @@
|
|
|
40
40
|
#include <string>
|
|
41
41
|
#include <vector>
|
|
42
42
|
|
|
43
|
+
#include "channel.h" // CALOnUI: the session is the UI thread's; CALCallJS
|
|
44
|
+
|
|
43
45
|
// src/backend.mm — NSApplication set up exactly once, whichever verb comes
|
|
44
46
|
// first. The sampler is AppKit UI like a panel, so it wants an app.
|
|
45
47
|
void BEnsureApp();
|
|
@@ -52,32 +54,36 @@ struct ColorAnswer {
|
|
|
52
54
|
};
|
|
53
55
|
|
|
54
56
|
// The callers of the session that is up, in the order they asked; empty when
|
|
55
|
-
// none is. Touched on the
|
|
56
|
-
//
|
|
57
|
-
// handler
|
|
58
|
-
static std::vector<
|
|
57
|
+
// none is. Touched on the UI thread only: the verb's own part runs there
|
|
58
|
+
// (inline in pump mode, a command from a worker), and so does AppKit's
|
|
59
|
+
// handler, inside the pump or the run.
|
|
60
|
+
static std::vector<CALTsfn> gWaiting;
|
|
59
61
|
|
|
60
62
|
// cb(err) | cb(null, null) | cb(null, { r, g, b }) on the JS thread, then the
|
|
61
63
|
// thread-safe function goes — which is what held the loop open meanwhile.
|
|
62
|
-
static void Deliver(
|
|
64
|
+
static void Deliver(const CALTsfn& tsfn, const ColorAnswer& answer) {
|
|
63
65
|
ColorAnswer* a = new ColorAnswer(answer);
|
|
64
|
-
|
|
66
|
+
bool queued = tsfn.Answer(
|
|
65
67
|
a, [](Napi::Env env, Napi::Function cb, ColorAnswer* a) {
|
|
68
|
+
// an environment on its way out gets no answer (channel.h)
|
|
69
|
+
if (!CALCanCallIntoJS(env)) {
|
|
70
|
+
delete a;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
66
73
|
if (!a->error.empty()) {
|
|
67
|
-
cb
|
|
74
|
+
CALCallJS(env, cb, {Napi::Error::New(env, a->error).Value()});
|
|
68
75
|
} else if (a->cancelled) {
|
|
69
|
-
cb
|
|
76
|
+
CALCallJS(env, cb, {env.Null(), env.Null()});
|
|
70
77
|
} else {
|
|
71
78
|
Napi::Object c = Napi::Object::New(env);
|
|
72
79
|
c.Set("r", Napi::Number::New(env, a->r));
|
|
73
80
|
c.Set("g", Napi::Number::New(env, a->g));
|
|
74
81
|
c.Set("b", Napi::Number::New(env, a->b));
|
|
75
|
-
cb
|
|
82
|
+
CALCallJS(env, cb, {env.Null(), c});
|
|
76
83
|
}
|
|
77
84
|
delete a;
|
|
78
85
|
});
|
|
79
|
-
if (
|
|
80
|
-
tsfn.Release();
|
|
86
|
+
if (!queued) delete a;
|
|
81
87
|
}
|
|
82
88
|
|
|
83
89
|
// The one place a session answers. The waiting list comes off first, so a
|
|
@@ -105,9 +111,9 @@ static void FinishSession(NSColor* picked) {
|
|
|
105
111
|
"sampleScreenColor: the sampler answered a colour with no sRGB form";
|
|
106
112
|
}
|
|
107
113
|
}
|
|
108
|
-
std::vector<
|
|
114
|
+
std::vector<CALTsfn> waiting;
|
|
109
115
|
waiting.swap(gWaiting);
|
|
110
|
-
for (
|
|
116
|
+
for (const CALTsfn& tsfn : waiting) Deliver(tsfn, a);
|
|
111
117
|
}
|
|
112
118
|
|
|
113
119
|
// sampleScreenColor(cb) — show the system sampler and answer once,
|
|
@@ -121,18 +127,22 @@ static Napi::Value SampleScreenColor(const Napi::CallbackInfo& info) {
|
|
|
121
127
|
.ThrowAsJavaScriptException();
|
|
122
128
|
return env.Undefined();
|
|
123
129
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
130
|
+
// made in the caller's environment, which is the one answered
|
|
131
|
+
CALTsfn tsfn = CALTsfn::New(env, info[0].As<Napi::Function>(),
|
|
132
|
+
"appkit:sampleScreenColor");
|
|
133
|
+
CALOnUI(^{
|
|
134
|
+
@autoreleasepool {
|
|
135
|
+
BEnsureApp();
|
|
136
|
+
bool sessionUp = !gWaiting.empty();
|
|
137
|
+
gWaiting.push_back(tsfn);
|
|
138
|
+
if (sessionUp) return;
|
|
139
|
+
// Nothing here keeps the sampler alive: AppKit retains it for as long
|
|
140
|
+
// as the session lasts, which its header promises in as many words.
|
|
141
|
+
[[NSColorSampler new] showSamplerWithSelectionHandler:^(NSColor* c) {
|
|
142
|
+
FinishSession(c);
|
|
143
|
+
}];
|
|
144
|
+
}
|
|
145
|
+
});
|
|
136
146
|
return env.Undefined();
|
|
137
147
|
}
|
|
138
148
|
|