Skip to content

Commit 0966374

Browse files
committed
debugui: use WidgetID to specify a popup instead of a name string
1 parent 99b97dc commit 0966374

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

container.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ func (c *Context) Window(title string, rect image.Rectangle, f func(layout Conta
7777
}
7878

7979
func (c *Context) window(title string, bounds image.Rectangle, opt option, id WidgetID, f func(layout ContainerLayout)) error {
80+
// A window is not a widget in the current implementation, but a window is a widget in the concept.
81+
c.currentID = id
8082
var err error
8183
c.idScopeFromID(id, func() {
8284
err = c.doWindow(title, bounds, opt, id, f)
@@ -234,10 +236,9 @@ func (c *Context) doWindow(title string, bounds image.Rectangle, opt option, id
234236
return nil
235237
}
236238

237-
func (c *Context) OpenPopup(name string) {
238-
id := c.idFromGlobalString(name)
239+
func (c *Context) OpenPopup(widgetID WidgetID) {
239240
c.wrapError(func() error {
240-
cnt := c.container(id, 0)
241+
cnt := c.container(widgetID, 0)
241242
// set as hover root so popup isn't closed in begin_window_ex()
242243
c.nextHoverRoot = cnt
243244
c.hoverRoot = c.nextHoverRoot
@@ -253,24 +254,25 @@ func (c *Context) OpenPopup(name string) {
253254
})
254255
}
255256

256-
func (c *Context) ClosePopup(name string) {
257-
id := c.idFromGlobalString(name)
257+
func (c *Context) ClosePopup(widgetID WidgetID) {
258258
c.wrapError(func() error {
259-
cnt := c.container(id, 0)
259+
cnt := c.container(widgetID, 0)
260260
cnt.open = false
261261
return nil
262262
})
263263
}
264264

265-
func (c *Context) Popup(name string, f func(layout ContainerLayout)) {
266-
id := c.idFromGlobalString(name)
265+
func (c *Context) Popup(f func(layout ContainerLayout)) WidgetID {
266+
pc := caller()
267+
id := c.idFromCaller(pc)
267268
c.wrapError(func() error {
268269
opt := optionPopup | optionAutoSize | optionNoResize | optionNoScroll | optionNoTitle | optionClosed
269-
if err := c.window(name, image.Rectangle{}, opt, id, f); err != nil {
270+
if err := c.window("", image.Rectangle{}, opt, id, f); err != nil {
270271
return err
271272
}
272273
return nil
273274
})
275+
return id
274276
}
275277

276278
func (c *Context) pushContainer(cnt *container) {

example/ui.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ func (g *Game) testWindow(ctx *debugui.Context) {
5555
if ctx.Button("Button 3") {
5656
g.writeLog("Pressed button 3")
5757
}
58-
if ctx.Button("Popup") {
59-
ctx.OpenPopup("Test Popup")
60-
}
61-
ctx.Popup("Test Popup", func(layout debugui.ContainerLayout) {
58+
popupWidgetID := ctx.Popup(func(layout debugui.ContainerLayout) {
59+
popupWidgetID := ctx.CurrentWidgetID()
6260
ctx.Button("Hello")
6361
ctx.Button("World")
6462
if ctx.Button("Close") {
65-
ctx.ClosePopup("Test Popup")
63+
ctx.ClosePopup(popupWidgetID)
6664
}
6765
})
66+
if ctx.Button("Popup") {
67+
ctx.OpenPopup(popupWidgetID)
68+
}
6869
})
6970
ctx.Header("Tree and Text", true, func() {
7071
ctx.SetGridLayout([]int{-1, -1}, nil)

id.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,6 @@ 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))
56-
}
57-
5854
func (c *Context) idFromString(str string) WidgetID {
5955
newID := c.idScopeToWidgetID()
6056
if len(newID) > 0 {

0 commit comments

Comments
 (0)