@windowkit/appkit 0.19.0 → 0.21.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 +129 -3
- package/binding.gyp +4 -1
- package/package.json +2 -2
- package/prebuilds/darwin-arm64/calayers.node +0 -0
- package/prebuilds/darwin-x64/calayers.node +0 -0
- package/src/addon.mm +4 -0
- package/src/backend.mm +511 -22
- package/src/video.mm +709 -0
package/README.md
CHANGED
|
@@ -125,15 +125,16 @@ 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*` (`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`; 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` |
|
|
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` |
|
|
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` |
|
|
133
133
|
| | `requestUserAttention`: a bridge id |
|
|
134
134
|
| | `openPanel` / `savePanel`: the answer through the callback, as before |
|
|
135
|
+
| | `popUpMenu`: the menu opens from a callout of its own, and answers through the callback |
|
|
135
136
|
| the published copy | `getWindowFrame`, `windowIsVisible`, `windowNumber` (all `null` until the window is made), `listScreens`, `accessibilityDisplayOptions`, `activationPolicy`, `appInfo`, `pasteboardChangeCount` |
|
|
136
|
-
| a callback, the last argument | `pasteboardReadText`, `snapshotWindow`, `snapshotStatusItem`, `windowNumberAtPoint`, `mainMenuInfo`, `dockMenuInfo`, `statusItemInfo`, `activateMenuItem`, `activateDockMenuItem`, `activateStatusItemMenuItem`, `clickStatusItem`, `dragItems`, `dragItemData`, `dragItemString`, `postDragEvent`. On the main thread each still answers synchronously when no callback is given. |
|
|
137
|
+
| a callback, the last argument | `pasteboardReadText`, `snapshotWindow`, `snapshotStatusItem`, `windowNumberAtPoint`, `mainMenuInfo`, `dockMenuInfo`, `statusItemInfo`, `activateMenuItem`, `activateDockMenuItem`, `activateStatusItemMenuItem`, `popUpMenuInfo`, `activatePopUpMenuItem`, `clickStatusItem`, `dragItems`, `dragItemData`, `dragItemString`, `postDragEvent`. On the main thread each still answers synchronously when no callback is given. |
|
|
137
138
|
| events | `beginDrag`: `drag-session-began`, or `drag-session-ended` with nothing dropped. Its `provide` is a TypeError; give every value up front. |
|
|
138
139
|
|
|
139
140
|
A few verbs behave differently from a worker:
|
|
@@ -557,6 +558,87 @@ native.ctxSetGlobalAlpha(win, 0.6);
|
|
|
557
558
|
native.ctxDrawSurfaceFaded(win, card, 0, 0, 556, 300, 100, 100, 556, 300, 0.6);
|
|
558
559
|
```
|
|
559
560
|
|
|
561
|
+
## Video surfaces
|
|
562
|
+
|
|
563
|
+
A frame a decoder hands over as bytes — ffmpeg over a pipe, a WASM decoder, an addon —
|
|
564
|
+
goes on the screen the way VideoToolbox's own output does: as a YCbCr IOSurface a plain
|
|
565
|
+
layer shows, converted and scaled by the render server. The CPU's share is the copy in.
|
|
566
|
+
|
|
567
|
+
- **`native.createVideoSurface(widthPx, heightPx, options?)`** → `{ handle, iosurfaceId }`.
|
|
568
|
+
`format` is `'NV12'` (the default: two planes, `420v`, or `420f` with `range: 'full'`)
|
|
569
|
+
or `'BGRA'`. `colorSpace` — `'bt709'` (the default), `'bt601'` or `'bt2020'` (SDR) — and
|
|
570
|
+
`range` — `'video'` (the default) or `'full'` — say what an NV12 surface's numbers mean,
|
|
571
|
+
and go on it as the attachments CoreVideo gives a decoded frame: the YCbCr matrix, the
|
|
572
|
+
primaries and the transfer function, which Core Animation reads. Shown through
|
|
573
|
+
`setLayerContentsIOSurface(layer, iosurfaceId)`, which keeps a colour a surface names
|
|
574
|
+
(or derives from CoreVideo's tags: see below). A new surface is black.
|
|
575
|
+
- **`native.writeVideoSurface(target, format, planes, options?)`** — one frame, `planes`
|
|
576
|
+
an array of Buffers or typed arrays: `'NV12'` (Y, then CbCr pairs), `'I420'` (Y, Cb, Cr)
|
|
577
|
+
or `'BGRA'`, each plane `options.strides[i]` bytes a row (packed by default) and checked
|
|
578
|
+
to hold every row; a 4:2:0 chroma plane is `ceil(width / 2)` by `ceil(height / 2)`. An
|
|
579
|
+
NV12 surface takes NV12, and I420 with its chroma interleaved on the way in; a BGRA
|
|
580
|
+
surface takes BGRA, and YCbCr converted. `target` may also be a **2D surface**
|
|
581
|
+
(`createSurface`, `createSurfaceIOSurface`), for a renderer that draws the frame in its
|
|
582
|
+
own paint order — something is drawn over the video — rather than lifting it onto a
|
|
583
|
+
layer: the frame lands at the surface's top-left, `options.width`/`height` of it (the
|
|
584
|
+
surface's size by default), converted with `options.colorSpace` and `range`. A frame's
|
|
585
|
+
fourth byte is ignored: a frame is opaque.
|
|
586
|
+
- **`native.videoSurfaceIsInUse(handle)`** — whether anything still reads the surface; a
|
|
587
|
+
renderer keeping a ring writes only into one this answers `false` for, and in threaded
|
|
588
|
+
mode `surface-released { id }` names the surface a frame took off a layer.
|
|
589
|
+
- **`native.releaseVideoSurface(handle)`** — free it now; idempotent; a layer showing it
|
|
590
|
+
keeps it until it lets go. Every other verb on a released handle throws.
|
|
591
|
+
- **`native.videoFormats()`** → `{ surfaces: ['NV12', 'BGRA'], frames: ['NV12', 'I420',
|
|
592
|
+
'BGRA'] }`, for a renderer to ask rather than assume.
|
|
593
|
+
|
|
594
|
+
Two things the verbs exist to get right, both measured on screen (`test/video-surface.js`):
|
|
595
|
+
|
|
596
|
+
- **Three planes draw nothing.** A layer shows `420v`, `420f` and `2vuy` surfaces and shows
|
|
597
|
+
`y420`/`f420` as transparent, so an I420 frame is interleaved into NV12, in the copy
|
|
598
|
+
the write makes anyway — 0.27ms at 1080p on an M1 Pro, against 0.07ms for NV12 as it is.
|
|
599
|
+
- **The same colours on a layer, in a bitmap and under `AVPlayerLayer`.** An NV12 surface
|
|
600
|
+
is named the colour space CoreVideo makes of its tags, and `setLayerContentsIOSurface`
|
|
601
|
+
names one the same way for a tagged surface it is handed by id. Shown with the tags
|
|
602
|
+
alone, Core Animation linearises a frame tagged BT.709 throughout with the exact 709
|
|
603
|
+
curve, where `AVPlayerLayer` — the platform's player — VideoToolbox's pixel transfer
|
|
604
|
+
and Core Image all use Apple's 1.961 gamma: a video-range grey of Y′=50 was sRGB 55 on
|
|
605
|
+
such a layer and 44 everywhere else. Named, a frame on a layer, the same frame
|
|
606
|
+
converted into a bitmap by VideoToolbox (about 1ms at 1080p) and the player showing it
|
|
607
|
+
agree to within a level or two. What cannot match is gamut: a layer keeps a 2020
|
|
608
|
+
colour outside sRGB on a wide-gamut panel, and an sRGB bitmap clips it.
|
|
609
|
+
|
|
610
|
+
## Text: at a display's scale
|
|
611
|
+
|
|
612
|
+
A text verb's size is the caller's pixels, and a renderer that draws in device pixels hands
|
|
613
|
+
over device pixels: a 13px label on a 2x display is 26. CoreText reads some of a face's data
|
|
614
|
+
by point size, though — the optical size and tracking San Francisco is set at, the tracking
|
|
615
|
+
Apple Color Emoji's `trak` table adds below 29pt — so a font made at 26 is San Francisco as
|
|
616
|
+
it is set at 26pt, 8% narrower than AppKit sets the 13pt label, and a 19px emoji is 1em wide
|
|
617
|
+
where it is 23pt at 19pt. Faces with no such data (Helvetica, Menlo) measure the same either
|
|
618
|
+
way.
|
|
619
|
+
|
|
620
|
+
- **`scale`** — the display's backing scale, as a `matchFont` option and a third argument
|
|
621
|
+
to `cgFontWithSize(cg, size, scale)` and `fontByPostScriptName(name, size, scale)`. The
|
|
622
|
+
font is made at `size / scale` points under a matrix that scales it by `scale`, so CoreText
|
|
623
|
+
reads the face at its point size, and every verb answers in the caller's pixels as before:
|
|
624
|
+
metrics, advances, `fontShapeText`'s runs, a layout's lines, runs, carets and hit testing,
|
|
625
|
+
and `drawLayout`, `ctxDrawGlyphs`, `drawLayoutGradient` and `layoutCoverage` draw it there.
|
|
626
|
+
`fontMetrics(font).size` is the size asked for. A copy — `fontWithSize`, variations,
|
|
627
|
+
features, a `fontFallbackFor` face, a face a layout substitutes — keeps the scale. Absent
|
|
628
|
+
or 1, nothing changes.
|
|
629
|
+
|
|
630
|
+
Two answers are the scaled face's own rather than the point-size face's times the scale. An
|
|
631
|
+
emoji's advance: CoreText rounds a bitmap glyph's to a whole pixel of the font it is set in,
|
|
632
|
+
a whole point at 1x and half of one at 2x, so a 19px emoji is 45 pixels at 2x where the
|
|
633
|
+
point-size face's 23 points are 46. And the system face's `postScriptName` is
|
|
634
|
+
`.SFNS-Regular`, CoreText's name for it, where NSFont calls the unscaled one by its alias,
|
|
635
|
+
`.AppleSystemUIFont`.
|
|
636
|
+
|
|
637
|
+
```js
|
|
638
|
+
// 13px at 2x: San Francisco as AppKit sets 13pt, measured in device pixels
|
|
639
|
+
const label = native.matchFont({ families: ['system-ui'], size: 26, scale: 2 });
|
|
640
|
+
```
|
|
641
|
+
|
|
560
642
|
## Text: letter spacing and OpenType features
|
|
561
643
|
|
|
562
644
|
The text verbs a renderer lays paragraphs out and shapes glyph runs with —
|
|
@@ -1205,6 +1287,50 @@ the item's window (pump afterwards; declined for left/right while a menu is
|
|
|
1205
1287
|
set, since that click would open it), and `snapshotStatusItem(item, file)`
|
|
1206
1288
|
writes the composited item to a PNG.
|
|
1207
1289
|
|
|
1290
|
+
## Pop-up menu (a control's menu)
|
|
1291
|
+
|
|
1292
|
+
The menu that drops from a control in a window — a `<select>`'s list, a
|
|
1293
|
+
pop-up button's — the way `NSPopUpButton`'s does: the current item placed
|
|
1294
|
+
over the control and checked, the menu at least the control's width, in the
|
|
1295
|
+
font asked for. It is run through an `NSPopUpButtonCell` that is never drawn,
|
|
1296
|
+
`performClickWithFrame:inView:`, which is what a browser does for a
|
|
1297
|
+
`<select>` on macOS, so the placement, the tracking (type-select, the scroll
|
|
1298
|
+
arrows of a long menu, VoiceOver) and the look are AppKit's own. The items
|
|
1299
|
+
are `setMainMenu`'s vocabulary.
|
|
1300
|
+
|
|
1301
|
+
```js
|
|
1302
|
+
const menu = native.popUpMenu(win, {
|
|
1303
|
+
items: [ // setMainMenu's item vocabulary
|
|
1304
|
+
{ id: 1, title: 'Default' },
|
|
1305
|
+
{ id: 2, title: 'Retro' },
|
|
1306
|
+
{ separator: true },
|
|
1307
|
+
{ id: 3, title: 'Chaos', enabled: false },
|
|
1308
|
+
],
|
|
1309
|
+
frame: [40, 60, 220, 24], // the control, in the window's content, top-left, points
|
|
1310
|
+
selected: 2, // placed over the control, checked; absent: none is
|
|
1311
|
+
fontSize: 13, // the menu's font; the menu font at its own size by default
|
|
1312
|
+
fontFamily: 'Avenir Next', // a family the system has, else the menu font at fontSize
|
|
1313
|
+
appearance: 'dark', // 'light' | 'dark'; absent: the window's
|
|
1314
|
+
rtl: false,
|
|
1315
|
+
}, (id) => {
|
|
1316
|
+
// the id chosen, or null for a menu dismissed (Escape, a click outside, cancelPopUpMenu)
|
|
1317
|
+
});
|
|
1318
|
+
native.cancelPopUpMenu(menu); // ends the tracking: the callback answers null
|
|
1319
|
+
```
|
|
1320
|
+
|
|
1321
|
+
Tracking is a modal loop. In pump mode it runs inside the call, the thread
|
|
1322
|
+
being AppKit's for the length of the gesture, and the callback has run when
|
|
1323
|
+
the call returns. From a worker the handle is answered at the call, the menu
|
|
1324
|
+
opens from a callout of its own, the answer arrives through the callback, and
|
|
1325
|
+
`cancelPopUpMenu` can end it while it is open.
|
|
1326
|
+
|
|
1327
|
+
For tests: `popUpMenuInfo(menu)` returns an open pop-up as `mainMenuInfo`'s
|
|
1328
|
+
shape plus `fontFamily`, `fontSize`, `appearance` and `rtl`, and null once
|
|
1329
|
+
it has answered; `activatePopUpMenuItem(menu, index)` chooses an item the
|
|
1330
|
+
way tracking would and ends the tracking. In pump mode, keys posted with
|
|
1331
|
+
`postKeyEvent` before the call are read by the tracking as a person's would
|
|
1332
|
+
be (`test/popup-menu.js`).
|
|
1333
|
+
|
|
1208
1334
|
## Dock & app presence
|
|
1209
1335
|
|
|
1210
1336
|
The handful of things an app shows outside its own windows — the Dock tile and the name
|
package/binding.gyp
CHANGED
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
"src/notifications.mm",
|
|
10
10
|
"src/calendars.mm",
|
|
11
11
|
"src/screencolor.mm",
|
|
12
|
-
"src/threaded.mm"
|
|
12
|
+
"src/threaded.mm",
|
|
13
|
+
"src/video.mm"
|
|
13
14
|
],
|
|
14
15
|
"include_dirs": [
|
|
15
16
|
"<!@(node -p \"require('node-addon-api').include\")"
|
|
@@ -28,6 +29,8 @@
|
|
|
28
29
|
"-framework CoreGraphics",
|
|
29
30
|
"-framework ImageIO",
|
|
30
31
|
"-framework IOSurface",
|
|
32
|
+
"-framework CoreVideo",
|
|
33
|
+
"-framework VideoToolbox",
|
|
31
34
|
"-framework UniformTypeIdentifiers",
|
|
32
35
|
"-framework AVFoundation",
|
|
33
36
|
"-framework CoreLocation",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@windowkit/appkit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.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/accessibility-display.js && node test/ignores-mouse-events.js && node test/surface-blit.js && node test/surface-faded.js && node test/surface-colorspace.js && node test/round-rect-xy.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/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/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/surface-colorspace.js && node test/video-surface.js && node test/round-rect-xy.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-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
|
|
Binary file
|
package/src/addon.mm
CHANGED
|
@@ -1718,6 +1718,9 @@ void InitScreenColor(Napi::Env env, Napi::Object exports);
|
|
|
1718
1718
|
// src/threaded.mm — threaded mode: runMain parks the main thread in
|
|
1719
1719
|
// [NSApp run], commands in through the main run loop, events out in batches.
|
|
1720
1720
|
void InitThreaded(Napi::Env env, Napi::Object exports);
|
|
1721
|
+
// src/video.mm — video surfaces: a YCbCr or BGRA IOSurface a layer shows
|
|
1722
|
+
// as it is, frames written into it, and frames converted into a surface.
|
|
1723
|
+
void InitVideo(Napi::Env env, Napi::Object exports);
|
|
1721
1724
|
|
|
1722
1725
|
static Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
1723
1726
|
#define FN(js, fn) exports.Set(js, Napi::Function::New(env, fn))
|
|
@@ -1764,6 +1767,7 @@ static Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
|
1764
1767
|
InitCalendars(env, exports);
|
|
1765
1768
|
InitScreenColor(env, exports);
|
|
1766
1769
|
InitThreaded(env, exports);
|
|
1770
|
+
InitVideo(env, exports);
|
|
1767
1771
|
return exports;
|
|
1768
1772
|
}
|
|
1769
1773
|
|