@windowkit/appkit 0.4.0 → 0.5.1

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/src/addon.mm CHANGED
@@ -44,13 +44,29 @@ static NSString* StrOr(Napi::Object o, const char* k, NSString* d) {
44
44
  }
45
45
 
46
46
  // [r,g,b] or [r,g,b,a], components 0..1 — caller owns the returned color.
47
+ //
48
+ // sRGB, like every other colour this bridge makes: the surfaces
49
+ // (createSurface, backend.mm), a text span's ink, an animation's from/to
50
+ // (BMakeColor). This one used to be Generic RGB, which the compositor
51
+ // converts on its way to the display, so a layer's backgroundColor came out
52
+ // paler than the same colour rastered into a surface — #dbe7f4 showed as
53
+ // (228, 236, 245) beside a bitmap's (219, 231, 244) — and a colour animation
54
+ // landed on a model value that did not match its own `to`. One space
55
+ // everywhere, and `colorSpace()` says so.
47
56
  static CGColorRef MakeColor(Napi::Value v) {
48
57
  Napi::Array a = v.As<Napi::Array>();
49
58
  double r = a.Get(0u).As<Napi::Number>().DoubleValue();
50
59
  double g = a.Get(1u).As<Napi::Number>().DoubleValue();
51
60
  double b = a.Get(2u).As<Napi::Number>().DoubleValue();
52
61
  double al = a.Length() > 3 ? a.Get(3u).As<Napi::Number>().DoubleValue() : 1.0;
53
- return CGColorCreateGenericRGB(r, g, b, al);
62
+ return CGColorCreateSRGB(r, g, b, al);
63
+ }
64
+
65
+ // The colour space every colour crossing this bridge is in — layer
66
+ // properties, animation values, presentation values, surfaces. A caller
67
+ // that rasters in sRGB can feature-detect that a layer colour will match.
68
+ static Napi::Value ColorSpace(const Napi::CallbackInfo& info) {
69
+ return Napi::String::New(info.Env(), "sRGB");
54
70
  }
55
71
 
