@windowkit/appkit 0.13.0 → 0.14.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/README.md CHANGED
@@ -222,6 +222,13 @@ nothing. While `runMain` runs, `listScreens`, `accessibilityDisplayOptions` and
222
222
  `pasteboardChangeCount` answer from it when called off the main thread. On the main
223
223
  thread they still ask AppKit live, as in pump mode.
224
224
 
225
+ A copy that changes with no event after it is a renderer left waiting, so `showWindow`
226
+ sends `window-shown { handle }` once the window is on screen and published. AppKit's own
227
+ notifications usually say as much, since the window moves as it is ordered in, but a
228
+ window taller than its screen is constrained, resized and moved before it is visible, and
229
+ nothing after that says it is shown. A worker that holds the frames of a window it cannot
230
+ see waited on that window for good.
231
+
225
232
  **Exit and signals.** Before the run stops, menu tracking is cancelled and an app-modal
226
233
  loop stopped, since `[NSApp stop:]` only ends the innermost loop. A drag session or a live
227
234
  resize cannot be ended from code, so the exit waits for the button to come up. While the run lasts,
@@ -555,6 +562,38 @@ const { width, height, data } = native.layoutCoverage(label.handle, 4);
555
562
  // data[y * width + x] is 0-255, and the text's origin is at (4, 4)
