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
}
func (w *Field) Id() string { return w.id }
func (w *Field) HandleResize(ev *tcell.EventResize) {
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) Id() string { return w.id }
func (w *Field) HandleResize(ev *tcell.EventResize) { w.w, w.h = ev.Size() }
func (w *Field) HandleKey(ev *tcell.EventKey) bool {
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) GetH() int { return w.h }
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 {

View File

@@ -352,28 +352,36 @@ func (w *LinearLayout) updateWidgetLayouts() {
// We need to determine the allowed size of this widget so we can determine
// it's position
func (w *LinearLayout) updateLLVWidgetSize(wd Widget) {
if w.stacked {
return
}
rW := w.w
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 w.stacked {
rW = wd.MinW()
} else {
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())
if wd.Id() == "campaignform.begindate" {
w.Log(" %s Size: %d,%d", wd.Id(), wd.GetW(), wd.GetH())
}
}
func (w *LinearLayout) updateLLHWidgetSize(wd Widget) {
if w.stacked {
return
}
rH := w.h
if wd == w.widgets[len(w.widgets)-1] {
wrk := float64(w.h) / float64(w.totalWeight)
if wrk == float64((w.h / w.totalWeight)) {
rH -= 1
if w.stacked {
rH = wd.MinH()
} else {
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())
@@ -399,7 +407,11 @@ func (w *LinearLayout) updateLLVWidgetPos(wd Widget) {
var ok bool
var flgs LayoutFlag
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'
@@ -460,6 +472,8 @@ func (w *LinearLayout) updateLLHWidgetPos(wd Widget) {
func (w *LinearLayout) getWeightedH(wd Widget) int {
if !w.Contains(wd) {
return 0
} else if w.stacked {
return wd.MinH()
}
wght := w.layoutWeights[wd]
if wght == 0 {
@@ -477,6 +491,8 @@ func (w *LinearLayout) getWeightedH(wd Widget) int {
func (w *LinearLayout) getWeightedW(wd Widget) int {
if !w.Contains(wd) {
return 0
} else if w.stacked {
return wd.MinW()
}
wght := w.layoutWeights[wd]
if wght == 0 {

View File

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

View File

@@ -29,6 +29,7 @@ import (
"github.com/gdamore/tcell"
)
// TODO: Fix this, it's not drawing correctly.
type TimeField struct {
id string
label string
@@ -44,12 +45,17 @@ type TimeField struct {
hr, mn, sc int
value time.Time
hasDate bool
hasTime bool
hasSeconds bool
dateFormat string
showNowBtn bool
nowBtnActive bool
fldYear *Field
fldMonth *Field
fldDay *Field
fldHour *Field
fldMinute *Field
fldSecond *Field
btnNow *Button
widgets []Widget
cursor int
keyMap KeyMap
@@ -69,14 +75,21 @@ func NewTimeField(id string, style tcell.Style) *TimeField {
func (w *TimeField) Init(id string, style tcell.Style) {
w.id = id
w.style = style
w.hasDate = true
w.hasTime = true
w.showNowBtn = true
w.fldYear = NewField(fmt.Sprintf("%s.fldyear", id), style)
w.fldMonth = NewField(fmt.Sprintf("%s.fldmonth", id), style)
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{
tcell.KeyLeft: w.handleLeft,
tcell.KeyRight: w.handleRight,
tcell.KeyHome: w.handleHome,
tcell.KeyEnd: w.handleEnd,
tcell.KeyHome: w.handleHome,
tcell.KeyEnd: w.handleEnd,
})
w.visible = true
w.tabbable = true
@@ -91,21 +104,50 @@ func (w *TimeField) HandleResize(ev *tcell.EventResize) {
if 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 {
if !w.active {
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
}
func (w *TimeField) HandleTime(ev *tcell.EventTime) {}
@@ -114,66 +156,14 @@ func (w *TimeField) Draw(screen tcell.Screen) {
return
}
dS := w.style.Dim(!w.active)
// pos := w.GetPos()
x := w.x
y := w.y
labelW := len(w.label)
if labelW > 0 {
if len(w.label) > 0 {
wh.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.label, wh.BRD_SIMPLE, dS, screen)
} else {
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
if w.hasDate {
yr, mo, dy := w.value.Year(), w.value.Month(), w.value.Day()
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)
}
pos := w.GetPos()
for _, wd := range w.widgets {
pos.DrawOffset(wd, 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) SetTabbable(b bool) { w.tabbable = b }
func (w *TimeField) Tabbable() bool { return w.tabbable }
func (w *TimeField) WantW() int {
wdt := 0
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) WantW() int { return w.MinW() }
func (w *TimeField) WantH() int { return w.MinH() }
func (w *TimeField) MinW() int {
wdt := 0
if w.hasDate {
if w.fldYear.Visible() {
wdt = 10 // yyyy-mm-dd
}
if w.hasTime {
if w.hasDate {
wdt += 1 // space between date & time
if w.fldSecond.Visible() {
if wdt > 10 {
wdt += 1
}
if w.hasSeconds {
wdt += 8 // hh:mm:ss
} else {
wdt += 5 // hh:mm
wdt += 8 // hh:mm:ss
} else if w.fldHour.Visible() {
if wdt > 10 {
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) SetLabel(lbl string) { w.label = lbl }
func (w *TimeField) SetTime(tm time.Time) { w.value = tm }
func (w *TimeField) SetHasSeconds(b bool) { w.hasSeconds = b }
func (w *TimeField) SetHasTime(b bool) { w.hasTime = b }
func (w *TimeField) SetHasDate(b bool) { w.hasDate = b }
func (w *TimeField) SetHasTime(hrmn, sec bool) {
w.fldHour.SetVisible(hrmn)
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) Value() time.Time { return w.value }
func (w *TimeField) handleLeft(ev *tcell.EventKey) bool {
if w.nowBtnActive {
w.cursor = w.cursorLength()
return true
} else if w.cursor > 0 {
w.cursor--
return true
func (w *TimeField) handleTab(ev *tcell.EventKey) bool {
if !w.visible {
return false
}
return false
}
func (w *TimeField) handleRight(ev *tcell.EventKey) bool {
if w.cursor < w.cursorLength() {
w.cursor++
return true
} else if !w.nowBtnActive {
w.nowBtnActive = true
return true
beg := w.cursor
w.cursor += 1
for !w.widgets[w.cursor].Visible() && w.cursor != beg {
w.cursor += 1
}
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 {
if w.cursor != 0 {
w.cursor = 0
return true
if !w.visible {
return false
}
for i := 0; i < len(w.widgets); i++ {
if w.widgets[i].Visible() {
w.cursor = i
w.updateActive()
return true
}
}
return false
}
func (w *TimeField) handleEnd(ev *tcell.EventKey) bool {
l := w.cursorLength()
if w.cursor != l {
w.cursor = w.cursorLength()
return true
if !w.visible {
return false
}
for i := len(w.widgets) - 1; i >= 0; i-- {
if w.widgets[i].Visible() {
w.cursor = i
w.updateActive()
return true
}
}
return false
}
func (w *TimeField) cursorLength() int {
var ret int
if w.hasDate {
ret += 8
func (w *TimeField) updateActive() {
for idx, wd := range w.widgets {
wd.SetActive(idx == w.cursor)
}
if w.hasTime {
ret += 4
if w.hasSeconds {
ret += 2
}
func (w *TimeField) totalFields() int {
var ret int
for i := range w.widgets {
if w.widgets[i].Visible() {
ret += 1
}
}
return ret

View File

@@ -57,6 +57,7 @@ func (w *TopMenuLayout) Init(id string, s tcell.Style) {
w.id = id
w.style = s
w.visible = true
w.SetActive(true)
w.menu = NewMenu(fmt.Sprintf("%s.mainmenu", id), tcell.StyleDefault)
w.menu.SetActive(false)
@@ -97,7 +98,9 @@ func (w *TopMenuLayout) HandleKey(ev *tcell.EventKey) bool {
// Pass the key through to the main widget
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 false