Parcourir la source

refactor: move UI components to ui package (#93)

ayntgl il y a 4 ans
Parent
commit
30dffbaa83
7 fichiers modifiés avec 93 ajouts et 79 suppressions
  1. 1 1
      discord.go
  2. 20 6
      main.go
  3. 0 72
      ui.go
  4. 15 0
      ui/channels_tree.go
  5. 22 0
      ui/login_form.go
  6. 19 0
      ui/message_input_field.go
  7. 16 0
      ui/messages_view.go

+ 1 - 1
discord.go

@@ -48,7 +48,7 @@ func onSessionReady(_ *discordgo.Session, r *discordgo.Ready) {
 
 	sort.Slice(r.Guilds, func(a, b int) bool {
 		found := false
-		for _, gID := range session.State.Settings.GuildPositions {
+		for _, gID := range r.Settings.GuildPositions {
 			if found {
 				if gID == r.Guilds[b].ID {
 					return true

+ 20 - 6
main.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"github.com/ayntgl/discordgo"
+	"github.com/ayntgl/discordo/ui"
 	"github.com/ayntgl/discordo/util"
 	"github.com/gdamore/tcell/v2"
 	"github.com/rivo/tview"
@@ -39,10 +40,23 @@ func main() {
 	tview.Styles.InverseTextColor = tcell.GetColor(conf.Theme.Text)
 	tview.Styles.ContrastSecondaryTextColor = tcell.GetColor(conf.Theme.Text)
 
-	app = newApp()
-	channelsTree = newChannelsTree()
-	messagesView = newMessagesView()
-	messageInputField = newMessageInputField()
+	app = tview.NewApplication()
+	app.
+		EnableMouse(conf.Mouse).
+		SetInputCapture(onAppInputCapture)
+
+	channelsTree = ui.NewChannelsTree()
+	channelsTree.SetSelectedFunc(onChannelsTreeSelected)
+
+	messagesView = ui.NewMessagesView()
+	messagesView.
+		SetChangedFunc(func() {
+			app.Draw()
+		}).
+		SetInputCapture(onMessagesViewInputCapture)
+
+	messageInputField = ui.NewMessageInputField()
+	messageInputField.SetInputCapture(onMessageInputFieldInputCapture)
 
 	rightFlex := tview.NewFlex().
 		SetDirection(tview.FlexRow).
@@ -69,7 +83,7 @@ func main() {
 			panic(err)
 		}
 	} else {
-		loginForm = newLoginForm(onLoginFormLoginButtonSelected, false)
+		loginForm = ui.NewLoginForm(onLoginFormLoginButtonSelected, false)
 		app.SetRoot(loginForm, true)
 	}
 
@@ -106,7 +120,7 @@ func onLoginFormLoginButtonSelected() {
 		go keyring.Set(service, "token", lr.Token)
 	} else if lr.MFA {
 		// The account has MFA enabled, reattempt login with code and ticket.
-		loginForm = newLoginForm(func() {
+		loginForm = ui.NewLoginForm(func() {
 			code := loginForm.GetFormItem(0).(*tview.InputField).GetText()
 			if code == "" {
 				return

+ 0 - 72
ui.go

@@ -16,15 +16,6 @@ var (
 	selectedMessage int = -1
 )
 
-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.FocusChannelsTree:
@@ -41,19 +32,6 @@ func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey {
 	return e
 }
 
-func newChannelsTree() *tview.TreeView {
-	treeView := tview.NewTreeView()
-	treeView.
-		SetSelectedFunc(onChannelsTreeSelected).
-		SetTopLevel(1).
-		SetRoot(tview.NewTreeNode("")).
-		SetTitle("Channels").
-		SetBorder(true).
-		SetBorderPadding(0, 0, 1, 0)
-
-	return treeView
-}
-
 func onChannelsTreeSelected(n *tview.TreeNode) {
 	selectedChannel = nil
 	selectedMessage = 0
@@ -151,23 +129,6 @@ func onChannelsTreeSelected(n *tview.TreeNode) {
 	}
 }
 
-func newMessagesView() *tview.TextView {
-	textView := tview.NewTextView()
-	textView.
-		SetRegions(true).
-		SetDynamicColors(true).
-		SetWordWrap(true).
-		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
@@ -270,20 +231,6 @@ func onMessagesViewInputCapture(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 {
 	switch e.Key() {
 	case tcell.KeyEnter:
@@ -333,22 +280,3 @@ func onMessageInputFieldInputCapture(e *tcell.EventKey) *tcell.EventKey {
 
 	return e
 }
-
-func newLoginForm(onLoginFormLoginButtonSelected func(), mfa bool) *tview.Form {
-	w := tview.NewForm()
-	w.
-		AddButton("Login", onLoginFormLoginButtonSelected).
-		SetButtonsAlign(tview.AlignCenter).
-		SetBorder(true).
-		SetBorderPadding(0, 0, 1, 0)
-
-	if mfa {
-		w.AddPasswordField("Code", "", 0, 0, nil)
-	} else {
-		w.
-			AddInputField("Email", "", 0, nil, nil).
-			AddPasswordField("Password", "", 0, 0, nil)
-	}
-
-	return w
-}

+ 15 - 0
ui/channels_tree.go

@@ -0,0 +1,15 @@
+package ui
+
+import "github.com/rivo/tview"
+
+func NewChannelsTree() *tview.TreeView {
+	treeView := tview.NewTreeView()
+	treeView.
+		SetTopLevel(1).
+		SetRoot(tview.NewTreeNode("")).
+		SetTitle("Channels").
+		SetBorder(true).
+		SetBorderPadding(0, 0, 1, 0)
+
+	return treeView
+}

+ 22 - 0
ui/login_form.go

@@ -0,0 +1,22 @@
+package ui
+
+import "github.com/rivo/tview"
+
+func NewLoginForm(onLoginFormLoginButtonSelected func(), mfa bool) *tview.Form {
+	w := tview.NewForm()
+	w.
+		AddButton("Login", onLoginFormLoginButtonSelected).
+		SetButtonsAlign(tview.AlignCenter).
+		SetBorder(true).
+		SetBorderPadding(0, 0, 1, 0)
+
+	if mfa {
+		w.AddPasswordField("Code", "", 0, 0, nil)
+	} else {
+		w.
+			AddInputField("Email", "", 0, nil, nil).
+			AddPasswordField("Password", "", 0, 0, nil)
+	}
+
+	return w
+}

+ 19 - 0
ui/message_input_field.go

@@ -0,0 +1,19 @@
+package ui
+
+import (
+	"github.com/gdamore/tcell/v2"
+	"github.com/rivo/tview"
+)
+
+func NewMessageInputField() *tview.InputField {
+	inputField := tview.NewInputField()
+	inputField.
+		SetPlaceholder("Message...").
+		SetPlaceholderTextColor(tcell.ColorWhite).
+		SetFieldBackgroundColor(tview.Styles.PrimitiveBackgroundColor).
+		SetBorder(true).
+		SetBorderPadding(0, 0, 1, 0).
+		SetTitleAlign(tview.AlignLeft)
+
+	return inputField
+}

+ 16 - 0
ui/messages_view.go

@@ -0,0 +1,16 @@
+package ui
+
+import "github.com/rivo/tview"
+
+func NewMessagesView() *tview.TextView {
+	textView := tview.NewTextView()
+	textView.
+		SetRegions(true).
+		SetDynamicColors(true).
+		SetWordWrap(true).
+		SetBorder(true).
+		SetBorderPadding(0, 0, 1, 0).
+		SetTitleAlign(tview.AlignLeft)
+
+	return textView
+}