@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/addon.mm
CHANGED
|
@@ -14,7 +14,9 @@
|
|
|
14
14
|
#import <ImageIO/ImageIO.h>
|
|
15
15
|
|
|
16
16
|
#include <cmath>
|
|
17
|
+
#include <cstdio>
|
|
17
18
|
#include <string>
|
|
19
|
+
#include <vector>
|
|
18
20
|
|
|
19
21
|
// ---------------------------------------------------------------------------
|
|
20
22
|
// helpers
|
|
@@ -88,6 +90,34 @@ static T Deref(Napi::Value v) {
|
|
|
88
90
|
return (__bridge T)(v.As<Napi::External<void>>().Data());
|
|
89
91
|
}
|
|
90
92
|
|
|
93
|
+
#include <pthread.h>
|
|
94
|
+
|
|
95
|
+
#include "channel.h"
|
|
96
|
+
|
|
97
|
+
// The first-generation window API (createWindow, setEventCallback, pump,
|
|
98
|
+
// closeWindow, windowScale, windowContentSize, hitTest, drawControl,
|
|
99
|
+
// appearanceIsDark) is pump mode's: called off the main thread it is an
|
|
100
|
+
// Error, never a crash on AppKit's thread checks. The verbs the backend's
|
|
101
|
+
// windows share (windowRootLayer, windowNumber, windowIsVisible,
|
|
102
|
+
// snapshotWindow, postMouseEvent) follow threaded mode instead.
|
|
103
|
+
static bool PumpModeOnly(const Napi::CallbackInfo& info, const char* name) {
|
|
104
|
+
if (pthread_main_np()) return true;
|
|
105
|
+
Napi::Error::New(info.Env(), std::string(name) +
|
|
106
|
+
": the first-generation API is pump mode's — "
|
|
107
|
+
"call it on the main thread")
|
|
108
|
+
.ThrowAsJavaScriptException();
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// windowIsVisible from the published copy (backend.mm).
|
|
113
|
+
bool CALWindowVisible(long number, bool* visible);
|
|
114
|
+
|
|
115
|
+
// A worker's window handle (createWindow2 off the main thread), or nil.
|
|
116
|
+
static CALHandle* WindowHandleOf(Napi::Value v) {
|
|
117
|
+
id target = CALHandleTarget(v);
|
|
118
|
+
return [target isKindOfClass:[CALHandle class]] ? (CALHandle*)target : nil;
|
|
119
|
+
}
|
|
120
|
+
|
|
91
121
|
// Wrap an ObjC object as an External holding a +1 retain, released on GC.
|
|
92
122
|
static Napi::Value WrapRetained(Napi::Env env, id obj) {
|
|
93
123
|
void* p = (void*)CFBridgingRetain(obj);
|
|
@@ -117,6 +147,7 @@ static void EnsureApp() { BEnsureApp(); }
|
|
|
117
147
|
|
|
118
148
|
static Napi::Value CreateWindowFn(const Napi::CallbackInfo& info) {
|
|
119
149
|
Napi::Env env = info.Env();
|
|
150
|
+
if (!PumpModeOnly(info, "createWindow")) return env.Undefined();
|
|
120
151
|
EnsureApp();
|
|
121
152
|
double w = info[0].As<Napi::Number>().DoubleValue();
|
|
122
153
|
double h = info[1].As<Napi::Number>().DoubleValue();
|
|
@@ -153,17 +184,26 @@ static Napi::Value CreateWindowFn(const Napi::CallbackInfo& info) {
|
|
|
153
184
|
return WrapRetained(env, win);
|
|
154
185
|
}
|
|
155
186
|
|
|
187
|
+
// From a worker: the root layer's handle, allocated with the window's (the
|
|
188
|
+
// layer verbs that take it are windowkit/appkit#52's).
|
|
156
189
|
static Napi::Value WindowRootLayer(const Napi::CallbackInfo& info) {
|
|
157
|
-
|
|
190
|
+
if (!pthread_main_np()) {
|
|
191
|
+
CALHandle* h = WindowHandleOf(info[0]);
|
|
192
|
+
if (!h || !h->part_) return info.Env().Null();
|
|
193
|
+
return CALWrapHandle(info.Env(), h->part_, false);
|
|
194
|
+
}
|
|
195
|
+
NSWindow* win = CALResolve(CALHandleTarget(info[0]));
|
|
158
196
|
return WrapRetained(info.Env(), win.contentView.layer);
|
|
159
197
|
}
|
|
160
198
|
|
|
161
199
|
static Napi::Value WindowScale(const Napi::CallbackInfo& info) {
|
|
200
|
+
if (!PumpModeOnly(info, "windowScale")) return info.Env().Undefined();
|
|
162
201
|
NSWindow* win = Deref<NSWindow*>(info[0]);
|
|
163
202
|
return Napi::Number::New(info.Env(), win.backingScaleFactor);
|
|
164
203
|
}
|
|
165
204
|
|
|
166
205
|
static Napi::Value WindowContentSize(const Napi::CallbackInfo& info) {
|
|
206
|
+
if (!PumpModeOnly(info, "windowContentSize")) return info.Env().Undefined();
|
|
167
207
|
NSWindow* win = Deref<NSWindow*>(info[0]);
|
|
168
208
|
NSSize s = win.contentView.bounds.size;
|
|
169
209
|
Napi::Array a = Napi::Array::New(info.Env(), 2);
|
|
@@ -172,17 +212,31 @@ static Napi::Value WindowContentSize(const Napi::CallbackInfo& info) {
|
|
|
172
212
|
return a;
|
|
173
213
|
}
|
|
174
214
|
|
|
215
|
+
// From a worker: the published copy, null until the window is made.
|
|
175
216
|
static Napi::Value WindowIsVisible(const Napi::CallbackInfo& info) {
|
|
176
|
-
|
|
217
|
+
if (!pthread_main_np()) {
|
|
218
|
+
CALHandle* h = WindowHandleOf(info[0]);
|
|
219
|
+
bool visible = false;
|
|
220
|
+
if (!h || !CALWindowVisible(h->number_.load(), &visible)) return info.Env().Null();
|
|
221
|
+
return Napi::Boolean::New(info.Env(), visible);
|
|
222
|
+
}
|
|
223
|
+
NSWindow* win = CALResolve(CALHandleTarget(info[0]));
|
|
177
224
|
return Napi::Boolean::New(info.Env(), win.isVisible);
|
|
178
225
|
}
|
|
179
226
|
|
|
227
|
+
// From a worker: the number once the window is made, null before.
|
|
180
228
|
static Napi::Value WindowNumber(const Napi::CallbackInfo& info) {
|
|
181
|
-
|
|
229
|
+
if (!pthread_main_np()) {
|
|
230
|
+
CALHandle* h = WindowHandleOf(info[0]);
|
|
231
|
+
long n = h ? h->number_.load() : 0;
|
|
232
|
+
return n ? Napi::Value(Napi::Number::New(info.Env(), (double)n)) : info.Env().Null();
|
|
233
|
+
}
|
|
234
|
+
NSWindow* win = CALResolve(CALHandleTarget(info[0]));
|
|
182
235
|
return Napi::Number::New(info.Env(), (double)win.windowNumber);
|
|
183
236
|
}
|
|
184
237
|
|
|
185
238
|
static Napi::Value CloseWindow(const Napi::CallbackInfo& info) {
|
|
239
|
+
if (!PumpModeOnly(info, "closeWindow")) return info.Env().Undefined();
|
|
186
240
|
NSWindow* win = Deref<NSWindow*>(info[0]);
|
|
187
241
|
[win close];
|
|
188
242
|
return info.Env().Undefined();
|
|
@@ -235,8 +289,9 @@ static void DispatchEvent(Napi::Env env, NSEvent* e) {
|
|
|
235
289
|
|
|
236
290
|
// postMouseEvent(win, 'down'|'up'|'move'|'drag', x, y) — synthesizes an event
|
|
237
291
|
// through the normal pump path (top-left coords). Handy for automated tests.
|
|
292
|
+
// A command in threaded mode, like every test-only post.
|
|
238
293
|
static Napi::Value PostMouseEvent(const Napi::CallbackInfo& info) {
|
|
239
|
-
|
|
294
|
+
id target = CALHandleTarget(info[0]);
|
|
240
295
|
std::string t = info[1].As<Napi::String>().Utf8Value();
|
|
241
296
|
double x = info[2].As<Napi::Number>().DoubleValue();
|
|
242
297
|
double y = info[3].As<Napi::Number>().DoubleValue();
|
|
@@ -245,22 +300,27 @@ static Napi::Value PostMouseEvent(const Napi::CallbackInfo& info) {
|
|
|
245
300
|
else if (t == "up") type = NSEventTypeLeftMouseUp;
|
|
246
301
|
else if (t == "drag") type = NSEventTypeLeftMouseDragged;
|
|
247
302
|
else type = NSEventTypeMouseMoved;
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
303
|
+
CALOnUI(^{
|
|
304
|
+
NSWindow* win = CALResolve(target);
|
|
305
|
+
if (!win) return;
|
|
306
|
+
NSView* v = win.contentView;
|
|
307
|
+
NSPoint wp = [v convertPoint:NSMakePoint(x, y) toView:nil]; // v is flipped
|
|
308
|
+
NSEvent* e = [NSEvent mouseEventWithType:type
|
|
309
|
+
location:wp
|
|
310
|
+
modifierFlags:0
|
|
311
|
+
timestamp:[[NSProcessInfo processInfo] systemUptime]
|
|
312
|
+
windowNumber:win.windowNumber
|
|
313
|
+
context:nil
|
|
314
|
+
eventNumber:0
|
|
315
|
+
clickCount:1
|
|
316
|
+
pressure:1];
|
|
317
|
+
[NSApp postEvent:e atStart:NO];
|
|
318
|
+
});
|
|
260
319
|
return info.Env().Undefined();
|
|
261
320
|
}
|
|
262
321
|
|
|
263
322
|
static Napi::Value SetEventCallback(const Napi::CallbackInfo& info) {
|
|
323
|
+
if (!PumpModeOnly(info, "setEventCallback")) return info.Env().Undefined();
|
|
264
324
|
if (info[0].IsFunction()) {
|
|
265
325
|
gEventCb = Napi::Persistent(info[0].As<Napi::Function>());
|
|
266
326
|
gEventCb.SuppressDestruct(); // static: outlives the env, see backend.mm
|
|
@@ -272,6 +332,7 @@ static Napi::Value SetEventCallback(const Napi::CallbackInfo& info) {
|
|
|
272
332
|
|
|
273
333
|
static Napi::Value Pump(const Napi::CallbackInfo& info) {
|
|
274
334
|
Napi::Env env = info.Env();
|
|
335
|
+
if (!PumpModeOnly(info, "pump")) return env.Undefined();
|
|
275
336
|
EnsureApp();
|
|
276
337
|
@autoreleasepool {
|
|
277
338
|
while (true) {
|
|
@@ -291,40 +352,134 @@ static Napi::Value Pump(const Napi::CallbackInfo& info) {
|
|
|
291
352
|
// ---------------------------------------------------------------------------
|
|
292
353
|
// layers
|
|
293
354
|
// ---------------------------------------------------------------------------
|
|
355
|
+
//
|
|
356
|
+
// Frames from a worker (windowkit/appkit#52). On the main thread (pump mode)
|
|
357
|
+
// every layer verb acts in the call, as it always has. Off it a layer is the
|
|
358
|
+
// UI thread's to touch, and a frame's changes have to land in one commit
|
|
359
|
+
// with nothing between them: a verb called between txBegin and txCommit
|
|
360
|
+
// records into its thread's open batch, and the outermost txCommit posts
|
|
361
|
+
// the batch as one command, applied on the UI thread in the order it was
|
|
362
|
+
// recorded. txBegin and txCommit are recorded too, so what applies is the
|
|
363
|
+
// very sequence of Core Animation calls pump mode would make — inside the
|
|
364
|
+
// drain's transaction, so one commit, with actions as the frame asked for
|
|
365
|
+
// them. A verb outside any txBegin is a command of its own, in a transaction
|
|
366
|
+
// with actions on, as pump mode's implicit transaction has them. Arguments
|
|
367
|
+
// are read on the calling thread into plain values (colours, paths,
|
|
368
|
+
// transforms and animation objects are thread-safe to make); a layer is
|
|
369
|
+
// made on the UI thread when its create applies, behind a handle answered
|
|
370
|
+
// at the call.
|
|
371
|
+
|
|
372
|
+
struct FrameBatch {
|
|
373
|
+
int depth = 0;
|
|
374
|
+
std::vector<dispatch_block_t> ops;
|
|
375
|
+
};
|
|
376
|
+
static thread_local FrameBatch tlFrame;
|
|
377
|
+
|
|
378
|
+
// Set while the UI thread applies a worker's layer changes, whose replaced
|
|
379
|
+
// IOSurfaces are then reported.
|
|
380
|
+
static bool gApplyingWorkerFrame = false;
|
|
381
|
+
static std::vector<uint32_t> gReleasedSurfaces; // the UI thread's
|
|
382
|
+
|
|
383
|
+
static void RunLayerOp(dispatch_block_t op) {
|
|
384
|
+
@try {
|
|
385
|
+
op();
|
|
386
|
+
} @catch (NSException* e) {
|
|
387
|
+
fprintf(stderr, "@windowkit/appkit: a layer change raised %s: %s\n",
|
|
388
|
+
e.name.UTF8String, e.reason.UTF8String ?: "");
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// The IOSurfaces a worker's frame took off its layers, as
|
|
393
|
+
// `surface-released { id }`: from a block, which runs after the source
|
|
394
|
+
// callout that queued it — after the drain's commit — so the new buffer has
|
|
395
|
+
// gone to the render server before the renderer hears it may draw into the
|
|
396
|
+
// old one. (Whether the old one is still scanning out is IOSurfaceIsInUse's
|
|
397
|
+
// to say: surfaceIsInUse.)
|
|
398
|
+
static void FlushReleasedSurfaces() {
|
|
399
|
+
if (gReleasedSurfaces.empty()) return;
|
|
400
|
+
std::vector<uint32_t> ids = std::move(gReleasedSurfaces);
|
|
401
|
+
gReleasedSurfaces.clear();
|
|
402
|
+
CFRunLoopPerformBlock(CFRunLoopGetMain(), kCFRunLoopCommonModes, ^{
|
|
403
|
+
for (uint32_t id : ids)
|
|
404
|
+
CALEmit(std::move(CALEvent("surface-released").Num("id", (double)id)));
|
|
405
|
+
});
|
|
406
|
+
CFRunLoopWakeUp(CFRunLoopGetMain());
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
void CALNoteContentsReplaced(id layer, id next) {
|
|
410
|
+
if (!gApplyingWorkerFrame) return;
|
|
411
|
+
id old = ((CALayer*)layer).contents;
|
|
412
|
+
if (!old || old == next ||
|
|
413
|
+
CFGetTypeID((__bridge CFTypeRef)old) != IOSurfaceGetTypeID())
|
|
414
|
+
return;
|
|
415
|
+
gReleasedSurfaces.push_back(IOSurfaceGetID((__bridge IOSurfaceRef)old));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
void CALOnLayers(dispatch_block_t op) {
|
|
419
|
+
if (pthread_main_np()) {
|
|
420
|
+
op();
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
if (tlFrame.depth > 0) {
|
|
424
|
+
tlFrame.ops.push_back(op);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
427
|
+
CALOnUI(^{
|
|
428
|
+
gApplyingWorkerFrame = true;
|
|
429
|
+
[CATransaction begin];
|
|
430
|
+
[CATransaction setDisableActions:NO];
|
|
431
|
+
RunLayerOp(op);
|
|
432
|
+
[CATransaction commit];
|
|
433
|
+
gApplyingWorkerFrame = false;
|
|
434
|
+
FlushReleasedSurfaces();
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// A layer: made in the call on the main thread; from a worker, a handle now
|
|
439
|
+
// and the layer when its create applies.
|
|
440
|
+
static Napi::Value NewLayer(const Napi::CallbackInfo& info, CALayer* (^make)(void)) {
|
|
441
|
+
if (pthread_main_np()) return WrapRetained(info.Env(), make());
|
|
442
|
+
CALHandle* h = CALNewHandle();
|
|
443
|
+
Napi::Value v = CALWrapHandle(info.Env(), h, false);
|
|
444
|
+
CALOnLayers(^{ h->object_ = make(); });
|
|
445
|
+
return v;
|
|
446
|
+
}
|
|
294
447
|
|
|
295
448
|
static Napi::Value CreateLayer(const Napi::CallbackInfo& info) {
|
|
296
|
-
return
|
|
449
|
+
return NewLayer(info, ^CALayer* { return [CALayer layer]; });
|
|
297
450
|
}
|
|
298
451
|
static Napi::Value CreateTextLayer(const Napi::CallbackInfo& info) {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
452
|
+
return NewLayer(info, ^CALayer* {
|
|
453
|
+
CATextLayer* t = [CATextLayer layer];
|
|
454
|
+
t.contentsScale = 2.0; // sane retina default; overridable via contentsScale
|
|
455
|
+
return t;
|
|
456
|
+
});
|
|
302
457
|
}
|
|
303
458
|
static Napi::Value CreateGradientLayer(const Napi::CallbackInfo& info) {
|
|
304
|
-
return
|
|
459
|
+
return NewLayer(info, ^CALayer* { return [CAGradientLayer layer]; });
|
|
305
460
|
}
|
|
306
461
|
static Napi::Value CreateShapeLayer(const Napi::CallbackInfo& info) {
|
|
307
|
-
return
|
|
462
|
+
return NewLayer(info, ^CALayer* { return [CAShapeLayer layer]; });
|
|
308
463
|
}
|
|
309
464
|
|
|
310
465
|
static Napi::Value AddSublayer(const Napi::CallbackInfo& info) {
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
466
|
+
id parent = CALHandleTarget(info[0]), child = CALHandleTarget(info[1]);
|
|
467
|
+
CALOnLayers(^{
|
|
468
|
+
CALayer* p = CALResolve(parent);
|
|
469
|
+
CALayer* c = CALResolve(child);
|
|
470
|
+
if (p && c) [p addSublayer:c];
|
|
471
|
+
});
|
|
314
472
|
return info.Env().Undefined();
|
|
315
473
|
}
|
|
316
474
|
|
|
317
475
|
static Napi::Value RemoveFromSuperlayer(const Napi::CallbackInfo& info) {
|
|
318
|
-
|
|
319
|
-
[
|
|
476
|
+
id target = CALHandleTarget(info[0]);
|
|
477
|
+
CALOnLayers(^{ [(CALayer*)CALResolve(target) removeFromSuperlayer]; });
|
|
320
478
|
return info.Env().Undefined();
|
|
321
479
|
}
|
|
322
480
|
|
|
323
|
-
static
|
|
324
|
-
if (v.IsNull() || v.IsUndefined())
|
|
325
|
-
L.transform = CATransform3DIdentity;
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
481
|
+
static CATransform3D TransformFrom(Napi::Value v) {
|
|
482
|
+
if (v.IsNull() || v.IsUndefined()) return CATransform3DIdentity;
|
|
328
483
|
Napi::Object t = v.As<Napi::Object>();
|
|
329
484
|
CATransform3D m = CATransform3DIdentity;
|
|
330
485
|
m = CATransform3DTranslate(m, NumOr(t, "translateX", 0), NumOr(t, "translateY", 0), 0);
|
|
@@ -333,72 +488,144 @@ static void ApplyTransform(CALayer* L, Napi::Value v) {
|
|
|
333
488
|
double s = NumOr(t, "scale", 1);
|
|
334
489
|
double sx = NumOr(t, "scaleX", s), sy = NumOr(t, "scaleY", s);
|
|
335
490
|
if (sx != 1 || sy != 1) m = CATransform3DScale(m, sx, sy, 1);
|
|
336
|
-
|
|
491
|
+
return m;
|
|
337
492
|
}
|
|
338
493
|
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
494
|
+
// A colour property as given: absent, null (cleared) or a colour.
|
|
495
|
+
struct ColorProp {
|
|
496
|
+
bool given = false;
|
|
497
|
+
id color = nil; // a CGColor, owned by ARC through the bridge; nil clears
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
static ColorProp ColorPropOf(Napi::Object o, const char* key) {
|
|
501
|
+
ColorProp c;
|
|
502
|
+
if (!o.Has(key)) return c;
|
|
503
|
+
c.given = true;
|
|
342
504
|
Napi::Value v = o.Get(key);
|
|
343
|
-
if (v.IsNull())
|
|
344
|
-
|
|
345
|
-
} else {
|
|
346
|
-
CGColorRef c = MakeColor(v);
|
|
347
|
-
setter(c);
|
|
348
|
-
CGColorRelease(c);
|
|
349
|
-
}
|
|
505
|
+
if (!v.IsNull()) c.color = CFBridgingRelease(MakeColor(v));
|
|
506
|
+
return c;
|
|
350
507
|
}
|
|
351
508
|
|
|
352
|
-
static
|
|
353
|
-
|
|
509
|
+
static CGColorRef ColorOf(const ColorProp& c) { return (__bridge CGColorRef)c.color; }
|
|
510
|
+
|
|
511
|
+
// setLayerProps' object, read on the calling thread; only what is present
|
|
512
|
+
// is applied.
|
|
513
|
+
struct LayerPropsSpec {
|
|
514
|
+
bool hasFrame = false, hasBounds = false, hasPosition = false, hasAnchor = false;
|
|
515
|
+
CGRect frame = CGRectZero, bounds = CGRectZero;
|
|
516
|
+
CGPoint position = CGPointZero, anchor = CGPointZero;
|
|
517
|
+
bool hasZ = false;
|
|
518
|
+
double z = 0;
|
|
519
|
+
ColorProp background, border, shadow;
|
|
520
|
+
bool hasCorner = false, hasBorderWidth = false, hasOpacity = false, hasHidden = false;
|
|
521
|
+
bool hasMasks = false, hasShadowOpacity = false, hasShadowRadius = false;
|
|
522
|
+
bool hasShadowOffset = false, hasContentsScale = false, hasName = false;
|
|
523
|
+
bool hasMask = false, hasTransform = false, clearContents = false;
|
|
524
|
+
double corner = 0, borderWidth = 0, shadowRadius = 3, contentsScale = 1;
|
|
525
|
+
float opacity = 1, shadowOpacity = 0;
|
|
526
|
+
bool hidden = false, masks = false;
|
|
527
|
+
CGSize shadowOffset = CGSizeZero;
|
|
528
|
+
NSString* name = nil;
|
|
529
|
+
id mask = nil; // a layer target; nil clears
|
|
530
|
+
CATransform3D transform = CATransform3DIdentity;
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
static LayerPropsSpec ParseLayerProps(Napi::Object o) {
|
|
534
|
+
LayerPropsSpec s;
|
|
535
|
+
if (o.Has("frame")) {
|
|
536
|
+
s.hasFrame = true;
|
|
537
|
+
s.frame = RectFrom(o.Get("frame"));
|
|
538
|
+
}
|
|
354
539
|
if (o.Has("bounds")) {
|
|
355
540
|
// [w, h] or [x, y, w, h] — the four-element form carries a bounds
|
|
356
541
|
// ORIGIN, which is Core Animation's native scroll: the layer shows its
|
|
357
542
|
// sublayers shifted by (-x, -y) with nothing repainted.
|
|
543
|
+
s.hasBounds = true;
|
|
358
544
|
Napi::Array a = o.Get("bounds").As<Napi::Array>();
|
|
359
545
|
if (a.Length() >= 4) {
|
|
360
|
-
|
|
546
|
+
s.bounds = CGRectMake(a.Get(0u).As<Napi::Number>().DoubleValue(),
|
|
361
547
|
a.Get(1u).As<Napi::Number>().DoubleValue(),
|
|
362
548
|
a.Get(2u).As<Napi::Number>().DoubleValue(),
|
|
363
549
|
a.Get(3u).As<Napi::Number>().DoubleValue());
|
|
364
550
|
} else {
|
|
365
|
-
|
|
551
|
+
s.bounds = CGRectMake(0, 0, a.Get(0u).As<Napi::Number>().DoubleValue(),
|
|
366
552
|
a.Get(1u).As<Napi::Number>().DoubleValue());
|
|
367
553
|
}
|
|
368
554
|
}
|
|
369
|
-
if (o.Has("position"))
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
if (o.Has("
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
555
|
+
if (o.Has("position")) {
|
|
556
|
+
s.hasPosition = true;
|
|
557
|
+
s.position = PointFrom(o.Get("position"));
|
|
558
|
+
}
|
|
559
|
+
if (o.Has("anchorPoint")) {
|
|
560
|
+
s.hasAnchor = true;
|
|
561
|
+
s.anchor = PointFrom(o.Get("anchorPoint"));
|
|
562
|
+
}
|
|
563
|
+
if (o.Has("zPosition")) {
|
|
564
|
+
s.hasZ = true;
|
|
565
|
+
s.z = NumOr(o, "zPosition", 0);
|
|
566
|
+
}
|
|
567
|
+
s.background = ColorPropOf(o, "backgroundColor");
|
|
568
|
+
s.border = ColorPropOf(o, "borderColor");
|
|
569
|
+
s.shadow = ColorPropOf(o, "shadowColor");
|
|
570
|
+
if ((s.hasCorner = o.Has("cornerRadius"))) s.corner = NumOr(o, "cornerRadius", 0);
|
|
571
|
+
if ((s.hasBorderWidth = o.Has("borderWidth"))) s.borderWidth = NumOr(o, "borderWidth", 0);
|
|
572
|
+
if ((s.hasOpacity = o.Has("opacity"))) s.opacity = (float)NumOr(o, "opacity", 1);
|
|
573
|
+
if ((s.hasHidden = o.Has("hidden"))) s.hidden = BoolOr(o, "hidden", false);
|
|
574
|
+
if ((s.hasMasks = o.Has("masksToBounds"))) s.masks = BoolOr(o, "masksToBounds", false);
|
|
575
|
+
if ((s.hasShadowOpacity = o.Has("shadowOpacity")))
|
|
576
|
+
s.shadowOpacity = (float)NumOr(o, "shadowOpacity", 0);
|
|
577
|
+
if ((s.hasShadowRadius = o.Has("shadowRadius"))) s.shadowRadius = NumOr(o, "shadowRadius", 3);
|
|
578
|
+
if ((s.hasShadowOffset = o.Has("shadowOffset"))) {
|
|
383
579
|
CGPoint p = PointFrom(o.Get("shadowOffset"));
|
|
384
|
-
|
|
580
|
+
s.shadowOffset = CGSizeMake(p.x, p.y);
|
|
385
581
|
}
|
|
386
|
-
if (
|
|
387
|
-
|
|
388
|
-
if (o.Has("
|
|
582
|
+
if ((s.hasContentsScale = o.Has("contentsScale")))
|
|
583
|
+
s.contentsScale = NumOr(o, "contentsScale", 1);
|
|
584
|
+
if ((s.hasName = o.Has("name"))) s.name = ToNSString(o.Get("name"));
|
|
585
|
+
if ((s.hasMask = o.Has("mask"))) {
|
|
389
586
|
Napi::Value v = o.Get("mask");
|
|
390
|
-
|
|
587
|
+
s.mask = (v.IsNull() || v.IsUndefined()) ? nil : CALHandleTarget(v);
|
|
391
588
|
}
|
|
392
|
-
if (o.Has("transform"))
|
|
393
|
-
if (o.Has("contents"))
|
|
394
|
-
|
|
395
|
-
|
|
589
|
+
if ((s.hasTransform = o.Has("transform"))) s.transform = TransformFrom(o.Get("transform"));
|
|
590
|
+
if (o.Has("contents")) s.clearContents = o.Get("contents").IsNull();
|
|
591
|
+
return s;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// On the UI thread, in the order the keys have always been applied.
|
|
595
|
+
static void ApplyLayerProps(CALayer* L, const LayerPropsSpec& s) {
|
|
596
|
+
if (s.hasFrame) L.frame = s.frame;
|
|
597
|
+
if (s.hasBounds) L.bounds = s.bounds;
|
|
598
|
+
if (s.hasPosition) L.position = s.position;
|
|
599
|
+
if (s.hasAnchor) L.anchorPoint = s.anchor;
|
|
600
|
+
if (s.hasZ) L.zPosition = s.z;
|
|
601
|
+
if (s.background.given) L.backgroundColor = ColorOf(s.background);
|
|
602
|
+
if (s.border.given) L.borderColor = ColorOf(s.border);
|
|
603
|
+
if (s.shadow.given) L.shadowColor = ColorOf(s.shadow);
|
|
604
|
+
if (s.hasCorner) L.cornerRadius = s.corner;
|
|
605
|
+
if (s.hasBorderWidth) L.borderWidth = s.borderWidth;
|
|
606
|
+
if (s.hasOpacity) L.opacity = s.opacity;
|
|
607
|
+
if (s.hasHidden) L.hidden = s.hidden;
|
|
608
|
+
if (s.hasMasks) L.masksToBounds = s.masks;
|
|
609
|
+
if (s.hasShadowOpacity) L.shadowOpacity = s.shadowOpacity;
|
|
610
|
+
if (s.hasShadowRadius) L.shadowRadius = s.shadowRadius;
|
|
611
|
+
if (s.hasShadowOffset) L.shadowOffset = s.shadowOffset;
|
|
612
|
+
if (s.hasContentsScale) L.contentsScale = s.contentsScale;
|
|
613
|
+
if (s.hasName) L.name = s.name;
|
|
614
|
+
if (s.hasMask) L.mask = s.mask ? CALResolve(s.mask) : nil;
|
|
615
|
+
if (s.hasTransform) L.transform = s.transform;
|
|
616
|
+
if (s.clearContents) {
|
|
617
|
+
CALNoteContentsReplaced(L, nil);
|
|
618
|
+
L.contents = nil;
|
|
396
619
|
}
|
|
397
620
|
}
|
|
398
621
|
|
|
399
622
|
static Napi::Value SetLayerProps(const Napi::CallbackInfo& info) {
|
|
400
|
-
|
|
401
|
-
|
|
623
|
+
id target = CALHandleTarget(info[0]);
|
|
624
|
+
LayerPropsSpec s = ParseLayerProps(info[1].As<Napi::Object>());
|
|
625
|
+
CALOnLayers(^{
|
|
626
|
+
CALayer* L = CALResolve(target);
|
|
627
|
+
if (L) ApplyLayerProps(L, s);
|
|
628
|
+
});
|
|
402
629
|
return info.Env().Undefined();
|
|
403
630
|
}
|
|
404
631
|
|
|
@@ -407,33 +634,42 @@ static Napi::Value SetLayerProps(const Napi::CallbackInfo& info) {
|
|
|
407
634
|
// ---------------------------------------------------------------------------
|
|
408
635
|
|
|
409
636
|
static Napi::Value SetTextProps(const Napi::CallbackInfo& info) {
|
|
410
|
-
|
|
637
|
+
id target = CALHandleTarget(info[0]);
|
|
411
638
|
Napi::Object o = info[1].As<Napi::Object>();
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
if (
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
if (
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
639
|
+
bool hasSize = o.Has("fontSize");
|
|
640
|
+
double size = NumOr(o, "fontSize", 14);
|
|
641
|
+
NSString* fontName = o.Has("fontName") ? ToNSString(o.Get("fontName")) : nil;
|
|
642
|
+
NSString* string = o.Has("string") ? ToNSString(o.Get("string")) : nil;
|
|
643
|
+
ColorProp color = ColorPropOf(o, "color");
|
|
644
|
+
NSString* align = o.Has("align") ? ToNSString(o.Get("align")) : nil;
|
|
645
|
+
int wrapped = o.Has("wrapped") ? (int)BoolOr(o, "wrapped", false) : -1;
|
|
646
|
+
NSString* truncation = o.Has("truncation") ? ToNSString(o.Get("truncation")) : nil;
|
|
647
|
+
CALOnLayers(^{
|
|
648
|
+
CATextLayer* T = (CATextLayer*)CALResolve(target);
|
|
649
|
+
if (!T) return;
|
|
650
|
+
if (hasSize) T.fontSize = size;
|
|
651
|
+
if (fontName) {
|
|
652
|
+
CTFontRef f = CTFontCreateWithName((__bridge CFStringRef)fontName,
|
|
653
|
+
T.fontSize > 0 ? T.fontSize : 14, NULL);
|
|
654
|
+
T.font = f;
|
|
655
|
+
CFRelease(f);
|
|
656
|
+
}
|
|
657
|
+
if (string) T.string = string;
|
|
658
|
+
if (color.given) T.foregroundColor = ColorOf(color);
|
|
659
|
+
if (align) {
|
|
660
|
+
if ([align isEqualToString:@"center"]) T.alignmentMode = kCAAlignmentCenter;
|
|
661
|
+
else if ([align isEqualToString:@"right"]) T.alignmentMode = kCAAlignmentRight;
|
|
662
|
+
else if ([align isEqualToString:@"justified"]) T.alignmentMode = kCAAlignmentJustified;
|
|
663
|
+
else T.alignmentMode = kCAAlignmentLeft;
|
|
664
|
+
}
|
|
665
|
+
if (wrapped >= 0) T.wrapped = wrapped;
|
|
666
|
+
if (truncation) {
|
|
667
|
+
if ([truncation isEqualToString:@"start"]) T.truncationMode = kCATruncationStart;
|
|
668
|
+
else if ([truncation isEqualToString:@"end"]) T.truncationMode = kCATruncationEnd;
|
|
669
|
+
else if ([truncation isEqualToString:@"middle"]) T.truncationMode = kCATruncationMiddle;
|
|
670
|
+
else T.truncationMode = kCATruncationNone;
|
|
671
|
+
}
|
|
672
|
+
});
|
|
437
673
|
return info.Env().Undefined();
|
|
438
674
|
}
|
|
439
675
|
|
|
@@ -442,32 +678,41 @@ static Napi::Value SetTextProps(const Napi::CallbackInfo& info) {
|
|
|
442
678
|
// ---------------------------------------------------------------------------
|
|
443
679
|
|
|
444
680
|
static Napi::Value SetGradientProps(const Napi::CallbackInfo& info) {
|
|
445
|
-
|
|
681
|
+
id target = CALHandleTarget(info[0]);
|
|
446
682
|
Napi::Object o = info[1].As<Napi::Object>();
|
|
683
|
+
NSMutableArray* colors = nil;
|
|
447
684
|
if (o.Has("colors")) {
|
|
448
685
|
Napi::Array arr = o.Get("colors").As<Napi::Array>();
|
|
449
|
-
|
|
686
|
+
colors = [NSMutableArray arrayWithCapacity:arr.Length()];
|
|
450
687
|
for (uint32_t i = 0; i < arr.Length(); i++) {
|
|
451
688
|
[colors addObject:CFBridgingRelease(MakeColor(arr.Get(i)))];
|
|
452
689
|
}
|
|
453
|
-
G.colors = colors;
|
|
454
690
|
}
|
|
691
|
+
NSMutableArray* locs = nil;
|
|
455
692
|
if (o.Has("locations")) {
|
|
456
693
|
Napi::Array arr = o.Get("locations").As<Napi::Array>();
|
|
457
|
-
|
|
694
|
+
locs = [NSMutableArray arrayWithCapacity:arr.Length()];
|
|
458
695
|
for (uint32_t i = 0; i < arr.Length(); i++) {
|
|
459
696
|
[locs addObject:@(arr.Get(i).As<Napi::Number>().DoubleValue())];
|
|
460
697
|
}
|
|
461
|
-
G.locations = locs;
|
|
462
|
-
}
|
|
463
|
-
if (o.Has("startPoint")) G.startPoint = PointFrom(o.Get("startPoint"));
|
|
464
|
-
if (o.Has("endPoint")) G.endPoint = PointFrom(o.Get("endPoint"));
|
|
465
|
-
if (o.Has("type")) {
|
|
466
|
-
NSString* t = ToNSString(o.Get("type"));
|
|
467
|
-
if ([t isEqualToString:@"radial"]) G.type = kCAGradientLayerRadial;
|
|
468
|
-
else if ([t isEqualToString:@"conic"]) G.type = kCAGradientLayerConic;
|
|
469
|
-
else G.type = kCAGradientLayerAxial;
|
|
470
698
|
}
|
|
699
|
+
bool hasStart = o.Has("startPoint"), hasEnd = o.Has("endPoint");
|
|
700
|
+
CGPoint start = hasStart ? PointFrom(o.Get("startPoint")) : CGPointZero;
|
|
701
|
+
CGPoint end = hasEnd ? PointFrom(o.Get("endPoint")) : CGPointZero;
|
|
702
|
+
NSString* type = o.Has("type") ? ToNSString(o.Get("type")) : nil;
|
|
703
|
+
CALOnLayers(^{
|
|
704
|
+
CAGradientLayer* G = (CAGradientLayer*)CALResolve(target);
|
|
705
|
+
if (!G) return;
|
|
706
|
+
if (colors) G.colors = colors;
|
|
707
|
+
if (locs) G.locations = locs;
|
|
708
|
+
if (hasStart) G.startPoint = start;
|
|
709
|
+
if (hasEnd) G.endPoint = end;
|
|
710
|
+
if (type) {
|
|
711
|
+
if ([type isEqualToString:@"radial"]) G.type = kCAGradientLayerRadial;
|
|
712
|
+
else if ([type isEqualToString:@"conic"]) G.type = kCAGradientLayerConic;
|
|
713
|
+
else G.type = kCAGradientLayerAxial;
|
|
714
|
+
}
|
|
715
|
+
});
|
|
471
716
|
return info.Env().Undefined();
|
|
472
717
|
}
|
|
473
718
|
|
|
@@ -496,35 +741,43 @@ static CGPathRef BuildPath(Napi::Array ops) {
|
|
|
496
741
|
}
|
|
497
742
|
|
|
498
743
|
static Napi::Value SetShapeProps(const Napi::CallbackInfo& info) {
|
|
499
|
-
|
|
744
|
+
id target = CALHandleTarget(info[0]);
|
|
500
745
|
Napi::Object o = info[1].As<Napi::Object>();
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
if (o.Has("strokeEnd")) S.strokeEnd = NumOr(o, "strokeEnd", 1);
|
|
511
|
-
if (o.Has("lineCap")) {
|
|
512
|
-
NSString* c = ToNSString(o.Get("lineCap"));
|
|
513
|
-
if ([c isEqualToString:@"round"]) S.lineCap = kCALineCapRound;
|
|
514
|
-
else if ([c isEqualToString:@"square"]) S.lineCap = kCALineCapSquare;
|
|
515
|
-
else S.lineCap = kCALineCapButt;
|
|
516
|
-
}
|
|
746
|
+
// a CGPath is immutable once built, so the one made here is the one set
|
|
747
|
+
id path = o.Has("path") ? CFBridgingRelease(BuildPath(o.Get("path").As<Napi::Array>())) : nil;
|
|
748
|
+
bool hasPath = o.Has("path");
|
|
749
|
+
ColorProp fill = ColorPropOf(o, "fillColor"), stroke = ColorPropOf(o, "strokeColor");
|
|
750
|
+
bool hasWidth = o.Has("lineWidth"), hasStart = o.Has("strokeStart"), hasEnd = o.Has("strokeEnd");
|
|
751
|
+
double width = NumOr(o, "lineWidth", 1), start = NumOr(o, "strokeStart", 0),
|
|
752
|
+
end = NumOr(o, "strokeEnd", 1);
|
|
753
|
+
NSString* cap = o.Has("lineCap") ? ToNSString(o.Get("lineCap")) : nil;
|
|
754
|
+
NSMutableArray* dash = nil;
|
|
517
755
|
if (o.Has("lineDashPattern")) {
|
|
518
756
|
Napi::Array arr = o.Get("lineDashPattern").As<Napi::Array>();
|
|
519
|
-
|
|
757
|
+
dash = [NSMutableArray arrayWithCapacity:arr.Length()];
|
|
520
758
|
for (uint32_t i = 0; i < arr.Length(); i++)
|
|
521
|
-
[
|
|
522
|
-
S.lineDashPattern = d;
|
|
523
|
-
}
|
|
524
|
-
if (o.Has("fillRule")) {
|
|
525
|
-
S.fillRule = [ToNSString(o.Get("fillRule")) isEqualToString:@"evenodd"]
|
|
526
|
-
? kCAFillRuleEvenOdd : kCAFillRuleNonZero;
|
|
759
|
+
[dash addObject:@(arr.Get(i).As<Napi::Number>().DoubleValue())];
|
|
527
760
|
}
|
|
761
|
+
int evenOdd = o.Has("fillRule")
|
|
762
|
+
? (int)[ToNSString(o.Get("fillRule")) isEqualToString:@"evenodd"]
|
|
763
|
+
: -1;
|
|
764
|
+
CALOnLayers(^{
|
|
765
|
+
CAShapeLayer* S = (CAShapeLayer*)CALResolve(target);
|
|
766
|
+
if (!S) return;
|
|
767
|
+
if (hasPath) S.path = (__bridge CGPathRef)path;
|
|
768
|
+
if (fill.given) S.fillColor = ColorOf(fill);
|
|
769
|
+
if (stroke.given) S.strokeColor = ColorOf(stroke);
|
|
770
|
+
if (hasWidth) S.lineWidth = width;
|
|
771
|
+
if (hasStart) S.strokeStart = start;
|
|
772
|
+
if (hasEnd) S.strokeEnd = end;
|
|
773
|
+
if (cap) {
|
|
774
|
+
if ([cap isEqualToString:@"round"]) S.lineCap = kCALineCapRound;
|
|
775
|
+
else if ([cap isEqualToString:@"square"]) S.lineCap = kCALineCapSquare;
|
|
776
|
+
else S.lineCap = kCALineCapButt;
|
|
777
|
+
}
|
|
778
|
+
if (dash) S.lineDashPattern = dash;
|
|
779
|
+
if (evenOdd >= 0) S.fillRule = evenOdd ? kCAFillRuleEvenOdd : kCAFillRuleNonZero;
|
|
780
|
+
});
|
|
528
781
|
return info.Env().Undefined();
|
|
529
782
|
}
|
|
530
783
|
|
|
@@ -545,9 +798,8 @@ static Napi::Value SetShapeProps(const Napi::CallbackInfo& info) {
|
|
|
545
798
|
// waits; and the presentation value plus the completion event, which say
|
|
546
799
|
// where an animation is and when it stopped.
|
|
547
800
|
|
|
548
|
-
//
|
|
549
|
-
|
|
550
|
-
void CALEmitBackendEvent(Napi::Env env, Napi::Object ev);
|
|
801
|
+
// The completion event goes out through the backend's one event path
|
|
802
|
+
// (channel.h, included above).
|
|
551
803
|
|
|
552
804
|
static bool ThrowType(Napi::Env env, const char* msg) {
|
|
553
805
|
Napi::TypeError::New(env, msg).ThrowAsJavaScriptException();
|
|
@@ -616,12 +868,13 @@ static CAMediaTimingFunction* TimingFrom(Napi::Env env, Napi::Value v) {
|
|
|
616
868
|
// The completion event. A delegate is set only when the caller passes an
|
|
617
869
|
// `id`, so an animation nobody wants to hear about costs nothing; CAAnimation
|
|
618
870
|
// holds its delegate strongly, so this lives exactly as long as the animation.
|
|
619
|
-
// animationDidStop: arrives on the main thread from the run loop
|
|
620
|
-
//
|
|
621
|
-
//
|
|
622
|
-
//
|
|
871
|
+
// animationDidStop: arrives on the main thread from the run loop — inside
|
|
872
|
+
// pump2() in pump mode, like a window delegate's methods, where it reaches
|
|
873
|
+
// JS inline; inside [NSApp run] in threaded mode, where it crosses to the
|
|
874
|
+
// connected environment like every other event. `finished` is NO for an
|
|
875
|
+
// animation that was removed, or whose layer left the tree, before it ran
|
|
876
|
+
// out.
|
|
623
877
|
@interface CALAnimationDelegate : NSObject <CAAnimationDelegate>
|
|
624
|
-
@property(nonatomic, assign) napi_env env;
|
|
625
878
|
@property(nonatomic, copy) NSString* animId;
|
|
626
879
|
@property(nonatomic, copy) NSString* key;
|
|
627
880
|
@property(nonatomic, copy) NSString* keyPath;
|
|
@@ -629,24 +882,23 @@ static CAMediaTimingFunction* TimingFrom(Napi::Env env, Napi::Value v) {
|
|
|
629
882
|
|
|
630
883
|
@implementation CALAnimationDelegate
|
|
631
884
|
- (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag {
|
|
632
|
-
if (!
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
ev.
|
|
637
|
-
ev.
|
|
638
|
-
ev
|
|
639
|
-
ev.Set("keyPath", self.keyPath.UTF8String);
|
|
640
|
-
ev.Set("finished", Napi::Boolean::New(env, flag));
|
|
641
|
-
CALEmitBackendEvent(env, ev);
|
|
885
|
+
if (!CALListening()) return;
|
|
886
|
+
CALEvent ev("animation-end");
|
|
887
|
+
ev.Str("id", self.animId.UTF8String);
|
|
888
|
+
ev.Str("key", self.key.UTF8String);
|
|
889
|
+
ev.Str("keyPath", self.keyPath.UTF8String);
|
|
890
|
+
ev.Bool("finished", flag);
|
|
891
|
+
CALEmit(std::move(ev));
|
|
642
892
|
}
|
|
643
893
|
@end
|
|
644
894
|
|
|
645
895
|
// What every animation kind shares: repetition, the curve, additive, a
|
|
646
896
|
// delay, speed/timeOffset, hold, and the completion delegate. false with a
|
|
647
897
|
// TypeError pending.
|
|
648
|
-
|
|
649
|
-
|
|
898
|
+
// *delay: the delay asked for, whose begin time is set as the animation is
|
|
899
|
+
// added (on the UI thread, in the layer's own time).
|
|
900
|
+
static bool ApplyTiming(Napi::Env env, CAPropertyAnimation* a, Napi::Object o,
|
|
901
|
+
NSString* key, double* delayOut) {
|
|
650
902
|
double rep = NumOr(o, "repeat", 0);
|
|
651
903
|
if (rep > 0) a.repeatCount = std::isinf(rep) ? HUGE_VALF : (float)rep;
|
|
652
904
|
a.autoreverses = BoolOr(o, "autoreverse", false);
|
|
@@ -667,11 +919,9 @@ static bool ApplyTiming(Napi::Env env, CAPropertyAnimation* a, CALayer* L, Napi:
|
|
|
667
919
|
if (o.Has("timeOffset")) a.timeOffset = NumOr(o, "timeOffset", 0);
|
|
668
920
|
bool hold = BoolOr(o, "hold", false);
|
|
669
921
|
double delay = NumOr(o, "delay", 0);
|
|
922
|
+
*delayOut = delay;
|
|
670
923
|
if (delay > 0) {
|
|
671
|
-
//
|
|
672
|
-
// been slowed or offset
|
|
673
|
-
a.beginTime = [L convertTime:CACurrentMediaTime() fromLayer:nil] + delay;
|
|
674
|
-
// and the layer shows `from` while it waits, rather than the model value
|
|
924
|
+
// the layer shows `from` while it waits, rather than the model value
|
|
675
925
|
// it is about to leave and then snap back from
|
|
676
926
|
a.fillMode = hold ? kCAFillModeBoth : kCAFillModeBackwards;
|
|
677
927
|
}
|
|
@@ -683,7 +933,6 @@ static bool ApplyTiming(Napi::Env env, CAPropertyAnimation* a, CALayer* L, Napi:
|
|
|
683
933
|
Napi::Value idv = o.Get("id");
|
|
684
934
|
if (!idv.IsString()) return ThrowType(env, "id: expected a string");
|
|
685
935
|
CALAnimationDelegate* d = [CALAnimationDelegate new];
|
|
686
|
-
d.env = env;
|
|
687
936
|
d.animId = ToNSString(idv);
|
|
688
937
|
d.key = key;
|
|
689
938
|
d.keyPath = a.keyPath;
|
|
@@ -718,9 +967,13 @@ static bool ReadFromTo(Napi::Env env, CABasicAnimation* a, Napi::Object o) {
|
|
|
718
967
|
// points), additive, cumulative, delay, speed, timeOffset, hold, and id —
|
|
719
968
|
// with an id the animation reports its end as an `animation-end` backend
|
|
720
969
|
// event { id, key, keyPath, finished }.
|
|
970
|
+
//
|
|
971
|
+
// The animation object is built in the call, on whichever thread (a plain
|
|
972
|
+
// model object; the validation throws there), and added where layers are
|
|
973
|
+
// touched — in the call on the main thread, with the frame from a worker.
|
|
721
974
|
static Napi::Value AddAnimation(const Napi::CallbackInfo& info) {
|
|
722
975
|
Napi::Env env = info.Env();
|
|
723
|
-
|
|
976
|
+
id target = CALHandleTarget(info[0]);
|
|
724
977
|
NSString* keyPath = ToNSString(info[1]);
|
|
725
978
|
Napi::Object o = info[2].As<Napi::Object>();
|
|
726
979
|
NSString* key = info.Length() > 3 && info[3].IsString() ? ToNSString(info[3]) : keyPath;
|
|
@@ -831,20 +1084,30 @@ static Napi::Value AddAnimation(const Napi::CallbackInfo& info) {
|
|
|
831
1084
|
b.duration = NumOr(o, "duration", 0.25);
|
|
832
1085
|
a = b;
|
|
833
1086
|
}
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
1087
|
+
double delay = 0;
|
|
1088
|
+
if (!ApplyTiming(env, a, o, key, &delay)) return env.Undefined();
|
|
1089
|
+
double duration = a.duration;
|
|
1090
|
+
CALOnLayers(^{
|
|
1091
|
+
CALayer* L = CALResolve(target);
|
|
1092
|
+
if (!L) return;
|
|
1093
|
+
// in the layer's own time — the media time unless the layer itself has
|
|
1094
|
+
// been slowed or offset
|
|
1095
|
+
if (delay > 0) a.beginTime = [L convertTime:CACurrentMediaTime() fromLayer:nil] + delay;
|
|
1096
|
+
[L addAnimation:a forKey:key];
|
|
1097
|
+
});
|
|
1098
|
+
return Napi::Number::New(env, duration);
|
|
837
1099
|
}
|
|
838
1100
|
|
|
839
1101
|
static Napi::Value RemoveAnimation(const Napi::CallbackInfo& info) {
|
|
840
|
-
|
|
841
|
-
|
|
1102
|
+
id target = CALHandleTarget(info[0]);
|
|
1103
|
+
NSString* key = ToNSString(info[1]);
|
|
1104
|
+
CALOnLayers(^{ [(CALayer*)CALResolve(target) removeAnimationForKey:key]; });
|
|
842
1105
|
return info.Env().Undefined();
|
|
843
1106
|
}
|
|
844
1107
|
|
|
845
1108
|
static Napi::Value RemoveAllAnimations(const Napi::CallbackInfo& info) {
|
|
846
|
-
|
|
847
|
-
[
|
|
1109
|
+
id target = CALHandleTarget(info[0]);
|
|
1110
|
+
CALOnLayers(^{ [(CALayer*)CALResolve(target) removeAllAnimations]; });
|
|
848
1111
|
return info.Env().Undefined();
|
|
849
1112
|
}
|
|
850
1113
|
|
|
@@ -907,22 +1170,32 @@ static Napi::Value JSFromCAValue(Napi::Env env, id v) {
|
|
|
907
1170
|
// showing for that key path right now, animations applied — or null before
|
|
908
1171
|
// the layer's first commit. The model value is what the caller set; this is
|
|
909
1172
|
// where the pixels are, which is the `from` an interrupted colour animation
|
|
910
|
-
// needs and the number a test reads a curve back through.
|
|
1173
|
+
// needs and the number a test reads a curve back through. From a worker,
|
|
1174
|
+
// presentationValue(layer, keyPath, cb): the render server's state is read
|
|
1175
|
+
// on the UI thread.
|
|
911
1176
|
static Napi::Value PresentationValue(const Napi::CallbackInfo& info) {
|
|
912
|
-
|
|
913
|
-
CALayer* L = Deref<CALayer*>(info[0]);
|
|
1177
|
+
id target = CALHandleTarget(info[0]);
|
|
914
1178
|
NSString* keyPath = ToNSString(info[1]);
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
1179
|
+
return CALAnswer(info, "presentationValue", ^CALValueBlock {
|
|
1180
|
+
CALayer* p = ((CALayer*)CALResolve(target)).presentationLayer;
|
|
1181
|
+
id v = nil;
|
|
1182
|
+
if (p) {
|
|
1183
|
+
@try {
|
|
1184
|
+
v = [p valueForKeyPath:keyPath];
|
|
1185
|
+
} @catch (NSException* e) {
|
|
1186
|
+
v = nil;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
// an NSNumber, an NSValue or a CGColor: immutable, made into JS on the
|
|
1190
|
+
// caller's thread
|
|
1191
|
+
return ^Napi::Value(Napi::Env e) { return JSFromCAValue(e, v); };
|
|
1192
|
+
});
|
|
924
1193
|
}
|
|
925
1194
|
|
|
1195
|
+
// txBegin(opts?) / txCommit(). On the main thread a Core Animation
|
|
1196
|
+
// transaction, as always. From a worker the frame batch: txBegin opens it
|
|
1197
|
+
// (or nests inside it), and the txCommit that closes the outermost one
|
|
1198
|
+
// posts the whole batch as one command.
|
|
926
1199
|
static Napi::Value TxBegin(const Napi::CallbackInfo& info) {
|
|
927
1200
|
Napi::Env env = info.Env();
|
|
928
1201
|
CAMediaTimingFunction* timing = nil;
|
|
@@ -937,18 +1210,63 @@ static Napi::Value TxBegin(const Napi::CallbackInfo& info) {
|
|
|
937
1210
|
if (!timing) return env.Undefined();
|
|
938
1211
|
}
|
|
939
1212
|
}
|
|
940
|
-
|
|
1213
|
+
bool hasDuration = false, disable = false;
|
|
1214
|
+
double duration = 0.25;
|
|
941
1215
|
if (info.Length() > 0 && info[0].IsObject()) {
|
|
942
1216
|
Napi::Object o = info[0].As<Napi::Object>();
|
|
943
|
-
|
|
944
|
-
|
|
1217
|
+
hasDuration = o.Has("duration");
|
|
1218
|
+
duration = NumOr(o, "duration", 0.25);
|
|
1219
|
+
disable = BoolOr(o, "disableActions", false);
|
|
1220
|
+
}
|
|
1221
|
+
if (pthread_main_np()) {
|
|
1222
|
+
[CATransaction begin];
|
|
1223
|
+
if (hasDuration) [CATransaction setAnimationDuration:duration];
|
|
1224
|
+
if (disable) [CATransaction setDisableActions:YES];
|
|
945
1225
|
if (hasTiming) [CATransaction setAnimationTimingFunction:timing];
|
|
1226
|
+
return env.Undefined();
|
|
946
1227
|
}
|
|
1228
|
+
// Recorded with its options stated in full: it applies inside the drain's
|
|
1229
|
+
// own transaction, whose actions are off, and a frame that did not ask
|
|
1230
|
+
// for that has them on, as in pump mode.
|
|
1231
|
+
tlFrame.depth++;
|
|
1232
|
+
tlFrame.ops.push_back(^{
|
|
1233
|
+
[CATransaction begin];
|
|
1234
|
+
[CATransaction setDisableActions:disable];
|
|
1235
|
+
if (hasDuration) [CATransaction setAnimationDuration:duration];
|
|
1236
|
+
if (hasTiming) [CATransaction setAnimationTimingFunction:timing];
|
|
1237
|
+
});
|
|
947
1238
|
return env.Undefined();
|
|
948
1239
|
}
|
|
949
1240
|
|
|
950
1241
|
static Napi::Value TxCommit(const Napi::CallbackInfo& info) {
|
|
951
|
-
|
|
1242
|
+
if (pthread_main_np()) {
|
|
1243
|
+
[CATransaction commit];
|
|
1244
|
+
return info.Env().Undefined();
|
|
1245
|
+
}
|
|
1246
|
+
if (tlFrame.depth == 0) return info.Env().Undefined(); // nothing open
|
|
1247
|
+
// txCommit({ width, height }) on the outermost commit: the size this frame
|
|
1248
|
+
// was painted at, which a window's resize handshake waits for
|
|
1249
|
+
// (setResizeHandshake, windowkit/appkit#53)
|
|
1250
|
+
bool sized = false;
|
|
1251
|
+
double width = 0, height = 0;
|
|
1252
|
+
if (info.Length() > 0 && info[0].IsObject()) {
|
|
1253
|
+
Napi::Object o = info[0].As<Napi::Object>();
|
|
1254
|
+
if (o.Get("width").IsNumber() && o.Get("height").IsNumber()) {
|
|
1255
|
+
sized = true;
|
|
1256
|
+
width = NumOr(o, "width", 0);
|
|
1257
|
+
height = NumOr(o, "height", 0);
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
tlFrame.ops.push_back(^{ [CATransaction commit]; });
|
|
1261
|
+
if (--tlFrame.depth > 0) return info.Env().Undefined();
|
|
1262
|
+
std::vector<dispatch_block_t> ops = std::move(tlFrame.ops);
|
|
1263
|
+
tlFrame.ops.clear();
|
|
1264
|
+
CALPostFrame(^{
|
|
1265
|
+
gApplyingWorkerFrame = true;
|
|
1266
|
+
for (dispatch_block_t op : ops) RunLayerOp(op);
|
|
1267
|
+
gApplyingWorkerFrame = false;
|
|
1268
|
+
FlushReleasedSurfaces();
|
|
1269
|
+
}, sized, width, height);
|
|
952
1270
|
return info.Env().Undefined();
|
|
953
1271
|
}
|
|
954
1272
|
|
|
@@ -957,6 +1275,7 @@ static Napi::Value TxCommit(const Napi::CallbackInfo& info) {
|
|
|
957
1275
|
// ---------------------------------------------------------------------------
|
|
958
1276
|
|
|
959
1277
|
static Napi::Value HitTest(const Napi::CallbackInfo& info) {
|
|
1278
|
+
if (!PumpModeOnly(info, "hitTest")) return info.Env().Undefined();
|
|
960
1279
|
Napi::Env env = info.Env();
|
|
961
1280
|
CALayer* root = Deref<CALayer*>(info[0]);
|
|
962
1281
|
double x = info[1].As<Napi::Number>().DoubleValue();
|
|
@@ -1054,11 +1373,17 @@ static Napi::Value CreateTextImage(const Napi::CallbackInfo& info) {
|
|
|
1054
1373
|
|
|
1055
1374
|
// setContentsImage(layer, imageExternal, contentsScale?)
|
|
1056
1375
|
static Napi::Value SetContentsImage(const Napi::CallbackInfo& info) {
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1376
|
+
id target = CALHandleTarget(info[0]);
|
|
1377
|
+
id img = (__bridge id)(CGImageRef)info[1].As<Napi::External<void>>().Data();
|
|
1378
|
+
bool hasScale = info.Length() > 2 && info[2].IsNumber();
|
|
1379
|
+
double scale = hasScale ? info[2].As<Napi::Number>().DoubleValue() : 1;
|
|
1380
|
+
CALOnLayers(^{
|
|
1381
|
+
CALayer* L = CALResolve(target);
|
|
1382
|
+
if (!L) return;
|
|
1383
|
+
CALNoteContentsReplaced(L, img);
|
|
1384
|
+
L.contents = img;
|
|
1385
|
+
if (hasScale) L.contentsScale = scale;
|
|
1386
|
+
});
|
|
1062
1387
|
return info.Env().Undefined();
|
|
1063
1388
|
}
|
|
1064
1389
|
|
|
@@ -1080,6 +1405,7 @@ static NSView* DummyDrawView() {
|
|
|
1080
1405
|
// -> { image: External<CGImage>, width, height, scale } (points)
|
|
1081
1406
|
// width/height default to the cell's natural cellSize (slider must pass them).
|
|
1082
1407
|
static Napi::Value DrawControl(const Napi::CallbackInfo& info) {
|
|
1408
|
+
if (!PumpModeOnly(info, "drawControl")) return info.Env().Undefined();
|
|
1083
1409
|
Napi::Env env = info.Env();
|
|
1084
1410
|
EnsureApp();
|
|
1085
1411
|
Napi::Object o = info[0].As<Napi::Object>();
|
|
@@ -1204,10 +1530,13 @@ static Napi::Value DrawControl(const Napi::CallbackInfo& info) {
|
|
|
1204
1530
|
// setLayerContentsIOSurface(layer, iosurfaceId) — the receiving end of an
|
|
1205
1531
|
// IOSurface render target (x11-dri's appleCreateTarget): the id is process-
|
|
1206
1532
|
// global, so the GPU addon and this one never share a pointer. The layer
|
|
1207
|
-
// retains the surface; our lookup reference
|
|
1533
|
+
// retains the surface; our lookup reference goes with the change. From a
|
|
1534
|
+
// worker the flip happens when the frame applies, so the renderer may not
|
|
1535
|
+
// draw into the buffer it replaced until `surface-released` names it (or
|
|
1536
|
+
// surfaceIsInUse says it is off glass).
|
|
1208
1537
|
static Napi::Value SetLayerContentsIOSurface(const Napi::CallbackInfo& info) {
|
|
1209
1538
|
Napi::Env env = info.Env();
|
|
1210
|
-
|
|
1539
|
+
id target = CALHandleTarget(info[0]);
|
|
1211
1540
|
uint32_t sid = info[1].As<Napi::Number>().Uint32Value();
|
|
1212
1541
|
IOSurfaceRef surface = IOSurfaceLookup(sid);
|
|
1213
1542
|
if (!surface) {
|
|
@@ -1215,17 +1544,23 @@ static Napi::Value SetLayerContentsIOSurface(const Napi::CallbackInfo& info) {
|
|
|
1215
1544
|
.ThrowAsJavaScriptException();
|
|
1216
1545
|
return env.Undefined();
|
|
1217
1546
|
}
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1547
|
+
id s = CFBridgingRelease(surface); // held until the change has been made
|
|
1548
|
+
CALOnLayers(^{
|
|
1549
|
+
CALayer* L = CALResolve(target);
|
|
1550
|
+
if (!L) return;
|
|
1551
|
+
// its own transaction, actions off: a present is a buffer flip, and the
|
|
1552
|
+
// implicit action for `contents` would turn it into a crossfade
|
|
1553
|
+
[CATransaction begin];
|
|
1554
|
+
[CATransaction setDisableActions:YES];
|
|
1555
|
+
CALNoteContentsReplaced(L, s);
|
|
1556
|
+
L.contents = s;
|
|
1557
|
+
[CATransaction commit];
|
|
1558
|
+
});
|
|
1225
1559
|
return env.Undefined();
|
|
1226
1560
|
}
|
|
1227
1561
|
|
|
1228
1562
|
static Napi::Value AppearanceIsDark(const Napi::CallbackInfo& info) {
|
|
1563
|
+
if (!PumpModeOnly(info, "appearanceIsDark")) return info.Env().Undefined();
|
|
1229
1564
|
EnsureApp();
|
|
1230
1565
|
NSAppearanceName n = [NSApp.effectiveAppearance
|
|
1231
1566
|
bestMatchFromAppearancesWithNames:@[ NSAppearanceNameAqua, NSAppearanceNameDarkAqua ]];
|
|
@@ -1236,37 +1571,44 @@ static Napi::Value AppearanceIsDark(const Napi::CallbackInfo& info) {
|
|
|
1236
1571
|
// snapshot (renderInContext -> PNG) — for debugging / headless verification
|
|
1237
1572
|
// ---------------------------------------------------------------------------
|
|
1238
1573
|
|
|
1574
|
+
// snapshotWindow(win, path, withShadow?, cb?) -> bool; off the main thread
|
|
1575
|
+
// the answer comes through cb (the last argument).
|
|
1239
1576
|
static Napi::Value SnapshotWindow(const Napi::CallbackInfo& info) {
|
|
1240
|
-
|
|
1241
|
-
NSWindow* win = Deref<NSWindow*>(info[0]);
|
|
1577
|
+
id target = CALHandleTarget(info[0]);
|
|
1242
1578
|
NSString* path = ToNSString(info[1]);
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1579
|
+
bool withShadow =
|
|
1580
|
+
info.Length() > 2 && !info[2].IsFunction() && info[2].ToBoolean().Value();
|
|
1581
|
+
return CALAnswer(info, "snapshotWindow", ^CALValueBlock {
|
|
1582
|
+
NSWindow* win = CALResolve(target);
|
|
1583
|
+
bool ok = false;
|
|
1584
|
+
if (win) {
|
|
1585
|
+
// Capture our own window's real composited pixels (allowed without the
|
|
1586
|
+
// screen-recording permission for windows the process owns). This
|
|
1587
|
+
// shows the true WindowServer output, including geometryFlipped, masks,
|
|
1588
|
+
// and shadows.
|
|
1247
1589
|
#pragma clang diagnostic push
|
|
1248
1590
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
kCGWindowImageBestResolution));
|
|
1591
|
+
CGImageRef img = CGWindowListCreateImage(
|
|
1592
|
+
CGRectNull, kCGWindowListOptionIncludingWindow, (CGWindowID)win.windowNumber,
|
|
1593
|
+
withShadow
|
|
1594
|
+
? (CGWindowImageOption)kCGWindowImageBestResolution
|
|
1595
|
+
: (CGWindowImageOption)(kCGWindowImageBoundsIgnoreFraming |
|
|
1596
|
+
kCGWindowImageBestResolution));
|
|
1256
1597
|
#pragma clang diagnostic pop
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1598
|
+
if (img) {
|
|
1599
|
+
NSURL* url = [NSURL fileURLWithPath:path];
|
|
1600
|
+
CGImageDestinationRef dst = CGImageDestinationCreateWithURL(
|
|
1601
|
+
(__bridge CFURLRef)url, CFSTR("public.png"), 1, NULL);
|
|
1602
|
+
if (dst) {
|
|
1603
|
+
CGImageDestinationAddImage(dst, img, NULL);
|
|
1604
|
+
ok = CGImageDestinationFinalize(dst);
|
|
1605
|
+
CFRelease(dst);
|
|
1606
|
+
}
|
|
1607
|
+
CGImageRelease(img);
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
return ^Napi::Value(Napi::Env e) { return Napi::Boolean::New(e, ok); };
|
|
1611
|
+
});
|
|
1270
1612
|
}
|
|
1271
1613
|
|
|
1272
1614
|
// ---------------------------------------------------------------------------
|
|
@@ -1289,6 +1631,9 @@ void InitCalendars(Napi::Env env, Napi::Object exports);
|
|
|
1289
1631
|
// src/screencolor.mm — the eyedropper: one colour off the screen through
|
|
1290
1632
|
// NSColorSampler, the system's own out-of-process sampler.
|
|
1291
1633
|
void InitScreenColor(Napi::Env env, Napi::Object exports);
|
|
1634
|
+
// src/threaded.mm — threaded mode: runMain parks the main thread in
|
|
1635
|
+
// [NSApp run], commands in through the main run loop, events out in batches.
|
|
1636
|
+
void InitThreaded(Napi::Env env, Napi::Object exports);
|
|
1292
1637
|
|
|
1293
1638
|
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
1294
1639
|
#define FN(js, fn) exports.Set(js, Napi::Function::New(env, fn))
|
|
@@ -1333,6 +1678,7 @@ static Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
|
1333
1678
|
InitNotifications(env, exports);
|
|
1334
1679
|
InitCalendars(env, exports);
|
|
1335
1680
|
InitScreenColor(env, exports);
|
|
1681
|
+
InitThreaded(env, exports);
|
|
1336
1682
|
return exports;
|
|
1337
1683
|
}
|
|
1338
1684
|
|