Skip to content
Cabbage
Install

Custom interfaces

Custom Widgets

Creating custom widget classes allows you to develop new controls while retaining the full editing tools provided by the Cabbage VS Code extension — including the property panel and drag-to-position editing. New widgets can be created using the command palette by selecting Create New Custom Widget.

If a custom widget folder does not already exist, the extension will prompt for its creation and automatically copy the required files into it. These files are placed inside a cabbage directory, which must not be moved.

Custom widget classes must be placed in the widgets subdirectory within cabbage, i.e. CustomWidgetFolder/cabbage/widgets. This ensures that the extension can locate and load them via the property panel. The file name also serves as the class name.

By convention, Cabbage classes use UpperCamelCase, while widget types use lowerCamelCase. The backend handles all value normalization required by the host DAW, allowing frontends to send and receive values in their full ranges (e.g., 20–20000 Hz for filter frequency, rather than normalized 0.0–1.0 values).

For a widget to be recognized by the Cabbage VS Code extension — particularly by the property panel — it must implement the following:

A JSON object containing properties accessible through the UI element inspectors in VS Code. Any property defined here can also be queried and modified using the Cabbage get and set opcodes in Csound. Properties can be objects, strings, or numbers. Boolean can be used only for set Cabbage properties such as visible, active, automatable, etc. Use 0/1 if you need to send boolean values to the backend.

At a minimum, the props object must include:

  • An id property
  • A channels array
  • An automatable property (boolean)
  • A bounds object to define dimensions and placement
  • A zIndex property (integer)
  • A visible property (boolean)
  • An active property (boolean)

The framework assigns these directly on the widget instance; do not add them to props:

  • this.vscode: The VS Code API instance when running from VS Code. It is null/undefined in plugin mode.
  • this.widgetDiv: The widget’s wrapper div. It is assigned before the first render() call and is available in both edit and performance modes, so prefer it over CabbageUtils.getWidgetDiv(this.props).

Optional. Called once per widget instance, after props are merged and after the wrapper has been added to the form. mode is either 'edit' or 'performance', and is fixed for the lifetime of that instance.

Use mount() to:

  • attach the widget’s own DOM to widgetDiv (a Preact/React root, a canvas, etc.),
  • register event listeners, choosing which ones apply to the current mode,
  • set up observers, timers or other resources that need the live DOM.

this.widgetDiv and this.vscode are already assigned. Register performance-only listeners when mode === 'performance'; the framework handles dragging and selection in edit mode. Do not attach listeners from render() or getInnerHTML().

The framework owns wrapper pointer-events: it sets them to none when the widget is hidden or inactive, and auto otherwise. Widgets should not need to assign widgetDiv.style.pointerEvents themselves.

Optional. Called once when the widget is discarded — when it is deleted, or when the form is rebuilt for a mode or backend change. Implement it if mount() registered anything that outlives the widget’s own subtree:

  • document / window listeners,
  • timers,
  • observers,
  • animation frames.

Listeners attached to the widget’s own wrapper or subtree do not need explicit cleanup; removing the wrapper is enough. Because each instance is mounted only once, unmount() is not required to undo a repeatable mount().

Returns the inner HTML used to render the UI element. The element’s size is determined by its bounds object. You can return an SVG element or a div, depending on your design. The WidgetManager calls this once during insertion.

getInnerHTML() is optional when render() is implemented. This is the pattern for widgets that render their own DOM (Preact, React, Web Components, canvas, etc.). The framework leaves the wrapper empty and lets render() create the content. For widgets that define getInnerHTML(), updates do not call it again; in-place renderers use render(), and plain HTML widgets are re-rendered by the framework.

Implementing render() marks the widget as an in-place renderer. The framework calls it after insertion (including in edit mode) and whenever values or properties change, instead of replacing the widget’s innerHTML. It is the correct place to re-render any DOM the widget owns — canvas, Preact/React, Web Components, or plain DOM.

render() should not attach event listeners; do that in mount(). It also must not return markup — render() owns its DOM and its return value is ignored.

When a widget is inserted, the framework runs these steps in order:

  1. The wrapper div is created and its id is assigned.
  2. new Widget() is called.
  3. this.widgetDiv and, when available, this.vscode are assigned.
  4. The widget’s props are merged with the props from the CSD file.
  5. getInnerHTML() is called if it exists; the result is placed inside the wrapper.
  6. The wrapper is appended to the form.
  7. mount(widgetDiv, mode) is called if it exists. This runs once.
  8. render() is called if it exists. This also runs in edit mode.
  9. The wrapper’s size, position and z-index are applied.
  10. When the widget is discarded, unmount() is called if it exists.

Mode changes rebuild widgets rather than reusing instances, so mode is fixed for the lifetime of each widget.

mount() receives the mode directly. Use CabbageUtils when you need to read it later:

CabbageUtils.getCabbageMode(); // 'draggable' in edit mode, 'nonDraggable' in performance mode
CabbageUtils.isEditMode(); // true in edit mode
CabbageUtils.isPerformanceMode(); // true outside edit mode

