Added Datepicker

Some other work too
This commit is contained in:
2025-10-01 15:37:31 -05:00
parent d3334d73f6
commit 998531f95d
4 changed files with 343 additions and 34 deletions

View File

@@ -19,6 +19,8 @@ THE SOFTWARE.
package widgets
import (
"fmt"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
)
@@ -59,6 +61,8 @@ func (w *Form) Init(id string, style tcell.Style) {
w.style = style
w.visible = true
w.focusable = true
w.submit = NewButton(fmt.Sprintf("%s-submit", id), style)
w.cancel = NewButton(fmt.Sprintf("%s-cancel", id), style)
}
func (w *Form) Id() string { return w.id }
@@ -67,7 +71,9 @@ func (w *Form) HandleResize(ev *tcell.EventResize) {
y := 0
for i := range w.fields {
w.fields[i].SetPos(Coord{X: 0, Y: y})
y += w.fields[i].WantH()
wantH := w.fields[i].WantH()
w.fields[i].HandleResize(Coord{X: w.w, Y: wantH}.ResizeEvent())
y += wantH
}
half := w.w / 2
if w.hasCancel && w.hasSubmit {
@@ -99,17 +105,21 @@ func (w *Form) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
return false
}
if w.getActive().HandleKey(ev) {
return true
}
if ev.Key() == tcell.KeyTab {
fldCnt := len(w.fields)
num := fldCnt
if w.hasCancel {
num += 1
fldCnt += 1
}
if w.hasSubmit {
num += 1
}
if num < fldCnt {
fldCnt += 1
}
pre := w.cursor
w.cursor = (w.cursor + 1) % fldCnt
w.updateWidgets()
return w.cursor > pre
}
return false
}
@@ -126,7 +136,14 @@ func (w *Form) Draw(screen tcell.Screen) {
}
p := w.GetPos()
for _, wdgt := range w.fields {
p.DrawOffset(wdgt, screen)
if !w.Active() {
wdAct := wdgt.Active()
wdgt.SetActive(false)
p.DrawOffset(wdgt, screen)
wdgt.SetActive(wdAct)
} else {
p.DrawOffset(wdgt, screen)
}
}
if w.hasCancel {
p.DrawOffset(w.cancel, screen)
@@ -136,8 +153,11 @@ func (w *Form) Draw(screen tcell.Screen) {
}
}
func (w *Form) Active() bool { return w.active }
func (w *Form) SetActive(a bool) { w.active = a }
func (w *Form) Active() bool { return w.active }
func (w *Form) SetActive(a bool) {
w.active = a
w.updateWidgets()
}
func (w *Form) Visible() bool { return w.visible }
func (w *Form) SetVisible(a bool) { w.visible = a }
func (w *Form) SetX(x int) { w.x = x }
@@ -186,6 +206,37 @@ func (w *Form) MinW() int { return 1 }
func (w *Form) MinH() int { return 1 }
// Non-Widget Functions
func (w *Form) updateWidgets() {
for i := 0; i < len(w.fields); i++ {
w.fields[i].SetActive(w.Active() && i == w.cursor)
}
if w.hasCancel {
w.cancel.SetActive(w.Active() && w.cursor == len(w.fields))
if w.hasSubmit {
w.submit.SetActive(w.Active() && w.cursor == len(w.fields)+1)
}
} else if w.hasSubmit {
w.submit.SetActive(w.Active() && w.cursor == len(w.fields))
}
}
func (w *Form) getActive() Widget {
if w.cursor < len(w.fields) {
return w.fields[w.cursor]
}
if w.cursor == len(w.fields) {
if w.hasCancel {
return w.cancel
}
if w.hasSubmit {
return w.submit
}
} else if w.cursor > len(w.fields) && w.hasSubmit {
return w.submit
}
return nil
}
func (w *Form) IndexOf(n Widget) int {
for i := range w.fields {
if w.fields[i] == n {