AllenDang / giu

Cross platform rapid GUI framework for golang based on Dear ImGui.
MIT License
2.33k stars 134 forks source link

Add widget dynamically #11

Closed J7mbo closed 4 years ago

J7mbo commented 4 years ago

Hi, I'm trying to, given the click of a button (in it's triggered func(){}) add another button next to the previous one.

I can't find a concrete example of how to do this because the widgets are not passed by reference. Do I need to copy and re-render the whole window with all it's previous state and just the new button added? How can I achieve this?

Many thanks for the great lib!

AllenDang commented 4 years ago

You could consider below approches:

  1. Use the Condition widget.
  2. Create a func returns g.Layout, build what ever You need in it.
  3. Use g.Custom widget add bool to indicate when to build a new button.
J7mbo commented 4 years ago

Excellent, thanks for the options.

rjablecki commented 4 years ago

@J7mbo or @AllenDang can You write an example for this case ?

HACKERALERT commented 3 years ago

@AllenDang @rjablecki Here's a short example of adding widgets dynamically:

package main

import (
    g "github.com/AllenDang/giu"
)

func buildStuff() g.Layout{
    dynamicLabel := g.Label("I am rendered dynamically!")
    dynamicButton := g.Button("I am also rendered dynamically!")
    l := g.Layout([]g.Widget{dynamicLabel,dynamicButton})
    return l
}

func loop() {
    g.SingleWindow("Drag and Drop").Layout(
        g.Label("I am not dynamic."),
        buildStuff(),
    )
}

func main() {
    wnd := g.NewMasterWindow("Hi",600,400,g.MasterWindowFlagsNotResizable,nil)
    wnd.Run(loop)
}

It took me a few minutes to figure out, so it might be beneficial for AllenDang to add this as an example.

AllenDang commented 3 years ago

@HACKERALERT Good suggestion, will do. And I think this is the most charming part of immediate ui. :)