Ok thats it next commit im getting rid of parent hooks
This commit is contained in:
parent
df21aa9fd0
commit
dcc7fcb251
@ -77,7 +77,7 @@ func (backend *Backend) NewWindow (
|
|||||||
|
|
||||||
func (window *Window) Adopt (child tomo.Element) {
|
func (window *Window) Adopt (child tomo.Element) {
|
||||||
if window.child != nil {
|
if window.child != nil {
|
||||||
child.SetParentHooks (tomo.ParentHooks { })
|
window.child.SetParentHooks (tomo.ParentHooks { })
|
||||||
if previousChild, ok := window.child.(tomo.Selectable); ok {
|
if previousChild, ok := window.child.(tomo.Selectable); ok {
|
||||||
if previousChild.Selected() {
|
if previousChild.Selected() {
|
||||||
previousChild.HandleDeselection()
|
previousChild.HandleDeselection()
|
||||||
|
@ -84,6 +84,14 @@ func (hooks ParentHooks) RunSelectionMotionRequest (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RunContentBoundsChange runs the ContentBoundsChange hook if it is not nil. If
|
||||||
|
// it is nil, it does nothing.
|
||||||
|
func (hooks ParentHooks) RunContentBoundsChange () {
|
||||||
|
if hooks.ContentBoundsChange != nil {
|
||||||
|
hooks.ContentBoundsChange()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Element represents a basic on-screen object.
|
// Element represents a basic on-screen object.
|
||||||
type Element interface {
|
type Element interface {
|
||||||
// Element must implement the Canvas interface. Elements should start
|
// Element must implement the Canvas interface. Elements should start
|
||||||
|
137
elements/basic/scrollcontainer.go
Normal file
137
elements/basic/scrollcontainer.go
Normal 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)
|
||||||
|
}
|
@ -247,7 +247,9 @@ func (element *TextBox) ScrollTo (position image.Point) {
|
|||||||
if element.core.HasImage () {
|
if element.core.HasImage () {
|
||||||
element.draw()
|
element.draw()
|
||||||
element.core.PushAll()
|
element.core.PushAll()
|
||||||
}}
|
}
|
||||||
|
element.core.NotifyContentBoundsChange()
|
||||||
|
}
|
||||||
|
|
||||||
// ScrollAxes returns the supported axes for scrolling.
|
// ScrollAxes returns the supported axes for scrolling.
|
||||||
func (element *TextBox) ScrollAxes () (horizontal, vertical bool) {
|
func (element *TextBox) ScrollAxes () (horizontal, vertical bool) {
|
||||||
@ -284,6 +286,7 @@ func (element *TextBox) scrollToCursor () {
|
|||||||
element.scroll -= minX - cursorPosition.X
|
element.scroll -= minX - cursorPosition.X
|
||||||
if element.scroll < 0 { element.scroll = 0 }
|
if element.scroll < 0 { element.scroll = 0 }
|
||||||
}
|
}
|
||||||
|
element.core.NotifyContentBoundsChange()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (element *TextBox) draw () {
|
func (element *TextBox) draw () {
|
||||||
@ -311,7 +314,6 @@ func (element *TextBox) draw () {
|
|||||||
} else {
|
} else {
|
||||||
// draw input value
|
// draw input value
|
||||||
textBounds := element.valueDrawer.LayoutBounds()
|
textBounds := element.valueDrawer.LayoutBounds()
|
||||||
println(textBounds.String())
|
|
||||||
offset := image.Point {
|
offset := image.Point {
|
||||||
X: theme.Padding() - element.scroll,
|
X: theme.Padding() - element.scroll,
|
||||||
Y: theme.Padding(),
|
Y: theme.Padding(),
|
||||||
|
@ -149,6 +149,12 @@ func (control CoreControl) NotifyFlexibleHeightChange () {
|
|||||||
control.core.hooks.RunFlexibleHeightChange()
|
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
|
// ConstrainSize contstrains the specified width and height to the minimum width
|
||||||
// and height, and returns wether or not anything ended up being constrained.
|
// and height, and returns wether or not anything ended up being constrained.
|
||||||
func (control CoreControl) ConstrainSize (
|
func (control CoreControl) ConstrainSize (
|
||||||
|
27
examples/scroll/main.go
Normal file
27
examples/scroll/main.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "git.tebibyte.media/sashakoshka/tomo"
|
||||||
|
import "git.tebibyte.media/sashakoshka/tomo/layouts"
|
||||||
|
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
|
||||||
|
import _ "git.tebibyte.media/sashakoshka/tomo/backends/x"
|
||||||
|
|
||||||
|
func main () {
|
||||||
|
tomo.Run(run)
|
||||||
|
}
|
||||||
|
|
||||||
|
func run () {
|
||||||
|
window, _ := tomo.NewWindow(2, 2)
|
||||||
|
window.SetTitle("Scroll")
|
||||||
|
container := basic.NewContainer(layouts.Vertical { true, true })
|
||||||
|
window.Adopt(container)
|
||||||
|
|
||||||
|
container.Adopt(basic.NewLabel("look at this non sense", false), false)
|
||||||
|
|
||||||
|
textBox := basic.NewTextBox("", "sample text sample text")
|
||||||
|
scrollContainer := basic.NewScrollContainer(true, true)
|
||||||
|
scrollContainer.Adopt(textBox)
|
||||||
|
container.Adopt(scrollContainer, false)
|
||||||
|
|
||||||
|
window.OnClose(tomo.Stop)
|
||||||
|
window.Show()
|
||||||
|
}
|
Reference in New Issue
Block a user