Skip to content

Commit 07d3282

Browse files
committed
debugui: bug fix: wrong popup width
Closes #21
1 parent 1ca4371 commit 07d3282

File tree

4 files changed

+55
-33
lines changed

4 files changed

+55
-33
lines changed

control.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,15 @@ func (c *Context) scrollbars(cnt *container, body image.Rectangle, callerPC uint
448448
return body
449449
}
450450

451-
func (c *Context) pushContainerBodyLayout(cnt *container, body image.Rectangle, opt option, callerPC uintptr) {
451+
func (c *Context) pushContainerBodyLayout(cnt *container, body image.Rectangle, opt option, callerPC uintptr) error {
452452
if (^opt & optionNoScroll) != 0 {
453453
body = c.scrollbars(cnt, body, callerPC)
454454
}
455-
c.pushLayout(body.Inset(c.style().padding), cnt.layout.ScrollOffset)
455+
if err := c.pushLayout(body.Inset(c.style().padding), cnt.layout.ScrollOffset, opt&optionAutoSize != 0); err != nil {
456+
return err
457+
}
456458
cnt.layout.BodyBounds = body
459+
return nil
457460
}
458461

459462
// SetScale sets the scale of the UI.

layout.go

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,23 @@ func (l *layout) sizeInPixels(sizes []int, index int, defaultSize int, entireSiz
5252
return max(8, int(float64(remain)*-float64(s)/float64(denom)))
5353
}
5454

55-
func (c *Context) pushLayout(body image.Rectangle, scroll image.Point) {
55+
func (c *Context) pushLayout(body image.Rectangle, scroll image.Point, autoResize bool) error {
5656
c.layoutStack = append(c.layoutStack, layout{
5757
body: body.Sub(scroll),
5858
max: image.Pt(-0x1000000, -0x1000000),
5959
widths: []int{0},
6060
heights: []int{0},
6161
})
62-
c.SetGridLayout(nil, nil)
62+
if autoResize {
63+
if err := c.setGridLayout([]int{0}, nil); err != nil {
64+
return err
65+
}
66+
} else {
67+
if err := c.setGridLayout(nil, nil); err != nil {
68+
return err
69+
}
70+
}
71+
return nil
6372
}
6473

6574
func (c *Context) popLayout() error {
@@ -88,7 +97,9 @@ func (c *Context) GridCell(f func()) {
8897

8998
func (c *Context) gridCell(f func() error) error {
9099
_, err := c.control(emptyControlID, 0, func(bounds image.Rectangle, wasFocused bool) (res bool, err error) {
91-
c.pushLayout(bounds, image.Pt(0, 0))
100+
if err := c.pushLayout(bounds, image.Pt(0, 0), false); err != nil {
101+
return false, err
102+
}
92103
defer func() {
93104
if err2 := c.popLayout(); err2 != nil && err == nil {
94105
err = err2
@@ -127,39 +138,43 @@ func (c *Context) layout() (*layout, error) {
127138
// For example, if widths is []int{100, -1}, the first column is 100 pixels and the second column takes the remaining space.
128139
// If widths is []int{100, -1, -2}, the first column is 100 pixels, the second column takes 1/3 of the remaining space, and the third column takes 2/3 of the remaining space.
129140
//
130-
// If widths is nil, one column width with -1 is used. This is the same as []int{-1}.
131-
// If heights is nil, the default size is used. This is the same as []int{0}.
141+
// If widths is nil, one column width with -1 is used for windows, and 0 (the default size) for popups.
142+
// If heights is nil, 0 (the default size) is used.
132143
//
133144
// When the number of items exceeds the number of grid cells, a new row is started with the same grid layout.
134145
func (c *Context) SetGridLayout(widths []int, heights []int) {
135146
c.wrapError(func() error {
136-
layout, err := c.layout()
137-
if err != nil {
138-
return err
139-
}
147+
return c.setGridLayout(widths, heights)
148+
})
149+
}
140150

141-
if len(layout.widths) < len(widths) {
142-
layout.widths = append(layout.widths, make([]int, len(widths)-len(layout.widths))...)
143-
}
144-
copy(layout.widths, widths)
145-
layout.widths = layout.widths[:len(widths)]
146-
if len(layout.widths) == 0 {
147-
layout.widths = append(layout.widths, -1)
148-
}
151+
func (c *Context) setGridLayout(widths []int, heights []int) error {
152+
layout, err := c.layout()
153+
if err != nil {
154+
return err
155+
}
149156

150-
if len(layout.heights) < len(heights) {
151-
layout.heights = append(layout.heights, make([]int, len(heights)-len(layout.heights))...)
152-
}
153-
copy(layout.heights, heights)
154-
layout.heights = layout.heights[:len(heights)]
155-
if len(layout.heights) == 0 {
156-
layout.heights = append(layout.heights, 0) // TODO: This should be -1?
157-
}
157+
if len(layout.widths) < len(widths) {
158+
layout.widths = append(layout.widths, make([]int, len(widths)-len(layout.widths))...)
159+
}
160+
copy(layout.widths, widths)
161+
layout.widths = layout.widths[:len(widths)]
162+
if len(layout.widths) == 0 {
163+
layout.widths = append(layout.widths, -1)
164+
}
158165

159-
layout.position = image.Pt(layout.indent, layout.nextRowY)
160-
layout.itemIndex = 0
161-
return nil
162-
})
166+
if len(layout.heights) < len(heights) {
167+
layout.heights = append(layout.heights, make([]int, len(heights)-len(layout.heights))...)
168+
}
169+
copy(layout.heights, heights)
170+
layout.heights = layout.heights[:len(heights)]
171+
if len(layout.heights) == 0 {
172+
layout.heights = append(layout.heights, 0) // TODO: This should be -1?
173+
}
174+
175+
layout.position = image.Pt(layout.indent, layout.nextRowY)
176+
layout.itemIndex = 0
177+
return nil
163178
}
164179

165180
func (c *Context) layoutNext() (image.Rectangle, error) {

panel.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ func (c *Context) panel(name string, opt option, callerPC uintptr, f func(layout
2929
c.containerStack = append(c.containerStack, cnt)
3030
defer c.popContainer()
3131

32-
c.pushContainerBodyLayout(cnt, cnt.layout.Bounds, opt, callerPC)
32+
if err := c.pushContainerBodyLayout(cnt, cnt.layout.Bounds, opt, callerPC); err != nil {
33+
return err
34+
}
3335
defer func() {
3436
if err2 := c.popLayout(); err2 != nil && err == nil {
3537
err = err2

window.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ func (c *Context) window(title string, bounds image.Rectangle, opt option, calle
104104
return nil
105105
}
106106

107-
c.pushContainerBodyLayout(cnt, body, opt, callerPC)
107+
if err := c.pushContainerBodyLayout(cnt, body, opt, callerPC); err != nil {
108+
return err
109+
}
108110
defer func() {
109111
if err2 := c.popLayout(); err2 != nil && err == nil {
110112
err = err2

0 commit comments

Comments
 (0)