@weasel-js/hud 1.4.2 → 1.4.4

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/dist/attach.d.ts CHANGED
@@ -1,10 +1,24 @@
1
1
  import type { Hud } from './hud';
2
2
  import type { CanvasExtensionApi } from '@weasel-js/core';
3
+ import { type FontAtlasUrls } from './fonts/registerDefaultFont';
3
4
  import { type ResolvedTheme } from '@weasel-js/theme';
4
5
  export interface AttachHudOptions {
5
6
  /** Resolved theme the widgets draw with. Defaults to the built-in theme's
6
7
  * default mode; pass `useTheme().resolved` to follow a live theme. */
7
8
  readonly theme?: ResolvedTheme;
9
+ /**
10
+ * Where the text a widget draws without naming a family comes from.
11
+ *
12
+ * - A family name: that family, already registered by the host. The HUD
13
+ * fetches nothing, which is the point — an app that has registered Inter
14
+ * as `'sans-serif'` was downloading the HUD's byte-identical copy of it a
15
+ * second time.
16
+ * - A `{ metricsUrl, atlasUrl }` pair: the HUD's own family, registered from
17
+ * the host's copy of the atlas rather than the bundled one.
18
+ *
19
+ * Unset, the bundled atlas is fetched and registered.
20
+ */
21
+ readonly font?: string | FontAtlasUrls;
8
22
  }
9
23
  /**
10
24
  * Bind a HUD to a canvas: register its render layer, route pointer input to
@@ -1 +1 @@
1
- {"version":3,"file":"attach.d.ts","sourceRoot":"","sources":["../src/attach.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AACjC,OAAO,KAAK,EAAE,kBAAkB,EAA+B,MAAM,iBAAiB,CAAC;AAOvF,OAAO,EAA6B,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjF,MAAM,WAAW,gBAAgB;IAC/B;2EACuE;IACvE,QAAQ,CAAC,KAAK,CAAC,EAAE,aAAa,CAAC;CAChC;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CACvB,GAAG,EAAE,kBAAkB,EACvB,GAAG,EAAE,GAAG,EACR,OAAO,GAAE,gBAAqB,GAC7B,MAAM,IAAI,CAkIZ"}
1
+ {"version":3,"file":"attach.d.ts","sourceRoot":"","sources":["../src/attach.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AACjC,OAAO,KAAK,EAAE,kBAAkB,EAA+B,MAAM,iBAAiB,CAAC;AAIvF,OAAO,EAGL,KAAK,aAAa,EACnB,MAAM,6BAA6B,CAAC;AAGrC,OAAO,EAA6B,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjF,MAAM,WAAW,gBAAgB;IAC/B;2EACuE;IACvE,QAAQ,CAAC,KAAK,CAAC,EAAE,aAAa,CAAC;IAC/B;;;;;;;;;;;OAWG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC;CACxC;AAED;;;;;;;GAOG;AACH,wBAAgB,SAAS,CACvB,GAAG,EAAE,kBAAkB,EACvB,GAAG,EAAE,GAAG,EACR,OAAO,GAAE,gBAAqB,GAC7B,MAAM,IAAI,CAwIZ"}
@@ -1,7 +1,19 @@
1
1
  /** Family name the bundled HUD font registers under. Widgets that name no
2
2
  * family draw with it. */
3
3
  export declare const DEFAULT_FONT_FAMILY = "weasel-hud-default";
4
- /** Register the bundled Inter atlas under `DEFAULT_FONT_FAMILY`. `attachHud`
5
- * calls this; calling it again is a no-op. */
6
- export declare function registerDefaultFont(): Promise<void>;
4
+ /** A bitmap face: the `registerFont` pair, as one value. */
5
+ export interface FontAtlasUrls {
6
+ metricsUrl: string;
7
+ atlasUrl: string;
8
+ }
9
+ /**
10
+ * Register an Inter-shaped atlas under {@link DEFAULT_FONT_FAMILY}. `attachHud`
11
+ * calls this; calling it again is a no-op.
12
+ *
13
+ * `urls` points the family at a copy the host already serves. The bundled atlas
14
+ * is the same Inter every weasel app ships in its own publicDir, and fetching
15
+ * both costs a second 150 kB transfer and a second `createImageBitmap` for
16
+ * bytes already in memory.
17
+ */
18
+ export declare function registerDefaultFont(urls?: FontAtlasUrls): Promise<void>;
7
19
  //# sourceMappingURL=registerDefaultFont.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"registerDefaultFont.d.ts","sourceRoot":"","sources":["../../src/fonts/registerDefaultFont.ts"],"names":[],"mappings":"AAOA;2BAC2B;AAC3B,eAAO,MAAM,mBAAmB,uBAAuB,CAAC;AAExD;+CAC+C;AAC/C,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CAEzD"}