556
563
  ```
557
564
 
565
+ ## Text: a paragraph laid out again
566
+
567
+ Most of a `createLayout` is the text becoming glyphs: the attributed string built from the
568
+ spans, and the typesetter CoreText shapes it with — two thirds of the call over a long
569
+ document. A layout of the same paragraph at another width breaks the same glyphs into other
570
+ lines, which is what a window resize does to every paragraph of a document, and what a
571
+ paragraph's max-content and wrapped measurements are.
572
+
573
+ - **`keep: true`** — the result carries the paragraph's typesetter as `typesetter`.
574
+ - **`typesetter`** — handed to a later `createLayout` in place of `spans`, it lays the same
575
+ text out at that call's `maxWidth`, `align`, `lineHeight`, `maxLines` and `ellipsis`,
576
+ and the layout is the one the spans would have made, line for line and pixel for pixel.
577
+ `rtl` is the typesetter's and ignored beside one.
578
+ - **`native.releaseTypesetter(typesetter)`** — frees it now, where a cache lets one go.
579
+ Idempotent; a released one handed to `createLayout` throws. The finalizer is the safety
580
+ net, and the memory is reported to V8, at about 25 bytes a UTF-16 unit.
581
+ - **`packed: true`** — the geometry as two `Float64Array`s in place of `lines`: `lineData`,
582
+ ten numbers a line (`x`, `y`, `width`, `height`, `baseline`, `ascent`, `descent`,
583
+ `start`, `end`, and how many runs it has), and `runData`, five a run in line order (`x`,
584
+ `width`, `start`, `end`, and `1` when it runs right to left). An object a line and a run
585
+ is a call into V8 a field, and for short paragraphs building them was a sixth of the call.
586
+
587
+ A 50-character paragraph on an M1 Pro: 34 µs a layout from its spans, 16 µs from its kept
588
+ typesetter, 8.6 µs packed.
589
+
590
+ ```js
591
+ const first = native.createLayout({ spans, maxWidth: 600, keep: true });
592
+ const narrower = native.createLayout({ typesetter: first.typesetter, maxWidth: 420, packed: true });
593
+ // narrower.lineData, narrower.runData
594
+ native.releaseTypesetter(first.typesetter);
595
+ ```
596
+
558
597
  ## SF Symbols in a surface
559
598
 
560
599
  The system's icons by name — what `createStatusItem`'s `image` and a menu item's `iconName`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.13.0",
3
+ "version": "0.14.1",
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/accessibility-display.js && node test/ignores-mouse-events.js && node test/surface-blit.js && node test/text-features.js && node test/text-coverage.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/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/accessibility-display.js && node test/ignores-mouse-events.js && node test/surface-blit.js && node test/text-features.js && node test/text-coverage.js && node test/text-typesetter.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/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/backend.mm CHANGED
@@ -1054,6 +1054,15 @@ static double BNumArg(const Napi::CallbackInfo& info, size_t i) {
1054
1054
  }
1055
1055
 
1056
1056
  // showWindow(win, activate) — map. Popups order front without activating.
1057
+ //
1058
+ // Then `window-shown`, once the window is on screen and published: a worker
1059
+ // reads a window's visibility from what was published last, and defers the
1060
+ // frames of a window it cannot see until an event says otherwise. The event
1061
+ // that usually says so is AppKit's own — the move that follows ordering the
1062
+ // window in — but a window AppKit constrains to the screen is resized and
1063
+ // moved while it is still being ordered in, before it is visible, and
1064
+ // nothing follows. Its frames waited for good, and a `<glarea>` in it never
1065
+ // drew a frame.
1057
1066
  static Napi::Value ShowWindowFn(const Napi::CallbackInfo& info) {
1058
1067
  bool activate = info.Length() > 1 && info[1].ToBoolean().Value();
1059
1068
  OnWindow(info[0], ^(NSWindow* win) {
@@ -1064,6 +1073,7 @@ static Napi::Value ShowWindowFn(const Napi::CallbackInfo& info) {
1064
1073
  [win orderFrontRegardless];
1065
1074
  }
1066
1075
  PublishWindow(win);
1076
+ CALEmit(WindowEvent(win, "window-shown"));
1067
1077
  });
1068
1078
  return info.Env().Undefined();
1069
1079
  }
@@ -4981,27 +4991,45 @@ static CALLayout* LayoutFrom(Napi::Value v) {
4981
4991
  return (CALLayout*)v.As<Napi::External<void>>().Data();
4982
4992
  }
4983
4993
 
4984
- // createLayout({ spans: [{text, font (handle), color:[r,g,b,a]}],
4985
- // maxWidth?, align: 0 left | 0.5 center | 1 right,
4986
- // lineHeight?, maxLines?, ellipsis?, rtl? })
4987
- // -> { handle, width, height,
4988
- // lines: [{x,y,width,height,baseline,descent,start,end,
4989
- // runs:[{x,width,start,end,rtl}]}] }
4990
- static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
4991
- Napi::Env env = info.Env();
4992
- Napi::Object o = info[0].As<Napi::Object>();
4993
- double maxWidth = BNumOr(o, "maxWidth", 0);
4994
- bool bounded = maxWidth > 0 && std::isfinite(maxWidth);
4995
- double flush = BNumOr(o, "align", 0);
4996
- double lineHeight = BNumOr(o, "lineHeight", 0);
4997
- long maxLines = (long)BNumOr(o, "maxLines", 0);
4998
- bool ellipsis = BBoolOr(o, "ellipsis", false);
4999
- bool rtl = BBoolOr(o, "rtl", false);
5000
- if (ellipsis && maxLines <= 0) maxLines = 1;
4994
+ // --- a paragraph's typesetter, kept -----------------------------------------
4995
+ //
4996
+ // Most of a layout is the text becoming glyphs: the attributed string built
4997
+ // from the spans, and the typesetter CoreText shapes it with — two thirds of
4998
+ // a createLayout over a long document, the lines broken from it the rest. A
4999
+ // layout of the same paragraph at another width breaks the same glyphs into
5000
+ // other lines, so a caller that asks with `keep: true` gets the typesetter
5001
+ // back (`typesetter` on the result) and hands it to the next createLayout of
5002
+ // that text in place of the spans. It holds the shaped text and nothing
5003
+ // about a width: align, lineHeight, maxLines and the ellipsis are the
5004
+ // layout's own. `releaseTypesetter` frees one now; the finalizer is the
5005
+ // safety net, and the bytes are reported to V8 so that it runs.
5006
+ struct CALTypesetter {
5007
+ CTTypesetterRef ts = nullptr;
5008
+ NSAttributedString* text = nil; // strong, under ARC
5009
+ NSDictionary* lastAttrs = nil; // the last span's: the ellipsis token's
5010
+ bool rtl = false;
5011
+ int64_t bytes = 0;
5012
+ void Release(Napi::Env env) {
5013
+ if (ts) CFRelease(ts);
5014
+ ts = nullptr;
5015
+ text = nil;
5016
+ lastAttrs = nil;
5017
+ if (bytes) Napi::MemoryManagement::AdjustExternalMemory(env, -bytes);
5018
+ bytes = 0;
5019
+ }
5020
+ };
5021
+
5022
+ // About what CoreText keeps for a shaped paragraph — its glyphs, their
5023
+ // positions and advances, the string — measured at 25 bytes a UTF-16 unit.
5024
+ static const int64_t kTypesetterBytesPerUnit = 25;
5001
5025
 
5026
+ // The attributed string for `spans` and its typesetter; `ts` stays null for
5027
+ // an empty paragraph.
5028
+ static CALTypesetter* TypesetSpans(Napi::Array spans, bool rtl) {
5029
+ auto* shaped = new CALTypesetter();
5030
+ shaped->rtl = rtl;
5002
5031
  NSMutableAttributedString* as = [[NSMutableAttributedString alloc] init];
5003
5032
  NSDictionary* lastAttrs = nil;
5004
- Napi::Array spans = o.Get("spans").As<Napi::Array>();
5005
5033
  for (uint32_t i = 0; i < spans.Length(); i++) {
5006
5034
  Napi::Object span = spans.Get(i).As<Napi::Object>();
5007
5035
  NSString* text = span.Has("text") && span.Get("text").IsString()
@@ -5035,12 +5063,88 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5035
5063
  [as appendAttributedString:[[NSAttributedString alloc] initWithString:text
5036
5064
  attributes:attrs]];
5037
5065
  }
5066
+ shaped->text = as;
5067
+ shaped->lastAttrs = lastAttrs;
5068
+ if (as.length > 0) {
5069
+ shaped->ts = CTTypesetterCreateWithAttributedString(
5070
+ (__bridge CFAttributedStringRef)as);
5071
+ }
5072
+ return shaped;
5073
+ }
5074
+
5075
+ static void TypesetterFinalize(Napi::Env env, void* d) {
5076
+ auto* shaped = (CALTypesetter*)d;
5077
+ shaped->Release(env);
5078
+ delete shaped;
5079
+ }
5080
+
5081
+ // releaseTypesetter(handle) — free a kept typesetter now. Idempotent; a
5082
+ // createLayout handed a released one throws.
5083
+ static Napi::Value ReleaseTypesetter(const Napi::CallbackInfo& info) {
5084
+ Napi::Env env = info.Env();
5085
+ if (!info[0].IsExternal()) {
5086
+ Napi::TypeError::New(env, "releaseTypesetter: expected a typesetter")
5087
+ .ThrowAsJavaScriptException();
5088
+ return env.Undefined();
5089
+ }
5090
+ ((CALTypesetter*)info[0].As<Napi::External<void>>().Data())->Release(env);
5091
+ return env.Undefined();
5092
+ }
5093
+
5094
+ // createLayout({ spans: [{text, font (handle), color:[r,g,b,a]}],
5095
+ // maxWidth?, align: 0 left | 0.5 center | 1 right,
5096
+ // lineHeight?, maxLines?, ellipsis?, rtl?,
5097
+ // keep?, typesetter?, packed? })
5098
+ // -> { handle, width, height,
5099
+ // lines: [{x,y,width,height,baseline,descent,start,end,
5100
+ // runs:[{x,width,start,end,rtl}]}],
5101
+ // typesetter? }
5102
+ //
5103
+ // `keep: true` returns the paragraph's typesetter as `typesetter`, and a
5104
+ // later createLayout given it in place of `spans` breaks the same shaped
5105
+ // text at its own options — see CALTypesetter; `rtl` is the typesetter's,
5106
+ // and ignored beside one. `packed: true` returns the geometry as two
5107
+ // Float64Arrays in place of `lines`, which is a tenth of the calls into the
5108
+ // engine for a paragraph of a few lines: `lineData`, ten numbers a line —
5109
+ // x, y, width, height, baseline, ascent, descent, start, end and how many
5110
+ // runs it has — and `runData`, five a run in line order — x, width, start,
5111
+ // end, and 1 when it runs right to left.
5112
+ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5113
+ Napi::Env env = info.Env();
5114
+ Napi::Object o = info[0].As<Napi::Object>();
5115
+ double maxWidth = BNumOr(o, "maxWidth", 0);
5116
+ bool bounded = maxWidth > 0 && std::isfinite(maxWidth);
5117
+ double flush = BNumOr(o, "align", 0);
5118
+ double lineHeight = BNumOr(o, "lineHeight", 0);
5119
+ long maxLines = (long)BNumOr(o, "maxLines", 0);
5120
+ bool ellipsis = BBoolOr(o, "ellipsis", false);
5121
+ bool rtl = BBoolOr(o, "rtl", false);
5122
+ bool keep = BBoolOr(o, "keep", false);
5123
+ bool packed = BBoolOr(o, "packed", false);
5124
+ if (ellipsis && maxLines <= 0) maxLines = 1;
5125
+
5126
+ CALTypesetter* shaped = nullptr;
5127
+ bool owned = false;
5128
+ if (o.Has("typesetter") && o.Get("typesetter").IsExternal()) {
5129
+ shaped = (CALTypesetter*)o.Get("typesetter")
5130
+ .As<Napi::External<void>>()
5131
+ .Data();
5132
+ if (!shaped->text) {
5133
+ Napi::Error::New(env, "createLayout: the typesetter was released")
5134
+ .ThrowAsJavaScriptException();
5135
+ return env.Undefined();
5136
+ }
5137
+ } else {
5138
+ shaped = TypesetSpans(o.Get("spans").As<Napi::Array>(), rtl);
5139
+ owned = true;
5140
+ }
5141
+ NSAttributedString* as = shaped->text;
5142
+ NSDictionary* lastAttrs = shaped->lastAttrs;
5143
+ CTTypesetterRef ts = shaped->ts;
5038
5144
 
5039
5145
  auto* layout = new CALLayout();
5040
5146
  long total = (long)as.length;
5041
- if (total > 0) {
5042
- CTTypesetterRef ts =
5043
- CTTypesetterCreateWithAttributedString((__bridge CFAttributedStringRef)as);
5147
+ if (total > 0 && ts) {
5044
5148
  double y = 0;
5045
5149
  long start = 0;
5046
5150
  long lineIndex = 0;
@@ -5132,7 +5236,6 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5132
5236
  if (maxLines > 0 && lineIndex >= maxLines) break;
5133
5237
  }
5134
5238
  layout->height = y;
5135
- CFRelease(ts);
5136
5239
  }
5137
5240
 
5138
5241
  Napi::Object r = Napi::Object::New(env);
@@ -5141,6 +5244,46 @@ static Napi::Value CreateLayout(const Napi::CallbackInfo& info) {
5141
5244
  }));
5142
5245
  r.Set("width", layout->width);
5143
5246
  r.Set("height", layout->height);
5247
+ if (owned && keep) {
5248
+ shaped->bytes = (int64_t)total * kTypesetterBytesPerUnit;
5249
+ if (shaped->bytes)
5250
+ Napi::MemoryManagement::AdjustExternalMemory(env, shaped->bytes);
5251
+ r.Set("typesetter",
5252
+ Napi::External<void>::New(env, shaped, TypesetterFinalize));
5253
+ } else if (owned) {
5254
+ shaped->Release(env);
5255
+ delete shaped;
5256
+ }
5257
+ if (packed) {
5258
+ size_t runCount = 0;
5259
+ for (const CALLine& L : layout->lines) runCount += L.runs.size();
5260
+ Napi::Float64Array lineData =
5261
+ Napi::Float64Array::New(env, layout->lines.size() * 10);
5262
+ Napi::Float64Array runData = Napi::Float64Array::New(env, runCount * 5);
5263
+ size_t li = 0, ri = 0;
5264
+ for (const CALLine& L : layout->lines) {
5265
+ lineData[li++] = L.x;
5266
+ lineData[li++] = L.y;
5267
+ lineData[li++] = L.width;
5268
+ lineData[li++] = L.height;
5269
+ lineData[li++] = L.baseline;
5270
+ lineData[li++] = L.ascent;
5271
+ lineData[li++] = L.descent;
5272
+ lineData[li++] = (double)L.start;
5273
+ lineData[li++] = (double)L.end;
5274
+ lineData[li++] = (double)L.runs.size();
5275
+ for (const CALRun& R : L.runs) {
5276
+ runData[ri++] = R.x;
5277
+ runData[ri++] = R.width;
5278
+ runData[ri++] = (double)R.start;
5279
+ runData[ri++] = (double)R.end;
5280
+ runData[ri++] = R.rtl ? 1 : 0;
5281
+ }
5282
+ }
5283
+ r.Set("lineData", lineData);
5284
+ r.Set("runData", runData);
5285
+ return r;
5286
+ }
5144
5287
  Napi::Array lines = Napi::Array::New(env, layout->lines.size());
5145
5288
  for (size_t i = 0; i < layout->lines.size(); i++) {
5146
5289
  const CALLine& L = layout->lines[i];
@@ -6937,6 +7080,7 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
6937
7080
  BFN("listFonts", ListFonts);
6938
7081
  BFN("loadFontData", LoadFontData);
6939
7082
  BFN("createLayout", CreateLayout);
7083
+ BFN("releaseTypesetter", ReleaseTypesetter);
6940
7084
  BFN("drawLayout", DrawLayout);
6941
7085
  BFN("ctxDrawGlyphs", CtxDrawGlyphs);
6942
7086
  BFN("ctxDrawSymbol", CtxDrawSymbol);