Skip to content

Custom commands - #3284

Merged
texodus merged 3 commits into
masterfrom
context-menu-commands
Nov 15, 2026
Merged

texodus merged 3 commits into
masterfrom
context-menu-commands

Conversation

@texodus

@texodus texodus commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

This PR adds setCommandSchema(), an API which lets an app embedding <perspective-viewer> add its own commands to the right-click menu, with click context. Also adds a new trading-terminal example which shows off a few of these new CommandSchema types and logs their output:

Screenshot 2026-11-15 at 10 59 45 AM

New APIs

  • setCommandSchema(schema, { scopes? }) registers the command schema function and the surfaces it is evaluated for, or removes it with null.
  • invalidateCommands({ panel? }) re-queries the schema for every rendered button surface, coalescing calls within one task, and resolves when the plugins have redrawn.
  • queryCommands(plugin, location, frame, refs) resolves one command list and context per ref against a plugin's retained viewport frame, without an engine call.
  • invokeCommand(key, context) dispatches perspective-command for an app command picked by a plugin's own button.

Example

await viewer.setCommandSchema(
    (context) => {
        if (context.hit?.column_names[0] !== "security_id") {
            return { commands: [] };
        }

        const id = String(context.hit.id);
        return {
            commands: [
                {
                    kind: "Command",
                    key: "ticket",
                    label: "Buy",
                    icon: "ticket",
                    title: `Buy 100 ${ctx.hit.value}`,
                },             
                {
                    kind: "Command",
                    key: "flag",
                    label: flagged.has(id) ? "Unflag" : "Flag",
                },
                {
                    kind: "Command",
                    key: "close",
                    label: "Close",
                    disabled: ctx.location !== "context-menu",
                },
            ],
        };
    },
    { scopes: ["context-menu", "datagrid-cell", "chart-tooltip"] },
);

viewer.addEventListener(
    "perspective-command",
    async ({ detail: { key, context } }) => {
        if (key === "flag") {
            if (flagged.has(String(context.hit.id))) {
                flagged.delete(String(context.hit.id));
            } else {
                flagged.add(String(context.hit.id));
            }

            await viewer.invalidateCommands({ panel: context.panel });
        } else if (key === "ticket") {
            const table = await viewer.getTable({ panel: context.panel });
            const view = await table.view(context.hit.config);
            const slice = await view.to_json({ end_row: 1 });
            openTicket(slice[0]);
            await view.delete();
        }
    },
);

Drive-bys

  • Move numeric-coercion for Arrow batches to Rust client (from TS), small performance improvement but more importantly this behavior is now portable to Python.
  • Fix over-rendering during drag/drop in layout panel, new panel drag/drop style.

@texodus texodus added the enhancement Feature requests or improvements label Nov 9, 2026
Signed-off-by: Andrew Stein <steinlink@gmail.com>
Signed-off-by: Andrew Stein <steinlink@gmail.com>
@texodus
texodus force-pushed the context-menu-commands branch 2 times, most recently from f9dc312 to f2940b3 Compare November 15, 2026 17:14
@texodus texodus changed the title Custom context-menu commands Custom commands Nov 15, 2026
@texodus
texodus force-pushed the context-menu-commands branch from f2940b3 to 76f0fb3 Compare November 15, 2026 18:57
@texodus
texodus marked this pull request as ready for review November 15, 2026 19:25
Signed-off-by: Andrew Stein <steinlink@gmail.com>
@texodus
texodus force-pushed the context-menu-commands branch from 76f0fb3 to 71b3b79 Compare November 15, 2026 20:55
@texodus
texodus merged commit 34f4a4d into master Nov 15, 2026
19 checks passed
@texodus
texodus deleted the context-menu-commands branch November 15, 2026 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Feature requests or improvements

Development

Successfully merging this pull request may close these issues.

1 participant