diff --git a/Context.go b/Context.go index 21607e1a..ccd17ad7 100644 --- a/Context.go +++ b/Context.go @@ -8,6 +8,20 @@ import ( "gopkg.in/eapache/queue.v1" ) +// GenAutoID automatically generates widget's ID. +// It returns an unique value each time it is called. +func GenAutoID(id string) string { + idx, ok := Context.widgetIndex[id] + if !ok { + Context.widgetIndex[id] = 0 + return id + } + + Context.widgetIndex[id]++ + + return fmt.Sprintf("%s##%d", id, idx) +} + // Context represents a giu context. var Context *context @@ -32,7 +46,7 @@ type context struct { isRunning bool - widgetIndexCounter int + widgetIndex map[string]int // Indicate whether current application is running isAlive bool @@ -57,6 +71,7 @@ func CreateContext(b imgui.Backend[imgui.GLFWWindowFlags]) *context { FontAtlas: newFontAtlas(), textureLoadingQueue: queue.New(), m: &sync.Mutex{}, + widgetIndex: make(map[string]int), } // Create font @@ -106,8 +121,8 @@ func (c *context) cleanState() { return true }) - // Reset widgetIndexCounter - c.widgetIndexCounter = 0 + // Reset widgetIndex + c.widgetIndex = make(map[string]int) } func SetState[T any, PT genericDisposable[T]](c *context, id string, data PT) { @@ -154,11 +169,3 @@ func (c *context) load(id any) (*state, bool) { return nil, false } - -// Get widget index for current layout. -func (c *context) GetWidgetIndex() int { - i := c.widgetIndexCounter - c.widgetIndexCounter++ - - return i -} diff --git a/Widgets.go b/Widgets.go index 2cc572bd..d27eb042 100644 --- a/Widgets.go +++ b/Widgets.go @@ -7,11 +7,6 @@ import ( imgui "github.com/AllenDang/cimgui-go" ) -// GenAutoID automatically generates fidget's id. -func GenAutoID(id string) string { - return fmt.Sprintf("%s##%d", id, Context.GetWidgetIndex()) -} - var _ Widget = &RowWidget{} // RowWidget joins a layout into one line diff --git a/Window.go b/Window.go index 50f20980..e082054f 100644 --- a/Window.go +++ b/Window.go @@ -12,7 +12,7 @@ import ( func SingleWindow() *WindowWidget { pos := imgui.MainViewport().Pos() sizeX, sizeY := Context.backend.DisplaySize() - title := fmt.Sprintf("SingleWindow_%d", Context.GetWidgetIndex()) + title := GenAutoID("SingleWindow") return Window(title). Flags( @@ -29,7 +29,7 @@ func SingleWindow() *WindowWidget { func SingleWindowWithMenuBar() *WindowWidget { pos := imgui.MainViewport().Pos() sizeX, sizeY := Context.backend.DisplaySize() - title := fmt.Sprintf("SingleWindow_%d", Context.GetWidgetIndex()) + title := GenAutoID("SingleWindowWithMenuBar") return Window(title). Flags(