Modularize tview widgets into separate functions

This commit is contained in:
ayntgl 2021-10-14 09:40:00 +04:00
parent d0b3af13b3
commit 4ab256ecb7
2 changed files with 58 additions and 37 deletions

40
main.go
View file

@ -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).

55
ui.go
View file

@ -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 {