This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/container.go

84 lines
2.2 KiB
Go
Raw Normal View History

2023-04-18 22:29:25 -06:00
package elements
import "git.tebibyte.media/sashakoshka/tomo"
type scratchEntry struct {
expand bool
minSize float64
minBreadth float64
}
type container struct {
2023-05-02 20:19:29 -06:00
entity tomo.Entity
2023-04-18 22:29:25 -06:00
scratch map[tomo.Element] scratchEntry
minimumSize func ()
}
// Entity returns this element's entity.
2023-04-18 22:29:25 -06:00
func (container *container) Entity () tomo.Entity {
return container.entity
}
// Adopt adds one or more elements to the container.
2023-04-18 22:29:25 -06:00
func (container *container) Adopt (children ...tomo.Element) {
container.adopt(false, children...)
}
func (container *container) init () {
container.scratch = make(map[tomo.Element] scratchEntry)
}
func (container *container) adopt (expand bool, children ...tomo.Element) {
for _, child := range children {
container.entity.Adopt(child)
container.scratch[child] = scratchEntry { expand: expand }
}
container.minimumSize()
container.entity.Invalidate()
container.entity.InvalidateLayout()
}
// Disown removes one or more elements from the container.
2023-04-18 22:29:25 -06:00
func (container *container) Disown (children ...tomo.Element) {
for _, child := range children {
index := container.entity.IndexOf(child)
if index < 0 { continue }
container.entity.Disown(index)
delete(container.scratch, child)
}
container.minimumSize()
container.entity.Invalidate()
container.entity.InvalidateLayout()
}
// DisownAll removes all elements from the container.
2023-04-18 22:29:25 -06:00
func (container *container) DisownAll () {
func () {
for index := 0; index < container.entity.CountChildren(); index ++ {
index := index
defer container.entity.Disown(index)
}
} ()
container.scratch = make(map[tomo.Element] scratchEntry)
container.minimumSize()
container.entity.Invalidate()
container.entity.InvalidateLayout()
}
// Child returns the child at the specified index.
2023-04-18 22:29:25 -06:00
func (container *container) Child (index int) tomo.Element {
if index < 0 || index >= container.entity.CountChildren() { return nil }
return container.entity.Child(index)
}
// CountChildren returns the amount of children in this container.
2023-04-18 22:29:25 -06:00
func (container *container) CountChildren () int {
return container.entity.CountChildren()
}
func (container *container) HandleChildMinimumSizeChange (child tomo.Element) {
container.minimumSize()
container.entity.Invalidate()
container.entity.InvalidateLayout()
}