A few tweaks

This commit is contained in:
2025-08-20 10:53:18 -05:00
parent 599554a798
commit 0374e89b4f
5 changed files with 208 additions and 177 deletions

View File

@@ -74,12 +74,8 @@ func (w *Field) Init(id string, style tcell.Style) {
w.tabbable = true w.tabbable = true
} }
func (w *Field) Id() string { return w.id } func (w *Field) Id() string { return w.id }
func (w *Field) HandleResize(ev *tcell.EventResize) { func (w *Field) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size() }
w.w, w.h = ev.Size()
// w.w = wh.Min(w.w, w.WantW())
// w.h = wh.Min(w.h, w.WantH())
}
func (w *Field) HandleKey(ev *tcell.EventKey) bool { func (w *Field) HandleKey(ev *tcell.EventKey) bool {
if !w.active { if !w.active {
@@ -152,7 +148,11 @@ func (w *Field) SetH(h int) { w.h = h }
func (w *Field) GetW() int { return w.w } func (w *Field) GetW() int { return w.w }
func (w *Field) GetH() int { return w.h } func (w *Field) GetH() int { return w.h }
func (w *Field) WantW() int { func (w *Field) WantW() int {
return len(w.label) + 2 + len(w.value) + 1 if len(w.label) > 0 {
return len(w.label) + 2 + len(w.value) + 1
} else {
return len(w.value)
}
} }
func (w *Field) WantH() int { func (w *Field) WantH() int {

View File

@@ -352,28 +352,36 @@ func (w *LinearLayout) updateWidgetLayouts() {
// We need to determine the allowed size of this widget so we can determine // We need to determine the allowed size of this widget so we can determine
// it's position // it's position
func (w *LinearLayout) updateLLVWidgetSize(wd Widget) { func (w *LinearLayout) updateLLVWidgetSize(wd Widget) {
if w.stacked {
return
}
rW := w.w rW := w.w
if wd == w.widgets[len(w.widgets)-1] { if w.stacked {
wrk := float64(w.w) / float64(w.totalWeight) rW = wd.MinW()
if wrk == float64((w.w / w.totalWeight)) { } else {
rW -= 1 if wd == w.widgets[len(w.widgets)-1] {
wrk := float64(w.w) / float64(w.totalWeight)
if wrk == float64((w.w / w.totalWeight)) {
rW -= 1
}
} }
} }
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) {
if w.stacked {
return
}
rH := w.h rH := w.h
if wd == w.widgets[len(w.widgets)-1] { if w.stacked {
wrk := float64(w.h) / float64(w.totalWeight) rH = wd.MinH()
if wrk == float64((w.h / w.totalWeight)) { } else {
rH -= 1 if wd == w.widgets[len(w.widgets)-1] {
wrk := float64(w.h) / float64(w.totalWeight)
if wrk == float64((w.h / w.totalWeight)) {
rH -= 1
}
} }
} }
wd.HandleResize((&Coord{X: w.getWeightedW(wd), Y: rH}).ResizeEvent()) wd.HandleResize((&Coord{X: w.getWeightedW(wd), Y: rH}).ResizeEvent())
@@ -399,7 +407,11 @@ func (w *LinearLayout) updateLLVWidgetPos(wd 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) if w.stacked { // If we're 'stacked', set things to top/left
flgs = LayoutFlag(LFAlignHLeft | LFAlignVTop)
} else {
flgs = LayoutFlag(LFAlignHCenter | LFAlignVCenter)
}
} }
// c.Y is the top of this 'cell' // c.Y is the top of this 'cell'
@@ -460,6 +472,8 @@ func (w *LinearLayout) updateLLHWidgetPos(wd Widget) {
func (w *LinearLayout) getWeightedH(wd Widget) int { func (w *LinearLayout) getWeightedH(wd Widget) int {
if !w.Contains(wd) { if !w.Contains(wd) {
return 0 return 0
} else if w.stacked {
return wd.MinH()
} }
wght := w.layoutWeights[wd] wght := w.layoutWeights[wd]
if wght == 0 { if wght == 0 {
@@ -477,6 +491,8 @@ func (w *LinearLayout) getWeightedH(wd Widget) int {
func (w *LinearLayout) getWeightedW(wd Widget) int { func (w *LinearLayout) getWeightedW(wd Widget) int {
if !w.Contains(wd) { if !w.Contains(wd) {
return 0 return 0
} else if w.stacked {
return wd.MinW()
} }
wght := w.layoutWeights[wd] wght := w.layoutWeights[wd]
if wght == 0 { if wght == 0 {

View File

@@ -39,11 +39,12 @@ type Searcher struct {
visible bool visible bool
tabbable bool tabbable bool
title string title string
search *Field search *Field
data []string data []string
filteredData []string filteredData []string
cursor int cursor int
disableBorder bool
selectFunc func(idx int, s string) bool selectFunc func(idx int, s string) bool
hideOnSelect bool hideOnSelect bool
@@ -87,6 +88,9 @@ func (w *Searcher) HandleResize(ev *tcell.EventResize) {
w.Log("Searcher<%s>.HandleResize: %d,%d", w.Id(), w.w, w.h) w.Log("Searcher<%s>.HandleResize: %d,%d", w.Id(), w.w, w.h)
// Remove 2 from each for borders // Remove 2 from each for borders
aW, aH := w.w-2, w.h-2 aW, aH := w.w-2, w.h-2
if !w.disableBorder {
aW, aH = aW-2, aH-2
}
w.search.SetPos(Coord{X: 1, Y: 1}) w.search.SetPos(Coord{X: 1, Y: 1})
w.search.HandleResize(Coord{X: aW, Y: aH}.ResizeEvent()) w.search.HandleResize(Coord{X: aW, Y: aH}.ResizeEvent())
} }
@@ -174,11 +178,15 @@ func (w *Searcher) Draw(screen tcell.Screen) {
return return
} }
dStyle := w.style.Dim(!w.active) dStyle := w.style.Dim(!w.active)
if len(w.title) > 0 { if !w.disableBorder {
wh.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h-1, w.title, wh.BRD_CSIMPLE, dStyle, screen) if len(w.title) > 0 {
// w.Log("Searcher<%s> Bounds: %d,%d -> %d,%d", w.Id(), w.x, w.y, w.x+w.w, w.y+w.h) wh.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h-1, w.title, wh.BRD_CSIMPLE, dStyle, screen)
} else { // w.Log("Searcher<%s> Bounds: %d,%d -> %d,%d", w.Id(), w.x, w.y, w.x+w.w, w.y+w.h)
wh.BorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, wh.BRD_CSIMPLE, dStyle, screen) } else {
wh.BorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, wh.BRD_CSIMPLE, dStyle, screen)
}
} else if len(w.title) > 0 {
// TODO: Output the label
} }
w.GetPos().DrawOffset(w.search, screen) w.GetPos().DrawOffset(w.search, screen)
x, y := w.x+1, w.y+2 x, y := w.x+1, w.y+2
@@ -226,7 +234,11 @@ func (w *Searcher) WantW() int {
} }
func (w *Searcher) WantH() int { func (w *Searcher) WantH() int {
return 2 + w.search.WantH() + len(w.filteredData) // Border + Field + Data ret := w.search.WantH() + len(w.filteredData)
if !w.disableBorder {
return ret + 2
}
return ret
} }
func (w *Searcher) GetPos() Coord { return Coord{X: w.x, Y: w.y} } func (w *Searcher) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
@@ -288,9 +300,8 @@ func (w *Searcher) updateFilter() {
w.cursor = 0 w.cursor = 0
} }
func (w *Searcher) SelectedValue() string { func (w *Searcher) SelectedValue() string { return w.filteredData[w.cursor] }
return w.filteredData[w.cursor] func (w *Searcher) SelectedIndex() int { return w.cursor }
}
func (w *Searcher) SetSearchValue(val string) { func (w *Searcher) SetSearchValue(val string) {
w.search.SetValue(val) w.search.SetValue(val)
@@ -312,3 +323,5 @@ func (w *Searcher) Log(txt string, args ...any) {
w.logger(txt, args...) w.logger(txt, args...)
} }
} }
func (w *Searcher) DisableBorder(b bool) { w.disableBorder = b }

View File

@@ -29,6 +29,7 @@ import (
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
) )
// TODO: Fix this, it's not drawing correctly.
type TimeField struct { type TimeField struct {
id string id string
label string label string
@@ -44,12 +45,17 @@ type TimeField struct {
hr, mn, sc int hr, mn, sc int
value time.Time value time.Time
hasDate bool dateFormat string
hasTime bool
hasSeconds bool
showNowBtn bool fldYear *Field
nowBtnActive bool fldMonth *Field
fldDay *Field
fldHour *Field
fldMinute *Field
fldSecond *Field
btnNow *Button
widgets []Widget
cursor int cursor int
keyMap KeyMap keyMap KeyMap
@@ -69,14 +75,21 @@ func NewTimeField(id string, style tcell.Style) *TimeField {
func (w *TimeField) Init(id string, style tcell.Style) { func (w *TimeField) Init(id string, style tcell.Style) {
w.id = id w.id = id
w.style = style w.style = style
w.hasDate = true w.fldYear = NewField(fmt.Sprintf("%s.fldyear", id), style)
w.hasTime = true w.fldMonth = NewField(fmt.Sprintf("%s.fldmonth", id), style)
w.showNowBtn = true w.fldDay = NewField(fmt.Sprintf("%s.fldday", id), style)
w.fldHour = NewField(fmt.Sprintf("%s.fldhour", id), style)
w.fldMinute = NewField(fmt.Sprintf("%s.fldminute", id), style)
w.fldSecond = NewField(fmt.Sprintf("%s.fldsecond", id), style)
w.btnNow = NewButton(fmt.Sprintf("%s.btnnow", id), style)
w.widgets = append(w.widgets,
w.fldYear, w.fldMonth, w.fldDay,
w.fldHour, w.fldMinute, w.fldSecond, w.btnNow,
)
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{ w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
tcell.KeyLeft: w.handleLeft, tcell.KeyHome: w.handleHome,
tcell.KeyRight: w.handleRight, tcell.KeyEnd: w.handleEnd,
tcell.KeyHome: w.handleHome,
tcell.KeyEnd: w.handleEnd,
}) })
w.visible = true w.visible = true
w.tabbable = true w.tabbable = true
@@ -91,21 +104,50 @@ func (w *TimeField) HandleResize(ev *tcell.EventResize) {
if w.h > w.WantH() { if w.h > w.WantH() {
w.h = w.WantH() w.h = w.WantH()
} }
x, y := w.x+1, w.y
if len(w.label) > 0 {
y += 1
}
if w.fldYear.Visible() {
w.fldYear.SetPos(Coord{X: x, Y: y})
w.fldYear.SetSize(Coord{X: 4, Y: 1})
x += 5 // year + '-'
w.fldMonth.SetPos(Coord{X: x, Y: y})
w.fldMonth.SetSize(Coord{X: 2, Y: 1})
x += 3 // month + '-'
w.fldDay.SetPos(Coord{X: x, Y: y})
w.fldDay.SetSize(Coord{X: 2, Y: 1})
x += 3 // day + ' '
}
if w.fldHour.Visible() {
w.fldHour.SetPos(Coord{X: x, Y: y})
w.fldHour.SetSize(Coord{X: 2, Y: 1})
x += 3 // hour + ':'
w.fldMinute.SetPos(Coord{X: x, Y: y})
w.fldMinute.SetSize(Coord{X: 2, Y: 1})
x += 3 // mm + ':' or ' '
if w.fldSecond.Visible() {
w.fldSecond.SetPos(Coord{X: x, Y: y})
w.fldSecond.SetSize(Coord{X: 2, Y: 1})
x += 3 // ss + ' '
}
}
if w.btnNow.Visible() {
w.btnNow.SetPos(Coord{X: x, Y: y})
w.btnNow.SetSize(Coord{X: 7, Y: 1})
}
} }
func (w *TimeField) HandleKey(ev *tcell.EventKey) bool { func (w *TimeField) HandleKey(ev *tcell.EventKey) bool {
if !w.active { if !w.active {
return false return false
} else if ev.Key() == tcell.KeyTab {
return w.handleTab(ev)
} else if w.keyMap.Handle(ev) {
return true
} else if w.cursor <= len(w.widgets) {
return w.widgets[w.cursor].HandleKey(ev)
} }
switch w.cursor {
case 0: // year
case 1: // month
case 2: // day
case 3: // hour
case 4: // minute
case 5: // second
}
return false return false
} }
func (w *TimeField) HandleTime(ev *tcell.EventTime) {} func (w *TimeField) HandleTime(ev *tcell.EventTime) {}
@@ -114,66 +156,14 @@ func (w *TimeField) Draw(screen tcell.Screen) {
return return
} }
dS := w.style.Dim(!w.active) dS := w.style.Dim(!w.active)
if len(w.label) > 0 {
// pos := w.GetPos()
x := w.x
y := w.y
labelW := len(w.label)
if labelW > 0 {
wh.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.label, wh.BRD_SIMPLE, dS, screen) wh.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.label, wh.BRD_SIMPLE, dS, screen)
} else { } else {
wh.BorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, wh.BRD_SIMPLE, dS, screen) wh.BorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, wh.BRD_SIMPLE, dS, screen)
} }
x, y = x+1, y+1 pos := w.GetPos()
if w.hasDate { for _, wd := range w.widgets {
yr, mo, dy := w.value.Year(), w.value.Month(), w.value.Day() pos.DrawOffset(wd, screen)
for idx, vl := range fmt.Sprintf("%4d%2d%2d", yr, mo, dy) {
if idx == 4 || idx == 7 {
wh.DrawText(x, y, "-", dS, screen)
x++
}
if idx == w.cursor && !w.nowBtnActive {
if w.active {
wh.DrawText(x, y, string(vl), dS.Reverse(true).Blink(true), screen)
} else {
wh.DrawText(x, y, string(vl), dS, screen)
}
} else {
wh.DrawText(x, y, string(vl), dS, screen)
}
x++
}
}
if w.hasTime {
hr, mn, sc := w.value.Hour(), w.value.Minute(), w.value.Second()
txt := fmt.Sprintf("%2d%2d", hr, mn)
if w.hasSeconds {
txt = fmt.Sprintf("%s%2d", txt, sc)
}
for idx, vl := range txt {
if idx == 2 || idx == 5 {
wh.DrawText(x, y, ":", dS, screen)
x++
}
if idx+8 == w.cursor && !w.nowBtnActive {
if w.active {
wh.DrawText(x, y, string(vl), dS.Reverse(true).Blink(true), screen)
} else {
wh.DrawText(x, y, string(vl), dS, screen)
}
} else {
wh.DrawText(x, y, string(vl), dS, screen)
}
x++
}
}
if w.showNowBtn {
x, y = x+w.w-8, y+1
if w.nowBtnActive {
wh.DrawText(x, y, "[ Now ]", dS.Reverse(true), screen)
} else {
wh.DrawText(x, y, "[ Now ]", dS, screen)
}
} }
} }
@@ -195,101 +185,110 @@ func (w *TimeField) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *TimeField) Focusable() bool { return true } func (w *TimeField) Focusable() bool { return true }
func (w *TimeField) SetTabbable(b bool) { w.tabbable = b } func (w *TimeField) SetTabbable(b bool) { w.tabbable = b }
func (w *TimeField) Tabbable() bool { return w.tabbable } func (w *TimeField) Tabbable() bool { return w.tabbable }
func (w *TimeField) WantW() int { func (w *TimeField) WantW() int { return w.MinW() }
wdt := 0 func (w *TimeField) WantH() int { return w.MinH() }
if w.hasDate {
wdt = 10 // yyyy-mm-dd
}
if w.hasTime {
if w.hasDate {
wdt += 1 // space between date & time
}
if w.hasSeconds {
wdt += 8 // hh:mm:ss
} else {
wdt += 5 // hh:mm
}
}
return wdt + 2
}
func (w *TimeField) WantH() int { return 3 }
func (w *TimeField) MinW() int { func (w *TimeField) MinW() int {
wdt := 0 wdt := 0
if w.hasDate { if w.fldYear.Visible() {
wdt = 10 // yyyy-mm-dd wdt = 10 // yyyy-mm-dd
} }
if w.hasTime { if w.fldSecond.Visible() {
if w.hasDate { if wdt > 10 {
wdt += 1 // space between date & time wdt += 1
} }
if w.hasSeconds { wdt += 8 // hh:mm:ss
wdt += 8 // hh:mm:ss } else if w.fldHour.Visible() {
} else { if wdt > 10 {
wdt += 5 // hh:mm wdt += 1
} }
wdt += 5 // hh:mm
} }
return wdt + 2 if w.btnNow.Visible() {
wdt += w.btnNow.MinW()
}
return wdt
} }
func (w *TimeField) MinH() int { return 3 } func (w *TimeField) MinH() int { return 3 }
func (w *TimeField) SetLabel(lbl string) { w.label = lbl } func (w *TimeField) SetLabel(lbl string) { w.label = lbl }
func (w *TimeField) SetTime(tm time.Time) { w.value = tm } func (w *TimeField) SetTime(tm time.Time) { w.value = tm }
func (w *TimeField) SetHasSeconds(b bool) { w.hasSeconds = b } func (w *TimeField) SetHasTime(hrmn, sec bool) {
func (w *TimeField) SetHasTime(b bool) { w.hasTime = b } w.fldHour.SetVisible(hrmn)
func (w *TimeField) SetHasDate(b bool) { w.hasDate = b } w.fldMinute.SetVisible(hrmn)
w.fldSecond.SetVisible(hrmn && sec)
}
func (w *TimeField) SetHasDate(b bool) {
w.fldYear.SetVisible(b)
w.fldMonth.SetVisible(b)
w.fldDay.SetVisible(b)
}
func (w *TimeField) SetHasNowBtn(b bool) { w.btnNow.SetVisible(b) }
func (w *TimeField) SetValue(v time.Time) { w.value = v } func (w *TimeField) SetValue(v time.Time) { w.value = v }
func (w *TimeField) Value() time.Time { return w.value } func (w *TimeField) Value() time.Time { return w.value }
func (w *TimeField) handleLeft(ev *tcell.EventKey) bool { func (w *TimeField) handleTab(ev *tcell.EventKey) bool {
if w.nowBtnActive { if !w.visible {
w.cursor = w.cursorLength() return false
return true
} else if w.cursor > 0 {
w.cursor--
return true
} }
return false beg := w.cursor
} w.cursor += 1
for !w.widgets[w.cursor].Visible() && w.cursor != beg {
func (w *TimeField) handleRight(ev *tcell.EventKey) bool { w.cursor += 1
if w.cursor < w.cursorLength() {
w.cursor++
return true
} else if !w.nowBtnActive {
w.nowBtnActive = true
return true
} }
return false for idx, wd := range w.widgets {
wd.SetActive(idx == w.cursor)
}
if w.cursor != beg { // we didn't actually change
return false
} else if w.cursor < beg { // we looped
return false
}
return true
} }
func (w *TimeField) handleHome(ev *tcell.EventKey) bool { func (w *TimeField) handleHome(ev *tcell.EventKey) bool {
if w.cursor != 0 { if !w.visible {
w.cursor = 0 return false
return true }
for i := 0; i < len(w.widgets); i++ {
if w.widgets[i].Visible() {
w.cursor = i
w.updateActive()
return true
}
} }
return false return false
} }
func (w *TimeField) handleEnd(ev *tcell.EventKey) bool { func (w *TimeField) handleEnd(ev *tcell.EventKey) bool {
l := w.cursorLength() if !w.visible {
if w.cursor != l { return false
w.cursor = w.cursorLength() }
return true for i := len(w.widgets) - 1; i >= 0; i-- {
if w.widgets[i].Visible() {
w.cursor = i
w.updateActive()
return true
}
} }
return false return false
} }
func (w *TimeField) cursorLength() int { func (w *TimeField) updateActive() {
var ret int for idx, wd := range w.widgets {
if w.hasDate { wd.SetActive(idx == w.cursor)
ret += 8
} }
if w.hasTime { }
ret += 4
if w.hasSeconds { func (w *TimeField) totalFields() int {
ret += 2 var ret int
for i := range w.widgets {
if w.widgets[i].Visible() {
ret += 1
} }
} }
return ret return ret

View File

@@ -57,6 +57,7 @@ func (w *TopMenuLayout) Init(id string, s tcell.Style) {
w.id = id w.id = id
w.style = s w.style = s
w.visible = true w.visible = true
w.SetActive(true)
w.menu = NewMenu(fmt.Sprintf("%s.mainmenu", id), tcell.StyleDefault) w.menu = NewMenu(fmt.Sprintf("%s.mainmenu", id), tcell.StyleDefault)
w.menu.SetActive(false) w.menu.SetActive(false)
@@ -97,7 +98,9 @@ func (w *TopMenuLayout) HandleKey(ev *tcell.EventKey) bool {
// Pass the key through to the main widget // Pass the key through to the main widget
if w.widget != nil { if w.widget != nil {
w.Log(" Passing Key to Main Widget") // If we're here, that means the menu isn't active, but we are. So the
// widget should be.
w.widget.SetActive(true)
return w.widget.HandleKey(ev) return w.widget.HandleKey(ev)
} }
return false return false