mirror of
https://github.com/eeeXun/GTT.git
synced 2025-05-16 07:40:44 -07:00
feat: add translatorDropDown
This commit is contained in:
parent
0faaa757e3
commit
1115d19b7a
@ -107,6 +107,10 @@ func updateConfig() {
|
||||
}
|
||||
}
|
||||
}
|
||||
if config.GetString("translator") != translator.GetEngineName() {
|
||||
changed = true
|
||||
config.Set("translator", translator.GetEngineName())
|
||||
}
|
||||
if config.GetBool("hide_below") != hideBelow {
|
||||
changed = true
|
||||
config.Set("hide_below", hideBelow)
|
||||
|
@ -15,9 +15,14 @@ const (
|
||||
)
|
||||
|
||||
type GoogleTranslate struct {
|
||||
srcLang string
|
||||
dstLang string
|
||||
SoundLock *lock.Lock
|
||||
srcLang string
|
||||
dstLang string
|
||||
EngineName string
|
||||
SoundLock *lock.Lock
|
||||
}
|
||||
|
||||
func (t *GoogleTranslate) GetEngineName() string {
|
||||
return t.EngineName
|
||||
}
|
||||
|
||||
func (t *GoogleTranslate) GetAllLang() []string {
|
||||
|
@ -15,9 +15,14 @@ const (
|
||||
)
|
||||
|
||||
type LibreTranslate struct {
|
||||
srcLang string
|
||||
dstLang string
|
||||
SoundLock *lock.Lock
|
||||
srcLang string
|
||||
dstLang string
|
||||
EngineName string
|
||||
SoundLock *lock.Lock
|
||||
}
|
||||
|
||||
func (t *LibreTranslate) GetEngineName() string {
|
||||
return t.EngineName
|
||||
}
|
||||
|
||||
func (t *LibreTranslate) GetAllLang() []string {
|
||||
|
@ -11,6 +11,8 @@ var (
|
||||
)
|
||||
|
||||
type Translator interface {
|
||||
// engine name
|
||||
GetEngineName() string
|
||||
// text
|
||||
GetAllLang() []string
|
||||
GetSrcLang() string
|
||||
@ -32,12 +34,14 @@ type Translator interface {
|
||||
|
||||
func NewGoogleTranslate() *googletranslate.GoogleTranslate {
|
||||
return &googletranslate.GoogleTranslate{
|
||||
SoundLock: lock.NewLock(),
|
||||
EngineName: "GoogleTranslate",
|
||||
SoundLock: lock.NewLock(),
|
||||
}
|
||||
}
|
||||
|
||||
func NewLibreTranslate() *libretranslate.LibreTranslate {
|
||||
return &libretranslate.LibreTranslate{
|
||||
SoundLock: lock.NewLock(),
|
||||
EngineName: "LibreTranslate",
|
||||
SoundLock: lock.NewLock(),
|
||||
}
|
||||
}
|
||||
|
3
main.go
3
main.go
@ -26,9 +26,10 @@ var (
|
||||
dstOutput = tview.NewTextView()
|
||||
defOutput = tview.NewTextArea()
|
||||
posOutput = tview.NewTextArea()
|
||||
translatorDropDown = tview.NewDropDown()
|
||||
srcLangDropDown = tview.NewDropDown()
|
||||
dstLangDropDown = tview.NewDropDown()
|
||||
langCycle = ui.NewUICycle(srcLangDropDown, dstLangDropDown)
|
||||
langCycle = ui.NewUICycle(translatorDropDown, srcLangDropDown, dstLangDropDown)
|
||||
themeDropDown = tview.NewDropDown()
|
||||
transparentDropDown = tview.NewDropDown()
|
||||
hideBelowDropDown = tview.NewDropDown()
|
||||
|
68
ui.go
68
ui.go
@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"gtt/internal/color"
|
||||
"gtt/internal/translate"
|
||||
"strconv"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
@ -73,6 +74,7 @@ func updateBackgroundColor() {
|
||||
|
||||
// dropdown
|
||||
for _, dropdown := range []*tview.DropDown{
|
||||
translatorDropDown,
|
||||
srcLangDropDown,
|
||||
dstLangDropDown,
|
||||
themeDropDown,
|
||||
@ -135,6 +137,7 @@ func updateNonConfigColor() {
|
||||
SetPrefixTextColor(style.PrefixColor())
|
||||
}
|
||||
for _, labelDropDown := range []*tview.DropDown{
|
||||
translatorDropDown,
|
||||
themeDropDown,
|
||||
transparentDropDown,
|
||||
hideBelowDropDown,
|
||||
@ -163,14 +166,30 @@ func updateNonConfigColor() {
|
||||
SetTitleColor(style.HighLightColor())
|
||||
}
|
||||
|
||||
// SetSelectedFunc of DropDown need to update when options change
|
||||
func updateLangDropDown() {
|
||||
srcLangDropDown.SetOptions(translator.GetAllLang(),
|
||||
func(text string, index int) {
|
||||
translator.SetSrcLang(text)
|
||||
srcInput.SetTitle(text)
|
||||
srcLangDropDown.SetTitle(text)
|
||||
})
|
||||
dstLangDropDown.SetOptions(translator.GetAllLang(),
|
||||
func(text string, index int) {
|
||||
translator.SetDstLang(text)
|
||||
dstOutput.SetTitle(text)
|
||||
dstLangDropDown.SetTitle(text)
|
||||
})
|
||||
}
|
||||
|
||||
func updateAllColor() {
|
||||
updateBackgroundColor()
|
||||
updateBorderColor()
|
||||
updateNonConfigColor()
|
||||
}
|
||||
|
||||
// Update title and option
|
||||
func updateTitle() {
|
||||
// Update language title and option
|
||||
func updateCurrentLang() {
|
||||
srcInput.SetTitle(translator.GetSrcLang())
|
||||
dstOutput.SetTitle(translator.GetDstLang())
|
||||
srcLangDropDown.SetCurrentOption(
|
||||
@ -200,10 +219,11 @@ func uiInit() {
|
||||
posOutput.SetBorder(true).SetTitle("Part of speech")
|
||||
|
||||
// dropdown
|
||||
for _, langDropDown := range []*tview.DropDown{srcLangDropDown, dstLangDropDown} {
|
||||
langDropDown.SetOptions(translator.GetAllLang(), nil).
|
||||
SetBorder(true)
|
||||
}
|
||||
translatorDropDown.SetLabel("Translator: ").
|
||||
SetOptions(translate.AllTranslator, nil).
|
||||
SetCurrentOption(IndexOf(translator.GetEngineName(), translate.AllTranslator))
|
||||
srcLangDropDown.SetBorder(true)
|
||||
dstLangDropDown.SetBorder(true)
|
||||
themeDropDown.SetLabel("Theme: ").
|
||||
SetOptions(color.AllTheme, nil).
|
||||
SetCurrentOption(IndexOf(style.Theme, color.AllTheme))
|
||||
@ -251,8 +271,19 @@ func uiInit() {
|
||||
AddItem(nil, 0, 1, false).
|
||||
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
|
||||
AddItem(nil, 0, 1, false).
|
||||
AddItem(srcLangDropDown, langStrMaxLength, 1, true).
|
||||
AddItem(dstLangDropDown, langStrMaxLength, 1, false).
|
||||
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
|
||||
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
|
||||
AddItem(nil, 0, 1, false).
|
||||
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
|
||||
AddItem(translatorDropDown, 0, 1, true),
|
||||
0, 2, true).
|
||||
AddItem(nil, 0, 1, false),
|
||||
1, 1, true).
|
||||
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
|
||||
AddItem(srcLangDropDown, langStrMaxLength, 1, false).
|
||||
AddItem(dstLangDropDown, langStrMaxLength, 1, false),
|
||||
0, 1, false),
|
||||
2*langStrMaxLength, 1, true).
|
||||
AddItem(nil, 0, 1, false),
|
||||
popOutWindowHeight, 1, true).
|
||||
AddItem(attachButton(), 1, 1, false).
|
||||
@ -291,7 +322,8 @@ func uiInit() {
|
||||
AddItem(nil, 0, 1, false)
|
||||
|
||||
updateAllColor()
|
||||
updateTitle()
|
||||
updateLangDropDown()
|
||||
updateCurrentLang()
|
||||
|
||||
// handler
|
||||
mainPage.SetInputCapture(mainPageHandler)
|
||||
@ -318,18 +350,14 @@ func uiInit() {
|
||||
langWindow.SetInputCapture(popOutWindowHandler)
|
||||
styleWindow.SetInputCapture(popOutWindowHandler)
|
||||
keyMapWindow.SetInputCapture(popOutWindowHandler)
|
||||
srcLangDropDown.SetDoneFunc(langDropDownHandler).
|
||||
translatorDropDown.SetDoneFunc(langDropDownHandler).
|
||||
SetSelectedFunc(func(text string, index int) {
|
||||
translator.SetSrcLang(text)
|
||||
srcInput.SetTitle(text)
|
||||
srcLangDropDown.SetTitle(text)
|
||||
})
|
||||
dstLangDropDown.SetDoneFunc(langDropDownHandler).
|
||||
SetSelectedFunc(func(text string, index int) {
|
||||
translator.SetDstLang(text)
|
||||
dstOutput.SetTitle(text)
|
||||
dstLangDropDown.SetTitle(text)
|
||||
translator = translators[text]
|
||||
updateLangDropDown()
|
||||
updateCurrentLang()
|
||||
})
|
||||
srcLangDropDown.SetDoneFunc(langDropDownHandler)
|
||||
dstLangDropDown.SetDoneFunc(langDropDownHandler)
|
||||
themeDropDown.SetDoneFunc(styleDropDownHandler).
|
||||
SetSelectedFunc(func(text string, index int) {
|
||||
style.Theme = text
|
||||
@ -442,7 +470,7 @@ func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
|
||||
}
|
||||
case tcell.KeyCtrlS:
|
||||
translator.SwapLang()
|
||||
updateTitle()
|
||||
updateCurrentLang()
|
||||
srcText := srcInput.GetText()
|
||||
dstText := dstOutput.GetText(false)
|
||||
if len(dstText) > 0 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user