From 6ede0d0770f9b353adca4c9cbca1d1a460a547c0 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 2 Apr 2023 22:02:55 -0400 Subject: [PATCH] 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. --- default/theme/default.go | 2 +- elements/checkbox.go | 7 +++---- elements/containers/container.go | 8 ++++++++ elements/containers/document.go | 8 ++++++++ elements/containers/scroll.go | 8 ++++++++ elements/core/core.go | 22 ++++++++++++++++++++++ elements/label.go | 9 +++------ elements/switch.go | 6 +++--- parent.go | 15 +++++++++++++++ 9 files changed, 71 insertions(+), 14 deletions(-) diff --git a/default/theme/default.go b/default/theme/default.go index ae82ab6..375d983 100644 --- a/default/theme/default.go +++ b/default/theme/default.go @@ -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) } } diff --git a/elements/checkbox.go b/elements/checkbox.go index 5b19558..f90b90d 100644 --- a/elements/checkbox.go +++ b/elements/checkbox.go @@ -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) diff --git a/elements/containers/container.go b/elements/containers/container.go index 904eb4c..325c260 100644 --- a/elements/containers/container.go +++ b/elements/containers/container.go @@ -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 } diff --git a/elements/containers/document.go b/elements/containers/document.go index f67511f..cf46b49 100644 --- a/elements/containers/document.go +++ b/elements/containers/document.go @@ -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 diff --git a/elements/containers/scroll.go b/elements/containers/scroll.go index 2dbcbd1..7f1e278 100644 --- a/elements/containers/scroll.go +++ b/elements/containers/scroll.go @@ -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 } diff --git a/elements/core/core.go b/elements/core/core.go index 1eaae56..eeb77a8 100644 --- a/elements/core/core.go +++ b/elements/core/core.go @@ -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() diff --git a/elements/label.go b/elements/label.go index 0bef6ab..412d636 100644 --- a/elements/label.go +++ b/elements/label.go @@ -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 ( diff --git a/elements/switch.go b/elements/switch.go index 25f6858..77bbc84 100644 --- a/elements/switch.go +++ b/elements/switch.go @@ -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() diff --git a/parent.go b/parent.go index f27ee6d..57aa45d 100644 --- a/parent.go +++ b/parent.go @@ -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) +}