Some Updates

This commit is contained in:
2025-08-21 14:25:17 -05:00
parent a9c059b987
commit e4405a8d6b
5 changed files with 95 additions and 57 deletions

View File

@@ -43,11 +43,20 @@ const (
LFAlignV = LayoutFlag(0x1100) LFAlignV = LayoutFlag(0x1100)
) )
const (
LFAlignTopLeft = LayoutFlag(LFAlignHLeft | LFAlignVTop)
)
func (f LayoutFlag) Add(fl LayoutFlag) { f |= fl } func (f LayoutFlag) Add(fl LayoutFlag) { f |= fl }
func (f LayoutFlag) AlignH() LayoutFlag { return f & LFAlignH } func (f LayoutFlag) AlignH() LayoutFlag { return f & LFAlignH }
func (f LayoutFlag) AlignV() LayoutFlag { return f & LFAlignV } func (f LayoutFlag) AlignV() LayoutFlag { return f & LFAlignV }
func (f LayoutFlag) IsAlignH() bool { return f.AlignH() > 0 } func (f LayoutFlag) IsAlignH() bool { return f.AlignH() > 0 }
func (f LayoutFlag) IsAlignV() bool { return f.AlignV() > 0 } func (f LayoutFlag) IsAlignV() bool { return f.AlignV() > 0 }
func (f LayoutFlag) SetTopLeft() {
f.ClearAll()
f = LFAlignHLeft | LFAlignVTop
}
func (f LayoutFlag) ClearAlignH() { func (f LayoutFlag) ClearAlignH() {
f = f &^ LFAlignH f = f &^ LFAlignH
f.Add(LFAlignHCenter) f.Add(LFAlignHCenter)
@@ -57,3 +66,8 @@ func (f LayoutFlag) ClearAlignV() {
f = f &^ LFAlignV f = f &^ LFAlignV
f.Add(LFAlignVCenter) f.Add(LFAlignVCenter)
} }
func (f LayoutFlag) ClearAll() {
f.ClearAlignH()
f.ClearAlignV()
}

View File

@@ -41,6 +41,7 @@ type Button struct {
tabbable bool tabbable bool
onPressed func() bool onPressed func() bool
logger func(string, ...any)
} }
var _ Widget = (*Button)(nil) var _ Widget = (*Button)(nil)
@@ -75,6 +76,7 @@ func (w *Button) HandleKey(ev *tcell.EventKey) bool {
return false return false
} }
func (w *Button) HandleTime(ev *tcell.EventTime) {} func (w *Button) HandleTime(ev *tcell.EventTime) {}
func (w *Button) Draw(screen tcell.Screen) { func (w *Button) Draw(screen tcell.Screen) {
if !w.visible { if !w.visible {
return return
@@ -96,6 +98,7 @@ func (w *Button) Draw(screen tcell.Screen) {
lbl = fmt.Sprintf("[%s]", wh.Center(lbl, w.w-2)) lbl = fmt.Sprintf("[%s]", wh.Center(lbl, w.w-2))
} }
wh.DrawText(w.x, w.y, lbl, dStyle, screen) wh.DrawText(w.x, w.y, lbl, dStyle, screen)
return
case 2: case 2:
lbl := w.label lbl := w.label
if w.w < len(lbl) { if w.w < len(lbl) {
@@ -133,7 +136,7 @@ func (w *Button) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
func (w *Button) SetW(x int) { w.w = x } func (w *Button) SetW(x int) { w.w = x }
func (w *Button) SetH(y int) { w.h = y } func (w *Button) SetH(y int) { w.h = y }
func (w *Button) GetW() int { return w.w } func (w *Button) GetW() int { return w.w }
func (w *Button) GetH() int { return w.y } func (w *Button) GetH() int { return w.h }
func (w *Button) WantW() int { return 4 + len(w.label) } func (w *Button) WantW() int { return 4 + len(w.label) }
func (w *Button) WantH() int { return 3 } func (w *Button) WantH() int { return 3 }
func (w *Button) SetSize(c Coord) { w.w, w.h = c.X, c.Y } func (w *Button) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
@@ -145,3 +148,10 @@ func (w *Button) MinH() int { return 1 }
func (w *Button) SetLabel(l string) { w.label = l } func (w *Button) SetLabel(l string) { w.label = l }
func (w *Button) SetOnPressed(p func() bool) { w.onPressed = p } func (w *Button) SetOnPressed(p func() bool) { w.onPressed = p }
func (w *Button) SetLogger(l func(string, ...any)) { w.logger = l }
func (w *Button) Log(txt string, args ...any) {
if w.logger != nil {
w.logger(txt, args...)
}
}

View File

@@ -162,7 +162,7 @@ func (w *Field) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *Field) Focusable() bool { return true } func (w *Field) Focusable() bool { return true }
func (w *Field) SetTabbable(b bool) { w.tabbable = b } func (w *Field) SetTabbable(b bool) { w.tabbable = b }
func (w *Field) Tabbable() bool { return w.tabbable } func (w *Field) Tabbable() bool { return w.tabbable }
func (w *Field) MinW() int { return len(w.label) + 15 } func (w *Field) MinW() int { return len(w.label) + wh.Max(15, len(w.value)) }
func (w *Field) MinH() int { return 1 } func (w *Field) MinH() int { return 1 }
/* Non-Widget-Interface Functions */ /* Non-Widget-Interface Functions */

View File

@@ -22,6 +22,9 @@ THE SOFTWARE.
package widgets package widgets
import ( import (
"fmt"
"time"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers" wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
) )
@@ -40,7 +43,11 @@ type LinearLayout struct {
layoutFlags map[Widget]LayoutFlag layoutFlags map[Widget]LayoutFlag
layoutWeights map[Widget]int layoutWeights map[Widget]int
totalWeight int totalWeight int
// Stacked makes the layout ignore weights and effectively shrink-wrap all fields defFlags LayoutFlag // The default flags applied to widgets if nothing
// is in layoutFlags
// Stacked makes the layout ignore weights and effectively shrink-wrap all
// fields
stacked bool stacked bool
active bool active bool
@@ -48,7 +55,6 @@ type LinearLayout struct {
tabbable bool tabbable bool
disableTab bool disableTab bool
cursor int
logger func(string, ...any) logger func(string, ...any)
} }
@@ -72,6 +78,7 @@ func (w *LinearLayout) Init(id string, s tcell.Style) {
w.style = s w.style = s
w.visible = true w.visible = true
w.tabbable = true w.tabbable = true
w.defFlags = LayoutFlag(LFAlignHCenter | LFAlignVCenter)
w.layoutFlags = make(map[Widget]LayoutFlag) w.layoutFlags = make(map[Widget]LayoutFlag)
w.layoutWeights = make(map[Widget]int) w.layoutWeights = make(map[Widget]int)
} }
@@ -83,30 +90,32 @@ func (w *LinearLayout) HandleResize(ev *tcell.EventResize) {
} }
func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool { func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool {
w.Log("LinearLayout<%s>.HandleKey", w.Id()) if !w.active || w.disableTab {
if !w.active {
w.Log("LinearLayout<%s>.HandleKey - NOPE. Not Active.", w.Id())
return false return false
} }
// First, see if the active widget handles the key active := w.findActive()
w.Log("LinearLayout<%s>.HandleKey: Checking Active Widget...", w.Id()) if active != nil {
if len(w.widgets) > w.cursor { if active.HandleKey(ev) {
w.Log("LinearLayout<%s>.HandleKey: Widget<%s>.HandleKey", w.Id(), w.widgets[w.cursor].Id()) w.Log("%s.HandleKey %s <- Consumed by %s", w.Id(), ev.Name(), active.Id())
if w.widgets[w.cursor].HandleKey(ev) {
return true return true
} }
w.Log("LinearLayout<%s>.HandleKey: Widget<%s>.HandleKey Returns", w.Id(), w.widgets[w.cursor].Id()) w.Log("%s.HandleKey Not handled by active (%s)", w.Id(), active.Id())
} }
if ev.Key() == tcell.KeyTab {
w.Log("LinearLayout<%s>.HandleKey: Checking for Tab", w.Id()) w.Log("%s-> Activating Next (current: %s)", w.Id(), w.findActive().Id())
if ev.Key() == tcell.KeyTab && !w.disableTab { ret := w.activateNext()
w.Log("LinearLayout<%s>.HandleKey: Handling Tab", w.Id()) if ret {
return w.handleTab() w.Log("%s<- Activated: %s", w.Id(), w.findActive().Id())
} else {
w.Log("%s<- Nothing Activated", w.Id())
}
return ret
} }
return false return false
} }
func (w *LinearLayout) GetActive() Widget { return w.findActive() }
func (w *LinearLayout) HandleTime(ev *tcell.EventTime) { func (w *LinearLayout) HandleTime(ev *tcell.EventTime) {
for _, wi := range w.widgets { for _, wi := range w.widgets {
@@ -125,10 +134,7 @@ func (w *LinearLayout) Draw(screen tcell.Screen) {
} }
func (w *LinearLayout) Active() bool { return w.active } func (w *LinearLayout) Active() bool { return w.active }
func (w *LinearLayout) SetActive(a bool) { func (w *LinearLayout) SetActive(a bool) { w.active = a }
w.active = a
w.updateActive()
}
func (w *LinearLayout) Visible() bool { return w.visible } func (w *LinearLayout) Visible() bool { return w.visible }
func (w *LinearLayout) SetVisible(a bool) { w.visible = a } func (w *LinearLayout) SetVisible(a bool) { w.visible = a }
func (w *LinearLayout) Focusable() bool { return true } func (w *LinearLayout) Focusable() bool { return true }
@@ -204,14 +210,35 @@ func (w *LinearLayout) MinH() int {
return minH return minH
} }
// Find the widget at 'cursor' and set it active. // Find the currently active widget, there should be only one.
// All others to inactive func (w *LinearLayout) findActive() Widget {
// If this layout is not active, everything is inactive for i := range w.widgets {
func (w *LinearLayout) updateActive() { if w.widgets[i].Active() {
for i, wd := range w.widgets { return w.widgets[i]
wd.SetActive(i == w.cursor && w.active)
} }
} }
// Didn't find one, return the first
if len(w.widgets) > 0 {
return w.widgets[0]
}
return nil
}
func (w *LinearLayout) activateNext() bool {
var found bool
for i := range w.widgets {
if found {
w.Log("%s.activeNext Setting Next Active: %s", w.Id(), w.widgets[i].Id())
w.widgets[i].SetActive(true)
return true
} else if w.widgets[i].Active() {
found = true
w.widgets[i].SetActive(false)
}
}
w.Log("%s.activeNext Hit End", w.Id())
return false
}
func (w *LinearLayout) SetOrientation(o LinearLayoutOrient) { w.orientation = o } func (w *LinearLayout) SetOrientation(o LinearLayoutOrient) { w.orientation = o }
func (w *LinearLayout) IndexOf(n Widget) int { func (w *LinearLayout) IndexOf(n Widget) int {
@@ -323,6 +350,8 @@ func (w *LinearLayout) SetWeight(wd Widget, wt int) {
w.updateTotalWeight() w.updateTotalWeight()
} }
func (w *LinearLayout) SetDefaultFlags(f LayoutFlag) { w.defFlags = f }
func (w *LinearLayout) updateTotalWeight() { func (w *LinearLayout) updateTotalWeight() {
w.totalWeight = 0 w.totalWeight = 0
for _, v := range w.layoutWeights { for _, v := range w.layoutWeights {
@@ -363,13 +392,7 @@ func (w *LinearLayout) updateLLVWidgetSize(wd Widget) {
} }
} }
} }
if wd.Id() == "campaignform.begindate" {
w.Log("Setting %s Size: %d,%d", wd.Id(), rW, w.getWeightedH(wd))
}
wd.HandleResize((&Coord{X: rW, Y: w.getWeightedH(wd)}).ResizeEvent()) wd.HandleResize((&Coord{X: rW, Y: w.getWeightedH(wd)}).ResizeEvent())
if wd.Id() == "campaignform.begindate" {
w.Log(" %s Size: %d,%d", wd.Id(), wd.GetW(), wd.GetH())
}
} }
func (w *LinearLayout) updateLLHWidgetSize(wd Widget) { func (w *LinearLayout) updateLLHWidgetSize(wd Widget) {
@@ -410,7 +433,7 @@ func (w *LinearLayout) updateLLVWidgetPos(wd Widget) {
if w.stacked { // If we're 'stacked', set things to top/left if w.stacked { // If we're 'stacked', set things to top/left
flgs = LayoutFlag(LFAlignHLeft | LFAlignVTop) flgs = LayoutFlag(LFAlignHLeft | LFAlignVTop)
} else { } else {
flgs = LayoutFlag(LFAlignHCenter | LFAlignVCenter) flgs = w.defFlags
} }
} }
@@ -441,19 +464,19 @@ func (w *LinearLayout) updateLLHWidgetPos(wd Widget) {
} }
break break
} }
c.X += w.getWeightedW(w.widgets[i]) c.X += w.getWeightedW(w.widgets[i]) + 2
} }
// Do we have a layout flag for this widget? // Do we have a layout flag for this widget?
var ok bool var ok bool
var flgs LayoutFlag var flgs LayoutFlag
if flgs, ok = w.layoutFlags[wd]; !ok { if flgs, ok = w.layoutFlags[wd]; !ok {
flgs = LayoutFlag(LFAlignHCenter | LFAlignVCenter) flgs = w.defFlags
} }
// c.X is the left-most of this 'cell' // c.X is the left-most of this 'cell'
if wd.GetW() < w.getWeightedW(wd) { if wd.GetW() < w.getWeightedW(wd) {
// But we've got some extra space. // We have extra horizontal space.
switch flgs.AlignH() { switch flgs.AlignH() {
case LFAlignHRight: case LFAlignHRight:
c.X += w.getWeightedW(wd) - wd.GetW() c.X += w.getWeightedW(wd) - wd.GetW()
@@ -461,10 +484,15 @@ func (w *LinearLayout) updateLLHWidgetPos(wd Widget) {
c.X += (w.getWeightedW(wd) / 2) - (wd.GetW() / 2) c.X += (w.getWeightedW(wd) / 2) - (wd.GetW() / 2)
} }
} }
if wd.GetW() < w.h {
c.Y = int((float64(w.h) / 2) - (float64(wd.GetH()) / 2))
} else {
c.Y = 0 c.Y = 0
if wd.GetW() < w.h {
// We have extra vertical space.
switch flgs.AlignV() {
case LFAlignVBottom:
c.Y = w.h - wd.GetH()
case LFAlignVCenter:
c.Y = int((float64(w.h) / 2) - (float64(wd.GetH()) / 2))
}
} }
wd.SetPos(c) wd.SetPos(c)
} }
@@ -507,23 +535,12 @@ func (w *LinearLayout) getWeightedW(wd Widget) int {
} }
} }
func (w *LinearLayout) handleTab() bool {
w.Log("%s - Handling Tab", w.Id())
beg := w.cursor
// Find the next tabbable widget
w.cursor = (w.cursor + 1) % len(w.widgets)
for !w.widgets[w.cursor].Tabbable() && beg != w.cursor {
w.cursor = (w.cursor + 1) % len(w.widgets)
}
w.updateActive()
return w.cursor > 0 && w.cursor != beg
}
func (w *LinearLayout) SetStacked(s bool) { w.stacked = s } func (w *LinearLayout) SetStacked(s bool) { w.stacked = s }
func (w *LinearLayout) SetLogger(l func(string, ...any)) { w.logger = l } func (w *LinearLayout) SetLogger(l func(string, ...any)) { w.logger = l }
func (w *LinearLayout) Log(txt string, args ...any) { func (w *LinearLayout) Log(txt string, args ...any) {
if w.logger != nil { if w.logger != nil {
txt = fmt.Sprintf("%s:%s", time.Now().Format(time.TimeOnly), txt)
w.logger(txt, args...) w.logger(txt, args...)
} }
} }

View File

@@ -91,9 +91,6 @@ func (w *List) Init(id string, style tcell.Style) {
func (w *List) Id() string { return w.id } func (w *List) Id() string { return w.id }
func (w *List) HandleResize(ev *tcell.EventResize) { func (w *List) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size() w.w, w.h = ev.Size()
// Fill as much space as we're given
// w.w = wh.Min(w.w, w.WantW())
// w.h = wh.Min(w.h, w.WantH())
} }
func (w *List) HandleKey(ev *tcell.EventKey) bool { func (w *List) HandleKey(ev *tcell.EventKey) bool {