If you need to listen for changes to widget properties, wrap this.props with CabbageUtils.createReactiveProps(). This creates a Proxy that automatically handles common widget behaviours (like toggling pointer-events when visible or active change) and can notify you of property changes. Call it in your constructor:

this.props = CabbageUtils.createReactiveProps(this, this.props, {
onPropertyChange: (change) => {
// change.key - the property name
// change.value - new value
// change.oldValue - previous value
// change.path - dot-separated path to the property
},
watchKeys: null, // null = watch all, or array of strings/RegExp
mode: 'change', // 'change' = only notify when value differs, 'set' = notify on every set
lazyPath: true // compute path only when notifying (more efficient)
});

The opts parameter is optional — you can call CabbageUtils.createReactiveProps(this, this.props) without options for basic reactive behaviour.

Performance Note: Don’t use onPropertyChange to listen for value updates from Csound resulting from calls to cabbageSetValue. The WidgetManager automatically calls render() for in-place widgets when channel values change, bypassing the reactive props system for efficiency. Value updates are sent as lightweight messages without full widget JSON, allowing high-frequency updates (e.g., for k-rate parameter changes) without overhead. Use reactive props for UI-driven property changes (like visible, bounds, or custom properties), not for high-frequency value streams.

Plugin export does not copy your whole custom widget folder. At export time Cabbage:

  1. Parses the instrument’s Cabbage JSON and collects the widget types it uses.
  2. Copies all built-in widget files, because widgetTypes.js imports them statically.
  3. Copies only the custom widget types actually used by the instrument.
  4. Bakes in the active widget kit, if one is selected (see Widget Kits).

Custom widget files are written to the plugin’s cabbage/widgets/ resource directory:

  • macOS: /Library/CabbageAudio/PLUGIN_NAME/cabbage/widgets/
  • Windows: C:/ProgramData/CabbageAudio/PLUGIN_NAME/cabbage/widgets/
  • Linux: ~/.config/CabbageAudio/PLUGIN_NAME/cabbage/widgets/

PLUGIN_NAME matches the exported plugin binary. See Exporting Instruments for how the resource folder works.

The exported plugin has no customWidgetInfo registry, so it falls back to auto-discovery and imports the widget by type name. Name the widget file after its type, using either form:

  • myWidget.js for type myWidget
  • MyWidget.js for type myWidget

The editor can discover a file whose name differs from its type, but that file may not load in an exported plugin. Also use the stable imports provided by the export’s import map:

import { Cabbage } from "cabbage";
import { CabbageUtils } from "cabbage/utils.js";

These resolve in both the editor and the exported plugin. Do not rely on this.vscode in plugin code — it is not available outside the editor. Pass this.vscode to APIs that accept it, where they safely ignore it in plugin mode.

Only the custom widget’s own .js file is copied. Relative helper modules or other files next to it are not copied automatically. If a widget needs multiple files, package it as a widget kit instead: kit directories are copied recursively into cabbage/widgets/kits/<kit-folder>/.

A custom widget cannot replace a built-in widget type (button, rotarySlider, etc.). To restyle or replace a built-in, use a widget kit. Kit export rules are described in Widget Kits § Exporting.

To send data from your widget to the Cabbage backend, use Cabbage.sendControlData():

Cabbage.sendControlData({ channel: "myChannel", value: 42, gesture: "complete" }, this.vscode);

Parameters:

  • channel: The channel name (string)
  • value: The value in its natural range (e.g., 20–20000 Hz for filter frequency)
  • gesture: Optional — "begin", "value", "end", or "complete" (default)
  • vscode: The VS Code API instance (this.vscode in your widget; safely ignored as a plugin)

The backend automatically determines whether the channel is automatable and routes accordingly. For non-automatable data you can also call sendChannelData() directly:

Cabbage.sendChannelData("myStringChannel", "hello", this.vscode);
Cabbage.sendChannelData("myNumberChannel", 3.14, this.vscode);

The channels array defines the channels used to send value updates from the front end to the back end. If no this.props.id is defined, the first channel.id in the array is used as the primary DOM identifier.

For multi-channel widgets (like an EQ controller with separate frequency and gain channels per band), the WidgetManager automatically routes incoming parameter updates to the correct channel based on the channel ID. Each channel should have:

  • id: Unique channel identifier
  • range: Object with min, max, defaultValue, and optionally value
  • event: Optional event type (e.g., "valueChanged", "mouseDragX", "mouseDragY")

Returns the DOM ID for the widget’s div element. Prioritises props.id, then falls back to channels[0].id.

const divId = CabbageUtils.getWidgetDivId(this.props);

Returns the actual DOM element for the widget div. Accepts either a props object or a string div ID. Returns null if not found. Inside a widget, prefer this.widgetDiv, which the framework assigns before the first render() call.

const widgetDiv = this.widgetDiv || CabbageUtils.getWidgetDiv(this.props);
const widgetDiv = CabbageUtils.getWidgetDiv("myWidgetId");

Returns the ID string of the nth channel from the channels array. Throws an error if the channel or its ID is not set.

const firstChannelId = CabbageUtils.getChannelId(this.props, 0);
const secondChannelId = CabbageUtils.getChannelId(this.props, 1);