With id

ImGui hashes a widget’s identifier into the ID stack to detect clicks, remember imgui.ini state, and tag draw-list entries. The boost layer piggybacks on the same hash for its telemetry path. When two render sites need to share a widget identifier — composed helpers, two panes showing the same kind of control, a button rendered under two scopes — the boost ships three knobs for sorting out the collision:

  • with_id("scope") { ... } pushes "scope" onto both ImGui’s ID stack and the boost path, so two scopes contain the same widget identifier without collision.

  • widget(IDENT, (id = "x")) mangles only ImGui’s hash — useful when the daslang identifier may get renamed but the imgui.ini state and any hex_id-driven scripts must stay stable.

  • widget(IDENT, (path = "x")) replaces the registry path leaf — the daslang identifier still names the global; "x" is what external drivers target via imgui_force_set / imgui_click.

Source: examples/tutorial/with_id.das.

Walkthrough

  1options gen2
  2
  3require imgui
  4require imgui_app
  5require opengl/opengl_boost
  6require live/glfw_live
  7require live/live_api
  8require live/live_commands
  9require live/live_vars
 10require live_host
 11require imgui/imgui_live
 12require imgui/imgui_boost_runtime
 13require imgui/imgui_boost_v2
 14require imgui/imgui_widgets_builtin
 15require imgui/imgui_containers_builtin
 16require imgui/imgui_id_builtin
 17require imgui/imgui_visual_aids
 18
 19// =============================================================================
 20// TUTORIAL: with_id — scope IDs and registry paths for composed render sites.
 21//
 22// Each widget's ImGui hash is derived from its identifier — the same global
 23// rendered at two sites would hash to the same value and collide. The boost
 24// already enforces "single-global rendered exactly once per frame" at the
 25// registry level (loops want the indexed form `IDENT[i]`), but real apps
 26// still need to compose helpers that surface the same widget kind under
 27// distinct labels. Three knobs cover the cases:
 28//
 29//   with_id("scope") { ... }         — push "scope" onto BOTH the ImGui ID
 30//                                      stack AND the boost path. Distinct
 31//                                      scopes → distinct hex_ids AND distinct
 32//                                      registry paths.
 33//   widget(IDENT, (id = "x"))        — mangle ImGui's hash with "x" only;
 34//                                      telemetry path stays the bare
 35//                                      identifier (script-stable across
 36//                                      label renames; preserves imgui.ini).
 37//   widget(IDENT, (path = "x"))      — replace the telemetry path leaf with
 38//                                      "x"; the bare identifier still names
 39//                                      the daslang global. Mangles BOTH the
 40//                                      path AND the ImGui hash.
 41//
 42// STANDALONE: daslang.exe modules/dasImgui/examples/tutorial/with_id.das
 43// LIVE:       daslang-live modules/dasImgui/examples/tutorial/with_id.das
 44//
 45// DRIVE (when running live):
 46//   curl -X POST -d '{"name":"imgui_snapshot"}'                                                       localhost:9090/command
 47//   curl -X POST -d '{"name":"imgui_click","args":{"target":"ID_WIN/section_a/SAVE_BTN"}}'             localhost:9090/command
 48//   curl -X POST -d '{"name":"imgui_click","args":{"target":"ID_WIN/section_b/SAVE_BTN"}}'             localhost:9090/command
 49//   curl -X POST -d '{"name":"imgui_force_set","args":{"target":"ID_WIN/rps_stable","value":0.7}}'           localhost:9090/command
 50// =============================================================================
 51
 52[export]
 53def init() {
 54    live_create_window("dasImgui with_id tutorial", 720, 520)
 55    live_imgui_init(live_window)
 56    let io & = unsafe(GetIO())
 57    GetStyle().FontScaleMain = 1.5
 58}
 59
 60[export]
 61def update() {
 62    if (!live_begin_frame()) return
 63    begin_frame()
 64
 65    ImGui_ImplGlfw_NewFrame()
 66    apply_synth_io_override()
 67    NewFrame()
 68
 69    SetNextWindowPos(ImVec2(30.0f, 30.0f), ImGuiCond.FirstUseEver)
 70    SetNextWindowSize(ImVec2(640.0f, 440.0f), ImGuiCond.FirstUseEver)
 71    window(ID_WIN, (text = "with_id", closable = false,
 72                    flags = ImGuiWindowFlags.None)) {
 73
 74        // ---- with_id scope: same widget, two render sites ----
 75        // SAVE_BTN is a single-global widget. Without with_id, rendering it
 76        // twice would panic at end-of-frame (registry detects N>1). Each
 77        // scope contributes a path segment AND a PushID, so the registry
 78        // sees ID_WIN/section_a/SAVE_BTN and ID_WIN/section_b/SAVE_BTN as
 79        // distinct entries with distinct ImGui hashes.
 80        text("Same SAVE_BTN, two scopes:")
 81        with_id("section_a") {
 82            button(SAVE_BTN, (text = "Save in A"))
 83        }
 84        with_id("section_b") {
 85            button(SAVE_BTN, (text = "Save in B"))
 86        }
 87        text("SAVE_BTN.click_count aggregates: {SAVE_BTN.click_count}")
 88
 89        separator(WI_SEP_1)
 90
 91        // ---- Nested chains: outer scope, inner scope, leaf widget ----
 92        // The path is the chain joined by "/": ID_WIN/outer/inner/NESTED_BTN.
 93        text("Nested with_id chains:")
 94        with_id("outer") {
 95            with_id("inner") {
 96                button(NESTED_BTN, (text = "Nested deep"))
 97            }
 98        }
 99
100        separator(WI_SEP_2)
101
102        // ---- id= per-call: stable ImGui hash across label renames ----
103        // The button's daslang identifier (HASHED_BTN) controls visibility
104        // and live-reload; the imgui hash is mangled by "stable_v1" so
105        // scripts targeting the widget by hex_id, and imgui.ini state for
106        // this button, stay stable even if HASHED_BTN is later renamed.
107        // Telemetry path stays ID_WIN/HASHED_BTN.
108        text("Per-call id= - script-stable ImGui hash:")
109        button(HASHED_BTN, (text = "Hashed via id=", id = "stable_v1"))
110
111        separator(WI_SEP_3)
112
113        // ---- path= per-call: stable telemetry path across renames ----
114        // RPS_PATH is the daslang global (rename it and the variable
115        // reference breaks). "rps_stable" is the registry path leaf — the
116        // imgui_force_set / imgui_click string used by external drivers. Both the
117        // path AND the ImGui hash adopt "rps_stable".
118        text("Per-call path= - script-stable telemetry path:")
119        slider_float(RPS_PATH, (text = "Speed (path=)",
120                                path = "rps_stable"))
121    }
122
123    end_of_frame()
124    Render()
125    var w, h : int
126    live_get_framebuffer_size(w, h)
127    glViewport(0, 0, w, h)
128    glClearColor(0.10f, 0.10f, 0.12f, 1.0f)
129    glClear(GL_COLOR_BUFFER_BIT)
130    live_imgui_render()
131
132    live_end_frame()
133}
134
135[export]
136def shutdown() {
137    live_imgui_shutdown()
138    live_destroy_window()
139}
140
141[export]
142def main() {
143    init()
144    while (!exit_requested()) {
145        update()
146    }
147    shutdown()
148}

