NextGram/plugins Build a plugin Declarative UI

Declarative UI

Node model, styling, actions, Canvas and accessibility for plugin interfaces.

TypeScript plugins describe screens and slots as a JSON tree. The host renders the tree into framework views, owns lifecycle, insets, back, rotation and recreation, and never runs plugin code during measure or draw.

Java plugins may also return a tree through DeclarativeScreen and DeclarativeSlot. Real android.view.View objects remain available to Java and hybrid plugins through PluginScreen and SlotContribution.

1. Node model#

Every node has an optional key, a style, an a11y block, onLongPress, testId and one of these type values:

GroupTypes
Layoutcolumn, row, stack, scroll, list, grid
Texttext, richText
Mediaimage, icon, avatar
Actionsbutton, iconButton
FormstextField, switch, slider, select, checkbox
Decorationspacer, divider, card, badge, progress
Custom drawingcanvas

list takes items and an optional itemHeight; grid takes items and a positive columns. A button or iconButton must declare onPress. Interactive nodes should declare an accessibility label.

Limits are enforced before attach:

ItemLimit
Nodes per tree10,000
Depth64
String length10,000 characters
Canvas operations10,000
Serialized tree2 MB

An invalid tree falls back to the stock surface and counts toward the plugin failure budget.

2. Style#

Sizes are dp except font sizes, which are sp and are multiplied by the current font scale. Colors accept #AARRGGBB, #RRGGBB and appearance tokens such as appearance.surface, appearance.textPrimary, appearance.accent, appearance.danger and appearance.border.

ts
interface Style {
  width?: number | "match" | "wrap";
  height?: number | "match" | "wrap";
  minWidth?: number; minHeight?: number; maxWidth?: number; maxHeight?: number;
  padding?: number | Insets; margin?: number | Insets;
  gap?: number; flex?: number;
  align?: "start" | "center" | "end" | "stretch";
  justify?: "start" | "center" | "end" | "spaceBetween" | "spaceAround";
  background?: string; color?: string; border?: Border; radius?: number;
  fontSize?: number; fontWeight?: "regular" | "medium" | "bold";
  fontFamily?: string; textAlign?: "start" | "center" | "end";
  opacity?: number; elevation?: number; visibility?: "visible" | "hidden" | "gone";
}

Use start/end, never left/right, so the same tree works in RTL. Do not hardcode pixel values; the host applies density and font scale.

3. Actions and state#

onPress, onChange, onLongPress and onGesture are action ids, not functions. The host sends { action, nodeKey, value } to the screen or slot handler:

ts
ctx.screens.replace("chat_list", {
  create: (screen) => column({
    style: { padding: 16, gap: 8 },
    children: [
      text(`Chats: ${screen.items.length}`),
      button("Refresh", { onPress: "refresh" }),
    ],
  }),
  onAction(action, nodeKey, value) {
    if (action === "refresh") ctx.ui.invalidate();
  },
});

The host diffs by key. ctx.ui.invalidate() coalesces rerenders to at most one per frame. A large list should request bounded pages with ctx.ui.page(...) instead of putting thousands of nodes in one tree.

4. Canvas#

A canvas node carries a retained display list. The host draws it without calling JavaScript during draw. Supported operations: save, restore, translate, rotate, scale, alpha, rect, circle, line, path, text, image and clip.

ts
canvas([
  rect({ x: 0, y: 0, w: 200, h: 120, color: "appearance.surface", radius: 16 }),
  circle({ x: 60, y: 60, r: 32, color: "appearance.accent" }),
  textOp("42", { x: 60, y: 68, size: 18, color: "#FFFFFF", align: "center" }),
], { onGesture: "chart_tap" });

Gestures are hit-tested by the host and delivered as UI events. A canvas without accessibility labels fails the demo review: provide a label on the node or an adjacent text description.

5. Screens and slots#

ts
ctx.screens.replace("chat_list", handler);
ctx.screens.addSlot("chat_list_footer", {
  id: "compose",
  create: () => button("Compose", { onPress: "compose" }),
  spec: { height: "wrap", order: 10 },
  onAction(action) { /* ... */ },
});

The catalogue and required permissions are listed in screens.md. A registration is rejected with E_PERMISSION when either ui.screens or the screen's content permissions are missing. Protected surfaces are never replaceable.

Settings entries accept the same declarative tree. The host maps text, divider, switch, checkbox, textField, slider, select and button nodes to native settings rows; other containers are flattened. Row keys are storage keys, and an action is dispatched back to the entry when a row changes or a button is pressed.

ts
ctx.contributions.addSettingsEntry({
  id: "general",
  title: "My plugin",
  screen: () => column({
    children: [
      toggle(true, { key: "enabled", text: "Enabled", onPress: "enabled" }),
      textField("en", { key: "target", text: "Target language", onPress: "target" }),
    ],
  }),
});

6. Localization and accessibility#

Declarative strings may come from i18n/<locale>.json. The host applies the current locale, falls back to the default locale for missing keys and re-renders on a locale or layout-direction change.

Nodes accept role, label, hint, stateDescription, focusable and focusOrder. The host keeps the system back gesture and offers onBackPressed to the plugin first. Test TalkBack, keyboard navigation, RTL and 200% font scale on every shipped screen.