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

    Keep a command's state without keeping the plugin running

    Goal: a "Save Snapshot" item that is greyed out unless there is a frame to capture, speed items with a check mark on the speed in use, and palette items with a check mark on the palette in use, all following the player while your plugin is stopped because nothing used it; plus a test that nothing keeps the plugin running.

    A command's enabled, checked, busy and reason take three kinds of value:

    Value Evaluated by Follows the player
    A plain value (true, "No frame") nobody: sent once no; the plugin sets a new one with Command.set
    A function (() => …) the SDK, in the plugin's update passes only as far as the plugin's own copy of the state: plugin.player.state follows the player while a stream is open, and otherwise moves only after the plugin's own calls and reads. The plugin must run for it to be evaluated at all
    A condition from | plugin.when the player itself, at every change always, also while the plugin is stopped

    So a state that comes from the player is a condition; a function is for the plugin's own state.

    A condition is data: the plugin builds it once and hands it to the player with the command. when.can("snapshot") is the player's own PlayerState.can.snapshot, when.phase("ready", "ended") the source's phase, and when.stream a generated stream:

    declare function saveSnapshot(): Promise<void>;
    // ---cut---
    const { when } = plugin;
    plugin.commands.add({
    id: "save",
    path: ["@video", "Save Snapshot…"],
    enabled: when.can("snapshot").and(when.phase("ready", "ended"), when.stream.not()),
    reason: "No frame to capture", // shown only while disabled
    run: saveSnapshot,
    });

    and, or and not() return new conditions and leave the original as it is. With an enabled condition the player shows reason only while the item is disabled, so a fixed text is enough.

    Condition Holds while
    when.open, when.ready, when.playing, when.paused, when.phase(…phases) A source is open; it is loaded; it plays; it is paused; its phase is one of these
    when.can(control) The source allows pause, seek, rate, snapshot, … (PlayerState.can)
    when.stream The source is generated by a plugin
    when.duration({ min, max }), when.volume(…), when.rate(…) The number is within min ≤ value < max (one bound may be left out); an unknown duration is false
    when.muted, when.looping The output is muted; an A-B loop is set
    when.audioTrack(id), when.subtitleTrack(id), when.audioDevice(id) That track or output device is the selected one
    when.fullscreen, when.maximized, when.pip The main window is fullscreen, maximized (never while fullscreen), or shows the video picture in picture
    when.theme(id), when.language(preference) The palette or the interface language in use

    A fact the player cannot read counts as false, so when.duration({ min: 600 }).not() is true while the duration is unknown. Two more atoms, when.time and when.position.in, are for triggers only; the player refuses them in a command (Learn what happens in the player).

    checked takes a condition too. Radio items over the speeds check the one in use, also when the user changed the speed elsewhere; an interval around each value absorbs rounding:

    const { when, player } = plugin;
    for (const rate of [0.5, 1, 1.5, 2])
    plugin.commands.add({
    id: `rate-${rate}`,
    path: ["@playback", "Speed", `${rate}×`],
    radio: true,
    checked: when.rate({ min: rate - 0.005, max: rate + 0.005 }),
    enabled: when.can("rate"),
    run: () => player.setRate(rate),
    });

    The palettes work the same way with when.theme(id), a Pause item with checked: when.paused, a track menu with when.audioTrack(track.id) per item.

    The language names only the player's state. For the plugin's own state (a setting, a mode), set a plain value with Command.set when it changes: a value is sent once and nothing listens. The player keeps the last value while the plugin is stopped; keep the state itself in plugin.storage.session (or plugin.storage.json for a setting) so activate starts from it again.

    let skip = (await plugin.storage.session.get<boolean>("skip-intros")) === true;
    const toggle = plugin.commands.add({
    id: "skip-intros",
    path: ["@playback", "Skip Intros"],
    checked: skip, // a value: nothing listens
    run: async () => {
    skip = !skip;
    toggle.set({ checked: skip });
    await plugin.storage.session.set("skip-intros", skip);
    },
    });

    A function that reads only the plugin's own variables (checked: () => skip) is fine too while the plugin runs: it is evaluated in the plugin's update passes and listens to nothing.

    run is called only when the item is enabled, so the condition already said yes. When the handler needs a value of the player (the position, the speed), it reads it then: await plugin.player.refresh() returns the state now. The controls act on the current state on their own (the SDK reads it first when no stream keeps it current); to act only on the state the handler read, pass it as ifCurrent.

    TestHost evaluates conditions as the player does: host.command(id).state follows setSource, setMainWindow and setAppearance without the plugin sending anything. Assert that keepsRunning is empty, as in the complete example.

    main.ts:

    import type { Plugin } from "@vestra-player/sdk";

    export default function activate(plugin: Plugin) {
    const { when } = plugin;

    plugin.commands.add({
    id: "save",
    path: ["@video", "Save Snapshot…"],
    // Evaluated by the player, also while this plugin is stopped.
    enabled: when.can("snapshot").and(when.phase("ready", "ended"), when.stream.not()),
    reason: "No frame to capture",
    run: async () => {
    const seen = await plugin.player.refresh(); // the frame the user sees now
    const destination = await plugin.dialogs.saveFile({ fileName: "frame.png" });
    if (!destination) return;
    const snapshot = await plugin.media.capture({ ifCurrent: seen });
    try {
    await (await snapshot.export({ format: "png", destination })).done;
    } finally {
    await snapshot.dispose();
    }
    },
    });

    for (const [id, title] of [["vestra", "Dark"], ["vestra-light", "Light"]] as const)
    plugin.commands.add({
    id: `theme.${id}`,
    path: ["Settings", "Appearance", title],
    radio: true,
    checked: when.theme(id),
    run: () => plugin.appearance.set(id),
    });
    }

    tests/main.test.ts:

    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 menu follows the player and nothing keeps the plugin running", async () => {
    const { host, stop } = await startPlugin(activate);
    try {
    await host.settle();
    assert.deepEqual(host.keepsRunning, []);
    assert.equal(host.command("save").state.enabled, false);
    host.setSource({ phase: "ready" });
    assert.equal(host.command("save").state.enabled, true);
    host.setAppearance("vestra-light");
    assert.equal(host.command("theme.vestra-light").state.checked, true);
    } finally {
    await stop();
    }
    });