package utils
|
|
|
|
import (
|
|
"github.com/gdamore/tcell/v2"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var (
|
|
CommandMenuKey = uiKeyInfo{
|
|
Key: tcell.Key(256),
|
|
KeyRune: 'm',
|
|
KeyLabel: "m",
|
|
KeyDesc: "display command menu",
|
|
}
|
|
NextScreenKey = uiKeyInfo{
|
|
Key: tcell.Key(256),
|
|
KeyRune: 'l',
|
|
KeyLabel: "l",
|
|
KeyDesc: "switch to next screen",
|
|
}
|
|
PreviousScreenKey = uiKeyInfo{
|
|
Key: tcell.Key(256),
|
|
KeyRune: 'h',
|
|
KeyLabel: "h",
|
|
KeyDesc: "switch to previous screen",
|
|
}
|
|
MoveUpKey = uiKeyInfo{
|
|
Key: tcell.KeyUp,
|
|
KeyRune: 'k',
|
|
KeyLabel: "k",
|
|
KeyDesc: "move up",
|
|
}
|
|
MoveDownKey = uiKeyInfo{
|
|
Key: tcell.KeyDown,
|
|
KeyRune: 'j',
|
|
KeyLabel: "j",
|
|
KeyDesc: "move down",
|
|
}
|
|
CloseDialogKey = uiKeyInfo{
|
|
Key: tcell.KeyEsc,
|
|
KeyLabel: "esc",
|
|
KeyDesc: "close the active dialog",
|
|
}
|
|
SwitchFocusKey = uiKeyInfo{
|
|
Key: tcell.KeyTab,
|
|
KeyLabel: "tab",
|
|
KeyDesc: "switch between widgets",
|
|
}
|
|
ArrowUpKey = uiKeyInfo{
|
|
Key: tcell.KeyUp,
|
|
KeyLabel: "arrow up",
|
|
KeyDesc: "move up",
|
|
}
|
|
ArrowDownKey = uiKeyInfo{
|
|
Key: tcell.KeyDown,
|
|
KeyLabel: "arrow down",
|
|
KeyDesc: "move down",
|
|
}
|
|
ArrowLeftKey = uiKeyInfo{
|
|
Key: tcell.KeyLeft,
|
|
KeyLabel: "arrow Left",
|
|
KeyDesc: "previous screen",
|
|
}
|
|
ArrowRightKey = uiKeyInfo{
|
|
Key: tcell.KeyRight,
|
|
KeyLabel: "arrow Right",
|
|
KeyDesc: "next screen",
|
|
}
|
|
AppExitKey = uiKeyInfo{
|
|
Key: tcell.KeyCtrlC,
|
|
KeyLabel: "ctrl+c",
|
|
KeyDesc: "exit application",
|
|
}
|
|
HelpScreenKey = uiKeyInfo{
|
|
Key: tcell.KeyF1,
|
|
KeyLabel: "F1",
|
|
KeyDesc: "display help screen",
|
|
}
|
|
AssetsScreenKey = uiKeyInfo{
|
|
Key: tcell.KeyF2,
|
|
KeyLabel: "F2",
|
|
KeyDesc: "display assets screen",
|
|
}
|
|
ShelvesScreenKey = uiKeyInfo{
|
|
Key: tcell.KeyF3,
|
|
KeyLabel: "F3",
|
|
KeyDesc: "display shelves screen",
|
|
}
|
|
CategoriesScreenKey = uiKeyInfo{
|
|
Key: tcell.KeyF4,
|
|
KeyLabel: "F4",
|
|
KeyDesc: "display categories screen",
|
|
}
|
|
BuildingsScreenKey = uiKeyInfo{
|
|
Key: tcell.KeyF5,
|
|
KeyLabel: "F5",
|
|
KeyDesc: "display buildings screen",
|
|
}
|
|
GroupsScreenKey = uiKeyInfo{
|
|
Key: tcell.KeyF6,
|
|
KeyLabel: "F6",
|
|
KeyDesc: "display groups screen",
|
|
}
|
|
)
|
|
|
|
var UIKeyBindings = []uiKeyInfo{
|
|
CommandMenuKey,
|
|
NextScreenKey,
|
|
PreviousScreenKey,
|
|
MoveUpKey,
|
|
MoveDownKey,
|
|
CloseDialogKey,
|
|
SwitchFocusKey,
|
|
ArrowUpKey,
|
|
ArrowDownKey,
|
|
ArrowLeftKey,
|
|
ArrowRightKey,
|
|
AppExitKey,
|
|
HelpScreenKey,
|
|
AssetsScreenKey,
|
|
ShelvesScreenKey,
|
|
CategoriesScreenKey,
|
|
BuildingsScreenKey,
|
|
GroupsScreenKey,
|
|
}
|
|
|
|
type uiKeyInfo struct {
|
|
Key tcell.Key
|
|
KeyRune rune
|
|
KeyLabel string
|
|
KeyDesc string
|
|
}
|
|
|
|
func (key *uiKeyInfo) Label() string {
|
|
return key.KeyLabel
|
|
}
|
|
|
|
func (key *uiKeyInfo) Rune() rune {
|
|
return key.KeyRune
|
|
}
|
|
|
|
func (key *uiKeyInfo) EventKey() tcell.Key {
|
|
return key.Key
|
|
}
|
|
|
|
func (key *uiKeyInfo) Description() string {
|
|
return key.KeyDesc
|
|
}
|
|
|
|
func ParseKeyEventKey(logger *zap.Logger, event *tcell.EventKey) *tcell.EventKey {
|
|
logger.Sugar().Debugw("parse key event",
|
|
"event", event,
|
|
"key", event.Key(),
|
|
"name", event.Name())
|
|
|
|
switch event.Rune() {
|
|
case MoveUpKey.KeyRune:
|
|
return tcell.NewEventKey(MoveUpKey.Key, MoveUpKey.KeyRune, tcell.ModNone)
|
|
case MoveDownKey.KeyRune:
|
|
return tcell.NewEventKey(MoveDownKey.Key, MoveDownKey.KeyRune, tcell.ModNone)
|
|
}
|
|
|
|
switch event.Key() {
|
|
case ArrowLeftKey.Key:
|
|
return tcell.NewEventKey(PreviousScreenKey.Key, PreviousScreenKey.KeyRune, tcell.ModNone)
|
|
case ArrowRightKey.Key:
|
|
return tcell.NewEventKey(NextScreenKey.Key, NextScreenKey.KeyRune, tcell.ModNone)
|
|
}
|
|
|
|
return event
|
|
}
|