2023-01-27 15:55:49 -07:00
|
|
|
package core
|
|
|
|
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// FocusableCore is a struct that can be embedded into objects to make them
|
|
|
|
// focusable, giving them the default keynav behavior.
|
|
|
|
type FocusableCore struct {
|
|
|
|
focused bool
|
|
|
|
enabled bool
|
|
|
|
drawFocusChange func ()
|
|
|
|
onFocusRequest func () (granted bool)
|
|
|
|
onFocusMotionRequest func(tomo.KeynavDirection) (granted bool)
|
2023-01-27 15:55:49 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// NewFocusableCore creates a new focusability core and its corresponding
|
|
|
|
// control. If your element needs to visually update itself when it's focus
|
2023-01-27 15:55:49 -07:00
|
|
|
// state changes (which it should), a callback to draw and push the update can
|
|
|
|
// be specified.
|
2023-01-30 15:01:47 -07:00
|
|
|
func NewFocusableCore (
|
|
|
|
drawFocusChange func (),
|
2023-01-27 15:55:49 -07:00
|
|
|
) (
|
2023-01-30 15:01:47 -07:00
|
|
|
core *FocusableCore,
|
|
|
|
control FocusableCoreControl,
|
2023-01-27 15:55:49 -07:00
|
|
|
) {
|
2023-01-30 15:01:47 -07:00
|
|
|
core = &FocusableCore {
|
|
|
|
drawFocusChange: drawFocusChange,
|
2023-01-27 15:55:49 -07:00
|
|
|
enabled: true,
|
|
|
|
}
|
2023-01-30 15:01:47 -07:00
|
|
|
control = FocusableCoreControl { core: core }
|
2023-01-27 15:55:49 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// Focused returns whether or not this element is currently focused.
|
|
|
|
func (core *FocusableCore) Focused () (focused bool) {
|
|
|
|
return core.focused
|
2023-01-27 15:55:49 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// Focus focuses this element, if its parent element grants the request.
|
|
|
|
func (core *FocusableCore) Focus () {
|
2023-01-27 15:55:49 -07:00
|
|
|
if !core.enabled { return }
|
2023-01-30 15:01:47 -07:00
|
|
|
if core.onFocusRequest != nil {
|
|
|
|
core.onFocusRequest()
|
2023-01-27 15:55:49 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// HandleFocus causes this element to mark itself as focused, if it can
|
2023-01-27 15:55:49 -07:00
|
|
|
// currently be. Otherwise, it will return false and do nothing.
|
2023-01-30 15:01:47 -07:00
|
|
|
func (core *FocusableCore) HandleFocus (
|
|
|
|
direction tomo.KeynavDirection,
|
2023-01-27 15:55:49 -07:00
|
|
|
) (
|
|
|
|
accepted bool,
|
|
|
|
) {
|
|
|
|
direction = direction.Canon()
|
|
|
|
if !core.enabled { return false }
|
2023-01-30 15:01:47 -07:00
|
|
|
if core.focused && direction != tomo.KeynavDirectionNeutral {
|
2023-01-27 15:55:49 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
core.focused = true
|
|
|
|
if core.drawFocusChange != nil { core.drawFocusChange() }
|
2023-01-27 15:55:49 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// HandleUnfocus causes this element to mark itself as unfocused.
|
|
|
|
func (core *FocusableCore) HandleUnfocus () {
|
|
|
|
core.focused = false
|
|
|
|
if core.drawFocusChange != nil { core.drawFocusChange() }
|
2023-01-27 15:55:49 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// OnFocusRequest sets a function to be called when this element
|
|
|
|
// wants its parent element to focus it. Parent elements should return
|
2023-01-27 15:55:49 -07:00
|
|
|
// true if the request was granted, and false if it was not.
|
2023-01-30 15:01:47 -07:00
|
|
|
func (core *FocusableCore) OnFocusRequest (callback func () (granted bool)) {
|
|
|
|
core.onFocusRequest = callback
|
2023-01-27 15:55:49 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// OnFocusMotionRequest sets a function to be called when this
|
|
|
|
// element wants its parent element to focus the element behind or in
|
2023-01-27 15:55:49 -07:00
|
|
|
// front of it, depending on the specified direction. Parent elements
|
|
|
|
// should return true if the request was granted, and false if it was
|
|
|
|
// not.
|
2023-01-30 15:01:47 -07:00
|
|
|
func (core *FocusableCore) OnFocusMotionRequest (
|
|
|
|
callback func (direction tomo.KeynavDirection) (granted bool),
|
2023-01-27 15:55:49 -07:00
|
|
|
) {
|
2023-01-30 15:01:47 -07:00
|
|
|
core.onFocusMotionRequest = callback
|
2023-01-27 15:55:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enabled returns whether or not the element is enabled.
|
2023-01-30 15:01:47 -07:00
|
|
|
func (core *FocusableCore) Enabled () (enabled bool) {
|
2023-01-27 15:55:49 -07:00
|
|
|
return core.enabled
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// FocusableCoreControl is a struct that can be used to exert control over a
|
|
|
|
// focusability core. It must not be directly embedded into an element, but
|
|
|
|
// instead kept as a private member. When a FocusableCore struct is created, a
|
|
|
|
// corresponding FocusableCoreControl struct is linked to it and returned
|
2023-01-27 15:55:49 -07:00
|
|
|
// alongside it.
|
2023-01-30 15:01:47 -07:00
|
|
|
type FocusableCoreControl struct {
|
|
|
|
core *FocusableCore
|
2023-01-27 15:55:49 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// SetEnabled sets whether the focusability core is enabled. If the state
|
|
|
|
// changes, this will call drawFocusChange.
|
|
|
|
func (control FocusableCoreControl) SetEnabled (enabled bool) {
|
2023-01-27 15:55:49 -07:00
|
|
|
if control.core.enabled == enabled { return }
|
|
|
|
control.core.enabled = enabled
|
2023-01-30 15:01:47 -07:00
|
|
|
if !enabled { control.core.focused = false }
|
|
|
|
if control.core.drawFocusChange != nil {
|
|
|
|
control.core.drawFocusChange()
|
2023-01-27 15:55:49 -07:00
|
|
|
}
|
|
|
|
}
|