56
72
  static CGPoint PointFrom(Napi::Value v) {
@@ -93,22 +109,11 @@ static Napi::Value WrapRetained(Napi::Env env, id obj) {
93
109
  - (void)keyDown:(NSEvent*)event { (void)event; }
94
110
  @end
95
111
 
96
- static bool gAppInited = false;
97
-
98
- static void EnsureApp() {
99
- if (gAppInited) return;
100
- @autoreleasepool {
101
- [NSApplication sharedApplication];
102
- [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
103
- [NSApp finishLaunching];
104
- }
105
- gAppInited = true;
106
- }
107
-
108
- static Napi::Value InitApp(const Napi::CallbackInfo& info) {
109
- EnsureApp();
110
- return info.Env().Undefined();
111
- }
112
+ // src/backend.mm owns the NSApplication setup (activation policy, no window
113
+ // tabbing, the app delegate that must precede finishLaunching); both faces
114
+ // of the addon share the one call so finishLaunching runs once.
115
+ void BEnsureApp();
116
+ static void EnsureApp() { BEnsureApp(); }
112
117
 
113
118
  static Napi::Value CreateWindowFn(const Napi::CallbackInfo& info) {
114
119
  Napi::Env env = info.Env();
@@ -127,6 +132,7 @@ static Napi::Value CreateWindowFn(const Napi::CallbackInfo& info) {
127
132
  backing:NSBackingStoreBuffered
128
133
  defer:NO];
129
134
  win.releasedWhenClosed = NO;
135
+ win.tabbingMode = NSWindowTabbingModeDisallowed;
130
136
  win.title = title;
131
137
  win.acceptsMouseMovedEvents = YES;
132
138
 
@@ -257,6 +263,7 @@ static Napi::Value PostMouseEvent(const Napi::CallbackInfo& info) {
257
263
  static Napi::Value SetEventCallback(const Napi::CallbackInfo& info) {
258
264
  if (info[0].IsFunction()) {
259
265
  gEventCb = Napi::Persistent(info[0].As<Napi::Function>());
266
+ gEventCb.SuppressDestruct(); // static: outlives the env, see backend.mm
260
267
  } else {
261
268
  gEventCb.Reset();
262
269
  }
@@ -524,7 +531,31 @@ static Napi::Value SetShapeProps(const Napi::CallbackInfo& info) {
524
531
  // ---------------------------------------------------------------------------
525
532
  // animations & transactions
526
533
  // ---------------------------------------------------------------------------
534
+ //
535
+ // Mechanism for a renderer that keeps its own model of what is animating
536
+ // (react-x11's transitions and loops, windowkit/appkit#29): the layer's
537
+ // model value is set under disableActions and an explicit animation carries
538
+ // the pixels from `from` to `to` — or through `values` — while the render
539
+ // server interpolates. Beyond CA's own vocabulary, four things let a
540
+ // renderer's timing model stay the truth about an animation it no longer
541
+ // ticks: a curve given as control points, which the renderer can evaluate
542
+ // identically on its side (a name means whatever each side thinks it
543
+ // means); an additive animation, so a retarget carries on from wherever the
544
+ // last one got to with nothing read back; a delay that shows `from` while it
545
+ // waits; and the presentation value plus the completion event, which say
546
+ // where an animation is and when it stopped.
547
+
548
+ // backend.mm's event callback, for the completion event.
549
+ bool CALHasBackendCb();
550
+ void CALEmitBackendEvent(Napi::Env env, Napi::Object ev);
551
+
552
+ static bool ThrowType(Napi::Env env, const char* msg) {
553
+ Napi::TypeError::New(env, msg).ThrowAsJavaScriptException();
554
+ return false;
555
+ }
527
556
 
557
+ // A number, a point [x, y] or a colour [r, g, b(, a)] — the value types a key
558
+ // path takes here. nil for anything else, which the caller reports.
528
559
  static id AnimValue(Napi::Value v) {
529
560
  if (v.IsNumber()) return @(v.As<Napi::Number>().DoubleValue());
530
561
  if (v.IsArray()) {
@@ -538,35 +569,271 @@ static id AnimValue(Napi::Value v) {
538
569
  return nil;
539
570
  }
540
571
 
541
- static CAMediaTimingFunction* TimingFn(NSString* name) {
542
- if ([name isEqualToString:@"linear"]) return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
543
- if ([name isEqualToString:@"easeIn"]) return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
544
- if ([name isEqualToString:@"easeOut"]) return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
545
- if ([name isEqualToString:@"easeInEaseOut"]) return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
546
- return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
572
+ // A timing function: one of CA's names (their CSS spellings are accepted
573
+ // too — they are the same four curves), or the control points of a cubic
574
+ // bezier. nil with a TypeError pending for anything else: an animation on
575
+ // the wrong curve is the kind of bug nobody files, they just think the app
576
+ // feels off.
577
+ static CAMediaTimingFunction* TimingFrom(Napi::Env env, Napi::Value v) {
578
+ if (v.IsString()) {
579
+ NSString* name = ToNSString(v);
580
+ if ([name isEqualToString:@"linear"])
581
+ return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
582
+ if ([name isEqualToString:@"easeIn"] || [name isEqualToString:@"ease-in"])
583
+ return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
584
+ if ([name isEqualToString:@"easeOut"] || [name isEqualToString:@"ease-out"])
585
+ return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
586
+ if ([name isEqualToString:@"easeInEaseOut"] || [name isEqualToString:@"ease-in-out"])
587
+ return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
588
+ if ([name isEqualToString:@"default"] || [name isEqualToString:@"ease"])
589
+ return [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];
590
+ ThrowType(env, "timing: expected linear, easeIn, easeOut, easeInEaseOut, default "
591
+ "(or the CSS spellings) or control points [x1, y1, x2, y2]");
592
+ return nil;
593
+ }
594
+ if (v.IsArray()) {
595
+ Napi::Array a = v.As<Napi::Array>();
596
+ float p[4] = {0, 0, 0, 0};
597
+ bool ok = a.Length() == 4;
598
+ for (uint32_t i = 0; ok && i < 4; i++) {
599
+ Napi::Value e = a.Get(i);
600
+ ok = e.IsNumber() && std::isfinite(e.As<Napi::Number>().DoubleValue());
601
+ if (ok) p[i] = (float)e.As<Napi::Number>().DoubleValue();
602
+ }
603
+ // x is time: it has to stay inside the unit interval for the curve to be
604
+ // a function of it. y may overshoot — that is what a back-out curve is.
605
+ if (ok) ok = p[0] >= 0 && p[0] <= 1 && p[2] >= 0 && p[2] <= 1;
606
+ if (!ok) {
607
+ ThrowType(env, "timing: control points are [x1, y1, x2, y2], finite, with x1 and x2 in 0..1");
608
+ return nil;
609
+ }
610
+ return [CAMediaTimingFunction functionWithControlPoints:p[0]:p[1]:p[2]:p[3]];
611
+ }
612
+ ThrowType(env, "timing: expected a curve name or control points [x1, y1, x2, y2]");
613
+ return nil;
614
+ }
615
+
616
+ // The completion event. A delegate is set only when the caller passes an
617
+ // `id`, so an animation nobody wants to hear about costs nothing; CAAnimation
618
+ // holds its delegate strongly, so this lives exactly as long as the animation.
619
+ // animationDidStop: arrives on the main thread from the run loop the pump
620
+ // drives — inside pump2(), like a window delegate's methods — so it calls
621
+ // into JS directly. `finished` is NO for an animation that was removed, or
622
+ // whose layer left the tree, before it ran out.
623
+ @interface CALAnimationDelegate : NSObject <CAAnimationDelegate>
624
+ @property(nonatomic, assign) napi_env env;
625
+ @property(nonatomic, copy) NSString* animId;
626
+ @property(nonatomic, copy) NSString* key;
627
+ @property(nonatomic, copy) NSString* keyPath;
628
+ @end
629
+
630
+ @implementation CALAnimationDelegate
631
+ - (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag {
632
+ if (!CALHasBackendCb()) return;
633
+ Napi::Env env(self.env);
634
+ Napi::HandleScope scope(env);
635
+ Napi::Object ev = Napi::Object::New(env);
636
+ ev.Set("type", "animation-end");
637
+ ev.Set("id", self.animId.UTF8String);
638
+ ev.Set("key", self.key.UTF8String);
639
+ ev.Set("keyPath", self.keyPath.UTF8String);
640
+ ev.Set("finished", Napi::Boolean::New(env, flag));
641
+ CALEmitBackendEvent(env, ev);
642
+ }
643
+ @end
644
+
645
+ // What every animation kind shares: repetition, the curve, additive, a
646
+ // delay, speed/timeOffset, hold, and the completion delegate. false with a
647
+ // TypeError pending.
648
+ static bool ApplyTiming(Napi::Env env, CAPropertyAnimation* a, CALayer* L, Napi::Object o,
649
+ NSString* key) {
650
+ double rep = NumOr(o, "repeat", 0);
651
+ if (rep > 0) a.repeatCount = std::isinf(rep) ? HUGE_VALF : (float)rep;
652
+ a.autoreverses = BoolOr(o, "autoreverse", false);
653
+ if (o.Has("timing")) {
654
+ CAMediaTimingFunction* fn = TimingFrom(env, o.Get("timing"));
655
+ if (!fn) return false;
656
+ a.timingFunction = fn;
657
+ }
658
+ // Additive: the animation's values are deltas over the model value, and
659
+ // several in flight on one key path sum. That is how a retarget stays
660
+ // continuous — the model goes straight to the new target, the animation
661
+ // runs (old − new) → 0 — and why nothing has to be read back for it.
662
+ a.additive = BoolOr(o, "additive", false);
663
+ a.cumulative = BoolOr(o, "cumulative", false);
664
+ // speed 0 with a timeOffset is an animation paused at that time: how a
665
+ // caller pauses one, and how a test samples a curve without waiting for it
666
+ if (o.Has("speed")) a.speed = (float)NumOr(o, "speed", 1);
667
+ if (o.Has("timeOffset")) a.timeOffset = NumOr(o, "timeOffset", 0);
668
+ bool hold = BoolOr(o, "hold", false);
669
+ double delay = NumOr(o, "delay", 0);
670
+ if (delay > 0) {
671
+ // in the layer's own time — the media time unless the layer itself has
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
675
+ // it is about to leave and then snap back from
676
+ a.fillMode = hold ? kCAFillModeBoth : kCAFillModeBackwards;
677
+ }
678
+ if (hold) {
679
+ a.removedOnCompletion = NO;
680
+ if (delay <= 0) a.fillMode = kCAFillModeForwards;
681
+ }
682
+ if (o.Has("id")) {
683
+ Napi::Value idv = o.Get("id");
684
+ if (!idv.IsString()) return ThrowType(env, "id: expected a string");
685
+ CALAnimationDelegate* d = [CALAnimationDelegate new];
686
+ d.env = env;
687
+ d.animId = ToNSString(idv);
688
+ d.key = key;
689
+ d.keyPath = a.keyPath;
690
+ a.delegate = d;
691
+ }
692
+ return true;
693
+ }
694
+
695
+ static bool ReadFromTo(Napi::Env env, CABasicAnimation* a, Napi::Object o) {
696
+ if (o.Has("from")) {
697
+ id v = AnimValue(o.Get("from"));
698
+ if (!v) return ThrowType(env, "from: expected a number, [x, y] or [r, g, b, a]");
699
+ a.fromValue = v;
700
+ }
701
+ if (o.Has("to")) {
702
+ id v = AnimValue(o.Get("to"));
703
+ if (!v) return ThrowType(env, "to: expected a number, [x, y] or [r, g, b, a]");
704
+ a.toValue = v;
705
+ }
706
+ return true;
547
707
  }
548
708
 
549
- // addAnimation(layer, keyPath, opts, key)
709
+ // addAnimation(layer, keyPath, opts, key) -> the animation's duration in
710
+ // seconds, which for a spring is its settling time — the number a caller
711
+ // needs to know when the animation is over.
712
+ //
713
+ // { from, to, duration } CABasicAnimation
714
+ // { values, keyTimes?, timings?, calculationMode? } CAKeyframeAnimation
715
+ // { spring: { mass, stiffness, damping, initialVelocity } | true, from, to }
716
+ // CASpringAnimation
717
+ // plus, for any of them: repeat, autoreverse, timing (a name or control
718
+ // points), additive, cumulative, delay, speed, timeOffset, hold, and id —
719
+ // with an id the animation reports its end as an `animation-end` backend
720
+ // event { id, key, keyPath, finished }.
550
721
  static Napi::Value AddAnimation(const Napi::CallbackInfo& info) {
722
+ Napi::Env env = info.Env();
551
723
  CALayer* L = Deref<CALayer*>(info[0]);
552
724
  NSString* keyPath = ToNSString(info[1]);
553
725
  Napi::Object o = info[2].As<Napi::Object>();
554
726
  NSString* key = info.Length() > 3 && info[3].IsString() ? ToNSString(info[3]) : keyPath;
555
727
 
556
- CABasicAnimation* a = [CABasicAnimation animationWithKeyPath:keyPath];
557
- if (o.Has("from")) a.fromValue = AnimValue(o.Get("from"));
558
- if (o.Has("to")) a.toValue = AnimValue(o.Get("to"));
559
- a.duration = NumOr(o, "duration", 0.25);
560
- double rep = NumOr(o, "repeat", 0);
561
- if (rep > 0) a.repeatCount = std::isinf(rep) ? HUGE_VALF : (float)rep;
562
- a.autoreverses = BoolOr(o, "autoreverse", false);
563
- if (o.Has("timing")) a.timingFunction = TimingFn(ToNSString(o.Get("timing")));
564
- if (BoolOr(o, "hold", false)) {
565
- a.removedOnCompletion = NO;
566
- a.fillMode = kCAFillModeForwards;
728
+ bool keyframe = o.Has("values");
729
+ bool spring = o.Has("spring");
730
+ if (keyframe && spring) {
731
+ ThrowType(env, "values and spring are two different animations; pass one of them");
732
+ return env.Undefined();
733
+ }
734
+
735
+ CAPropertyAnimation* a = nil;
736
+ if (keyframe) {
737
+ CAKeyframeAnimation* k = [CAKeyframeAnimation animationWithKeyPath:keyPath];
738
+ Napi::Value vv = o.Get("values");
739
+ if (!vv.IsArray() || vv.As<Napi::Array>().Length() < 2) {
740
+ ThrowType(env, "values: expected an array of at least two values");
741
+ return env.Undefined();
742
+ }
743
+ Napi::Array varr = vv.As<Napi::Array>();
744
+ NSMutableArray* values = [NSMutableArray arrayWithCapacity:varr.Length()];
745
+ for (uint32_t i = 0; i < varr.Length(); i++) {
746
+ id v = AnimValue(varr.Get(i));
747
+ if (!v) {
748
+ ThrowType(env, "values: each entry is a number, [x, y] or [r, g, b, a]");
749
+ return env.Undefined();
750
+ }
751
+ [values addObject:v];
752
+ }
753
+ k.values = values;
754
+ if (o.Has("keyTimes")) {
755
+ Napi::Value kv = o.Get("keyTimes");
756
+ bool ok = kv.IsArray() && kv.As<Napi::Array>().Length() == varr.Length();
757
+ NSMutableArray* times = [NSMutableArray arrayWithCapacity:varr.Length()];
758
+ double last = 0;
759
+ for (uint32_t i = 0; ok && i < varr.Length(); i++) {
760
+ Napi::Value t = kv.As<Napi::Array>().Get(i);
761
+ double d = t.IsNumber() ? t.As<Napi::Number>().DoubleValue() : -1;
762
+ ok = d >= last && d <= 1 && (i > 0 || d == 0);
763
+ last = d;
764
+ [times addObject:@(d)];
765
+ }
766
+ if (!ok) {
767
+ ThrowType(env, "keyTimes: one per value, from 0 to at most 1, never decreasing");
768
+ return env.Undefined();
769
+ }
770
+ k.keyTimes = times;
771
+ }
772
+ if (o.Has("timings")) {
773
+ Napi::Value tv = o.Get("timings");
774
+ if (!tv.IsArray() || tv.As<Napi::Array>().Length() != varr.Length() - 1) {
775
+ ThrowType(env, "timings: one curve per segment, so one fewer than values");
776
+ return env.Undefined();
777
+ }
778
+ NSMutableArray* fns = [NSMutableArray arrayWithCapacity:varr.Length() - 1];
779
+ for (uint32_t i = 0; i + 1 < varr.Length(); i++) {
780
+ CAMediaTimingFunction* fn = TimingFrom(env, tv.As<Napi::Array>().Get(i));
781
+ if (!fn) return env.Undefined();
782
+ [fns addObject:fn];
783
+ }
784
+ k.timingFunctions = fns;
785
+ }
786
+ if (o.Has("calculationMode")) {
787
+ NSString* mode = StrOr(o, "calculationMode", @"linear");
788
+ if ([mode isEqualToString:@"linear"]) k.calculationMode = kCAAnimationLinear;
789
+ else if ([mode isEqualToString:@"discrete"]) k.calculationMode = kCAAnimationDiscrete;
790
+ else if ([mode isEqualToString:@"paced"]) k.calculationMode = kCAAnimationPaced;
791
+ else if ([mode isEqualToString:@"cubic"]) k.calculationMode = kCAAnimationCubic;
792
+ else if ([mode isEqualToString:@"cubicPaced"]) k.calculationMode = kCAAnimationCubicPaced;
793
+ else {
794
+ ThrowType(env, "calculationMode: expected linear, discrete, paced, cubic or cubicPaced");
795
+ return env.Undefined();
796
+ }
797
+ }
798
+ k.duration = NumOr(o, "duration", 0.25);
799
+ a = k;
800
+ } else if (spring) {
801
+ CASpringAnimation* s = [CASpringAnimation animationWithKeyPath:keyPath];
802
+ Napi::Value sv = o.Get("spring");
803
+ if (sv.IsObject()) {
804
+ Napi::Object so = sv.As<Napi::Object>();
805
+ // checked before they are set: CA refuses a bad value with a log line
806
+ // and keeps its default, which would make the check below pass
807
+ double mass = NumOr(so, "mass", s.mass);
808
+ double stiffness = NumOr(so, "stiffness", s.stiffness);
809
+ double damping = NumOr(so, "damping", s.damping);
810
+ double velocity = NumOr(so, "initialVelocity", s.initialVelocity);
811
+ if (!(mass > 0 && stiffness > 0 && damping >= 0 && std::isfinite(velocity))) {
812
+ ThrowType(env, "spring: mass and stiffness are positive, damping is not negative");
813
+ return env.Undefined();
814
+ }
815
+ s.mass = mass;
816
+ s.stiffness = stiffness;
817
+ s.damping = damping;
818
+ s.initialVelocity = velocity;
819
+ } else if (!(sv.IsBoolean() && sv.As<Napi::Boolean>().Value())) {
820
+ ThrowType(env, "spring: expected { mass, stiffness, damping, initialVelocity } or true for CA's defaults");
821
+ return env.Undefined();
822
+ }
823
+ if (!ReadFromTo(env, s, o)) return env.Undefined();
824
+ // CA's default duration is 0.25s and would cut the spring off mid-swing;
825
+ // it settles when the physics says, unless the caller cuts it themselves
826
+ s.duration = o.Has("duration") ? NumOr(o, "duration", 0.25) : s.settlingDuration;
827
+ a = s;
828
+ } else {
829
+ CABasicAnimation* b = [CABasicAnimation animationWithKeyPath:keyPath];
830
+ if (!ReadFromTo(env, b, o)) return env.Undefined();
831
+ b.duration = NumOr(o, "duration", 0.25);
832
+ a = b;
567
833
  }
834
+ if (!ApplyTiming(env, a, L, o, key)) return env.Undefined();
568
835
  [L addAnimation:a forKey:key];
569
- return info.Env().Undefined();
836
+ return Napi::Number::New(env, a.duration);
570
837
  }
571
838
 
572
839
  static Napi::Value RemoveAnimation(const Napi::CallbackInfo& info) {
@@ -581,15 +848,103 @@ static Napi::Value RemoveAllAnimations(const Napi::CallbackInfo& info) {
581
848
  return info.Env().Undefined();
582
849
  }
583
850
 
851
+ // A CA value back to JS: a number; a point or size as [x, y]; a rect as
852
+ // [x, y, w, h]; a colour as [r, g, b, a] in generic RGB; a transform as its
853
+ // sixteen components, row by row. null for anything else.
854
+ static Napi::Value JSFromCAValue(Napi::Env env, id v) {
855
+ if (!v) return env.Null();
856
+ if ([v isKindOfClass:[NSNumber class]]) return Napi::Number::New(env, [(NSNumber*)v doubleValue]);
857
+ if ([v isKindOfClass:[NSValue class]]) {
858
+ NSValue* nv = (NSValue*)v;
859
+ const char* t = nv.objCType;
860
+ Napi::Array arr = Napi::Array::New(env);
861
+ if (strcmp(t, @encode(CGPoint)) == 0) {
862
+ CGPoint p = nv.pointValue;
863
+ arr.Set(0u, p.x); arr.Set(1u, p.y);
864
+ } else if (strcmp(t, @encode(CGSize)) == 0) {
865
+ CGSize s = nv.sizeValue;
866
+ arr.Set(0u, s.width); arr.Set(1u, s.height);
867
+ } else if (strcmp(t, @encode(CGRect)) == 0) {
868
+ CGRect r = nv.rectValue;
869
+ arr.Set(0u, r.origin.x); arr.Set(1u, r.origin.y);
870
+ arr.Set(2u, r.size.width); arr.Set(3u, r.size.height);
871
+ } else if (strcmp(t, @encode(CATransform3D)) == 0) {
872
+ CATransform3D m = nv.CATransform3DValue;
873
+ const CGFloat* c = &m.m11;
874
+ for (uint32_t i = 0; i < 16; i++) arr.Set(i, (double)c[i]);
875
+ } else {
876
+ return env.Null();
877
+ }
878
+ return arr;
879
+ }
880
+ if (CFGetTypeID((__bridge CFTypeRef)v) == CGColorGetTypeID()) {
881
+ CGColorRef c = (__bridge CGColorRef)v;
882
+ // read back in the space colours go in by (MakeColor), so a value that
883
+ // went out comes back as the same numbers
884
+ CGColorSpaceRef rgb = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
885
+ CGColorRef conv = CGColorCreateCopyByMatchingToColorSpace(rgb, kCGRenderingIntentDefault, c, NULL);
886
+ CGColorSpaceRelease(rgb);
887
+ CGColorRef src = conv ? conv : c;
888
+ const CGFloat* comps = CGColorGetComponents(src);
889
+ size_t n = CGColorGetNumberOfComponents(src);
890
+ Napi::Array arr = Napi::Array::New(env, 4);
891
+ if (n == 4) {
892
+ for (uint32_t i = 0; i < 4; i++) arr.Set(i, (double)comps[i]);
893
+ } else if (n == 2) {
894
+ arr.Set(0u, (double)comps[0]); arr.Set(1u, (double)comps[0]);
895
+ arr.Set(2u, (double)comps[0]); arr.Set(3u, (double)comps[1]);
896
+ } else {
897
+ if (conv) CGColorRelease(conv);
898
+ return env.Null();
899
+ }
900
+ if (conv) CGColorRelease(conv);
901
+ return arr;
902
+ }
903
+ return env.Null();
904
+ }
905
+
906
+ // presentationValue(layer, keyPath) -> the value the render server is
907
+ // showing for that key path right now, animations applied — or null before
908
+ // the layer's first commit. The model value is what the caller set; this is
909
+ // where the pixels are, which is the `from` an interrupted colour animation
910
+ // needs and the number a test reads a curve back through.
911
+ static Napi::Value PresentationValue(const Napi::CallbackInfo& info) {
912
+ Napi::Env env = info.Env();
913
+ CALayer* L = Deref<CALayer*>(info[0]);
914
+ NSString* keyPath = ToNSString(info[1]);
915
+ CALayer* p = L.presentationLayer;
916
+ if (!p) return env.Null();
917
+ id v = nil;
918
+ @try {
919
+ v = [p valueForKeyPath:keyPath];
920
+ } @catch (NSException* e) {
921
+ return env.Null();
922
+ }
923
+ return JSFromCAValue(env, v);
924
+ }
925
+
584
926
  static Napi::Value TxBegin(const Napi::CallbackInfo& info) {
927
+ Napi::Env env = info.Env();
928
+ CAMediaTimingFunction* timing = nil;
929
+ bool hasTiming = false;
930
+ if (info.Length() > 0 && info[0].IsObject()) {
931
+ Napi::Object o = info[0].As<Napi::Object>();
932
+ if (o.Has("timing")) {
933
+ hasTiming = true;
934
+ timing = TimingFrom(env, o.Get("timing"));
935
+ // a bad curve is reported before anything is begun, so a throw here
936
+ // leaves no transaction open
937
+ if (!timing) return env.Undefined();
938
+ }
939
+ }
585
940
  [CATransaction begin];
586
941
  if (info.Length() > 0 && info[0].IsObject()) {
587
942
  Napi::Object o = info[0].As<Napi::Object>();
588
943
  if (o.Has("duration")) [CATransaction setAnimationDuration:NumOr(o, "duration", 0.25)];
589
944
  if (BoolOr(o, "disableActions", false)) [CATransaction setDisableActions:YES];
590
- if (o.Has("timing")) [CATransaction setAnimationTimingFunction:TimingFn(ToNSString(o.Get("timing")))];
945
+ if (hasTiming) [CATransaction setAnimationTimingFunction:timing];
591
946
  }
592
- return info.Env().Undefined();
947
+ return env.Undefined();
593
948
  }
594
949
 
595
950
  static Napi::Value TxCommit(const Napi::CallbackInfo& info) {
@@ -623,7 +978,7 @@ static NSAttributedString* AttrString(Napi::Object o, CGColorRef* outColor) {
623
978
  NSString* fontName = StrOr(o, "fontName", @"Helvetica");
624
979
  double fontSize = NumOr(o, "fontSize", 14);
625
980
  CGColorRef color = o.Has("color") ? MakeColor(o.Get("color"))
626
- : CGColorCreateGenericRGB(0, 0, 0, 1);
981
+ : CGColorCreateSRGB(0, 0, 0, 1);
627
982
  CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)fontName, fontSize, NULL);
628
983
  NSDictionary* attrs = @{
629
984
  (__bridge id)kCTFontAttributeName : (__bridge id)font,
@@ -918,13 +1273,19 @@ static Napi::Value SnapshotWindow(const Napi::CallbackInfo& info) {
918
1273
  // module init
919
1274
  // ---------------------------------------------------------------------------
920
1275
 
921
- // src/backend.mm — the react-x11 backend surface (windows with delegates,
922
- // enriched events, CG surfaces, CoreText layouts, pasteboard, screens).
1276
+ // src/backend.mm — the react-x11 backend surface (initApp and the app
1277
+ // delegate, windows with delegates, enriched events, CG surfaces, CoreText
1278
+ // layouts, pasteboard, screens).
923
1279
  void InitBackend(Napi::Env env, Napi::Object exports);
1280
+ // src/permissions.mm — privacy (TCC) authorizations: status, the system
1281
+ // prompt where a framework offers one, the Settings pane otherwise.
1282
+ void InitPermissions(Napi::Env env, Napi::Object exports);
1283
+ // src/notifications.mm — user notifications through UNUserNotificationCenter:
1284
+ // settings, authorization, categories, post/update/remove, action events.
1285
+ void InitNotifications(Napi::Env env, Napi::Object exports);
924
1286
 
925
1287
  static Napi::Object Init(Napi::Env env, Napi::Object exports) {
926
1288
  #define FN(js, fn) exports.Set(js, Napi::Function::New(env, fn))
927
- FN("initApp", InitApp);
928
1289
  FN("pump", Pump);
929
1290
  FN("setEventCallback", SetEventCallback);
930
1291
  FN("postMouseEvent", PostMouseEvent);
@@ -951,6 +1312,8 @@ static Napi::Object Init(Napi::Env env, Napi::Object exports) {
951
1312
  FN("removeAllAnimations", RemoveAllAnimations);
952
1313
  FN("txBegin", TxBegin);
953
1314
  FN("txCommit", TxCommit);
1315
+ FN("presentationValue", PresentationValue);
1316
+ FN("colorSpace", ColorSpace);
954
1317
  FN("hitTest", HitTest);
955
1318
  FN("measureText", MeasureText);
956
1319
  FN("createTextImage", CreateTextImage);
@@ -960,6 +1323,8 @@ static Napi::Object Init(Napi::Env env, Napi::Object exports) {
960
1323
  FN("appearanceIsDark", AppearanceIsDark);
961
1324
  #undef FN
962
1325
  InitBackend(env, exports);
1326
+ InitPermissions(env, exports);
1327
+ InitNotifications(env, exports);
963
1328
  return exports;
964
1329
  }
965
1330