@windowkit/appkit 0.11.0 → 0.12.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,7 +125,7 @@ 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, every `ctx*`, layouts and fonts, `pasteboardTypeForMIME`, `pasteboardTypeInfo`, `contentTypeFor`, `colorSpace` |
128
+ | unchanged, on the calling thread | surfaces, every `ctx*` (`ctxDrawSymbol` included), layouts and fonts, `symbolSize`, `pasteboardTypeForMIME`, `pasteboardTypeInfo`, `contentTypeFor`, `colorSpace` |
129
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`; 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 |
@@ -524,6 +524,41 @@ const label = native.createLayout({
524
524
  });
525
525
  ```
526
526
 
527
+ ## SF Symbols in a surface
528
+
529
+ The system's icons by name — what `createStatusItem`'s `image` and a menu item's `iconName`
530
+ already take — drawn into a surface, for a renderer's own content:
531
+
532
+ - **`native.ctxDrawSymbol(surface, name, x, y, width, height, options?)`** — the symbol fitted
533
+ into the rect, centred and keeping its proportions, in the **current fill colour**. A symbol
534
+ is a template, its shape in whatever colour it is drawn with, so it is drawn the way
535
+ `ctxDrawGlyphs` draws a run: through the surface's CTM and clip, at its global alpha and
536
+ blend mode, over what is already there. Answers `false`, drawing nothing, for a name the
537
+ catalogue does not know — a name from another platform's icon theme simply misses.
538
+ - **`native.symbolSize(name, options?)`** — `{ width, height }` in points, the padding the
539
+ symbol is designed with included, or `null` for an unknown name: the box a symbol sits in
540
+ beside text of the same point size.
541
+
542
+ | option | |
543
+ | --------------- | ----------------------------------------------------------------------------- |
544
+ | `pointSize` | the size it is designed for, default 13 — the size of the text beside it |
545
+ | `weight` | 100-900, default 400: the nearest of AppKit's weights, as `matchFont` maps it |
546
+ | `scale` | `'small'`, `'medium'` (default) or `'large'`, relative to the point size |
547
+ | `variableValue` | 0-1, how much of a variable symbol shows — macOS 13 and later, ignored before |
548
+
549
+ Both run on the calling thread in threaded mode too, and draw the same pixels there.
550
+
551
+ ```js
552
+ const s = native.createSurface(96, 96, 2);
553
+ native.ctxSetFillColor(s, 0.2, 0.4, 0.8, 1);
554
+ const { width, height } = native.symbolSize('speaker.wave.3.fill', { pointSize: 20 });
555
+ native.ctxDrawSymbol(s, 'speaker.wave.3.fill', 8, 8, width * 2, height * 2, {
556
+ pointSize: 20,
557
+ weight: 600,
558
+ variableValue: 0.66,
559
+ });
560
+ ```
561
+
527
562
  ## App lifecycle: open-URL, open-file, reopen, quit
528
563
 
