Replaced the `i18n` and `toml` dependencies with Fyne's built-in language system for localization management. Updated the `Localizer` implementation to handle translations using JSON files and embed functionality. Simplified language selection and persisted settings via Fyne's preferences API.
149 lines
3.4 KiB
Go
149 lines
3.4 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"
|
|
"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 {
|
|
_ = t.repository.Save(themeInfo.GetName())
|
|
|
|
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("themesNameDefault"),
|
|
}
|
|
|
|
themesNameLight := &themeInfo{
|
|
name: "light",
|
|
title: localizer.GetMessage("themesNameLight"),
|
|
variant: fyneTheme.VariantLight,
|
|
}
|
|
|
|
themesNameDark := &themeInfo{
|
|
name: "dark",
|
|
title: localizer.GetMessage("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)
|
|
}
|