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:
@@ -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)
|
||||
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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 (
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user