From 6a08d0f317b84c0f72787156ba3f392646c0385e Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 23 Mar 2023 17:34:08 -0400 Subject: [PATCH] Added a Component parameter to theme cases --- elements/fun/piano.go | 4 ++-- theme/default.go | 40 ++++++++++++++++++---------------- theme/state.go | 50 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 64 insertions(+), 30 deletions(-) diff --git a/elements/fun/piano.go b/elements/fun/piano.go index f7ca895..7893e35 100644 --- a/elements/fun/piano.go +++ b/elements/fun/piano.go @@ -313,7 +313,7 @@ func (element *Piano) drawFlat ( ) { state.Pressed = pressed pattern := element.theme.Theme.Pattern ( - theme.PatternButton, state, theme.C("fun", "flatKey")) + theme.PatternButton, state, theme.C("fun", "piano", "flatKey")) pattern.Draw(element.core, bounds) } @@ -324,6 +324,6 @@ func (element *Piano) drawSharp ( ) { state.Pressed = pressed pattern := element.theme.Theme.Pattern ( - theme.PatternButton, state, theme.C("fun", "sharpKey")) + theme.PatternButton, state, theme.C("fun", "piano", "sharpKey")) pattern.Draw(element.core, bounds) } diff --git a/theme/default.go b/theme/default.go index b59624c..e284207 100644 --- a/theme/default.go +++ b/theme/default.go @@ -210,26 +210,30 @@ func (Default) Pattern (id Pattern, state State, c Case) artist.Pattern { case PatternBackground: return patterns.Uhex(0xaaaaaaFF) case PatternDead: return defaultTextures[0][offset] case PatternRaised: - if c == C("basic", "listEntry") { + if c.Match("basic", "listEntry", "") { return defaultTextures[10][offset] } else { return defaultTextures[1][offset] } - case PatternSunken: return defaultTextures[2][offset] - case PatternPinboard: return defaultTextures[3][offset] + case PatternSunken: return defaultTextures[2][offset] + case PatternPinboard: return defaultTextures[3][offset] case PatternButton: - switch c { - case C("basic", "checkbox"): return defaultTextures[9][offset] - case C("fun", "flatKey"): return defaultTextures[11][offset] - case C("fun", "sharpKey"): return defaultTextures[12][offset] - default: return defaultTextures[4][offset] + switch { + case c.Match("basic", "checkbox", ""): + return defaultTextures[9][offset] + case c.Match("fun", "piano", "flatKey"): + return defaultTextures[11][offset] + case c.Match("fun", "piano", "sharpKey"): + return defaultTextures[12][offset] + default: + return defaultTextures[4][offset] } - case PatternInput: return defaultTextures[5][offset] - case PatternGutter: return defaultTextures[6][offset] - case PatternHandle: return defaultTextures[7][offset] - case PatternLine: return defaultTextures[8][offset] - case PatternMercury: return defaultTextures[13][offset] - default: return patterns.Uhex(0xFF00FFFF) + case PatternInput: return defaultTextures[5][offset] + case PatternGutter: return defaultTextures[6][offset] + case PatternHandle: return defaultTextures[7][offset] + case PatternLine: return defaultTextures[8][offset] + case PatternMercury: return defaultTextures[13][offset] + default: return patterns.Uhex(0xFF00FFFF) } } @@ -249,21 +253,21 @@ func (Default) Color (id Color, state State, c Case) color.RGBA { func (Default) Padding (id Pattern, c Case) artist.Inset { switch id { case PatternRaised: - if c == C("basic", "listEntry") { + if c.Match("basic", "listEntry", "") { return artist.Inset { 4, 8, 4, 8 } } else { return artist.Inset { 8, 8, 8, 8 } } case PatternSunken: - if c == C("basic", "list") { + if c.Match("basic", "list", "") { return artist.Inset { 4, 0, 3, 0 } - } else if c == C("basic", "progressBar") { + } else if c.Match("basic", "progressBar", "") { return artist.Inset { 2, 1, 1, 2 } } else { return artist.Inset { 8, 8, 8, 8 } } case PatternPinboard: - if c == C("fun", "piano") { + if c.Match("fun", "piano", "") { return artist.Inset { 2, 2, 2, 2 } } else { return artist.Inset { 8, 8, 8, 8 } diff --git a/theme/state.go b/theme/state.go index 57f96a4..b68911b 100644 --- a/theme/state.go +++ b/theme/state.go @@ -1,29 +1,59 @@ package theme // Case sepecifies what kind of element is using a pattern. It contains a -// namespace parameter and an element parameter. The element parameter does not -// necissarily need to match an element name, but if it can, it should. Both -// parameters should be written in camel case. Themes can change their styling -// based on this parameter for fine-grained control over the look and feel of +// namespace parameter, an element parameter, and an optional component trail. +// All parameter values should be written in camel case. Themes can change their +// styling based on the case for fine-grained control over the look and feel of // specific elements. -type Case struct { Namespace, Element string } +type Case struct { + // Namespace refers to the package that the element comes from. This is + // so different element packages can have elements with the same name + // while still allowing themes to differentiate between them. + Namespace string + + // Element refers to the name of the element. This should (generally) be + // the type name of the element. For example: Button, Input, Container, + // etc. + Element string + + // Component specifies the specific part of the element that is being + // referred to. This parameter is entirely optional. + Component string +} -// C can be used as shorthand to generate a case struct as used in State. -func C (namespace, element string) (c Case) { +// C can be used as shorthand to generate a case struct. The component parameter +// may be left out of this argument list for brevity. Arguments passed after +// component will be ignored. +func C (namespace, element string, component ...string) Case { + if component == nil { component = []string { "" } } return Case { Namespace: namespace, - Element: element, + Element: element, + Component: component[0], } } +// Match determines if a case matches the specified parameters. A blank string +// will act as a wildcard. +func (c Case) Match (namespace, element, component string) bool { + if namespace == "" { namespace = c.Namespace } + if element == "" { element = c.Element } + if component == "" { component = c.Component } + + return namespace == c.Namespace && + element == c.Element && + component == c.Component +} + // State lists parameters which can change the appearance of some patterns and // colors. For example, passing a State with Selected set to true may result in // a pattern that has a colored border within it. type State struct { // On should be set to true if the element that is using this pattern is // in some sort of selected or "on" state, such as if a checkbox is - // checked or a switch is toggled on. This is only necessary if the - // element in question is capable of being toggled or selected. + // checked, a file is selected, or a switch is toggled on. This is only + // necessary if the element in question is capable of being toggled or + // selected. On bool // Focused should be set to true if the element that is using this