Skip to content

Commit 99b97dc

Browse files
committed
debugui: expose WidgetID and specify ID at SetTextFieldValue explicitly
1 parent 177d704 commit 99b97dc

File tree

13 files changed

+82
-72
lines changed

13 files changed

+82
-72
lines changed

button.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (c *Context) Button(label string) bool {
2727
return res
2828
}
2929

30-
func (c *Context) button(label string, opt option, id widgetID) (bool, error) {
30+
func (c *Context) button(label string, opt option, id WidgetID) (bool, error) {
3131
res, err := c.widget(id, opt, func(bounds image.Rectangle, wasFocused bool) (bool, error) {
3232
var res bool
3333
// handle click

container.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ type container struct {
1717
open bool
1818
collapsed bool
1919

20-
toggledIDs map[widgetID]struct{}
21-
textInputTextFields map[widgetID]*textinput.Field
20+
toggledIDs map[WidgetID]struct{}
21+
textInputTextFields map[WidgetID]*textinput.Field
2222
}
2323

2424
// ContainerLayout represents the layout of a container widget.
@@ -37,7 +37,7 @@ type ContainerLayout struct {
3737
ScrollOffset image.Point
3838
}
3939

40-
func (c *Context) container(id widgetID, opt option) *container {
40+
func (c *Context) container(id WidgetID, opt option) *container {
4141
if container, ok := c.idToContainer[id]; ok {
4242
c.addUsedContainer(id)
4343
return container
@@ -48,7 +48,7 @@ func (c *Context) container(id widgetID, opt option) *container {
4848
}
4949

5050
if c.idToContainer == nil {
51-
c.idToContainer = map[widgetID]*container{}
51+
c.idToContainer = map[WidgetID]*container{}
5252
}
5353
cnt := &container{
5454
headIdx: -1,
@@ -76,15 +76,15 @@ func (c *Context) Window(title string, rect image.Rectangle, f func(layout Conta
7676
})
7777
}
7878

79-
func (c *Context) window(title string, bounds image.Rectangle, opt option, id widgetID, f func(layout ContainerLayout)) error {
79+
func (c *Context) window(title string, bounds image.Rectangle, opt option, id WidgetID, f func(layout ContainerLayout)) error {
8080
var err error
8181
c.idScopeFromID(id, func() {
8282
err = c.doWindow(title, bounds, opt, id, f)
8383
})
8484
return err
8585
}
8686

87-
func (c *Context) doWindow(title string, bounds image.Rectangle, opt option, id widgetID, f func(layout ContainerLayout)) (err error) {
87+
func (c *Context) doWindow(title string, bounds image.Rectangle, opt option, id WidgetID, f func(layout ContainerLayout)) (err error) {
8888
cnt := c.container(id, opt)
8989
if cnt == nil || !cnt.open {
9090
return nil
@@ -296,31 +296,34 @@ func (c *Context) SetScroll(scroll image.Point) {
296296
c.currentContainer().layout.ScrollOffset = scroll
297297
}
298298

299-
func (c *container) textInputTextField(id widgetID) *textinput.Field {
299+
func (c *container) textInputTextField(id WidgetID, createIfNeeded bool) *textinput.Field {
300300
if id == emptyWidgetID {
301301
return nil
302302
}
303303
if _, ok := c.textInputTextFields[id]; !ok {
304+
if !createIfNeeded {
305+
return nil
306+
}
304307
if c.textInputTextFields == nil {
305-
c.textInputTextFields = make(map[widgetID]*textinput.Field)
308+
c.textInputTextFields = make(map[WidgetID]*textinput.Field)
306309
}
307310
c.textInputTextFields[id] = &textinput.Field{}
308311
}
309312
return c.textInputTextFields[id]
310313
}
311314

312-
func (c *container) toggled(id widgetID) bool {
315+
func (c *container) toggled(id WidgetID) bool {
313316
_, ok := c.toggledIDs[id]
314317
return ok
315318
}
316319

317-
func (c *container) toggle(id widgetID) {
320+
func (c *container) toggle(id WidgetID) {
318321
if _, toggled := c.toggledIDs[id]; toggled {
319322
delete(c.toggledIDs, id)
320323
return
321324
}
322325
if c.toggledIDs == nil {
323-
c.toggledIDs = map[widgetID]struct{}{}
326+
c.toggledIDs = map[WidgetID]struct{}{}
324327
}
325328
c.toggledIDs[id] = struct{}{}
326329
}

context.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ import (
1515
type Context struct {
1616
pointing pointing
1717

18-
scaleMinus1 int
19-
hover widgetID
20-
focus widgetID
21-
lastID widgetID
22-
lastTextFieldID widgetID
23-
lastZIndex int
24-
keepFocus bool
25-
hoverRoot *container
26-
nextHoverRoot *container
27-
scrollTarget *container
28-
numberEditBuf string
29-
numberEdit widgetID
30-
31-
idStack []widgetID
18+
scaleMinus1 int
19+
hover WidgetID
20+
focus WidgetID
21+
currentID WidgetID
22+
lastID WidgetID
23+
lastZIndex int
24+
keepFocus bool
25+
hoverRoot *container
26+
nextHoverRoot *container
27+
scrollTarget *container
28+
numberEditBuf string
29+
numberEdit WidgetID
30+
31+
idStack []WidgetID
3232
commandList []*command
3333
rootList []*container
3434
containerStack []*container
35-
usedContainers map[widgetID]struct{}
35+
usedContainers map[WidgetID]struct{}
3636
clipStack []image.Rectangle
3737
layoutStack []layout
3838

39-
idToContainer map[widgetID]*container
39+
idToContainer map[WidgetID]*container
4040

4141
lastPointingPos image.Point
4242

@@ -75,8 +75,8 @@ func (c *Context) begin() {
7575
c.scrollTarget = nil
7676
c.hoverRoot = c.nextHoverRoot
7777
c.nextHoverRoot = nil
78+
c.currentID = emptyWidgetID
7879
c.lastID = emptyWidgetID
79-
c.lastTextFieldID = emptyWidgetID
8080
}
8181

8282
func (c *Context) end() error {

draw.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func (c *Context) drawFrame(rect image.Rectangle, colorid int) {
214214
}
215215
}
216216

217-
func (c *Context) drawWidgetFrame(id widgetID, rect image.Rectangle, colorid int, opt option) {
217+
func (c *Context) drawWidgetFrame(id WidgetID, rect image.Rectangle, colorid int, opt option) {
218218
if (opt & optionNoFrame) != 0 {
219219
return
220220
}

example/ui.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func (g *Game) logWindow(ctx *debugui.Context) {
184184
submit = true
185185
}
186186
}
187+
textFieldID := ctx.CurrentWidgetID()
187188
if ctx.Button("Submit") {
188189
if g.logSubmitBuf != "" {
189190
submit = true
@@ -192,7 +193,7 @@ func (g *Game) logWindow(ctx *debugui.Context) {
192193
if submit {
193194
g.writeLog(g.logSubmitBuf)
194195
g.logSubmitBuf = ""
195-
ctx.SetTextFieldValue(g.logSubmitBuf)
196+
ctx.SetTextFieldValue(textFieldID, g.logSubmitBuf)
196197
}
197198
})
198199
})

export_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
package debugui
55

6-
type WidgetID = widgetID
7-
86
const EmptyWidgetID = emptyWidgetID
97

108
func (c *Context) IDFromCaller() WidgetID {

header.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *Context) TreeNode(label string, f func()) {
4848
})
4949
}
5050

51-
func (c *Context) header(label string, isTreeNode bool, opt option, id widgetID, f func() error) error {
51+
func (c *Context) header(label string, isTreeNode bool, opt option, id WidgetID, f func() error) error {
5252
c.SetGridLayout(nil, nil)
5353

5454
var expanded bool
@@ -97,7 +97,7 @@ func (c *Context) header(label string, isTreeNode bool, opt option, id widgetID,
9797
return nil
9898
}
9999

100-
func (c *Context) treeNode(label string, opt option, id widgetID, f func()) error {
100+
func (c *Context) treeNode(label string, opt option, id WidgetID, f func()) error {
101101
if err := c.header(label, true, opt, id, func() (err error) {
102102
l, err := c.layout()
103103
if err != nil {

helpers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ func (c *Context) Focus() {
1616
c.setFocus(c.lastID)
1717
}
1818

19-
func (c *Context) setFocus(id widgetID) {
19+
func (c *Context) setFocus(id WidgetID) {
2020
c.focus = id
2121
c.keepFocus = true
2222
}
2323

24-
func (c *Context) addUsedContainer(id widgetID) {
24+
func (c *Context) addUsedContainer(id WidgetID) {
2525
if c.usedContainers == nil {
26-
c.usedContainers = map[widgetID]struct{}{}
26+
c.usedContainers = map[WidgetID]struct{}{}
2727
}
2828
c.usedContainers[id] = struct{}{}
2929
}

id.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ func caller() uintptr {
2424
// IDScope is useful when you want to create multiple widgets at the same position e.g. in a for loop.
2525
func (c *Context) IDScope(name string, f func()) {
2626
pc := caller()
27-
c.idStack = append(c.idStack, widgetID(fmt.Sprintf("caller:%d", pc)))
28-
c.idStack = append(c.idStack, widgetID(fmt.Sprintf("string:%q", name)))
27+
c.idStack = append(c.idStack, WidgetID(fmt.Sprintf("caller:%d", pc)))
28+
c.idStack = append(c.idStack, WidgetID(fmt.Sprintf("string:%q", name)))
2929
defer func() {
3030
c.idStack = slices.Delete(c.idStack, len(c.idStack)-2, len(c.idStack))
3131
}()
3232
f()
3333
}
3434

35-
func (c *Context) idScopeFromID(id widgetID, f func()) {
35+
func (c *Context) idScopeFromID(id WidgetID, f func()) {
3636
c.idStack = append(c.idStack, id)
3737
defer func() {
3838
c.idStack = slices.Delete(c.idStack, len(c.idStack)-1, len(c.idStack))
3939
}()
4040
f()
4141
}
4242

43-
func (c *Context) idScopeToWidgetID() widgetID {
44-
var newID widgetID
43+
func (c *Context) idScopeToWidgetID() WidgetID {
44+
var newID WidgetID
4545
for _, id := range c.idStack {
4646
if len(newID) > 0 {
4747
newID += ":"
@@ -51,25 +51,25 @@ func (c *Context) idScopeToWidgetID() widgetID {
5151
return newID
5252
}
5353

54-
func (c *Context) idFromGlobalString(str string) widgetID {
55-
return widgetID(fmt.Sprintf("string:%q", str))
54+
func (c *Context) idFromGlobalString(str string) WidgetID {
55+
return WidgetID(fmt.Sprintf("string:%q", str))
5656
}
5757

58-
func (c *Context) idFromString(str string) widgetID {
58+
func (c *Context) idFromString(str string) WidgetID {
5959
newID := c.idScopeToWidgetID()
6060
if len(newID) > 0 {
6161
newID += ":"
6262
}
63-
newID += widgetID(fmt.Sprintf("string:%q", str))
63+
newID += WidgetID(fmt.Sprintf("string:%q", str))
6464
return newID
6565
}
6666

6767
// idFromCaller returns a hash value based on the caller's file and line number.
68-
func (c *Context) idFromCaller(callerPC uintptr) widgetID {
68+
func (c *Context) idFromCaller(callerPC uintptr) WidgetID {
6969
newID := c.idScopeToWidgetID()
7070
if len(newID) > 0 {
7171
newID += ":"
7272
}
73-
newID += widgetID(fmt.Sprintf("caller:%d", callerPC))
73+
newID += WidgetID(fmt.Sprintf("caller:%d", callerPC))
7474
return newID
7575
}

panel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ func (c *Context) Panel(f func(layout ContainerLayout)) {
1414
})
1515
}
1616

17-
func (c *Context) panel(opt option, id widgetID, f func(layout ContainerLayout)) (err error) {
17+
func (c *Context) panel(opt option, id WidgetID, f func(layout ContainerLayout)) (err error) {
1818
c.idScopeFromID(id, func() {
1919
err = c.doPanel(opt, id, f)
2020
})
2121
return
2222
}
2323

24-
func (c *Context) doPanel(opt option, id widgetID, f func(layout ContainerLayout)) (err error) {
24+
func (c *Context) doPanel(opt option, id WidgetID, f func(layout ContainerLayout)) (err error) {
2525
cnt := c.container(id, opt)
2626
l, err := c.layoutNext()
2727
if err != nil {

0 commit comments

Comments
 (0)