Added keynav input for lists

This commit is contained in:
Sasha Koshka 2023-01-26 11:53:49 -05:00
parent 3dc2fe390a
commit 48679c8ad2
2 changed files with 33 additions and 20 deletions

View File

@ -58,8 +58,6 @@ func (element *List) Resize (width, height int) {
}
}
// TODO: handle keyboard and mouse events
// Collapse forces a minimum width and height upon the list. If a zero value is
// given for a dimension, its minimum will be determined by the list's content.
// If the list's height goes beyond the forced size, it will need to be accessed
@ -85,18 +83,6 @@ func (element *List) HandleMouseDown (x, y int, button tomo.Button) {
func (element *List) HandleMouseUp (x, y int, button tomo.Button) {
if button != tomo.ButtonLeft { return }
element.pressed = false
// if element.core.HasImage() {
// element.draw()
// element.core.DamageAll()
// }
//
// within := image.Point { x, y }.
// In(element.Bounds())
//
// if !element.enabled { return }
// if within && element.onClick != nil {
// element.onClick()
// }
}
func (element *List) HandleMouseMove (x, y int) {
@ -112,7 +98,21 @@ func (element *List) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
func (element *List) HandleKeyDown (key tomo.Key, modifiers tomo.Modifiers) {
if !element.enabled { return }
// TODO
newIndex := element.selectedEntry
switch key {
case tomo.KeyLeft, tomo.KeyUp: newIndex --
case tomo.KeyRight, tomo.KeyDown: newIndex ++
default: return
}
if newIndex < 0 { newIndex = len(element.entries) - 1 }
if newIndex >= len(element.entries) { newIndex = 0 }
if element.selectEntry(newIndex) && element.core.HasImage () {
element.draw()
element.core.DamageAll()
}
}
func (element *List) HandleKeyUp(key tomo.Key, modifiers tomo.Modifiers) { }
@ -354,8 +354,12 @@ func (element *List) selectUnderMouse (x, y int) (updated bool) {
}
}
if element.selectedEntry == newlySelectedEntryIndex { return false }
element.selectedEntry = newlySelectedEntryIndex
return element.selectEntry(newlySelectedEntryIndex)
}
func (element *List) selectEntry (index int) (updated bool) {
if element.selectedEntry == index { return false }
element.selectedEntry = index
if element.onSelectedEntryChange != nil {
element.onSelectedEntryChange(element.selectedEntry)
}
@ -401,7 +405,7 @@ func (element *List) draw () {
artist.FillRectangle (
element,
theme.ListPattern(),
theme.ListPattern(element.selected),
bounds)
dot := image.Point {

View File

@ -13,6 +13,11 @@ var listPattern = artist.NewMultiBordered (
},
artist.Stroke { Pattern: artist.NewUniform(hex(0x999C99FF)) })
var selectedListPattern = artist.NewMultiBordered (
artist.Stroke { Weight: 1, Pattern: strokePattern },
artist.Stroke { Weight: 1, Pattern: accentPattern },
artist.Stroke { Pattern: artist.NewUniform(hex(0x999C99FF)) })
var listEntryPattern = artist.NewMultiBordered (
artist.Stroke { Weight: 1, Pattern: artist.QuadBeveled {
@ -34,8 +39,12 @@ var selectedListEntryPattern = artist.NewMultiBordered (
},
artist.Stroke { Pattern: artist.NewUniform(hex(0x97a09cFF)) })
func ListPattern () (pattern artist.Pattern) {
return listPattern
func ListPattern (selected bool) (pattern artist.Pattern) {
if selected {
return selectedListPattern
} else {
return listPattern
}
}
func ListEntryPattern (selected bool) (pattern artist.Pattern) {