Much Work

- Definable KeyMaps
- Change 'Tabbable' to just use 'Focusable'
This commit is contained in:
2025-09-04 11:09:34 -05:00
parent c1db729bb3
commit f571b13a31
26 changed files with 881 additions and 615 deletions

View File

@@ -35,16 +35,17 @@ const (
)
type Checkbox struct {
id string
label string
style tcell.Style
active bool
visible bool
tabbable bool
state int
x, y int
w, h int
id string
label string
style tcell.Style
active bool
visible bool
focusable bool
state int
x, y int
w, h int
keyMap KeyMap
stateRunes []rune
}
@@ -61,28 +62,39 @@ func (w *Checkbox) Init(id string, style tcell.Style) {
w.style = style
w.visible = true
w.stateRunes = []rune{'X', ' ', '-'}
w.tabbable = true
w.focusable = true
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
tcell.KeyEnter: func(_ *tcell.EventKey) bool {
if w.state == CHECKBOX_ON {
w.state = CHECKBOX_OFF
} else {
w.state = CHECKBOX_ON
}
return true
},
})
}
func (w *Checkbox) Id() string { return w.id }
func (w *Checkbox) 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 *Checkbox) SetKeyMap(km KeyMap) { w.keyMap = km }
func (w *Checkbox) AddToKeyMap(km KeyMap) { w.keyMap.Merge(km) }
func (w *Checkbox) RemoveFromKeyMap(km KeyMap) {
for k := range km.Keys {
w.keyMap.Remove(k)
}
for r := range km.Runes {
w.keyMap.RemoveRune(r)
}
}
func (w *Checkbox) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
return false
}
if ev.Key() == tcell.KeyEnter {
if w.state == CHECKBOX_ON {
w.state = CHECKBOX_OFF
} else {
w.state = CHECKBOX_ON
}
return true
}
return false
return w.keyMap.Handle(ev)
}
func (w *Checkbox) HandleTime(ev *tcell.EventTime) {}
func (w *Checkbox) Draw(screen tcell.Screen) {
@@ -113,11 +125,10 @@ func (w *Checkbox) GetH() int { return w.y }
func (w *Checkbox) WantW() int {
return len(fmt.Sprintf("[%s] %s", string(w.state), w.label))
}
func (w *Checkbox) WantH() int { return 1 }
func (w *Checkbox) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *Checkbox) Focusable() bool { return true }
func (w *Checkbox) SetTabbable(b bool) { w.tabbable = b }
func (w *Checkbox) Tabbable() bool { return w.tabbable }
func (w *Checkbox) WantH() int { return 1 }
func (w *Checkbox) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *Checkbox) Focusable() bool { return w.focusable }
func (w *Checkbox) SetFocusable(b bool) { w.focusable = b }
func (w *Checkbox) MinW() int {
return len(fmt.Sprintf("[%s] %s", string(w.state), w.label))
}