From 30dffbaa8310d434a15030c0efbc7c34f277fb95 Mon Sep 17 00:00:00 2001 From: ayntgl <90600421+ayntgl@users.noreply.github.com> Date: Sat, 30 Oct 2021 18:35:50 +0400 Subject: [PATCH] refactor: move UI components to ui package (#93) --- discord.go | 2 +- main.go | 26 ++++++++++---- ui.go | 72 --------------------------------------- ui/channels_tree.go | 15 ++++++++ ui/login_form.go | 22 ++++++++++++ ui/message_input_field.go | 19 +++++++++++ ui/messages_view.go | 16 +++++++++ 7 files changed, 93 insertions(+), 79 deletions(-) create mode 100644 ui/channels_tree.go create mode 100644 ui/login_form.go create mode 100644 ui/message_input_field.go create mode 100644 ui/messages_view.go diff --git a/discord.go b/discord.go index 8e99c66..1de6d5c 100644 --- a/discord.go +++ b/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 diff --git a/main.go b/main.go index a982eed..845a9c0 100644 --- a/main.go +++ b/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 diff --git a/ui.go b/ui.go index ff9fa53..784a703 100644 --- a/ui.go +++ b/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 -} diff --git a/ui/channels_tree.go b/ui/channels_tree.go new file mode 100644 index 0000000..22948ee --- /dev/null +++ b/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 +} diff --git a/ui/login_form.go b/ui/login_form.go new file mode 100644 index 0000000..1ec1848 --- /dev/null +++ b/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 +} diff --git a/ui/message_input_field.go b/ui/message_input_field.go new file mode 100644 index 0000000..2699230 --- /dev/null +++ b/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 +} diff --git a/ui/messages_view.go b/ui/messages_view.go new file mode 100644 index 0000000..1306fed --- /dev/null +++ b/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 +}