@windowkit/appkit 0.26.0 → 0.27.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 CHANGED
@@ -431,6 +431,22 @@ the bridge turns them over where the layer's space is flipped; `presentationValu
431
431
  'contentsGravity')` answers Core Animation's name, `bottomLeft` for a `topLeft` here.
432
432
  Any other name, or a value that is not one, is a `TypeError`, and nothing is applied.
433
433
 
434
+ **Contents rect and center.** `setLayerProps`' `contentsRect` is the part of a layer's
435
+ contents it shows, and `contentsCenter` the part that stretches when they are scaled to
436
+ bounds that are not their size — what is outside it keeps its size, at its edge of the
437
+ bounds. Both are rects in the unit square of the contents, `[x, y, width, height]`, named
438
+ as they look: `y` runs down from the window's top, where Core Animation's runs up from the
439
+ layer's least `y`, so the bridge turns them over where the layer's space is not flipped,
440
+ as it does gravities. `null` is the whole square, the default for both. Between them a
441
+ frame whose bounds outgrew it can be shown at its size with its last column and row
442
+ carried over the rest — the way a page continues past its edge, where a gravity either
443
+ stretches the frame or leaves a strip of whatever is under it: `contentsRect` crops an
444
+ axis the bounds shrank on, and `contentsCenter` names the last pixel. Name its middle, a
445
+ sliver of it: a centre a whole pixel wide stretches what the filter makes of it and the
446
+ pixel before it, a blend of the two, and Core Animation ignores a centre of no width.
447
+ Four finite numbers or `null`, or the call is a `TypeError` naming the key, and nothing is
448
+ applied.
449
+
434
450
  **`native.transformForms()`** — `['translate', 'rotate', 'scale', 'matrix', 'matrix3d']`:
435
451
  the forms a transform takes on its way in. A caller that hands over matrices
436
452
  feature-detects `matrix` here; before 0.19 an object holding one was read as no transform
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.26.0",
3
+ "version": "0.27.0",
4
4
  "description": "Retained-mode AppKit backend for Node.js: Core Animation (CALayer) layer trees, CoreText layout and drawing, IOSurface presentation, NSMenu, and native control bezels — the macOS half of react-x11.",
5
5
  "author": "Andrey Sidorov <andrey.sidorov@gmail.com>",
6
6
  "license": "MIT",
@@ -43,7 +43,7 @@
43
43
  "install": "node scripts/install.js",
44
44
  "build": "node-gyp rebuild",
45
45
  "demo": "node demo/demo.js",
46
- "test": "node test/app-lifecycle.js && node test/animation.js && node test/layer-transform.js && node test/contents-gravity.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/popup-menu.js && node test/surface-blit.js && node test/surface-faded.js && node test/image-smoothing.js && node test/surface-colorspace.js && node test/video-surface.js && node test/player.js && node test/round-rect-xy.js && node test/clip-rule.js && node test/control-bezels.js && node test/text-features.js && node test/font-from-data.js && node test/text-coverage.js && node test/text-typesetter.js && node test/text-line-height.js && node test/text-line-width.js && node test/text-justify.js && node test/text-scale.js && node test/symbols.js && node test/eventkit-permissions.js && node test/calendars.js && node test/calendar-writes.js && node test/screencolor.js && node test/notifications.js && node test/threaded.js && node test/threaded-verbs.js && node test/threaded-frames.js && node test/threaded-resize.js && node test/ending-worker.js && node test/live-resize.js && node test/activation-policy.js"
46
+ "test": "node test/app-lifecycle.js && node test/animation.js && node test/layer-transform.js && node test/contents-gravity.js && node test/contents-center.js && node test/accessibility-display.js && node test/ignores-mouse-events.js && node test/popup-menu.js && node test/surface-blit.js && node test/surface-faded.js && node test/image-smoothing.js && node test/surface-colorspace.js && node test/video-surface.js && node test/player.js && node test/round-rect-xy.js && node test/clip-rule.js && node test/control-bezels.js && node test/text-features.js && node test/font-from-data.js && node test/text-coverage.js && node test/text-typesetter.js && node test/text-line-height.js && node test/text-line-width.js && node test/text-justify.js && node test/text-scale.js && node test/symbols.js && node test/eventkit-permissions.js && node test/calendars.js && node test/calendar-writes.js && node test/screencolor.js && node test/notifications.js && node test/threaded.js && node test/threaded-verbs.js && node test/threaded-frames.js && node test/threaded-resize.js && node test/ending-worker.js && node test/live-resize.js && node test/activation-policy.js"
47
47
  },
48
48
  "dependencies": {
49
49
  "node-addon-api": "^8.3.0"
Binary file
package/src/addon.mm CHANGED
@@ -585,6 +585,10 @@ struct LayerPropsSpec {
585
585
  CATransform3D transform = CATransform3DIdentity;
586
586
  bool hasGravity = false, badGravity = false;
587
587
  NSString* gravity = nil; // as named: the window's top is `top`
588
+ // unit rects as named: y down from the window's top (`CALUnitRect`)
589
+ bool hasContentsRect = false, hasContentsCenter = false;
590
+ const char* badUnitRect = nullptr; // the key of a value that is no unit rect
591
+ CGRect contentsRect = CGRectMake(0, 0, 1, 1), contentsCenter = CGRectMake(0, 0, 1, 1);
588
592
  };
589
593
 
590
594
  // `contentsGravity`: how a layer's contents sit in bounds that are not
@@ -626,6 +630,44 @@ static NSString* CALGravity(CALayer* L, NSString* name) {
626
630
  return kCAGravityResize;
627
631
  }
628
632
 
633
+ // `contentsRect` and `contentsCenter`, rects in the unit square of a layer's
634
+ // contents, [x, y, width, height]: four finite numbers, or null for the whole
635
+ // square, which is Core Animation's default for both. `contentsRect` is the
636
+ // part of the contents the layer shows. `contentsCenter` is the part that
637
+ // stretches when the contents are scaled to bounds that are not their size —
638
+ // what is outside it keeps its size, at its edge of the bounds — so a frame
639
+ // whose bounds outgrew it can be shown at its size with its last column and
640
+ // row carried over the rest, the way a page continues past its edge, where
641
+ // gravity alone either stretches it or leaves a strip of whatever is under
642
+ // it.
643
+ static bool UnitRectFrom(Napi::Value v, CGRect* out) {
644
+ if (v.IsNull() || v.IsUndefined()) {
645
+ *out = CGRectMake(0, 0, 1, 1);
646
+ return true;
647
+ }
648
+ if (!v.IsArray() || v.As<Napi::Array>().Length() != 4) return false;
649
+ Napi::Array a = v.As<Napi::Array>();
650
+ double c[4];
651
+ for (uint32_t i = 0; i < 4; i++) {
652
+ Napi::Value e = a.Get(i);
653
+ if (!e.IsNumber()) return false;
654
+ c[i] = e.As<Napi::Number>().DoubleValue();
655
+ if (!std::isfinite(c[i])) return false;
656
+ }
657
+ *out = CGRectMake(c[0], c[1], c[2], c[3]);
658
+ return true;
659
+ }
660
+
661
+ // A unit rect named as it looks, y down from the window's top, made Core
662
+ // Animation's on the UI thread, where the layer can say whether its space is
663
+ // flipped. CA's unit square is the layer's own space, as its gravities are
664
+ // (`CALGravity`): its least y is the window's top where the contents are
665
+ // flipped, under a window's geometry-flipped root, and the bottom elsewhere.
666
+ static CGRect CALUnitRect(CALayer* L, CGRect r) {
667
+ if (L.contentsAreFlipped) return r;
668
+ return CGRectMake(r.origin.x, 1 - r.origin.y - r.size.height, r.size.width, r.size.height);
669
+ }
670
+
629
671
  static LayerPropsSpec ParseLayerProps(Napi::Object o) {
630
672
  LayerPropsSpec s;
631
673
  if (o.Has("frame")) {
@@ -694,6 +736,12 @@ static LayerPropsSpec ParseLayerProps(Napi::Object o) {
694
736
  s.gravity = g.IsString() ? ToNSString(g) : nil;
695
737
  s.badGravity = !CALGravityKnown(s.gravity);
696
738
  }
739
+ if ((s.hasContentsRect = o.Has("contentsRect")) &&
740
+ !UnitRectFrom(o.Get("contentsRect"), &s.contentsRect))
741
+ s.badUnitRect = "contentsRect";
742
+ if ((s.hasContentsCenter = o.Has("contentsCenter")) &&
743
+ !UnitRectFrom(o.Get("contentsCenter"), &s.contentsCenter))
744
+ s.badUnitRect = "contentsCenter";
697
745
  return s;
698
746
  }
699
747
 
@@ -724,6 +772,8 @@ static void ApplyLayerProps(CALayer* L, const LayerPropsSpec& s) {
724
772
  L.contents = nil;
725
773
  }
726
774
  if (s.hasGravity) L.contentsGravity = CALGravity(L, s.gravity);
775
+ if (s.hasContentsRect) L.contentsRect = CALUnitRect(L, s.contentsRect);
776
+ if (s.hasContentsCenter) L.contentsCenter = CALUnitRect(L, s.contentsCenter);
727
777
  }
728
778
 
729
779
  static Napi::Value SetLayerProps(const Napi::CallbackInfo& info) {
@@ -734,6 +784,14 @@ static Napi::Value SetLayerProps(const Napi::CallbackInfo& info) {
734
784
  .ThrowAsJavaScriptException();
735
785
  return info.Env().Undefined();
736
786
  }
787
+ if (s.badUnitRect) {
788
+ Napi::TypeError::New(info.Env(),
789
+ std::string(s.badUnitRect) +
790
+ ": [x, y, width, height], four finite numbers in the unit "
791
+ "square of the contents, y down from the top, or null")
792
+ .ThrowAsJavaScriptException();
793
+ return info.Env().Undefined();
794
+ }
737
795
  if (s.badGravity) {
738
796
  Napi::TypeError::New(info.Env(),
739
797
  "contentsGravity: resize, resizeAspect, resizeAspectFill, center, "