With style

ImGui’s style stack lets you change a color or metric for a scoped region of the UI — push it once, pop it when you’re done. Hand-balancing the push/pop pairs is bug-prone (miss a pop and EndFrame asserts), and heterogeneous tuples can’t go through daslang’s uniform-element-type varargs, so the boost layer ships with_style((key, value), ...) { ... } as a [call_macro]. Each (key, value) tuple is dispatched at compile-time — ImGuiCol keys route to PushStyleColor, ImGuiStyleVar keys route to PushStyleVar — and a single bulk pop_style_n undoes them all at block exit.

Source: examples/tutorial/with_style.das.

Walkthrough

with_style recording
  1options gen2
  2
  3require imgui
  4require imgui_app
  5require glfw/glfw_boost
  6require opengl/opengl_boost
  7require live/glfw_live
  8require live/live_api
  9require live/live_commands
 10require live/live_vars
 11require live/opengl_live
 12require live_host
 13require imgui/imgui_live
 14require imgui/imgui_boost_runtime
 15require imgui/imgui_boost_v2
 16require imgui/imgui_widgets_builtin
 17require imgui/imgui_containers_builtin
 18require imgui/imgui_style_builtin
 19require imgui/imgui_visual_aids
 20
 21// =============================================================================
 22// TUTORIAL: with_style — scoped color/metric overrides for a sub-tree.
 23//
 24// ImGui's style stack is one of its main customization knobs: PushStyleColor
 25// changes a color until the matching PopStyleColor; PushStyleVar changes a
 26// metric (rounding, padding, spacing, ...) until the matching PopStyleVar.
 27// Hand-balancing the pushes and pops is bug-prone — miss a pop and the
 28// next frame asserts on EndFrame. The `with_style((key, val), ...) { ... }`
 29// boost macro brackets the push/pop pair around a block:
 30//
 31//   with_style((ImGuiCol.Button, ImVec4(0.8f, 0.2f, 0.2f, 1.0f)),
 32//              (ImGuiStyleVar.FrameRounding, 8.0f)) {
 33//       button(STYLED_BTN, (text = "Red rounded"))
 34//   }
 35//
 36// Each `(key, value)` tuple is dispatched at compile time by overload
 37// resolution — ImGuiCol keys go to PushStyleColor, ImGuiStyleVar keys to
 38// PushStyleVar. After the block, the matching pops fire in one bulk call.
 39//
 40// STANDALONE: daslang.exe modules/dasImgui/examples/tutorial/with_style.das
 41// LIVE:       daslang-live modules/dasImgui/examples/tutorial/with_style.das
 42//
 43// DRIVE (when running live):
 44//   curl -X POST -d '{"name":"imgui_snapshot"}'                                                       localhost:9090/command
 45//   curl -X POST -d '{"name":"imgui_click","args":{"target":"STYLE_WIN/STYLED_BTN"}}'                  localhost:9090/command
 46//   curl -X POST -d '{"name":"imgui_click","args":{"target":"STYLE_WIN/NESTED_STYLED_BTN"}}'           localhost:9090/command
 47// =============================================================================
 48
 49[export]
 50def init() {
 51    live_create_window("dasImgui with_style tutorial", 720, 520)
 52    live_imgui_init(live_window)
 53    var io & = unsafe(GetIO())
 54    io.FontGlobalScale = 1.5
 55}
 56
 57[export]
 58def update() {
 59    if (!live_begin_frame()) return
 60    begin_frame()
 61
 62    ImGui_ImplOpenGL3_NewFrame()
 63    ImGui_ImplGlfw_NewFrame()
 64    apply_synth_io_override()
 65    NewFrame()
 66
 67    SetNextWindowPos(ImVec2(30.0f, 30.0f), ImGuiCond.FirstUseEver)
 68    SetNextWindowSize(ImVec2(640.0f, 440.0f), ImGuiCond.FirstUseEver)
 69    window(STYLE_WIN, (text = "with_style", closable = false,
 70                       flags = ImGuiWindowFlags.None)) {
 71        text("Mixed color + var, single block:")
 72        with_style((ImGuiCol.Button, ImVec4(0.85f, 0.20f, 0.20f, 1.0f)),
 73                   (ImGuiStyleVar.FrameRounding, 8.0f)) {
 74            button(STYLED_BTN, (text = "Red rounded"))
 75        }
 76
 77        separator(WS_SEP_1)
 78        text("Nested with_style — inner stacks on outer:")
 79        with_style((ImGuiCol.Text, ImVec4(1.0f, 0.95f, 0.30f, 1.0f))) {
 80            text("Yellow text inherited from outer scope.")
 81            with_style((ImGuiCol.Button, ImVec4(0.20f, 0.45f, 0.85f, 1.0f)),
 82                       (ImGuiStyleVar.FrameRounding, 4.0f)) {
 83                // Both the outer yellow text and the inner blue/rounded
 84                // button are in scope.
 85                button(NESTED_STYLED_BTN, (text = "Blue button + yellow text"))
 86            }
 87        }
 88
 89        separator(WS_SEP_2)
 90        text("After with_style — baseline style restored.")
 91        // If push/pop counts were off, this button would render with a
 92        // stale color/rounding from the block above. ImGui asserts on a
 93        // stack imbalance at EndFrame, so the live process would crash
 94        // before reaching the next frame. The boost macro guarantees the
 95        // bulk pop_style_n fires regardless of what the block body does.
 96        button(UNSTYLED_BTN, (text = "Plain button"))
 97    }
 98
 99    end_of_frame()
100    Render()
101    var w, h : int
102    live_get_framebuffer_size(w, h)
103    glViewport(0, 0, w, h)
104    glClearColor(0.10f, 0.10f, 0.12f, 1.0f)
105    glClear(GL_COLOR_BUFFER_BIT)
106    ImGui_ImplOpenGL3_RenderDrawData(GetDrawData())
107
108    live_end_frame()
109}
110
111[export]
112def shutdown() {
113    live_imgui_shutdown()
114    live_destroy_window()
115}
116
117[export]
118def main() {
119    init()
120    while (!exit_requested()) {
121        update()
122    }
123    shutdown()
124}

