v/GTT
1
0
mirror of https://github.com/eeeXun/GTT.git synced 2025-05-19 01:00:20 -07:00

Merge branch 'master' into part_of_speech

This commit is contained in:
eeeXun 2023-01-11 16:51:57 +08:00
commit 40179f17e2
3 changed files with 78 additions and 68 deletions

View File

@ -24,7 +24,7 @@ func configInit() {
} }
config.AddConfigPath("$HOME/.config/gtt") config.AddConfigPath("$HOME/.config/gtt")
// create config file if not exists // Create config file if not exists
if err := config.ReadInConfig(); err != nil { if err := config.ReadInConfig(); err != nil {
config.Set("transparent", false) config.Set("transparent", false)
config.Set("theme", "Gruvbox") config.Set("theme", "Gruvbox")

View File

@ -45,10 +45,10 @@ func main() {
configInit() configInit()
uiInit() uiInit()
mainPage.AddPage("translatePage", translateWindow, true, true) mainPage.AddPage("translateWindow", translateWindow, true, true)
mainPage.AddPage("langPage", langWindow, true, false) mainPage.AddPage("langWindow", langWindow, true, false)
mainPage.AddPage("stylePage", styleWindow, true, false) mainPage.AddPage("styleWindow", styleWindow, true, false)
mainPage.AddPage("keyMapPage", keyMapWindow, true, false) mainPage.AddPage("keyMapWindow", keyMapWindow, true, false)
if err := app.SetRoot(mainPage, true). if err := app.SetRoot(mainPage, true).
EnableMouse(true).Run(); err != nil { EnableMouse(true).Run(); err != nil {

136
ui.go
View File

@ -43,10 +43,10 @@ const (
func updateBackgroundColor() { func updateBackgroundColor() {
// input/output // input/output
srcInput.SetBackgroundColor(style.BackgroundColor())
srcInput.SetTextStyle(tcell.StyleDefault. srcInput.SetTextStyle(tcell.StyleDefault.
Background(style.BackgroundColor()). Background(style.BackgroundColor()).
Foreground(style.ForegroundColor())) Foreground(style.ForegroundColor())).
SetBackgroundColor(style.BackgroundColor())
dstOutput.SetBackgroundColor(style.BackgroundColor()) dstOutput.SetBackgroundColor(style.BackgroundColor())
defOutput.SetBackgroundColor(style.BackgroundColor()) defOutput.SetBackgroundColor(style.BackgroundColor())
defOutput.SetTextStyle(tcell.StyleDefault. defOutput.SetTextStyle(tcell.StyleDefault.
@ -58,48 +58,48 @@ func updateBackgroundColor() {
Foreground(style.ForegroundColor())) Foreground(style.ForegroundColor()))
// dropdown // dropdown
srcLangDropDown.SetBackgroundColor(style.BackgroundColor())
srcLangDropDown.SetListStyles(tcell.StyleDefault. srcLangDropDown.SetListStyles(tcell.StyleDefault.
Background(style.BackgroundColor()). Background(style.BackgroundColor()).
Foreground(style.ForegroundColor()), Foreground(style.ForegroundColor()),
tcell.StyleDefault. tcell.StyleDefault.
Background(style.SelectedColor()). Background(style.SelectedColor()).
Foreground(style.PrefixColor())) Foreground(style.PrefixColor())).
dstLangDropDown.SetBackgroundColor(style.BackgroundColor()) SetBackgroundColor(style.BackgroundColor())
dstLangDropDown.SetListStyles(tcell.StyleDefault. dstLangDropDown.SetListStyles(tcell.StyleDefault.
Background(style.BackgroundColor()). Background(style.BackgroundColor()).
Foreground(style.ForegroundColor()), Foreground(style.ForegroundColor()),
tcell.StyleDefault. tcell.StyleDefault.
Background(style.SelectedColor()). Background(style.SelectedColor()).
Foreground(style.PrefixColor())) Foreground(style.PrefixColor())).
themeDropDown.SetBackgroundColor(style.BackgroundColor()) SetBackgroundColor(style.BackgroundColor())
themeDropDown.SetListStyles(tcell.StyleDefault. themeDropDown.SetListStyles(tcell.StyleDefault.
Background(style.BackgroundColor()). Background(style.BackgroundColor()).
Foreground(style.ForegroundColor()), Foreground(style.ForegroundColor()),
tcell.StyleDefault. tcell.StyleDefault.
Background(style.SelectedColor()). Background(style.SelectedColor()).
Foreground(style.PrefixColor())) Foreground(style.PrefixColor())).
transparentDropDown.SetBackgroundColor(style.BackgroundColor()) SetBackgroundColor(style.BackgroundColor())
transparentDropDown.SetListStyles(tcell.StyleDefault. transparentDropDown.SetListStyles(tcell.StyleDefault.
Background(style.BackgroundColor()). Background(style.BackgroundColor()).
Foreground(style.ForegroundColor()), Foreground(style.ForegroundColor()),
tcell.StyleDefault. tcell.StyleDefault.
Background(style.SelectedColor()). Background(style.SelectedColor()).
Foreground(style.PrefixColor())) Foreground(style.PrefixColor())).
srcBorderDropDown.SetBackgroundColor(style.BackgroundColor()) SetBackgroundColor(style.BackgroundColor())
srcBorderDropDown.SetListStyles(tcell.StyleDefault. srcBorderDropDown.SetListStyles(tcell.StyleDefault.
Background(style.BackgroundColor()). Background(style.BackgroundColor()).
Foreground(style.ForegroundColor()), Foreground(style.ForegroundColor()),
tcell.StyleDefault. tcell.StyleDefault.
Background(style.SelectedColor()). Background(style.SelectedColor()).
Foreground(style.PrefixColor())) Foreground(style.PrefixColor())).
dstBorderDropDown.SetBackgroundColor(style.BackgroundColor()) SetBackgroundColor(style.BackgroundColor())
dstBorderDropDown.SetListStyles(tcell.StyleDefault. dstBorderDropDown.SetListStyles(tcell.StyleDefault.
Background(style.BackgroundColor()). Background(style.BackgroundColor()).
Foreground(style.ForegroundColor()), Foreground(style.ForegroundColor()),
tcell.StyleDefault. tcell.StyleDefault.
Background(style.SelectedColor()). Background(style.SelectedColor()).
Foreground(style.PrefixColor())) Foreground(style.PrefixColor())).
SetBackgroundColor(style.BackgroundColor())
// key map // key map
keyMapMenu.SetBackgroundColor(style.BackgroundColor()) keyMapMenu.SetBackgroundColor(style.BackgroundColor())
@ -147,20 +147,20 @@ func updateNonConfigColor() {
dstLangDropDown.SetFieldBackgroundColor(style.SelectedColor()). dstLangDropDown.SetFieldBackgroundColor(style.SelectedColor()).
SetFieldTextColor(style.ForegroundColor()). SetFieldTextColor(style.ForegroundColor()).
SetPrefixTextColor(style.PrefixColor()) SetPrefixTextColor(style.PrefixColor())
themeDropDown.SetLabelColor(style.LabelColor()) themeDropDown.SetLabelColor(style.LabelColor()).
themeDropDown.SetFieldBackgroundColor(style.SelectedColor()). SetFieldBackgroundColor(style.SelectedColor()).
SetFieldTextColor(style.ForegroundColor()). SetFieldTextColor(style.ForegroundColor()).
SetPrefixTextColor(style.PrefixColor()) SetPrefixTextColor(style.PrefixColor())
transparentDropDown.SetLabelColor(style.LabelColor()) transparentDropDown.SetLabelColor(style.LabelColor()).
transparentDropDown.SetFieldBackgroundColor(style.SelectedColor()). SetFieldBackgroundColor(style.SelectedColor()).
SetFieldTextColor(style.ForegroundColor()). SetFieldTextColor(style.ForegroundColor()).
SetPrefixTextColor(style.PrefixColor()) SetPrefixTextColor(style.PrefixColor())
srcBorderDropDown.SetLabelColor(style.LabelColor()) srcBorderDropDown.SetLabelColor(style.LabelColor()).
srcBorderDropDown.SetFieldBackgroundColor(style.SelectedColor()). SetFieldBackgroundColor(style.SelectedColor()).
SetFieldTextColor(style.ForegroundColor()). SetFieldTextColor(style.ForegroundColor()).
SetPrefixTextColor(style.PrefixColor()) SetPrefixTextColor(style.PrefixColor())
dstBorderDropDown.SetLabelColor(style.LabelColor()) dstBorderDropDown.SetLabelColor(style.LabelColor()).
dstBorderDropDown.SetFieldBackgroundColor(style.SelectedColor()). SetFieldBackgroundColor(style.SelectedColor()).
SetFieldTextColor(style.ForegroundColor()). SetFieldTextColor(style.ForegroundColor()).
SetPrefixTextColor(style.PrefixColor()) SetPrefixTextColor(style.PrefixColor())
@ -197,10 +197,14 @@ func updateAllColor() {
func updateTitle() { func updateTitle() {
srcInput.SetTitle(translator.SrcLang) srcInput.SetTitle(translator.SrcLang)
dstOutput.SetTitle(translator.DstLang) dstOutput.SetTitle(translator.DstLang)
srcLangDropDown.SetCurrentOption(IndexOf(translator.SrcLang, translate.Lang)) srcLangDropDown.SetCurrentOption(
srcLangDropDown.SetTitle(translator.SrcLang) IndexOf(translator.SrcLang,
dstLangDropDown.SetCurrentOption(IndexOf(translator.DstLang, translate.Lang)) translate.Lang)).
dstLangDropDown.SetTitle(translator.DstLang) SetTitle(translator.SrcLang)
dstLangDropDown.SetCurrentOption(
IndexOf(translator.DstLang,
translate.Lang)).
SetTitle(translator.DstLang)
} }
func attachButton() *tview.Flex { func attachButton() *tview.Flex {
@ -224,10 +228,10 @@ func uiInit() {
SetTitle("Part of speech") SetTitle("Part of speech")
// dropdown // dropdown
srcLangDropDown.SetBorder(true) srcLangDropDown.SetOptions(translate.Lang, nil).
srcLangDropDown.SetOptions(translate.Lang, nil) SetBorder(true)
dstLangDropDown.SetBorder(true) dstLangDropDown.SetOptions(translate.Lang, nil).
dstLangDropDown.SetOptions(translate.Lang, nil) SetBorder(true)
themeDropDown.SetLabel("Theme: "). themeDropDown.SetLabel("Theme: ").
SetOptions(color.AllTheme, nil). SetOptions(color.AllTheme, nil).
SetCurrentOption(IndexOf(style.Theme, color.AllTheme)) SetCurrentOption(IndexOf(style.Theme, color.AllTheme))
@ -238,19 +242,25 @@ func uiInit() {
[]string{"true", "false"})) []string{"true", "false"}))
srcBorderDropDown.SetLabel("Border Color: "). srcBorderDropDown.SetLabel("Border Color: ").
SetOptions(color.Palette, nil). SetOptions(color.Palette, nil).
SetCurrentOption(IndexOf(style.SrcBorderStr(), color.Palette)) SetCurrentOption(
srcBorderDropDown.SetBorder(true). IndexOf(style.SrcBorderStr(),
color.Palette)).
SetBorder(true).
SetTitle("Source") SetTitle("Source")
dstBorderDropDown.SetLabel("Border Color: "). dstBorderDropDown.SetLabel("Border Color: ").
SetOptions(color.Palette, nil). SetOptions(color.Palette, nil).
SetCurrentOption(IndexOf(style.DstBorderStr(), color.Palette)) SetCurrentOption(
dstBorderDropDown.SetBorder(true). IndexOf(style.DstBorderStr(),
color.Palette)).
SetBorder(true).
SetTitle("Destination") SetTitle("Destination")
// key map // key map
keyMapMenu.SetBorder(true). keyMapMenu.SetDynamicColors(true).
SetText(fmt.Sprintf(keyMapText,
fmt.Sprintf("%.6x", style.HighLightColor().TrueColor().Hex()))).
SetBorder(true).
SetTitle("Key Map") SetTitle("Key Map")
keyMapMenu.SetDynamicColors(true)
// window // window
translateWindow.SetDirection(tview.FlexColumn). translateWindow.SetDirection(tview.FlexColumn).
@ -309,7 +319,7 @@ func uiInit() {
// handler // handler
mainPage.SetInputCapture(mainPageHandler) mainPage.SetInputCapture(mainPageHandler)
translateWindow.SetInputCapture(translatePageHandler) translateWindow.SetInputCapture(translateWindowHandler)
langWindow.SetInputCapture(popOutWindowHandler) langWindow.SetInputCapture(popOutWindowHandler)
styleWindow.SetInputCapture(popOutWindowHandler) styleWindow.SetInputCapture(popOutWindowHandler)
keyMapWindow.SetInputCapture(popOutWindowHandler) keyMapWindow.SetInputCapture(popOutWindowHandler)
@ -348,25 +358,25 @@ func uiInit() {
keyMapMenu.SetDoneFunc(func(key tcell.Key) { keyMapMenu.SetDoneFunc(func(key tcell.Key) {
switch key { switch key {
case tcell.KeyEsc: case tcell.KeyEsc:
mainPage.HidePage("keyMapPage") mainPage.HidePage("keyMapWindow")
} }
}) })
langButton.SetSelectedFunc(func() { langButton.SetSelectedFunc(func() {
mainPage.HidePage("stylePage") mainPage.HidePage("styleWindow")
mainPage.HidePage("keyMapPage") mainPage.HidePage("keyMapWindow")
mainPage.ShowPage("langPage") mainPage.ShowPage("langWindow")
app.SetFocus(langCycle.GetCurrentUI()) app.SetFocus(langCycle.GetCurrentUI())
}) })
styleButton.SetSelectedFunc(func() { styleButton.SetSelectedFunc(func() {
mainPage.HidePage("langPage") mainPage.HidePage("langWindow")
mainPage.HidePage("keyMapPage") mainPage.HidePage("keyMapWindow")
mainPage.ShowPage("stylePage") mainPage.ShowPage("styleWindow")
app.SetFocus(styleCycle.GetCurrentUI()) app.SetFocus(styleCycle.GetCurrentUI())
}) })
keyMapButton.SetSelectedFunc(func() { keyMapButton.SetSelectedFunc(func() {
mainPage.HidePage("langPage") mainPage.HidePage("langWindow")
mainPage.HidePage("stylePage") mainPage.HidePage("styleWindow")
mainPage.ShowPage("keyMapPage") mainPage.ShowPage("keyMapWindow")
}) })
} }
@ -386,17 +396,17 @@ func mainPageHandler(event *tcell.EventKey) *tcell.EventKey {
return event return event
} }
func translatePageHandler(event *tcell.EventKey) *tcell.EventKey { func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
key := event.Key() key := event.Key()
switch key { switch key {
case tcell.KeyEsc: case tcell.KeyEsc:
mainPage.ShowPage("langPage") mainPage.ShowPage("langWindow")
app.SetFocus(langCycle.GetCurrentUI()) app.SetFocus(langCycle.GetCurrentUI())
case tcell.KeyCtrlJ: case tcell.KeyCtrlJ:
message := srcInput.GetText() message := srcInput.GetText()
if len(message) > 0 {
// Only translate when message exist // Only translate when message exist
if len(message) > 0 {
translation, partOfSpeech, err := translator.Translate(message) translation, partOfSpeech, err := translator.Translate(message)
if err != nil { if err != nil {
dstOutput.SetText(err.Error()) dstOutput.SetText(err.Error())
@ -447,8 +457,8 @@ func translatePageHandler(event *tcell.EventKey) *tcell.EventKey {
// Play source sound // Play source sound
if translator.SoundLock.Available() { if translator.SoundLock.Available() {
message := srcInput.GetText() message := srcInput.GetText()
if len(message) > 0 {
// Only play when message exist // Only play when message exist
if len(message) > 0 {
translator.SoundLock.Acquire() translator.SoundLock.Acquire()
go func() { go func() {
err := translator.PlaySound(translator.SrcLang, message) err := translator.PlaySound(translator.SrcLang, message)
@ -463,8 +473,8 @@ func translatePageHandler(event *tcell.EventKey) *tcell.EventKey {
// Play destination sound // Play destination sound
if translator.SoundLock.Available() { if translator.SoundLock.Available() {
message := dstOutput.GetText(false) message := dstOutput.GetText(false)
if len(message) > 0 {
// Only play when message exist // Only play when message exist
if len(message) > 0 {
translator.SoundLock.Acquire() translator.SoundLock.Acquire()
go func() { go func() {
err := translator.PlaySound(translator.DstLang, message) err := translator.PlaySound(translator.DstLang, message)
@ -487,19 +497,19 @@ func popOutWindowHandler(event *tcell.EventKey) *tcell.EventKey {
switch ch { switch ch {
case '1': case '1':
mainPage.HidePage("stylePage") mainPage.HidePage("styleWindow")
mainPage.HidePage("keyMapPage") mainPage.HidePage("keyMapWindow")
mainPage.ShowPage("langPage") mainPage.ShowPage("langWindow")
app.SetFocus(langCycle.GetCurrentUI()) app.SetFocus(langCycle.GetCurrentUI())
case '2': case '2':
mainPage.HidePage("langPage") mainPage.HidePage("langWindow")
mainPage.HidePage("keyMapPage") mainPage.HidePage("keyMapWindow")
mainPage.ShowPage("stylePage") mainPage.ShowPage("styleWindow")
app.SetFocus(styleCycle.GetCurrentUI()) app.SetFocus(styleCycle.GetCurrentUI())
case '3': case '3':
mainPage.HidePage("langPage") mainPage.HidePage("langWindow")
mainPage.HidePage("stylePage") mainPage.HidePage("styleWindow")
mainPage.ShowPage("keyMapPage") mainPage.ShowPage("keyMapWindow")
} }
return event return event
@ -514,7 +524,7 @@ func langDropDownHandler(key tcell.Key) {
langCycle.Decrease() langCycle.Decrease()
app.SetFocus(langCycle.GetCurrentUI()) app.SetFocus(langCycle.GetCurrentUI())
case tcell.KeyEsc: case tcell.KeyEsc:
mainPage.HidePage("langPage") mainPage.HidePage("langWindow")
} }
} }
@ -527,6 +537,6 @@ func styleDropDownHandler(key tcell.Key) {
styleCycle.Decrease() styleCycle.Decrease()
app.SetFocus(styleCycle.GetCurrentUI()) app.SetFocus(styleCycle.GetCurrentUI())
case tcell.KeyEsc: case tcell.KeyEsc:
mainPage.HidePage("stylePage") mainPage.HidePage("styleWindow")
} }
} }