2023-03-04 01:31:30 +00:00
|
|
|
package core
|
|
|
|
|
2023-03-04 05:18:27 +00:00
|
|
|
import "image"
|
2023-03-04 01:31:30 +00:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements"
|
|
|
|
|
2023-03-04 06:05:37 +00:00
|
|
|
// Parent represents an object that can provide access to a list of child
|
|
|
|
// elements.
|
|
|
|
type Parent interface {
|
2023-03-04 05:57:17 +00:00
|
|
|
Child (index int) elements.Element
|
|
|
|
CountChildren () int
|
2023-03-04 01:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Propagator is a struct that can be embedded into elements that contain one or
|
|
|
|
// more children in order to propagate events to them without having to write
|
|
|
|
// all of the event handlers. It also implements standard behavior for focus
|
|
|
|
// propagation and keyboard navigation.
|
|
|
|
type Propagator struct {
|
2023-03-04 06:05:37 +00:00
|
|
|
parent Parent
|
2023-03-04 01:31:30 +00:00
|
|
|
drags [10]elements.MouseTarget
|
2023-03-04 05:18:27 +00:00
|
|
|
focused bool
|
2023-03-04 05:38:37 +00:00
|
|
|
|
|
|
|
onFocusRequest func () (granted bool)
|
2023-03-04 01:31:30 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 06:05:37 +00:00
|
|
|
// NewPropagator creates a new event propagator that uses the specified parent
|
2023-03-04 01:31:30 +00:00
|
|
|
// to access a list of child elements that will have events propagated to them.
|
2023-03-04 06:05:37 +00:00
|
|
|
// If parent is nil, the function will return nil.
|
|
|
|
func NewPropagator (parent Parent) (propagator *Propagator) {
|
|
|
|
if parent == nil { return nil }
|
2023-03-04 01:31:30 +00:00
|
|
|
propagator = &Propagator {
|
2023-03-04 06:05:37 +00:00
|
|
|
parent: parent,
|
2023-03-04 01:31:30 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-04 05:18:27 +00:00
|
|
|
// ----------- Interface fulfillment methods ----------- //
|
|
|
|
|
2023-03-04 01:31:30 +00:00
|
|
|
// Focused returns whether or not this element or any of its children
|
|
|
|
// are currently focused.
|
2023-03-04 04:48:10 +00:00
|
|
|
func (propagator *Propagator) Focused () (focused bool) {
|
2023-03-04 05:18:27 +00:00
|
|
|
return propagator.focused
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
2023-03-04 01:31:30 +00:00
|
|
|
|
|
|
|
// Focus focuses this element, if its parent element grants the
|
|
|
|
// request.
|
2023-03-04 04:48:10 +00:00
|
|
|
func (propagator *Propagator) Focus () {
|
2023-03-04 05:38:37 +00:00
|
|
|
if propagator.onFocusRequest != nil {
|
|
|
|
propagator.onFocusRequest()
|
|
|
|
}
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
2023-03-04 01:31:30 +00:00
|
|
|
|
|
|
|
// HandleFocus causes this element to mark itself as focused. If the
|
2023-03-04 04:48:10 +00:00
|
|
|
// element does not have children or there are no more focusable children in
|
|
|
|
// the given direction, it should return false and do nothing. Otherwise, it
|
|
|
|
// marks itself as focused along with any applicable children and returns
|
|
|
|
// true.
|
|
|
|
func (propagator *Propagator) HandleFocus (direction input.KeynavDirection) (accepted bool) {
|
2023-03-04 05:57:17 +00:00
|
|
|
direction = direction.Canon()
|
|
|
|
|
|
|
|
firstFocused := propagator.firstFocused()
|
|
|
|
if firstFocused < 0 {
|
|
|
|
// no element is currently focused, so we need to focus either
|
|
|
|
// the first or last focusable element depending on the
|
|
|
|
// direction.
|
|
|
|
switch direction {
|
2023-03-04 07:20:48 +00:00
|
|
|
case input.KeynavDirectionForward:
|
|
|
|
// if we recieve a forward direction, focus the first
|
|
|
|
// focusable element.
|
2023-03-04 05:57:17 +00:00
|
|
|
return propagator.focusFirstFocusableElement(direction)
|
|
|
|
|
|
|
|
case input.KeynavDirectionBackward:
|
|
|
|
// if we recieve a backward direction, focus the last
|
|
|
|
// focusable element.
|
|
|
|
return propagator.focusLastFocusableElement(direction)
|
2023-03-04 07:20:48 +00:00
|
|
|
|
|
|
|
case input.KeynavDirectionNeutral:
|
|
|
|
// if we recieve a neutral direction, just focus this
|
|
|
|
// element and nothing else.
|
|
|
|
propagator.focused = true
|
|
|
|
return true
|
2023-03-04 05:57:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// an element is currently focused, so we need to move the
|
|
|
|
// focus in the specified direction
|
|
|
|
firstFocusedChild :=
|
2023-03-04 06:05:37 +00:00
|
|
|
propagator.parent.Child(firstFocused).
|
2023-03-04 05:57:17 +00:00
|
|
|
(elements.Focusable)
|
|
|
|
|
|
|
|
// before we move the focus, the currently focused child
|
|
|
|
// may also be able to move its focus. if the child is able
|
|
|
|
// to do that, we will let it and not move ours.
|
|
|
|
if firstFocusedChild.HandleFocus(direction) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the previous/next focusable element relative to the
|
|
|
|
// currently focused element, if it exists.
|
|
|
|
for index := firstFocused + int(direction);
|
2023-03-04 06:05:37 +00:00
|
|
|
index < propagator.parent.CountChildren() && index >= 0;
|
2023-03-04 05:57:17 +00:00
|
|
|
index += int(direction) {
|
|
|
|
|
|
|
|
child, focusable :=
|
2023-03-04 06:05:37 +00:00
|
|
|
propagator.parent.Child(index).
|
2023-03-04 05:57:17 +00:00
|
|
|
(elements.Focusable)
|
|
|
|
if focusable && child.HandleFocus(direction) {
|
|
|
|
// we have found one, so we now actually move
|
|
|
|
// the focus.
|
|
|
|
firstFocusedChild.HandleUnfocus()
|
|
|
|
propagator.focused = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleDeselection causes this element to mark itself and all of its children
|
|
|
|
// as unfocused.
|
|
|
|
func (propagator *Propagator) HandleUnfocus () {
|
2023-03-04 05:38:37 +00:00
|
|
|
propagator.forFocusable (func (child elements.Focusable) bool {
|
|
|
|
child.HandleUnfocus()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
propagator.focused = false
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnFocusRequest sets a function to be called when this element wants its
|
|
|
|
// parent element to focus it. Parent elements should return true if the request
|
|
|
|
// was granted, and false if it was not. If the parent element returns true, the
|
|
|
|
// element acts as if a HandleFocus call was made with KeynavDirectionNeutral.
|
2023-03-04 05:38:37 +00:00
|
|
|
func (propagator *Propagator) OnFocusRequest (callback func () (granted bool)) {
|
|
|
|
propagator.onFocusRequest = callback
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnFocusMotionRequest sets a function to be called when this element wants its
|
|
|
|
// parent element to focus the element behind or in 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-03-04 05:38:37 +00:00
|
|
|
func (propagator *Propagator) OnFocusMotionRequest (
|
|
|
|
callback func (direction input.KeynavDirection) (granted bool),
|
2023-03-04 06:20:23 +00:00
|
|
|
) { }
|
2023-03-04 04:48:10 +00:00
|
|
|
|
|
|
|
// HandleKeyDown propogates the keyboard event to the currently selected child.
|
|
|
|
func (propagator *Propagator) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
2023-03-04 05:18:27 +00:00
|
|
|
propagator.forFocused (func (child elements.Focusable) bool {
|
|
|
|
typedChild, handlesKeyboard := child.(elements.KeyboardTarget)
|
|
|
|
if handlesKeyboard {
|
|
|
|
typedChild.HandleKeyDown(key, modifiers)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleKeyUp propogates the keyboard event to the currently selected child.
|
|
|
|
func (propagator *Propagator) HandleKeyUp (key input.Key, modifiers input.Modifiers) {
|
2023-03-04 05:18:27 +00:00
|
|
|
propagator.forFocused (func (child elements.Focusable) bool {
|
|
|
|
typedChild, handlesKeyboard := child.(elements.KeyboardTarget)
|
|
|
|
if handlesKeyboard {
|
|
|
|
typedChild.HandleKeyUp(key, modifiers)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleMouseDown propagates the mouse event to the element under the mouse
|
|
|
|
// pointer.
|
|
|
|
func (propagator *Propagator) HandleMouseDown (x, y int, button input.Button) {
|
2023-03-04 05:18:27 +00:00
|
|
|
child, handlesMouse :=
|
|
|
|
propagator.childAt(image.Pt(x, y)).
|
|
|
|
(elements.MouseTarget)
|
|
|
|
if handlesMouse {
|
|
|
|
propagator.drags[button] = child
|
|
|
|
child.HandleMouseDown(x, y, button)
|
|
|
|
}
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleMouseUp propagates the mouse event to the element that the released
|
|
|
|
// mouse button was originally pressed on.
|
|
|
|
func (propagator *Propagator) HandleMouseUp (x, y int, button input.Button) {
|
2023-03-04 05:18:27 +00:00
|
|
|
child := propagator.drags[button]
|
|
|
|
if child != nil {
|
|
|
|
propagator.drags[button] = nil
|
|
|
|
child.HandleMouseUp(x, y, button)
|
|
|
|
}
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleMouseMove propagates the mouse event to the element that was last
|
|
|
|
// pressed down by the mouse if the mouse is currently being held down, else it
|
|
|
|
// propagates the event to whichever element is underneath the mouse pointer.
|
|
|
|
func (propagator *Propagator) HandleMouseMove (x, y int) {
|
2023-03-04 05:18:27 +00:00
|
|
|
handled := false
|
|
|
|
for _, child := range propagator.drags {
|
|
|
|
if child != nil {
|
|
|
|
child.HandleMouseMove(x, y)
|
|
|
|
handled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if handled {
|
|
|
|
child, handlesMouse :=
|
|
|
|
propagator.childAt(image.Pt(x, y)).
|
|
|
|
(elements.MouseTarget)
|
|
|
|
if handlesMouse {
|
|
|
|
child.HandleMouseMove(x, y)
|
|
|
|
}
|
|
|
|
}
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleScroll propagates the mouse event to the element under the mouse
|
|
|
|
// pointer.
|
|
|
|
func (propagator *Propagator) HandleMouseScroll (x, y int, deltaX, deltaY float64) {
|
2023-03-04 05:18:27 +00:00
|
|
|
child, handlesMouse :=
|
|
|
|
propagator.childAt(image.Pt(x, y)).
|
|
|
|
(elements.MouseTarget)
|
|
|
|
if handlesMouse {
|
|
|
|
child.HandleMouseScroll(x, y, deltaX, deltaY)
|
|
|
|
}
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetTheme sets the theme of all children to the specified theme.
|
2023-03-04 05:18:27 +00:00
|
|
|
func (propagator *Propagator) SetTheme (theme theme.Theme) {
|
2023-03-04 06:05:37 +00:00
|
|
|
propagator.forChildren (func (child elements.Element) bool {
|
2023-03-04 05:18:27 +00:00
|
|
|
typedChild, themeable := child.(elements.Themeable)
|
|
|
|
if themeable {
|
|
|
|
typedChild.SetTheme(theme)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the theme of all children to the specified config.
|
2023-03-04 05:18:27 +00:00
|
|
|
func (propagator *Propagator) SetConfig (config config.Config) {
|
2023-03-04 06:05:37 +00:00
|
|
|
propagator.forChildren (func (child elements.Element) bool {
|
2023-03-04 05:18:27 +00:00
|
|
|
typedChild, configurable := child.(elements.Configurable)
|
|
|
|
if configurable {
|
|
|
|
typedChild.SetConfig(config)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-04 05:57:17 +00:00
|
|
|
// ----------- Focusing utilities ----------- //
|
|
|
|
|
|
|
|
func (propagator *Propagator) focusFirstFocusableElement (
|
|
|
|
direction input.KeynavDirection,
|
|
|
|
) (
|
|
|
|
ok bool,
|
|
|
|
) {
|
|
|
|
propagator.forFocusable (func (child elements.Focusable) bool {
|
|
|
|
if child.HandleFocus(direction) {
|
|
|
|
propagator.focused = true
|
|
|
|
ok = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (propagator *Propagator) focusLastFocusableElement (
|
|
|
|
direction input.KeynavDirection,
|
|
|
|
) (
|
|
|
|
ok bool,
|
|
|
|
) {
|
2023-03-04 06:05:37 +00:00
|
|
|
propagator.forChildrenReverse (func (child elements.Element) bool {
|
|
|
|
typedChild, focusable := child.(elements.Focusable)
|
|
|
|
if focusable && typedChild.HandleFocus(direction) {
|
2023-03-04 05:57:17 +00:00
|
|
|
propagator.focused = true
|
|
|
|
ok = true
|
2023-03-04 06:05:37 +00:00
|
|
|
return false
|
2023-03-04 05:57:17 +00:00
|
|
|
}
|
2023-03-04 06:05:37 +00:00
|
|
|
return true
|
|
|
|
})
|
2023-03-04 05:57:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-04 05:18:27 +00:00
|
|
|
// ----------- Iterator utilities ----------- //
|
|
|
|
|
2023-03-04 06:05:37 +00:00
|
|
|
func (propagator *Propagator) forChildren (callback func (child elements.Element) bool) {
|
|
|
|
for index := 0; index < propagator.parent.CountChildren(); index ++ {
|
|
|
|
child := propagator.parent.Child(index)
|
2023-03-04 06:29:45 +00:00
|
|
|
if child == nil { continue }
|
|
|
|
if !callback(child) { break }
|
2023-03-04 06:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (propagator *Propagator) forChildrenReverse (callback func (child elements.Element) bool) {
|
|
|
|
for index := propagator.parent.CountChildren() - 1; index > 0; index -- {
|
|
|
|
child := propagator.parent.Child(index)
|
2023-03-04 07:04:47 +00:00
|
|
|
if child == nil { continue }
|
|
|
|
if !callback(child) { break }
|
2023-03-04 06:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-04 05:57:17 +00:00
|
|
|
|
2023-03-04 05:18:27 +00:00
|
|
|
func (propagator *Propagator) childAt (position image.Point) (child elements.Element) {
|
2023-03-04 06:05:37 +00:00
|
|
|
propagator.forChildren (func (current elements.Element) bool {
|
2023-03-04 05:18:27 +00:00
|
|
|
if position.In(current.Bounds()) {
|
|
|
|
child = current
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (propagator *Propagator) forFocused (callback func (child elements.Focusable) bool) {
|
2023-03-04 06:05:37 +00:00
|
|
|
propagator.forChildren (func (child elements.Element) bool {
|
2023-03-04 05:18:27 +00:00
|
|
|
typedChild, focusable := child.(elements.Focusable)
|
|
|
|
if focusable && typedChild.Focused() {
|
|
|
|
if !callback(typedChild) { return false }
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (propagator *Propagator) forFocusable (callback func (child elements.Focusable) bool) {
|
2023-03-04 06:05:37 +00:00
|
|
|
propagator.forChildren (func (child elements.Element) bool {
|
2023-03-04 05:18:27 +00:00
|
|
|
typedChild, focusable := child.(elements.Focusable)
|
|
|
|
if focusable {
|
|
|
|
if !callback(typedChild) { return false }
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (propagator *Propagator) forFlexible (callback func (child elements.Flexible) bool) {
|
2023-03-04 06:05:37 +00:00
|
|
|
propagator.forChildren (func (child elements.Element) bool {
|
2023-03-04 05:18:27 +00:00
|
|
|
typedChild, flexible := child.(elements.Flexible)
|
|
|
|
if flexible {
|
|
|
|
if !callback(typedChild) { return false }
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-04 06:48:16 +00:00
|
|
|
func (propagator *Propagator) firstFocused () int {
|
|
|
|
for index := 0; index < propagator.parent.CountChildren(); index ++ {
|
|
|
|
child, focusable := propagator.parent.Child(index).(elements.Focusable)
|
|
|
|
if focusable && child.Focused() {
|
|
|
|
return index
|
2023-03-04 05:18:27 +00:00
|
|
|
}
|
2023-03-04 06:48:16 +00:00
|
|
|
}
|
|
|
|
return -1
|
2023-03-04 04:48:10 +00:00
|
|
|
}
|