Cleaned up the (ChildIterator -> Parent) interface

This commit is contained in:
Sasha Koshka 2023-03-04 01:05:37 -05:00
parent 1d9fb6024d
commit 56e11ae1de
1 changed files with 39 additions and 35 deletions

View File

@ -6,11 +6,9 @@ import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config" import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/elements" import "git.tebibyte.media/sashakoshka/tomo/elements"
// ChildIterator represents an object that can iterate over a list of children, // Parent represents an object that can provide access to a list of child
// calling a specified iterator function for each one. When keepGoing is false, // elements.
// the iterator stops the current loop and OverChildren returns. type Parent interface {
type ChildIterator interface {
OverChildren (func (child elements.Element) (keepGoing bool))
Child (index int) elements.Element Child (index int) elements.Element
CountChildren () int CountChildren () int
} }
@ -20,7 +18,7 @@ type ChildIterator interface {
// all of the event handlers. It also implements standard behavior for focus // all of the event handlers. It also implements standard behavior for focus
// propagation and keyboard navigation. // propagation and keyboard navigation.
type Propagator struct { type Propagator struct {
iterator ChildIterator parent Parent
drags [10]elements.MouseTarget drags [10]elements.MouseTarget
focused bool focused bool
@ -28,13 +26,13 @@ type Propagator struct {
onFocusMotionRequest func (input.KeynavDirection) (granted bool) onFocusMotionRequest func (input.KeynavDirection) (granted bool)
} }
// NewPropagator creates a new event propagator that uses the specified iterator // NewPropagator creates a new event propagator that uses the specified parent
// to access a list of child elements that will have events propagated to them. // to access a list of child elements that will have events propagated to them.
// If iterator is nil, the function will return nil. // If parent is nil, the function will return nil.
func NewPropagator (iterator ChildIterator) (propagator *Propagator) { func NewPropagator (parent Parent) (propagator *Propagator) {
if iterator == nil { return nil } if parent == nil { return nil }
propagator = &Propagator { propagator = &Propagator {
iterator: iterator, parent: parent,
} }
return return
} }
@ -83,7 +81,7 @@ func (propagator *Propagator) HandleFocus (direction input.KeynavDirection) (acc
// an element is currently focused, so we need to move the // an element is currently focused, so we need to move the
// focus in the specified direction // focus in the specified direction
firstFocusedChild := firstFocusedChild :=
propagator.iterator.Child(firstFocused). propagator.parent.Child(firstFocused).
(elements.Focusable) (elements.Focusable)
// before we move the focus, the currently focused child // before we move the focus, the currently focused child
@ -96,11 +94,11 @@ func (propagator *Propagator) HandleFocus (direction input.KeynavDirection) (acc
// find the previous/next focusable element relative to the // find the previous/next focusable element relative to the
// currently focused element, if it exists. // currently focused element, if it exists.
for index := firstFocused + int(direction); for index := firstFocused + int(direction);
index < propagator.iterator.CountChildren() && index >= 0; index < propagator.parent.CountChildren() && index >= 0;
index += int(direction) { index += int(direction) {
child, focusable := child, focusable :=
propagator.iterator.Child(index). propagator.parent.Child(index).
(elements.Focusable) (elements.Focusable)
if focusable && child.HandleFocus(direction) { if focusable && child.HandleFocus(direction) {
// we have found one, so we now actually move // we have found one, so we now actually move
@ -222,7 +220,7 @@ func (propagator *Propagator) HandleMouseScroll (x, y int, deltaX, deltaY float6
// SetTheme sets the theme of all children to the specified theme. // SetTheme sets the theme of all children to the specified theme.
func (propagator *Propagator) SetTheme (theme theme.Theme) { func (propagator *Propagator) SetTheme (theme theme.Theme) {
propagator.iterator.OverChildren (func (child elements.Element) bool { propagator.forChildren (func (child elements.Element) bool {
typedChild, themeable := child.(elements.Themeable) typedChild, themeable := child.(elements.Themeable)
if themeable { if themeable {
typedChild.SetTheme(theme) typedChild.SetTheme(theme)
@ -233,7 +231,7 @@ func (propagator *Propagator) SetTheme (theme theme.Theme) {
// SetConfig sets the theme of all children to the specified config. // SetConfig sets the theme of all children to the specified config.
func (propagator *Propagator) SetConfig (config config.Config) { func (propagator *Propagator) SetConfig (config config.Config) {
propagator.iterator.OverChildren (func (child elements.Element) bool { propagator.forChildren (func (child elements.Element) bool {
typedChild, configurable := child.(elements.Configurable) typedChild, configurable := child.(elements.Configurable)
if configurable { if configurable {
typedChild.SetConfig(config) typedChild.SetConfig(config)
@ -265,32 +263,38 @@ func (propagator *Propagator) focusLastFocusableElement (
) ( ) (
ok bool, ok bool,
) { ) {
focusables := []elements.Focusable { } propagator.forChildrenReverse (func (child elements.Element) bool {
propagator.forFocusable (func (child elements.Focusable) bool { typedChild, focusable := child.(elements.Focusable)
focusables = append(focusables, child) if focusable && typedChild.HandleFocus(direction) {
return true
})
for index := len(focusables) - 1; index >= 0; index -- {
child, focusable := focusables[index].(elements.Focusable)
if focusable && child.HandleFocus(direction) {
propagator.focused = true propagator.focused = true
ok = true ok = true
break return false
} }
} return true
})
return return
} }
// ----------- Iterator utilities ----------- // // ----------- Iterator utilities ----------- //
// TODO: remove ChildIterator.OverChildren, reimplement that here, rework these func (propagator *Propagator) forChildren (callback func (child elements.Element) bool) {
// methods based on that, add a reverse iteration method, and then rework for index := 0; index < propagator.parent.CountChildren(); index ++ {
// focusLastFocusableElement based on that. child := propagator.parent.Child(index)
if child == nil { continue }
if callback(child) { break }
}
}
func (propagator *Propagator) forChildrenReverse (callback func (child elements.Element) bool) {
for index := propagator.parent.CountChildren() - 1; index > 0; index -- {
child := propagator.parent.Child(index)
if child == nil { continue }
if callback(child) { break }
}
}
func (propagator *Propagator) childAt (position image.Point) (child elements.Element) { func (propagator *Propagator) childAt (position image.Point) (child elements.Element) {
propagator.iterator.OverChildren (func (current elements.Element) bool { propagator.forChildren (func (current elements.Element) bool {
if position.In(current.Bounds()) { if position.In(current.Bounds()) {
child = current child = current
} }
@ -300,7 +304,7 @@ func (propagator *Propagator) childAt (position image.Point) (child elements.Ele
} }
func (propagator *Propagator) forFocused (callback func (child elements.Focusable) bool) { func (propagator *Propagator) forFocused (callback func (child elements.Focusable) bool) {
propagator.iterator.OverChildren (func (child elements.Element) bool { propagator.forChildren (func (child elements.Element) bool {
typedChild, focusable := child.(elements.Focusable) typedChild, focusable := child.(elements.Focusable)
if focusable && typedChild.Focused() { if focusable && typedChild.Focused() {
if !callback(typedChild) { return false } if !callback(typedChild) { return false }
@ -310,7 +314,7 @@ func (propagator *Propagator) forFocused (callback func (child elements.Focusabl
} }
func (propagator *Propagator) forFocusable (callback func (child elements.Focusable) bool) { func (propagator *Propagator) forFocusable (callback func (child elements.Focusable) bool) {
propagator.iterator.OverChildren (func (child elements.Element) bool { propagator.forChildren (func (child elements.Element) bool {
typedChild, focusable := child.(elements.Focusable) typedChild, focusable := child.(elements.Focusable)
if focusable { if focusable {
if !callback(typedChild) { return false } if !callback(typedChild) { return false }
@ -320,7 +324,7 @@ func (propagator *Propagator) forFocusable (callback func (child elements.Focusa
} }
func (propagator *Propagator) forFlexible (callback func (child elements.Flexible) bool) { func (propagator *Propagator) forFlexible (callback func (child elements.Flexible) bool) {
propagator.iterator.OverChildren (func (child elements.Element) bool { propagator.forChildren (func (child elements.Element) bool {
typedChild, flexible := child.(elements.Flexible) typedChild, flexible := child.(elements.Flexible)
if flexible { if flexible {
if !callback(typedChild) { return false } if !callback(typedChild) { return false }