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

View File

@ -31,9 +31,17 @@ func (w *Frame) Pack(child Widget, config ...Pack) {
C = config[0] 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? // Initialize the pack list for this side?
if _, ok := w.packs[C.Side]; !ok { 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 // 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. // Adopt the child widget so it can access the Frame.
child.SetParent(w) 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, widget: child,
pack: C, pack: C,
}) })
@ -63,7 +71,7 @@ func (w *Frame) Unpack(child Widget) bool {
var any = false var any = false
for side, widgets := range w.packs { for side, widgets := range w.packs {
var ( var (
replace = []packedWidget{} replace = []*packedWidget{}
found = false 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. // so we can expand them to fill remaining space in fixed size Frames.
maxWidth int maxWidth int
maxHeight int maxHeight int
visited = []packedWidget{} visited = []*packedWidget{}
expanded = []packedWidget{} expanded = []*packedWidget{}
) )
// Iterate through all directions and compute how much space to // Iterate through all directions and compute how much space to

View File

@ -37,9 +37,18 @@ type placedWidget struct {
place Place 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) { 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, widget: child,
place: config, place: config,
}) })
@ -89,6 +98,7 @@ func (w *Frame) computePlaced(e render.Engine) {
if row.place.Middle { if row.place.Middle {
moveTo.Y = frameSize.H - (w.Size().H / 2) - (row.widget.Size().H / 2) moveTo.Y = frameSize.H - (w.Size().H / 2) - (row.widget.Size().H / 2)
} }
row.widget.MoveTo(moveTo) row.widget.MoveTo(moveTo)
row.widget.Compute(e) row.widget.Compute(e)
} }