2023-01-27 15:55:49 -07:00
|
|
|
package core
|
|
|
|
|
2023-03-11 23:57:56 -07:00
|
|
|
// import "runtime/debug"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-03-14 16:30:32 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements"
|
2023-01-27 15:55:49 -07:00
|
|
|
|
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 {
|
2023-03-14 16:30:32 -06:00
|
|
|
core CoreControl
|
2023-01-30 15:01:47 -07:00
|
|
|
focused bool
|
|
|
|
enabled bool
|
|
|
|
drawFocusChange func ()
|
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 (
|
2023-03-14 16:30:32 -06:00
|
|
|
core CoreControl,
|
2023-01-30 15:01:47 -07:00
|
|
|
drawFocusChange func (),
|
2023-01-27 15:55:49 -07:00
|
|
|
) (
|
2023-03-14 16:30:32 -06:00
|
|
|
focusable *FocusableCore,
|
2023-01-30 15:01:47 -07:00
|
|
|
control FocusableCoreControl,
|
2023-01-27 15:55:49 -07:00
|
|
|
) {
|
2023-03-14 16:30:32 -06:00
|
|
|
focusable = &FocusableCore {
|
|
|
|
core: core,
|
2023-01-30 15:01:47 -07:00
|
|
|
drawFocusChange: drawFocusChange,
|
2023-01-27 15:55:49 -07:00
|
|
|
enabled: true,
|
|
|
|
}
|
2023-03-14 16:30:32 -06:00
|
|
|
control = FocusableCoreControl { core: focusable }
|
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-02-08 23:30:14 -07:00
|
|
|
if !core.enabled || core.focused { return }
|
2023-03-14 16:30:32 -06:00
|
|
|
parent := core.core.Parent()
|
|
|
|
if parent, ok := parent.(elements.FocusableParent); ok && parent != nil {
|
|
|
|
core.focused = parent.RequestFocus (
|
2023-03-15 15:08:43 -06:00
|
|
|
core.core.Outer().(elements.Focusable))
|
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 (
|
2023-02-01 23:48:16 -07:00
|
|
|
direction input.KeynavDirection,
|
2023-01-27 15:55:49 -07:00
|
|
|
) (
|
|
|
|
accepted bool,
|
|
|
|
) {
|
|
|
|
direction = direction.Canon()
|
|
|
|
if !core.enabled { return false }
|
2023-02-01 23:48:16 -07:00
|
|
|
if core.focused && direction != input.KeynavDirectionNeutral {
|
2023-01-27 15:55:49 -07:00
|
|
|
return false
|
|
|
|
}
|
2023-02-08 23:30:14 -07:00
|
|
|
|
|
|
|
if core.focused == false {
|
|
|
|
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
|
2023-03-11 23:57:56 -07:00
|
|
|
// debug.PrintStack()
|
2023-01-30 15:01:47 -07:00
|
|
|
if core.drawFocusChange != nil { core.drawFocusChange() }
|
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
|
|
|
}
|
|
|
|
}
|