Leonid Nikitin 82167f042f
Add theme management functionality to the application
Implemented a theme management system allowing users to select and persist themes (default, light, dark) in the settings menu.
2025-05-23 20:18:05 +05:00

175 lines
4.9 KiB
Go

package handler
import (
"fyne.io/fyne/v2"
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/kernel"
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/localizer"
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/menu"
"git.kor-elf.net/kor-elf/gui-for-ffmpeg/theme"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
type MenuHandlerContract interface {
GetMainMenu() *fyne.MainMenu
LanguageSelection()
}
type MenuHandler struct {
app kernel.AppContract
convertorHandler ConvertorHandlerContract
menuView menu.ViewContract
menuViewSetting menu.ViewSettingContract
localizerView localizer.ViewContract
localizerRepository localizer.RepositoryContract
themeService theme.ThemeContract
}
func NewMenuHandler(
app kernel.AppContract,
convertorHandler ConvertorHandlerContract,
menuView menu.ViewContract,
menuViewSetting menu.ViewSettingContract,
localizerView localizer.ViewContract,
localizerRepository localizer.RepositoryContract,
themeService theme.ThemeContract,
) *MenuHandler {
return &MenuHandler{
app: app,
convertorHandler: convertorHandler,
menuView: menuView,
menuViewSetting: menuViewSetting,
localizerView: localizerView,
localizerRepository: localizerRepository,
themeService: themeService,
}
}
func (h MenuHandler) GetMainMenu() *fyne.MainMenu {
settings := h.getMenuSettings()
help := h.getMenuHelp()
return fyne.NewMainMenu(settings, help)
}
func (h MenuHandler) getMenuSettings() *fyne.Menu {
quit := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "exit",
}), nil)
quit.IsQuit = true
h.app.GetLocalizerService().AddChangeCallback("exit", func(text string) {
quit.Label = text
})
settingsSelection := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "settings",
}), h.settingsSelection)
h.app.GetLocalizerService().AddChangeCallback("settings", func(text string) {
settingsSelection.Label = text
})
ffPathSelection := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "changeFFPath",
}), h.convertorHandler.FfPathSelection)
h.app.GetLocalizerService().AddChangeCallback("changeFFPath", func(text string) {
ffPathSelection.Label = text
})
settings := fyne.NewMenu(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "settings",
}), settingsSelection, ffPathSelection, quit)
h.app.GetLocalizerService().AddChangeCallback("settings", func(text string) {
settings.Label = text
settings.Refresh()
})
return settings
}
func (h MenuHandler) getMenuHelp() *fyne.Menu {
about := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "about",
}), h.openAbout)
h.app.GetLocalizerService().AddChangeCallback("about", func(text string) {
about.Label = text
})
gratitude := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "gratitude",
}), h.menuView.Gratitude)
h.app.GetLocalizerService().AddChangeCallback("gratitude", func(text string) {
gratitude.Label = text
})
helpFFplay := fyne.NewMenuItem(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "helpFFplay",
}), h.menuView.HelpFFplay)
h.app.GetLocalizerService().AddChangeCallback("helpFFplay", func(text string) {
helpFFplay.Label = text
})
help := fyne.NewMenu(h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "help",
}), helpFFplay, about, gratitude)
h.app.GetLocalizerService().AddChangeCallback("help", func(text string) {
help.Label = text
help.Refresh()
})
return help
}
func (h MenuHandler) openAbout() {
ffmpeg, err := h.convertorHandler.GetFfmpegVersion()
if err != nil {
ffmpeg = h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "errorFFmpegVersion",
})
}
ffprobe, err := h.convertorHandler.GetFfprobeVersion()
if err != nil {
ffprobe = h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "errorFFprobeVersion",
})
}
ffplay, err := h.convertorHandler.GetFfplayVersion()
if err != nil {
ffplay = h.app.GetLocalizerService().GetMessage(&i18n.LocalizeConfig{
MessageID: "errorFFplayVersion",
})
}
h.menuView.About(ffmpeg, ffprobe, ffplay)
}
func (h MenuHandler) LanguageSelection() {
h.localizerView.LanguageSelection(func(lang kernel.Lang) {
_, _ = h.localizerRepository.Save(lang.Code)
h.convertorHandler.MainConvertor()
})
}
func (h MenuHandler) settingsSelection() {
save := func(setting *menu.SettingForm) error {
err := h.app.GetLocalizerService().SetCurrentLanguage(setting.Language)
if err != nil {
return err
}
_, err = h.localizerRepository.Save(setting.Language.Code)
if err != nil {
return err
}
err = h.themeService.SetCurrentTheme(setting.ThemeInfo)
if err != nil {
return err
}
h.convertorHandler.MainConvertor()
return nil
}
cancel := func() {
h.convertorHandler.MainConvertor()
}
h.menuViewSetting.Main(save, cancel)
}