From c99e79d9b0820243e1b988bb53946b9cca9e911d Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Sat, 24 Sep 2022 18:44:45 -0700 Subject: [PATCH] Pack() or Place() multiple times updates the config --- frame.go | 8 ++++---- frame_pack.go | 18 +++++++++++++----- frame_place.go | 14 ++++++++++++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/frame.go b/frame.go index 2e3257c..2ee5bf0 100644 --- a/frame.go +++ b/frame.go @@ -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{} diff --git a/frame_pack.go b/frame_pack.go index 9c6ed06..106f03a 100644 --- a/frame_pack.go +++ b/frame_pack.go @@ -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 diff --git a/frame_place.go b/frame_place.go index 42a1f59..b7369a1 100644 --- a/frame_place.go +++ b/frame_place.go @@ -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) }