Requires

One extra module on top of the baseline boost layer:

  • imgui/imgui_style_builtin — the with_style [call_macro] plus the push_style_one / pop_style_n primitives it lowers to.

Single block, mixed types

Each tuple in the with_style(...) argument list is independent — colors and metric overrides mix freely:

with_style((ImGuiCol.Button, ImVec4(0.85f, 0.20f, 0.20f, 1.0f)),
           (ImGuiStyleVar.FrameRounding, 8.0f)) {
    button(STYLED_BTN, (text = "Red rounded"))
}

The macro emits one push_style_one(key, val) per tuple in source order, then invoke(blk), then a single pop_style_n(N) that pops the matching count in two bulk ImGui calls (one PopStyleColor for the color pushes, one PopStyleVar for the metric pushes).

Nesting

Nested with_style blocks stack: the inner block adds to the outer’s overrides without disturbing them. When the inner block exits, the outer scope is restored:

with_style((ImGuiCol.Text, ImVec4(1.0f, 0.95f, 0.30f, 1.0f))) {
    Text("Yellow text inherited from outer scope.")
    with_style((ImGuiCol.Button, ImVec4(0.20f, 0.45f, 0.85f, 1.0f)),
               (ImGuiStyleVar.FrameRounding, 4.0f)) {
        // Yellow text + blue button + 4-radius rounding all active.
        button(NESTED_STYLED_BTN, (text = "Blue button + yellow text"))
    }
    // Back to: yellow text, default button color/rounding.
}

Pop balance

After every with_style block exits, the baseline style is restored — the underlying g_style_pop_stack tracks per-push kind tags so the bulk pop fires the right number of PopStyleColor / PopStyleVar calls. Miss a pop and ImGui asserts at EndFrame; the macro is the only API surface that pushes, so user code can’t accidentally leak pushes past the block boundary.

Standalone vs live

Same convention as previous tutorials.

Driving from outside

with_style is purely structural — there’s no per-block state to set. Drive the buttons inside instead:

curl -X POST -d '{"name":"imgui_click","args":{"target":"STYLE_WIN/STYLED_BTN"}}' \
     localhost:9090/command
curl -X POST -d '{"name":"imgui_click","args":{"target":"STYLE_WIN/NESTED_STYLED_BTN"}}' \
     localhost:9090/command

Next steps

Widget identity comes next — with_id to disambiguate widgets that share the same name within an ImGui ID scope, plus the id=/path= sugar that lets the boost layer steer the registry path without restructuring the call hierarchy.

See also

Full source: examples/tutorial/with_style.das

Richer reference: examples/features/style_override.das — the features-side demo with the same surface plus a baseline-restore check.

Integration test: tests/integration/test_style_with_style.das.

Previous tutorial: Docking

Boost macros — the macro layer.