529
564
  The OS talks to the application as a whole through Apple Events: a URL for a scheme
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@windowkit/appkit",
3
- "version": "0.11.0",
3
+ "version": "0.12.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/accessibility-display.js && node test/ignores-mouse-events.js && node test/surface-blit.js && node test/text-features.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/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
@@ -4196,19 +4196,24 @@ static Napi::Value ScrollSurface(const Napi::CallbackInfo& info) {
4196
4196
  // fonts + text layout (CoreText)
4197
4197
  // ---------------------------------------------------------------------------
4198
4198
 
4199
+ // A CSS weight, 100-900, as the nearest of AppKit's named weights — what a
4200
+ // face and an SF Symbol are both configured with.
4201
+ static NSFontWeight FontWeightFromCss(double weight) {
4202
+ if (weight <= 150) return NSFontWeightUltraLight;
4203
+ if (weight <= 250) return NSFontWeightThin;
4204
+ if (weight <= 350) return NSFontWeightLight;
4205
+ if (weight <= 450) return NSFontWeightRegular;
4206
+ if (weight <= 550) return NSFontWeightMedium;
4207
+ if (weight <= 650) return NSFontWeightSemibold;
4208
+ if (weight <= 750) return NSFontWeightBold;
4209
+ if (weight <= 850) return NSFontWeightHeavy;
4210
+ return NSFontWeightBlack;
4211
+ }
4212
+
4199
4213
  // matchFont({ families: [..], size, weight (100-900), italic }) -> font handle
4200
4214
  static NSFont* ResolveFamily(NSString* family, double size, double weight,
4201
4215
  bool italic) {
4202
- NSFontWeight w = NSFontWeightRegular;
4203
- if (weight <= 150) w = NSFontWeightUltraLight;
4204
- else if (weight <= 250) w = NSFontWeightThin;
4205
- else if (weight <= 350) w = NSFontWeightLight;
4206
- else if (weight <= 450) w = NSFontWeightRegular;
4207
- else if (weight <= 550) w = NSFontWeightMedium;
4208
- else if (weight <= 650) w = NSFontWeightSemibold;
4209
- else if (weight <= 750) w = NSFontWeightBold;
4210
- else if (weight <= 850) w = NSFontWeightHeavy;
4211
- else w = NSFontWeightBlack;
4216
+ NSFontWeight w = FontWeightFromCss(weight);
4212
4217
 
4213
4218
  NSFont* font = nil;
4214
4219
  NSString* lower = family.lowercaseString;
@@ -5229,6 +5234,121 @@ static Napi::Value CtxDrawGlyphs(const Napi::CallbackInfo& info) {
5229
5234
  return info.Env().Undefined();
5230
5235
  }
5231
5236
 
5237
+ // ---------------------------------------------------------------------------
5238
+ // SF Symbols, drawn in a surface (sidorares/react-x11#591)
5239
+ // ---------------------------------------------------------------------------
5240
+ //
5241
+ // The system's icons by name — what a status item and a menu item already
5242
+ // take — for a renderer drawing its own content. A symbol is a template: its
5243
+ // shape, in whatever colour it is drawn with, so it is drawn here the way a
5244
+ // glyph run is, in the current fill colour, and tints the way text does.
5245
+
5246
+ // The symbol's image for (name, options), or nil for a name the catalogue
5247
+ // does not know. Options: pointSize (default 13), weight (100-900, default
5248
+ // 400), scale ('small' | 'medium' | 'large', default 'medium'),
5249
+ // variableValue (0-1, macOS 13 and later; ignored before).
5250
+ static NSImage* SymbolImage(Napi::Value nameValue, Napi::Value optionsValue) {
5251
+ if (!nameValue.IsString()) {
5252
+ Napi::TypeError::New(nameValue.Env(), "expected a symbol name")
5253
+ .ThrowAsJavaScriptException();
5254
+ return nil;
5255
+ }
5256
+ NSString* name = BToNSString(nameValue);
5257
+ double pointSize = 13, weight = 400, variable = NAN;
5258
+ NSImageSymbolScale scale = NSImageSymbolScaleMedium;
5259
+ if (optionsValue.IsObject()) {
5260
+ Napi::Object o = optionsValue.As<Napi::Object>();
5261
+ pointSize = BNumOr(o, "pointSize", 13);
5262
+ weight = BNumOr(o, "weight", 400);
5263
+ NSString* s = BStrOr(o, "scale", @"medium");
5264
+ if ([s isEqualToString:@"small"]) scale = NSImageSymbolScaleSmall;
5265
+ else if ([s isEqualToString:@"large"]) scale = NSImageSymbolScaleLarge;
5266
+ if (o.Has("variableValue") && o.Get("variableValue").IsNumber())
5267
+ variable = o.Get("variableValue").As<Napi::Number>().DoubleValue();
5268
+ }
5269
+ NSImage* img = nil;
5270
+ if (!std::isnan(variable)) {
5271
+ if (@available(macOS 13.0, *)) {
5272
+ img = [NSImage imageWithSystemSymbolName:name
5273
+ variableValue:variable
5274
+ accessibilityDescription:nil];
5275
+ }
5276
+ }
5277
+ if (!img) {
5278
+ img = [NSImage imageWithSystemSymbolName:name accessibilityDescription:nil];
5279
+ }
5280
+ if (!img) return nil;
5281
+ NSImageSymbolConfiguration* cfg = [NSImageSymbolConfiguration
5282
+ configurationWithPointSize:pointSize
5283
+ weight:FontWeightFromCss(weight)
5284
+ scale:scale];
5285
+ return [img imageWithSymbolConfiguration:cfg];
5286
+ }
5287
+
5288
+ // symbolSize(name, options?) -> { width, height } in points, or null for a
5289
+ // name the catalogue does not know. The image's size, the padding the symbol
5290
+ // is designed with included: the box a symbol sits in beside text.
5291
+ static Napi::Value SymbolSize(const Napi::CallbackInfo& info) {
5292
+ Napi::Env env = info.Env();
5293
+ @autoreleasepool {
5294
+ NSImage* img = SymbolImage(info[0], info.Length() > 1 ? info[1] : env.Undefined());
5295
+ if (env.IsExceptionPending()) return env.Undefined();
5296
+ if (!img) return env.Null();
5297
+ Napi::Object out = Napi::Object::New(env);
5298
+ out.Set("width", img.size.width);
5299
+ out.Set("height", img.size.height);
5300
+ return out;
5301
+ }
5302
+ }
5303
+
5304
+ // ctxDrawSymbol(surface, name, x, y, width, height, options?) -> boolean
5305
+ // The symbol fitted into the rect, centred, keeping its proportions, in the
5306
+ // current fill colour — so it follows the surface CTM, clip, global alpha and
5307
+ // blend mode like every other ctx verb. Answers false, drawing nothing, for a
5308
+ // name the catalogue does not know. Options as symbolSize's.
5309
+ static Napi::Value CtxDrawSymbol(const Napi::CallbackInfo& info) {
5310
+ Napi::Env env = info.Env();
5311
+ CALSurface* s = SurfaceFrom(info[0]);
5312
+ if (!s) return env.Undefined();
5313
+ @autoreleasepool {
5314
+ NSImage* img = SymbolImage(info[1], info.Length() > 6 ? info[6] : env.Undefined());
5315
+ if (env.IsExceptionPending()) return env.Undefined();
5316
+ if (!img) return Napi::Boolean::New(env, false);
5317
+ double x = info[2].As<Napi::Number>().DoubleValue();
5318
+ double y = info[3].As<Napi::Number>().DoubleValue();
5319
+ double w = info[4].As<Napi::Number>().DoubleValue();
5320
+ double h = info[5].As<Napi::Number>().DoubleValue();
5321
+ NSSize size = img.size;
5322
+ if (!(w > 0 && h > 0 && size.width > 0 && size.height > 0))
5323
+ return Napi::Boolean::New(env, true);
5324
+ double k = std::min(w / size.width, h / size.height);
5325
+ CGRect rect = CGRectMake(x + (w - size.width * k) / 2, y + (h - size.height * k) / 2,
5326
+ size.width * k, size.height * k);
5327
+ CGContextRef ctx = s->ctx;
5328
+ CGContextSaveGState(ctx);
5329
+ // The template's shape into a layer of its own, then the fill colour
5330
+ // through it: the layer composites once with the context's alpha, blend
5331
+ // mode and clip, as a glyph run's ink does.
5332
+ CGContextBeginTransparencyLayerWithRect(ctx, rect, NULL);
5333
+ // the surface's base CTM is y-down, so the context is a flipped one
5334
+ NSGraphicsContext* g = [NSGraphicsContext graphicsContextWithCGContext:ctx flipped:YES];
5335
+ [NSGraphicsContext saveGraphicsState];
5336
+ [NSGraphicsContext setCurrentContext:g];
5337
+ [img drawInRect:NSRectFromCGRect(rect)
5338
+ fromRect:NSZeroRect
5339
+ operation:NSCompositingOperationSourceOver
5340
+ fraction:1.0
5341
+ respectFlipped:YES
5342
+ hints:nil];
5343
+ [NSGraphicsContext restoreGraphicsState];
5344
+ CGContextSetBlendMode(ctx, kCGBlendModeSourceIn);
5345
+ CGContextFillRect(ctx, rect);
5346
+ CGContextEndTransparencyLayer(ctx);
5347
+ CGContextRestoreGState(ctx);
5348
+ return Napi::Boolean::New(env, true);
5349
+ }
5350
+ }
5351
+
5232
5352
  // drawLayoutGradient(surface, layoutHandle, x, y, x0, y0, x1, y1,
5233
5353
  // stops [offset,r,g,b,a,...])
5234
5354
  // The glyph outlines become the clip and a linear gradient fills through
@@ -6518,6 +6638,8 @@ void InitBackend(Napi::Env env, Napi::Object exports) {
6518
6638
  BFN("createLayout", CreateLayout);
6519
6639
  BFN("drawLayout", DrawLayout);
6520
6640
  BFN("ctxDrawGlyphs", CtxDrawGlyphs);
6641
+ BFN("ctxDrawSymbol", CtxDrawSymbol);
6642
+ BFN("symbolSize", SymbolSize);
6521
6643
  BFN("layoutIndexAt", LayoutIndexAt);
6522
6644
  BFN("layoutCaret", LayoutCaret);
6523
6645
  BFN("pasteboardWriteText", PbWriteTextFn);