Skip to content

Commit

Permalink
De-duplicate list lookup code and test
Browse files Browse the repository at this point in the history
Moved code round a little so settings were in a sensible place
  • Loading branch information
andydotxyz committed Apr 19, 2020
1 parent c741019 commit 8e18a13
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 75 deletions.
10 changes: 0 additions & 10 deletions internal/ui/desk.go
Expand Up @@ -217,16 +217,6 @@ func (l *deskLayout) WindowManager() fynedesk.WindowManager {
return l.wm
}

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

return false
}

func (l *deskLayout) Modules() []fynedesk.Module {
var mods []fynedesk.Module
for _, meta := range fynedesk.AvailableModules() {
Expand Down
54 changes: 0 additions & 54 deletions internal/ui/desk_test.go
Expand Up @@ -58,60 +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

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
}

type testScreensProvider struct {
screens []*fynedesk.Screen
primary *fynedesk.Screen
Expand Down
10 changes: 10 additions & 0 deletions internal/ui/settings.go
Expand Up @@ -77,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
72 changes: 72 additions & 0 deletions internal/ui/settings_test.go
@@ -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
}
12 changes: 1 addition & 11 deletions internal/ui/settings_ui.go
Expand Up @@ -205,22 +205,12 @@ func (d *settingsUI) loadBarScreen() fyne.CanvasObject {
header, applyButton, widget.NewVBox(bar, details))
}

func listContains(list []string, item string) bool {
for _, listItem := range list {
if listItem == item {
return true
}
}

return false
}

func (d *settingsUI) loadModuleScreen() fyne.CanvasObject {
var modules []fyne.CanvasObject

for _, mod := range fynedesk.AvailableModules() {
name := mod.Name
enabled := listContains(d.settings.moduleNames, name)
enabled := isModuleEnabled(name, d.settings)

check := widget.NewCheck(name, func(bool) {})
check.SetChecked(enabled)
Expand Down

0 comments on commit 8e18a13

Please sign in to comment.