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) { func NewButton (text string) (element *Button) {
element = &Button { showText: true } element = &Button { showText: true }
element.theme.Case = theme.C("basic", "button") 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.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.drawAndPush) element.focusableControl = core.NewFocusableCore(element.core, element.drawAndPush)
element.SetText(text) element.SetText(text)
return return
} }
@ -61,9 +61,6 @@ func (element *Button) HandleMouseUp (x, y int, button input.Button) {
element.drawAndPush() 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) { func (element *Button) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
if !element.Enabled() { return } if !element.Enabled() { return }
if key == input.KeyEnter { if key == input.KeyEnter {

View File

@ -29,9 +29,9 @@ type Checkbox struct {
func NewCheckbox (text string, checked bool) (element *Checkbox) { func NewCheckbox (text string, checked bool) (element *Checkbox) {
element = &Checkbox { checked: checked } element = &Checkbox { checked: checked }
element.theme.Case = theme.C("basic", "checkbox") 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.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.redo) element.focusableControl = core.NewFocusableCore(element.core, element.redo)
element.SetText(text) element.SetText(text)
return 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) { func (element *Checkbox) HandleKeyDown (key input.Key, modifiers input.Modifiers) {
if key == input.KeyEnter { if key == input.KeyEnter {
element.pressed = true element.pressed = true

View File

@ -32,8 +32,8 @@ type Container struct {
func NewContainer (layout layouts.Layout) (element *Container) { func NewContainer (layout layouts.Layout) (element *Container) {
element = &Container { } element = &Container { }
element.theme.Case = theme.C("basic", "container") element.theme.Case = theme.C("basic", "container")
element.Core, element.core = core.NewCore(element.redoAll) element.Core, element.core = core.NewCore(element, element.redoAll)
element.Propagator = core.NewPropagator(element) element.Propagator = core.NewPropagator(element, element.core)
element.SetLayout(layout) element.SetLayout(layout)
return return
} }
@ -51,33 +51,13 @@ func (element *Container) SetLayout (layout layouts.Layout) {
// the element will expand (instead of contract to its minimum size), in // the element will expand (instead of contract to its minimum size), in
// whatever way is defined by the current layout. // whatever way is defined by the current layout.
func (element *Container) Adopt (child elements.Element, expand bool) { func (element *Container) Adopt (child elements.Element, expand bool) {
// set event handlers
if child0, ok := child.(elements.Themeable); ok { if child0, ok := child.(elements.Themeable); ok {
child0.SetTheme(element.theme.Theme) child0.SetTheme(element.theme.Theme)
} }
if child0, ok := child.(elements.Configurable); ok { if child0, ok := child.(elements.Configurable); ok {
child0.SetConfig(element.config.Config) child0.SetConfig(element.config.Config)
} }
child.OnDamage (func (region canvas.Canvas) { child.SetParent(element)
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)
})
}
// add child // add child
element.children = append (element.children, layouts.LayoutEntry { 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) { func (element *Container) clearChildEventHandlers (child elements.Element) {
child.DrawTo(nil, image.Rectangle { }) child.DrawTo(nil, image.Rectangle { }, nil)
child.OnDamage(nil) child.SetParent(nil)
child.OnMinimumSizeChange(nil)
if child0, ok := child.(elements.Focusable); ok { if child, ok := child.(elements.Focusable); ok {
child0.OnFocusRequest(nil) if child.Focused() {
child0.OnFocusMotionRequest(nil) child.HandleUnfocus()
if child0.Focused() {
child0.HandleUnfocus()
} }
} }
} }
@ -200,7 +178,7 @@ func (element *Container) redoAll () {
// remove child canvasses so that any operations done in here will not // remove child canvasses so that any operations done in here will not
// cause a child to draw to a wack ass canvas. // cause a child to draw to a wack ass canvas.
for _, entry := range element.children { for _, entry := range element.children {
entry.DrawTo(nil, entry.Bounds) entry.DrawTo(nil, entry.Bounds, nil)
} }
// do a layout // do a layout
@ -220,10 +198,20 @@ func (element *Container) redoAll () {
for _, entry := range element.children { for _, entry := range element.children {
entry.DrawTo ( entry.DrawTo (
canvas.Cut(element.core, entry.Bounds), 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. // SetTheme sets the element's theme.
func (element *Container) SetTheme (new theme.Theme) { func (element *Container) SetTheme (new theme.Theme) {
if new == element.theme.Theme { return } if new == element.theme.Theme { return }
@ -241,32 +229,6 @@ func (element *Container) SetConfig (new config.Config) {
element.redoAll() 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 () { func (element *Container) updateMinimumSize () {
margin := element.theme.Margin(theme.PatternBackground) margin := element.theme.Margin(theme.PatternBackground)
padding := element.theme.Padding(theme.PatternBackground) padding := element.theme.Padding(theme.PatternBackground)

View File

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

View File

@ -13,7 +13,7 @@ type Image struct {
func NewImage (image image.Image) (element *Image) { func NewImage (image image.Image) (element *Image) {
element = &Image { buffer: canvas.FromImage(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() bounds := image.Bounds()
element.core.SetMinimumSize(bounds.Dx(), bounds.Dy()) element.core.SetMinimumSize(bounds.Dx(), bounds.Dy())
return return

View File

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

View File

@ -20,7 +20,7 @@ type ProgressBar struct {
func NewProgressBar (progress float64) (element *ProgressBar) { func NewProgressBar (progress float64) (element *ProgressBar) {
element = &ProgressBar { progress: progress } element = &ProgressBar { progress: progress }
element.theme.Case = theme.C("basic", "progressBar") 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 return
} }

View File

@ -49,7 +49,7 @@ func NewScrollBar (vertical bool) (element *ScrollBar) {
} else { } else {
element.theme.Case = theme.C("basic", "scrollBarVertical") 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() element.updateMinimumSize()
return return
} }

View File

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

View File

@ -39,9 +39,9 @@ func NewSlider (value float64, vertical bool) (element *Slider) {
} else { } else {
element.theme.Case = theme.C("basic", "sliderHorizontal") 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.FocusableCore,
element.focusableControl = core.NewFocusableCore(element.redo) element.focusableControl = core.NewFocusableCore(element.core, element.redo)
element.updateMinimumSize() element.updateMinimumSize()
return return
} }

View File

@ -20,7 +20,7 @@ type Spacer struct {
func NewSpacer (line bool) (element *Spacer) { func NewSpacer (line bool) (element *Spacer) {
element = &Spacer { line: line } element = &Spacer { line: line }
element.theme.Case = theme.C("basic", "spacer") 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() element.updateMinimumSize()
return return
} }

View File

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