1
+ {"version":3,"file":"registerDefaultFont.d.ts","sourceRoot":"","sources":["../../src/fonts/registerDefaultFont.ts"],"names":[],"mappings":"AAOA;2BAC2B;AAC3B,eAAO,MAAM,mBAAmB,uBAAuB,CAAC;AAExD,4DAA4D;AAC5D,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAsB,mBAAmB,CAAC,IAAI,CAAC,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAO7E"}
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export type { Widget, WidgetBounds, HudDrawCtx, HudPointerEvent, } from './widge
5
5
  export { DEFAULT_WIDGET_CLAIMS, claimsOf } from './widget';
6
6
  export type { HudHost } from './host';
7
7
  export { DEFAULT_FONT_FAMILY, registerDefaultFont } from './fonts/registerDefaultFont';
8
+ export type { FontAtlasUrls } from './fonts/registerDefaultFont';
8
9
  export type { RectOptions, RectWidget } from './widgets/rect';
9
10
  export type { TextOptions, TextWidget } from './widgets/text';
10
11
  export type { ImageOptions, ImageWidget } from './widgets/image';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,GAAG,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;A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-1,4 +1,4 @@
1
- import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "./tool-sq7ioZyd.js";
1
+ import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "./tool-ahNtvOfu.js";
2
2
  import { createViewportLayer as u, screenToWorld as d, viewToTransform as f } from "@weasel-js/core";
3
3
  import { createLoupeModel as p, loupeInnerView as m, loupeInnerView as h } from "@weasel-js/loupe";
4
4
  //#region src/loupe/readback.ts
@@ -1,16 +1,23 @@
1
- import { c as e, n as t, r as n } from "../tool-sq7ioZyd.js";
1
+ import { c as e, n as t, r as n } from "../tool-ahNtvOfu.js";
2
2
  import { resolveTheme as r, weaselTheme as i } from "@weasel-js/theme";
3
3
  import { useEffect as a, useMemo as o, useState as s } from "react";
4
4
  import { useThemeOptional as c } from "@weasel-js/theme/react";
5
5
  //#region src/react/useHud.ts
6
- function l(t) {
7
- let [l] = s(() => e()), u = c(), d = o(() => u?.resolved ?? r(i, i.defaultMode), [u]);
6
+ function l(t, l = {}) {
7
+ let [u] = s(() => e()), d = c(), f = o(() => d?.resolved ?? r(i, i.defaultMode), [d]), p = l.font;
8
8
  return a(() => {
9
9
  let e = t.current;
10
10
  if (!e) return;
11
- let r = n(e, l, { theme: d });
11
+ let r = n(e, u, {
12
+ theme: f,
13
+ ...p === void 0 ? {} : { font: p }
14
+ });
12
15
  return e.requestRedraw(), r;
13
- }, [l, d]), l;
16
+ }, [
17
+ u,
18
+ f,
19
+ p
20
+ ]), u;
14
21
  }
