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

355 lines
8.7 KiB
Go
Raw Normal View History

2023-07-02 00:52:14 -06:00
package x
2023-07-05 01:25:50 -06:00
import "image"
import "image/color"
2023-07-02 00:52:14 -06:00
import "git.tebibyte.media/tomo/tomo"
2023-07-05 01:25:50 -06: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 10:48:09 -06:00
hAlign, vAlign tomo.Align
2023-07-05 01:25:50 -06:00
contentBounds image.Rectangle
scroll image.Point
2023-09-09 13:05:08 -06:00
capture [4]bool
2023-07-15 22:42:47 -06:00
gap image.Point
children []tomo.Box
layout tomo.Layout
2023-07-05 01:25:50 -06:00
on struct {
contentBoundsChange event.FuncBroadcaster
}
}
2023-07-02 00:52:14 -06:00
func (backend *Backend) NewContainerBox() tomo.ContainerBox {
2023-09-09 13:05:08 -06:00
this := &containerBox { }
this.box = backend.newBox(this)
return this
2023-07-05 01:25:50 -06:00
}
func (this *containerBox) SetColor (c color.Color) {
this.box.SetColor(c)
this.invalidateTransparentChildren()
}
func (this *containerBox) SetTexture (texture canvas.Texture) {
this.box.SetTexture(texture)
this.invalidateTransparentChildren()
}
2023-07-05 01:25:50 -06:00
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 10:48:09 -06: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 01:25:50 -06:00
func (this *containerBox) ContentBounds () image.Rectangle {
return this.contentBounds
}
func (this *containerBox) ScrollTo (point image.Point) {
2024-05-13 15:43:26 -06:00
if this.scroll == point { return }
2023-07-05 01:25:50 -06:00
this.scroll = point
this.invalidateLayout()
}
func (this *containerBox) OnContentBoundsChange (callback func()) event.Cookie {
return this.on.contentBoundsChange.Connect(callback)
}
2024-05-13 15:43:26 -06:00
func (this *containerBox) CaptureDND (capture bool) {
2023-09-09 13:05:08 -06:00
this.capture[eventCategoryDND] = capture
}
2024-05-13 15:43:26 -06:00
func (this *containerBox) CaptureMouse (capture bool) {
2023-09-09 13:05:08 -06:00
this.capture[eventCategoryMouse] = capture
}
2024-05-13 15:43:26 -06:00
func (this *containerBox) CaptureScroll (capture bool) {
2023-09-09 13:05:08 -06:00
this.capture[eventCategoryScroll] = capture
}
2024-05-13 15:43:26 -06:00
func (this *containerBox) CaptureKeyboard (capture bool) {
2023-09-09 13:05:08 -06:00
this.capture[eventCategoryKeyboard] = capture
2023-07-15 22:42:47 -06:00
}
2023-07-06 00:08:10 -06:00
func (this *containerBox) SetGap (gap image.Point) {
2023-07-05 01:25:50 -06:00
if this.gap == gap { return }
this.gap = gap
this.invalidateLayout()
this.invalidateMinimum()
2023-07-05 01:25:50 -06:00
}
func (this *containerBox) Add (child tomo.Object) {
2023-08-11 23:03:34 -06:00
box := assertAnyBox(child.GetBox())
if indexOf(this.children, tomo.Box(box)) > -1 { return }
2023-07-05 01:25:50 -06:00
box.setParent(this)
box.flushActionQueue()
2023-07-05 01:25:50 -06:00
this.children = append(this.children, box)
this.invalidateLayout()
this.invalidateMinimum()
2023-07-05 01:25:50 -06:00
}
func (this *containerBox) Delete (child tomo.Object) {
2023-08-11 23:03:34 -06:00
box := assertAnyBox(child.GetBox())
index := indexOf(this.children, tomo.Box(box))
2023-07-05 01:25:50 -06:00
if index < 0 { return }
2023-07-05 01:25:50 -06:00
box.setParent(nil)
this.children = remove(this.children, index)
this.invalidateLayout()
this.invalidateMinimum()
2023-07-05 01:25:50 -06:00
}
func (this *containerBox) Insert (child, before tomo.Object) {
2023-08-11 23:03:34 -06:00
box := assertAnyBox(child.GetBox())
if indexOf(this.children, tomo.Box(box)) > -1 { return }
2023-07-05 01:25:50 -06:00
2023-08-11 23:03:34 -06:00
beforeBox := assertAnyBox(before.GetBox())
index := indexOf(this.children, tomo.Box(beforeBox))
2024-05-07 18:11:58 -06:00
if index < 0 {
this.children = append(this.children, tomo.Box(box))
} else {
this.children = insert(this.children, index, tomo.Box(box))
}
2023-07-05 01:25:50 -06:00
box.setParent(this)
2024-05-07 18:11:58 -06:00
2023-07-05 01:25:50 -06:00
this.invalidateLayout()
this.invalidateMinimum()
2023-07-05 01:25:50 -06:00
}
func (this *containerBox) Clear () {
for _, box := range this.children {
box.(anyBox).setParent(nil)
2023-07-05 01:25:50 -06:00
}
this.children = nil
this.invalidateLayout()
this.invalidateMinimum()
2023-07-05 01:25:50 -06: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()
this.invalidateMinimum()
2023-07-05 01:25:50 -06:00
}
func (this *containerBox) Draw (can canvas.Canvas) {
2023-07-11 23:17:12 -06:00
if can == nil { return }
2023-07-05 01:25:50 -06:00
rocks := make([]image.Rectangle, len(this.children))
for index, box := range this.children {
rocks[index] = box.Bounds()
}
2023-07-05 02:11:24 -06:00
for _, tile := range canvas.Shatter(this.bounds, rocks...) {
2023-08-29 13:52:24 -06:00
clipped := can.Clip(tile)
if this.transparent() && this.parent != nil {
2023-08-29 13:52:24 -06:00
this.parent.drawBackgroundPart(clipped)
}
2023-08-29 13:52:24 -06:00
if clipped == nil { continue }
pen := clipped.Pen()
pen.Fill(this.color)
pen.Texture(this.texture)
pen.Rectangle(this.innerClippingBounds)
}
}
func (this *containerBox) drawBackgroundPart (can canvas.Canvas) {
if can == nil { return }
pen := can.Pen()
pen.Fill(this.color)
pen.Texture(this.texture)
if this.transparent() && this.parent != nil {
this.parent.drawBackgroundPart(can)
2023-07-05 01:25:50 -06:00
}
2023-08-29 13:52:24 -06:00
pen.Rectangle(this.innerClippingBounds)
2023-07-05 01:25:50 -06:00
}
func (this *containerBox) invalidateTransparentChildren () {
window := this.window()
2023-09-09 13:05:08 -06:00
if window == nil { return }
for _, box := range this.children {
box := assertAnyBox(box)
if box.transparent() {
window.invalidateDraw(box)
}
}
}
func (this *containerBox) flushActionQueue () {
for _, box := range this.children {
box.(anyBox).flushActionQueue()
}
this.box.flushActionQueue()
}
2023-07-05 01:25:50 -06:00
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.invalidateMinimum()
size := child.MinimumSize()
bounds := child.Bounds()
if bounds.Dx() < size.X || bounds.Dy() < size.Y {
this.invalidateLayout()
}
}
2024-05-13 15:43:26 -06:00
func (this *containerBox) boundedLayoutHints () tomo.LayoutHints {
hints := this.layoutHints()
hints.Bounds = this.ContentBounds().Add(this.InnerBounds().Min)
return hints
}
2023-07-06 00:08:10 -06:00
func (this *containerBox) layoutHints () tomo.LayoutHints {
return tomo.LayoutHints {
OverflowX: this.hOverflow,
OverflowY: this.vOverflow,
2023-07-13 10:51:38 -06:00
AlignX: this.hAlign,
AlignY: this.vAlign,
2023-07-06 00:08:10 -06:00
Gap: this.gap,
}
}
func (this *containerBox) contentMinimum () image.Point {
minimum := this.box.contentMinimum()
if this.layout != nil {
2024-05-13 15:43:26 -06:00
layoutMinimum := this.layout.MinimumSize (
this.boundedLayoutHints(),
this.children)
if this.hOverflow { layoutMinimum.X = 0 }
if this.vOverflow { layoutMinimum.Y = 0 }
minimum = minimum.Add(layoutMinimum)
}
return minimum
2023-07-11 23:17:12 -06:00
}
2023-07-05 01:25:50 -06:00
func (this *containerBox) doLayout () {
this.box.doLayout()
previousContentBounds := this.contentBounds
2024-05-13 15:43:26 -06:00
// by default, use innerBounds for contentBounds. if a direction
// overflows, use the layout's minimum size for it.
var minimum image.Point
if this.layout != nil {
minimum = this.layout.MinimumSize (
this.layoutHints(),
this.children)
}
innerBounds := this.InnerBounds()
this.contentBounds = innerBounds
if this.hOverflow { this.contentBounds.Max.X = this.contentBounds.Min.X + minimum.X }
if this.vOverflow { this.contentBounds.Max.Y = this.contentBounds.Min.Y + minimum.Y }
// offset the content bounds by the scroll so children can be positioned
// accordingly.
this.constrainScroll()
2024-05-13 17:35:03 -06:00
this.contentBounds = this.contentBounds.Add(this.scroll).Sub(innerBounds.Min)
2024-05-13 15:43:26 -06:00
// arrange children
2023-07-05 01:25:50 -06:00
if this.layout != nil {
2024-05-13 15:43:26 -06:00
this.layout.Arrange(this.boundedLayoutHints(), this.children)
2023-07-05 01:25:50 -06:00
}
2024-05-13 15:43:26 -06:00
2023-07-05 01:25:50 -06:00
if previousContentBounds != this.contentBounds {
this.on.contentBoundsChange.Broadcast()
2024-05-13 15:43:26 -06:00
}
}
func (this *containerBox) constrainScroll () {
innerBounds := this.InnerBounds()
width := this.contentBounds.Dx()
height := this.contentBounds.Dy()
// X
if width <= innerBounds.Dx() {
this.scroll.X = 0
2024-05-13 17:35:03 -06:00
} else if this.scroll.X > 0 {
2024-05-13 15:43:26 -06:00
this.scroll.X = 0
2024-05-13 17:35:03 -06:00
} else if this.scroll.X < innerBounds.Dx() - width {
this.scroll.X = innerBounds.Dx() - width
2024-05-13 15:43:26 -06:00
}
// Y
if height <= innerBounds.Dy() {
this.scroll.Y = 0
2024-05-13 17:35:03 -06:00
} else if this.scroll.Y > 0 {
2024-05-13 15:43:26 -06:00
this.scroll.Y = 0
2024-05-13 17:35:03 -06:00
} else if this.scroll.Y < innerBounds.Dy() - height {
this.scroll.Y = innerBounds.Dy() - height
2023-07-05 01:25:50 -06:00
}
}
func (this *containerBox) recursiveRedo () {
this.doLayout()
this.doDraw()
for _, child := range this.children {
child.(anyBox).recursiveRedo()
2023-07-05 01:25:50 -06:00
}
2023-07-02 00:52:14 -06:00
}
2023-07-15 22:42:47 -06:00
2023-09-09 13:05:08 -06:00
func (this *containerBox) boxUnder (point image.Point, category eventCategory) anyBox {
if !this.capture[category] {
2023-07-15 22:42:47 -06:00
for _, box := range this.children {
2023-09-09 13:05:08 -06:00
candidate := box.(anyBox).boxUnder(point, category)
2023-07-15 22:42:47 -06:00
if candidate != nil { return candidate }
}
}
2023-09-09 13:05:08 -06:00
return this.box.boxUnder(point, category)
2023-07-15 22:42:47 -06:00
}
2023-07-21 21:22:03 -06: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}
2023-07-21 21:22:03 -06:00
for _, box := range this.children {
box := box.(anyBox)
if !box.propagateAlt(callback) { return false }
}
2023-07-21 21:22:03 -06:00
return true
}
2023-09-09 13:05:08 -06:00
func (this *containerBox) captures (category eventCategory) bool {
return this.capture[category]
}