Преглед на файлове

Move ui events handlers to corresponding file

rigormorrtiss преди 4 години
родител
ревизия
88614c1888
променени са 4 файла, в които са добавени 25 реда и са изтрити 21 реда
  1. 4 16
      discordo.go
  2. 5 1
      ui/app.go
  3. 8 2
      ui/forms.go
  4. 8 2
      ui/textviews.go

+ 4 - 16
discordo.go

@@ -46,9 +46,9 @@ func main() {
 	loginModal = ui.NewLoginModal(onLoginModalDone)
 	guildsDropDown = ui.NewGuildsDropDown(onGuildsDropDownSelected)
 	channelsList = ui.NewChannelsList(onChannelsListSelected)
-	messagesTextView = ui.NewMessagesTextView(onMessagesTextViewChanged)
+	messagesTextView = ui.NewMessagesTextView(app)
 	mainFlex = ui.NewMainFlex(guildsDropDown, channelsList, messagesTextView)
-	app = ui.NewApp(onAppInputCapture)
+	app = ui.NewApp()
 
 	token := util.GetPassword("token")
 	if token != "" {
@@ -66,26 +66,14 @@ func main() {
 	}
 }
 
-func onAppInputCapture(event *tcell.EventKey) *tcell.EventKey {
-	return event
-}
-
-func onLoginFormQuitButtonSelected() {
-	app.Stop()
-}
-
-func onMessagesTextViewChanged() {
-	app.Draw()
-}
-
 func onLoginModalDone(buttonIndex int, buttonLabel string) {
 	if buttonLabel == ui.LoginViaEmailPasswordLoginModalButton {
 		loginVia = "emailpassword"
-		loginForm = ui.NewLoginForm(loginVia, onLoginFormLoginButtonSelected, onLoginFormQuitButtonSelected)
+		loginForm = ui.NewLoginForm(app, loginVia, onLoginFormLoginButtonSelected)
 		app.SetRoot(loginForm, true)
 	} else if buttonLabel == ui.LoginViaTokenLoginModalButton {
 		loginVia = "token"
-		loginForm = ui.NewLoginForm(loginVia, onLoginFormLoginButtonSelected, onLoginFormQuitButtonSelected)
+		loginForm = ui.NewLoginForm(app, loginVia, onLoginFormLoginButtonSelected)
 		app.SetRoot(loginForm, true)
 	}
 }

+ 5 - 1
ui/app.go

@@ -5,10 +5,14 @@ import (
 	"github.com/rivo/tview"
 )
 
-func NewApp(onAppInputCapture func(event *tcell.EventKey) *tcell.EventKey) (app *tview.Application) {
+func NewApp() (app *tview.Application) {
 	app = tview.NewApplication().
 		EnableMouse(true).
 		SetInputCapture(onAppInputCapture)
 
 	return
 }
+
+func onAppInputCapture(event *tcell.EventKey) *tcell.EventKey {
+	return event
+}

+ 8 - 2
ui/forms.go

@@ -5,10 +5,10 @@ import (
 	"github.com/rivo/tview"
 )
 
-func NewLoginForm(via string, onLoginFormLoginButtonSelected func(), onLoginFormQuitButtonSelected func()) (loginForm *tview.Form) {
+func NewLoginForm(app *tview.Application, via string, onLoginFormLoginButtonSelected func()) (loginForm *tview.Form) {
 	loginForm = tview.NewForm().
 		AddButton("Login", onLoginFormLoginButtonSelected).
-		AddButton("Quit", onLoginFormQuitButtonSelected).
+		AddButton("Quit", onLoginFormQuitButtonSelected(app)).
 		SetButtonsAlign(tview.AlignCenter)
 	loginForm.
 		SetButtonBackgroundColor(tcell.GetColor("#5865F2")).
@@ -25,3 +25,9 @@ func NewLoginForm(via string, onLoginFormLoginButtonSelected func(), onLoginForm
 
 	return loginForm
 }
+
+func onLoginFormQuitButtonSelected(app *tview.Application) func() {
+	return func() {
+		app.Stop()
+	}
+}

+ 8 - 2
ui/textviews.go

@@ -4,17 +4,23 @@ import (
 	"github.com/rivo/tview"
 )
 
-func NewMessagesTextView(onMessagesTextViewChanged func()) (messagesTextView *tview.TextView) {
+func NewMessagesTextView(app *tview.Application) (messagesTextView *tview.TextView) {
 	messagesTextView = tview.NewTextView().
 		SetDynamicColors(true).
 		SetWrap(true).
 		SetWordWrap(true).
 		SetScrollable(true).
 		ScrollToEnd().
-		SetChangedFunc(onMessagesTextViewChanged)
+		SetChangedFunc(onMessagesTextViewChanged(app))
 	messagesTextView.
 		SetBorder(true).
 		SetBorderPadding(0, 0, 1, 1)
 
 	return
 }
+
+func onMessagesTextViewChanged(app *tview.Application) func() {
+	return func() {
+		app.Draw()
+	}
+}