2023-03-16 12:22:56 -06:00
|
|
|
package containers
|
2023-03-11 16:00:29 -07:00
|
|
|
|
|
|
|
import "image"
|
2023-03-30 21:19:04 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
2023-03-11 16:00:29 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
2023-03-30 23:06:29 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/default/config"
|
2023-03-11 16:00:29 -07:00
|
|
|
|
|
|
|
type DocumentContainer struct {
|
|
|
|
*core.Core
|
|
|
|
*core.Propagator
|
|
|
|
core core.CoreControl
|
|
|
|
|
2023-03-30 21:19:04 -06:00
|
|
|
children []tomo.LayoutEntry
|
2023-03-11 16:00:29 -07:00
|
|
|
scroll image.Point
|
|
|
|
warping bool
|
|
|
|
contentBounds image.Rectangle
|
|
|
|
|
|
|
|
config config.Wrapped
|
|
|
|
theme theme.Wrapped
|
2023-03-15 21:56:00 -06:00
|
|
|
|
|
|
|
onScrollBoundsChange func ()
|
2023-03-11 16:00:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDocumentContainer creates a new document container.
|
|
|
|
func NewDocumentContainer () (element *DocumentContainer) {
|
|
|
|
element = &DocumentContainer { }
|
2023-03-30 23:06:29 -06:00
|
|
|
element.theme.Case = tomo.C("tomo", "documentContainer")
|
2023-03-14 23:41:23 -06:00
|
|
|
element.Core, element.core = core.NewCore(element, element.redoAll)
|
|
|
|
element.Propagator = core.NewPropagator(element, element.core)
|
2023-03-11 16:00:29 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-04 14:39:12 -06:00
|
|
|
// Adopt adds a new child element to the container. If expand is true, then the
|
|
|
|
// element will stretch to either side of the container (much like a css block
|
|
|
|
// element). If expand is false, the element will share a line with other inline
|
|
|
|
// elements.
|
|
|
|
func (element *DocumentContainer) Adopt (child tomo.Element, expand bool) {
|
2023-03-11 16:00:29 -07:00
|
|
|
// set event handlers
|
2023-03-30 21:19:04 -06:00
|
|
|
if child0, ok := child.(tomo.Themeable); ok {
|
2023-03-11 16:00:29 -07:00
|
|
|
child0.SetTheme(element.theme.Theme)
|
|
|
|
}
|
2023-03-30 21:19:04 -06:00
|
|
|
if child0, ok := child.(tomo.Configurable); ok {
|
2023-03-11 16:00:29 -07:00
|
|
|
child0.SetConfig(element.config.Config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add child
|
2023-03-30 21:19:04 -06:00
|
|
|
element.children = append (element.children, tomo.LayoutEntry {
|
2023-03-11 16:00:29 -07:00
|
|
|
Element: child,
|
2023-04-04 14:39:12 -06:00
|
|
|
Expand: expand,
|
2023-03-11 16:00:29 -07:00
|
|
|
})
|
|
|
|
|
2023-03-15 22:24:40 -06:00
|
|
|
child.SetParent(element)
|
|
|
|
|
2023-03-11 16:00:29 -07:00
|
|
|
// refresh stale data
|
2023-03-16 23:52:26 -06:00
|
|
|
element.updateMinimumSize()
|
2023-03-11 16:00:29 -07:00
|
|
|
if element.core.HasImage() && !element.warping {
|
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 *DocumentContainer) Warp (callback func ()) {
|
|
|
|
if element.warping {
|
|
|
|
callback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
element.warping = true
|
|
|
|
callback()
|
|
|
|
element.warping = false
|
|
|
|
|
|
|
|
if element.core.HasImage() {
|
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disown removes the given child from the container if it is contained within
|
|
|
|
// it.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *DocumentContainer) Disown (child tomo.Element) {
|
2023-03-11 16:00:29 -07:00
|
|
|
for index, entry := range element.children {
|
|
|
|
if entry.Element == child {
|
|
|
|
element.clearChildEventHandlers(entry.Element)
|
|
|
|
element.children = append (
|
|
|
|
element.children[:index],
|
|
|
|
element.children[index + 1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 23:52:26 -06:00
|
|
|
element.updateMinimumSize()
|
2023-03-11 16:00:29 -07:00
|
|
|
if element.core.HasImage() && !element.warping {
|
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *DocumentContainer) clearChildEventHandlers (child tomo.Element) {
|
2023-03-14 23:41:23 -06:00
|
|
|
child.DrawTo(nil, image.Rectangle { }, nil)
|
|
|
|
child.SetParent(nil)
|
|
|
|
|
2023-03-30 21:19:04 -06:00
|
|
|
if child, ok := child.(tomo.Focusable); ok {
|
2023-03-14 23:41:23 -06:00
|
|
|
if child.Focused() {
|
|
|
|
child.HandleUnfocus()
|
2023-03-11 16:00:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisownAll removes all child elements from the container at once.
|
|
|
|
func (element *DocumentContainer) DisownAll () {
|
|
|
|
for _, entry := range element.children {
|
|
|
|
element.clearChildEventHandlers(entry.Element)
|
|
|
|
}
|
|
|
|
element.children = nil
|
|
|
|
|
2023-03-16 23:52:26 -06:00
|
|
|
element.updateMinimumSize()
|
2023-03-11 16:00:29 -07:00
|
|
|
if element.core.HasImage() && !element.warping {
|
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Children returns a slice containing this element's children.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *DocumentContainer) Children () (children []tomo.Element) {
|
|
|
|
children = make([]tomo.Element, len(element.children))
|
2023-03-11 16:00:29 -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 *DocumentContainer) 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-03-30 21:19:04 -06:00
|
|
|
func (element *DocumentContainer) Child (index int) (child tomo.Element) {
|
2023-03-11 16:00:29 -07:00
|
|
|
if index < 0 || index > len(element.children) { return }
|
|
|
|
return element.children[index].Element
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-03-30 21:19:04 -06:00
|
|
|
func (element *DocumentContainer) ChildAt (point image.Point) (child tomo.Element) {
|
2023-03-11 16:00:29 -07:00
|
|
|
for _, entry := range element.children {
|
|
|
|
if point.In(entry.Bounds) {
|
|
|
|
child = entry.Element
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *DocumentContainer) redoAll () {
|
|
|
|
if !element.core.HasImage() { return }
|
|
|
|
|
|
|
|
// do a layout
|
|
|
|
element.doLayout()
|
2023-03-11 18:04:08 -07:00
|
|
|
|
|
|
|
maxScrollHeight := element.maxScrollHeight()
|
|
|
|
if element.scroll.Y > maxScrollHeight {
|
|
|
|
element.scroll.Y = maxScrollHeight
|
|
|
|
element.doLayout()
|
|
|
|
}
|
2023-03-11 16:00:29 -07:00
|
|
|
|
|
|
|
// draw a background
|
|
|
|
rocks := make([]image.Rectangle, len(element.children))
|
|
|
|
for index, entry := range element.children {
|
|
|
|
rocks[index] = entry.Bounds
|
|
|
|
}
|
|
|
|
pattern := element.theme.Pattern (
|
2023-03-30 23:06:29 -06:00
|
|
|
tomo.PatternBackground,
|
|
|
|
tomo.State { })
|
2023-03-11 23:15:36 -07:00
|
|
|
artist.DrawShatter(element.core, pattern, element.Bounds(), rocks...)
|
2023-03-11 16:00:29 -07:00
|
|
|
|
|
|
|
element.partition()
|
2023-03-30 21:19:04 -06:00
|
|
|
if parent, ok := element.core.Parent().(tomo.ScrollableParent); ok {
|
2023-03-14 23:41:23 -06:00
|
|
|
parent.NotifyScrollBoundsChange(element)
|
2023-03-11 16:00:29 -07:00
|
|
|
}
|
2023-03-17 00:00:19 -06:00
|
|
|
if element.onScrollBoundsChange != nil {
|
|
|
|
element.onScrollBoundsChange()
|
|
|
|
}
|
2023-03-11 16:00:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (element *DocumentContainer) partition () {
|
|
|
|
for _, entry := range element.children {
|
2023-03-14 23:41:23 -06:00
|
|
|
entry.DrawTo(nil, entry.Bounds, nil)
|
2023-03-11 16:00:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// cut our canvas up and give peices to child elements
|
|
|
|
for _, entry := range element.children {
|
|
|
|
if entry.Bounds.Overlaps(element.Bounds()) {
|
2023-03-11 23:15:36 -07:00
|
|
|
entry.DrawTo (
|
|
|
|
canvas.Cut(element.core, entry.Bounds),
|
2023-03-14 23:41:23 -06:00
|
|
|
entry.Bounds, func (region image.Rectangle) {
|
|
|
|
element.core.DamageRegion(region)
|
|
|
|
})
|
2023-03-11 16:00:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 01:25:46 -06:00
|
|
|
func (element *DocumentContainer) Window () tomo.Window {
|
|
|
|
return element.core.Window()
|
|
|
|
}
|
|
|
|
|
2023-03-14 23:41:23 -06:00
|
|
|
// NotifyMinimumSizeChange notifies the container that the minimum size of a
|
|
|
|
// child element has changed.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *DocumentContainer) NotifyMinimumSizeChange (child tomo.Element) {
|
2023-03-14 23:41:23 -06:00
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
|
2023-04-02 20:02:55 -06:00
|
|
|
// DrawBackground draws a portion of the container's background pattern within
|
|
|
|
// the specified bounds. The container will not push these changes.
|
|
|
|
func (element *DocumentContainer) DrawBackground (bounds image.Rectangle) {
|
|
|
|
element.core.DrawBackgroundBounds (
|
|
|
|
element.theme.Pattern(tomo.PatternBackground, tomo.State { }),
|
|
|
|
bounds)
|
|
|
|
}
|
|
|
|
|
2023-03-15 21:57:22 -06:00
|
|
|
// NotifyFlexibleHeightChange notifies the parent that the parameters
|
|
|
|
// affecting a child's flexible height have changed. This method is
|
|
|
|
// expected to be called by flexible child element when their content
|
|
|
|
// changes.
|
2023-03-30 21:19:04 -06:00
|
|
|
func (element *DocumentContainer) NotifyFlexibleHeightChange (child tomo.Flexible) {
|
2023-03-15 21:57:22 -06:00
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
|
2023-03-11 16:00:29 -07:00
|
|
|
// SetTheme sets the element's theme.
|
2023-03-30 23:06:29 -06:00
|
|
|
func (element *DocumentContainer) SetTheme (new tomo.Theme) {
|
2023-03-11 16:00:29 -07:00
|
|
|
if new == element.theme.Theme { return }
|
|
|
|
element.theme.Theme = new
|
|
|
|
element.Propagator.SetTheme(new)
|
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the element's configuration.
|
2023-03-30 23:06:29 -06:00
|
|
|
func (element *DocumentContainer) SetConfig (new tomo.Config) {
|
2023-03-11 16:00:29 -07:00
|
|
|
if new == element.config.Config { return }
|
|
|
|
element.Propagator.SetConfig(new)
|
|
|
|
element.redoAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollContentBounds returns the full content size of the element.
|
|
|
|
func (element *DocumentContainer) ScrollContentBounds () image.Rectangle {
|
|
|
|
return element.contentBounds
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollViewportBounds returns the size and position of the element's
|
|
|
|
// viewport relative to ScrollBounds.
|
|
|
|
func (element *DocumentContainer) ScrollViewportBounds () image.Rectangle {
|
2023-03-30 23:06:29 -06:00
|
|
|
padding := element.theme.Padding(tomo.PatternBackground)
|
2023-03-11 16:00:29 -07:00
|
|
|
bounds := padding.Apply(element.Bounds())
|
|
|
|
bounds = bounds.Sub(bounds.Min).Add(element.scroll)
|
|
|
|
return bounds
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrollTo scrolls the viewport to the specified point relative to
|
|
|
|
// ScrollBounds.
|
|
|
|
func (element *DocumentContainer) ScrollTo (position image.Point) {
|
|
|
|
if position.Y < 0 {
|
|
|
|
position.Y = 0
|
|
|
|
}
|
2023-03-11 16:27:16 -07:00
|
|
|
maxScrollHeight := element.maxScrollHeight()
|
|
|
|
if position.Y > maxScrollHeight {
|
|
|
|
position.Y = maxScrollHeight
|
|
|
|
}
|
2023-03-11 16:00:29 -07:00
|
|
|
element.scroll = position
|
|
|
|
if element.core.HasImage() && !element.warping {
|
|
|
|
element.redoAll()
|
|
|
|
element.core.DamageAll()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-15 21:56:00 -06:00
|
|
|
// OnScrollBoundsChange sets a function to be called when the element's viewport
|
|
|
|
// bounds, content bounds, or scroll axes change.
|
|
|
|
func (element *DocumentContainer) OnScrollBoundsChange (callback func ()) {
|
|
|
|
element.onScrollBoundsChange = callback
|
|
|
|
}
|
|
|
|
|
2023-03-11 16:27:16 -07:00
|
|
|
func (element *DocumentContainer) maxScrollHeight () (height int) {
|
2023-03-30 23:06:29 -06:00
|
|
|
padding := element.theme.Padding(tomo.PatternSunken)
|
2023-03-11 16:27:16 -07:00
|
|
|
viewportHeight := element.Bounds().Dy() - padding.Vertical()
|
|
|
|
height = element.contentBounds.Dy() - viewportHeight
|
|
|
|
if height < 0 { height = 0 }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-11 16:00:29 -07:00
|
|
|
// ScrollAxes returns the supported axes for scrolling.
|
|
|
|
func (element *DocumentContainer) ScrollAxes () (horizontal, vertical bool) {
|
|
|
|
return false, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *DocumentContainer) doLayout () {
|
2023-03-30 23:06:29 -06:00
|
|
|
margin := element.theme.Margin(tomo.PatternBackground)
|
|
|
|
padding := element.theme.Padding(tomo.PatternBackground)
|
2023-03-11 16:00:29 -07:00
|
|
|
bounds := padding.Apply(element.Bounds())
|
|
|
|
element.contentBounds = image.Rectangle { }
|
|
|
|
|
|
|
|
dot := bounds.Min.Sub(element.scroll)
|
2023-04-04 14:39:12 -06:00
|
|
|
xStart := dot.X
|
|
|
|
rowHeight := 0
|
|
|
|
|
|
|
|
nextLine := func () {
|
|
|
|
dot.X = xStart
|
|
|
|
dot.Y += margin.Y
|
|
|
|
dot.Y += rowHeight
|
|
|
|
rowHeight = 0
|
|
|
|
}
|
|
|
|
|
2023-03-11 16:00:29 -07:00
|
|
|
for index, entry := range element.children {
|
2023-04-04 14:39:12 -06:00
|
|
|
if dot.X > xStart && entry.Expand {
|
|
|
|
nextLine()
|
2023-03-11 16:00:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
width, height := entry.MinimumSize()
|
2023-04-04 14:39:12 -06:00
|
|
|
if width + dot.X > bounds.Dx() && !entry.Expand {
|
|
|
|
nextLine()
|
|
|
|
}
|
|
|
|
if width < bounds.Dx() && entry.Expand {
|
2023-03-11 16:00:29 -07:00
|
|
|
width = bounds.Dx()
|
|
|
|
}
|
2023-03-30 21:19:04 -06:00
|
|
|
if typedChild, ok := entry.Element.(tomo.Flexible); ok {
|
2023-03-11 16:00:29 -07:00
|
|
|
height = typedChild.FlexibleHeightFor(width)
|
|
|
|
}
|
2023-04-04 14:39:12 -06:00
|
|
|
if rowHeight < height {
|
|
|
|
rowHeight = height
|
|
|
|
}
|
2023-03-11 16:00:29 -07:00
|
|
|
|
|
|
|
entry.Bounds.Min = dot
|
|
|
|
entry.Bounds.Max = image.Pt(dot.X + width, dot.Y + height)
|
|
|
|
element.children[index] = entry
|
|
|
|
element.contentBounds = element.contentBounds.Union(entry.Bounds)
|
2023-04-04 14:39:12 -06:00
|
|
|
|
|
|
|
if entry.Expand {
|
|
|
|
nextLine()
|
|
|
|
} else {
|
|
|
|
dot.X += width + margin.X
|
|
|
|
}
|
2023-03-11 16:00:29 -07:00
|
|
|
}
|
2023-03-11 17:25:35 -07:00
|
|
|
|
2023-03-11 16:27:16 -07:00
|
|
|
element.contentBounds =
|
2023-03-16 23:52:26 -06:00
|
|
|
element.contentBounds.Sub(element.contentBounds.Min)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (element *DocumentContainer) updateMinimumSize () {
|
2023-03-30 23:06:29 -06:00
|
|
|
padding := element.theme.Padding(tomo.PatternBackground)
|
2023-03-16 23:52:26 -06:00
|
|
|
minimumWidth := 0
|
|
|
|
for _, entry := range element.children {
|
|
|
|
width, _ := entry.MinimumSize()
|
|
|
|
if width > minimumWidth {
|
|
|
|
minimumWidth = width
|
|
|
|
}
|
|
|
|
}
|
2023-03-11 17:25:35 -07:00
|
|
|
element.core.SetMinimumSize (
|
|
|
|
minimumWidth + padding.Horizontal(),
|
|
|
|
padding.Vertical())
|
2023-03-11 16:00:29 -07:00
|
|
|
}
|