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

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

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)),
           (ImGuiCol.ButtonHovered, ImVec4(0.95f, 0.30f, 0.30f, 1.0f)),
           (ImGuiCol.ButtonActive, ImVec4(0.70f, 0.12f, 0.12f, 1.0f)),
           (ImGuiStyleVar.FrameRounding, 8.0f)) {
    button(STYLED_BTN, (text = "Red rounded"))
}

A full button restyle pushes all three interaction states — Button, ButtonHovered, ButtonActive — so the color holds while the pointer is over it (push only Button and the default hover/active theme colors show through on interaction). Here they mix with a rounding metric in one block.

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.