Skip to content

Commit

Permalink
Create settings API to manage theme preferences
Browse files Browse the repository at this point in the history
Allow reloading of theme to rendered widgets
  • Loading branch information
andydotxyz committed May 22, 2018
1 parent 2ba5caf commit 4869829
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 19 deletions.
63 changes: 63 additions & 0 deletions app/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package app

import "os"

// Settings describes the system configuration available and allows configurable
// items to be changed.
type Settings interface {
Theme() string
SetTheme(string)

AddChangeListener(chan Settings)
}

type settings struct {
theme string

changeListeners []chan Settings
}

var singleton *settings

func (s *settings) Theme() string {
return s.theme
}

func (s *settings) SetTheme(theme string) {
s.theme = theme
s.apply()
}

func (s *settings) AddChangeListener(listener chan Settings) {
s.changeListeners = append(s.changeListeners, listener)
}

func (s *settings) apply() {
for _, listener := range s.changeListeners {
go func(listener chan Settings) {
listener <- s
}(listener)
}
}

func loadSettings() *settings {
s := &settings{}

env := os.Getenv("FYNE_THEME")
if env == "light" {
s.theme = env
} else {
s.theme = "dark"
}

return s
}

// GetSettings returns the system wide settings currently configured
func GetSettings() Settings {
if singleton == nil {
singleton = loadSettings()
}

return singleton
}
4 changes: 2 additions & 2 deletions ui/canvas/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func (t *Text) MinSize() ui.Size {
}

// NewText returns a new Text implementation
func NewText(text string) *Text {
func NewText(text string, color color.RGBA) *Text {
return &Text{
Color: theme.TextColor(),
Color: color,
Text: text,
FontSize: theme.TextSize(),
}
Expand Down
4 changes: 2 additions & 2 deletions ui/layout/gridlayout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func TestGridLayoutRounding(t *testing.T) {
}

func TestGridLayoutMinSize(t *testing.T) {
text1 := canvas.NewText("Large Text")
text2 := canvas.NewText("small")
text1 := canvas.NewText("Large Text", color.RGBA{0xff, 0, 0, 0})
text2 := canvas.NewText("small", color.RGBA{0xff, 0, 0, 0})
minSize := text1.MinSize().Add(ui.NewSize(0, text1.MinSize().Height+theme.Padding()))

container := ui.NewContainer(text1, text2)
Expand Down
4 changes: 2 additions & 2 deletions ui/layout/maxlayout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestMaxLayout(t *testing.T) {
}

func TestMaxLayoutMinSize(t *testing.T) {
text := canvas.NewText("Padding")
text := canvas.NewText("Padding", color.RGBA{0, 0xff, 0, 0})
minSize := text.MinSize()

container := ui.NewContainer(text)
Expand All @@ -34,7 +34,7 @@ func TestMaxLayoutMinSize(t *testing.T) {
}

func TestContainerMaxLayoutMinSize(t *testing.T) {
text := canvas.NewText("Padding")
text := canvas.NewText("Padding", color.RGBA{0, 0xff, 0, 0})
minSize := text.MinSize()

container := ui.NewContainer(text)
Expand Down
24 changes: 15 additions & 9 deletions ui/theme/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
package theme

import "image/color"
import "os"
import "path"
import "runtime"

import "github.com/fyne-io/fyne/app"

var loadedColors *themeColors
var loadedTheme string

type themeColors struct {
Background color.RGBA
Expand Down Expand Up @@ -36,18 +38,22 @@ func loadDarkColors() *themeColors {

// Load the right theme colours based on environment / settings
func colors() *themeColors {
if loadedColors != nil {
return loadedColors
if loadedTheme != app.GetSettings().Theme() {
loadedColors = nil
}

env := os.Getenv("FYNE_THEME")
if env == "light" {
loadedColors = loadLightColors()
} else {
loadedColors = loadDarkColors()
c := loadedColors
if loadedColors == nil {

if app.GetSettings().Theme() == "light" {
c = loadLightColors()
} else {
c = loadDarkColors()
}
}

return loadedColors
loadedColors = c
return c
}

// BackgroundColor returns the theme's background colour
Expand Down
7 changes: 6 additions & 1 deletion ui/widget/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ func (b *Button) OnMouseDown(*ui.MouseEvent) {
}
}

func (b *Button) ApplyTheme() {
b.label.Color = theme.TextColor()
b.background.FillColor = theme.ButtonColor()
}

// NewButton creates a new button widget with the set label and tap handler
func NewButton(label string, tapped func()) *Button {
text := canvas.NewText(label)
text := canvas.NewText(label, theme.TextColor())
bg := canvas.NewRectangle(theme.ButtonColor())

return &Button{
Expand Down
14 changes: 11 additions & 3 deletions ui/widget/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ type Label struct {

Text string

label *canvas.Text
label *canvas.Text
background *canvas.Rectangle
}

// MinSize calculates the minimum size of a label.
Expand All @@ -34,17 +35,24 @@ func (l *Label) Layout(size ui.Size) []ui.CanvasObject {
return l.objects
}

func (b *Label) ApplyTheme() {
b.label.Color = theme.TextColor()
b.background.FillColor = theme.BackgroundColor()
}

// NewLabel creates a new layout widget with the set text content
func NewLabel(text string) *Label {
obj := canvas.NewText(text)
obj := canvas.NewText(text, theme.TextColor())
bg := canvas.NewRectangle(theme.BackgroundColor())
return &Label{
baseWidget{
objects: []ui.CanvasObject{
canvas.NewRectangle(theme.BackgroundColor()),
bg,
obj,
},
},
text,
obj,
bg,
}
}
3 changes: 3 additions & 0 deletions ui/widget/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func (l *List) Append(object ui.CanvasObject) {
ui.GetCanvas(l).Refresh(l)
}

func (l *List) ApplyTheme() {
}

// NewList creates a new list widget with the specified list of child objects
func NewList(children ...ui.CanvasObject) *List {
return &List{
Expand Down
1 change: 1 addition & 0 deletions ui/widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Widget interface {

// TODO should this move to a widget impl?... (private)
Layout(ui.Size) []ui.CanvasObject
ApplyTheme()
}

// A base widget class to define the standard widget behaviours.
Expand Down

0 comments on commit 4869829

Please sign in to comment.