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

Allow custom themes to react theme variant changes, fixes #2006 #2049

Merged
merged 2 commits into from Mar 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 23 additions & 13 deletions app/settings.go
Expand Up @@ -139,26 +139,36 @@ func (s *settings) fileChanged() {
}

func (s *settings) setupTheme() {
if s.themeSpecified {
return
}
name := s.schema.ThemeName
if env := os.Getenv("FYNE_THEME"); env != "" {
s.themeSpecified = true
name = env
}

if name == "light" {
s.applyTheme(theme.LightTheme(), theme.VariantLight)
} else if name == "dark" {
s.applyTheme(theme.DarkTheme(), theme.VariantDark)
} else {
if defaultVariant() == theme.VariantLight {
s.applyTheme(theme.LightTheme(), theme.VariantLight)
} else {
s.applyTheme(theme.DarkTheme(), theme.VariantDark)
var variant fyne.ThemeVariant
effectiveTheme := s.theme
switch name {
case "light":
variant = theme.VariantLight
if !s.themeSpecified {
effectiveTheme = theme.LightTheme()
}
case "dark":
variant = theme.VariantDark
if !s.themeSpecified {
effectiveTheme = theme.DarkTheme()
}
default:
variant = defaultVariant()
if s.themeSpecified {
break
}
effectiveTheme = theme.DarkTheme()
if variant == theme.VariantLight {
effectiveTheme = theme.LightTheme()
}
}

s.applyTheme(effectiveTheme, variant)
}

func loadSettings() *settings {
Expand Down
54 changes: 53 additions & 1 deletion app/settings_test.go
Expand Up @@ -76,16 +76,68 @@ func TestOverrideTheme_IgnoresSettingsChange(t *testing.T) {
}
set.setupTheme()
assert.Equal(t, theme.LightTheme(), set.Theme())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have edited this test, by removing the behavior that ignores forever the changes in the application once FYNE_THEME env var is set at least once, is that ok?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYNE_THEME is an override, once set it should not be changed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we had considered the env changing during runtime. That could go either way.
What I meant was that if env is set nothing else should influence the setting

err = set.loadFromFile(filepath.Join("testdata", "dark-theme.json"))
if err != nil {
t.Error(err)
}
set.setupTheme()
assert.Equal(t, theme.LightTheme(), set.Theme())
err = os.Setenv("FYNE_THEME", "")
if err != nil {
t.Error(err)
}
}

func TestCustomTheme(t *testing.T) {
type customTheme struct {
fyne.Theme
}
set := &settings{}
ctheme := &customTheme{theme.LightTheme()}
set.SetTheme(ctheme)

set.setupTheme()
assert.True(t, set.Theme() == ctheme)
assert.Equal(t, defaultVariant(), set.ThemeVariant())

err := set.loadFromFile(filepath.Join("testdata", "light-theme.json"))
if err != nil {
t.Error(err)
}
set.setupTheme()
assert.True(t, set.Theme() == ctheme)
assert.Equal(t, theme.VariantLight, set.ThemeVariant())

err = set.loadFromFile(filepath.Join("testdata", "dark-theme.json"))
if err != nil {
t.Error(err)
}
set.setupTheme()
assert.True(t, set.Theme() == ctheme)
assert.Equal(t, theme.VariantDark, set.ThemeVariant())

err = os.Setenv("FYNE_THEME", "light")
if err != nil {
t.Error(err)
}
set.setupTheme()
assert.Equal(t, theme.LightTheme(), set.Theme())
assert.True(t, set.Theme() == ctheme)
assert.Equal(t, theme.VariantLight, set.ThemeVariant())

err = os.Setenv("FYNE_THEME", "dark")
if err != nil {
t.Error(err)
}
set.setupTheme()
assert.True(t, set.Theme() == ctheme)
assert.Equal(t, theme.VariantDark, set.ThemeVariant())

err = os.Setenv("FYNE_THEME", "")
if err != nil {
t.Error(err)
}
set.setupTheme()
assert.True(t, set.Theme() == ctheme)
assert.Equal(t, theme.VariantDark, set.ThemeVariant())
}