Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contedt: redesign Auto ID system #797

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions Context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -32,7 +46,7 @@ type context struct {

isRunning bool

widgetIndexCounter int
widgetIndex map[string]int

// Indicate whether current application is running
isAlive bool
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
5 changes: 0 additions & 5 deletions Widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Window.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
Loading