Container no longer embeds tomo.ContainerBox

Progress on #7
This commit is contained in:
Sasha Koshka 2024-08-24 15:02:17 -04:00
parent 7bcb4cf823
commit c889838c9c
3 changed files with 82 additions and 13 deletions

View File

@ -1,6 +1,8 @@
package objects package objects
import "image"
import "git.tebibyte.media/tomo/tomo" import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/tomo/event"
// Container is an object that can contain other objects. It can be used as a // Container is an object that can contain other objects. It can be used as a
// primitive for building more complex layouts. It has two variants: an outer // primitive for building more complex layouts. It has two variants: an outer
@ -8,14 +10,14 @@ import "git.tebibyte.media/tomo/tomo"
// its edges, whereas the inner container does not. The container will have a // its edges, whereas the inner container does not. The container will have a
// corresponding object role variation of either "outer" or "inner". // corresponding object role variation of either "outer" or "inner".
type Container struct { type Container struct {
tomo.ContainerBox box tomo.ContainerBox
} }
func newContainer (layout tomo.Layout, children ...tomo.Object) *Container { func newContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := &Container { this := &Container {
ContainerBox: tomo.NewContainerBox(), box: tomo.NewContainerBox(),
} }
this.SetAttr(tomo.ALayout(layout)) this.box.SetAttr(tomo.ALayout(layout))
for _, child := range children { for _, child := range children {
this.Add(child) this.Add(child)
} }
@ -27,8 +29,8 @@ func newContainer (layout tomo.Layout, children ...tomo.Object) *Container {
// window, tab pane, etc. // window, tab pane, etc.
func NewOuterContainer (layout tomo.Layout, children ...tomo.Object) *Container { func NewOuterContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := newContainer(layout, children...) this := newContainer(layout, children...)
this.SetRole(tomo.R("objects", "Container")) this.box.SetRole(tomo.R("objects", "Container"))
this.SetTag("outer", true) this.box.SetTag("outer", true)
return this return this
} }
@ -36,20 +38,87 @@ func NewOuterContainer (layout tomo.Layout, children ...tomo.Object) *Container
// around it. It is meant to be used as a root container for a ScrollContainer. // around it. It is meant to be used as a root container for a ScrollContainer.
func NewSunkenContainer (layout tomo.Layout, children ...tomo.Object) *Container { func NewSunkenContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := newContainer(layout, children...) this := newContainer(layout, children...)
this.SetRole(tomo.R("objects", "Container")) this.box.SetRole(tomo.R("objects", "Container"))
this.SetTag("sunken", true) this.box.SetTag("sunken", true)
return this return this
} }
// NewInnerContainer creates a new container that has no padding around it. // NewInnerContainer creates a new container that has no padding around it.
func NewInnerContainer (layout tomo.Layout, children ...tomo.Object) *Container { func NewInnerContainer (layout tomo.Layout, children ...tomo.Object) *Container {
this := newContainer(layout, children...) this := newContainer(layout, children...)
this.SetRole(tomo.R("objects", "Container")) this.box.SetRole(tomo.R("objects", "Container"))
this.SetTag("inner", true) this.box.SetTag("inner", true)
return this return this
} }
// GetBox returns the underlying box.
func (this *Container) GetBox () tomo.Box {
return this.box
}
// ContentBounds returns the bounds of the inner content of the container
// relative to the container's InnerBounds.
func (this *Container) ContentBounds () image.Rectangle {
return this.box.ContentBounds()
}
// ScrollTo shifts the origin of the container's content to the origin of the
// container's InnerBounds, offset by the given point.
func (this *Container) ScrollTo (position image.Point) {
this.box.ScrollTo(position)
}
// OnContentBoundsChange specifies a function to be called when the container's
// ContentBounds or InnerBounds changes.
func (this *Container) OnContentBoundsChange (callback func ()) event.Cookie {
return this.box.OnContentBoundsChange(callback)
}
// SetLayout sets the layout of the container. // SetLayout sets the layout of the container.
func (this *Container) SetLayout (layout tomo.Layout) { func (this *Container) SetLayout (layout tomo.Layout) {
this.SetAttr(tomo.ALayout(layout)) this.box.SetAttr(tomo.ALayout(layout))
}
// SetAlign sets the X and Y alignment of the container.
func (this *Container) SetAlign (x, y tomo.Align) {
this.box.SetAttr(tomo.AAlign(x, y))
}
// SetOverflow sets the X and Y overflow of the container.
func (this *Container) SetOverflow (x, y bool) {
this.box.SetAttr(tomo.AOverflow(x, y))
}
// Add appends a child object. If the object is already a child of another
// object, it will be removed from that object first.
func (this *Container) Add (object tomo.Object) {
this.box.Add(object)
}
// Remove removes a child object, if it is a child of this container.
func (this *Container) Remove (object tomo.Object) {
this.box.Remove(object)
}
// Insert inserts a child object before a specified object. If the before object
// is nil or is not contained within this container, the inserted object is
// appended. If the inserted object is already a child of another object, it
// will be removed from that object first.
func (this *Container) Insert (child tomo.Object, before tomo.Object) {
this.box.Insert(child, before)
}
// Clear removes all child objects.
func (this *Container) Clear () {
this.box.Clear()
}
// Len returns hte amount of child objects.
func (this *Container) Len () int {
return this.box.Len()
}
// At returns the child object at the specified index.
func (this *Container) At (index int) tomo.Object {
return this.box.At(index)
} }

View File

@ -16,7 +16,7 @@ type DialogKind int; const (
// Dialog is a modal dialog window. // Dialog is a modal dialog window.
type Dialog struct { type Dialog struct {
tomo.Window tomo.Window
controlRow tomo.ContainerBox controlRow *Container
} }
type clickable interface { type clickable interface {
@ -64,7 +64,7 @@ func NewDialog (kind DialogKind, parent tomo.Window, title, message string, opti
} }
} }
dialog.controlRow = NewInnerContainer(layouts.ContractHorizontal, options...) dialog.controlRow = NewInnerContainer(layouts.ContractHorizontal, options...)
dialog.controlRow.SetAttr(tomo.AAlign(tomo.AlignEnd, tomo.AlignEnd)) dialog.controlRow.SetAlign(tomo.AlignEnd, tomo.AlignEnd)
dialog.SetRoot(NewOuterContainer ( dialog.SetRoot(NewOuterContainer (
layouts.Column { true, false }, layouts.Column { true, false },

View File

@ -129,7 +129,7 @@ func (this *Swatch) Choose () {
layouts.ContractHorizontal, layouts.ContractHorizontal,
cancelButton, cancelButton,
okButton) okButton)
controlRow.SetAttr(tomo.AAlign(tomo.AlignEnd, tomo.AlignMiddle)) controlRow.SetAlign(tomo.AlignEnd, tomo.AlignMiddle)
window.SetRoot(NewOuterContainer ( window.SetRoot(NewOuterContainer (
layouts.Column { true, false }, layouts.Column { true, false },
colorPicker, colorPicker,