From 7a9d0e066e4c1beacee6a4ae664b5dd567d91c53 Mon Sep 17 00:00:00 2001 From: ayntgl Date: Tue, 14 Sep 2021 14:28:29 +0400 Subject: [PATCH] refactor(ui): pass event handlers in main func --- discordo.go | 24 ++++++++++++++---------- ui/guilds.go | 5 +---- ui/messages.go | 7 +------ 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/discordo.go b/discordo.go index e227d5a..40bf4d4 100644 --- a/discordo.go +++ b/discordo.go @@ -33,15 +33,19 @@ func main() { config = util.NewConfig() tview.Styles = config.Theme - app = tview.NewApplication(). - EnableMouse(config.Mouse). - SetInputCapture(onAppInputCapture) - guildsView = ui.NewGuildsView(onGuildsTreeViewSelected) - messagesView = ui.NewMessagesView( - app, - onMessagesTextViewInputCapture, - ) - messageInputField = ui.NewMessageInputField(onMessageInputFieldInputCapture) + app = tview.NewApplication() + app.EnableMouse(config.Mouse) + app.SetInputCapture(onAppInputCapture) + + guildsView = ui.NewGuildsView() + guildsView.SetSelectedFunc(onGuildsTreeViewSelected) + + messagesView = ui.NewMessagesView(app) + messagesView.SetInputCapture(onMessagesViewInputCapture) + + messageInputField = ui.NewMessageInputField() + messageInputField.SetInputCapture(onMessageInputFieldInputCapture) + mainFlex = ui.NewMainFlex( guildsView, messagesView, @@ -99,7 +103,7 @@ func findByMessageID(ms []*discordgo.Message, mID string) (int, *discordgo.Messa return -1, nil } -func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { +func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey { if selectedChannel == nil { return nil } diff --git a/ui/guilds.go b/ui/guilds.go index 72cbd78..9a356f6 100644 --- a/ui/guilds.go +++ b/ui/guilds.go @@ -7,14 +7,11 @@ import ( ) // NewGuildsView creates and returns a new guilds treeview. -func NewGuildsView( - onGuildsTreeViewSelected func(*tview.TreeNode), -) *tview.TreeView { +func NewGuildsView() *tview.TreeView { v := tview.NewTreeView() v. SetTopLevel(1). SetRoot(tview.NewTreeNode("")). - SetSelectedFunc(onGuildsTreeViewSelected). SetBorder(true). SetBorderPadding(0, 0, 1, 0). SetTitle("Guilds"). diff --git a/ui/messages.go b/ui/messages.go index e18fda5..5365b87 100644 --- a/ui/messages.go +++ b/ui/messages.go @@ -8,7 +8,6 @@ import ( // NewMessagesView creates and returns a new messages textview. func NewMessagesView( app *tview.Application, - onMessagesTextViewInputCapture func(*tcell.EventKey) *tcell.EventKey, ) *tview.TextView { v := tview.NewTextView() v. @@ -19,7 +18,6 @@ func NewMessagesView( SetChangedFunc(func() { app.Draw() }). - SetInputCapture(onMessagesTextViewInputCapture). SetBorder(true). SetBorderPadding(0, 0, 1, 0). SetTitleAlign(tview.AlignLeft) @@ -28,15 +26,12 @@ func NewMessagesView( } // NewMessageInputField creates and returns a new message inputfield. -func NewMessageInputField( - onMessageInputFieldInputCapture func(*tcell.EventKey) *tcell.EventKey, -) *tview.InputField { +func NewMessageInputField() *tview.InputField { i := tview.NewInputField() i. SetPlaceholder("Message..."). SetPlaceholderTextColor(tcell.ColorWhite). SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor). - SetInputCapture(onMessageInputFieldInputCapture). SetBorder(true). SetBorderPadding(0, 0, 1, 0). SetTitleAlign(tview.AlignLeft)