Keynav works flawlessly

This commit is contained in:
Sasha Koshka 2023-01-16 12:21:47 -05:00
parent 354d5f205d
commit fb0795ec7b
4 changed files with 37 additions and 37 deletions

View File

@ -69,13 +69,15 @@ func (window *Window) handleKeyPress (
}
if key == tomo.KeyTab && modifiers.Alt {
if _, ok := window.child.(tomo.Selectable); ok {
if child, ok := window.child.(tomo.Selectable); ok {
direction := tomo.SelectionDirectionForward
if modifiers.Shift {
direction = tomo.SelectionDirectionBackward
}
window.advanceSelectionInChild(direction)
if !child.HandleSelection(direction) {
child.HandleDeselection()
}
}
} else if child, ok := window.child.(tomo.KeyboardTarget); ok {
// FIXME: pass correct value for repeated
@ -83,17 +85,6 @@ func (window *Window) handleKeyPress (
}
}
func (window *Window) advanceSelectionInChild (direction tomo.SelectionDirection) {
child := window.child.(tomo.Selectable)
if child.Selected() {
if !child.HandleSelection(direction) {
child.HandleDeselection()
}
} else {
child.HandleSelection(tomo.SelectionDirectionNeutral)
}
}
func (window *Window) handleKeyRelease (
connection *xgbutil.XUtil,
event xevent.KeyReleaseEvent,

View File

@ -79,6 +79,17 @@ const (
SelectionDirectionForward SelectionDirection = 1
)
// Canon returns a well-formed direction.
func (direction SelectionDirection) Canon () (canon SelectionDirection) {
if direction > 0 {
return SelectionDirectionForward
} else if direction == 0 {
return SelectionDirectionNeutral
} else {
return SelectionDirectionBackward
}
}
// Selectable represents an element that has keyboard navigation support. This
// includes inputs, buttons, sliders, etc. as well as any elements that have
// children (so keyboard navigation events can be propagated downward).

View File

@ -103,16 +103,17 @@ func (element *Button) HandleSelection (
) (
accepted bool,
) {
if direction == tomo.SelectionDirectionNeutral && element.enabled {
element.selected = true
if element.core.HasImage() {
element.draw()
element.core.PushAll()
}
return true
} else {
if !element.enabled { return false }
if element.selected && direction != tomo.SelectionDirectionNeutral {
return false
}
element.selected = true
if element.core.HasImage() {
element.draw()
element.core.PushAll()
}
return true
}
func (element *Button) HandleDeselection () {

View File

@ -244,44 +244,41 @@ func (element *Container) Select () {
// FIXME
func (element *Container) HandleSelection (direction tomo.SelectionDirection) (ok bool) {
if !element.selectable { return false }
direction = direction.Canon()
firstSelected := element.firstSelected()
if firstSelected < 0 {
found := false
switch direction {
case tomo.SelectionDirectionNeutral,
tomo.SelectionDirectionBackward:
case tomo.SelectionDirectionBackward:
element.forSelectableBackward (func (child tomo.Selectable) bool {
if child.HandleSelection (
tomo.SelectionDirectionNeutral,
) {
if child.HandleSelection(direction) {
element.selected = true
found = true
return false
}
return true
})
return true
case tomo.SelectionDirectionForward:
case tomo.SelectionDirectionNeutral, tomo.SelectionDirectionForward:
element.forSelectable (func (child tomo.Selectable) bool {
if child.HandleSelection (
tomo.SelectionDirectionNeutral,
) {
if child.HandleSelection(direction) {
element.selected = true
found = true
return false
}
return true
})
}
return false
return found
} else {
firstSelectedChild :=
element.children[firstSelected].Element.(tomo.Selectable)
step := 1
if direction < 0 { step = - 1 }
for index := firstSelected + step;
for index := firstSelected + int(direction);
index < len(element.children) && index >= 0;
index += step {
index += int(direction) {
child, selectable :=
element.children[index].
@ -294,7 +291,7 @@ func (element *Container) HandleSelection (direction tomo.SelectionDirection) (o
}
}
return
return false
}
func (element *Container) HandleDeselection () {