mirror of
https://github.com/eeeXun/GTT.git
synced 2025-05-16 07:40:44 -07:00
feat: support custom key mapping (#22)
* feat: support custom key mapping * remove testing code * feat: support function key * style: add comment * feat: support read config * docs: add keymap.yaml example * docs: add function key in README * refactor: rename config key name * feat: support Alt prefix * docs: fix syntax * feat: let exit be configurable * refactor: remove KeyEsc in keyNames * refactor: space to Space * refactor: style code
This commit is contained in:
parent
7be328cf8d
commit
d01ef4e768
31
README.md
31
README.md
@ -78,7 +78,7 @@ git clone https://github.com/eeeXun/gtt.git && cd gtt && go build -ldflags="-s -
|
|||||||
docker run -it eeexun/gtt:latest
|
docker run -it eeexun/gtt:latest
|
||||||
```
|
```
|
||||||
|
|
||||||
## Create a theme
|
## Customize theme
|
||||||
|
|
||||||
You can create a theme with theme name. And you must provide the color of `bg`, `fg`, `gray`, `red`, `green`, `yellow`, `blue`, `purple`, `cyan`, `orange`.
|
You can create a theme with theme name. And you must provide the color of `bg`, `fg`, `gray`, `red`, `green`, `yellow`, `blue`, `purple`, `cyan`, `orange`.
|
||||||
|
|
||||||
@ -91,7 +91,7 @@ And note that:
|
|||||||
- `orange` is for KeyMap menu color
|
- `orange` is for KeyMap menu color
|
||||||
- `purple` is for button pressed color
|
- `purple` is for button pressed color
|
||||||
|
|
||||||
See the example in [theme.yaml](example/theme.yaml) file. This file should located at `$XDG_CONFIG_HOME/gtt/theme.yaml` or `$HOME/.config/gtt/theme.yaml`
|
See the example in [theme.yaml](example/theme.yaml) file. This file should be located at `$XDG_CONFIG_HOME/gtt/theme.yaml` or `$HOME/.config/gtt/theme.yaml`.
|
||||||
|
|
||||||
## Language in argument
|
## Language in argument
|
||||||
|
|
||||||
@ -113,6 +113,8 @@ See available languages on:
|
|||||||
|
|
||||||
## Key Map
|
## Key Map
|
||||||
|
|
||||||
|
### Default key map
|
||||||
|
|
||||||
`<C-c>`
|
`<C-c>`
|
||||||
Exit program.
|
Exit program.
|
||||||
|
|
||||||
@ -158,6 +160,31 @@ Cycle through the pop out widget.
|
|||||||
`<1>`, `<2>`, `<3>`
|
`<1>`, `<2>`, `<3>`
|
||||||
Switch pop out window.
|
Switch pop out window.
|
||||||
|
|
||||||
|
### Customize key map
|
||||||
|
|
||||||
|
You can overwrite the following key
|
||||||
|
|
||||||
|
- `exit`: Exit program.
|
||||||
|
- `translate`: Translate from source to destination window.
|
||||||
|
- `swap_language`: Swap language.
|
||||||
|
- `clear`: Clear all text in source of translation window.
|
||||||
|
- `copy_selected`: Copy selected text.
|
||||||
|
- `copy_source`: Copy all text in source of translation window.
|
||||||
|
- `copy_destination`: Copy all text in destination of translation window.
|
||||||
|
- `tts_source`: Play text to speech on source of translation window.
|
||||||
|
- `tts_destination`: Play text to speech on destination of translation window.
|
||||||
|
- `stop_tts`: Stop playing text to speech.
|
||||||
|
- `toggle_transparent`: Toggle transparent.
|
||||||
|
- `toggle_below`: Toggle Definition/Example & Part of speech.
|
||||||
|
|
||||||
|
For key to combine with `Ctrl`, the value can be `"C-Space"`, `"C-\\"`, `"C-]"`, `"C-^"`, `"C-_"` or `"C-a"` to `"C-z"`.
|
||||||
|
|
||||||
|
For key to combine with `Alt`, the value can be `"A-Space"` or `"A-"` + the character you want.
|
||||||
|
|
||||||
|
Or you can use function key, the value can be `"F1"` to `"F64"`.
|
||||||
|
|
||||||
|
See the example in [keymap.yaml](example/keymap.yaml) file. This file should be located at `$XDG_CONFIG_HOME/gtt/keymap.yaml` or `$HOME/.config/gtt/keymap.yaml`.
|
||||||
|
|
||||||
## Credit
|
## Credit
|
||||||
|
|
||||||
[soimort/translate-shell](https://github.com/soimort/translate-shell),
|
[soimort/translate-shell](https://github.com/soimort/translate-shell),
|
||||||
|
39
config.go
39
config.go
@ -14,7 +14,22 @@ func configInit() {
|
|||||||
var (
|
var (
|
||||||
defaultConfigPath string
|
defaultConfigPath string
|
||||||
themeConfig = config.New()
|
themeConfig = config.New()
|
||||||
defaultConfig = map[string]interface{}{
|
keyMapConfig = config.New()
|
||||||
|
defaultKeyMaps = map[string]string{
|
||||||
|
"exit": "C-c",
|
||||||
|
"translate": "C-j",
|
||||||
|
"swap_language": "C-s",
|
||||||
|
"clear": "C-q",
|
||||||
|
"copy_selected": "C-y",
|
||||||
|
"copy_source": "C-g",
|
||||||
|
"copy_destination": "C-r",
|
||||||
|
"tts_source": "C-o",
|
||||||
|
"tts_destination": "C-p",
|
||||||
|
"stop_tts": "C-x",
|
||||||
|
"toggle_transparent": "C-t",
|
||||||
|
"toggle_below": "C-\\",
|
||||||
|
}
|
||||||
|
defaultConfig = map[string]interface{}{
|
||||||
"hide_below": false,
|
"hide_below": false,
|
||||||
"transparent": false,
|
"transparent": false,
|
||||||
"theme": "gruvbox",
|
"theme": "gruvbox",
|
||||||
@ -42,17 +57,21 @@ func configInit() {
|
|||||||
config.SetConfigType("yaml")
|
config.SetConfigType("yaml")
|
||||||
themeConfig.SetConfigName("theme")
|
themeConfig.SetConfigName("theme")
|
||||||
themeConfig.SetConfigType("yaml")
|
themeConfig.SetConfigType("yaml")
|
||||||
|
keyMapConfig.SetConfigName("keymap")
|
||||||
|
themeConfig.SetConfigType("yaml")
|
||||||
if len(os.Getenv("XDG_CONFIG_HOME")) > 0 {
|
if len(os.Getenv("XDG_CONFIG_HOME")) > 0 {
|
||||||
defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt"
|
defaultConfigPath = os.Getenv("XDG_CONFIG_HOME") + "/gtt"
|
||||||
config.AddConfigPath(defaultConfigPath)
|
config.AddConfigPath(defaultConfigPath)
|
||||||
themeConfig.AddConfigPath(defaultConfigPath)
|
themeConfig.AddConfigPath(defaultConfigPath)
|
||||||
|
keyMapConfig.AddConfigPath(defaultConfigPath)
|
||||||
} else {
|
} else {
|
||||||
defaultConfigPath = os.Getenv("HOME") + "/.config/gtt"
|
defaultConfigPath = os.Getenv("HOME") + "/.config/gtt"
|
||||||
}
|
}
|
||||||
config.AddConfigPath("$HOME/.config/gtt")
|
config.AddConfigPath("$HOME/.config/gtt")
|
||||||
themeConfig.AddConfigPath("$HOME/.config/gtt")
|
themeConfig.AddConfigPath("$HOME/.config/gtt")
|
||||||
|
keyMapConfig.AddConfigPath("$HOME/.config/gtt")
|
||||||
|
|
||||||
// import theme if file exists
|
// Import theme if file exists
|
||||||
if err := themeConfig.ReadInConfig(); err == nil {
|
if err := themeConfig.ReadInConfig(); err == nil {
|
||||||
var (
|
var (
|
||||||
palate = make(map[string]int32)
|
palate = make(map[string]int32)
|
||||||
@ -98,6 +117,22 @@ func configInit() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Setup key map
|
||||||
|
// If keymap file exist and action in file exist, then set the keyMap
|
||||||
|
// Otherwise, set to defaultKeyMap
|
||||||
|
if err := keyMapConfig.ReadInConfig(); err == nil {
|
||||||
|
for action, key := range defaultKeyMaps {
|
||||||
|
if keyMapConfig.Get(action) == nil {
|
||||||
|
keyMaps[action] = key
|
||||||
|
} else {
|
||||||
|
keyMaps[action] = keyMapConfig.GetString(action)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for action, key := range defaultKeyMaps {
|
||||||
|
keyMaps[action] = key
|
||||||
|
}
|
||||||
|
}
|
||||||
// Setup
|
// Setup
|
||||||
for _, name := range translate.AllTranslator {
|
for _, name := range translate.AllTranslator {
|
||||||
translators[name] = translate.NewTranslator(name)
|
translators[name] = translate.NewTranslator(name)
|
||||||
|
35
example/keymap.yaml
Normal file
35
example/keymap.yaml
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
# This file should be located at $XDG_CONFIG_HOME/gtt/keymap.yaml or $HOME/.config/gtt/keymap.yaml.
|
||||||
|
|
||||||
|
# For key to combine with Ctrl, the value can be "C-Space", "C-\\", "C-]", "C-^", "C-_" or "C-a" to "C-z".
|
||||||
|
|
||||||
|
# For key to combine with Alt, the value can be "A-Space" or "A-" + the character you want.
|
||||||
|
|
||||||
|
# Or you can use function key, the value can be "F1" to "F64".
|
||||||
|
|
||||||
|
|
||||||
|
# The following is the default key map
|
||||||
|
|
||||||
|
# Exit program, <C-c>
|
||||||
|
exit: "C-c"
|
||||||
|
# Translate from source to destination window, <C-j>
|
||||||
|
translate: "C-j"
|
||||||
|
# Swap language, <C-s>
|
||||||
|
swap_language: "C-s"
|
||||||
|
# Clear all text in source of translation window, <C-q>
|
||||||
|
clear: "C-q"
|
||||||
|
# Copy selected text, <C-y>
|
||||||
|
copy_selected: "C-y"
|
||||||
|
# Copy all text in source of translation window, <C-g>
|
||||||
|
copy_source: "C-g"
|
||||||
|
# Copy all text in destination of translation window, <C-r>
|
||||||
|
copy_destination: "C-r"
|
||||||
|
# Play text to speech on source of translation window, <C-o>
|
||||||
|
tts_source: "C-o"
|
||||||
|
# Play text to speech on destination of translation window, <C-p>
|
||||||
|
tts_destination: "C-p"
|
||||||
|
# Stop playing text to speech, <C-x>
|
||||||
|
stop_tts: "C-x"
|
||||||
|
# Toggle transparent, <C-t>
|
||||||
|
toggle_transparent: "C-t"
|
||||||
|
# Toggle Definition/Example & Part of speech, <C-\>
|
||||||
|
toggle_below: "C-\\"
|
@ -1,6 +1,6 @@
|
|||||||
# This file should located at $XDG_CONFIG_HOME/gtt/theme.yaml or $HOME/.config/gtt/theme.yaml
|
# This file should be located at $XDG_CONFIG_HOME/gtt/theme.yaml or $HOME/.config/gtt/theme.yaml.
|
||||||
# You have to provide theme name. e.g. tokyonight
|
# You have to provide theme name. e.g. tokyonight.
|
||||||
# And colors: bg, fg, gray, red, green, yellow, blue, purple, cyan, orange
|
# And colors: bg, fg, gray, red, green, yellow, blue, purple, cyan, orange.
|
||||||
# bg is for background color
|
# bg is for background color
|
||||||
# fg is for foreground color
|
# fg is for foreground color
|
||||||
# gray is for selected color
|
# gray is for selected color
|
||||||
|
121
key.go
Normal file
121
key.go
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gdamore/tcell/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var keyNames = map[tcell.Key]string{
|
||||||
|
tcell.KeyF1: "F1",
|
||||||
|
tcell.KeyF2: "F2",
|
||||||
|
tcell.KeyF3: "F3",
|
||||||
|
tcell.KeyF4: "F4",
|
||||||
|
tcell.KeyF5: "F5",
|
||||||
|
tcell.KeyF6: "F6",
|
||||||
|
tcell.KeyF7: "F7",
|
||||||
|
tcell.KeyF8: "F8",
|
||||||
|
tcell.KeyF9: "F9",
|
||||||
|
tcell.KeyF10: "F10",
|
||||||
|
tcell.KeyF11: "F11",
|
||||||
|
tcell.KeyF12: "F12",
|
||||||
|
tcell.KeyF13: "F13",
|
||||||
|
tcell.KeyF14: "F14",
|
||||||
|
tcell.KeyF15: "F15",
|
||||||
|
tcell.KeyF16: "F16",
|
||||||
|
tcell.KeyF17: "F17",
|
||||||
|
tcell.KeyF18: "F18",
|
||||||
|
tcell.KeyF19: "F19",
|
||||||
|
tcell.KeyF20: "F20",
|
||||||
|
tcell.KeyF21: "F21",
|
||||||
|
tcell.KeyF22: "F22",
|
||||||
|
tcell.KeyF23: "F23",
|
||||||
|
tcell.KeyF24: "F24",
|
||||||
|
tcell.KeyF25: "F25",
|
||||||
|
tcell.KeyF26: "F26",
|
||||||
|
tcell.KeyF27: "F27",
|
||||||
|
tcell.KeyF28: "F28",
|
||||||
|
tcell.KeyF29: "F29",
|
||||||
|
tcell.KeyF30: "F30",
|
||||||
|
tcell.KeyF31: "F31",
|
||||||
|
tcell.KeyF32: "F32",
|
||||||
|
tcell.KeyF33: "F33",
|
||||||
|
tcell.KeyF34: "F34",
|
||||||
|
tcell.KeyF35: "F35",
|
||||||
|
tcell.KeyF36: "F36",
|
||||||
|
tcell.KeyF37: "F37",
|
||||||
|
tcell.KeyF38: "F38",
|
||||||
|
tcell.KeyF39: "F39",
|
||||||
|
tcell.KeyF40: "F40",
|
||||||
|
tcell.KeyF41: "F41",
|
||||||
|
tcell.KeyF42: "F42",
|
||||||
|
tcell.KeyF43: "F43",
|
||||||
|
tcell.KeyF44: "F44",
|
||||||
|
tcell.KeyF45: "F45",
|
||||||
|
tcell.KeyF46: "F46",
|
||||||
|
tcell.KeyF47: "F47",
|
||||||
|
tcell.KeyF48: "F48",
|
||||||
|
tcell.KeyF49: "F49",
|
||||||
|
tcell.KeyF50: "F50",
|
||||||
|
tcell.KeyF51: "F51",
|
||||||
|
tcell.KeyF52: "F52",
|
||||||
|
tcell.KeyF53: "F53",
|
||||||
|
tcell.KeyF54: "F54",
|
||||||
|
tcell.KeyF55: "F55",
|
||||||
|
tcell.KeyF56: "F56",
|
||||||
|
tcell.KeyF57: "F57",
|
||||||
|
tcell.KeyF58: "F58",
|
||||||
|
tcell.KeyF59: "F59",
|
||||||
|
tcell.KeyF60: "F60",
|
||||||
|
tcell.KeyF61: "F61",
|
||||||
|
tcell.KeyF62: "F62",
|
||||||
|
tcell.KeyF63: "F63",
|
||||||
|
tcell.KeyF64: "F64",
|
||||||
|
tcell.KeyCtrlA: "C-a",
|
||||||
|
tcell.KeyCtrlB: "C-b",
|
||||||
|
tcell.KeyCtrlC: "C-c",
|
||||||
|
tcell.KeyCtrlD: "C-d",
|
||||||
|
tcell.KeyCtrlE: "C-e",
|
||||||
|
tcell.KeyCtrlF: "C-f",
|
||||||
|
tcell.KeyCtrlG: "C-g",
|
||||||
|
tcell.KeyCtrlJ: "C-j",
|
||||||
|
tcell.KeyCtrlK: "C-k",
|
||||||
|
tcell.KeyCtrlL: "C-l",
|
||||||
|
tcell.KeyCtrlN: "C-n",
|
||||||
|
tcell.KeyCtrlO: "C-o",
|
||||||
|
tcell.KeyCtrlP: "C-p",
|
||||||
|
tcell.KeyCtrlQ: "C-q",
|
||||||
|
tcell.KeyCtrlR: "C-r",
|
||||||
|
tcell.KeyCtrlS: "C-s",
|
||||||
|
tcell.KeyCtrlT: "C-t",
|
||||||
|
tcell.KeyCtrlU: "C-u",
|
||||||
|
tcell.KeyCtrlV: "C-v",
|
||||||
|
tcell.KeyCtrlW: "C-w",
|
||||||
|
tcell.KeyCtrlX: "C-x",
|
||||||
|
tcell.KeyCtrlY: "C-y",
|
||||||
|
tcell.KeyCtrlZ: "C-z",
|
||||||
|
tcell.KeyCtrlSpace: "C-Space",
|
||||||
|
tcell.KeyCtrlUnderscore: "C-_",
|
||||||
|
tcell.KeyCtrlRightSq: "C-]",
|
||||||
|
tcell.KeyCtrlBackslash: "C-\\",
|
||||||
|
tcell.KeyCtrlCarat: "C-^",
|
||||||
|
}
|
||||||
|
|
||||||
|
func getKeyName(event *tcell.EventKey) string {
|
||||||
|
var (
|
||||||
|
keyName string
|
||||||
|
key = event.Key()
|
||||||
|
)
|
||||||
|
|
||||||
|
if key == tcell.KeyRune {
|
||||||
|
if event.Modifiers() == tcell.ModAlt {
|
||||||
|
if event.Rune() == ' ' {
|
||||||
|
keyName = "A-Space"
|
||||||
|
} else {
|
||||||
|
keyName = "A-" + string(event.Rune())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
keyName = keyNames[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
return keyName
|
||||||
|
}
|
2
main.go
2
main.go
@ -20,6 +20,8 @@ var (
|
|||||||
translators = make(map[string]translate.Translator, len(translate.AllTranslator))
|
translators = make(map[string]translate.Translator, len(translate.AllTranslator))
|
||||||
// UI style
|
// UI style
|
||||||
uiStyle = style.NewStyle()
|
uiStyle = style.NewStyle()
|
||||||
|
// keyMaps
|
||||||
|
keyMaps = make(map[string]string)
|
||||||
// UI
|
// UI
|
||||||
app = tview.NewApplication()
|
app = tview.NewApplication()
|
||||||
srcInput = tview.NewTextArea()
|
srcInput = tview.NewTextArea()
|
||||||
|
124
ui.go
124
ui.go
@ -10,6 +10,13 @@ import (
|
|||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Item struct {
|
||||||
|
item tview.Primitive
|
||||||
|
fixedSize int
|
||||||
|
proportion int
|
||||||
|
focus bool
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
popOutWindowHeight int = 20
|
popOutWindowHeight int = 20
|
||||||
langStrMaxLength int = 32
|
langStrMaxLength int = 32
|
||||||
@ -17,27 +24,27 @@ const (
|
|||||||
Exit program.
|
Exit program.
|
||||||
[#%[1]s]<Esc>[-]
|
[#%[1]s]<Esc>[-]
|
||||||
Toggle pop out window.
|
Toggle pop out window.
|
||||||
[#%[1]s]<C-j>[-]
|
[#%[1]s]<%[2]s>[-]
|
||||||
Translate from source to destination window.
|
Translate from source to destination window.
|
||||||
[#%[1]s]<C-s>[-]
|
[#%[1]s]<%[3]s>[-]
|
||||||
Swap language.
|
Swap language.
|
||||||
[#%[1]s]<C-q>[-]
|
[#%[1]s]<%[4]s>[-]
|
||||||
Clear all text in source of translation window.
|
Clear all text in source of translation window.
|
||||||
[#%[1]s]<C-y>[-]
|
[#%[1]s]<%[5]s>[-]
|
||||||
Copy selected text.
|
Copy selected text.
|
||||||
[#%[1]s]<C-g>[-]
|
[#%[1]s]<%[6]s>[-]
|
||||||
Copy all text in source of translation window.
|
Copy all text in source of translation window.
|
||||||
[#%[1]s]<C-r>[-]
|
[#%[1]s]<%[7]s>[-]
|
||||||
Copy all text in destination of translation window.
|
Copy all text in destination of translation window.
|
||||||
[#%[1]s]<C-o>[-]
|
[#%[1]s]<%[8]s>[-]
|
||||||
Play text to speech on source of translation window.
|
Play text to speech on source of translation window.
|
||||||
[#%[1]s]<C-p>[-]
|
[#%[1]s]<%[9]s>[-]
|
||||||
Play text to speech on destination of translation window.
|
Play text to speech on destination of translation window.
|
||||||
[#%[1]s]<C-x>[-]
|
[#%[1]s]<%[10]s>[-]
|
||||||
Stop playing text to speech.
|
Stop playing text to speech.
|
||||||
[#%[1]s]<C-t>[-]
|
[#%[1]s]<%[11]s>[-]
|
||||||
Toggle transparent.
|
Toggle transparent.
|
||||||
[#%[1]s]<C-\>[-]
|
[#%[1]s]<%[12]s>[-]
|
||||||
Toggle Definition/Example & Part of speech.
|
Toggle Definition/Example & Part of speech.
|
||||||
[#%[1]s]<Tab>, <S-Tab>[-]
|
[#%[1]s]<Tab>, <S-Tab>[-]
|
||||||
Cycle through the pop out widget.
|
Cycle through the pop out widget.
|
||||||
@ -45,13 +52,6 @@ const (
|
|||||||
Switch pop out window.`
|
Switch pop out window.`
|
||||||
)
|
)
|
||||||
|
|
||||||
type Item struct {
|
|
||||||
item tview.Primitive
|
|
||||||
fixedSize int
|
|
||||||
proportion int
|
|
||||||
focus bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func updateTranslateWindow() {
|
func updateTranslateWindow() {
|
||||||
if uiStyle.HideBelow {
|
if uiStyle.HideBelow {
|
||||||
translateWindow.RemoveItem(translateBelowWidget)
|
translateWindow.RemoveItem(translateBelowWidget)
|
||||||
@ -167,8 +167,19 @@ func updateNonConfigColor() {
|
|||||||
// key map
|
// key map
|
||||||
keyMapMenu.SetTextColor(uiStyle.ForegroundColor()).
|
keyMapMenu.SetTextColor(uiStyle.ForegroundColor()).
|
||||||
SetText(fmt.Sprintf(keyMapText,
|
SetText(fmt.Sprintf(keyMapText,
|
||||||
fmt.Sprintf("%.6x",
|
fmt.Sprintf("%.6x", uiStyle.HighLightColor().TrueColor().Hex()),
|
||||||
uiStyle.HighLightColor().TrueColor().Hex()))).
|
keyMaps["translate"],
|
||||||
|
keyMaps["swap_language"],
|
||||||
|
keyMaps["clear"],
|
||||||
|
keyMaps["copy_selected"],
|
||||||
|
keyMaps["copy_source"],
|
||||||
|
keyMaps["copy_destination"],
|
||||||
|
keyMaps["tts_source"],
|
||||||
|
keyMaps["tts_destination"],
|
||||||
|
keyMaps["stop_tts"],
|
||||||
|
keyMaps["toggle_transparent"],
|
||||||
|
keyMaps["toggle_below"],
|
||||||
|
)).
|
||||||
SetBorderColor(uiStyle.HighLightColor()).
|
SetBorderColor(uiStyle.HighLightColor()).
|
||||||
SetTitleColor(uiStyle.HighLightColor())
|
SetTitleColor(uiStyle.HighLightColor())
|
||||||
}
|
}
|
||||||
@ -358,16 +369,21 @@ func uiInit() {
|
|||||||
updateCurrentLang()
|
updateCurrentLang()
|
||||||
|
|
||||||
// handler
|
// handler
|
||||||
mainPage.SetInputCapture(mainPageHandler)
|
app.SetInputCapture(appHandler)
|
||||||
translateWindow.SetInputCapture(translateWindowHandler)
|
translateWindow.SetInputCapture(translateWindowHandler)
|
||||||
for _, widget := range []*tview.TextArea{srcInput, defOutput, posOutput} {
|
for _, widget := range []*tview.TextArea{srcInput, defOutput, posOutput} {
|
||||||
// fix for loop problem
|
// fix for loop problem
|
||||||
// https://github.com/golang/go/discussions/56010
|
// https://github.com/golang/go/discussions/56010
|
||||||
widget := widget
|
widget := widget
|
||||||
widget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
widget.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
||||||
key := event.Key()
|
keyName := getKeyName(event)
|
||||||
switch key {
|
|
||||||
case tcell.KeyCtrlY:
|
if len(keyName) == 0 {
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
switch keyName {
|
||||||
|
case keyMaps["copy_selected"]:
|
||||||
// copy selected text
|
// copy selected text
|
||||||
text, _, _ := widget.GetSelection()
|
text, _, _ := widget.GetSelection()
|
||||||
|
|
||||||
@ -375,6 +391,7 @@ func uiInit() {
|
|||||||
if len(text) > 0 {
|
if len(text) > 0 {
|
||||||
CopyToClipboard(text)
|
CopyToClipboard(text)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return event
|
return event
|
||||||
})
|
})
|
||||||
@ -427,37 +444,54 @@ func uiInit() {
|
|||||||
keyMapButton.SetSelectedFunc(showKeyMapPopout)
|
keyMapButton.SetSelectedFunc(showKeyMapPopout)
|
||||||
}
|
}
|
||||||
|
|
||||||
func mainPageHandler(event *tcell.EventKey) *tcell.EventKey {
|
func appHandler(event *tcell.EventKey) *tcell.EventKey {
|
||||||
key := event.Key()
|
keyName := getKeyName(event)
|
||||||
|
|
||||||
switch key {
|
if len(keyName) == 0 {
|
||||||
case tcell.KeyCtrlT:
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
switch keyName {
|
||||||
|
case keyMaps["exit"]:
|
||||||
|
app.Stop()
|
||||||
|
return nil
|
||||||
|
case keyMaps["toggle_transparent"]:
|
||||||
// Toggle transparent
|
// Toggle transparent
|
||||||
uiStyle.Transparent = !uiStyle.Transparent
|
uiStyle.Transparent = !uiStyle.Transparent
|
||||||
// The following will trigger transparentDropDown SetDoneFunc
|
// The following will trigger transparentDropDown SetDoneFunc
|
||||||
transparentDropDown.SetCurrentOption(
|
transparentDropDown.SetCurrentOption(
|
||||||
IndexOf(strconv.FormatBool(uiStyle.Transparent),
|
IndexOf(strconv.FormatBool(uiStyle.Transparent),
|
||||||
[]string{"true", "false"}))
|
[]string{"true", "false"}))
|
||||||
case tcell.KeyCtrlBackslash:
|
return nil
|
||||||
|
case keyMaps["toggle_below"]:
|
||||||
// Toggle Hide below window
|
// Toggle Hide below window
|
||||||
uiStyle.HideBelow = !uiStyle.HideBelow
|
uiStyle.HideBelow = !uiStyle.HideBelow
|
||||||
// The following will trigger hideBelowDropDown SetDoneFunc
|
// The following will trigger hideBelowDropDown SetDoneFunc
|
||||||
hideBelowDropDown.SetCurrentOption(
|
hideBelowDropDown.SetCurrentOption(
|
||||||
IndexOf(strconv.FormatBool(uiStyle.HideBelow),
|
IndexOf(strconv.FormatBool(uiStyle.HideBelow),
|
||||||
[]string{"true", "false"}))
|
[]string{"true", "false"}))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Force C-c not to exit program
|
||||||
|
if event.Key() == tcell.KeyCtrlC {
|
||||||
|
return tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone)
|
||||||
}
|
}
|
||||||
|
|
||||||
return event
|
return event
|
||||||
}
|
}
|
||||||
|
|
||||||
func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
|
func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
|
||||||
key := event.Key()
|
if event.Key() == tcell.KeyEsc {
|
||||||
|
|
||||||
switch key {
|
|
||||||
case tcell.KeyEsc:
|
|
||||||
mainPage.ShowPage("langPopOut")
|
mainPage.ShowPage("langPopOut")
|
||||||
app.SetFocus(langCycle.GetCurrentUI())
|
app.SetFocus(langCycle.GetCurrentUI())
|
||||||
case tcell.KeyCtrlJ:
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
keyName := getKeyName(event)
|
||||||
|
|
||||||
|
switch keyName {
|
||||||
|
case keyMaps["translate"]:
|
||||||
message := srcInput.GetText()
|
message := srcInput.GetText()
|
||||||
// Only translate when message exist
|
// Only translate when message exist
|
||||||
if len(message) > 0 {
|
if len(message) > 0 {
|
||||||
@ -470,9 +504,11 @@ func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
posOutput.SetText(translation.POS, false)
|
posOutput.SetText(translation.POS, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case tcell.KeyCtrlQ:
|
return nil
|
||||||
|
case keyMaps["clear"]:
|
||||||
srcInput.SetText("", true)
|
srcInput.SetText("", true)
|
||||||
case tcell.KeyCtrlG:
|
return nil
|
||||||
|
case keyMaps["copy_source"]:
|
||||||
// copy all text in Input
|
// copy all text in Input
|
||||||
text := srcInput.GetText()
|
text := srcInput.GetText()
|
||||||
|
|
||||||
@ -480,7 +516,8 @@ func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
if len(text) > 0 {
|
if len(text) > 0 {
|
||||||
CopyToClipboard(text)
|
CopyToClipboard(text)
|
||||||
}
|
}
|
||||||
case tcell.KeyCtrlR:
|
return nil
|
||||||
|
case keyMaps["copy_destination"]:
|
||||||
// copy all text in Output
|
// copy all text in Output
|
||||||
text := dstOutput.GetText(false)
|
text := dstOutput.GetText(false)
|
||||||
|
|
||||||
@ -488,7 +525,8 @@ func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
if len(text) > 0 {
|
if len(text) > 0 {
|
||||||
CopyToClipboard(text[:len(text)-1])
|
CopyToClipboard(text[:len(text)-1])
|
||||||
}
|
}
|
||||||
case tcell.KeyCtrlS:
|
return nil
|
||||||
|
case keyMaps["swap_language"]:
|
||||||
translator.SwapLang()
|
translator.SwapLang()
|
||||||
updateCurrentLang()
|
updateCurrentLang()
|
||||||
srcText := srcInput.GetText()
|
srcText := srcInput.GetText()
|
||||||
@ -500,7 +538,8 @@ func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
srcInput.SetText(dstText, true)
|
srcInput.SetText(dstText, true)
|
||||||
}
|
}
|
||||||
dstOutput.SetText(srcText)
|
dstOutput.SetText(srcText)
|
||||||
case tcell.KeyCtrlO:
|
return nil
|
||||||
|
case keyMaps["tts_source"]:
|
||||||
// Play text to speech on source of translation window.
|
// Play text to speech on source of translation window.
|
||||||
if translator.LockAvailable() {
|
if translator.LockAvailable() {
|
||||||
message := srcInput.GetText()
|
message := srcInput.GetText()
|
||||||
@ -517,7 +556,8 @@ func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
case tcell.KeyCtrlP:
|
return nil
|
||||||
|
case keyMaps["tts_destination"]:
|
||||||
// Play text to speech on destination of translation window.
|
// Play text to speech on destination of translation window.
|
||||||
if translator.LockAvailable() {
|
if translator.LockAvailable() {
|
||||||
message := dstOutput.GetText(false)
|
message := dstOutput.GetText(false)
|
||||||
@ -533,9 +573,11 @@ func translateWindowHandler(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case tcell.KeyCtrlX:
|
return nil
|
||||||
|
case keyMaps["stop_tts"]:
|
||||||
// Stop play sound
|
// Stop play sound
|
||||||
translator.StopTTS()
|
translator.StopTTS()
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return event
|
return event
|
||||||
|
Loading…
x
Reference in New Issue
Block a user