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

finish dropdown handler

This commit is contained in:
eeeXun 2022-10-21 15:01:35 +08:00
parent fa87e0cfe5
commit 33423da3ca
5 changed files with 106 additions and 21 deletions

View File

@ -11,7 +11,7 @@ var (
"Gruvbox": { "Gruvbox": {
"bg": tcell.NewHexColor(0x282828), "bg": tcell.NewHexColor(0x282828),
"fg": tcell.NewHexColor(0xebdbb2), "fg": tcell.NewHexColor(0xebdbb2),
"gray": tcell.NewHexColor(0x928374), "gray": tcell.NewHexColor(0x665c54),
"red": tcell.NewHexColor(0xfb4934), "red": tcell.NewHexColor(0xfb4934),
"green": tcell.NewHexColor(0xfabd2f), "green": tcell.NewHexColor(0xfabd2f),
"yellow": tcell.NewHexColor(0xfabd2f), "yellow": tcell.NewHexColor(0xfabd2f),
@ -38,8 +38,8 @@ type Colors struct {
foreground_color tcell.Color foreground_color tcell.Color
border_color tcell.Color border_color tcell.Color
text_color tcell.Color text_color tcell.Color
title_color tcell.Color
selected_color tcell.Color selected_color tcell.Color
prefix_color tcell.Color
} }
type Window struct { type Window struct {
@ -59,9 +59,9 @@ func (w *Window) color_init() {
} }
w.src.border_color = Themes[theme]["red"] w.src.border_color = Themes[theme]["red"]
w.src.foreground_color = Themes[theme]["fg"] w.src.foreground_color = Themes[theme]["fg"]
w.src.title_color = Themes[theme]["yellow"]
w.src.selected_color = Themes[theme]["gray"] w.src.selected_color = Themes[theme]["gray"]
w.src.prefix_color = Themes[theme]["yellow"]
w.dst.foreground_color = Themes[theme]["fg"] w.dst.foreground_color = Themes[theme]["fg"]
w.dst.border_color = Themes[theme]["blue"] w.dst.border_color = Themes[theme]["blue"]
w.dst.title_color = Themes[theme]["cyan"] w.dst.prefix_color = Themes[theme]["yellow"]
} }

View File

@ -21,7 +21,7 @@ var (
func main() { func main() {
translator.src_lang = "English" translator.src_lang = "English"
translator.dest_lang = "Chinese (Traditional)" translator.dst_lang = "Chinese (Traditional)"
// result, _ := translator.Translate("Hello world\nApple\nbumper") // result, _ := translator.Translate("Hello world\nApple\nbumper")
// fmt.Println(result) // fmt.Println(result)
window.color_init() window.color_init()
@ -39,7 +39,7 @@ func main() {
AddItem(nil, 0, 1, false) AddItem(nil, 0, 1, false)
pages.AddPage("translate_page", translate_page, true, true) pages.AddPage("translate_page", translate_page, true, true)
pages.AddPage("lang_page", lang_page, true, true) pages.AddPage("lang_page", lang_page, true, false)
if err := app.SetRoot(pages, true). if err := app.SetRoot(pages, true).
EnableMouse(true).Run(); err != nil { EnableMouse(true).Run(); err != nil {

View File

@ -19,7 +19,7 @@ const (
type Translator struct { type Translator struct {
src_lang string src_lang string
dest_lang string dst_lang string
} }
func (t Translator) Translate(message string) (string, error) { func (t Translator) Translate(message string) (string, error) {
@ -29,7 +29,7 @@ func (t Translator) Translate(message string) (string, error) {
url_str := fmt.Sprintf( url_str := fmt.Sprintf(
api_url, api_url,
Lang_Code[t.src_lang], Lang_Code[t.src_lang],
Lang_Code[t.dest_lang], Lang_Code[t.dst_lang],
url.QueryEscape(message), url.QueryEscape(message),
) )
res, err := http.Get(url_str) res, err := http.Get(url_str)

99
ui.go
View File

@ -5,10 +5,14 @@ import (
) )
func ui_init() { func ui_init() {
// page
translate_page.SetInputCapture(TranslatePageHandler)
// box
src_box.SetBorder(true). src_box.SetBorder(true).
SetTitle(translator.src_lang). SetTitle(translator.src_lang).
SetBorderColor(window.src.border_color). SetBorderColor(window.src.border_color).
SetTitleColor(window.src.title_color). SetTitleColor(window.src.border_color).
SetBackgroundColor(window.src.background_color) SetBackgroundColor(window.src.background_color)
src_box.SetTextStyle(tcell.StyleDefault. src_box.SetTextStyle(tcell.StyleDefault.
Background(window.src.background_color). Background(window.src.background_color).
@ -16,26 +20,97 @@ func ui_init() {
src_box.SetSelectedStyle(tcell.StyleDefault. src_box.SetSelectedStyle(tcell.StyleDefault.
Background(window.src.selected_color). Background(window.src.selected_color).
Foreground(window.src.foreground_color)) Foreground(window.src.foreground_color))
src_box.SetInputCapture(InputHandle) src_box.SetInputCapture(SrcBoxHandler)
dst_box.SetBorder(true). dst_box.SetBorder(true).
SetTitle(translator.dest_lang). SetTitle(translator.dst_lang).
SetBorderColor(window.dst.border_color). SetBorderColor(window.dst.border_color).
SetTitleColor(window.dst.title_color). SetTitleColor(window.dst.border_color).
SetBackgroundColor(window.dst.background_color) SetBackgroundColor(window.dst.background_color)
dst_box.SetTextColor(window.dst.foreground_color) dst_box.SetTextColor(window.dst.foreground_color)
src_dropdown.SetOptions(Lang, nil) // dropdown
dst_dropdown.SetOptions(Lang, nil) src_dropdown.SetOptions(Lang, nil).
// src_dropdown.SetOptions([]string{"a", "b"}, nil) SetCurrentOption(IndexOf(translator.src_lang, Lang))
// dst_dropdown.SetOptions([]string{"a", "b"}, nil) src_dropdown.SetListStyles(tcell.StyleDefault.
Background(window.src.background_color).
Foreground(window.src.foreground_color),
tcell.StyleDefault.
Background(window.src.selected_color).
Foreground(window.src.prefix_color))
src_dropdown.SetFieldBackgroundColor(window.src.selected_color).
SetFieldTextColor(window.src.foreground_color).
SetPrefixTextColor(window.dst.prefix_color)
src_dropdown.SetBorder(true). src_dropdown.SetBorder(true).
SetTitle("Select an option (hit Enter): ") SetTitle(translator.src_lang).
SetBackgroundColor(window.src.background_color).
SetBorderColor(window.src.border_color).
SetTitleColor(window.src.border_color)
src_dropdown.SetSelectedFunc(SrcSelected)
src_dropdown.SetDoneFunc(SrcDropDownHandler)
dst_dropdown.SetOptions(Lang, nil).
SetCurrentOption(IndexOf(translator.dst_lang, Lang))
dst_dropdown.SetListStyles(tcell.StyleDefault.
Background(window.src.background_color).
Foreground(window.src.foreground_color),
tcell.StyleDefault.
Background(window.src.selected_color).
Foreground(window.src.prefix_color))
dst_dropdown.SetFieldBackgroundColor(window.src.selected_color).
SetFieldTextColor(window.src.foreground_color).
SetPrefixTextColor(window.dst.prefix_color)
dst_dropdown.SetBorder(true). dst_dropdown.SetBorder(true).
SetTitle("Select an option (hit Enter): ") SetTitle(translator.dst_lang).
SetBackgroundColor(window.dst.background_color).
SetBorderColor(window.dst.border_color).
SetTitleColor(window.dst.border_color)
dst_dropdown.SetSelectedFunc(DstSelected)
dst_dropdown.SetDoneFunc(DstDropDownHandler)
} }
func InputHandle(event *tcell.EventKey) *tcell.EventKey { func TranslatePageHandler(event *tcell.EventKey) *tcell.EventKey {
key := event.Key()
switch key {
case tcell.KeyEsc:
pages.ShowPage("lang_page")
}
return event
}
func SrcSelected(text string, index int) {
translator.src_lang = text
src_box.SetTitle(text)
src_dropdown.SetTitle(text)
}
func DstSelected(text string, index int) {
translator.dst_lang = text
dst_box.SetTitle(text)
dst_dropdown.SetTitle(text)
}
func SrcDropDownHandler(key tcell.Key) {
switch key {
case tcell.KeyTAB:
app.SetFocus(dst_dropdown)
case tcell.KeyEsc:
pages.HidePage("lang_page")
}
}
func DstDropDownHandler(key tcell.Key) {
switch key {
case tcell.KeyTAB:
app.SetFocus(src_dropdown)
case tcell.KeyEsc:
pages.HidePage("lang_page")
}
}
func SrcBoxHandler(event *tcell.EventKey) *tcell.EventKey {
key := event.Key() key := event.Key()
// panic(event.Name()) // panic(event.Name())
@ -52,7 +127,7 @@ func InputHandle(event *tcell.EventKey) *tcell.EventKey {
case tcell.KeyCtrlN: case tcell.KeyCtrlN:
translator.PlaySound(translator.src_lang, src_box.GetText()) translator.PlaySound(translator.src_lang, src_box.GetText())
case tcell.KeyCtrlP: case tcell.KeyCtrlP:
translator.PlaySound(translator.dest_lang, dst_box.GetText(false)) translator.PlaySound(translator.dst_lang, dst_box.GetText(false))
case tcell.KeyCtrlT: case tcell.KeyCtrlT:
dst_box.SetText("TTT") dst_box.SetText("TTT")
case tcell.KeyCtrlS: case tcell.KeyCtrlS:

10
utils.go Normal file
View File

@ -0,0 +1,10 @@
package main
func IndexOf(candidate string, arr []string) int {
for index, element := range arr {
if element == candidate {
return index
}
}
return -1
}