Skip to content

Commit

Permalink
Don't allow settings being applied to overwrite custom themes
Browse files Browse the repository at this point in the history
This applies to app.Settings().SetTheme() and also to the FYNE_THEME selection.
  • Loading branch information
andydotxyz committed Apr 4, 2020
1 parent 9c287e2 commit 4306aa5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
20 changes: 15 additions & 5 deletions app/settings.go
Expand Up @@ -26,8 +26,9 @@ func (sc *SettingsSchema) StoragePath() string {
var _ fyne.Settings = (*settings)(nil)

type settings struct {
themeLock sync.RWMutex
theme fyne.Theme
themeLock sync.RWMutex
theme fyne.Theme
themeSpecified bool

listenerLock sync.Mutex
changeListeners []chan fyne.Settings
Expand All @@ -43,6 +44,11 @@ func (s *settings) Theme() fyne.Theme {
}

func (s *settings) SetTheme(theme fyne.Theme) {
s.themeSpecified = true
s.applyTheme(theme)
}

func (s *settings) applyTheme(theme fyne.Theme) {
s.themeLock.Lock()
defer s.themeLock.Unlock()
s.theme = theme
Expand Down Expand Up @@ -103,17 +109,21 @@ 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.SetTheme(theme.LightTheme())
s.applyTheme(theme.LightTheme())
} else if name == "dark" {
s.SetTheme(theme.DarkTheme())
s.applyTheme(theme.DarkTheme())
} else {
s.SetTheme(defaultTheme())
s.applyTheme(defaultTheme())
}
}

Expand Down
23 changes: 23 additions & 0 deletions app/settings_test.go
Expand Up @@ -56,3 +56,26 @@ func TestOverrideTheme(t *testing.T) {
t.Error(err)
}
}

func TestOverrideTheme_IgnoresSettingsChange(t *testing.T) {
// check that a file-load does not overwrite our value
set := &settings{}
err := os.Setenv("FYNE_THEME", "light")
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)
}

err = set.loadFromFile(filepath.Join("testdata", "dark-theme.json"))
if err != nil {
t.Error(err)
}

set.setupTheme()
assert.Equal(t, theme.LightTheme(), set.Theme())
}
4 changes: 3 additions & 1 deletion widget/box.go
Expand Up @@ -21,7 +21,9 @@ type Box struct {

// Refresh updates this box to match the current theme
func (b *Box) Refresh() {
b.background = theme.BackgroundColor()
if b.background != nil {
b.background = theme.BackgroundColor()
}

b.BaseWidget.Refresh()
}
Expand Down

0 comments on commit 4306aa5

Please sign in to comment.