Ok thats it next commit im getting rid of parent hooks

This commit is contained in:
2023-01-19 13:07:27 -05:00
parent df21aa9fd0
commit dcc7fcb251
6 changed files with 183 additions and 3 deletions

View File

@@ -0,0 +1,137 @@
package basic
import "image"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
// ScrollContainer is a container that is capable of holding a scrollable
// element.
type ScrollContainer struct {
*core.Core
core core.CoreControl
selected bool
child tomo.Scrollable
horizontal struct {
enabled bool
bounds image.Rectangle
}
vertical struct {
enabled bool
bounds image.Rectangle
}
}
// NewScrollContainer creates a new scroll container with the specified scroll
// bars.
func NewScrollContainer (horizontal, vertical bool) (element *ScrollContainer) {
element = &ScrollContainer { }
element.Core, element.core = core.NewCore(element)
element.updateMinimumSize()
element.horizontal.enabled = horizontal
element.vertical.enabled = vertical
return
}
// Resize resizes the scroll box.
func (element *ScrollContainer) Resize (width, height int) {
element.core.AllocateCanvas(width, height)
element.recalculate()
element.child.Resize (
element.vertical.bounds.Min.X,
element.horizontal.bounds.Min.Y)
element.draw()
}
// Adopt adds a scrollable element to the scroll container. The container can
// only contain one scrollable element at a time, and when a new one is adopted
// it replaces the last one.
func (element *ScrollContainer) Adopt (child tomo.Scrollable) {
// disown previous child if it exists
if element.child != nil {
element.child.SetParentHooks (tomo.ParentHooks { })
if previousChild, ok := element.child.(tomo.Selectable); ok {
if previousChild.Selected() {
previousChild.HandleDeselection()
}
}
}
// adopt new child
element.child = child
if child != nil {
child.SetParentHooks (tomo.ParentHooks {
// Draw: window.childDrawCallback,
// MinimumSizeChange: window.childMinimumSizeChangeCallback,
// FlexibleHeightChange: window.resizeChildToFit,
// SelectionRequest: window.childSelectionRequestCallback,
})
// TODO: somehow inform the core that we do not in fact want to
// redraw the element.
element.updateMinimumSize()
if element.core.HasImage() {
element.recalculate()
element.child.Resize (
element.horizontal.bounds.Min.X,
element.vertical.bounds.Min.X)
element.draw()
}
}
}
func (element *ScrollContainer) recalculate () {
horizontal := &element.horizontal
vertical := &element.vertical
bounds := element.Bounds()
thickness := theme.Padding() * 2
// reset bounds
horizontal.bounds = image.Rectangle { }
vertical.bounds = image.Rectangle { }
// if enabled, give substance to the bars
if horizontal.enabled {
horizontal.bounds.Max.X = bounds.Max.X - thickness
horizontal.bounds.Max.Y = thickness
}
if vertical.enabled {
vertical.bounds.Max.X = thickness
vertical.bounds.Max.Y = bounds.Max.Y - thickness
}
// move the bars to the edge of the element
horizontal.bounds = horizontal.bounds.Add (
bounds.Max.Sub(horizontal.bounds.Max))
vertical.bounds = vertical.bounds.Add (
bounds.Max.Sub(vertical.bounds.Max))
}
func (element *ScrollContainer) draw () {
artist.Paste(element.core, element.child, image.Point { })
element.drawHorizontalBar()
element.drawVerticalBar()
}
func (element *ScrollContainer) drawHorizontalBar () {
}
func (element *ScrollContainer) drawVerticalBar () {
}
func (element *ScrollContainer) updateMinimumSize () {
width := theme.Padding() * 2
height := theme.Padding() * 2
if element.child != nil {
childWidth, childHeight := element.child.MinimumSize()
width += childWidth
height += childHeight
}
element.core.SetMinimumSize(width, height)
}

View File

@@ -247,7 +247,9 @@ func (element *TextBox) ScrollTo (position image.Point) {
if element.core.HasImage () {
element.draw()
element.core.PushAll()
}}
}
element.core.NotifyContentBoundsChange()
}
// ScrollAxes returns the supported axes for scrolling.
func (element *TextBox) ScrollAxes () (horizontal, vertical bool) {
@@ -284,6 +286,7 @@ func (element *TextBox) scrollToCursor () {
element.scroll -= minX - cursorPosition.X
if element.scroll < 0 { element.scroll = 0 }
}
element.core.NotifyContentBoundsChange()
}
func (element *TextBox) draw () {
@@ -311,7 +314,6 @@ func (element *TextBox) draw () {
} else {
// draw input value
textBounds := element.valueDrawer.LayoutBounds()
println(textBounds.String())
offset := image.Point {
X: theme.Padding() - element.scroll,
Y: theme.Padding(),

View File

@@ -149,6 +149,12 @@ func (control CoreControl) NotifyFlexibleHeightChange () {
control.core.hooks.RunFlexibleHeightChange()
}
// NotifyContentBoundsChange notifies the parent element that this element's
// inner content bounds or scroll position have changed.
func (control CoreControl) NotifyContentBoundsChange () {
control.core.hooks.RunContentBoundsChange()
}
// ConstrainSize contstrains the specified width and height to the minimum width
// and height, and returns wether or not anything ended up being constrained.
func (control CoreControl) ConstrainSize (