Skip to content

Commit e5bbff0

Browse files
committed
debugui: refactoring: put commands to each container
This change removes the jump command.
1 parent af43f66 commit e5bbff0

File tree

5 files changed

+48
-77
lines changed

5 files changed

+48
-77
lines changed

command.go

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import (
1212
)
1313

1414
const (
15-
commandJump = 1 + iota
16-
commandClip
15+
commandClip = 1 + iota
1716
commandRect
1817
commandText
1918
commandIcon
@@ -55,38 +54,35 @@ type drawCommand struct {
5554

5655
type command struct {
5756
typ int
58-
idx int
5957
base baseCommand // type 0 (TODO)
60-
jump jumpCommand // type 1
61-
clip clipCommand // type 2
62-
rect rectCommand // type 3
63-
text textCommand // type 4
64-
icon iconCommand // type 5
65-
draw drawCommand // type 6
58+
clip clipCommand // type 1
59+
rect rectCommand // type 2
60+
text textCommand // type 3
61+
icon iconCommand // type 4
62+
draw drawCommand // type 5
6663
}
6764

6865
// appendCommand adds a new command with type cmd_type to the command list.
69-
func (c *Context) appendCommand(cmd_type int) *command {
66+
func (c *Context) appendCommand(cmdType int) *command {
7067
cmd := command{
71-
typ: cmd_type,
68+
typ: cmdType,
7269
}
73-
cmd.base.typ = cmd_type
74-
cmd.idx = len(c.commandList)
75-
c.commandList = append(c.commandList, &cmd)
70+
cmd.base.typ = cmdType
71+
cnt := c.currentRootContainer()
72+
cnt.commandList = append(cnt.commandList, &cmd)
7673
return &cmd
7774
}
7875

79-
// appendJumpCommand appends a new jump command to the command list.
80-
// dstIdx is set to -1. This can be updated later.
81-
func (c *Context) appendJumpCommand() int {
82-
cmd := c.appendCommand(commandJump)
83-
cmd.jump.dstIdx = -1
84-
return len(c.commandList) - 1
85-
}
86-
8776
func (c *Context) commands() iter.Seq[*command] {
8877
return func(yield func(command *command) bool) {
89-
if len(c.commandList) == 0 {
78+
for _, cnt := range c.rootContainers {
79+
for _, cmd := range cnt.commandList {
80+
if !yield(cmd) {
81+
return
82+
}
83+
}
84+
}
85+
/*if len(c.commandList) == 0 {
9086
return
9187
}
9288
@@ -104,6 +100,6 @@ func (c *Context) commands() iter.Seq[*command] {
104100
return
105101
}
106102
cmd = c.commandList[idx]
107-
}
103+
}*/
108104
}
109105
}

container.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ import (
1010
)
1111

1212
type container struct {
13+
parent *container
14+
1315
layout ContainerLayout
14-
headIdx int
15-
tailIdx int
1616
zIndex int
1717
open bool
1818
collapsed bool
1919

20+
// commandList is valid only for root containers.
21+
// See the implementation of appendCommand which is the only place to append commands.
22+
commandList []*command
23+
2024
toggledIDs map[WidgetID]struct{}
2125
textInputTextFields map[WidgetID]*textinput.Field
2226
}
@@ -51,9 +55,7 @@ func (c *Context) container(id WidgetID, opt option) *container {
5155
c.idToContainer = map[WidgetID]*container{}
5256
}
5357
cnt := &container{
54-
headIdx: -1,
55-
tailIdx: -1,
56-
open: true,
58+
open: true,
5759
}
5860
c.idToContainer[id] = cnt
5961
c.addUsedContainer(id)
@@ -65,6 +67,13 @@ func (c *Context) currentContainer() *container {
6567
return c.containerStack[len(c.containerStack)-1]
6668
}
6769

70+
func (c *Context) currentRootContainer() *container {
71+
var cnt *container
72+
for cnt = c.currentContainer(); cnt != nil && cnt.parent != nil; cnt = cnt.parent {
73+
}
74+
return cnt
75+
}
76+
6877
func (c *Context) Window(title string, rect image.Rectangle, f func(layout ContainerLayout)) {
6978
pc := caller()
7079
id := c.idFromCaller(pc)
@@ -95,19 +104,10 @@ func (c *Context) doWindow(title string, bounds image.Rectangle, opt option, id
95104
cnt.layout.Bounds = bounds
96105
}
97106

98-
c.pushContainer(cnt)
107+
c.pushContainer(cnt, true)
99108
defer c.popContainer()
100109

101-
// push container to roots list and push head command
102-
c.rootList = append(c.rootList, cnt)
103-
cnt.headIdx = c.appendJumpCommand()
104-
defer func() {
105-
// push tail 'goto' jump command and set head 'skip' command. the final steps
106-
// on initing these are done in End
107-
cnt := c.currentContainer()
108-
cnt.tailIdx = c.appendJumpCommand()
109-
c.commandList[cnt.headIdx].jump.dstIdx = len(c.commandList) //- 1
110-
}()
110+
c.rootContainers = append(c.rootContainers, cnt)
111111

112112
// set as hover root if the pointing device is overlapping this container and it has a
113113
// higher zindex than the current hover root
@@ -275,7 +275,10 @@ func (c *Context) Popup(f func(layout ContainerLayout)) WidgetID {
275275
return id
276276
}
277277

278-
func (c *Context) pushContainer(cnt *container) {
278+
func (c *Context) pushContainer(cnt *container, root bool) {
279+
if !root && len(c.containerStack) > 0 {
280+
cnt.parent = c.containerStack[len(c.containerStack)-1]
281+
}
279282
c.containerStack = append(c.containerStack, cnt)
280283
}
281284

context.go

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ type Context struct {
2828
numberEdit WidgetID
2929

3030
idStack []WidgetID
31-
commandList []*command
32-
rootList []*container
31+
rootContainers []*container
3332
containerStack []*container
3433
usedContainers map[WidgetID]struct{}
3534
clipStack []image.Rectangle
@@ -69,8 +68,10 @@ func (c *Context) update(f func(ctx *Context) error) (err error) {
6968
}
7069

7170
func (c *Context) begin() {
72-
c.commandList = slices.Delete(c.commandList, 0, len(c.commandList))
73-
c.rootList = slices.Delete(c.rootList, 0, len(c.rootList))
71+
for _, cnt := range c.rootContainers {
72+
cnt.commandList = slices.Delete(cnt.commandList, 0, len(cnt.commandList))
73+
}
74+
c.rootContainers = slices.Delete(c.rootContainers, 0, len(c.rootContainers))
7475
c.scrollTarget = nil
7576
c.hoverRoot = c.nextHoverRoot
7677
c.nextHoverRoot = nil
@@ -116,34 +117,10 @@ func (c *Context) end() error {
116117
c.lastPointingPos = c.pointingPosition()
117118

118119
// sort root containers by zindex
119-
sort.SliceStable(c.rootList, func(i, j int) bool {
120-
return c.rootList[i].zIndex < c.rootList[j].zIndex
120+
sort.SliceStable(c.rootContainers, func(i, j int) bool {
121+
return c.rootContainers[i].zIndex < c.rootContainers[j].zIndex
121122
})
122123

123-
// set root container jump commands
124-
for i := range c.rootList {
125-
cnt := c.rootList[i]
126-
// if this is the first container then make the first command jump to it.
127-
// otherwise set the previous container's tail to jump to this one
128-
if i == 0 {
129-
cmd := c.commandList[0]
130-
if cmd.typ != commandJump {
131-
panic("debugui: expected jump command")
132-
}
133-
cmd.jump.dstIdx = cnt.headIdx + 1
134-
if cnt.headIdx >= len(c.commandList) {
135-
panic("debugui: invalid head index")
136-
}
137-
} else {
138-
prev := c.rootList[i-1]
139-
c.commandList[prev.tailIdx].jump.dstIdx = cnt.headIdx + 1
140-
}
141-
// make the last container's tail jump to the end of command list
142-
if i == len(c.rootList)-1 {
143-
c.commandList[cnt.tailIdx].jump.dstIdx = len(c.commandList)
144-
}
145-
}
146-
147124
for id := range c.idToContainer {
148125
if _, ok := c.usedContainers[id]; !ok {
149126
delete(c.idToContainer, id)

panel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (c *Context) doPanel(opt option, id WidgetID, f func(layout ContainerLayout
3232
c.drawFrame(cnt.layout.Bounds, colorPanelBG)
3333
}
3434

35-
c.pushContainer(cnt)
35+
c.pushContainer(cnt, false)
3636
defer c.popContainer()
3737

3838
if err := c.pushContainerBodyLayout(cnt, cnt.layout.Bounds, opt); err != nil {

widget.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ func (c *Context) inHoverRoot() bool {
3737
if c.containerStack[i] == c.hoverRoot {
3838
return true
3939
}
40-
// only root containers have their `head` field set; stop searching if we've
41-
// reached the current root container
42-
if c.containerStack[i].headIdx >= 0 {
43-
break
44-
}
4540
}
4641
return false
4742
}

0 commit comments

Comments
 (0)