@silkui/core 0.1.4 → 0.1.6
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 +19 -4
- package/package.json +1 -1
- package/src/index.d.ts +19 -18
- package/src/index.js +46 -87
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
The node and event API of a [Silk UI](https://github.com/silk-lang/SilkUI) tree, for
|
|
4
4
|
JavaScript apps that run on the SilkJS engine embedded in a Silk UI program. Its nodes are
|
|
5
|
-
the tree's own (box, text, input, rich input, image,
|
|
5
|
+
the tree's own (box, text, input, rich input, image, canvas) and inline svg. Its methods and its
|
|
6
6
|
event flow are the DOM's: `appendChild`, `addEventListener`, capture and bubble,
|
|
7
7
|
`preventDefault`.
|
|
8
8
|
|
|
@@ -62,13 +62,12 @@ mount(createBox({ position: "absolute", inset: 0, display: "flex", alignItems: "
|
|
|
62
62
|
| `createInput(style?)` | An editable text field. `value`, `selectionStart`, `selectionEnd` and `setSelectionRange` work as on an `<input>` |
|
|
63
63
|
| `createRichInput(style?)` | A text field with marks and inline atoms (see below) |
|
|
64
64
|
| `createImage(src, style?)` | An image, as an `<img>`: `src` is a `data:` URL, a file or an `http(s):` address. An SVG file draws as an inline `<svg>` |
|
|
65
|
-
| `createIcon(svg, style?)` | SVG markup drawn at the node's size, with its `color` as `currentColor`. `icon.src` takes an SVG address instead |
|
|
66
65
|
| `createSvgElement(tag)` | `document.createElementNS` for SVG: an `Svg` root for `"svg"`, else an `SvgElement` (`path`, `g`, ...) to append inside one |
|
|
67
|
-
| `createNode(kind)` | A node of any kind: `"box"`, `"text"`, `"input"`, `"richinput"`, `"image"` (or `"img"`), `"
|
|
66
|
+
| `createNode(kind)` | A node of any kind: `"box"`, `"text"`, `"input"`, `"richinput"`, `"image"` (or `"img"`), `"canvas"`, `"svg"` |
|
|
68
67
|
| `mount(node)` / `root()` | Makes a node the root of the window's tree, or returns it |
|
|
69
68
|
| `nodeById(id)` | The node with a tree id, for hosts and tools |
|
|
70
69
|
|
|
71
|
-
An `Svg` is one
|
|
70
|
+
An `Svg` is one node whose markup its `SvgElement` and text children make, as an inline
|
|
72
71
|
`<svg>` is on a page. Class, look, events and ARIA attributes are the node's; `viewBox`,
|
|
73
72
|
`fill`, `width` and every other attribute are markup, and React's spellings (`strokeWidth`)
|
|
74
73
|
work too. Changes are drawn once, before the host applies the batch (`flushSvg()` draws them
|
|
@@ -158,6 +157,22 @@ registerShortcut("Mod+Shift+B", build); // Mod is Command on macOS, Control else
|
|
|
158
157
|
whichever app has focus; another app may hold it, and Wayland has none. `removeTray()` and
|
|
159
158
|
`unregisterShortcut(accelerator)` undo them.
|
|
160
159
|
|
|
160
|
+
`memoryUsage()` is the app's memory as the system shows it to users, to put next to an
|
|
161
|
+
Electron app's in Activity Monitor or Task Manager. It is synchronous and allocates almost
|
|
162
|
+
nothing, so it can run every second.
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
import { memoryUsage } from "@silkui/core";
|
|
166
|
+
|
|
167
|
+
const { footprint, processes } = memoryUsage(); // bytes, and how many processes they cover
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
`footprint` is `phys_footprint` on macOS (Activity Monitor's "Memory", `/usr/bin/footprint`),
|
|
171
|
+
`Pss` from `/proc/self/smaps_rollup` on Linux and `PrivateUsage` on Windows. `processes` is 1:
|
|
172
|
+
the host runs the app, its tree and its JavaScript in one process. In a web-canvas build it
|
|
173
|
+
is the wasm tree's memory only, without the page's JavaScript heap. It is `null` when the
|
|
174
|
+
host cannot say.
|
|
175
|
+
|
|
161
176
|
### Attributes and accessibility
|
|
162
177
|
|
|
163
178
|
`setAttribute(name, value)` sets the tree's attributes: `tabIndex`, `disabled`, `label`,
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export function updateStyleSheet(id: string, compiled: Uint8Array | ArrayBuffer
|
|
|
11
11
|
import type * as CSS from "csstype";
|
|
12
12
|
|
|
13
13
|
/** The node kinds of the tree. `richinput` is an `input` with marks and atoms. */
|
|
14
|
-
export type NodeKind = "box" | "text" | "input" | "richinput" | "image" | "
|
|
14
|
+
export type NodeKind = "box" | "text" | "input" | "richinput" | "image" | "canvas" | "svg";
|
|
15
15
|
|
|
16
16
|
/** The looks a state lays over the base look: `:hover`, `:active`, `:focus`, `:focus-visible`, `@starting-style`. */
|
|
17
17
|
export type LookState = "base" | "hover" | "active" | "focus" | "focusVisible" | "starting";
|
|
@@ -182,17 +182,6 @@ export class Image extends Node {
|
|
|
182
182
|
src: string;
|
|
183
183
|
}
|
|
184
184
|
|
|
185
|
-
/**
|
|
186
|
-
* An icon: SVG drawn at the node's CSS size, with its `color` as `currentColor`. Without a
|
|
187
|
-
* CSS size it takes the `<svg>`'s `width` and `height`, else a square of the font size.
|
|
188
|
-
* `svg` is markup and `src` the address of an SVG file; setting one clears the other.
|
|
189
|
-
*/
|
|
190
|
-
export class Icon extends Node {
|
|
191
|
-
constructor(svg?: string);
|
|
192
|
-
svg: string;
|
|
193
|
-
src: string;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
185
|
/** Text inside an `<svg>`. */
|
|
197
186
|
export class SvgText {
|
|
198
187
|
constructor(data?: string);
|
|
@@ -226,10 +215,12 @@ export class SvgElement {
|
|
|
226
215
|
}
|
|
227
216
|
|
|
228
217
|
/**
|
|
229
|
-
* An inline `<svg>`:
|
|
230
|
-
*
|
|
218
|
+
* An inline `<svg>`: one node drawing the markup its `SvgElement` children make, at its CSS
|
|
219
|
+
* size (else its `width` and `height`, else a square of the font size), with its `color` as
|
|
220
|
+
* `currentColor`. Class, look, events and ARIA are the node's; `viewBox`, `fill`, `width`
|
|
221
|
+
* and the rest are markup.
|
|
231
222
|
*/
|
|
232
|
-
export class Svg extends
|
|
223
|
+
export class Svg extends Node {
|
|
233
224
|
constructor();
|
|
234
225
|
readonly outerHTML: string;
|
|
235
226
|
textContent: string;
|
|
@@ -363,7 +354,6 @@ export interface EventMap {
|
|
|
363
354
|
export function createNode(kind: "text"): Text;
|
|
364
355
|
export function createNode(kind: "input"): Input;
|
|
365
356
|
export function createNode(kind: "richinput"): RichInput;
|
|
366
|
-
export function createNode(kind: "icon"): Icon;
|
|
367
357
|
export function createNode(kind: "image" | "img"): Image;
|
|
368
358
|
export function createNode(kind: "svg"): Svg;
|
|
369
359
|
export function createNode(kind: NodeKind): Node;
|
|
@@ -373,8 +363,6 @@ export function createBox(style?: Style | null, ...children: (Node | string)[]):
|
|
|
373
363
|
export function createText(data?: string): Text;
|
|
374
364
|
export function createInput(style?: Style | null): Input;
|
|
375
365
|
export function createRichInput(style?: Style | null): RichInput;
|
|
376
|
-
/** An icon showing `svg` markup, with `style` as its base look. */
|
|
377
|
-
export function createIcon(svg: string, style?: Style | null): Icon;
|
|
378
366
|
/** An image showing `src`, with `style` as its base look. */
|
|
379
367
|
export function createImage(src: string, style?: Style | null): Image;
|
|
380
368
|
/** `document.createElementNS` in the SVG namespace: an `Svg` for `svg`, else an `SvgElement`. */
|
|
@@ -500,3 +488,16 @@ export function removeTray(): void;
|
|
|
500
488
|
*/
|
|
501
489
|
export function registerShortcut(accelerator: string, callback: () => void): Promise<boolean>;
|
|
502
490
|
export function unregisterShortcut(accelerator: string): void;
|
|
491
|
+
|
|
492
|
+
/** The app's memory: `footprint` in bytes over `processes` processes. */
|
|
493
|
+
export interface MemoryUsage {
|
|
494
|
+
footprint: number;
|
|
495
|
+
processes: number;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* The app's memory as the system shows it to users (macOS `phys_footprint`, Linux `Pss`,
|
|
500
|
+
* Windows `PrivateUsage`). In the browser, the wasm tree's linear memory only. Cheap enough
|
|
501
|
+
* to call every second; `null` when the host cannot say.
|
|
502
|
+
*/
|
|
503
|
+
export function memoryUsage(): MemoryUsage | null;
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @silkui/core: the nodes of a Silk UI tree and their events, for JavaScript that runs on the
|
|
2
|
-
// embedded SilkJS engine. The primitives are the tree's own (box, text, input, image,
|
|
3
|
-
//
|
|
2
|
+
// embedded SilkJS engine. The primitives are the tree's own (box, text, input, image, canvas)
|
|
3
|
+
// and inline svg; the methods and the event flow are the DOM's, so code written for the web reads
|
|
4
4
|
// the same here.
|
|
5
5
|
//
|
|
6
6
|
// Every call queues a mutation through the native `__ui` functions. The host applies the
|
|
@@ -524,9 +524,9 @@ export class Node {
|
|
|
524
524
|
} else if (aria !== null) {
|
|
525
525
|
native.attribute(this.nodeId, name, text);
|
|
526
526
|
} else if (name === "src") {
|
|
527
|
-
// An image's
|
|
527
|
+
// An image's address, as `<img src>`.
|
|
528
528
|
if ("src" in this) this.src = text ?? "";
|
|
529
|
-
else warnOnce("only an image
|
|
529
|
+
else warnOnce("only an image has a src: it is ignored");
|
|
530
530
|
} else if (name === "alt") {
|
|
531
531
|
native.attribute(this.nodeId, "label", text ?? "");
|
|
532
532
|
} else {
|
|
@@ -684,8 +684,8 @@ export class Input extends Node {
|
|
|
684
684
|
}
|
|
685
685
|
}
|
|
686
686
|
|
|
687
|
-
// Inline SVG markup as icon resources: identical markup shares one, freed when no
|
|
688
|
-
// it. The ids start high, clear of resources a host or a URL table numbers from 1.
|
|
687
|
+
// Inline SVG markup as icon resources: identical markup shares one, freed when no `<svg>`
|
|
688
|
+
// shows it. The ids start high, clear of resources a host or a URL table numbers from 1.
|
|
689
689
|
const ICONS = new Map();
|
|
690
690
|
let nextIcon = 0x40000000;
|
|
691
691
|
|
|
@@ -708,28 +708,26 @@ function dropIcon(markup) {
|
|
|
708
708
|
}
|
|
709
709
|
}
|
|
710
710
|
|
|
711
|
-
// Addresses as image
|
|
712
|
-
//
|
|
711
|
+
// Addresses as image resources, shared and freed as markup is. The tree keeps a load state per
|
|
712
|
+
// number, so these count from 1. The loader tells an SVG file from a bitmap by itself.
|
|
713
713
|
const ADDRESSES = new Map();
|
|
714
714
|
let nextAddress = 1;
|
|
715
715
|
|
|
716
|
-
function holdAddress(url
|
|
717
|
-
|
|
718
|
-
let entry = ADDRESSES.get(key);
|
|
716
|
+
function holdAddress(url) {
|
|
717
|
+
let entry = ADDRESSES.get(url);
|
|
719
718
|
if (!entry) {
|
|
720
719
|
entry = { id: nextAddress++, count: 0 };
|
|
721
|
-
ADDRESSES.set(
|
|
722
|
-
native.load(entry.id, url,
|
|
720
|
+
ADDRESSES.set(url, entry);
|
|
721
|
+
native.load(entry.id, url, false);
|
|
723
722
|
}
|
|
724
723
|
entry.count++;
|
|
725
724
|
return entry.id;
|
|
726
725
|
}
|
|
727
726
|
|
|
728
|
-
function dropAddress(url
|
|
729
|
-
const
|
|
730
|
-
const entry = ADDRESSES.get(key);
|
|
727
|
+
function dropAddress(url) {
|
|
728
|
+
const entry = ADDRESSES.get(url);
|
|
731
729
|
if (entry && --entry.count === 0) {
|
|
732
|
-
ADDRESSES.delete(
|
|
730
|
+
ADDRESSES.delete(url);
|
|
733
731
|
native.release(entry.id);
|
|
734
732
|
}
|
|
735
733
|
}
|
|
@@ -754,72 +752,17 @@ export class Image extends Node {
|
|
|
754
752
|
set src(url) {
|
|
755
753
|
url = url == null ? "" : String(url);
|
|
756
754
|
if (url === this.#src) return;
|
|
757
|
-
if (this.#src) dropAddress(this.#src
|
|
755
|
+
if (this.#src) dropAddress(this.#src);
|
|
758
756
|
this.#src = url;
|
|
759
|
-
native.source(this.nodeId, url ? holdAddress(url
|
|
757
|
+
native.source(this.nodeId, url ? holdAddress(url) : 0);
|
|
760
758
|
}
|
|
761
759
|
|
|
762
760
|
_forget() {
|
|
763
|
-
if (this.#src) dropAddress(this.#src
|
|
761
|
+
if (this.#src) dropAddress(this.#src);
|
|
764
762
|
this.#src = "";
|
|
765
763
|
}
|
|
766
764
|
}
|
|
767
765
|
|
|
768
|
-
/**
|
|
769
|
-
* An icon: SVG drawn at the node's CSS size, as an inline `<svg>` is. Its `currentColor` is
|
|
770
|
-
* the node's `color`, so an icon that paints only with it (Lucide's, for example) takes the
|
|
771
|
-
* text color, and any other keeps its own colors. Without a CSS size it takes the `width`
|
|
772
|
-
* and `height` of its `<svg>`, else a square of the font size.
|
|
773
|
-
*
|
|
774
|
-
* `svg` is its markup; `src` is the address of an SVG file instead (a `data:image/svg+xml`
|
|
775
|
-
* URL too). Setting one clears the other.
|
|
776
|
-
*/
|
|
777
|
-
export class Icon extends Node {
|
|
778
|
-
#svg = "";
|
|
779
|
-
#src = "";
|
|
780
|
-
|
|
781
|
-
constructor(svg = "", kind = "icon") {
|
|
782
|
-
super(kind);
|
|
783
|
-
this.svg = svg;
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
/** The SVG markup, as `outerHTML` of an `<svg>` element would give it. */
|
|
787
|
-
get svg() {
|
|
788
|
-
return this.#svg;
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
set svg(markup) {
|
|
792
|
-
markup = markup == null ? "" : String(markup);
|
|
793
|
-
if (markup === this.#svg) return;
|
|
794
|
-
this.#release();
|
|
795
|
-
this.#svg = markup;
|
|
796
|
-
native.source(this.nodeId, markup ? holdIcon(markup) : 0);
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
get src() {
|
|
800
|
-
return this.#src;
|
|
801
|
-
}
|
|
802
|
-
|
|
803
|
-
set src(url) {
|
|
804
|
-
url = url == null ? "" : String(url);
|
|
805
|
-
if (url === this.#src) return;
|
|
806
|
-
this.#release();
|
|
807
|
-
this.#src = url;
|
|
808
|
-
native.source(this.nodeId, url ? holdAddress(url, true) : 0);
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
#release() {
|
|
812
|
-
if (this.#svg) dropIcon(this.#svg);
|
|
813
|
-
if (this.#src) dropAddress(this.#src, true);
|
|
814
|
-
this.#svg = "";
|
|
815
|
-
this.#src = "";
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
_forget() {
|
|
819
|
-
this.#release();
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
|
|
823
766
|
const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
|
|
824
767
|
|
|
825
768
|
// SVG attribute names that are camel case in SVG itself. Any other name with a capital letter
|
|
@@ -1092,11 +1035,14 @@ function treeAttribute(name) {
|
|
|
1092
1035
|
* are the node's; `viewBox`, `fill`, `width` and the other SVG attributes are markup.
|
|
1093
1036
|
* Changes are drawn once, before the host applies the batch.
|
|
1094
1037
|
*/
|
|
1095
|
-
export class Svg extends
|
|
1038
|
+
export class Svg extends Node {
|
|
1096
1039
|
#attributes = new Map();
|
|
1040
|
+
#markup = "";
|
|
1097
1041
|
|
|
1098
1042
|
constructor() {
|
|
1099
|
-
|
|
1043
|
+
// The tree's node for drawn SVG markup is an icon; to code it is an `svg`.
|
|
1044
|
+
super("icon");
|
|
1045
|
+
this.kind = "svg";
|
|
1100
1046
|
this.localName = "svg";
|
|
1101
1047
|
this.tagName = "svg";
|
|
1102
1048
|
this.namespaceURI = SVG_NAMESPACE;
|
|
@@ -1144,7 +1090,17 @@ export class Svg extends Icon {
|
|
|
1144
1090
|
}
|
|
1145
1091
|
|
|
1146
1092
|
_draw() {
|
|
1147
|
-
if (nodes.has(this.nodeId))
|
|
1093
|
+
if (!nodes.has(this.nodeId)) return;
|
|
1094
|
+
const markup = this.outerHTML;
|
|
1095
|
+
if (markup === this.#markup) return;
|
|
1096
|
+
this._forget();
|
|
1097
|
+
this.#markup = markup;
|
|
1098
|
+
native.source(this.nodeId, holdIcon(markup));
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
_forget() {
|
|
1102
|
+
if (this.#markup) dropIcon(this.#markup);
|
|
1103
|
+
this.#markup = "";
|
|
1148
1104
|
}
|
|
1149
1105
|
}
|
|
1150
1106
|
|
|
@@ -1364,10 +1320,9 @@ export function createNode(kind) {
|
|
|
1364
1320
|
if (kind === "richinput") return new RichInput();
|
|
1365
1321
|
if (kind === "svg") return new Svg();
|
|
1366
1322
|
if (kind === "img") kind = "image";
|
|
1367
|
-
if (!(kind in KINDS)) throw new TypeError("the tree has no node kind '" + kind + "'");
|
|
1323
|
+
if (!(kind in KINDS) || kind === "icon") throw new TypeError("the tree has no node kind '" + kind + "'");
|
|
1368
1324
|
if (kind === "text") return new Text();
|
|
1369
1325
|
if (kind === "input") return new Input();
|
|
1370
|
-
if (kind === "icon") return new Icon();
|
|
1371
1326
|
if (kind === "image") return new Image();
|
|
1372
1327
|
return new Node(kind);
|
|
1373
1328
|
}
|
|
@@ -1384,13 +1339,6 @@ export function createText(data = "") {
|
|
|
1384
1339
|
return new Text(data);
|
|
1385
1340
|
}
|
|
1386
1341
|
|
|
1387
|
-
/** An icon showing `svg` markup, with `style` as its base look. */
|
|
1388
|
-
export function createIcon(svg, style) {
|
|
1389
|
-
const icon = new Icon(svg);
|
|
1390
|
-
if (style) icon.style.set(style);
|
|
1391
|
-
return icon;
|
|
1392
|
-
}
|
|
1393
|
-
|
|
1394
1342
|
/** An image showing `src` (see `Image`), with `style` as its base look. */
|
|
1395
1343
|
export function createImage(src, style) {
|
|
1396
1344
|
const image = new Image(src);
|
|
@@ -1949,3 +1897,14 @@ export function unregisterShortcut(accelerator) {
|
|
|
1949
1897
|
shortcuts.delete(id);
|
|
1950
1898
|
shellCall(4, [id]);
|
|
1951
1899
|
}
|
|
1900
|
+
|
|
1901
|
+
/**
|
|
1902
|
+
* The app's memory in bytes, as the system shows it to users: `phys_footprint` on macOS
|
|
1903
|
+
* (Activity Monitor's "Memory"), `Pss` on Linux and `PrivateUsage` on Windows. `processes` is
|
|
1904
|
+
* how many processes `footprint` counts: the host is one. In the browser it is the wasm
|
|
1905
|
+
* tree's linear memory, without the page's JavaScript heap. `null` when there is no answer.
|
|
1906
|
+
*/
|
|
1907
|
+
export function memoryUsage() {
|
|
1908
|
+
const footprint = native.memoryUsage?.() ?? 0;
|
|
1909
|
+
return footprint > 0 ? { footprint, processes: 1 } : null;
|
|
1910
|
+
}
|