@windowkit/appkit 0.27.0 → 0.29.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
@@ -125,8 +125,8 @@ waits on the UI thread (`test/threaded-verbs.js` covers each row):
125
125
 
126
126
  | from a worker | verbs |
127
127
  | --- | --- |
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` |
128
+ | unchanged, on the calling thread | surfaces and video surfaces, every `ctx*` (`ctxDrawSymbol` included), layouts and fonts, `symbolSize`, `pasteboardTypeForMIME`, `pasteboardTypeInfo`, `contentTypeFor`, `colorSpace`, `lastInputMs` |
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` |
@@ -1599,6 +1599,23 @@ Mechanism only: what to do with `reduceMotion` is the renderer's (react-x11: loo
1599
1599
  so the observer path can be exercised without touching the user's settings — test-only, like
1600
1600
  `postAppleEvent`; `test/accessibility-display.js` is the check.
1601
1601
 
1602
+ ## Idle time (an empty desk)
1603
+
1604
+ How long the user has been away from the keyboard and pointer, across the whole session and
1605
+ every app — the clock a screen saver keys off (react-x11's `useIdle`,
1606
+ [react-x11#951](https://github.com/sidorares/react-x11/pull/951)):
1607
+
1608
+ ```js
1609
+ native.lastInputMs(); // -> 41250.3: ms since the last keyboard, mouse, trackpad or tablet event
1610
+ ```
1611
+
1612
+ It is `CGEventSourceSecondsSinceLastEventType(kCGEventSourceStateCombinedSessionState,
1613
+ kCGAnyInputEventType)`, in milliseconds. There is no event: poll it. It asks nothing of AppKit and
1614
+ needs no permission, so it answers synchronously on whichever thread calls it — before
1615
+ `initApp`, in pump mode, and from a worker with the main thread parked in `runMain`. It is not
1616
+ `ioreg`'s `HIDIdleTime`: the two are kept apart, and while they agree at a desk, on CI's VMs this
1617
+ one ran 3–5 s longer. `test/last-input.js` is the check.
1618
+
1602
1619
  ## Mapping to a React reconciler
1603
1620
 
1604
1621
  The shape of a host config on top of this:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.27.0",
3
+ "version": "0.29.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/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"
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 && node test/last-input.js"
47
47
  },
48
48
  "dependencies": {
49
49
  "node-addon-api": "^8.3.0"
Binary file
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]);
@@ -6888,6 +6929,22 @@ static Napi::Value PbChangeCountFn(const Napi::CallbackInfo& info) {
6888
6929
  (double)NSPasteboard.generalPasteboard.changeCount);
6889
6930
  }
6890
6931
 
6932
+ // ---------------------------------------------------------------------------
6933
+ // idle time
6934
+ // ---------------------------------------------------------------------------
6935
+
6936
+ // lastInputMs() -> number: milliseconds since the last input event of any
6937
+ // kind in the login session — keyboard, mouse, trackpad, tablet — from any
6938
+ // app, the clock a screen saver keys off. CGEventSource keeps it per session,
6939
+ // so it asks nothing of AppKit or the window server's event stream and needs
6940
+ // no permission; it answers on whichever thread calls it, before initApp
6941
+ // and from a worker alike.
6942
+ static Napi::Value LastInputMsFn(const Napi::CallbackInfo& info) {
6943
+ CFTimeInterval s = CGEventSourceSecondsSinceLastEventType(
6944
+ kCGEventSourceStateCombinedSessionState, kCGAnyInputEventType);
6945
+ return Napi::Number::New(info.Env(), s * 1000.0);
6946
+ }
6947
+
6891
6948
  // ---------------------------------------------------------------------------
6892
6949
  // screens + cursors + appearance
6893
6950
  // ---------------------------------------------------------------------------
@@ -7221,6 +7278,33 @@ static NSArray<NSPasteboardItem*>* BuildPasteboardItems(const PbItemsSpec& spec,
7221
7278
  return items;
7222
7279
  }
7223
7280
 
7281
+ // pasteboardWrite(items) — the general pasteboard's contents, replaced by
7282
+ // any number of representations at once: `{ 'public.png': bytes,
7283
+ // 'public.utf8-plain-text': 'caption' }` is one item a paste may take
7284
+ // either way, and an array is one entry per item. The same shape a drag
7285
+ // takes, so a copy and a drag of one payload are one vocabulary. Values are
7286
+ // strings or bytes (Buffer, typed array, ArrayBuffer); null is dropped,
7287
+ // since nothing would ever be asked to provide it. Read on the calling
7288
+ // thread, written on the UI thread, like pasteboardWriteText.
7289
+ static Napi::Value PbWriteFn(const Napi::CallbackInfo& info) {
7290
+ Napi::Env env = info.Env();
7291
+ if (info.Length() < 1 || !info[0].IsObject()) {
7292
+ Napi::TypeError::New(env,
7293
+ "pasteboardWrite(items): items is { [type]: string "
7294
+ "| bytes } or an array of them")
7295
+ .ThrowAsJavaScriptException();
7296
+ return env.Undefined();
7297
+ }
7298
+ PbItemsSpec spec = ParsePasteboardItems(env, info[0], false);
7299
+ CALOnUI(^{
7300
+ NSPasteboard* pb = NSPasteboard.generalPasteboard;
7301
+ [pb clearContents];
7302
+ [pb writeObjects:BuildPasteboardItems(spec, nil)];
7303
+ PublishPasteboardCount();
7304
+ });
7305
+ return env.Undefined();
7306
+ }
7307
+
7224
7308
  // The drag image, from either bitmap this addon deals in: `surface`, a
7225
7309
  // surface handle (the renderer's own paint, its scale known, its pixels
7226
7310
  // copied as they are at the call), or `image`, a CGImage External — or the
@@ -7954,6 +8038,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
7954
8038
  BFN("ctxClearRect", CtxClearRect);
7955
8039
  BFN("ctxFillRects", CtxFillRects);
7956
8040
  BFN("ctxFillLinearGradient", CtxFillLinearGradient);
8041
+ BFN("ctxFillRadialGradient", CtxFillRadialGradient);
7957
8042
  BFN("ctxDrawSurface", CtxDrawSurface);
7958
8043
  BFN("ctxDrawSurfaceFaded", CtxDrawSurfaceFaded);
7959
8044
  BFN("ctxPutImageData", CtxPutImageData);
@@ -7987,9 +8072,11 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
7987
8072
  BFN("layoutCaret", LayoutCaret);
7988
8073
  BFN("layoutCoverage", LayoutCoverage);
7989
8074
  BFN("pasteboardWriteText", PbWriteTextFn);
8075
+ BFN("pasteboardWrite", PbWriteFn);
7990
8076
  BFN("pasteboardReadText", PbReadTextFn);
7991
8077
  BFN("pasteboardClear", PbClearFn);
7992
8078
  BFN("pasteboardChangeCount", PbChangeCountFn);
8079
+ BFN("lastInputMs", LastInputMsFn);
7993
8080
  BFN("setMainMenu", SetMainMenuFn);
7994
8081
  BFN("mainMenuInfo", MainMenuInfoFn);
7995
8082
  BFN("activateMenuItem", ActivateMenuItemFn);