Support for focus shortcuts (#271)

This commit is contained in:
ayn2op 2023-01-15 02:21:42 -08:00 • committed by GitHub
parent 892349545c
commit cf8cc8f72b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 24 deletions

View file

@ -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",

View file

@ -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)
}

18
main.go
View file

@ -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)

53
main_flex.go Normal file
View file

@ -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
}