Skip to content

Commit 77e7b62

Browse files
committed
debugui: refactoring: move using layout stack from gridCell to widget
1 parent d544ded commit 77e7b62

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

layout.go

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,41 +85,16 @@ func (c *Context) popLayout() error {
8585

8686
func (c *Context) GridCell(f func(bounds image.Rectangle)) {
8787
_ = c.wrapEventHandlerAndError(func() (EventHandler, error) {
88-
if err := c.gridCell(func(bounds image.Rectangle) {
88+
if _, err := c.widget(emptyWidgetID, 0, func(bounds image.Rectangle, wasFocused bool) (e EventHandler, err error) {
8989
f(bounds)
90+
return nil, nil
9091
}); err != nil {
9192
return nil, err
9293
}
9394
return nil, nil
9495
})
9596
}
9697

97-
func (c *Context) gridCell(f func(bounds image.Rectangle)) error {
98-
_, err := c.widget(emptyWidgetID, 0, func(bounds image.Rectangle, wasFocused bool) (e EventHandler, err error) {
99-
if err := c.pushLayout(bounds, image.Pt(0, 0), false); err != nil {
100-
return nil, err
101-
}
102-
defer func() {
103-
if err2 := c.popLayout(); err2 != nil && err == nil {
104-
err = err2
105-
}
106-
}()
107-
f(bounds)
108-
b := &c.layoutStack[len(c.layoutStack)-1]
109-
// inherit position/next_row/max from child layout if they are greater
110-
a := &c.layoutStack[len(c.layoutStack)-2]
111-
a.position.X = max(a.position.X, b.position.X+b.body.Min.X-a.body.Min.X)
112-
a.nextRowY = max(a.nextRowY, b.nextRowY+b.body.Min.Y-a.body.Min.Y)
113-
a.max.X = max(a.max.X, b.max.X)
114-
a.max.Y = max(a.max.Y, b.max.Y)
115-
return nil, nil
116-
})
117-
if err != nil {
118-
return err
119-
}
120-
return nil
121-
}
122-
12398
func (c *Context) layout() (*layout, error) {
12499
if len(c.layoutStack) == 0 {
125100
return nil, errors.New("debugui: layout stack is empty; perhaps a window is absent")

widget.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,29 @@ func (c *Context) updateWidget(id WidgetID, bounds image.Rectangle, opt option)
9090

9191
func (c *Context) widget(id WidgetID, opt option, f func(bounds image.Rectangle, wasFocused bool) (EventHandler, error)) (EventHandler, error) {
9292
c.currentID = id
93-
r, err := c.layoutNext()
93+
bounds, err := c.layoutNext()
9494
if err != nil {
9595
return nil, err
9696
}
97-
wasFocused := c.updateWidget(id, r, opt)
98-
e, err := f(r, wasFocused)
97+
98+
if err := c.pushLayout(bounds, image.Pt(0, 0), false); err != nil {
99+
return nil, err
100+
}
101+
defer func() {
102+
b := &c.layoutStack[len(c.layoutStack)-1]
103+
// inherit position/next_row/max from child layout if they are greater
104+
a := &c.layoutStack[len(c.layoutStack)-2]
105+
a.position.X = max(a.position.X, b.position.X+b.body.Min.X-a.body.Min.X)
106+
a.nextRowY = max(a.nextRowY, b.nextRowY+b.body.Min.Y-a.body.Min.Y)
107+
a.max.X = max(a.max.X, b.max.X)
108+
a.max.Y = max(a.max.Y, b.max.Y)
109+
if err2 := c.popLayout(); err2 != nil && err == nil {
110+
err = err2
111+
}
112+
}()
113+
114+
wasFocused := c.updateWidget(id, bounds, opt)
115+
e, err := f(bounds, wasFocused)
99116
if err != nil {
100117
return nil, err
101118
}

0 commit comments

Comments
 (0)