Buttons respond to enter key, window auto-selects root child

This commit is contained in:
Sasha Koshka 2023-01-09 18:19:19 -05:00
parent 70e0566f3f
commit 80aafdf60c
2 changed files with 26 additions and 2 deletions

View File

@ -77,7 +77,8 @@ func (backend *Backend) NewWindow (
func (window *Window) Adopt (child tomo.Element) {
if window.child != nil {
child.SetParentHooks (tomo.ParentHooks {})
child.SetParentHooks (tomo.ParentHooks { })
if child.Selectable() { child.Handle(tomo.EventDeselect { }) }
}
window.child = child
if child != nil {
@ -86,6 +87,7 @@ func (window *Window) Adopt (child tomo.Element) {
MinimumSizeChange: window.childMinimumSizeChangeCallback,
SelectionRequest: window.childSelectionRequestCallback,
})
if child.Selectable() { child.Handle(tomo.EventSelect { }) }
window.resizeChildToFit()
}
window.childMinimumSizeChangeCallback(child.MinimumSize())

View File

@ -47,6 +47,16 @@ func (element *Button) Handle (event tomo.Event) {
element.core.PushAll()
}
case tomo.EventKeyDown:
keyDownEvent := event.(tomo.EventKeyDown)
if keyDownEvent.Key == tomo.KeyEnter {
element.pressed = true
if element.core.HasImage() {
element.draw()
element.core.PushAll()
}
}
case tomo.EventMouseUp:
if !element.enabled { break }
@ -65,12 +75,24 @@ func (element *Button) Handle (event tomo.Event) {
element.onClick()
}
case tomo.EventKeyUp:
keyDownEvent := event.(tomo.EventKeyUp)
if keyDownEvent.Key == tomo.KeyEnter {
element.pressed = false
if element.core.HasImage() {
element.draw()
element.core.PushAll()
}
if element.onClick != nil {
element.onClick()
}
}
case tomo.EventSelect:
element.selected = true
case tomo.EventDeselect:
element.selected = false
// TODO: handle selection events, and the enter key
}
return
}