A few tweaks
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user