This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
x/containerbox.go

249 lines
5.8 KiB
Go
Raw Normal View History

2023-07-02 06:52:14 +00:00
package x
2023-07-05 07:25:50 +00:00
import "image"
2023-07-02 06:52:14 +00:00
import "git.tebibyte.media/tomo/tomo"
2023-07-05 07:25:50 +00:00
import "git.tebibyte.media/tomo/tomo/event"
import "git.tebibyte.media/tomo/tomo/canvas"
type containerBox struct {
*box
hOverflow, vOverflow bool
2023-07-13 16:48:09 +00:00
hAlign, vAlign tomo.Align
2023-07-05 07:25:50 +00:00
contentBounds image.Rectangle
scroll image.Point
2023-07-16 04:42:47 +00:00
gap image.Point
children []tomo.Box
layout tomo.Layout
propagateEvents bool
2023-07-05 07:25:50 +00:00
on struct {
contentBoundsChange event.FuncBroadcaster
}
}
2023-07-02 06:52:14 +00:00
func (backend *Backend) NewContainerBox() tomo.ContainerBox {
2023-07-05 07:25:50 +00:00
box := &containerBox {
2023-07-16 04:42:47 +00:00
box: backend.NewBox().(*box),
propagateEvents: true,
2023-07-05 07:25:50 +00:00
}
box.drawer = box
box.outer = box
return box
}
func (this *containerBox) Box () tomo.Box {
return this
}
func (this *containerBox) SetOverflow (horizontal, vertical bool) {
if this.hOverflow == horizontal && this.vOverflow == vertical { return }
this.hOverflow = horizontal
this.vOverflow = vertical
this.invalidateLayout()
}
2023-07-13 16:48:09 +00:00
func (this *containerBox) SetAlign (x, y tomo.Align) {
if this.hAlign == x && this.vAlign == y { return }
this.hAlign = x
this.vAlign = y
this.invalidateLayout()
}
2023-07-05 07:25:50 +00:00
func (this *containerBox) ContentBounds () image.Rectangle {
return this.contentBounds
}
func (this *containerBox) ScrollTo (point image.Point) {
// TODO: constrain scroll
this.scroll = point
this.invalidateLayout()
}
func (this *containerBox) OnContentBoundsChange (callback func()) event.Cookie {
return this.on.contentBoundsChange.Connect(callback)
}
func (this *containerBox) SetPropagateEvents (propagate bool) {
2023-07-16 04:42:47 +00:00
this.propagateEvents = propagate
}
2023-07-06 06:08:10 +00:00
func (this *containerBox) SetGap (gap image.Point) {
2023-07-05 07:25:50 +00:00
if this.gap == gap { return }
this.gap = gap
this.invalidateLayout()
2023-07-12 05:17:12 +00:00
this.recalculateMinimumSize()
2023-07-05 07:25:50 +00:00
}
func (this *containerBox) Add (child tomo.Object) {
box := assertAnyBox(child.Box())
if indexOf(this.children, tomo.Box(box)) > -1 { return }
2023-07-05 07:25:50 +00:00
box.setParent(this)
this.children = append(this.children, box)
this.invalidateLayout()
2023-07-12 05:17:12 +00:00
this.recalculateMinimumSize()
2023-07-05 07:25:50 +00:00
}
func (this *containerBox) Delete (child tomo.Object) {
box := assertAnyBox(child.Box())
index := indexOf(this.children, tomo.Box(box))
2023-07-05 07:25:50 +00:00
if index < 0 { return }
box.setParent(nil)
this.children = remove(this.children, index)
this.invalidateLayout()
2023-07-12 05:17:12 +00:00
this.recalculateMinimumSize()
2023-07-05 07:25:50 +00:00
}
func (this *containerBox) Insert (child, before tomo.Object) {
box := assertAnyBox(child.Box())
if indexOf(this.children, tomo.Box(box)) > -1 { return }
2023-07-05 07:25:50 +00:00
beforeBox := assertAnyBox(before.Box())
index := indexOf(this.children, tomo.Box(beforeBox))
2023-07-05 07:25:50 +00:00
if index < 0 { return }
box.setParent(this)
this.children = insert(this.children, index, tomo.Box(box))
2023-07-05 07:25:50 +00:00
this.invalidateLayout()
2023-07-12 05:17:12 +00:00
this.recalculateMinimumSize()
2023-07-05 07:25:50 +00:00
}
func (this *containerBox) Clear () {
for _, box := range this.children {
box.(anyBox).setParent(nil)
2023-07-05 07:25:50 +00:00
}
this.children = nil
this.invalidateLayout()
2023-07-12 05:17:12 +00:00
this.recalculateMinimumSize()
2023-07-05 07:25:50 +00:00
}
func (this *containerBox) Length () int {
return len(this.children)
}
func (this *containerBox) At (index int) tomo.Object {
if index < 0 || index >= len(this.children) {
return nil
}
return this.children[index]
}
func (this *containerBox) SetLayout (layout tomo.Layout) {
this.layout = layout
this.invalidateLayout()
2023-07-12 05:17:12 +00:00
this.recalculateMinimumSize()
2023-07-05 07:25:50 +00:00
}
func (this *containerBox) Draw (can canvas.Canvas) {
2023-07-12 05:17:12 +00:00
if can == nil { return }
2023-07-05 07:25:50 +00:00
this.drawBorders(can)
pen := can.Pen()
pen.Fill(this.color)
rocks := make([]image.Rectangle, len(this.children))
for index, box := range this.children {
rocks[index] = box.Bounds()
}
2023-07-05 08:11:24 +00:00
for _, tile := range canvas.Shatter(this.bounds, rocks...) {
2023-07-05 07:25:50 +00:00
pen.Rectangle(tile)
}
}
func (this *containerBox) window () *window {
if this.parent == nil { return nil }
return this.parent.window()
}
func (this *containerBox) canvas () canvas.Canvas {
return this.box.canvas
}
func (this *containerBox) notifyMinimumSizeChange (child anyBox) {
this.recalculateMinimumSize()
size := child.MinimumSize()
bounds := child.Bounds()
if bounds.Dx() < size.X || bounds.Dy() < size.Y {
this.invalidateLayout()
}
}
2023-07-06 06:08:10 +00:00
func (this *containerBox) layoutHints () tomo.LayoutHints {
innerBounds := this.InnerBounds().Sub(this.scroll)
return tomo.LayoutHints {
Bounds: innerBounds,
OverflowX: this.hOverflow,
OverflowY: this.vOverflow,
2023-07-13 16:51:38 +00:00
AlignX: this.hAlign,
AlignY: this.vAlign,
2023-07-06 06:08:10 +00:00
Gap: this.gap,
}
}
2023-07-12 05:17:12 +00:00
func (this *containerBox) recalculateMinimumSize () {
if this.layout == nil {
this.SetMinimumSize(image.Point { })
return
}
minimum := this.layout.MinimumSize(this.layoutHints(), this.children)
minimum.X += this.padding.Horizontal()
minimum.Y += this.padding.Vertical()
2023-07-13 16:48:09 +00:00
borderSum := this.borderSum()
minimum.X += borderSum.Horizontal()
minimum.Y += borderSum.Vertical()
this.SetMinimumSize(minimum)
2023-07-12 05:17:12 +00:00
}
2023-07-05 07:25:50 +00:00
func (this *containerBox) doLayout () {
this.box.doLayout()
previousContentBounds := this.contentBounds
if this.layout != nil {
this.layout.Arrange(this.layoutHints(), this.children)
2023-07-05 07:25:50 +00:00
}
if previousContentBounds != this.contentBounds {
this.on.contentBoundsChange.Broadcast()
}
}
func (this *containerBox) recursiveRedo () {
this.doLayout()
this.doDraw()
for _, child := range this.children {
child.(anyBox).recursiveRedo()
2023-07-05 07:25:50 +00:00
}
2023-07-02 06:52:14 +00:00
}
2023-07-16 04:42:47 +00:00
func (this *containerBox) boxUnder (point image.Point) anyBox {
if this.propagateEvents {
for _, box := range this.children {
candidate := box.(anyBox).boxUnder(point)
if candidate != nil { return candidate }
}
}
return this.box.boxUnder(point)
}
2023-07-22 03:22:03 +00:00
func (this *containerBox) propagate (callback func (anyBox) bool) bool {
for _, box := range this.children {
box := box.(anyBox)
if !box.propagate(callback) { return false }
}
return callback(this)
}
func (this *containerBox) propagateAlt (callback func (anyBox) bool) bool {
if !callback(this) { return false}
for _, box := range this.children {
box := box.(anyBox)
if !box.propagateAlt(callback) { return false }
}
return true
}