@windowkit/appkit 0.26.0 → 0.28.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
@@ -126,7 +126,7 @@ waits on the UI thread (`test/threaded-verbs.js` covers each row):
126
126
  | from a worker | verbs |
127
127
  | --- | --- |
128
128
  | unchanged, on the calling thread | surfaces and video surfaces, every `ctx*` (`ctxDrawSymbol` included), layouts and fonts, `symbolSize`, `pasteboardTypeForMIME`, `pasteboardTypeInfo`, `contentTypeFor`, `colorSpace` |
129
- | a command, answering nothing | `initApp`, `setActivationPolicy`, `setAppName`, `activateApp`, `showWindow`, `hideWindow`, `setWindowFrame`, `setWindowTitle`, `setWindowMinMax`, `setWindowIgnoresMouseEvents`, `invalidateWindowShadow`, `destroyWindow2`, `setCursor`, `setMainMenu`, `setDockMenu`, `setDockBadge`, `cancelUserAttention`, `setStatusItem`, `setStatusItemMenu`, `removeStatusItem`, `registerDropTypes`, `setDropResponse`, `pasteboardWriteText`, `pasteboardClear`, `cancelPanel`, `cancelPopUpMenu`; the test posts `postMouseEvent`, `postKeyEvent`, `postAppleEvent`, `postAccessibilityDisplayChange` |
129
+ | a command, answering nothing | `initApp`, `setActivationPolicy`, `setAppName`, `activateApp`, `showWindow`, `hideWindow`, `setWindowFrame`, `setWindowTitle`, `setWindowMinMax`, `setWindowIgnoresMouseEvents`, `invalidateWindowShadow`, `destroyWindow2`, `setCursor`, `setMainMenu`, `setDockMenu`, `setDockBadge`, `cancelUserAttention`, `setStatusItem`, `setStatusItemMenu`, `removeStatusItem`, `registerDropTypes`, `setDropResponse`, `pasteboardWriteText`, `pasteboardWrite`, `pasteboardClear`, `cancelPanel`, `cancelPopUpMenu`; the test posts `postMouseEvent`, `postKeyEvent`, `postAppleEvent`, `postAccessibilityDisplayChange` |
130
130
  | a handle at the call | `createWindow2`, followed by `window-created { handle, windowNumber }`; every event about the window, input included, carries `handle`, so `ev.handle === win` |
131
131
  | | `windowRootLayer`, allocated with the window |
132
132
  | | `createStatusItem`: its clicks carry the handle, and it is held until `removeStatusItem` |
@@ -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.28.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/pasteboard-write.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/radial-gradient.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, "
package/src/backend.mm CHANGED
@@ -4371,16 +4371,9 @@ static Napi::Value CtxFillRects(const Napi::CallbackInfo& info) {
4371
4371
  return info.Env().Undefined();
4372
4372
  }
4373
4373
 
