Prechádzať zdrojové kódy

refactor: move event handlers to ui package

rigormorrtiss 4 rokov pred
rodič
commit
044e48da2e
4 zmenil súbory, kde vykonal 48 pridanie a 39 odobranie
  1. 3 20
      discordo.go
  2. 34 13
      ui/inputfields.go
  3. 3 4
      ui/modals.go
  4. 8 2
      ui/textviews.go

+ 3 - 20
discordo.go

@@ -3,7 +3,6 @@ package main
 import (
 	"context"
 	"sort"
-	"strings"
 
 	"github.com/99designs/keyring"
 	"github.com/diamondburned/arikawa/v3/api"
@@ -55,13 +54,13 @@ func main() {
 	kr = util.OpenKeyringBackend()
 	config = util.NewConfig()
 
+	app = ui.NewApp(onAppInputCapture)
 	loginModal = ui.NewLoginModal(onLoginModalDone)
 	guildsDropDown = ui.NewGuildsDropDown(onGuildsDropDownSelected, config.Theme)
 	channelsTreeNode = tview.NewTreeNode("")
 	channelsTreeView = ui.NewChannelsTreeView(channelsTreeNode, onChannelsTreeViewSelected, config.Theme)
-	messagesTextView = ui.NewMessagesTextView(onMessagesTextViewChanged, config.Theme)
+	messagesTextView = ui.NewMessagesTextView(app, config.Theme)
 	mainFlex = ui.NewMainFlex(guildsDropDown, channelsTreeView, messagesTextView)
-	app = ui.NewApp(onAppInputCapture)
 
 	token := util.GetItem(kr, "token")
 	if token != "" {
@@ -96,10 +95,6 @@ func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey {
 	return e
 }
 
-func onMessagesTextViewChanged() {
-	app.Draw()
-}
-
 func onLoginModalDone(i int, label string) {
 	if label == ui.LoginViaEmailPasswordLoginModalButton {
 		loginVia = "emailpassword"
@@ -213,7 +208,7 @@ func onChannelsTreeViewSelected(n *tview.TreeNode) {
 		}
 	case discord.GuildText, discord.GuildNews:
 		if messageInputField == nil {
-			messageInputField = ui.NewMessageInputField(onMessageInputFieldDone, config.Theme)
+			messageInputField = ui.NewMessageInputField(discordSession, currentChannel, config.Theme)
 			mainFlex.AddItem(messageInputField, 3, 1, false)
 		}
 		app.SetFocus(messageInputField)
@@ -228,18 +223,6 @@ func onChannelsTreeViewSelected(n *tview.TreeNode) {
 	}
 }
 
-func onMessageInputFieldDone(k tcell.Key) {
-	if k == tcell.KeyEnter {
-		currentText := strings.TrimSpace(messageInputField.GetText())
-		if currentText == "" {
-			return
-		}
-
-		messageInputField.SetText("")
-		discordSession.SendMessage(currentChannel.ID, currentText)
-	}
-}
-
 func onLoginFormLoginButtonSelected() {
 	if loginVia == "emailpassword" {
 		email := loginForm.GetFormItemByLabel("Email").(*tview.InputField).GetText()

+ 34 - 13
ui/inputfields.go

@@ -1,32 +1,53 @@
 package ui
 
 import (
+	"strings"
+
 	"github.com/atotto/clipboard"
+	"github.com/diamondburned/arikawa/v3/discord"
+	"github.com/diamondburned/arikawa/v3/session"
 	"github.com/gdamore/tcell/v2"
 	"github.com/rigormorrtiss/discordo/util"
 	"github.com/rivo/tview"
 )
 
-func NewMessageInputField(onMessageInputFieldDone func(key tcell.Key), theme *util.Theme) *tview.InputField {
-	messageInputField := tview.NewInputField()
-
-	messageInputField.
+func NewMessageInputField(s *session.Session, c discord.Channel, theme *util.Theme) *tview.InputField {
+	i := tview.NewInputField()
+	i.
 		SetPlaceholder("Message...").
 		SetFieldWidth(0).
-		SetDoneFunc(onMessageInputFieldDone).
+		SetDoneFunc(onMessageInputFieldDone(i, s, c)).
 		SetFieldBackgroundColor(tcell.GetColor(theme.InputFieldBackground)).
 		SetBackgroundColor(tcell.GetColor(theme.InputFieldBackground)).
 		SetBorder(true).
 		SetBorderPadding(0, 0, 1, 1).
-		SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
-			if event.Key() == tcell.KeyCtrlV {
-				text, _ := clipboard.ReadAll()
-				text = messageInputField.GetText() + text
-				messageInputField.SetText(text)
+		SetInputCapture(onMessageInputFieldInputCapture(i))
+
+	return i
+}
+
+func onMessageInputFieldDone(i *tview.InputField, s *session.Session, c discord.Channel) func(k tcell.Key) {
+	return func(k tcell.Key) {
+		if k == tcell.KeyEnter {
+			t := strings.TrimSpace(i.GetText())
+			if t == "" {
+				return
 			}
 
-			return event
-		})
+			i.SetText("")
+			s.SendMessage(c.ID, t)
+		}
+	}
+}
+
+func onMessageInputFieldInputCapture(i *tview.InputField) func(event *tcell.EventKey) *tcell.EventKey {
+	return func(event *tcell.EventKey) *tcell.EventKey {
+		if event.Key() == tcell.KeyCtrlV {
+			text, _ := clipboard.ReadAll()
+			text = i.GetText() + text
+			i.SetText(text)
+		}
 
-	return messageInputField
+		return event
+	}
 }

+ 3 - 4
ui/modals.go

@@ -10,14 +10,13 @@ const (
 )
 
 func NewLoginModal(onLoginModalDone func(buttonIndex int, buttonLabel string)) *tview.Modal {
-	loginModal := tview.NewModal()
-
-	loginModal.
+	m := tview.NewModal()
+	m.
 		SetText("Choose a login method:").
 		AddButtons([]string{LoginViaTokenLoginModalButton, LoginViaEmailPasswordLoginModalButton}).
 		SetDoneFunc(onLoginModalDone).
 		SetBorder(true).
 		SetBorderPadding(0, 0, 1, 1)
 
-	return loginModal
+	return m
 }

+ 8 - 2
ui/textviews.go

@@ -6,17 +6,23 @@ import (
 	"github.com/rivo/tview"
 )
 
-func NewMessagesTextView(onMessagesTextViewChanged func(), theme *util.Theme) *tview.TextView {
+func NewMessagesTextView(app *tview.Application, theme *util.Theme) *tview.TextView {
 	messagesTextView := tview.NewTextView()
 
 	messagesTextView.
 		SetDynamicColors(true).
 		SetWordWrap(true).
 		ScrollToEnd().
-		SetChangedFunc(onMessagesTextViewChanged).
+		SetChangedFunc(onMessagesTextViewChanged(app)).
 		SetBackgroundColor(tcell.GetColor(theme.TextViewBackground)).
 		SetBorder(true).
 		SetBorderPadding(0, 0, 1, 1)
 
 	return messagesTextView
 }
+
+func onMessagesTextViewChanged(app *tview.Application) func() {
+	return func() {
+		app.Draw()
+	}
+}