Bläddra i källkod

Modularize tview widgets into separate functions

ayntgl 4 år sedan
förälder
incheckning
4ab256ecb7
2 ändrade filer med 58 tillägg och 37 borttagningar
  1. 4 36
      main.go
  2. 54 1
      ui.go

+ 4 - 36
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).

+ 54 - 1
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 {