Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style setter: add Add method #951

Merged
merged 3 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions StyleSetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,46 @@ func Style() *StyleSetter {
return &ss
}

// Add merges two StyleSetters.
// Add puts other "on top" of ss, meaning, "other" is applied after "ss".
// e.g. if both StyleSetters set imgui.StyleVarAlpha, the value from "other" will be used.
// NOTE: font value "nil" is treated as "not set" and will not be changed if declared by other.
// NOTE: true is preffered over false for disabled field.
// NOTE: layout field will be reset.
func (ss *StyleSetter) Add(other *StyleSetter) *StyleSetter {
if other == nil {
return ss
}

for k, v := range other.colors {
ss.colors[k] = v
}

for k, v := range other.styles {
ss.styles[k] = v
}

for k, v := range other.plotColors {
ss.plotColors[k] = v
}

for k, v := range other.plotStyles {
ss.plotStyles[k] = v
}

if other.font != nil {
ss.font = other.font
}

if other.disabled {
ss.disabled = true
}

ss.layout = nil

return ss
}

// SetColor sets colorID's color.
func (ss *StyleSetter) SetColor(colorID StyleColorID, col color.Color) *StyleSetter {
ss.colors[colorID] = col
Expand Down
53 changes: 53 additions & 0 deletions StyleSetter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package giu

import (
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/image/colornames"
)

func TestStyleSetter_Add(t *testing.T) {
cases := []struct {
name string
setter *StyleSetter
other *StyleSetter
expected *StyleSetter
}{
{
name: "Adding nil",
setter: Style().SetColor(StyleColorText, colornames.Red).
SetStyle(StyleVarWindowPadding, 10, 10),
other: nil,
expected: Style().SetColor(StyleColorText, colornames.Red).
SetStyle(StyleVarWindowPadding, 10, 10),
},
{
name: "Adding some styles/colors",
setter: Style().SetColor(StyleColorText, colornames.Red).
SetStyle(StyleVarWindowPadding, 10, 10),
other: Style().SetColor(StyleColorWindowBg, colornames.Red).
SetStyle(StyleVarFramePadding, 10, 10),
expected: Style().SetColor(StyleColorText, colornames.Red).
SetStyle(StyleVarWindowPadding, 10, 10).
SetColor(StyleColorWindowBg, colornames.Red).
SetStyle(StyleVarFramePadding, 10, 10),
},
{
name: "Overwrite",
setter: Style().SetColor(StyleColorText, colornames.Red).
SetStyle(StyleVarWindowPadding, 10, 10),
other: Style().SetColor(StyleColorText, colornames.Blue).
SetStyle(StyleVarWindowPadding, 11, 11),
expected: Style().SetColor(StyleColorText, colornames.Blue).
SetStyle(StyleVarWindowPadding, 11, 11),
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
c.setter.Add(c.other)
assert.True(t, assert.ObjectsAreEqual(c.setter, c.expected), "Expected: %v, got: %v", c.expected, c.setter)
})
}
}
Loading