Skip to content

Commit

Permalink
Merge pull request #93 from fyne-io/feature/moduleconfig
Browse files Browse the repository at this point in the history
Feature/moduleconfig
  • Loading branch information
andydotxyz committed Apr 19, 2020
2 parents 88f4d5c + 8e18a13 commit d63d4d8
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 104 deletions.
2 changes: 2 additions & 0 deletions cmd/fynedesk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fyne.io/fyne/app"
"fyne.io/fyne/theme"

_ "fyne.io/fynedesk/modules/builtin"
)

func main() {
Expand Down
36 changes: 20 additions & 16 deletions internal/ui/background.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,7 @@ type background struct {
}

func (b *background) CreateRenderer() fyne.WidgetRenderer {
mods := fynedesk.Instance().Modules()
objects := []fyne.CanvasObject{b.wallpaper}

for _, m := range mods {
if deskMod, ok := m.(fynedesk.ScreenAreaModule); ok {
wid := deskMod.ScreenAreaWidget()
if wid == nil {
continue
}

objects = append(objects, wid)
}
}

c := fyne.NewContainerWithLayout(layout.NewMaxLayout(), objects...)
c := fyne.NewContainerWithLayout(layout.NewMaxLayout(), b.loadModules()...)
return &backgroundRenderer{b: b, c: c}
}

Expand Down Expand Up @@ -67,7 +53,24 @@ func (b *backgroundRenderer) Objects() []fyne.CanvasObject {
func (b *backgroundRenderer) Destroy() {
}

func (b *background) updateBackgroundPath(path string) {
func (b *background) loadModules() []fyne.CanvasObject {
objects := []fyne.CanvasObject{b.wallpaper}

for _, m := range fynedesk.Instance().Modules() {
if deskMod, ok := m.(fynedesk.ScreenAreaModule); ok {
wid := deskMod.ScreenAreaWidget()
if wid == nil {
continue
}

objects = append(objects, wid)
}
}

return objects
}

func (b *background) updateBackground(path string) {
_, err := os.Stat(path)
if path == "" || os.IsNotExist(err) {
b.wallpaper.Resource = wmtheme.Background
Expand All @@ -77,6 +80,7 @@ func (b *background) updateBackgroundPath(path string) {

b.wallpaper.Resource = nil
b.wallpaper.File = path
b.loadModules()
}

func backgroundPath() string {
Expand Down
46 changes: 29 additions & 17 deletions internal/ui/desk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
deskDriver "fyne.io/fyne/driver/desktop"

"fyne.io/fynedesk"
"fyne.io/fynedesk/modules/builtin"
)

const (
Expand All @@ -27,12 +26,13 @@ type deskLayout struct {

backgroundScreenMap map[*background]*fynedesk.Screen

bar *bar
widgets, mouse fyne.CanvasObject
controlWin fyne.Window
primaryWin fyne.Window
roots []fyne.Window
refreshing bool
bar *bar
widgets *widgetPanel
mouse fyne.CanvasObject
controlWin fyne.Window
primaryWin fyne.Window
roots []fyne.Window
refreshing bool
}

func (l *deskLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
Expand Down Expand Up @@ -79,7 +79,7 @@ func (l *deskLayout) newDesktopWindow(outputName string) fyne.Window {

func (l *deskLayout) updateBackgrounds(path string) {
for bg := range l.backgroundScreenMap {
bg.updateBackgroundPath(path)
bg.updateBackground(path)
canvas.Refresh(bg)
}
}
Expand All @@ -98,11 +98,13 @@ func (l *deskLayout) createRoot(screen *fynedesk.Screen) {
if screen == l.screens.Primary() {
l.primaryWin = win
l.createPrimaryContent()
win.SetOnClosed(func() {
if !l.refreshing {
l.controlWin.Close()
}
})
if l.wm != nil {
win.SetOnClosed(func() {
if !l.refreshing {
l.controlWin.Close()
}
})
}
win.SetContent(fyne.NewContainerWithLayout(l, bg, l.bar, l.widgets, l.mouse))
l.mouse.Hide()
} else {
Expand Down Expand Up @@ -216,7 +218,15 @@ func (l *deskLayout) WindowManager() fynedesk.WindowManager {
}

func (l *deskLayout) Modules() []fynedesk.Module {
return []fynedesk.Module{builtin.NewBattery(), builtin.NewBrightness()}
var mods []fynedesk.Module
for _, meta := range fynedesk.AvailableModules() {
if !isModuleEnabled(meta.Name, l.settings) {
continue
}
mods = append(mods, meta.NewInstance())
}

return mods
}

func (l *deskLayout) scaleVars(scale float32) []string {
Expand Down Expand Up @@ -260,10 +270,12 @@ func (l *deskLayout) MouseOutNotify() {
l.bar.MouseOut()
}

func (l *deskLayout) startSettingsChangeListener(listener chan fynedesk.DeskSettings) {
func (l *deskLayout) startSettingsChangeListener(settings chan fynedesk.DeskSettings) {
for {
_ = <-listener
l.updateBackgrounds(l.Settings().Background())
s := <-settings
l.updateBackgrounds(s.Background())
l.widgets.reloadModules(l.Modules())

l.bar.iconSize = l.Settings().LauncherIconSize()
l.bar.iconScale = float32(l.Settings().LauncherZoomScale())
l.bar.disableZoom = l.Settings().LauncherDisableZoom()
Expand Down
51 changes: 1 addition & 50 deletions internal/ui/desk_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ui

import (
"image/color"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -59,54 +58,6 @@ func (*testDesk) Modules() []fynedesk.Module {
return nil
}

type testSettings struct {
background string
iconTheme string
launcherIcons []string
launcherIconSize int
launcherZoomScale float64
launcherDisableZoom bool
launcherDisableTaskbar bool
}

func (ts *testSettings) IconTheme() string {
return ts.iconTheme
}

func (ts *testSettings) Background() string {
return ts.background
}

func (ts *testSettings) LauncherIcons() []string {
return ts.launcherIcons
}

func (ts *testSettings) LauncherIconSize() int {
if ts.launcherIconSize == 0 {
return 32
}
return ts.launcherIconSize
}

func (ts *testSettings) LauncherDisableTaskbar() bool {
return ts.launcherDisableTaskbar
}

func (ts *testSettings) LauncherDisableZoom() bool {
return ts.launcherDisableZoom
}

func (ts *testSettings) LauncherZoomScale() float64 {
if ts.launcherZoomScale == 0 {
return 1.0
}
return ts.launcherZoomScale
}

func (*testSettings) AddChangeListener(listener chan fynedesk.DeskSettings) {
return
}

type testScreensProvider struct {
screens []*fynedesk.Screen
primary *fynedesk.Screen
Expand Down Expand Up @@ -218,7 +169,7 @@ func TestDeskLayout_Layout(t *testing.T) {
l := &deskLayout{screens: &testScreensProvider{screens: []*fynedesk.Screen{{Name: "Screen0", X: 0, Y: 0,
Width: 2000, Height: 1000, Scale: 1.0}}}}
l.bar = testBar([]string{})
l.widgets = canvas.NewRectangle(color.Black)
l.widgets = newWidgetPanel(l)
bg := &background{wallpaper: canvas.NewImageFromResource(theme.FyneLogo())}
l.backgroundScreenMap = make(map[*background]*fynedesk.Screen)
l.backgroundScreenMap[bg] = l.screens.Primary()
Expand Down
30 changes: 29 additions & 1 deletion internal/ui/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type deskSettings struct {
launcherDisableZoom bool
launcherZoomScale float64

moduleNames []string

listenerLock sync.Mutex
changeListeners []chan fynedesk.DeskSettings
}
Expand Down Expand Up @@ -51,6 +53,10 @@ func (d *deskSettings) LauncherZoomScale() float64 {
return d.launcherZoomScale
}

func (d *deskSettings) ModuleNames() []string {
return d.moduleNames
}

func (d *deskSettings) AddChangeListener(listener chan fynedesk.DeskSettings) {
d.listenerLock.Lock()
defer d.listenerLock.Unlock()
Expand All @@ -71,6 +77,16 @@ func (d *deskSettings) apply() {
}
}

func isModuleEnabled(name string, settings fynedesk.DeskSettings) bool {
for _, mod := range settings.ModuleNames() {
if mod == name {
return true
}
}

return false
}

func (d *deskSettings) setBackground(name string) {
d.background = name
fyne.CurrentApp().Preferences().SetString("background", d.background)
Expand Down Expand Up @@ -114,6 +130,13 @@ func (d *deskSettings) setLauncherZoomScale(scale float64) {
d.apply()
}

func (d *deskSettings) setModuleNames(names []string) {
newModuleNames := strings.Join(names, "|")
d.moduleNames = names
fyne.CurrentApp().Preferences().SetString("modulenames", newModuleNames)
d.apply()
}

func (d *deskSettings) load() {
env := os.Getenv("FYNEDESK_BACKGROUND")
if env != "" {
Expand All @@ -134,7 +157,7 @@ func (d *deskSettings) load() {

launcherIcons := fyne.CurrentApp().Preferences().String("launchericons")
if launcherIcons != "" {
d.launcherIcons = strings.SplitN(fyne.CurrentApp().Preferences().String("launchericons"), "|", -1)
d.launcherIcons = strings.Split(launcherIcons, "|")
}
if len(d.launcherIcons) == 0 {
defaultApps := fynedesk.Instance().IconProvider().DefaultApps()
Expand All @@ -155,6 +178,11 @@ func (d *deskSettings) load() {
if d.launcherZoomScale == 0.0 {
d.launcherZoomScale = 2.0
}

moduleNames := fyne.CurrentApp().Preferences().StringWithFallback("modulenames", "Battery|Brightness")
if moduleNames != "" {
d.moduleNames = strings.Split(moduleNames, "|")
}
}

// newDeskSettings loads the user's preferences from environment or config
Expand Down
72 changes: 72 additions & 0 deletions internal/ui/settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ui

import (
"testing"

"github.com/stretchr/testify/assert"

"fyne.io/fynedesk"
)

func TestDeskSettings_IsModuleEnabled(t *testing.T) {
s := &testSettings{moduleNames: []string{"Yes", "maybe"}}

assert.True(t, isModuleEnabled("Yes", s))
assert.True(t, isModuleEnabled("maybe", s))
assert.False(t, isModuleEnabled("Maybe", s))
assert.False(t, isModuleEnabled("No", s))
}

type testSettings struct {
background string
iconTheme string
launcherIcons []string
launcherIconSize int
launcherZoomScale float64
launcherDisableZoom bool
launcherDisableTaskbar bool

moduleNames []string
}

func (ts *testSettings) IconTheme() string {
return ts.iconTheme
}

func (ts *testSettings) Background() string {
return ts.background
}

func (ts *testSettings) LauncherIcons() []string {
return ts.launcherIcons
}

func (ts *testSettings) LauncherIconSize() int {
if ts.launcherIconSize == 0 {
return 32
}
return ts.launcherIconSize
}

func (ts *testSettings) LauncherDisableTaskbar() bool {
return ts.launcherDisableTaskbar
}

func (ts *testSettings) LauncherDisableZoom() bool {
return ts.launcherDisableZoom
}

func (ts *testSettings) LauncherZoomScale() float64 {
if ts.launcherZoomScale == 0 {
return 1.0
}
return ts.launcherZoomScale
}

func (ts *testSettings) ModuleNames() []string {
return ts.moduleNames
}

func (*testSettings) AddChangeListener(listener chan fynedesk.DeskSettings) {
return
}

0 comments on commit d63d4d8

Please sign in to comment.