From 303441125b9424a0cd1560fea314b8bb60e122f7 Mon Sep 17 00:00:00 2001 From: eeeXun Date: Tue, 14 Feb 2023 15:45:51 +0800 Subject: [PATCH] refacotr: move hide_below to style struct --- config.go | 13 +++---------- internal/style/style.go | 5 +++-- main.go | 3 +++ ui.go | 10 +++++----- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/config.go b/config.go index a84dbe9..88a465b 100644 --- a/config.go +++ b/config.go @@ -4,17 +4,10 @@ import ( "fmt" "os" - "github.com/eeeXun/gtt/internal/style" "github.com/eeeXun/gtt/internal/translate" config "github.com/spf13/viper" ) -var ( - // settings - uiStyle = style.NewStyle() - hideBelow bool -) - // Search XDG_CONFIG_HOME or $HOME/.config func configInit() { var ( @@ -77,8 +70,8 @@ func configInit() { config.GetString(fmt.Sprintf("destination.language.%s", name))) } translator = translators[config.GetString("translator")] - hideBelow = config.GetBool("hide_below") uiStyle.Theme = config.GetString("theme") + uiStyle.HideBelow = config.GetBool("hide_below") uiStyle.Transparent = config.GetBool("transparent") uiStyle.SetSrcBorderColor(config.GetString("source.borderColor")). SetDstBorderColor(config.GetString("destination.borderColor")) @@ -117,9 +110,9 @@ func updateConfig() { changed = true config.Set("translator", translator.GetEngineName()) } - if config.GetBool("hide_below") != hideBelow { + if config.GetBool("hide_below") != uiStyle.HideBelow { changed = true - config.Set("hide_below", hideBelow) + config.Set("hide_below", uiStyle.HideBelow) } if config.GetString("theme") != uiStyle.Theme { changed = true diff --git a/internal/style/style.go b/internal/style/style.go index e7407f8..a55429f 100644 --- a/internal/style/style.go +++ b/internal/style/style.go @@ -5,6 +5,9 @@ import ( ) type style struct { + HideBelow bool + Transparent bool + Theme string srcBorderColor string dstBorderColor string backgroundColor string @@ -14,8 +17,6 @@ type style struct { labelColor string pressColor string highLightColor string - Theme string - Transparent bool } func NewStyle() *style { diff --git a/main.go b/main.go index f96d2eb..68f8921 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "flag" + "github.com/eeeXun/gtt/internal/style" "github.com/eeeXun/gtt/internal/translate" "github.com/eeeXun/gtt/internal/ui" "github.com/rivo/tview" @@ -17,6 +18,8 @@ var ( // Translate translator translate.Translator translators = make(map[string]translate.Translator, len(translate.AllTranslator)) + // UI style + uiStyle = style.NewStyle() // UI app = tview.NewApplication() srcInput = tview.NewTextArea() diff --git a/ui.go b/ui.go index c3dbad3..dd58fa2 100644 --- a/ui.go +++ b/ui.go @@ -54,7 +54,7 @@ type Item struct { func updateTranslateWindow() { translateWindow.Clear() - if hideBelow { + if uiStyle.HideBelow { translateWindow.AddItem(translateAboveWidget, 0, 1, true) } else { translateWindow.SetDirection(tview.FlexRow). @@ -252,7 +252,7 @@ func uiInit() { hideBelowDropDown.SetLabel("Hide below: "). SetOptions([]string{"true", "false"}, nil). SetCurrentOption( - IndexOf(strconv.FormatBool(hideBelow), + IndexOf(strconv.FormatBool(uiStyle.HideBelow), []string{"true", "false"})) transparentDropDown.SetLabel("Transparent: "). SetOptions([]string{"true", "false"}, nil). @@ -382,7 +382,7 @@ func uiInit() { }) hideBelowDropDown.SetDoneFunc(styleDropDownHandler). SetSelectedFunc(func(text string, index int) { - hideBelow, _ = strconv.ParseBool(text) + uiStyle.HideBelow, _ = strconv.ParseBool(text) updateTranslateWindow() }) srcBorderDropDown.SetDoneFunc(styleDropDownHandler). @@ -432,10 +432,10 @@ func mainPageHandler(event *tcell.EventKey) *tcell.EventKey { IndexOf(strconv.FormatBool(uiStyle.Transparent), []string{"true", "false"})) case tcell.KeyCtrlBackslash: - hideBelow = !hideBelow + uiStyle.HideBelow = !uiStyle.HideBelow updateTranslateWindow() hideBelowDropDown.SetCurrentOption( - IndexOf(strconv.FormatBool(hideBelow), + IndexOf(strconv.FormatBool(uiStyle.HideBelow), []string{"true", "false"})) }