4374
- // fillLinearGradient(surface, x0, y0, x1, y1, stops [offset,r,g,b,a,...],
4375
- // mode: 0 = fill current path, 1 = fill rect args follow)
4376
- static Napi::Value CtxFillLinearGradient(const Napi::CallbackInfo& info) {
4377
- CALSurface* s = SurfaceFrom(info[0]);
4378
- if (!s) return info.Env().Undefined();
4379
- double x0 = info[1].As<Napi::Number>().DoubleValue();
4380
- double y0 = info[2].As<Napi::Number>().DoubleValue();
4381
- double x1 = info[3].As<Napi::Number>().DoubleValue();
4382
- double y1 = info[4].As<Napi::Number>().DoubleValue();
4383
- Napi::Array stopsArr = info[5].As<Napi::Array>();
4374
+ // A gradient's stops, flat as [offset, r, g, b, a, ...] with each offset in
4375
+ // [0, 1], in sRGB: what fillLinearGradient and fillRadialGradient draw.
4376
+ static CGGradientRef GradientOfStops(Napi::Array stopsArr) {
4384
4377
  std::vector<CGFloat> locs;
4385
4378
  std::vector<CGFloat> comps;
4386
4379
  for (uint32_t i = 0; i + 4 < stopsArr.Length(); i += 5) {
@@ -4394,17 +4387,37 @@ static Napi::Value CtxFillLinearGradient(const Napi::CallbackInfo& info) {
4394
4387
  CGGradientRef grad = CGGradientCreateWithColorComponents(
4395
4388
  cs, comps.data(), locs.data(), locs.size());
4396
4389
  CGColorSpaceRelease(cs);
4397
- CGContextSaveGState(s->ctx);
4398
- if (info.Length() > 6 && info[6].IsNumber()) {
4399
- // clip to the given rect (fillRect with a gradient fillStyle)
4400
- CGContextClipToRect(s->ctx,
4401
- CGRectMake(info[6].As<Napi::Number>().DoubleValue(),
4402
- info[7].As<Napi::Number>().DoubleValue(),
4403
- info[8].As<Napi::Number>().DoubleValue(),
4404
- info[9].As<Napi::Number>().DoubleValue()));
4390
+ return grad;
4391
+ }
4392
+
4393
+ // What a gradient fill is cut to, in a saved state: the rect whose x, y, w
4394
+ // and h start at argument `at` where one follows (fillRect with a gradient
4395
+ // fillStyle), else the current path, which is kept for after.
4396
+ static void ClipForGradient(CALSurface* s, const Napi::CallbackInfo& info,
4397
+ size_t at) {
4398
+ if (info.Length() > at + 3 && info[at].IsNumber()) {
4399
+ CGContextClipToRect(
4400
+ s->ctx, CGRectMake(info[at].As<Napi::Number>().DoubleValue(),
4401
+ info[at + 1].As<Napi::Number>().DoubleValue(),
4402
+ info[at + 2].As<Napi::Number>().DoubleValue(),
4403
+ info[at + 3].As<Napi::Number>().DoubleValue()));
4405
4404
  } else {
4406
4405
  KeepPathAround(s->ctx, ^{ CGContextClip(s->ctx); });
4407
4406
  }
4407
+ }
4408
+
4409
+ // fillLinearGradient(surface, x0, y0, x1, y1, stops [offset,r,g,b,a,...],
4410
+ // mode: 0 = fill current path, 1 = fill rect args follow)
4411
+ static Napi::Value CtxFillLinearGradient(const Napi::CallbackInfo& info) {
4412
+ CALSurface* s = SurfaceFrom(info[0]);
4413
+ if (!s) return info.Env().Undefined();
4414
+ double x0 = info[1].As<Napi::Number>().DoubleValue();
4415
+ double y0 = info[2].As<Napi::Number>().DoubleValue();
4416
+ double x1 = info[3].As<Napi::Number>().DoubleValue();
4417
+ double y1 = info[4].As<Napi::Number>().DoubleValue();
4418
+ CGGradientRef grad = GradientOfStops(info[5].As<Napi::Array>());
4419
+ CGContextSaveGState(s->ctx);
4420
+ ClipForGradient(s, info, 6);
4408
4421
  CGContextDrawLinearGradient(
4409
4422
  s->ctx, grad, CGPointMake(x0, y0), CGPointMake(x1, y1),
4410
4423
  kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
@@ -4413,6 +4426,34 @@ static Napi::Value CtxFillLinearGradient(const Napi::CallbackInfo& info) {
4413
4426
  return info.Env().Undefined();
4414
4427
  }
4415
4428
 
4429
+ // fillRadialGradient(surface, x0, y0, r0, x1, y1, r1,
4430
+ // stops [offset,r,g,b,a,...], x?, y?, w?, h?)
4431
+ //
4432
+ // canvas's createRadialGradient: the colours spread from the start circle to
4433
+ // the end one, and the end colours carried on past both, as canvas extends
4434
+ // them — the current path filled with it, or the rect that follows the
4435
+ // stops. CoreGraphics' two-circle gradient is canvas's: the same cone where
4436
+ // the circles do not nest, painted from the end circle in.
4437
+ static Napi::Value CtxFillRadialGradient(const Napi::CallbackInfo& info) {
4438
+ CALSurface* s = SurfaceFrom(info[0]);
4439
+ if (!s) return info.Env().Undefined();
4440
+ double x0 = info[1].As<Napi::Number>().DoubleValue();
4441
+ double y0 = info[2].As<Napi::Number>().DoubleValue();
4442
+ double r0 = info[3].As<Napi::Number>().DoubleValue();
4443
+ double x1 = info[4].As<Napi::Number>().DoubleValue();
4444
+ double y1 = info[5].As<Napi::Number>().DoubleValue();
4445
+ double r1 = info[6].As<Napi::Number>().DoubleValue();
4446
+ CGGradientRef grad = GradientOfStops(info[7].As<Napi::Array>());
4447
+ CGContextSaveGState(s->ctx);
4448
+ ClipForGradient(s, info, 8);
4449
+ CGContextDrawRadialGradient(
4450
+ s->ctx, grad, CGPointMake(x0, y0), r0, CGPointMake(x1, y1), r1,
4451
+ kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
4452
+ CGContextRestoreGState(s->ctx);
4453
+ CGGradientRelease(grad);
4454
+ return info.Env().Undefined();
4455
+ }
4456
+
4416
4457
  // drawSurface(dst, src, sx, sy, sw, sh, dx, dy, dw, dh)
4417
4458
  static Napi::Value CtxDrawSurface(const Napi::CallbackInfo& info) {
4418
4459
  CALSurface* dst = SurfaceFrom(info[0]);
@@ -7221,6 +7262,33 @@ static NSArray<NSPasteboardItem*>* BuildPasteboardItems(const PbItemsSpec& spec,
7221
7262
  return items;
7222
7263
  }
7223
7264
 
7265
+ // pasteboardWrite(items) — the general pasteboard's contents, replaced by
7266
+ // any number of representations at once: `{ 'public.png': bytes,
7267
+ // 'public.utf8-plain-text': 'caption' }` is one item a paste may take
7268
+ // either way, and an array is one entry per item. The same shape a drag
7269
+ // takes, so a copy and a drag of one payload are one vocabulary. Values are
7270
+ // strings or bytes (Buffer, typed array, ArrayBuffer); null is dropped,
7271
+ // since nothing would ever be asked to provide it. Read on the calling
7272
+ // thread, written on the UI thread, like pasteboardWriteText.
7273
+ static Napi::Value PbWriteFn(const Napi::CallbackInfo& info) {
7274
+ Napi::Env env = info.Env();
7275
+ if (info.Length() < 1 || !info[0].IsObject()) {
7276
+ Napi::TypeError::New(env,
7277
+ "pasteboardWrite(items): items is { [type]: string "
7278
+ "| bytes } or an array of them")
7279
+ .ThrowAsJavaScriptException();
7280
+ return env.Undefined();
7281
+ }
7282
+ PbItemsSpec spec = ParsePasteboardItems(env, info[0], false);
7283
+ CALOnUI(^{
7284
+ NSPasteboard* pb = NSPasteboard.generalPasteboard;
7285
+ [pb clearContents];
7286
+ [pb writeObjects:BuildPasteboardItems(spec, nil)];
7287
+ PublishPasteboardCount();
7288
+ });
7289
+ return env.Undefined();
7290
+ }
7291
+
7224
7292
  // The drag image, from either bitmap this addon deals in: `surface`, a
7225
7293
  // surface handle (the renderer's own paint, its scale known, its pixels
7226
7294
  // copied as they are at the call), or `image`, a CGImage External — or the
@@ -7954,6 +8022,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
7954
8022
  BFN("ctxClearRect", CtxClearRect);
7955
8023
  BFN("ctxFillRects", CtxFillRects);
7956
8024
  BFN("ctxFillLinearGradient", CtxFillLinearGradient);
8025
+ BFN("ctxFillRadialGradient", CtxFillRadialGradient);
7957
8026
  BFN("ctxDrawSurface", CtxDrawSurface);
7958
8027
  BFN("ctxDrawSurfaceFaded", CtxDrawSurfaceFaded);
7959
8028
  BFN("ctxPutImageData", CtxPutImageData);
@@ -7987,6 +8056,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
7987
8056
  BFN("layoutCaret", LayoutCaret);
7988
8057
  BFN("layoutCoverage", LayoutCoverage);
7989
8058
  BFN("pasteboardWriteText", PbWriteTextFn);
8059
+ BFN("pasteboardWrite", PbWriteFn);
7990
8060
  BFN("pasteboardReadText", PbReadTextFn);
7991
8061
  BFN("pasteboardClear", PbClearFn);
7992
8062
  BFN("pasteboardChangeCount", PbChangeCountFn);