Przeglądaj źródła

refactor(ui): pass event handlers in main func

ayntgl 4 lat temu
rodzic
commit
7a9d0e066e
3 zmienionych plików z 16 dodań i 20 usunięć
  1. 14 10
      discordo.go
  2. 1 4
      ui/guilds.go
  3. 1 6
      ui/messages.go

+ 14 - 10
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
 	}

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

+ 1 - 6
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)