From cf8cc8f72b387eef426e8d229a338e635e891583 Mon Sep 17 00:00:00 2001 From: ayn2op <119342035+ayn2op@users.noreply.github.com> Date: Sun, 15 Jan 2023 02:21:42 -0800 Subject: [PATCH] Support for focus shortcuts (#271) --- config/keys.go | 18 +++++++++++++++++ login_form.go | 11 ++--------- main.go | 18 +++-------------- main_flex.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 24 deletions(-) create mode 100644 main_flex.go diff --git a/config/keys.go b/config/keys.go index 99d7461..176711c 100644 --- a/config/keys.go +++ b/config/keys.go @@ -1,7 +1,13 @@ package config type ( + GuildsTreeKeys struct { + Focus string `yaml:"focus"` + } + MessagesTextKeys struct { + Focus string `yaml:"focus"` + CopyContent string `yaml:"copy_content"` Reply string `yaml:"reply"` @@ -15,6 +21,8 @@ type ( } MessageInputKeys struct { + Focus string `yaml:"focus"` + Send string `yaml:"send"` Paste string `yaml:"paste"` @@ -25,6 +33,7 @@ type ( type Keys struct { Cancel string `yaml:"cancel"` + GuildsTree GuildsTreeKeys `yaml:"guilds_tree"` MessagesText MessagesTextKeys `yaml:"messages_text"` MessageInput MessageInputKeys `yaml:"message_input"` } @@ -33,7 +42,13 @@ func newKeys() Keys { return Keys{ Cancel: "Esc", + GuildsTree: GuildsTreeKeys{ + Focus: "Alt+Rune[g]", + }, + MessagesText: MessagesTextKeys{ + Focus: "Alt+Rune[m]", + CopyContent: "Rune[c]", Reply: "Rune[r]", @@ -45,7 +60,10 @@ func newKeys() Keys { SelectFirst: "Home", SelectLast: "End", }, + MessageInput: MessageInputKeys{ + Focus: "Alt+Rune[i]", + Send: "Enter", Paste: "Ctrl+V", diff --git a/login_form.go b/login_form.go index fae0f0d..cf3adc0 100644 --- a/login_form.go +++ b/login_form.go @@ -69,13 +69,6 @@ func (lf *LoginForm) onLoginButtonSelected() { log.Fatal(err) } - right := tview.NewFlex() - right.SetDirection(tview.FlexRow) - right.AddItem(messagesText, 0, 1, false) - right.AddItem(messageInput, 3, 1, false) - // The guilds tree is always focused first at start-up. - flex.AddItem(guildsTree, 0, 1, true) - flex.AddItem(right, 0, 4, false) - - app.SetRoot(flex, true) + mainFlex = newMainFlex() + app.SetRoot(mainFlex, true) } diff --git a/main.go b/main.go index 2062064..1fdb873 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ var ( discordState *State app = tview.NewApplication() - flex = tview.NewFlex() + mainFlex *MainFlex guildsTree *GuildsTree messagesText *MessagesText messageInput *MessageInput @@ -69,11 +69,6 @@ func main() { log.Fatal(err) } - // UI must be initialized after the configuration has been loaded and before the plugins are loaded. - guildsTree = newGuildsTree() - messagesText = newMessagesText() - messageInput = newMessageInput() - // mission failed, we'll get 'em next time if token == "" { app.SetRoot(newLoginForm(), true) @@ -84,15 +79,8 @@ func main() { log.Fatal(err) } - right := tview.NewFlex() - right.SetDirection(tview.FlexRow) - right.AddItem(messagesText, 0, 1, false) - right.AddItem(messageInput, 3, 1, false) - // The guilds tree is always focused first at start-up. - flex.AddItem(guildsTree, 0, 1, true) - flex.AddItem(right, 0, 4, false) - - app.SetRoot(flex, true) + mainFlex = newMainFlex() + app.SetRoot(mainFlex, true) } app.EnableMouse(cfg.Mouse) diff --git a/main_flex.go b/main_flex.go new file mode 100644 index 0000000..e2895ff --- /dev/null +++ b/main_flex.go @@ -0,0 +1,53 @@ +package main + +import ( + "log" + + "github.com/gdamore/tcell/v2" + "github.com/rivo/tview" +) + +type MainFlex struct { + *tview.Flex +} + +func newMainFlex() *MainFlex { + mf := &MainFlex{ + Flex: tview.NewFlex(), + } + + // Initialize UI widgets + guildsTree = newGuildsTree() + messagesText = newMessagesText() + messageInput = newMessageInput() + + right := tview.NewFlex() + right.SetDirection(tview.FlexRow) + right.AddItem(messagesText, 0, 1, false) + right.AddItem(messageInput, 3, 1, false) + + // The guilds tree is always focused first at start-up. + mf.AddItem(guildsTree, 0, 1, true) + mf.AddItem(right, 0, 4, false) + + mf.SetInputCapture(mainFlex.onInputCapture) + + return mf +} + +func (mf *MainFlex) onInputCapture(event *tcell.EventKey) *tcell.EventKey { + log.Println(event.Name()) + switch event.Name() { + case cfg.Keys.GuildsTree.Focus: + app.SetFocus(guildsTree) + return nil + case cfg.Keys.MessagesText.Focus: + app.SetFocus(messagesText) + return nil + case cfg.Keys.MessageInput.Focus: + app.SetFocus(messageInput) + return nil + } + + return event +}