217 lines
5.8 KiB
Go
217 lines
5.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"
|
|
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
type DatePicker struct {
|
|
id string
|
|
style tcell.Style
|
|
|
|
x, y int
|
|
w, h int
|
|
lookActive bool
|
|
active bool
|
|
visible bool
|
|
focusable bool
|
|
|
|
date time.Time
|
|
format string
|
|
|
|
dateFld *Field
|
|
dateNow *Button
|
|
|
|
keyMap KeyMap
|
|
|
|
logger func(string, ...any)
|
|
}
|
|
|
|
var _ Widget = (*DatePicker)(nil)
|
|
|
|
func NewDatePicker(id string, s tcell.Style) *DatePicker {
|
|
ret := &DatePicker{}
|
|
ret.Init(id, s)
|
|
return ret
|
|
}
|
|
|
|
func (w *DatePicker) Init(id string, s tcell.Style) {
|
|
w.id = id
|
|
w.style = s
|
|
w.visible = true
|
|
w.focusable = true
|
|
|
|
w.format = time.RFC3339
|
|
|
|
w.dateFld = NewField(fmt.Sprintf("%s-date", id), s)
|
|
w.dateFld.SetValue(w.date.Format(w.format))
|
|
w.dateFld.SetH(1)
|
|
|
|
w.dateNow = NewButton(fmt.Sprintf("%s-now", id), s)
|
|
w.dateNow.SetLabel("Now")
|
|
w.dateNow.SetOnPressed(func() bool {
|
|
w.SetValue(time.Now())
|
|
return true
|
|
})
|
|
w.dateNow.SetW(5)
|
|
w.dateNow.SetH(1)
|
|
|
|
w.initKeyMap()
|
|
}
|
|
|
|
func (w *DatePicker) Id() string { return w.id }
|
|
func (w *DatePicker) HandleResize(ev *tcell.EventResize) {
|
|
wd, ht := ev.Size()
|
|
w.SetW(wd)
|
|
w.SetH(ht)
|
|
if wd >= w.WantW() {
|
|
w.dateFld.HandleResize(Coord{X: w.dateFld.WantW(), Y: ht}.ResizeEvent())
|
|
w.dateNow.HandleResize(Coord{X: w.dateNow.WantW(), Y: ht}.ResizeEvent())
|
|
}
|
|
w.dateFld.SetPos(Coord{X: 0, Y: 0})
|
|
w.dateNow.SetPos(Coord{X: wd - 6, Y: 0})
|
|
}
|
|
|
|
func (w *DatePicker) SetKeyMap(km KeyMap) { w.keyMap = km }
|
|
func (w *DatePicker) AddToKeyMap(km KeyMap) { w.keyMap.Merge(km) }
|
|
func (w *DatePicker) RemoveFromKeyMap(km KeyMap) {
|
|
for k := range km.Keys {
|
|
w.keyMap.Remove(k)
|
|
}
|
|
for r := range km.Runes {
|
|
w.keyMap.RemoveRune(r)
|
|
}
|
|
}
|
|
|
|
func (w *DatePicker) HandleKey(ev *tcell.EventKey) bool {
|
|
if !w.active {
|
|
return false
|
|
}
|
|
if ev.Key() == tcell.KeyTab {
|
|
if w.dateFld.Active() {
|
|
w.dateFld.SetActive(false)
|
|
w.dateNow.SetActive(true)
|
|
return true
|
|
} else if w.dateNow.Active() {
|
|
w.dateFld.SetActive(false)
|
|
w.dateNow.SetActive(false)
|
|
return false
|
|
}
|
|
return false
|
|
}
|
|
if w.dateFld.Active() {
|
|
if w.dateFld.HandleKey(ev) {
|
|
// Date Field updated, see if we can parse it.
|
|
dt, err := time.Parse(w.format, w.dateFld.Value())
|
|
if err == nil {
|
|
w.date = dt
|
|
}
|
|
}
|
|
} else if w.dateNow.Active() {
|
|
return w.dateNow.HandleKey(ev)
|
|
}
|
|
return w.keyMap.Handle(ev)
|
|
}
|
|
func (w *DatePicker) HandleTime(ev *tcell.EventTime) {}
|
|
func (w *DatePicker) Draw(screen tcell.Screen) {
|
|
if !w.visible {
|
|
return
|
|
}
|
|
|
|
w.GetPos().DrawOffset(w.dateFld, screen)
|
|
w.GetPos().DrawOffset(w.dateNow, screen)
|
|
}
|
|
|
|
func (w *DatePicker) Active() bool { return w.active }
|
|
func (w *DatePicker) SetActive(a bool) {
|
|
if !w.active && a {
|
|
// Wasn't active, but turning on
|
|
w.dateFld.SetActive(true)
|
|
}
|
|
w.active = a
|
|
}
|
|
func (w *DatePicker) Visible() bool { return w.visible }
|
|
func (w *DatePicker) SetVisible(a bool) { w.visible = a }
|
|
func (w *DatePicker) Focusable() bool { return w.focusable }
|
|
func (w *DatePicker) SetFocusable(b bool) { w.focusable = b }
|
|
func (w *DatePicker) SetX(x int) { w.x = x }
|
|
func (w *DatePicker) SetY(y int) { w.y = y }
|
|
func (w *DatePicker) GetX() int { return w.x }
|
|
func (w *DatePicker) GetY() int { return w.y }
|
|
func (w *DatePicker) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
|
func (w *DatePicker) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
|
func (w *DatePicker) GetW() int { return w.w }
|
|
func (w *DatePicker) GetH() int { return w.h }
|
|
func (w *DatePicker) SetW(wd int) { w.w = wd }
|
|
func (w *DatePicker) SetH(h int) { w.h = h }
|
|
func (w *DatePicker) SetSize(c Coord) {
|
|
w.SetW(c.X)
|
|
w.SetH(c.Y)
|
|
}
|
|
|
|
func (w *DatePicker) WantW() int {
|
|
return w.dateFld.WantW() + w.dateNow.WantW() + 2
|
|
}
|
|
|
|
func (w *DatePicker) WantH() int {
|
|
return 1
|
|
}
|
|
func (w *DatePicker) MinW() int { return w.dateFld.MinW() + w.dateNow.MinW() + 2 }
|
|
func (w *DatePicker) MinH() int { return 1 }
|
|
|
|
func (w *DatePicker) updateUI() {
|
|
w.dateFld.SetValue(w.date.Format(w.format))
|
|
w.dateFld.SetSize(Coord{X: len(w.dateFld.Label()) + len(w.format) + 3, Y: 1})
|
|
w.dateNow.SetSize(Coord{X: 5, Y: 1})
|
|
}
|
|
|
|
func (w *DatePicker) initKeyMap() {
|
|
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{})
|
|
}
|
|
|
|
func (w *DatePicker) Format() string { return w.format }
|
|
func (w *DatePicker) SetFormat(fmt string) {
|
|
w.format = fmt
|
|
w.updateUI()
|
|
}
|
|
func (w *DatePicker) Label() string { return w.dateFld.Label() }
|
|
func (w *DatePicker) SetLabel(lbl string) {
|
|
w.dateFld.SetLabel(lbl)
|
|
w.updateUI()
|
|
}
|
|
func (w *DatePicker) Value() time.Time { return w.date }
|
|
func (w *DatePicker) SetValue(t time.Time) {
|
|
w.date = t
|
|
w.updateUI()
|
|
}
|
|
|
|
func (w *DatePicker) SetLogger(l func(string, ...any)) { w.logger = l }
|
|
func (w *DatePicker) Log(t string, a ...any) {
|
|
if w.logger != nil {
|
|
w.logger(t, a...)
|
|
}
|
|
}
|