Forge auto widget layout

From Nebulavenus/forge-gpu · View interactive version

Forge auto widget layout is an AI agent skill from the GitHub repository Nebulavenus/forge-gpu. Add automatic widget layout to a ForgeUiContext application. Replace manual rect calculations with a stack-based cursor model supporting vertical/horizontal directions, padding, spacing, and nesting.

How to use this skill

Skill file

Add automatic widget positioning to any application that uses ForgeUiContext.

Based on UI Lesson 08.

When to use

to automate placement

rect by hand

Key API calls

layout region onto the stack

advance the cursor

the current layout

current layout

placed by the current layout

placed by the current layout

Correct order

  1. Push a layout with a bounding rect, direction, padding, and spacing
  2. Declare widgets using _layout() variants — each call advances the

cursor automatically

  1. For nested layouts: call layout_next() to reserve a sub-rect from the

parent, then layout_push() inside that rect

  1. Pop nested layouts before continuing in the parent
  2. Pop the root layout when done

Every layout_push() must have a matching layout_pop() — including on error

paths. Unmatched pushes are logged as warnings by forge_ui_ctx_end().

Key concepts

  1. Layout cursor — a position (x, y) that starts at the top-left content

area (after padding) and advances after each widget

  1. Direction — FORGE_UI_LAYOUT_VERTICAL advances downward (widgets get

full width, caller specifies height); FORGE_UI_LAYOUT_HORIZONTAL advances

rightward (widgets get full height, caller specifies width)

  1. Padding — uniform inset from all four edges of the layout rect
  2. Spacing — gap inserted before each widget except the first
  3. Size parameter — the widget dimension along the layout's primary axis

(height for vertical, width for horizontal); the cross-axis dimension is

filled automatically

  1. Layout stack — up to FORGE_UI_LAYOUT_MAX_DEPTH (8) nested layouts
  2. Spacing-before-item model — spacing is added before each item (except

the first), keeping remaining_h/remaining_w accurate

Common mistakes

  1. Forgetting layout_pop() — every push needs a pop on every code path,

including early returns from failed nested pushes

  1. Using standard widget API inside a layout — use _layout() variants

(button_layout, checkbox_layout) to get automatic positioning; the

standard API (button, checkbox) still requires explicit rects

  1. Wrong size parameter — in a vertical layout, size is height; in a

horizontal layout, size is width. Passing the wrong dimension produces

unexpectedly stretched or squished widgets

  1. Not reserving a sub-rect for nested layouts — call layout_next(row_h)

first to get the sub-rect, then push a new layout inside it. Pushing a

layout with the full parent rect would overlap existing widgets

  1. Exceeding stack depth — layout_push() returns false if the stack

overflows (8 levels max). Always check the return value

Ready-to-use template

Vertical panel with mixed widgets

#define PANEL_PADDING    16.0f   /* inset from panel edges */
#define WIDGET_SPACING    8.0f   /* vertical gap between widgets */
#define LABEL_HEIGHT     30.0f   /* height of a label row */
#define CHECKBOX_HEIGHT  28.0f   /* height of a checkbox row */
#define BUTTON_HEIGHT    34.0f   /* height of a button */
#define SLIDER_HEIGHT    32.0f   /* height of a slider */
#define BUTTON_SPACING   10.0f   /* horizontal gap between buttons */

ForgeUiRect panel = { x, y, w, h };

/* Draw panel background */
forge_ui__emit_rect(ctx, panel, bg_r, bg_g, bg_b, bg_a);

/* Push vertical layout for the panel */
if (!forge_ui_ctx_layout_push(ctx, panel,
                              FORGE_UI_LAYOUT_VERTICAL,
                              PANEL_PADDING, WIDGET_SPACING)) {
    return;
}

/* Title label */
forge_ui_ctx_label_layout(ctx, "Settings", LABEL_HEIGHT,
                          title_r, title_g, title_b, title_a);

/* Checkboxes */
(void)forge_ui_ctx_checkbox_layout(ctx, ID_CB_1, "Option A",
                                   &option_a, CHECKBOX_HEIGHT);
(void)forge_ui_ctx_checkbox_layout(ctx, ID_CB_2, "Option B",
                                   &option_b, CHECKBOX_HEIGHT);

/* Horizontal button row */
ForgeUiRect btn_row = forge_ui_ctx_layout_next(ctx, BUTTON_HEIGHT);

if (!forge_ui_ctx_layout_push(ctx, btn_row,
                              FORGE_UI_LAYOUT_HORIZONTAL,
                              0.0f, BUTTON_SPACING)) {
    forge_ui_ctx_layout_pop(ctx);  /* pop outer before returning */
    return;
}

float btn_w = (btn_row.w - BUTTON_SPACING) * 0.5f;
(void)forge_ui_ctx_button_layout(ctx, ID_BTN_OK, "OK", btn_w);
(void)forge_ui_ctx_button_layout(ctx, ID_BTN_CANCEL, "Cancel", btn_w);

forge_ui_ctx_layout_pop(ctx);  /* end horizontal row */

/* Slider */
(void)forge_ui_ctx_slider_layout(ctx, ID_SLIDER, &value,
                                 0.0f, 100.0f, SLIDER_HEIGHT);

forge_ui_ctx_layout_pop(ctx);  /* end vertical panel */

Key observations

the bounds for the inner horizontal layout

Reference

with manual vs automatic comparison

the ForgeUiContext this builds on

implementation

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.