Implemented a theme management system allowing users to select and persist themes (default, light, dark) in the settings menu.
159 lines
3.6 KiB
Go
159 lines
3.6 KiB
Go
package theme
|
|
|
|
import (
|
|
"fyne.io/fyne/v2"
|
|
fyneTheme "fyne.io/fyne/v2/theme"
|
|
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/kernel"
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
"image/color"
|
|
)
|
|
|
|
type ThemeContract interface {
|
|
List() map[string]ThemeInfoContract
|
|
GetCurrentThemeInfo() ThemeInfoContract
|
|
SetCurrentTheme(themeInfo ThemeInfoContract) error
|
|
}
|
|
|
|
type theme struct {
|
|
app kernel.AppContract
|
|
repository RepositoryContract
|
|
list map[string]ThemeInfoContract
|
|
}
|
|
|
|
func NewTheme(app kernel.AppContract, repository RepositoryContract) ThemeContract {
|
|
theme := &theme{
|
|
app: app,
|
|
repository: repository,
|
|
list: getThemes(app.GetLocalizerService()),
|
|
}
|
|
|
|
theme.init()
|
|
|
|
return theme
|
|
}
|
|
|
|
func (t theme) init() {
|
|
themeInfo := t.GetCurrentThemeInfo()
|
|
if themeInfo.GetName() == "default" {
|
|
t.app.GetAppFyne().Settings().SetTheme(fyneTheme.DefaultTheme())
|
|
return
|
|
}
|
|
t.app.GetAppFyne().Settings().SetTheme(&forcedVariant{theme: fyneTheme.DefaultTheme(), variant: themeInfo.GetVariant()})
|
|
}
|
|
|
|
func (t theme) GetCurrentThemeInfo() ThemeInfoContract {
|
|
themes := t.List()
|
|
if themeInfo, ok := themes[t.repository.GetCode()]; ok {
|
|
return themeInfo
|
|
}
|
|
|
|
return themes["default"]
|
|
}
|
|
|
|
func (t theme) List() map[string]ThemeInfoContract {
|
|
return t.list
|
|
}
|
|
|
|
func (t theme) SetCurrentTheme(themeInfo ThemeInfoContract) error {
|
|
_, err := t.repository.Save(themeInfo.GetName())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if themeInfo.GetName() == "default" {
|
|
t.app.GetAppFyne().Settings().SetTheme(fyneTheme.DefaultTheme())
|
|
return nil
|
|
}
|
|
t.app.GetAppFyne().Settings().SetTheme(&forcedVariant{theme: fyneTheme.DefaultTheme(), variant: themeInfo.GetVariant()})
|
|
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
func getThemes(localizer kernel.LocalizerContract) map[string]ThemeInfoContract {
|
|
themesNameDefault := &themeInfo{
|
|
name: "default",
|
|
title: localizer.GetMessage(&i18n.LocalizeConfig{
|
|
MessageID: "themesNameDefault",
|
|
}),
|
|
}
|
|
|
|
themesNameLight := &themeInfo{
|
|
name: "light",
|
|
title: localizer.GetMessage(&i18n.LocalizeConfig{
|
|
MessageID: "themesNameLight",
|
|
}),
|
|
variant: fyneTheme.VariantLight,
|
|
}
|
|
|
|
themesNameDark := &themeInfo{
|
|
name: "dark",
|
|
title: localizer.GetMessage(&i18n.LocalizeConfig{
|
|
MessageID: "themesNameDark",
|
|
}),
|
|
variant: fyneTheme.VariantDark,
|
|
}
|
|
|
|
list := map[string]ThemeInfoContract{
|
|
"default": themesNameDefault,
|
|
"light": themesNameLight,
|
|
"dark": themesNameDark,
|
|
}
|
|
|
|
localizer.AddChangeCallback("themesNameDefault", func(text string) {
|
|
themesNameDefault.title = text
|
|
})
|
|
localizer.AddChangeCallback("themesNameLight", func(text string) {
|
|
themesNameLight.title = text
|
|
})
|
|
localizer.AddChangeCallback("themesNameDark", func(text string) {
|
|
themesNameDark.title = text
|
|
})
|
|
|
|
return list
|
|
}
|
|
|
|
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 fyneTheme.DefaultTheme().Font(style)
|
|
}
|
|
|
|
func (f *forcedVariant) Icon(name fyne.ThemeIconName) fyne.Resource {
|
|
return fyneTheme.DefaultTheme().Icon(name)
|
|
}
|
|
|
|
func (f *forcedVariant) Size(name fyne.ThemeSizeName) float32 {
|
|
return fyneTheme.DefaultTheme().Size(name)
|
|
}
|