Pack() or Place() multiple times updates the config

master
Noah 2022-09-24 18:44:45 -07:00
parent 3b653e503c
commit c99e79d9b0
3 changed files with 29 additions and 11 deletions

View File

@ -13,8 +13,8 @@ type Frame struct {
BaseWidget
// Widget placement settings.
packs map[Side][]packedWidget // Packed widgets
placed []placedWidget // Placed widgets
packs map[Side][]*packedWidget // Packed widgets
placed []*placedWidget // Placed widgets
widgets []Widget
}
@ -22,7 +22,7 @@ type Frame struct {
func NewFrame(name string) *Frame {
w := &Frame{
Name: name,
packs: map[Side][]packedWidget{},
packs: map[Side][]*packedWidget{},
widgets: []Widget{},
}
w.SetBackground(render.RGBA(1, 0, 0, 0)) // invisible default BG
@ -37,7 +37,7 @@ func NewFrame(name string) *Frame {
// Setup ensures all the Frame's data is initialized and not null.
func (w *Frame) Setup() {
if w.packs == nil {
w.packs = map[Side][]packedWidget{}
w.packs = map[Side][]*packedWidget{}
}
if w.widgets == nil {
w.widgets = []Widget{}

View File

@ -31,9 +31,17 @@ func (w *Frame) Pack(child Widget, config ...Pack) {
C = config[0]
}
// Update an already placed widget.
for _, current := range w.packs[C.Side] {
if current.widget == child {
current.pack = C
return
}
}
// Initialize the pack list for this side?
if _, ok := w.packs[C.Side]; !ok {
w.packs[C.Side] = []packedWidget{}
w.packs[C.Side] = []*packedWidget{}
}
// Padding: if the user only provided Padding add it to both
@ -51,7 +59,7 @@ func (w *Frame) Pack(child Widget, config ...Pack) {
// Adopt the child widget so it can access the Frame.
child.SetParent(w)
w.packs[C.Side] = append(w.packs[C.Side], packedWidget{
w.packs[C.Side] = append(w.packs[C.Side], &packedWidget{
widget: child,
pack: C,
})
@ -63,7 +71,7 @@ func (w *Frame) Unpack(child Widget) bool {
var any = false
for side, widgets := range w.packs {
var (
replace = []packedWidget{}
replace = []*packedWidget{}
found = false
)
@ -98,8 +106,8 @@ func (w *Frame) computePacked(e render.Engine) {
// so we can expand them to fill remaining space in fixed size Frames.
maxWidth int
maxHeight int
visited = []packedWidget{}
expanded = []packedWidget{}
visited = []*packedWidget{}
expanded = []*packedWidget{}
)
// Iterate through all directions and compute how much space to

View File

@ -37,9 +37,18 @@ type placedWidget struct {
place Place
}
// Place a widget into the frame.
// Place a widget into the frame. You may call Place on a widget multiple times to update its configuration.
func (w *Frame) Place(child Widget, config Place) {
w.placed = append(w.placed, placedWidget{
// Update an already placed widget.
for _, current := range w.placed {
if current.widget == child {
current.place = config
return
}
}
// Append it.
w.placed = append(w.placed, &placedWidget{
widget: child,
place: config,
})
@ -89,6 +98,7 @@ func (w *Frame) computePlaced(e render.Engine) {
if row.place.Middle {
moveTo.Y = frameSize.H - (w.Size().H / 2) - (row.widget.Size().H / 2)
}
row.widget.MoveTo(moveTo)
row.widget.Compute(e)
}