65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
|
|
package settings
|
||
|
|
|
||
|
|
import "strings"
|
||
|
|
|
||
|
|
const (
|
||
|
|
MinReadingSpeed = 100
|
||
|
|
MaxReadingSpeed = 1000
|
||
|
|
|
||
|
|
MinFontSize = 24
|
||
|
|
MaxFontSize = 96
|
||
|
|
|
||
|
|
ThemeDark = "dark"
|
||
|
|
ThemeLight = "light"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Settings contains user preferences.
|
||
|
|
type Settings struct {
|
||
|
|
ReadingSpeed int `json:"readingSpeed"`
|
||
|
|
FontSize int `json:"fontSize"`
|
||
|
|
Theme string `json:"theme"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// Default returns the default application settings.
|
||
|
|
func Default() Settings {
|
||
|
|
return Settings{
|
||
|
|
ReadingSpeed: 300,
|
||
|
|
FontSize: 48,
|
||
|
|
Theme: ThemeDark,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Validate checks whether all settings contain supported values.
|
||
|
|
func (s Settings) Validate() bool {
|
||
|
|
return s.ReadingSpeed >= MinReadingSpeed &&
|
||
|
|
s.ReadingSpeed <= MaxReadingSpeed &&
|
||
|
|
s.FontSize >= MinFontSize &&
|
||
|
|
s.FontSize <= MaxFontSize &&
|
||
|
|
(s.Theme == ThemeDark || s.Theme == ThemeLight)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Normalize returns settings with values restricted to supported options.
|
||
|
|
func (s Settings) Normalize() Settings {
|
||
|
|
defaults := Default()
|
||
|
|
|
||
|
|
if s.ReadingSpeed < MinReadingSpeed {
|
||
|
|
s.ReadingSpeed = MinReadingSpeed
|
||
|
|
} else if s.ReadingSpeed > MaxReadingSpeed {
|
||
|
|
s.ReadingSpeed = MaxReadingSpeed
|
||
|
|
}
|
||
|
|
|
||
|
|
if s.FontSize < MinFontSize {
|
||
|
|
s.FontSize = MinFontSize
|
||
|
|
} else if s.FontSize > MaxFontSize {
|
||
|
|
s.FontSize = MaxFontSize
|
||
|
|
}
|
||
|
|
|
||
|
|
s.Theme = strings.ToLower(strings.TrimSpace(s.Theme))
|
||
|
|
|
||
|
|
if s.Theme != ThemeDark && s.Theme != ThemeLight {
|
||
|
|
s.Theme = defaults.Theme
|
||
|
|
}
|
||
|
|
|
||
|
|
return s
|
||
|
|
}
|