2023-02-01 23:48:16 -07:00
|
|
|
package basicElements
|
2023-01-10 14:39:37 -07:00
|
|
|
|
2023-01-10 15:34:40 -07:00
|
|
|
import "image"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-01-10 14:39:37 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
2023-02-07 22:22:40 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
2023-01-10 14:39:37 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/layouts"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements"
|
2023-01-10 14:39:37 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
|
|
|
|
2023-01-12 14:02:33 -07:00
|
|
|
// Container is an element capable of containg other elements, and arranging
|
|
|
|
// them in a layout.
|
2023-01-10 14:39:37 -07:00
|
|
|
type Container struct {
|
|
|
|
*core.Core
|
|
|
|
core core.CoreControl
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
layout layouts.Layout
|
|
|
|
children []layouts.LayoutEntry
|
|
|
|
drags [10]elements.MouseTarget
|
2023-01-30 15:01:47 -07:00
|
|
|
warping bool
|
|
|
|
focused bool
|
|
|
|
focusable bool
|
|
|
|
flexible bool
|
2023-01-19 14:49:34 -07:00
|
|
|
|
2023-02-08 12:36:14 -07:00
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
2023-02-07 22:22:40 -07:00
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
onFocusRequest func () (granted bool)
|
2023-02-01 23:48:16 -07:00
|
|
|
onFocusMotionRequest func (input.KeynavDirection) (granted bool)
|
2023-01-19 14:49:34 -07:00
|
|
|
onFlexibleHeightChange func ()
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:02:33 -07:00
|
|
|
// NewContainer creates a new container.
|
2023-02-01 23:48:16 -07:00
|
|
|
func NewContainer (layout layouts.Layout) (element *Container) {
|
2023-02-08 12:36:14 -07:00
|
|
|
element = &Container { }
|
|
|
|
element.theme.Case = theme.C("basic", "container")
|
2023-02-07 22:22:40 -07:00
|
|
|
element.Core, element.core = core.NewCore(element.redoAll)
|
2023-01-10 14:39:37 -07:00
|
|
|
element.SetLayout(layout)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-12 14:02:33 -07:00
|
|
|
// SetLayout sets the layout of this container.
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) SetLayout (layout layouts.Layout) {
|
2023-01-10 14:39:37 -07:00
|
|
|
element.layout = layout
|
2023-01-10 15:34:40 -07:00
|
|
|
if element.core.HasImage() {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.redoAll()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-10 15:34:40 -07:00
|
|
|
}
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
2023-01-12 14:02:33 -07:00
|
|
|
// Adopt adds a new child element to the container. If expand is set to true,
|
|
|
|
// the element will expand (instead of contract to its minimum size), in
|
|
|
|
// whatever way is defined by the current layout.
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) Adopt (child elements.Element, expand bool) {
|
2023-01-19 14:49:34 -07:00
|
|
|
// set event handlers
|
2023-02-08 12:36:14 -07:00
|
|
|
if child0, ok := child.(elements.Themeable); ok {
|
|
|
|
child0.SetTheme(element.theme.Theme)
|
|
|
|
}
|
|
|
|
if child0, ok := child.(elements.Configurable); ok {
|
|
|
|
child0.SetConfig(element.config.Config)
|
|
|
|
}
|
2023-02-01 23:48:16 -07:00
|
|
|
child.OnDamage (func (region canvas.Canvas) {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.core.DamageRegion(region.Bounds())
|
2023-01-10 14:39:37 -07:00
|
|
|
})
|
2023-02-08 23:30:14 -07:00
|
|
|
child.OnMinimumSizeChange (func () {
|
2023-02-09 00:04:58 -07:00
|
|
|
// TODO: this could probably stand to be more efficient. I mean
|
|
|
|
// seriously?
|
2023-02-08 23:30:14 -07:00
|
|
|
element.updateMinimumSize()
|
|
|
|
element.redoAll()
|
2023-02-09 00:04:58 -07:00
|
|
|
element.core.DamageAll()
|
2023-02-08 23:30:14 -07:00
|
|
|
})
|
2023-02-01 23:48:16 -07:00
|
|
|
if child0, ok := child.(elements.Flexible); ok {
|
2023-01-19 14:49:34 -07:00
|
|
|
child0.OnFlexibleHeightChange(element.updateMinimumSize)
|
|
|
|
}
|
2023-02-01 23:48:16 -07:00
|
|
|
if child0, ok := child.(elements.Focusable); ok {
|
2023-01-30 15:01:47 -07:00
|
|
|
child0.OnFocusRequest (func () (granted bool) {
|
|
|
|
return element.childFocusRequestCallback(child0)
|
2023-01-19 14:49:34 -07:00
|
|
|
})
|
2023-01-30 15:01:47 -07:00
|
|
|
child0.OnFocusMotionRequest (
|
2023-02-01 23:48:16 -07:00
|
|
|
func (direction input.KeynavDirection) (granted bool) {
|
2023-01-30 15:01:47 -07:00
|
|
|
if element.onFocusMotionRequest == nil { return }
|
|
|
|
return element.onFocusMotionRequest(direction)
|
2023-01-19 14:49:34 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// add child
|
2023-02-01 23:48:16 -07:00
|
|
|
element.children = append (element.children, layouts.LayoutEntry {
|
2023-01-10 14:39:37 -07:00
|
|
|
Element: child,
|
2023-01-10 15:34:40 -07:00
|
|
|
Expand: expand,
|
2023-01-10 14:39:37 -07:00
|
|
|
})
|
|
|
|
|
2023-01-19 14:49:34 -07:00
|
|
|
// refresh stale data
|
2023-01-10 14:39:37 -07:00
|
|
|
element.updateMinimumSize()
|
2023-01-16 21:34:17 -07:00
|
|
|
element.reflectChildProperties()
|
2023-01-12 14:02:33 -07:00
|
|
|
if element.core.HasImage() && !element.warping {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.redoAll()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-12 14:02:33 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Warp runs the specified callback, deferring all layout and rendering updates
|
|
|
|
// until the callback has finished executing. This allows for aplications to
|
|
|
|
// perform batch gui updates without flickering and stuff.
|
|
|
|
func (element *Container) Warp (callback func ()) {
|
|
|
|
if element.warping {
|
|
|
|
callback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
element.warping = true
|
|
|
|
callback()
|
|
|
|
element.warping = false
|
|
|
|
|
|
|
|
// TODO: create some sort of task list so we don't do a full recalculate
|
|
|
|
// and redraw every time, because although that is the most likely use
|
|
|
|
// case, it is not the only one.
|
2023-01-10 15:34:40 -07:00
|
|
|
if element.core.HasImage() {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.redoAll()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-10 15:34:40 -07:00
|
|
|
}
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disown removes the given child from the container if it is contained within
|
|
|
|
// it.
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) Disown (child elements.Element) {
|
2023-01-10 14:39:37 -07:00
|
|
|
for index, entry := range element.children {
|
|
|
|
if entry.Element == child {
|
2023-01-19 14:49:34 -07:00
|
|
|
element.clearChildEventHandlers(entry.Element)
|
2023-01-10 14:39:37 -07:00
|
|
|
element.children = append (
|
|
|
|
element.children[:index],
|
|
|
|
element.children[index + 1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
element.updateMinimumSize()
|
2023-01-16 21:34:17 -07:00
|
|
|
element.reflectChildProperties()
|
2023-01-12 14:02:33 -07:00
|
|
|
if element.core.HasImage() && !element.warping {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.redoAll()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) clearChildEventHandlers (child elements.Element) {
|
2023-01-31 12:54:43 -07:00
|
|
|
child.DrawTo(nil)
|
2023-01-19 14:49:34 -07:00
|
|
|
child.OnDamage(nil)
|
|
|
|
child.OnMinimumSizeChange(nil)
|
2023-02-01 23:48:16 -07:00
|
|
|
if child0, ok := child.(elements.Focusable); ok {
|
2023-01-30 15:01:47 -07:00
|
|
|
child0.OnFocusRequest(nil)
|
|
|
|
child0.OnFocusMotionRequest(nil)
|
|
|
|
if child0.Focused() {
|
|
|
|
child0.HandleUnfocus()
|
2023-01-19 14:49:34 -07:00
|
|
|
}
|
|
|
|
}
|
2023-02-01 23:48:16 -07:00
|
|
|
if child0, ok := child.(elements.Flexible); ok {
|
2023-01-19 14:49:34 -07:00
|
|
|
child0.OnFlexibleHeightChange(nil)
|
2023-01-10 15:34:40 -07:00
|
|
|
}
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
2023-01-10 16:07:51 -07:00
|
|
|
// DisownAll removes all child elements from the container at once.
|
|
|
|
func (element *Container) DisownAll () {
|
|
|
|
element.children = nil
|
|
|
|
|
|
|
|
element.updateMinimumSize()
|
2023-01-16 21:34:17 -07:00
|
|
|
element.reflectChildProperties()
|
2023-01-12 14:02:33 -07:00
|
|
|
if element.core.HasImage() && !element.warping {
|
2023-01-31 12:54:43 -07:00
|
|
|
element.redoAll()
|
2023-01-19 14:49:34 -07:00
|
|
|
element.core.DamageAll()
|
2023-01-10 16:07:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 14:39:37 -07:00
|
|
|
// Children returns a slice containing this element's children.
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) Children () (children []elements.Element) {
|
|
|
|
children = make([]elements.Element, len(element.children))
|
2023-01-10 14:39:37 -07:00
|
|
|
for index, entry := range element.children {
|
|
|
|
children[index] = entry.Element
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CountChildren returns the amount of children contained within this element.
|
|
|
|
func (element *Container) CountChildren () (count int) {
|
|
|
|
return len(element.children)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Child returns the child at the specified index. If the index is out of
|
|
|
|
// bounds, this method will return nil.
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) Child (index int) (child elements.Element) {
|
2023-01-10 14:39:37 -07:00
|
|
|
if index < 0 || index > len(element.children) { return }
|
|
|
|
return element.children[index].Element
|
|
|
|
}
|
|
|
|
|
2023-01-10 15:34:40 -07:00
|
|
|
// ChildAt returns the child that contains the specified x and y coordinates. If
|
|
|
|
// there are no children at the coordinates, this method will return nil.
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) ChildAt (point image.Point) (child elements.Element) {
|
2023-01-10 15:34:40 -07:00
|
|
|
for _, entry := range element.children {
|
2023-01-31 12:54:43 -07:00
|
|
|
if point.In(entry.Bounds) {
|
2023-01-10 15:34:40 -07:00
|
|
|
child = entry.Element
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) childPosition (child elements.Element) (position image.Point) {
|
2023-01-10 15:34:40 -07:00
|
|
|
for _, entry := range element.children {
|
|
|
|
if entry.Element == child {
|
2023-01-31 12:54:43 -07:00
|
|
|
position = entry.Bounds.Min
|
2023-01-10 15:34:40 -07:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:54:43 -07:00
|
|
|
func (element *Container) redoAll () {
|
2023-02-08 23:30:14 -07:00
|
|
|
if !element.core.HasImage() { return }
|
2023-01-31 12:54:43 -07:00
|
|
|
// do a layout
|
2023-02-09 00:04:58 -07:00
|
|
|
element.doLayout()
|
2023-01-31 12:54:43 -07:00
|
|
|
|
|
|
|
// draw a background
|
2023-02-11 19:17:43 -07:00
|
|
|
rocks := make([]image.Rectangle, len(element.children))
|
|
|
|
for index, entry := range element.children {
|
|
|
|
rocks[index] = entry.Bounds
|
|
|
|
}
|
2023-02-07 22:22:40 -07:00
|
|
|
pattern := element.theme.Pattern (
|
|
|
|
theme.PatternBackground,
|
2023-02-26 20:20:17 -07:00
|
|
|
theme.State { })
|
|
|
|
artist.DrawShatter (
|
|
|
|
element.core, pattern, rocks...)
|
2023-01-31 12:54:43 -07:00
|
|
|
|
2023-01-31 16:04:12 -07:00
|
|
|
// cut our canvas up and give peices to child elements
|
2023-01-31 12:54:43 -07:00
|
|
|
for _, entry := range element.children {
|
2023-02-12 23:49:33 -07:00
|
|
|
entry.DrawTo(canvas.Cut(element.core, entry.Bounds))
|
2023-01-31 12:54:43 -07:00
|
|
|
}
|
2023-01-16 09:54:02 -07:00
|
|
|
}
|
2023-01-10 15:34:40 -07:00
|
|
|
|
2023-02-07 22:22:40 -07:00
|
|
|
|
|
|
|
// SetTheme sets the element's theme.
|
|
|
|
func (element *Container) SetTheme (new theme.Theme) {
|
2023-02-08 12:36:14 -07:00
|
|
|
if new == element.theme.Theme { return }
|
|
|
|
element.theme.Theme = new
|
2023-02-07 09:27:59 -07:00
|
|
|
for _, child := range element.children {
|
2023-02-07 22:22:40 -07:00
|
|
|
if child0, ok := child.Element.(elements.Themeable); ok {
|
2023-02-08 12:36:14 -07:00
|
|
|
child0.SetTheme(element.theme.Theme)
|
2023-02-07 09:27:59 -07:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-02-07 09:27:59 -07:00
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
|
2023-02-07 22:22:40 -07:00
|
|
|
// SetConfig sets the element's configuration.
|
|
|
|
func (element *Container) SetConfig (new config.Config) {
|
2023-02-08 12:36:14 -07:00
|
|
|
if new == element.config.Config { return }
|
|
|
|
element.config.Config = new
|
2023-02-07 09:27:59 -07:00
|
|
|
for _, child := range element.children {
|
2023-02-07 22:22:40 -07:00
|
|
|
if child0, ok := child.Element.(elements.Configurable); ok {
|
|
|
|
child0.SetConfig(element.config)
|
2023-02-07 09:27:59 -07:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 22:22:40 -07:00
|
|
|
element.updateMinimumSize()
|
2023-02-07 09:27:59 -07:00
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) HandleMouseDown (x, y int, button input.Button) {
|
|
|
|
child, handlesMouse := element.ChildAt(image.Pt(x, y)).(elements.MouseTarget)
|
2023-01-16 09:54:02 -07:00
|
|
|
if !handlesMouse { return }
|
|
|
|
element.drags[button] = child
|
2023-01-31 16:04:12 -07:00
|
|
|
child.HandleMouseDown(x, y, button)
|
2023-01-16 09:54:02 -07:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) HandleMouseUp (x, y int, button input.Button) {
|
2023-01-16 09:54:02 -07:00
|
|
|
child := element.drags[button]
|
|
|
|
if child == nil { return }
|
|
|
|
element.drags[button] = nil
|
2023-01-31 16:04:12 -07:00
|
|
|
child.HandleMouseUp(x, y, button)
|
2023-01-16 09:54:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Container) HandleMouseMove (x, y int) {
|
|
|
|
for _, child := range element.drags {
|
|
|
|
if child == nil { continue }
|
2023-01-31 16:04:12 -07:00
|
|
|
child.HandleMouseMove(x, y)
|
2023-01-16 09:54:02 -07:00
|
|
|
}
|
|
|
|
}
|
2023-01-10 15:34:40 -07:00
|
|
|
|
2023-01-19 14:55:46 -07:00
|
|
|
func (element *Container) HandleMouseScroll (x, y int, deltaX, deltaY float64) {
|
2023-02-01 23:48:16 -07:00
|
|
|
child, handlesMouse := element.ChildAt(image.Pt(x, y)).(elements.MouseTarget)
|
2023-01-16 09:54:02 -07:00
|
|
|
if !handlesMouse { return }
|
2023-01-31 16:04:12 -07:00
|
|
|
child.HandleMouseScroll(x, y, deltaX, deltaY)
|
2023-01-16 09:54:02 -07:00
|
|
|
}
|
2023-01-11 13:46:48 -07:00
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
|
|
|
|
element.forFocused (func (child elements.Focusable) bool {
|
|
|
|
child0, handlesKeyboard := child.(elements.KeyboardTarget)
|
2023-01-16 09:54:02 -07:00
|
|
|
if handlesKeyboard {
|
2023-01-20 15:40:28 -07:00
|
|
|
child0.HandleKeyDown(key, modifiers)
|
2023-01-11 20:30:14 -07:00
|
|
|
}
|
2023-01-16 09:54:02 -07:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2023-01-11 13:46:48 -07:00
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) HandleKeyUp (key input.Key, modifiers input.Modifiers) {
|
|
|
|
element.forFocused (func (child elements.Focusable) bool {
|
|
|
|
child0, handlesKeyboard := child.(elements.KeyboardTarget)
|
2023-01-16 09:54:02 -07:00
|
|
|
if handlesKeyboard {
|
|
|
|
child0.HandleKeyUp(key, modifiers)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2023-01-11 20:30:14 -07:00
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *Container) FlexibleHeightFor (width int) (height int) {
|
2023-02-26 20:20:17 -07:00
|
|
|
margin := element.theme.Margin(theme.PatternBackground)
|
|
|
|
// TODO: have layouts take in x and y margins
|
2023-02-01 23:48:16 -07:00
|
|
|
return element.layout.FlexibleHeightFor (
|
|
|
|
element.children,
|
2023-02-26 20:20:17 -07:00
|
|
|
margin.X, width)
|
2023-01-30 15:01:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Container) OnFlexibleHeightChange (callback func ()) {
|
|
|
|
element.onFlexibleHeightChange = callback
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Container) Focused () (focused bool) {
|
|
|
|
return element.focused
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *Container) Focus () {
|
|
|
|
if element.onFocusRequest != nil {
|
|
|
|
element.onFocusRequest()
|
2023-01-19 14:49:34 -07:00
|
|
|
}
|
2023-01-11 20:30:14 -07:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) HandleFocus (direction input.KeynavDirection) (ok bool) {
|
2023-01-30 15:01:47 -07:00
|
|
|
if !element.focusable { return false }
|
2023-01-16 10:21:47 -07:00
|
|
|
direction = direction.Canon()
|
2023-01-11 20:30:14 -07:00
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
firstFocused := element.firstFocused()
|
|
|
|
if firstFocused < 0 {
|
|
|
|
// no element is currently focused, so we need to focus either
|
|
|
|
// the first or last focusable element depending on the
|
2023-01-26 12:00:54 -07:00
|
|
|
// direction.
|
2023-01-16 09:54:02 -07:00
|
|
|
switch direction {
|
2023-02-01 23:48:16 -07:00
|
|
|
case input.KeynavDirectionNeutral, input.KeynavDirectionForward:
|
2023-01-30 15:01:47 -07:00
|
|
|
// if we recieve a neutral or forward direction, focus
|
|
|
|
// the first focusable element.
|
|
|
|
return element.focusFirstFocusableElement(direction)
|
2023-01-26 12:00:54 -07:00
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
case input.KeynavDirectionBackward:
|
2023-01-30 15:01:47 -07:00
|
|
|
// if we recieve a backward direction, focus the last
|
|
|
|
// focusable element.
|
|
|
|
return element.focusLastFocusableElement(direction)
|
2023-01-11 20:30:14 -07:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-30 15:01:47 -07:00
|
|
|
// an element is currently focused, so we need to move the
|
|
|
|
// focus in the specified direction
|
|
|
|
firstFocusedChild :=
|
2023-02-01 23:48:16 -07:00
|
|
|
element.children[firstFocused].Element.(elements.Focusable)
|
2023-01-26 12:00:54 -07:00
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// before we move the focus, the currently focused child
|
|
|
|
// may also be able to move its focus. if the child is able
|
2023-01-26 12:00:54 -07:00
|
|
|
// to do that, we will let it and not move ours.
|
2023-01-30 15:01:47 -07:00
|
|
|
if firstFocusedChild.HandleFocus(direction) {
|
2023-01-26 12:00:54 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
// find the previous/next focusable element relative to the
|
|
|
|
// currently focused element, if it exists.
|
|
|
|
for index := firstFocused + int(direction);
|
2023-01-16 09:54:02 -07:00
|
|
|
index < len(element.children) && index >= 0;
|
2023-01-16 10:21:47 -07:00
|
|
|
index += int(direction) {
|
2023-01-11 20:30:14 -07:00
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
child, focusable :=
|
2023-01-16 09:54:02 -07:00
|
|
|
element.children[index].
|
2023-02-01 23:48:16 -07:00
|
|
|
Element.(elements.Focusable)
|
2023-01-30 15:01:47 -07:00
|
|
|
if focusable && child.HandleFocus(direction) {
|
2023-01-26 12:00:54 -07:00
|
|
|
// we have found one, so we now actually move
|
2023-01-30 15:01:47 -07:00
|
|
|
// the focus.
|
|
|
|
firstFocusedChild.HandleUnfocus()
|
|
|
|
element.focused = true
|
2023-01-16 09:54:02 -07:00
|
|
|
return true
|
2023-01-11 20:30:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 10:21:47 -07:00
|
|
|
return false
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *Container) focusFirstFocusableElement (
|
2023-02-01 23:48:16 -07:00
|
|
|
direction input.KeynavDirection,
|
2023-01-26 12:00:54 -07:00
|
|
|
) (
|
|
|
|
ok bool,
|
|
|
|
) {
|
2023-02-01 23:48:16 -07:00
|
|
|
element.forFocusable (func (child elements.Focusable) bool {
|
2023-01-30 15:01:47 -07:00
|
|
|
if child.HandleFocus(direction) {
|
|
|
|
element.focused = true
|
2023-01-26 12:00:54 -07:00
|
|
|
ok = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *Container) focusLastFocusableElement (
|
2023-02-01 23:48:16 -07:00
|
|
|
direction input.KeynavDirection,
|
2023-01-26 12:00:54 -07:00
|
|
|
) (
|
|
|
|
ok bool,
|
|
|
|
) {
|
2023-02-01 23:48:16 -07:00
|
|
|
element.forFocusableBackward (func (child elements.Focusable) bool {
|
2023-01-30 15:01:47 -07:00
|
|
|
if child.HandleFocus(direction) {
|
|
|
|
element.focused = true
|
2023-01-26 12:00:54 -07:00
|
|
|
ok = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *Container) HandleUnfocus () {
|
|
|
|
element.focused = false
|
2023-02-01 23:48:16 -07:00
|
|
|
element.forFocused (func (child elements.Focusable) bool {
|
2023-01-30 15:01:47 -07:00
|
|
|
child.HandleUnfocus()
|
2023-01-16 09:54:02 -07:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *Container) OnFocusRequest (callback func () (granted bool)) {
|
|
|
|
element.onFocusRequest = callback
|
2023-01-19 14:49:34 -07:00
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *Container) OnFocusMotionRequest (
|
2023-02-01 23:48:16 -07:00
|
|
|
callback func (direction input.KeynavDirection) (granted bool),
|
2023-01-19 14:49:34 -07:00
|
|
|
) {
|
2023-01-30 15:01:47 -07:00
|
|
|
element.onFocusMotionRequest = callback
|
2023-01-19 14:49:34 -07:00
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) forFocused (callback func (child elements.Focusable) bool) {
|
2023-01-16 09:54:02 -07:00
|
|
|
for _, entry := range element.children {
|
2023-02-01 23:48:16 -07:00
|
|
|
child, focusable := entry.Element.(elements.Focusable)
|
2023-01-30 15:01:47 -07:00
|
|
|
if focusable && child.Focused() {
|
2023-01-16 09:54:02 -07:00
|
|
|
if !callback(child) { break }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) forFocusable (callback func (child elements.Focusable) bool) {
|
2023-01-16 09:54:02 -07:00
|
|
|
for _, entry := range element.children {
|
2023-02-01 23:48:16 -07:00
|
|
|
child, focusable := entry.Element.(elements.Focusable)
|
2023-01-30 15:01:47 -07:00
|
|
|
if focusable {
|
2023-01-16 09:54:02 -07:00
|
|
|
if !callback(child) { break }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) forFlexible (callback func (child elements.Flexible) bool) {
|
2023-01-16 21:34:17 -07:00
|
|
|
for _, entry := range element.children {
|
2023-02-01 23:48:16 -07:00
|
|
|
child, flexible := entry.Element.(elements.Flexible)
|
2023-01-30 15:01:47 -07:00
|
|
|
if flexible {
|
2023-01-16 21:34:17 -07:00
|
|
|
if !callback(child) { break }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 23:48:16 -07:00
|
|
|
func (element *Container) forFocusableBackward (callback func (child elements.Focusable) bool) {
|
2023-01-16 09:54:02 -07:00
|
|
|
for index := len(element.children) - 1; index >= 0; index -- {
|
2023-02-01 23:48:16 -07:00
|
|
|
child, focusable := element.children[index].Element.(elements.Focusable)
|
2023-01-30 15:01:47 -07:00
|
|
|
if focusable {
|
2023-01-16 09:54:02 -07:00
|
|
|
if !callback(child) { break }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *Container) firstFocused () (index int) {
|
2023-01-11 20:30:14 -07:00
|
|
|
for currentIndex, entry := range element.children {
|
2023-02-01 23:48:16 -07:00
|
|
|
child, focusable := entry.Element.(elements.Focusable)
|
2023-01-30 15:01:47 -07:00
|
|
|
if focusable && child.Focused() {
|
2023-01-11 20:30:14 -07:00
|
|
|
return currentIndex
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2023-01-16 21:34:17 -07:00
|
|
|
func (element *Container) reflectChildProperties () {
|
2023-01-30 15:01:47 -07:00
|
|
|
element.focusable = false
|
2023-02-01 23:48:16 -07:00
|
|
|
element.forFocusable (func (elements.Focusable) bool {
|
2023-01-30 15:01:47 -07:00
|
|
|
element.focusable = true
|
2023-01-16 09:54:02 -07:00
|
|
|
return false
|
|
|
|
})
|
2023-01-16 21:34:17 -07:00
|
|
|
element.flexible = false
|
2023-02-01 23:48:16 -07:00
|
|
|
element.forFlexible (func (elements.Flexible) bool {
|
2023-01-16 21:34:17 -07:00
|
|
|
element.flexible = true
|
|
|
|
return false
|
|
|
|
})
|
2023-01-30 15:01:47 -07:00
|
|
|
if !element.focusable {
|
|
|
|
element.focused = false
|
2023-01-16 09:54:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 15:01:47 -07:00
|
|
|
func (element *Container) childFocusRequestCallback (
|
2023-02-01 23:48:16 -07:00
|
|
|
child elements.Focusable,
|
2023-01-16 09:54:02 -07:00
|
|
|
) (
|
|
|
|
granted bool,
|
|
|
|
) {
|
2023-01-30 15:01:47 -07:00
|
|
|
if element.onFocusRequest != nil && element.onFocusRequest() {
|
2023-02-09 12:25:55 -07:00
|
|
|
element.focused = true
|
2023-02-01 23:48:16 -07:00
|
|
|
element.forFocused (func (child elements.Focusable) bool {
|
2023-01-30 15:01:47 -07:00
|
|
|
child.HandleUnfocus()
|
2023-01-16 09:54:02 -07:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *Container) updateMinimumSize () {
|
2023-02-26 20:20:17 -07:00
|
|
|
margin := element.theme.Margin(theme.PatternBackground)
|
|
|
|
// TODO: have layouts take in x and y margins
|
|
|
|
width, height := element.layout.MinimumSize(element.children, margin.X)
|
2023-01-16 21:34:17 -07:00
|
|
|
if element.flexible {
|
2023-02-01 23:48:16 -07:00
|
|
|
height = element.layout.FlexibleHeightFor (
|
2023-02-26 20:20:17 -07:00
|
|
|
element.children,
|
|
|
|
margin.X, width)
|
2023-01-16 21:34:17 -07:00
|
|
|
}
|
|
|
|
element.core.SetMinimumSize(width, height)
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|
|
|
|
|
2023-02-09 00:04:58 -07:00
|
|
|
func (element *Container) doLayout () {
|
2023-02-26 20:20:17 -07:00
|
|
|
margin := element.theme.Margin(theme.PatternBackground)
|
|
|
|
// TODO: have layouts take in x and y margins
|
2023-02-01 23:48:16 -07:00
|
|
|
element.layout.Arrange (
|
2023-02-26 20:20:17 -07:00
|
|
|
element.children, margin.X, element.Bounds())
|
2023-01-10 14:39:37 -07:00
|
|
|
}
|