Kaynağa Gözat

Support for focus shortcuts (#271)

ayn2op 3 yıl önce
ebeveyn
işleme
cf8cc8f72b
4 değiştirilmiş dosya ile 76 ekleme ve 24 silme
  1. 18 0
      config/keys.go
  2. 2 9
      login_form.go
  3. 3 15
      main.go
  4. 53 0
      main_flex.go

+ 18 - 0
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",

+ 2 - 9
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)
 }

+ 3 - 15
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)

+ 53 - 0
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
+}