Added the BackgroundParent interface

Parents are now able to draw backgrounds for their children. This
means we can now have elements inside other elements that aren't
restricted to one background color.
This commit is contained in:
Sasha Koshka 2023-04-02 22:02:55 -04:00
parent 7521808872
commit 6ede0d0770
9 changed files with 71 additions and 14 deletions

View File

@ -238,7 +238,7 @@ func (Default) Pattern (id tomo.Pattern, state tomo.State, c tomo.Case) artist.P
case tomo.PatternMercury: return defaultTextures[13][offset]
case tomo.PatternTableHead: return defaultTextures[14][offset]
case tomo.PatternTableCell: return defaultTextures[15][offset]
default: return patterns.Uhex(0xFF00FFFF)
default: return patterns.Uhex(0xFF00FFFF)
}
}

View File

@ -171,10 +171,9 @@ func (element *Checkbox) draw () {
On: element.checked,
}
backgroundPattern := element.theme.Pattern (
tomo.PatternBackground, state)
backgroundPattern.Draw(element.core, bounds)
element.core.DrawBackground (
element.theme.Pattern(tomo.PatternBackground, state))
pattern := element.theme.Pattern(tomo.PatternButton, state)
pattern.Draw(element.core, boxBounds)

View File

@ -216,6 +216,14 @@ func (element *Container) NotifyMinimumSizeChange (child tomo.Element) {
element.core.DamageAll()
}
// DrawBackground draws a portion of the container's background pattern within
// the specified bounds. The container will not push these changes.
func (element *Container) DrawBackground (bounds image.Rectangle) {
element.core.DrawBackgroundBounds (
element.theme.Pattern(tomo.PatternBackground, tomo.State { }),
bounds)
}
// SetTheme sets the element's theme.
func (element *Container) SetTheme (new tomo.Theme) {
if new == element.theme.Theme { return }

View File

@ -213,6 +213,14 @@ func (element *DocumentContainer) NotifyMinimumSizeChange (child tomo.Element) {
element.core.DamageAll()
}
// DrawBackground draws a portion of the container's background pattern within
// the specified bounds. The container will not push these changes.
func (element *DocumentContainer) DrawBackground (bounds image.Rectangle) {
element.core.DrawBackgroundBounds (
element.theme.Pattern(tomo.PatternBackground, tomo.State { }),
bounds)
}
// NotifyFlexibleHeightChange notifies the parent that the parameters
// affecting a child's flexible height have changed. This method is
// expected to be called by flexible child element when their content

View File

@ -135,6 +135,14 @@ func (element *ScrollContainer) NotifyScrollBoundsChange (child tomo.Scrollable)
}
}
// DrawBackground draws a portion of the container's background pattern within
// the specified bounds. The container will not push these changes.
func (element *ScrollContainer) DrawBackground (bounds image.Rectangle) {
element.core.DrawBackgroundBounds (
element.theme.Pattern(tomo.PatternBackground, tomo.State { }),
bounds)
}
// SetTheme sets the element's theme.
func (element *ScrollContainer) SetTheme (new tomo.Theme) {
if new == element.theme.Theme { return }

View File

@ -4,6 +4,7 @@ import "image"
import "image/color"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/canvas"
import "git.tebibyte.media/sashakoshka/tomo/artist"
// Core is a struct that implements some core functionality common to most
// widgets. It is meant to be embedded directly into a struct.
@ -122,6 +123,27 @@ func (control CoreControl) Parent () tomo.Parent {
return control.core.parent
}
// DrawBackground fills the element's canvas with the parent's background
// pattern, if the parent supports it. If it is not supported, the fallback
// pattern will be used instead.
func (control CoreControl) DrawBackground (fallback artist.Pattern) {
control.DrawBackgroundBounds(fallback, control.Bounds())
}
// DrawBackgroundBounds is like DrawBackground, but it takes in a bounding
// rectangle instead of using the element's bounds.
func (control CoreControl) DrawBackgroundBounds (
fallback artist.Pattern,
bounds image.Rectangle,
) {
parent, ok := control.Parent().(tomo.BackgroundParent)
if ok {
parent.DrawBackground(bounds)
} else if fallback != nil {
fallback.Draw(control, bounds)
}
}
// Window returns the window containing the element.
func (control CoreControl) Window () tomo.Window {
parent := control.Parent()

View File

@ -200,13 +200,10 @@ func (element *Label) updateMinimumSize () {
}
func (element *Label) draw () {
element.core.DrawBackground (
element.theme.Pattern(tomo.PatternBackground, tomo.State { }))
bounds := element.Bounds()
pattern := element.theme.Pattern (
tomo.PatternBackground,
tomo.State { })
pattern.Draw(element.core, bounds)
textBounds := element.drawer.LayoutBounds()
foreground := element.theme.Color (

View File

@ -165,9 +165,9 @@ func (element *Switch) draw () {
Focused: element.Focused(),
Pressed: element.pressed,
}
backgroundPattern := element.theme.Pattern (
tomo.PatternBackground, state)
backgroundPattern.Draw(element.core, bounds)
element.core.DrawBackground (
element.theme.Pattern(tomo.PatternBackground, state))
if element.checked {
handleBounds.Min.X += bounds.Dy()

View File

@ -1,5 +1,7 @@
package tomo
import "image"
// Parent represents a type capable of containing child elements.
type Parent interface {
// NotifyMinimumSizeChange notifies the container that a child element's
@ -54,3 +56,16 @@ type ScrollableParent interface {
// call to ScrollTo()), or their content size.
NotifyScrollBoundsChange (child Scrollable)
}
// BackgroundParent represents a parent that is able to re-draw a portion of its
// background upon request. This is intended to be used by transparent elements
// that want to adopt their parent's background pattern. If a parent implements
// this interface, it should call a child's DrawTo method when its area of the
// background is affected.
type BackgroundParent interface {
Parent
// DrawBackground draws a portion of the parent's background pattern
// within the specified bounds. The parent will not push these changes.
DrawBackground (bounds image.Rectangle)
}