How to toggle a button with Makie.jl?

I have a 3D viewer written in MakieGL. I have a button “PLAY”, and I want it to change to a “PAUSE” button when clicked and back to a “PLAY” button when clicked again.

How can I achieve that?

You can for example use an observable to track the play/pause state and lift the button label off of that.

running = Node(false)

function toggle_running()
    running[] = !running[] # or more complex logic
end

b = Button(..., label = @lift($running ? "PAUSE" : "PLAY"))

on(b.clicks) do _
    toggle_running()
end
1 Like

Works nicely!

Complete example:

using GLMakie

running = Node(false)

function toggle_running()
    running[] = !running[] # or more complex logic
end

fig=Figure()
fig[2, 1] = buttongrid = GridLayout(tellwidth = false)
btn = Button(fig, label = @lift($running ? "PAUSE" : "PLAY"))
buttongrid[1, 1:1] = [btn]

on(btn.clicks) do _
    toggle_running()
end

gl_screen = display(fig)
wait(gl_screen)

return nothing