@vestra-player/sdk - v1.11.3
    Preparing search index...

    Example: Theme widgets

    A tool window drawn with the player's own theme and controls, so it looks like part of the player: its colours, spacing, buttons, sliders and tooltips.

    @vestra/theme.slint and @vestra/widgets.slint · a slider bound two-way to a property · the keys of the slider's commands beside it (plugin.commands.keys, KeyHint, ToolTipArea.keys).

    Open a tool window and remember its state teaches a window with the player's theme and widgets; the rules are in Design rules.

    Theme widgets in Vestra Player

    Tools ▸ Theme widgets… opens a small window with:

    • a label "Level 50" with a tooltip that names the keys ("Level F7 − F8 + · Default resets it"), a slider in the panel style, the key caps F7 F8 and a small Default button;
    • a second slider in the bar style, moving together with the first;
    • two icon buttons: a cross (with a tooltip) that resets the level, and a play icon that lights up above 50.
    examples/theme-widgets/
      manifest.json                  identity
      main.ts                        the code
      ui/theme-widgets.slint         the window
      tests/theme-widgets.test.ts    a test against the SDK's in-memory player
      package.json                   the SDK as a dev dependency, and the scripts
    
    {
    "id": "examples.theme-widgets",
    "version": "0.1.0",
    "apiVersion": "vestra/1",
    "minPlayerVersion": "0.9.0",
    "entry": "main.ts"
    }

    commands for the menu item and the keys, ui.window for the window.

    import { Theme } from "@vestra/theme.slint";
    import { Button, KeyHint, Slider, ToolTipArea } from "@vestra/widgets.slint";
    
    export component ThemeWidgets inherits Window {
        title: "Theme widgets";
        width: 301px;
        background: Theme.bg-panel;
        in-out property <float> level;
        in property <string> label;
        in property <string> level-down;
        in property <string> level-up;
        callback level-changed(float);
        callback reset();
        VerticalLayout {
            padding: Theme.dialog-padding;
            spacing: 12px;
            HorizontalLayout {
                spacing: 8px;
                ToolTipArea {
                    text: "Level";
                    keys: [{ key: root.level-down, text: "−" }, { key: root.level-up, text: "+" }];
                    detail: "· Default resets it";
                    width: 65px;
                    Text { text: root.label; color: Theme.text; font-size: Theme.font-body; horizontal-alignment: right; vertical-alignment: center; }
                }
                Slider { panel: true; value <=> root.level; changed(value) => { root.level-changed(value); } }
                HorizontalLayout {
                    spacing: 2px;
                    alignment: center;
                    VerticalLayout { alignment: center; KeyHint { text: root.level-down; } }
                    VerticalLayout { alignment: center; KeyHint { text: root.level-up; } }
                }
                Button { text: "Default"; small: true; clicked => { root.reset(); } }
            }
            Slider { value <=> root.level; changed(value) => { root.level-changed(value); } }
            HorizontalLayout {
                alignment: end;
                spacing: 8px;
                ToolTipArea {
                    text: "Reset";
                    Button { width: 25px; icon: "M 3 2.5 L 11 10.5 M 11 2.5 L 3 10.5"; stroked: true; clicked => { root.reset(); } }
                }
                Button { width: 25px; icon: "M 0 0 L 12 6 L 0 12 Z"; icon-width: 12px; checked: root.level > 0.5; }
            }
        }
    }
    

    From top to bottom:

    • The imports. @vestra/theme.slint and @vestra/widgets.slint are supplied by the player at run time. Theme holds the palette, fonts and sizes; Button, Slider and ToolTipArea look like the player's own controls. For editor support, point the Slint language server's library path vestra at node_modules/@vestra-player/sdk/slint/vestra.
    • The window. A fixed width; Theme.bg-panel instead of a hard-coded colour, so the background follows the player's palette.
    • The properties. level is in-out because both the plugin and the user change it; label is only written by the plugin. level-changed reports a drag; reset reports the reset buttons.
    • The layout. Theme.dialog-padding and Theme.font-body give the spacing and text size of the player's dialogs.
    • KeyHint draws one key cap. level-down and level-up are the labels the plugin passes; an empty one draws nothing and takes no space, so a key the user cleared leaves no gap. The caps sit at the end of the slider's row, minus before plus.
    • ToolTipArea shows its text after the pointer rests on its content for about half a second. The tip never takes a press or the keyboard focus: a click goes to the control under the pointer even while the tip shows. With keys (a list of { key, text }) it names the keys after the title as caps, each followed by what it does; an entry with an empty key is left out. detail follows dimmed.
    • The sliders are bound two-way to level with <=>, so dragging one moves the other at once, inside Slint. changed passes the value to the plugin. A one-way binding (value: root.level) would be broken by Slint the first time the user moved the slider. panel: true is the panel style; without it, the bar style.
    • Button with text is a dialog button; small: true is the 18-pixel one.
    • Icon buttons have no text; icon is SVG-style path data drawn in an icon-width × icon-height box (14×12 by default). stroked: true draws lines instead of filling. checked draws the icon in the accent colour.
    let level = 0.5;
    const step = (delta: number) =>
    (level = Math.min(1, Math.max(0, Math.round((level + delta) * 100) / 100)));
    const themeWidgetsWindow = plugin.windows.create("ui/theme-widgets.slint");
    themeWidgetsWindow.bind(() => ({
    level,
    label: `Level ${Math.round(level * 100)}`,
    // The keys of the two commands below as the menus write them, always
    // the current ones: "" hides a cap (no key, or the user cleared it).
    levelDown: plugin.commands.keys("level.down")[0] ?? "",
    levelUp: plugin.commands.keys("level.up")[0] ?? "",
    }));
    themeWidgetsWindow.on("levelChanged", (value: number) => (level = value));
    themeWidgetsWindow.on("reset", () => (level = 0.5));
    • level is the plugin's state, 0 to 1.
    • bind keeps the window's level and label equal to it.
    • levelChanged stores the dragged value. bind then sends it back, but it is the value the slider already shows, so nothing jumps.
    • reset sets the state back to 0.5; bind moves both sliders and the label.
    // Outside the menu (no path), in the command palette and on their keys.
    plugin.commands.add({
    id: "level.down",
    title: "Level −",
    bindings: ["F7"],
    repeat: true,
    run: () => step(-0.05),
    });
    plugin.commands.add({
    id: "level.up",
    title: "Level +",
    bindings: ["F8"],
    repeat: true,
    run: () => step(0.05),
    });
    • Two commands outside the menu (no path): the command palette and their keys F7 and F8 run them; repeat keeps stepping while a key is held.
    • In the window's bind, plugin.commands.keys gives each command's keys as the menus write them. They are always the current ones: when the user changes or clears a key in Keyboard Shortcuts… (from the command palette), the player sends the new labels and bind runs again. [0] is the key the menus show; ?? "" hides the cap when there is none.
    plugin.commands.add({
    id: "open",
    path: ["@tools", "Theme widgets…"],
    run: () => themeWidgetsWindow.show(),
    });

    run opens the window, or brings it back if it is hidden.

    import assert from "node:assert/strict";
    import test from "node:test";
    import { startPlugin } from "@vestra-player/sdk/testing";
    import activate from "../main.ts";

    test("the sliders report the level; Default resets it", async () => {
    const { host, stop } = await startPlugin(activate, {
    directory: new URL("..", import.meta.url),
    });
    await host.command("open").run();
    const themeWidgetsWindow = host.window("theme-widgets");
    assert.equal(themeWidgetsWindow.props.label, "Level 50");

    themeWidgetsWindow.emit("level-changed", 0.8); // the user drags a slider
    await host.settle();
    assert.equal(themeWidgetsWindow.props.label, "Level 80");

    themeWidgetsWindow.emit("reset"); // the user presses Default
    await host.settle();
    assert.equal(themeWidgetsWindow.props.level, 0.5);
    assert.equal(themeWidgetsWindow.props.label, "Level 50");
    await stop();
    });

    test("the key caps show the current keys and follow the user's changes", async () => {
    const { host, stop } = await startPlugin(activate, {
    directory: new URL("..", import.meta.url),
    });
    await host.command("open").run();
    await host.settle();
    const themeWidgetsWindow = host.window("theme-widgets");
    assert.equal(themeWidgetsWindow.props["level-down"], "F7");
    assert.equal(themeWidgetsWindow.props["level-up"], "F8");

    await host.press("F8"); // the key does what the slider does
    assert.equal(themeWidgetsWindow.props.label, "Level 55");

    host.rebind("level.up", ["Shift+F8"]); // the user picks another key
    host.rebind("level.down", []); // and clears this one
    await host.settle();
    assert.equal(themeWidgetsWindow.props["level-up"], "⇧F8"); // a Mac by default
    assert.equal(themeWidgetsWindow.props["level-down"], ""); // no cap
    await stop();
    });

    test("only the open window keeps the plugin running", async () => {
    const { host, stop } = await startPlugin(activate, {
    directory: new URL("..", import.meta.url),
    });
    const keepsRunning = () => host.keepsRunning;
    await host.settle();
    assert.deepEqual(keepsRunning(), []);
    await host.command("open").run();
    await host.settle();
    assert.deepEqual(keepsRunning(), ["an open window (theme-widgets)"]);
    host.window("theme-widgets").userClose();
    await host.settle();
    assert.deepEqual(keepsRunning(), []);
    await stop();
    });

    emit plays the user's part (a drag, a click on Default); props shows what the window displays afterwards. press presses a key, and rebind changes or clears a key as the user does in Keyboard Shortcuts; the test host writes labels as macOS does (⇧F8) unless host.systemInfo.os says otherwise.

    npm ci                    # once, in the SDK repository root
    cd examples/theme-widgets
    npm install
    npm run check && npm test
    npm run pack # examples.theme-widgets-0.1.0.vxp

    Start the player with --plugin <path to examples/theme-widgets> and choose Tools ▸ Theme widgets….

    1. Use the level. Add ui.osd and show the level with plugin.osd.show when the slider is released (the Slider's released callback).
    2. A text field. Add UrlInputRow from @vestra/widgets.slint, the player's input row with confirm and cancel buttons.
    3. More tokens. Theme lists every Theme token and widget property.