QWidget functions and TypeScript
Function lets you write TypeScript logic inside a widget. Scripts run in an isolated sandbox iframe with no DOM, network, localStorage, or import; they can read inputs, return a plain object, and read/write the current widget’s visual properties through Host.
Minimal example
Structure
Function
Function.Interface
String / Number / Bool / DateTime / Json / Binary, or another Struct name.
Function.Input
Input).
Function.Output
{Results.<Function>.<Output>}.
Function.Script
TypeScript source; CDATA is recommended:Date is serialized as an ISO string. Top-level await is supported, so scripts can wait for Host calls directly.
Input sources
Input is built in this order:
ArgumentsJSON fromCallFunction(supports binding interpolation).- Otherwise, values from
Inputsmatched to the input Struct field names.
Script globals
Host
AllHost methods are asynchronous and return a Promise; you may await them or ignore the result.
Writable properties
Every visual node addressed byTarget exposes its XML visual attributes, for example:
Text:Value,Size,Weight,Color,Clamp,AlignButton/Link:Text,Variant,Size,UrlTextBox/CheckBox/Slider/Select/ColorPicker/FilePicker:Value,Checked,Options, etc.List:Items(an array is accepted directly),Gap,EmptyTextContainer/Row/Column/Grid:Gap,Padding,Border,Background,Radius,Align,JustifyImage/Media:Src,Alt,Source,Volume,Muted, etc.- All visual nodes:
Visible, plus the content aliasContent(Text → Value, Button → Text, List → Items, etc.)
Host.SetProperty cannot change structural or action attributes such as Target, Name, OnClick, OnChange, Function, Arguments, Command, WorkingDirectory, Timeout, PluginId, or Method.
Invoke methods
Example
Host.Invoke("RefreshButton", "Click") runs the button’s action again; invoking the button from its own script recurses, so only invoke other buttons or guard with a condition.
Scripts still cannot execute commands or call plugin APIs directly; those remain button/trigger actions.
Triggers
Loaded: once after mount.DataChanged: after the named data source loads.Click: when a control with the matchingNameis clicked.Timer: everyIntervalms, minimum 1000.Resize: 150ms after the grid or pixel size changes; useLoadedfor the initial layout.- Rate limit: 20 executions per second.
Run in the editor
Open the Functions tab, select a function, and click Run:- Executes with current
Inputsand sample data. - Shows returned JSON below the editor.
- Syntax errors are reported from sucrase.
- Run only verifies inputs/outputs;
Host.SetProperty/Host.InvokeUI changes apply when the widget runs on the dashboard, not to the editor preview.
Sandbox and security
- Scripts run in a sandbox iframe with CSP blocking network, images, media, frames, and workers.
- No
window,document,localStorage,fetch,XMLHttpRequest,import, orrequire. - On timeout the iframe is destroyed and recreated.
- Outputs are validated against
Function.Output. - Remote widgets can contain scripts and update silently; only use widgets you trust.
Debugging
- Use Run to verify inputs/outputs.
- Put intermediate values into outputs or
Host.Log. - Avoid infinite loops; the sandbox is destroyed on timeout.
- Fetch external data with
Datafirst; scripts only process loaded data.