15
22
  function u() {
16
23
  return o(() => t(), []);
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../src/react/useHud.ts"],"sourcesContent":["import { useEffect, useMemo, useState } from 'react';\nimport { useThemeOptional } from '@weasel-js/theme/react';\nimport { resolveTheme, weaselTheme } from '@weasel-js/theme';\nimport { createHud, type Hud } from '../hud';\nimport { attachHud } from '../attach';\nimport type { CanvasExtensionApi, Contribution } from '@weasel-js/core';\nimport { createHudContribution } from '../tool';\n\n/**\n * Create a HUD and attach it to a Canvas via its imperative ref handle.\n * Returns the Hud immediately (unbound); attaches in an effect after the\n * canvas ref populates. Cleans up on unmount.\n *\n * Usage:\n * const ref = useRef<CanvasExtensionApi>(null);\n * const hud = useHud(ref);\n * // ... later\n * const btn = hud.button({ ... });\n */\nexport function useHud(canvasRef: { current: CanvasExtensionApi | null }): Hud {\n const [hud] = useState(() => createHud());\n\n // Follow the app's theme when there is one. Widgets are drawn into the\n // canvas, so they can't inherit the cascade — handing them the resolved\n // record is what keeps HUD chrome in step with a mode switch.\n const provided = useThemeOptional();\n const theme = useMemo(\n () => provided?.resolved ?? resolveTheme(weaselTheme, weaselTheme.defaultMode),\n [provided],\n );\n\n useEffect(() => {\n const api = canvasRef.current;\n if (!api) return;\n const detach = attachHud(api, hud, { theme });\n api.requestRedraw();\n return detach;\n // canvasRef.current changing during component lifetime is unusual for\n // canvas refs; treat as effectively-stable in v1. The dep on `hud` is\n // also stable (it comes from useState's initializer, never changes).\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [hud, theme]);\n\n return hud;\n}\n\n/**\n * Memoize the HUD's input contribution — see {@link createHudContribution}\n * for how the routing works.\n *\n * It is stateless (its actions resolve the hit widget from the gesture's\n * affordance payload), so one instance serves any number of HUDs on the page.\n */\nexport function useHudContribution(): Contribution {\n return useMemo(() => createHudContribution(), []);\n}\n"],"mappings":";;;;;AAmBA,SAAgB,EAAO,GAAwD;CAC7E,IAAM,CAAC,KAAO,QAAe,GAAW,CAAC,EAKnC,IAAW,GAAkB,EAC7B,IAAQ,QACN,GAAU,YAAY,EAAa,GAAa,EAAY,YAAY,EAC9E,CAAC,EAAS,CACX;AAcD,QAZA,QAAgB;EACd,IAAM,IAAM,EAAU;AACtB,MAAI,CAAC,EAAK;EACV,IAAM,IAAS,EAAU,GAAK,GAAK,EAAE,UAAO,CAAC;AAE7C,SADA,EAAI,eAAe,EACZ;IAKN,CAAC,GAAK,EAAM,CAAC,EAET;;AAUT,SAAgB,IAAmC;AACjD,QAAO,QAAc,GAAuB,EAAE,EAAE,CAAC"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../src/react/useHud.ts"],"sourcesContent":["import { useEffect, useMemo, useState } from 'react';\nimport { useThemeOptional } from '@weasel-js/theme/react';\nimport { resolveTheme, weaselTheme } from '@weasel-js/theme';\nimport { createHud, type Hud } from '../hud';\nimport { attachHud, type AttachHudOptions } from '../attach';\nimport type { CanvasExtensionApi, Contribution } from '@weasel-js/core';\nimport { createHudContribution } from '../tool';\n\n/**\n * Create a HUD and attach it to a Canvas via its imperative ref handle.\n * Returns the Hud immediately (unbound); attaches in an effect after the\n * canvas ref populates. Cleans up on unmount.\n *\n * Usage:\n * const ref = useRef<CanvasExtensionApi>(null);\n * const hud = useHud(ref);\n * // ... later\n * const btn = hud.button({ ... });\n *\n * An app that has already registered a bitmap family should say so —\n * `useHud(ref, { font: 'sans-serif' })` — or the HUD fetches its own copy of\n * the same atlas.\n */\nexport function useHud(\n canvasRef: { current: CanvasExtensionApi | null },\n options: Pick<AttachHudOptions, 'font'> = {},\n): Hud {\n const [hud] = useState(() => createHud());\n\n // Follow the app's theme when there is one. Widgets are drawn into the\n // canvas, so they can't inherit the cascade — handing them the resolved\n // record is what keeps HUD chrome in step with a mode switch.\n const provided = useThemeOptional();\n const theme = useMemo(\n () => provided?.resolved ?? resolveTheme(weaselTheme, weaselTheme.defaultMode),\n [provided],\n );\n\n const font = options.font;\n useEffect(() => {\n const api = canvasRef.current;\n if (!api) return;\n const detach = attachHud(api, hud, { theme, ...(font !== undefined ? { font } : {}) });\n api.requestRedraw();\n return detach;\n // canvasRef.current changing during component lifetime is unusual for\n // canvas refs; treat as effectively-stable in v1. The dep on `hud` is\n // also stable (it comes from useState's initializer, never changes).\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [hud, theme, font]);\n\n return hud;\n}\n\n/**\n * Memoize the HUD's input contribution — see {@link createHudContribution}\n * for how the routing works.\n *\n * It is stateless (its actions resolve the hit widget from the gesture's\n * affordance payload), so one instance serves any number of HUDs on the page.\n */\nexport function useHudContribution(): Contribution {\n return useMemo(() => createHudContribution(), []);\n}\n"],"mappings":";;;;;AAuBA,SAAgB,EACd,GACA,IAA0C,EAAE,EACvC;CACL,IAAM,CAAC,KAAO,QAAe,GAAW,CAAC,EAKnC,IAAW,GAAkB,EAC7B,IAAQ,QACN,GAAU,YAAY,EAAa,GAAa,EAAY,YAAY,EAC9E,CAAC,EAAS,CACX,EAEK,IAAO,EAAQ;AAarB,QAZA,QAAgB;EACd,IAAM,IAAM,EAAU;AACtB,MAAI,CAAC,EAAK;EACV,IAAM,IAAS,EAAU,GAAK,GAAK;GAAE;GAAO,GAAI,MAAS,KAAA,IAAuB,EAAE,GAAb,EAAE,SAAM;GAAQ,CAAC;AAEtF,SADA,EAAI,eAAe,EACZ;IAKN;EAAC;EAAK;EAAO;EAAK,CAAC,EAEf;;AAUT,SAAgB,IAAmC;AACjD,QAAO,QAAc,GAAuB,EAAE,EAAE,CAAC"}
@@ -1,4 +1,5 @@
1
1
  import { type Hud } from '../hud';
2
+ import { type AttachHudOptions } from '../attach';
2
3
  import type { CanvasExtensionApi, Contribution } from '@weasel-js/core';
3
4
  /**
4
5
  * Create a HUD and attach it to a Canvas via its imperative ref handle.
@@ -10,10 +11,14 @@ import type { CanvasExtensionApi, Contribution } from '@weasel-js/core';
10
11
  * const hud = useHud(ref);
11
12
  * // ... later
12
13
  * const btn = hud.button({ ... });
14
+ *
15
+ * An app that has already registered a bitmap family should say so —
16
+ * `useHud(ref, { font: 'sans-serif' })` — or the HUD fetches its own copy of
17
+ * the same atlas.
13
18
  */
14
19
  export declare function useHud(canvasRef: {
15
20
  current: CanvasExtensionApi | null;
16
- }): Hud;
21
+ }, options?: Pick<AttachHudOptions, 'font'>): Hud;
17
22
  /**
18
23
  * Memoize the HUD's input contribution — see {@link createHudContribution}
19
24
  * for how the routing works.
@@ -1 +1 @@
1
- {"version":3,"file":"useHud.d.ts","sourceRoot":"","sources":["../../src/react/useHud.ts"],"names":[],"mappings":"AAGA,OAAO,EAAa,KAAK,GAAG,EAAE,MAAM,QAAQ,CAAC;AAE7C,OAAO,KAAK,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAGxE;;;;;;;;;;GAUG;AACH,wBAAgB,MAAM,CAAC,SAAS,EAAE;IAAE,OAAO,EAAE,kBAAkB,GAAG,IAAI,CAAA;CAAE,GAAG,GAAG,CAyB7E;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,IAAI,YAAY,CAEjD"}
1
+ {"version":3,"file":"useHud.d.ts","sourceRoot":"","sources":["../../src/react/useHud.ts"],"names":[],"mappings":"AAGA,OAAO,EAAa,KAAK,GAAG,EAAE,MAAM,QAAQ,CAAC;AAC7C,OAAO,EAAa,KAAK,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,KAAK,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAGxE;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,MAAM,CACpB,SAAS,EAAE;IAAE,OAAO,EAAE,kBAAkB,GAAG,IAAI,CAAA;CAAE,EACjD,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,MAAM,CAAM,GAC3C,GAAG,CA0BL;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,IAAI,YAAY,CAEjD"}