From 4ab256ecb77795096bf08bbbc8765e3e66ad805e Mon Sep 17 00:00:00 2001 From: ayntgl Date: Thu, 14 Oct 2021 09:40:00 +0400 Subject: [PATCH] Modularize tview widgets into separate functions --- main.go | 40 ++++------------------------------------ ui.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/main.go b/main.go index e9afac3..605f2c0 100644 --- a/main.go +++ b/main.go @@ -23,42 +23,10 @@ func main() { tview.Styles.InverseTextColor = tcell.GetColor(conf.Theme.Text.Inverse) tview.Styles.ContrastSecondaryTextColor = tcell.GetColor(conf.Theme.Text.ContrastSecondary) - app = tview.NewApplication() - app. - EnableMouse(conf.Mouse). - SetInputCapture(onAppInputCapture) - - channelsTree = tview.NewTreeView() - channelsTree. - SetSelectedFunc(onChannelsTreeSelected). - SetTopLevel(1). - SetRoot(tview.NewTreeNode("")). - SetBorder(true). - SetBorderPadding(0, 0, 1, 0) - - messagesView = tview.NewTextView() - messagesView. - SetRegions(true). - SetDynamicColors(true). - SetWordWrap(true). - ScrollToEnd(). - SetChangedFunc(func() { - app.Draw() - }). - SetInputCapture(onMessagesTextViewInputCapture). - SetBorder(true). - SetBorderPadding(0, 0, 1, 0). - SetTitleAlign(tview.AlignLeft) - - messageInputField = tview.NewInputField() - messageInputField. - SetPlaceholder("Message..."). - SetPlaceholderTextColor(tcell.ColorWhite). - SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor). - SetInputCapture(onMessageInputFieldInputCapture). - SetBorder(true). - SetBorderPadding(0, 0, 1, 0). - SetTitleAlign(tview.AlignLeft) + app = newApp() + channelsTree = newChannelsTree() + messagesView = newMessagesView() + messageInputField = newMessageInputField() rightFlex := tview.NewFlex(). SetDirection(tview.FlexRow). diff --git a/ui.go b/ui.go index 2754d86..519c68e 100644 --- a/ui.go +++ b/ui.go @@ -18,6 +18,15 @@ var ( mainFlex *tview.Flex ) +func newApp() *tview.Application { + a := tview.NewApplication() + a. + EnableMouse(conf.Mouse). + SetInputCapture(onAppInputCapture) + + return a +} + func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey { switch e.Name() { case conf.Keybindings.ChannelsTree.Focus: @@ -31,6 +40,18 @@ func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey { return e } +func newChannelsTree() *tview.TreeView { + treeView := tview.NewTreeView() + treeView. + SetSelectedFunc(onChannelsTreeSelected). + SetTopLevel(1). + SetRoot(tview.NewTreeNode("")). + SetBorder(true). + SetBorderPadding(0, 0, 1, 0) + + return treeView +} + func onChannelsTreeSelected(n *tview.TreeNode) { selectedChannel = nil selectedMessage = nil @@ -181,7 +202,25 @@ func getTreeNodeByReference(r interface{}) (mn *tview.TreeNode) { return } -func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { +func newMessagesView() *tview.TextView { + textView := tview.NewTextView() + textView. + SetRegions(true). + SetDynamicColors(true). + SetWordWrap(true). + ScrollToEnd(). + SetChangedFunc(func() { + app.Draw() + }). + SetInputCapture(onMessagesViewInputCapture). + SetBorder(true). + SetBorderPadding(0, 0, 1, 0). + SetTitleAlign(tview.AlignLeft) + + return textView +} + +func onMessagesViewInputCapture(e *tcell.EventKey) *tcell.EventKey { if selectedChannel == nil { return nil } @@ -286,6 +325,20 @@ func onMessagesTextViewInputCapture(e *tcell.EventKey) *tcell.EventKey { return e } +func newMessageInputField() *tview.InputField { + inputField := tview.NewInputField() + inputField. + SetPlaceholder("Message..."). + SetPlaceholderTextColor(tcell.ColorWhite). + SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor). + SetInputCapture(onMessageInputFieldInputCapture). + SetBorder(true). + SetBorderPadding(0, 0, 1, 0). + SetTitleAlign(tview.AlignLeft) + + return inputField +} + func onMessageInputFieldInputCapture(e *tcell.EventKey) *tcell.EventKey { // If the "Alt" modifier key is pressed, do not handle the event. if e.Modifiers() == tcell.ModAlt {