From 7eaee14787a73f2ff4923b3f8423b5943fbbad01 Mon Sep 17 00:00:00 2001 From: eeeXun Date: Mon, 24 Oct 2022 17:40:32 +0800 Subject: [PATCH] refactor: Theme & Transparent to style --- color.go | 20 +++++++++++--------- config.go | 12 ++++++------ main.go | 8 +++----- ui.go | 12 ++++++------ 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/color.go b/color.go index 4fb5672..d38684c 100644 --- a/color.go +++ b/color.go @@ -47,6 +47,8 @@ type Style struct { prefixColor string labelColor string pressColor string + Theme string + Transparent bool } func NewStyle() *Style { @@ -61,38 +63,38 @@ func NewStyle() *Style { } func (s Style) BackgroundColor() tcell.Color { - if transparent { + if s.Transparent { return Transparent } - return Themes[theme][s.backgroundColor] + return Themes[s.Theme][s.backgroundColor] } func (s Style) ForegroundColor() tcell.Color { - return Themes[theme][s.foregroundColor] + return Themes[s.Theme][s.foregroundColor] } func (s Style) SelectedColor() tcell.Color { - return Themes[theme][s.selectedColor] + return Themes[s.Theme][s.selectedColor] } func (s Style) PrefixColor() tcell.Color { - return Themes[theme][s.prefixColor] + return Themes[s.Theme][s.prefixColor] } func (s Style) LabelColor() tcell.Color { - return Themes[theme][s.labelColor] + return Themes[s.Theme][s.labelColor] } func (s Style) PressColor() tcell.Color { - return Themes[theme][s.pressColor] + return Themes[s.Theme][s.pressColor] } func (s Style) SrcBorderColor() tcell.Color { - return Themes[theme][s.src.borderColor] + return Themes[s.Theme][s.src.borderColor] } func (s Style) DstBorderColor() tcell.Color { - return Themes[theme][s.dst.borderColor] + return Themes[s.Theme][s.dst.borderColor] } func (s Style) SrcBorderStr() string { diff --git a/config.go b/config.go index e54f3bc..dc75389 100644 --- a/config.go +++ b/config.go @@ -33,10 +33,10 @@ func configInit() { } // setup - theme = config.GetString("theme") - transparent = config.GetBool("transparent") translator.srcLang = config.GetString("source.language") translator.dstLang = config.GetString("destination.language") + style.Theme = config.GetString("theme") + style.Transparent = config.GetBool("transparent") style.SetSrcBorderColor(config.GetString("source.borderColor")). SetDstBorderColor(config.GetString("destination.borderColor")) } @@ -45,13 +45,13 @@ func configInit() { func updateConfig() { changed := false - if config.GetString("theme") != theme { + if config.GetString("theme") != style.Theme { changed = true - config.Set("theme", theme) + config.Set("theme", style.Theme) } - if config.GetBool("transparent") != transparent { + if config.GetBool("transparent") != style.Transparent { changed = true - config.Set("transparent", transparent) + config.Set("transparent", style.Transparent) } if config.GetString("source.language") != translator.srcLang { changed = true diff --git a/main.go b/main.go index c288545..ad5d3f2 100644 --- a/main.go +++ b/main.go @@ -27,11 +27,9 @@ var ( langWindow = tview.NewFlex() styleWindow = tview.NewFlex() mainPage = tview.NewPages() - style = NewStyle() - // config - config = viper.New() - theme string - transparent bool + // settings + config = viper.New() + style = NewStyle() ) func main() { diff --git a/ui.go b/ui.go index 3b920fc..8e24adf 100644 --- a/ui.go +++ b/ui.go @@ -194,11 +194,11 @@ func uiInit() { dstLangDropDown.SetOptions(Lang, nil) themeDropDown.SetLabel("Theme: "). SetOptions(ThemesName, nil). - SetCurrentOption(IndexOf(theme, ThemesName)) + SetCurrentOption(IndexOf(style.Theme, ThemesName)) transparentDropDown.SetLabel("Transparent: "). SetOptions([]string{"true", "false"}, nil). SetCurrentOption( - IndexOf(strconv.FormatBool(transparent), + IndexOf(strconv.FormatBool(style.Transparent), []string{"true", "false"})) srcBorderDropDown.SetLabel("Border Color: "). SetOptions(Palette, nil). @@ -270,12 +270,12 @@ func uiInit() { }) themeDropDown.SetDoneFunc(styleDropDownHandler). SetSelectedFunc(func(text string, index int) { - theme = text + style.Theme = text updateAllColor() }) transparentDropDown.SetDoneFunc(styleDropDownHandler). SetSelectedFunc(func(text string, index int) { - transparent, _ = strconv.ParseBool(text) + style.Transparent, _ = strconv.ParseBool(text) updateBackgroundColor() }) srcBorderDropDown.SetDoneFunc(styleDropDownHandler). @@ -305,10 +305,10 @@ func pagesHandler(event *tcell.EventKey) *tcell.EventKey { switch key { case tcell.KeyCtrlT: - transparent = !transparent + style.Transparent = !style.Transparent updateBackgroundColor() transparentDropDown.SetCurrentOption( - IndexOf(strconv.FormatBool(transparent), + IndexOf(strconv.FormatBool(style.Transparent), []string{"true", "false"})) }