From 445240c840cc54f65886ee17dabe4e3a220dc1b7 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Thu, 9 May 2024 11:45:48 +0200 Subject: [PATCH] contedt: redesign Auto ID system make it less possible to change some IDs and cause strange layout behaviours Sometimes, when there were nested layouts and some of them were dynamically generated depending on other widgets, it caused some strange UI behaviours --- Context.go | 25 +++++++++++++------------ Window.go | 4 ++-- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Context.go b/Context.go index f7b19cd5..04b5c0dc 100644 --- a/Context.go +++ b/Context.go @@ -11,7 +11,15 @@ import ( // GenAutoID automatically generates widget's ID. // It returns an unique value each time it is called. func GenAutoID(id string) string { - return fmt.Sprintf("%s##%d", id, Context.GetWidgetIndex()) + 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. @@ -38,7 +46,7 @@ type context struct { isRunning bool - widgetIndexCounter int + widgetIndex map[string]int // this function could be overwrited by user. // Especially, do this if you want to disable auto ID generator. GenAutoID func(baseID string) string @@ -67,6 +75,7 @@ func CreateContext(b imgui.Backend[imgui.GLFWWindowFlags]) *context { textureLoadingQueue: queue.New(), m: &sync.Mutex{}, GenAutoID: GenAutoID, + widgetIndex: make(map[string]int), } // Create font @@ -116,8 +125,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) { @@ -164,11 +173,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/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(