From 044e48da2e7a2a2f6936c5a6d3d4477f1db54efa Mon Sep 17 00:00:00 2001 From: rigormorrtiss Date: Tue, 10 Aug 2021 22:34:37 +0400 Subject: [PATCH] refactor: move event handlers to ui package --- discordo.go | 23 +++------------------- ui/inputfields.go | 49 +++++++++++++++++++++++++++++++++-------------- ui/modals.go | 7 +++---- ui/textviews.go | 10 ++++++++-- 4 files changed, 49 insertions(+), 40 deletions(-) diff --git a/discordo.go b/discordo.go index 8b371c5..19af472 100644 --- a/discordo.go +++ b/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() diff --git a/ui/inputfields.go b/ui/inputfields.go index 74bcf56..2727256 100644 --- a/ui/inputfields.go +++ b/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 - }) - - return messageInputField + 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 event + } } diff --git a/ui/modals.go b/ui/modals.go index 5d825df..af89470 100644 --- a/ui/modals.go +++ b/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 } diff --git a/ui/textviews.go b/ui/textviews.go index 599a5bb..0830948 100644 --- a/ui/textviews.go +++ b/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() + } +}