Introduce a new `MainSettings` view for managing application settings, including language and theme selection. Implement theme management methods in the `setting` package to handle theme initialization, retrieval, and updates.
111 lines
2.4 KiB
Go
111 lines
2.4 KiB
Go
package setting
|
|
|
|
import (
|
|
"fyne.io/fyne/v2"
|
|
"fyne.io/fyne/v2/lang"
|
|
"fyne.io/fyne/v2/theme"
|
|
"image/color"
|
|
)
|
|
|
|
func (s *setting) GetTheme() ThemeInfoContract {
|
|
name := s.fyneApp.Preferences().String("theme")
|
|
if name != "" {
|
|
if _, ok := s.GetThemes()[name]; ok {
|
|
return s.GetThemes()[name]
|
|
}
|
|
}
|
|
|
|
return s.GetThemes()["default"]
|
|
}
|
|
|
|
func (s *setting) SetTheme(themeInfo ThemeInfoContract) {
|
|
s.fyneApp.Preferences().SetString("theme", themeInfo.GetName())
|
|
|
|
if themeInfo.GetName() == "default" {
|
|
s.fyneApp.Settings().SetTheme(theme.DefaultTheme())
|
|
return
|
|
}
|
|
s.fyneApp.Settings().SetTheme(&forcedVariant{theme: theme.DefaultTheme(), variant: themeInfo.GetVariant()})
|
|
}
|
|
|
|
func (s *setting) ThemeInit() {
|
|
themeInfo := s.GetTheme()
|
|
if themeInfo.GetName() == "default" {
|
|
s.fyneApp.Settings().SetTheme(theme.DefaultTheme())
|
|
return
|
|
}
|
|
s.fyneApp.Settings().SetTheme(&forcedVariant{theme: theme.DefaultTheme(), variant: themeInfo.GetVariant()})
|
|
}
|
|
|
|
func (s *setting) GetThemes() map[string]ThemeInfoContract {
|
|
themesNameDefault := &themeInfo{
|
|
name: "default",
|
|
title: lang.L("themesNameDefault"),
|
|
}
|
|
|
|
themesNameLight := &themeInfo{
|
|
name: "light",
|
|
title: lang.L("themesNameLight"),
|
|
variant: theme.VariantLight,
|
|
}
|
|
|
|
themesNameDark := &themeInfo{
|
|
name: "dark",
|
|
title: lang.L("themesNameDark"),
|
|
variant: theme.VariantDark,
|
|
}
|
|
|
|
list := map[string]ThemeInfoContract{
|
|
"default": themesNameDefault,
|
|
"light": themesNameLight,
|
|
"dark": themesNameDark,
|
|
}
|
|
|
|
return list
|
|
}
|
|
|
|
type ThemeInfoContract interface {
|
|
GetName() string
|
|
GetTitle() string
|
|
GetVariant() fyne.ThemeVariant
|
|
}
|
|
|
|
type themeInfo struct {
|
|
name string
|
|
title string
|
|
variant fyne.ThemeVariant
|
|
}
|
|
|
|
func (inf *themeInfo) GetName() string {
|
|
return inf.name
|
|
}
|
|
|
|
func (inf *themeInfo) GetTitle() string {
|
|
return inf.title
|
|
}
|
|
|
|
func (inf *themeInfo) GetVariant() fyne.ThemeVariant {
|
|
return inf.variant
|
|
}
|
|
|
|
type forcedVariant struct {
|
|
theme fyne.Theme
|
|
variant fyne.ThemeVariant
|
|
}
|
|
|
|
func (f *forcedVariant) Color(name fyne.ThemeColorName, _ fyne.ThemeVariant) color.Color {
|
|
return f.theme.Color(name, f.variant)
|
|
}
|
|
|
|
func (f *forcedVariant) Font(style fyne.TextStyle) fyne.Resource {
|
|
return theme.DefaultTheme().Font(style)
|
|
}
|
|
|
|
func (f *forcedVariant) Icon(name fyne.ThemeIconName) fyne.Resource {
|
|
return theme.DefaultTheme().Icon(name)
|
|
}
|
|
|
|
func (f *forcedVariant) Size(name fyne.ThemeSizeName) float32 {
|
|
return theme.DefaultTheme().Size(name)
|
|
}
|