Requires

One extra module on top of the baseline boost layer:

  • imgui/imgui_id_builtin — defines with_id(s) { ... } and wires the id= / path= named-arg sugar for every widget macro.

The single-render rule

Single-global widgets render exactly once per frame. Two violations collapse into one runtime panic at end-of-frame:

  • Single-global widget inside a for loop (would render N times) — the boost macro can usually catch this lexically and macro_error at expansion with a fixit pointing at the indexed form IDENT[i] / IDENT[key] (see Widgets tour for the indexed form).

  • Single-global widget called from two distinct sites (e.g. shown in two windows at once) — when the macro can’t see the second site lexically, the runtime registry catches it and panics.

with_id is the explicit ID-stack push for case 2 when the indexed form can’t reach (composed helpers, sub-tree data-driven structure).

with_id scopes

Each with_id("scope") { ... } block pushes "scope" onto two stacks: ImGui’s ID stack (so child widgets get distinct hashes) and the boost registry path (so child entries register under distinct paths). Both pop on block exit:

with_id("section_a") {
    button(SAVE_BTN, (text = "Save in A"))
}
with_id("section_b") {
    button(SAVE_BTN, (text = "Save in B"))
}

The two renders show up in the snapshot as ID_WIN/section_a/SAVE_BTN and ID_WIN/section_b/SAVE_BTN — distinct paths AND distinct ImGui hashes. SAVE_BTN.click_count is one global; clicks on either button increment the same counter. If you need separate counters, switch to SAVE_BTN[key] indexed-widget form instead.

