Skip to content

Commit 92fd8b6

Browse files
committed
debugui: refactoring
1 parent 4724dec commit 92fd8b6

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

control.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ func (c *Context) Text(text string) {
208208
})
209209
}
210210

211-
func (c *Context) button(label string, opt option, callerPC uintptr) (controlID, bool, error) {
212-
id := c.idFromCaller(callerPC)
211+
func (c *Context) button(label string, opt option, id controlID) (bool, error) {
213212
res, err := c.control(id, opt, func(bounds image.Rectangle, wasFocused bool) (bool, error) {
214213
var res bool
215214
// handle click
@@ -224,9 +223,9 @@ func (c *Context) button(label string, opt option, callerPC uintptr) (controlID,
224223
return res, nil
225224
})
226225
if err != nil {
227-
return emptyControlID, false, err
226+
return false, err
228227
}
229-
return id, res, nil
228+
return res, nil
230229
}
231230

232231
// Checkbox creates a checkbox with the given boolean state and text label.
@@ -356,8 +355,7 @@ func (c *Context) sliderF(value *float64, low, high, step float64, digits int, i
356355
return res, nil
357356
}
358357

359-
func (c *Context) header(label string, isTreeNode bool, opt option, callerPC uintptr, f func() error) error {
360-
id := c.idFromCaller(callerPC)
358+
func (c *Context) header(label string, isTreeNode bool, opt option, id controlID, f func() error) error {
361359
c.SetGridLayout(nil, nil)
362360

363361
var expanded bool
@@ -406,8 +404,8 @@ func (c *Context) header(label string, isTreeNode bool, opt option, callerPC uin
406404
return nil
407405
}
408406

409-
func (c *Context) treeNode(label string, opt option, callerPC uintptr, f func()) error {
410-
if err := c.header(label, true, opt, callerPC, func() (err error) {
407+
func (c *Context) treeNode(label string, opt option, id controlID, f func()) error {
408+
if err := c.header(label, true, opt, id, func() (err error) {
411409
l, err := c.layout()
412410
if err != nil {
413411
return err

debugui_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestMultipleButtonsInForLoop(t *testing.T) {
1717
ctx.Window("Window", image.Rect(0, 0, 100, 100), func(layout debugui.ContainerLayout) {
1818
var id debugui.ControlID
1919
for range 10 {
20-
id2 := ctx.ButtonID("a")
20+
id2 := ctx.IDFromCaller()
2121
if id2 == debugui.EmptyControlID {
2222
t.Errorf("Caller() returned 0")
2323
continue
@@ -44,12 +44,12 @@ func TestMultipleButtonsOnOneLine(t *testing.T) {
4444
var d debugui.DebugUI
4545
if err := d.Update(func(ctx *debugui.Context) error {
4646
ctx.Window("Window", image.Rect(0, 0, 100, 100), func(layout debugui.ContainerLayout) {
47-
idA1 := ctx.ButtonID("a")
48-
idA2 := ctx.ButtonID("a")
47+
idA1 := ctx.IDFromCaller()
48+
idA2 := ctx.IDFromCaller()
4949
if idA1 == idA2 {
5050
t.Errorf("Button() returned the same value twice: %q", idA1)
5151
}
52-
idB1, idB2 := ctx.ButtonID("b"), ctx.ButtonID("b")
52+
idB1, idB2 := ctx.IDFromCaller(), ctx.IDFromCaller()
5353
if idB1 == idB2 {
5454
t.Errorf("Button() returned the same value twice: %q", idB1)
5555
}

export_test.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,9 @@ type ControlID = controlID
77

88
const EmptyControlID = emptyControlID
99

10-
func (c *Context) ButtonID(label string) ControlID {
10+
func (c *Context) IDFromCaller() ControlID {
1111
pc := caller()
12-
var id controlID
13-
c.wrapError(func() error {
14-
var err error
15-
id, _, err = c.button(label, optionAlignCenter, pc)
16-
if err != nil {
17-
return err
18-
}
19-
return nil
20-
})
21-
return id
12+
return c.idFromCaller(pc)
2213
}
2314

2415
func (d *DebugUI) ContainerCounter() int {

widget.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ package debugui
1212
// If you want to generate different controls with the same function call in a loop (such as a for loop), use [IDScope].
1313
func (c *Context) Button(label string) bool {
1414
pc := caller()
15+
id := c.idFromCaller(pc)
1516
var res bool
1617
c.wrapError(func() error {
1718
var err error
18-
_, res, err = c.button(label, optionAlignCenter, pc)
19+
res, err = c.button(label, optionAlignCenter, id)
1920
if err != nil {
2021
return err
2122
}
@@ -84,12 +85,13 @@ func (c *Context) SliderF(value *float64, lo, hi float64, step float64, digits i
8485
// If you want to generate different controls with the same function call in a loop (such as a for loop), use [IDScope].
8586
func (c *Context) Header(label string, initialExpansion bool, f func()) {
8687
pc := caller()
88+
id := c.idFromCaller(pc)
8789
c.wrapError(func() error {
8890
var opt option
8991
if initialExpansion {
9092
opt |= optionExpanded
9193
}
92-
if err := c.header(label, false, opt, pc, func() error {
94+
if err := c.header(label, false, opt, id, func() error {
9395
f()
9496
return nil
9597
}); err != nil {
@@ -106,8 +108,9 @@ func (c *Context) Header(label string, initialExpansion bool, f func()) {
106108
// If you want to generate different controls with the same function call in a loop (such as a for loop), use [IDScope].
107109
func (c *Context) TreeNode(label string, f func()) {
108110
pc := caller()
111+
id := c.idFromCaller(pc)
109112
c.wrapError(func() error {
110-
if err := c.treeNode(label, 0, pc, f); err != nil {
113+
if err := c.treeNode(label, 0, id, f); err != nil {
111114
return err
112115
}
113116
return nil

0 commit comments

Comments
 (0)