Files
tcell-widgets/wdgt_timefield.go
2025-10-26 08:47:07 -05:00

309 lines
7.8 KiB
Go

/*
Copyright © Brian Buller <brian@bullercodeworks.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package widgets
import (
"fmt"
"time"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
)
// TODO: Fix this, it's not drawing correctly.
type TimeField struct {
id string
label string
style tcell.Style
active bool
visible bool
focusable bool
x, y int
w, h int
yr, mo, dy int
hr, mn, sc int
value time.Time
dateFormat string
fldYear *Field
fldMonth *Field
fldDay *Field
fldHour *Field
fldMinute *Field
fldSecond *Field
btnNow *Button
widgets []Widget
cursor int
keyMap *KeyMap
}
// TODO: Allow changing the format.
// For now it's just yyyy-mm-dd hh:mm:ss
var _ Widget = (*TimeField)(nil)
func NewTimeField(id string, style tcell.Style) *TimeField {
ret := &TimeField{style: style}
ret.Init(id, style)
return ret
}
func (w *TimeField) Init(id string, style tcell.Style) {
w.id = id
w.style = style
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(
NewKey(BuildEK(tcell.KeyHome), w.handleHome),
NewKey(BuildEK(tcell.KeyEnd), w.handleEnd),
)
w.visible = true
w.focusable = true
}
func (w *TimeField) Id() string { return w.id }
func (w *TimeField) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size()
if w.w > w.WantW() {
w.w = w.WantW()
}
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) GetKeyMap() *KeyMap { return w.keyMap }
func (w *TimeField) SetKeyMap(km *KeyMap) { w.keyMap = km }
func (w *TimeField) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
return false
} else if ev.Key() == tcell.KeyTab {
return w.handleTab(ev)
}
if w.keyMap.Handle(ev) {
return true
}
if w.cursor <= len(w.widgets) {
return w.widgets[w.cursor].HandleKey(ev)
}
return false
}
func (w *TimeField) HandleTime(ev *tcell.EventTime) {
for _, wd := range []Widget{
w.fldYear, w.fldMonth, w.fldDay, w.fldHour,
w.fldMinute, w.fldSecond, w.btnNow,
} {
wd.HandleTime(ev)
}
}
func (w *TimeField) Draw(screen tcell.Screen) {
if !w.visible {
return
}
dS := w.style.Dim(!w.active)
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)
}
pos := w.GetPos()
for _, wd := range w.widgets {
pos.DrawOffset(wd, screen)
}
}
func (w *TimeField) Active() bool { return w.active }
func (w *TimeField) SetActive(a bool) { w.active = a }
func (w *TimeField) Visible() bool { return w.visible }
func (w *TimeField) SetVisible(a bool) { w.visible = a }
func (w *TimeField) SetX(x int) { w.x = x }
func (w *TimeField) SetY(y int) { w.y = y }
func (w *TimeField) GetX() int { return w.x }
func (w *TimeField) GetY() int { return w.y }
func (w *TimeField) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
func (w *TimeField) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
func (w *TimeField) SetW(x int) { w.w = x }
func (w *TimeField) SetH(y int) { w.h = y }
func (w *TimeField) GetW() int { return w.w }
func (w *TimeField) GetH() int { return w.y }
func (w *TimeField) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *TimeField) Focusable() bool { return w.focusable }
func (w *TimeField) SetFocusable(b bool) { w.focusable = b }
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.fldYear.Visible() {
wdt = 10 // yyyy-mm-dd
}
if w.fldSecond.Visible() {
if wdt > 10 {
wdt += 1
}
wdt += 8 // hh:mm:ss
} else if w.fldHour.Visible() {
if wdt > 10 {
wdt += 1
}
wdt += 5 // hh:mm
}
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) 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) handleTab(ev *tcell.EventKey) bool {
if !w.visible {
return false
}
beg := w.cursor
w.cursor += 1
for !w.widgets[w.cursor].Visible() && w.cursor != beg {
w.cursor += 1
}
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.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 {
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) updateActive() {
for idx, wd := range w.widgets {
wd.SetActive(idx == w.cursor)
}
}
func (w *TimeField) totalFields() int {
var ret int
for i := range w.widgets {
if w.widgets[i].Visible() {
ret += 1
}
}
return ret
}