Nested chains

with_id chains nest. Each scope contributes one path segment, and the leaf widget’s registry path is the chain joined by /:

with_id("outer") {
    with_id("inner") {
        button(NESTED_BTN, (text = "Nested deep"))
    }
}
// registry path: ID_WIN/outer/inner/NESTED_BTN

with_id doesn’t register a container entry of its own — only the path segment. The leaf widget’s entry is what shows up in the snapshot.

Per-call id= — stable hash across renames

The daslang identifier is the source-of-truth name for a widget — it defines the global, drives live-reload visibility, and determines how the variable is referenced elsewhere in the program. The ImGui hash and the registry path are conventions built on top.

id="x" decouples the ImGui hash from the identifier:

button(HASHED_BTN, (text = "Hashed via id=", id = "stable_v1"))

Telemetry path is still ID_WIN/HASHED_BTN (the bare identifier); ImGui’s hash is mangled by "stable_v1" via PushID/PopID wrapping the render call. Practical wins:

  • imgui.ini stability — rename HASHED_BTN to SAVE_BUTTON_V2 in source, and imgui.ini’s open/close state for this button still matches the same ImGui hash; users don’t lose their layout.

  • Hex_id-driven scripts — external drivers that target widgets by the hex_id field in the snapshot keep working across daslang identifier renames.

Per-call path= — stable registry path across renames

path="x" is the inverse case — keep the daslang identifier internal, expose a stable string to external drivers:

slider_float(RPS_PATH, (text = "Speed (path=)", path = "rps_stable"))

The registry path becomes ID_WIN/rps_stable (the bare identifier is NOT registered). RPS_PATH is still the daslang global — rename it without breaking script drivers. path= also mangles the ImGui hash since widget_prelude does PushID(widget_ident) and widget_ident is the overridden value.

Standalone vs live

Same convention as previous tutorials: daslang for standalone or daslang-live to keep the live-reload server running.

Driving from outside

The registry paths shown above are exactly what external drivers target:

curl -X POST -d '{"name":"imgui_click","args":{"target":"ID_WIN/section_a/SAVE_BTN"}}' \
     localhost:9090/command
curl -X POST -d '{"name":"imgui_click","args":{"target":"ID_WIN/section_b/SAVE_BTN"}}' \
     localhost:9090/command
curl -X POST -d '{"name":"imgui_force_set","args":{"target":"ID_WIN/rps_stable","value":0.7}}' \
     localhost:9090/command

Note the third targets rps_stable (the path= override), not the RPS_PATH identifier — the override replaces the bare-identifier path entry, so the bare-name target would return “no such widget.”

Next steps

State and telemetry come next — the registered widget state structs that back every boost widget, how dotted flags on the identifier (RPS.PUBLIC.NOTLIVE) tune cross-module visibility and live-reload behavior, and the auto-emit hook that surfaces app-side values to the snapshot.

See also

Full source: examples/tutorial/with_id.das

Richer reference: examples/features/id_override.das — the features-side demo with the same surface plus a same-state shared-button demonstration.

Integration test: tests/integration/test_id_override.das.

Previous tutorial: With style

Boost macros — the macro layer.