8.18.1.36. Color button hover

color_button is a click trigger — its ClickState records click counts and the clicked per-frame flag. Some call sites want the swatch’s geometry only, with no click bookkeeping; the hover variant covers that:

let hovered = color_button_hover(SWATCH,
    (desc_id = "##red", col = float4(0.85f, 0.20f, 0.20f, 1.0f),
     size = float2(96.0f, 64.0f), flags = ImGuiColorEditFlags.NoTooltip))
if (hovered) {
    // per-frame side-effect, e.g. SetNextFrameWantCaptureMouse(...)
}

EmptyMarkerState backs the widget; the return value is the per-frame hover bool (ImGui::IsItemHovered() of the just-drawn swatch).

Source: modules/dasImgui/examples/tutorial/color_button_hover.das.

8.18.1.36.1. Walkthrough

  1options gen2
  2options _comment_hygiene = true
  3
  4require imgui
  5require imgui_app
  6require opengl/opengl_boost
  7require live/glfw_live
  8require live/live_api
  9require live/live_commands
 10require live/live_vars
 11require live_host
 12require imgui/imgui_live
 13require imgui/imgui_boost_runtime
 14require imgui/imgui_boost_v2
 15require imgui/imgui_widgets_builtin
 16require imgui/imgui_containers_builtin
 17require imgui/imgui_visual_aids
 18
 19// =============================================================================
 20// TUTORIAL: color_button_hover — hover-target variant of color_button.
 21//
 22//   color_button(IDENT, (col = ...))        — click trigger (ClickState).
 23//   color_button_hover(IDENT, (col = ...))  — hover only (EmptyMarkerState);
 24//                                              returns true while ImGui
 25//                                              considers the swatch hovered.
 26//
 27// Use the hover variant when the swatch is the surface on which a
 28// hover-driven side-effect is mounted (e.g. "while hovering this swatch,
 29// override io.WantCaptureMouse"); the click form would force the caller to
 30// distinguish click vs hover at every site.
 31//
 32// STANDALONE: daslang.exe modules/dasImgui/examples/tutorial/color_button_hover.das
 33// LIVE:       daslang-live modules/dasImgui/examples/tutorial/color_button_hover.das
 34// =============================================================================
 35
 36var private STATE_MESSAGE = "Hover any swatch."
 37
 38[export]
 39def init() {
 40    live_create_window("dasImgui color_button_hover tutorial", 640, 380)
 41    live_imgui_init(live_window)
 42    let io & = unsafe(GetIO())
 43    GetStyle().FontScaleMain = 1.4
 44}
 45
 46[export]
 47def update() {
 48    if (!live_begin_frame()) return
 49    begin_frame()
 50
 51    ImGui_ImplGlfw_NewFrame()
 52    apply_synth_io_override()
 53    NewFrame()
 54
 55    SetNextWindowPos(ImVec2(40.0f, 40.0f), ImGuiCond.Always)
 56    SetNextWindowSize(ImVec2(560.0f, 300.0f), ImGuiCond.Always)
 57    window(CBH_WIN, (text = "color_button_hover", closable = false,
 58                     flags = ImGuiWindowFlags.None)) {
 59        text("color_button_hover returns true while ImGui considers the")
 60        text("just-drawn swatch hovered. No click state, no toggle.")
 61        separator()
 62
 63        let swatch_flags = (ImGuiColorEditFlags.NoTooltip | ImGuiColorEditFlags.NoDragDrop)
 64        let red = color_button_hover(CBH_RED,
 65            (desc_id = "##red", col = float4(0.85f, 0.20f, 0.20f, 1.0f),
 66             size = float2(96.0f, 64.0f), flags = swatch_flags))
 67        same_line(CBH_SL_1)
 68        let green = color_button_hover(CBH_GREEN,
 69            (desc_id = "##green", col = float4(0.20f, 0.85f, 0.30f, 1.0f),
 70             size = float2(96.0f, 64.0f), flags = swatch_flags))
 71        same_line(CBH_SL_2)
 72        let blue = color_button_hover(CBH_BLUE,
 73            (desc_id = "##blue", col = float4(0.20f, 0.40f, 0.85f, 1.0f),
 74             size = float2(96.0f, 64.0f), flags = swatch_flags))
 75
 76        if (red) {
 77            STATE_MESSAGE = "Hovering: red"
 78        } elif (green) {
 79            STATE_MESSAGE = "Hovering: green"
 80        } elif (blue) {
 81            STATE_MESSAGE = "Hovering: blue"
 82        }
 83
 84        text(CBH_STATUS, (text = STATE_MESSAGE))
 85    }
 86
 87    end_of_frame()
 88    Render()
 89    var w, h : int
 90    live_get_framebuffer_size(w, h)
 91    glViewport(0, 0, w, h)
 92    glClearColor(0.10f, 0.10f, 0.12f, 1.0f)
 93    glClear(GL_COLOR_BUFFER_BIT)
 94    live_imgui_render()
 95
 96    live_end_frame()
 97}
 98
 99[export]
100def shutdown() {
101    live_imgui_shutdown()
102    live_destroy_window()
103}
104
105[export]
106def main() {
107    init()
108    while (!exit_requested()) {
109        update()
110    }
111    shutdown()
112}

8.18.1.36.1.1. Requires

Same baseline as color_button — already in imgui/imgui_widgets_builtin (re-exported by imgui/imgui_boost_v2).

8.18.1.36.1.2. When to reach for it

When the swatch is a surface, not a trigger:

  • examples/imgui_demo/inputs.das — the WantCapture-override panel uses a hover-target swatch to override io.WantCaptureMouse / io.WantCaptureKeyboard while the swatch is hovered. The ClickState of the regular color_button would force the caller to distinguish click vs hover.

  • Style previews where moving the mouse over the swatch shows a tooltip or status line — the hover bool is the natural drive signal.

8.18.1.36.1.3. Click vs hover

The two variants share the same call shape but pick the right state shape for the call site:

  • color_button(IDENT, (col, size, flags)) — returns bool clicked, records state.click_count and state.clicked.

  • color_button_hover(IDENT, (col, size, flags)) — returns bool hovered, no click bookkeeping.

A site that needs both (click triggers an action, hover updates a preview) should use the regular color_button and read IsItemHovered() after the call — the post-item hover query is allowed by the lint as a pure read.

8.18.1.36.1.4. Standalone vs live

Same convention as the other widget tutorials.

See also

Full source: modules/dasImgui/examples/tutorial/color_button_hover.das

Integration test: modules/dasImgui/tests/test_color_button_hover.das.

Boost macros — the macro layer.