Basic elements now conform to new API

This commit is contained in:
Sasha Koshka 2023-03-15 01:41:23 -04:00
parent f4799ba03d
commit 0015820fac
15 changed files with 137 additions and 272 deletions

View File

@ -35,9 +35,9 @@ type Button struct {
func NewButton (text string) (element *Button) {
element = &Button { showText: true }
element.theme.Case = theme.C("basic", "button")
element.Core, element.core = core.NewCore(element.drawAll)
element.Core, element.core = core.NewCore(element, element.drawAll)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.drawAndPush)
element.focusableControl = core.NewFocusableCore(element.core, element.drawAndPush)
element.SetText(text)
return
}
@ -61,9 +61,6 @@ func (element *Button) HandleMouseUp (x, y int, button input.Button) {
element.drawAndPush()
}
func (element *Button) HandleMouseMove (x, y int) { }
func (element *Button) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
func (element *Button) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
if !element.Enabled() { return }
if key == input.KeyEnter {

View File

@ -29,9 +29,9 @@ type Checkbox struct {
func NewCheckbox (text string, checked bool) (element *Checkbox) {
element = &Checkbox { checked: checked }
element.theme.Case = theme.C("basic", "checkbox")
element.Core, element.core = core.NewCore(element.draw)
element.Core, element.core = core.NewCore(element, element.draw)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.redo)
element.focusableControl = core.NewFocusableCore(element.core, element.redo)
element.SetText(text)
return
}
@ -65,9 +65,6 @@ func (element *Checkbox) HandleMouseUp (x, y int, button input.Button) {
}
}
func (element *Checkbox) HandleMouseMove (x, y int) { }
func (element *Checkbox) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
func (element *Checkbox) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
if key == input.KeyEnter {
element.pressed = true

View File

@ -32,8 +32,8 @@ type Container struct {
func NewContainer (layout layouts.Layout) (element *Container) {
element = &Container { }
element.theme.Case = theme.C("basic", "container")
element.Core, element.core = core.NewCore(element.redoAll)
element.Propagator = core.NewPropagator(element)
element.Core, element.core = core.NewCore(element, element.redoAll)
element.Propagator = core.NewPropagator(element, element.core)
element.SetLayout(layout)
return
}
@ -51,33 +51,13 @@ func (element *Container) SetLayout (layout layouts.Layout) {
// the element will expand (instead of contract to its minimum size), in
// whatever way is defined by the current layout.
func (element *Container) Adopt (child elements.Element, expand bool) {
// set event handlers
if child0, ok := child.(elements.Themeable); ok {
child0.SetTheme(element.theme.Theme)
}
if child0, ok := child.(elements.Configurable); ok {
child0.SetConfig(element.config.Config)
}
child.OnDamage (func (region canvas.Canvas) {
element.core.DamageRegion(region.Bounds())
})
child.OnMinimumSizeChange (func () {
// TODO: this could probably stand to be more efficient. I mean
// seriously?
element.updateMinimumSize()
element.redoAll()
element.core.DamageAll()
})
if child0, ok := child.(elements.Focusable); ok {
child0.OnFocusRequest (func () (granted bool) {
return element.childFocusRequestCallback(child0)
})
child0.OnFocusMotionRequest (
func (direction input.KeynavDirection) (granted bool) {
if element.onFocusMotionRequest == nil { return }
return element.onFocusMotionRequest(direction)
})
}
child.SetParent(element)
// add child
element.children = append (element.children, layouts.LayoutEntry {
@ -136,14 +116,12 @@ func (element *Container) Disown (child elements.Element) {
}
func (element *Container) clearChildEventHandlers (child elements.Element) {
child.DrawTo(nil, image.Rectangle { })
child.OnDamage(nil)
child.OnMinimumSizeChange(nil)
if child0, ok := child.(elements.Focusable); ok {
child0.OnFocusRequest(nil)
child0.OnFocusMotionRequest(nil)
if child0.Focused() {
child0.HandleUnfocus()
child.DrawTo(nil, image.Rectangle { }, nil)
child.SetParent(nil)
if child, ok := child.(elements.Focusable); ok {
if child.Focused() {
child.HandleUnfocus()
}
}
}
@ -200,7 +178,7 @@ func (element *Container) redoAll () {
// remove child canvasses so that any operations done in here will not
// cause a child to draw to a wack ass canvas.
for _, entry := range element.children {
entry.DrawTo(nil, entry.Bounds)
entry.DrawTo(nil, entry.Bounds, nil)
}
// do a layout
@ -220,10 +198,20 @@ func (element *Container) redoAll () {
for _, entry := range element.children {
entry.DrawTo (
canvas.Cut(element.core, entry.Bounds),
entry.Bounds)
entry.Bounds, func (region image.Rectangle) {
element.core.DamageRegion(region)
})
}
}
// NotifyMinimumSizeChange notifies the container that the minimum size of a
// child element has changed.
func (element *Container) NotifyMinimumSizeChange (child elements.Element) {
element.updateMinimumSize()
element.redoAll()
element.core.DamageAll()
}
// SetTheme sets the element's theme.
func (element *Container) SetTheme (new theme.Theme) {
if new == element.theme.Theme { return }
@ -241,32 +229,6 @@ func (element *Container) SetConfig (new config.Config) {
element.redoAll()
}
func (element *Container) OnFocusRequest (callback func () (granted bool)) {
element.onFocusRequest = callback
element.Propagator.OnFocusRequest(callback)
}
func (element *Container) OnFocusMotionRequest (
callback func (direction input.KeynavDirection) (granted bool),
) {
element.onFocusMotionRequest = callback
element.Propagator.OnFocusMotionRequest(callback)
}
func (element *Container) childFocusRequestCallback (
child elements.Focusable,
) (
granted bool,
) {
if element.onFocusRequest != nil && element.onFocusRequest() {
element.Propagator.HandleUnfocus()
element.Propagator.HandleFocus(input.KeynavDirectionNeutral)
return true
} else {
return false
}
}
func (element *Container) updateMinimumSize () {
margin := element.theme.Margin(theme.PatternBackground)
padding := element.theme.Padding(theme.PatternBackground)

View File

@ -1,7 +1,6 @@
package basicElements
import "image"
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
@ -22,18 +21,14 @@ type DocumentContainer struct {
config config.Wrapped
theme theme.Wrapped
onFocusRequest func () (granted bool)
onFocusMotionRequest func (input.KeynavDirection) (granted bool)
onScrollBoundsChange func ()
}
// NewDocumentContainer creates a new document container.
func NewDocumentContainer () (element *DocumentContainer) {
element = &DocumentContainer { }
element.theme.Case = theme.C("basic", "documentContainer")
element.Core, element.core = core.NewCore(element.redoAll)
element.Propagator = core.NewPropagator(element)
element.Core, element.core = core.NewCore(element, element.redoAll)
element.Propagator = core.NewPropagator(element, element.core)
return
}
@ -46,29 +41,12 @@ func (element *DocumentContainer) Adopt (child elements.Element) {
if child0, ok := child.(elements.Configurable); ok {
child0.SetConfig(element.config.Config)
}
child.OnDamage (func (region canvas.Canvas) {
element.core.DamageRegion(region.Bounds())
})
child.OnMinimumSizeChange (func () {
element.redoAll()
element.core.DamageAll()
})
if child0, ok := child.(elements.Flexible); ok {
child0.OnFlexibleHeightChange (func () {
element.redoAll()
element.core.DamageAll()
})
}
if child0, ok := child.(elements.Focusable); ok {
child0.OnFocusRequest (func () (granted bool) {
return element.childFocusRequestCallback(child0)
})
child0.OnFocusMotionRequest (
func (direction input.KeynavDirection) (granted bool) {
if element.onFocusMotionRequest == nil { return }
return element.onFocusMotionRequest(direction)
})
}
// if child0, ok := child.(elements.Flexible); ok {
// child0.OnFlexibleHeightChange (func () {
// element.redoAll()
// element.core.DamageAll()
// })
// }
// add child
element.children = append (element.children, layouts.LayoutEntry {
@ -123,14 +101,12 @@ func (element *DocumentContainer) Disown (child elements.Element) {
}
func (element *DocumentContainer) clearChildEventHandlers (child elements.Element) {
child.DrawTo(nil, image.Rectangle { })
child.OnDamage(nil)
child.OnMinimumSizeChange(nil)
if child0, ok := child.(elements.Focusable); ok {
child0.OnFocusRequest(nil)
child0.OnFocusMotionRequest(nil)
if child0.Focused() {
child0.HandleUnfocus()
child.DrawTo(nil, image.Rectangle { }, nil)
child.SetParent(nil)
if child, ok := child.(elements.Focusable); ok {
if child.Focused() {
child.HandleUnfocus()
}
}
}
@ -204,14 +180,14 @@ func (element *DocumentContainer) redoAll () {
artist.DrawShatter(element.core, pattern, element.Bounds(), rocks...)
element.partition()
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
func (element *DocumentContainer) partition () {
for _, entry := range element.children {
entry.DrawTo(nil, entry.Bounds)
entry.DrawTo(nil, entry.Bounds, nil)
}
// cut our canvas up and give peices to child elements
@ -219,11 +195,20 @@ func (element *DocumentContainer) partition () {
if entry.Bounds.Overlaps(element.Bounds()) {
entry.DrawTo (
canvas.Cut(element.core, entry.Bounds),
entry.Bounds)
entry.Bounds, func (region image.Rectangle) {
element.core.DamageRegion(region)
})
}
}
}
// NotifyMinimumSizeChange notifies the container that the minimum size of a
// child element has changed.
func (element *DocumentContainer) NotifyMinimumSizeChange (child elements.Element) {
element.redoAll()
element.core.DamageAll()
}
// SetTheme sets the element's theme.
func (element *DocumentContainer) SetTheme (new theme.Theme) {
if new == element.theme.Theme { return }
@ -239,18 +224,6 @@ func (element *DocumentContainer) SetConfig (new config.Config) {
element.redoAll()
}
func (element *DocumentContainer) OnFocusRequest (callback func () (granted bool)) {
element.onFocusRequest = callback
element.Propagator.OnFocusRequest(callback)
}
func (element *DocumentContainer) OnFocusMotionRequest (
callback func (direction input.KeynavDirection) (granted bool),
) {
element.onFocusMotionRequest = callback
element.Propagator.OnFocusMotionRequest(callback)
}
// ScrollContentBounds returns the full content size of the element.
func (element *DocumentContainer) ScrollContentBounds () image.Rectangle {
return element.contentBounds
@ -295,12 +268,6 @@ func (element *DocumentContainer) ScrollAxes () (horizontal, vertical bool) {
return false, true
}
// OnScrollBoundsChange sets a function to be called when the element's
// ScrollContentBounds, ScrollViewportBounds, or ScrollAxes are changed.
func (element *DocumentContainer) OnScrollBoundsChange(callback func()) {
element.onScrollBoundsChange = callback
}
func (element *DocumentContainer) reflectChildProperties () {
focusable := false
for _, entry := range element.children {
@ -315,20 +282,6 @@ func (element *DocumentContainer) reflectChildProperties () {
}
}
func (element *DocumentContainer) childFocusRequestCallback (
child elements.Focusable,
) (
granted bool,
) {
if element.onFocusRequest != nil && element.onFocusRequest() {
element.Propagator.HandleUnfocus()
element.Propagator.HandleFocus(input.KeynavDirectionNeutral)
return true
} else {
return false
}
}
func (element *DocumentContainer) doLayout () {
margin := element.theme.Margin(theme.PatternBackground)
padding := element.theme.Padding(theme.PatternBackground)

View File

@ -19,7 +19,7 @@ func NewIcon (id theme.Icon, size theme.IconSize) (element *Icon) {
size: size,
}
element.theme.Case = theme.C("basic", "icon")
element.Core, element.core = core.NewCore(element.draw)
element.Core, element.core = core.NewCore(element, element.draw)
element.updateMinimumSize()
return
}

View File

@ -13,7 +13,7 @@ type Image struct {
func NewImage (image image.Image) (element *Image) {
element = &Image { buffer: canvas.FromImage(image) }
element.Core, element.core = core.NewCore(element.draw)
element.Core, element.core = core.NewCore(element, element.draw)
bounds := image.Bounds()
element.core.SetMinimumSize(bounds.Dx(), bounds.Dy())
return

View File

@ -29,7 +29,7 @@ type Label struct {
func NewLabel (text string, wrap bool) (element *Label) {
element = &Label { }
element.theme.Case = theme.C("basic", "label")
element.Core, element.core = core.NewCore(element.handleResize)
element.Core, element.core = core.NewCore(element, element.handleResize)
element.SetWrap(wrap)
element.SetText(text)
return

View File

@ -7,6 +7,7 @@ import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/elements"
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
// List is an element that contains several objects that a user can select.
@ -29,7 +30,6 @@ type List struct {
config config.Wrapped
theme theme.Wrapped
onScrollBoundsChange func ()
onNoEntrySelected func ()
}
@ -37,9 +37,9 @@ type List struct {
func NewList (entries ...ListEntry) (element *List) {
element = &List { selectedEntry: -1 }
element.theme.Case = theme.C("basic", "list")
element.Core, element.core = core.NewCore(element.handleResize)
element.Core, element.core = core.NewCore(element, element.handleResize)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore (func () {
element.focusableControl = core.NewFocusableCore (element.core, func () {
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
@ -64,8 +64,8 @@ func (element *List) handleResize () {
element.scroll = element.maxScrollHeight()
}
element.draw()
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
@ -102,8 +102,8 @@ func (element *List) redo () {
element.draw()
element.core.DamageAll()
}
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
@ -210,8 +210,8 @@ func (element *List) ScrollTo (position image.Point) {
element.draw()
element.core.DamageAll()
}
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
@ -233,10 +233,6 @@ func (element *List) maxScrollHeight () (height int) {
return
}
func (element *List) OnScrollBoundsChange (callback func ()) {
element.onScrollBoundsChange = callback
}
// OnNoEntrySelected sets a function to be called when the user chooses to
// deselect the current selected entry by clicking on empty space within the
// list or by pressing the escape key.
@ -263,8 +259,8 @@ func (element *List) Append (entry ListEntry) {
element.draw()
element.core.DamageAll()
}
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
@ -296,8 +292,8 @@ func (element *List) Insert (index int, entry ListEntry) {
element.draw()
element.core.DamageAll()
}
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
@ -319,8 +315,8 @@ func (element *List) Remove (index int) {
element.draw()
element.core.DamageAll()
}
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
@ -341,8 +337,8 @@ func (element *List) Replace (index int, entry ListEntry) {
element.draw()
element.core.DamageAll()
}
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}

View File

@ -20,7 +20,7 @@ type ProgressBar struct {
func NewProgressBar (progress float64) (element *ProgressBar) {
element = &ProgressBar { progress: progress }
element.theme.Case = theme.C("basic", "progressBar")
element.Core, element.core = core.NewCore(element.draw)
element.Core, element.core = core.NewCore(element, element.draw)
return
}

View File

@ -49,7 +49,7 @@ func NewScrollBar (vertical bool) (element *ScrollBar) {
} else {
element.theme.Case = theme.C("basic", "scrollBarVertical")
}
element.Core, element.core = core.NewCore(element.handleResize)
element.Core, element.core = core.NewCore(element, element.handleResize)
element.updateMinimumSize()
return
}

View File

@ -31,12 +31,12 @@ type ScrollContainer struct {
func NewScrollContainer (horizontal, vertical bool) (element *ScrollContainer) {
element = &ScrollContainer { }
element.theme.Case = theme.C("basic", "scrollContainer")
element.Core, element.core = core.NewCore(element.redoAll)
element.Propagator = core.NewPropagator(element)
element.Core, element.core = core.NewCore(element, element.redoAll)
element.Propagator = core.NewPropagator(element, element.core)
if horizontal {
element.horizontal = NewScrollBar(false)
element.setChildEventHandlers(element.horizontal)
element.setUpChild(element.horizontal)
element.horizontal.OnScroll (func (viewport image.Point) {
if element.child != nil {
element.child.ScrollTo(viewport)
@ -50,7 +50,7 @@ func NewScrollContainer (horizontal, vertical bool) (element *ScrollContainer) {
}
if vertical {
element.vertical = NewScrollBar(true)
element.setChildEventHandlers(element.vertical)
element.setUpChild(element.vertical)
element.vertical.OnScroll (func (viewport image.Point) {
if element.child != nil {
element.child.ScrollTo(viewport)
@ -72,13 +72,13 @@ func NewScrollContainer (horizontal, vertical bool) (element *ScrollContainer) {
func (element *ScrollContainer) Adopt (child elements.Scrollable) {
// disown previous child if it exists
if element.child != nil {
element.clearChildEventHandlers(child)
element.disownChild(child)
}
// adopt new child
element.child = child
if child != nil {
element.setChildEventHandlers(child)
element.setUpChild(child)
}
element.updateEnabled()
@ -89,50 +89,47 @@ func (element *ScrollContainer) Adopt (child elements.Scrollable) {
}
}
func (element *ScrollContainer) setChildEventHandlers (child elements.Element) {
if child0, ok := child.(elements.Themeable); ok {
child0.SetTheme(element.theme.Theme)
func (element *ScrollContainer) setUpChild (child elements.Element) {
child.SetParent(element)
if child, ok := child.(elements.Themeable); ok {
child.SetTheme(element.theme.Theme)
}
if child0, ok := child.(elements.Configurable); ok {
child0.SetConfig(element.config.Config)
}
child.OnDamage (func (region canvas.Canvas) {
element.core.DamageRegion(region.Bounds())
})
child.OnMinimumSizeChange (func () {
element.updateMinimumSize()
element.redoAll()
element.core.DamageAll()
})
if child0, ok := child.(elements.Focusable); ok {
child0.OnFocusRequest (func () (granted bool) {
return element.childFocusRequestCallback(child0)
})
child0.OnFocusMotionRequest (
func (direction input.KeynavDirection) (granted bool) {
if element.onFocusMotionRequest == nil { return }
return element.onFocusMotionRequest(direction)
})
}
if child0, ok := child.(elements.Scrollable); ok {
child0.OnScrollBoundsChange(element.childScrollBoundsChangeCallback)
if child, ok := child.(elements.Configurable); ok {
child.SetConfig(element.config.Config)
}
}
func (element *ScrollContainer) clearChildEventHandlers (child elements.Scrollable) {
child.DrawTo(nil, image.Rectangle { })
child.OnDamage(nil)
child.OnMinimumSizeChange(nil)
child.OnScrollBoundsChange(nil)
if child0, ok := child.(elements.Focusable); ok {
child0.OnFocusRequest(nil)
child0.OnFocusMotionRequest(nil)
if child0.Focused() {
child0.HandleUnfocus()
func (element *ScrollContainer) disownChild (child elements.Scrollable) {
child.DrawTo(nil, image.Rectangle { }, nil)
child.SetParent(nil)
if child, ok := child.(elements.Focusable); ok {
if child.Focused() {
child.HandleUnfocus()
}
}
}
// NotifyMinimumSizeChange notifies the container that the minimum size of a
// child element has changed.
func (element *ScrollContainer) NotifyMinimumSizeChange (child elements.Element) {
element.redoAll()
element.core.DamageAll()
}
// NotifyScrollBoundsChange notifies the container that the scroll bounds or
// axes of a child have changed.
func (element *ScrollContainer) NotifyScrollBoundsChange (child elements.Scrollable) {
element.updateEnabled()
viewportBounds := element.child.ScrollViewportBounds()
contentBounds := element.child.ScrollContentBounds()
if element.horizontal != nil {
element.horizontal.SetBounds(contentBounds, viewportBounds)
}
if element.vertical != nil {
element.vertical.SetBounds(contentBounds, viewportBounds)
}
}
// SetTheme sets the element's theme.
func (element *ScrollContainer) SetTheme (new theme.Theme) {
if new == element.theme.Theme { return }
@ -157,18 +154,6 @@ func (element *ScrollContainer) HandleMouseScroll (
element.scrollChildBy(int(deltaX), int(deltaY))
}
func (element *ScrollContainer) OnFocusRequest (callback func () (granted bool)) {
element.onFocusRequest = callback
element.Propagator.OnFocusRequest(callback)
}
func (element *ScrollContainer) OnFocusMotionRequest (
callback func (direction input.KeynavDirection) (granted bool),
) {
element.onFocusMotionRequest = callback
element.Propagator.OnFocusMotionRequest(callback)
}
// CountChildren returns the amount of children contained within this element.
func (element *ScrollContainer) CountChildren () (count int) {
return 3
@ -199,25 +184,25 @@ func (element *ScrollContainer) redoAll () {
if !element.core.HasImage() { return }
zr := image.Rectangle { }
if element.child != nil { element.child.DrawTo(nil, zr) }
if element.horizontal != nil { element.horizontal.DrawTo(nil, zr) }
if element.vertical != nil { element.vertical.DrawTo(nil, zr) }
if element.child != nil { element.child.DrawTo(nil, zr, nil) }
if element.horizontal != nil { element.horizontal.DrawTo(nil, zr, nil) }
if element.vertical != nil { element.vertical.DrawTo(nil, zr, nil) }
childBounds, horizontalBounds, verticalBounds := element.layout()
if element.child != nil {
element.child.DrawTo (
canvas.Cut(element.core, childBounds),
childBounds)
childBounds, element.childDamageCallback)
}
if element.horizontal != nil {
element.horizontal.DrawTo (
canvas.Cut(element.core, horizontalBounds),
horizontalBounds)
horizontalBounds, element.childDamageCallback)
}
if element.vertical != nil {
element.vertical.DrawTo (
canvas.Cut(element.core, verticalBounds),
verticalBounds)
verticalBounds, element.childDamageCallback)
}
element.draw()
}
@ -230,18 +215,8 @@ func (element *ScrollContainer) scrollChildBy (x, y int) {
element.child.ScrollTo(scrollPoint)
}
func (element *ScrollContainer) childFocusRequestCallback (
child elements.Focusable,
) (
granted bool,
) {
if element.onFocusRequest != nil && element.onFocusRequest() {
element.Propagator.HandleUnfocus()
element.Propagator.HandleFocus(input.KeynavDirectionNeutral)
return true
} else {
return false
}
func (element *ScrollContainer) childDamageCallback (region image.Rectangle) {
element.core.DamageRegion(region)
}
func (element *ScrollContainer) layout () (
@ -308,18 +283,6 @@ func (element *ScrollContainer) updateMinimumSize () {
element.core.SetMinimumSize(width, height)
}
func (element *ScrollContainer) childScrollBoundsChangeCallback () {
element.updateEnabled()
viewportBounds := element.child.ScrollViewportBounds()
contentBounds := element.child.ScrollContentBounds()
if element.horizontal != nil {
element.horizontal.SetBounds(contentBounds, viewportBounds)
}
if element.vertical != nil {
element.vertical.SetBounds(contentBounds, viewportBounds)
}
}
func (element *ScrollContainer) updateEnabled () {
horizontal, vertical := element.child.ScrollAxes()
if element.horizontal != nil {

View File

@ -39,9 +39,9 @@ func NewSlider (value float64, vertical bool) (element *Slider) {
} else {
element.theme.Case = theme.C("basic", "sliderHorizontal")
}
element.Core, element.core = core.NewCore(element.draw)
element.Core, element.core = core.NewCore(element, element.draw)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.redo)
element.focusableControl = core.NewFocusableCore(element.core, element.redo)
element.updateMinimumSize()
return
}

View File

@ -20,7 +20,7 @@ type Spacer struct {
func NewSpacer (line bool) (element *Spacer) {
element = &Spacer { line: line }
element.theme.Case = theme.C("basic", "spacer")
element.Core, element.core = core.NewCore(element.draw)
element.Core, element.core = core.NewCore(element, element.draw)
element.updateMinimumSize()
return
}

View File

@ -33,9 +33,9 @@ func NewSwitch (text string, on bool) (element *Switch) {
text: text,
}
element.theme.Case = theme.C("basic", "switch")
element.Core, element.core = core.NewCore(element.draw)
element.Core, element.core = core.NewCore(element, element.draw)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.redo)
element.focusableControl = core.NewFocusableCore(element.core, element.redo)
element.drawer.SetText([]rune(text))
element.updateMinimumSize()
return

View File

@ -6,6 +6,7 @@ import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/config"
import "git.tebibyte.media/sashakoshka/tomo/artist"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/elements"
import "git.tebibyte.media/sashakoshka/tomo/textdraw"
import "git.tebibyte.media/sashakoshka/tomo/textmanip"
import "git.tebibyte.media/sashakoshka/tomo/fixedutil"
@ -33,7 +34,6 @@ type TextBox struct {
onKeyDown func (key input.Key, modifiers input.Modifiers) (handled bool)
onChange func ()
onScrollBoundsChange func ()
}
// NewTextBox creates a new text box with the specified placeholder text, and
@ -42,9 +42,9 @@ type TextBox struct {
func NewTextBox (placeholder, value string) (element *TextBox) {
element = &TextBox { }
element.theme.Case = theme.C("basic", "textBox")
element.Core, element.core = core.NewCore(element.handleResize)
element.Core, element.core = core.NewCore(element, element.handleResize)
element.FocusableCore,
element.focusableControl = core.NewFocusableCore (func () {
element.focusableControl = core.NewFocusableCore (element.core, func () {
if element.core.HasImage () {
element.draw()
element.core.DamageAll()
@ -60,8 +60,8 @@ func NewTextBox (placeholder, value string) (element *TextBox) {
func (element *TextBox) handleResize () {
element.scrollToCursor()
element.draw()
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
@ -187,9 +187,10 @@ func (element *TextBox) HandleKeyDown(key input.Key, modifiers input.Modifiers)
element.scrollToCursor()
}
if (textChanged || scrollMemory != element.scroll) &&
element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
if (textChanged || scrollMemory != element.scroll) {
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
if altered {
@ -274,8 +275,8 @@ func (element *TextBox) ScrollTo (position image.Point) {
if element.scroll > maxPosition { element.scroll = maxPosition }
element.redo()
if element.onScrollBoundsChange != nil {
element.onScrollBoundsChange()
if parent, ok := element.core.Parent().(elements.ScrollableParent); ok {
parent.NotifyScrollBoundsChange(element)
}
}
@ -284,10 +285,6 @@ func (element *TextBox) ScrollAxes () (horizontal, vertical bool) {
return true, false
}
func (element *TextBox) OnScrollBoundsChange (callback func ()) {
element.onScrollBoundsChange = callback
}
func (element *TextBox) runOnChange () {
if element.onChange != nil {
element.onChange()