feat(skin): dock recovery as host-owned toolbar buttons

Add chrome.json slot mode "toolbar" with grouped and per-action recovery buttons, page-specific anchor candidates and sanitized toolbar style tokens. Keep the floating capsule as the bad-anchor fallback, hide the duplicate plugin launcher, and preserve legacy capsule slots. Document the full contract, add a minimal toolbar example package, and cover the new behavior in unit and browser tests.
This commit is contained in:
OddTurtleHead committed 2026-11-26 23:06:43 +08:00
1 parent 9a12e2ee7b
commit 881354e283
33 files changed
+2016 -113

No files matched your search

+74 -7
View File
@@ -18,7 +18,8 @@ import {
* - `icons`: bitmap overrides for stable icon slots, keyed by part name.
* - `slots`: declarative anchors for trusted host controls. A skin never
* receives the control; it only names a part it already renders so the
* host can position its own top-layer capsule there and fall back to the
* host can position its own top-layer capsule (or, with an explicit
* `mode: "toolbar"`, chrome-less host buttons) there and fall back to the
* default corner when the anchor stops being usable.
*
* Both the browser importer and the server-side registry parse the exact same
@@ -29,6 +30,7 @@ export const SKIN_CHROME_MAX_BYTES = 32 * 1024;
export const SKIN_CHROME_MAX_HIDDEN_PARTS = 32;
export const SKIN_CHROME_MAX_ICONS = 16;
export const SKIN_CHROME_MAX_SLOTS = 8;
export const SKIN_CHROME_MAX_SLOT_ANCHORS = 8;
export const SKIN_ICON_MAX_BYTES = 64 * 1024;
export const SKIN_ICONS_MAX_TOTAL_BYTES = 512 * 1024;
export const SKIN_ICON_DIRECTORY = "assets/icons/";
@@ -72,13 +74,34 @@ export interface SkinChromeDocument {
export type SkinChromeSlotAlign = "start" | "center" | "end";
/**
* How a slotted recovery control is presented:
* - `capsule` (default): the existing rounded host capsule with its own shell.
* - `toolbar`: independent, chrome-less host buttons that can match a skin's
* top bar. The host still creates the buttons, their labels, focus and
* click handlers; the skin only names anchors and style tokens.
*
* The mode is explicit and never inferred from `compact`: an old slot without
* `mode` keeps docking the capsule exactly as before.
*/
export type SkinChromeSlotMode = "capsule" | "toolbar";
/** One anchor name, or ordered candidates (for example one per page). */
export type SkinChromeSlotAnchor = string | string[];
export interface SkinChromeSlot {
/** Existing `data-ws-part` inside `.ws-skin-root` used as the anchor box. */
anchor: string;
/**
* Existing `data-ws-part` inside `.ws-skin-root` used as the anchor box.
* A list lists page/component candidates: the host takes the first visible
* one, so one slot can cover both the home and the voice page.
*/
anchor: SkinChromeSlotAnchor;
/** Horizontal alignment inside the anchor; the control is vertically centred. */
align: SkinChromeSlotAlign;
/** Collapse to the mark only until hovered or focused. */
/** Collapse to the mark only until hovered or focused (capsule mode only). */
compact: boolean;
/** Explicit presentation; see `SkinChromeSlotMode`. */
mode: SkinChromeSlotMode;
}
export interface SkinChromeAssetInfo {
@@ -147,6 +170,28 @@ function normalizeSlotAnchor(raw: unknown): string {
return raw;
}
/**
* A slot may name one anchor or an ordered candidate list (one per page or
* per component variant). Candidates are validated exactly like a single
* anchor; duplicates are removed so a page cannot be tried twice per check.
*/
function normalizeSlotAnchors(raw: unknown): SkinChromeSlotAnchor {
if (!Array.isArray(raw)) return normalizeSlotAnchor(raw);
if (!raw.length || raw.length > SKIN_CHROME_MAX_SLOT_ANCHORS) {
return reject(`A slot anchor list must contain between 1 and ${SKIN_CHROME_MAX_SLOT_ANCHORS} part names.`, "SKIN_CHROME_SLOT_INVALID");
}
const anchors: string[] = [];
for (const entry of raw) {
const anchor = normalizeSlotAnchor(entry);
if (!anchors.includes(anchor)) anchors.push(anchor);
}
return anchors;
}
function isSlotMode(value: unknown): value is SkinChromeSlotMode {
return value === "capsule" || value === "toolbar";
}
function isSlotAlign(value: unknown): value is SkinChromeSlotAlign {
return value === "start" || value === "center" || value === "end";
}
@@ -226,20 +271,42 @@ export function parseSkinChromeDocument(source: string): SkinChromeDocument {
return reject(`Slot ${part} must be an object.`, "SKIN_CHROME_SLOT_INVALID");
}
for (const key of Object.keys(value)) {
if (key !== "anchor" && key !== "align" && key !== "compact") {
if (key !== "anchor" && key !== "align" && key !== "compact" && key !== "mode") {
return reject(`Slot ${part} contains an unsupported field: ${key}.`, "SKIN_CHROME_SLOT_INVALID");
}
}
const anchor = normalizeSlotAnchor(value.anchor);
const anchor = normalizeSlotAnchors(value.anchor);
const align = value.align === undefined ? "end" : value.align;
if (!isSlotAlign(align)) {
return reject(`Slot ${part} has an invalid align value: ${String(align)}.`, "SKIN_CHROME_SLOT_INVALID");
}
// The group slot keeps the historical capsule default. The two
// single-action slots only exist for toolbar presentation; a one-button
// capsule would be a new, ambiguous surface with no product need.
const mode = value.mode === undefined
? (part === "host.recovery" ? "capsule" : "toolbar")
: value.mode;
if (!isSlotMode(mode)) {
return reject(`Slot ${part} has an invalid mode value: ${String(mode)}.`, "SKIN_CHROME_SLOT_INVALID");
}
if (part !== "host.recovery" && mode !== "toolbar") {
return reject(`Slot ${part} only supports toolbar mode.`, "SKIN_CHROME_SLOT_INVALID");
}
const compact = value.compact === undefined ? false : value.compact;
if (typeof compact !== "boolean") {
return reject(`Slot ${part} compact must be a boolean.`, "SKIN_CHROME_SLOT_INVALID");
}
slots[part] = { anchor, align, compact };
if (mode === "toolbar" && compact) {
return reject(`Slot ${part} compact is only valid in capsule mode.`, "SKIN_CHROME_SLOT_INVALID");
}
slots[part] = { anchor, align, compact, mode };
}
const perAction = slots["host.recovery.built-in"] !== undefined || slots["host.recovery.library"] !== undefined;
if (slots["host.recovery"] && perAction) {
return reject(
"Declare either host.recovery or its per-action slots (host.recovery.built-in / host.recovery.library), not both.",
"SKIN_CHROME_SLOT_INVALID",
);
}
}
+73 -3
View File
@@ -197,7 +197,11 @@ test("chrome.json declares hidden parts and icon bitmaps, with path and size val
});
test("chrome.json slots accept skin-owned anchors and reject trusted host UI", () => {
assert.deepEqual([...HOST_SLOTTABLE_PART_IDS], ["host.recovery"]);
assert.deepEqual([...HOST_SLOTTABLE_PART_IDS], [
"host.recovery",
"host.recovery.built-in",
"host.recovery.library",
]);
const explicit = parseSkinChromeDocument(JSON.stringify({
schemaVersion: 1,
@@ -209,10 +213,11 @@ test("chrome.json slots accept skin-owned anchors and reject trusted host UI", (
anchor: "voice.audio-dock",
align: "start",
compact: true,
mode: "capsule",
});
// Defaults: align "end", compact off. Component and runtime-plugin parts are
// ordinary anchors; only host/trusted parts are off limits.
// Old slots without `mode` keep the docked capsule: the presentation is
// explicit, never inferred from compact or any other field.
const defaults = parseSkinChromeDocument(JSON.stringify({
schemaVersion: 1,
slots: {
@@ -223,6 +228,7 @@ test("chrome.json slots accept skin-owned anchors and reject trusted host UI", (
anchor: "skin.voice.harbor-dock.part-anchor",
align: "end",
compact: false,
mode: "capsule",
});
assert.doesNotThrow(() => parseSkinChromeDocument(JSON.stringify({
schemaVersion: 1,
@@ -246,6 +252,70 @@ test("chrome.json slots accept skin-owned anchors and reject trusted host UI", (
[{ schemaVersion: 1, slots: { "host.recovery": { anchor: "../escape" } } }, /not a valid part name/],
[{ schemaVersion: 1, slots: { "host.recovery": { anchor: "Voice Audio Dock" } } }, /not a valid part name/],
[{ schemaVersion: 1, slots: { "host.recovery": { anchor: "" } } }, /at most 240 characters/],
[{ schemaVersion: 1, slots: { "host.recovery": { anchor: "voice.audio-dock", mode: "float" } } }, /invalid mode/],
[{ schemaVersion: 1, slots: { "host.recovery": { anchor: "voice.audio-dock", mode: "toolbar", compact: true } } }, /compact is only valid in capsule mode/],
[{ schemaVersion: 1, slots: { "host.recovery.built-in": { anchor: "voice.audio-dock", mode: "capsule" } } }, /only supports toolbar mode/],
[{ schemaVersion: 1, slots: { "host.recovery.built-in": { anchor: "voice.audio-dock", compact: true } } }, /compact is only valid in capsule mode/],
];
for (const [value, message] of invalidDocuments) {
assert.throws(() => parseSkinChromeDocument(JSON.stringify(value)), message, JSON.stringify(value));
}
});
test("chrome.json toolbar slots support page candidates and separate action buttons", () => {
const toolbar = parseSkinChromeDocument(JSON.stringify({
schemaVersion: 1,
slots: {
"host.recovery": {
mode: "toolbar",
anchor: [
"skin.home.topbar.part-host-actions",
"skin.voice.topbar.part-host-actions",
"skin.home.topbar.part-host-actions",
],
align: "end",
},
},
}));
assert.deepEqual(toolbar.slots?.["host.recovery"], {
anchor: [
"skin.home.topbar.part-host-actions",
"skin.voice.topbar.part-host-actions",
],
align: "end",
compact: false,
mode: "toolbar",
});
const perAction = parseSkinChromeDocument(JSON.stringify({
schemaVersion: 1,
slots: {
"host.recovery.built-in": {
mode: "toolbar",
anchor: "skin.voice.topbar.part-built-in",
align: "start",
},
"host.recovery.library": {
mode: "toolbar",
anchor: "skin.voice.topbar.part-library",
},
},
}));
assert.deepEqual(perAction.slots?.["host.recovery.built-in"], {
anchor: "skin.voice.topbar.part-built-in",
align: "start",
compact: false,
mode: "toolbar",
});
// Single-action slots are toolbar-only and default to toolbar mode.
assert.equal(perAction.slots?.["host.recovery.library"]?.mode, "toolbar");
assert.equal(perAction.slots?.["host.recovery.library"]?.align, "end");
const invalidDocuments: Array<[unknown, RegExp]> = [
[{ schemaVersion: 1, slots: { "host.recovery": { mode: "toolbar", anchor: [] } } }, /between 1 and 8/],
[{ schemaVersion: 1, slots: { "host.recovery": { mode: "toolbar", anchor: Array.from({ length: 9 }, (_, index) => `part-${index}`) } } }, /between 1 and 8/],
[{ schemaVersion: 1, slots: { "host.recovery": { mode: "toolbar", anchor: ["voice.audio-dock", "host.plugins.launcher"] } } }, /trusted host UI/],
[{ schemaVersion: 1, slots: { "host.recovery": { mode: "toolbar", anchor: "voice.audio-dock" }, "host.recovery.built-in": { mode: "toolbar", anchor: "voice.audio-dock" } } }, /either host.recovery or its per-action slots/],
];
for (const [value, message] of invalidDocuments) {
assert.throws(() => parseSkinChromeDocument(JSON.stringify(value)), message, JSON.stringify(value));
+9 -2
View File
@@ -47,11 +47,18 @@ export const HOST_TRUSTED_PART_IDS: ReadonlySet<string> = new Set([
* Trusted host controls that an open skin may fold into its own layout through
* `chrome.json#slots`. The key names the protected part, not a CSS hook: the
* skin only declares an anchor rectangle, the host still owns, paints and
* hit-tests the control. Extend this set (for example with `host.permission`)
* when another protected control learns the same runtime placement.
* hit-tests the control.
*
* `host.recovery` is the historical capsule group. The two action keys only
* exist for `mode: "toolbar"` and let a skin dock the "use built-in skin" and
* "application library" host buttons separately; they are still host-owned
* buttons, never DOM hooks. Extend this set when another protected control
* learns the same runtime placement.
*/
export const HOST_SLOTTABLE_PART_IDS: ReadonlySet<string> = new Set([
"host.recovery",
"host.recovery.built-in",
"host.recovery.library",
]);
/** All host-owned parts (used by the part registry and documentation). */