From 4bf68d1763f130e30cef143b6be382389e0cf286 Mon Sep 17 00:00:00 2001 From: eeeXun Date: Mon, 24 Oct 2022 12:48:23 +0800 Subject: [PATCH] use UICycle to cycle through layout --- main.go | 2 ++ ui.go | 76 ++++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/main.go b/main.go index 4cedbbf..a617b9b 100644 --- a/main.go +++ b/main.go @@ -14,8 +14,10 @@ var ( dstBox = tview.NewTextView() srcLangDropDown = tview.NewDropDown() dstLangDropDown = tview.NewDropDown() + langCycle = NewUICycle(srcLangDropDown, dstLangDropDown) themeDropDown = tview.NewDropDown() transparentDropDown = tview.NewDropDown() + styleCycle = NewUICycle(themeDropDown, transparentDropDown) langButton = tview.NewButton("(1)Language") styleButton = tview.NewButton("(2)Style") menuButton = tview.NewButton("(3)KeyMap") diff --git a/ui.go b/ui.go index 9593d79..55107e0 100644 --- a/ui.go +++ b/ui.go @@ -6,6 +6,34 @@ import ( "strconv" ) +type UICycle struct { + widget []tview.Primitive + index int + len int +} + +func NewUICycle(widgets ...tview.Primitive) *UICycle { + var w []tview.Primitive + + for _, widget := range widgets { + w = append(w, widget) + } + + return &UICycle{ + widget: w, + index: 0, + len: len(w), + } +} + +func (ui *UICycle) Increase() { + ui.index = (ui.index + 1) % ui.len +} + +func (ui *UICycle) GetCurrentUI() tview.Primitive { + return ui.widget[ui.index] +} + func updateBackgroundColor() { // box srcBox.SetBackgroundColor(window.src.backgroundColor) @@ -175,12 +203,14 @@ func uiInit() { langWindow.SetInputCapture(langWindowHandler) styleWindow.SetInputCapture(styleWindowHandler) translateWindow.SetInputCapture(translatePageHandler) - srcLangDropDown.SetDoneFunc(srcDropDownHandler). + srcLangDropDown.SetDoneFunc(langDropDownHandler). SetSelectedFunc(srcLangSelected) - dstLangDropDown.SetDoneFunc(dstDropDownHandler). + dstLangDropDown.SetDoneFunc(langDropDownHandler). SetSelectedFunc(dstLangSelected) - themeDropDown.SetSelectedFunc(themeSelected) - transparentDropDown.SetSelectedFunc(transparentSelected) + themeDropDown.SetDoneFunc(styleDropDownHandler). + SetSelectedFunc(themeSelected) + transparentDropDown.SetDoneFunc(styleDropDownHandler). + SetSelectedFunc(transparentSelected) langButton.SetSelectedFunc(func() { mainPage.HidePage("stylePage") mainPage.ShowPage("langPage") @@ -301,6 +331,16 @@ func translatePageHandler(event *tcell.EventKey) *tcell.EventKey { return event } +func langDropDownHandler(key tcell.Key) { + switch key { + case tcell.KeyTAB: + langCycle.Increase() + app.SetFocus(langCycle.GetCurrentUI()) + case tcell.KeyEsc: + mainPage.HidePage("langPage") + } +} + func srcLangSelected(text string, index int) { translator.srcLang = text srcBox.SetTitle(text) @@ -313,6 +353,16 @@ func dstLangSelected(text string, index int) { dstLangDropDown.SetTitle(text) } +func styleDropDownHandler(key tcell.Key) { + switch key { + case tcell.KeyTAB: + styleCycle.Increase() + app.SetFocus(styleCycle.GetCurrentUI()) + case tcell.KeyEsc: + mainPage.HidePage("stylePage") + } +} + func themeSelected(text string, index int) { theme = text window.colorInit() @@ -330,21 +380,3 @@ func transparentSelected(text string, index int) { } updateBackgroundColor() } - -func srcDropDownHandler(key tcell.Key) { - switch key { - case tcell.KeyTAB: - app.SetFocus(dstLangDropDown) - case tcell.KeyEsc: - mainPage.HidePage("langPage") - } -} - -func dstDropDownHandler(key tcell.Key) { - switch key { - case tcell.KeyTAB: - app.SetFocus(srcLangDropDown) - case tcell.KeyEsc: - mainPage.HidePage("langPage") - } -}