From 5b362d7c4f9a643b734500107f0e978cac19d7ad Mon Sep 17 00:00:00 2001 From: ayntgl <90600421+ayntgl@users.noreply.github.com> Date: Sun, 16 Jan 2022 00:01:57 +0400 Subject: [PATCH] feat(ui): implement message actions list (#111) --- config/mod.go | 42 ++++++++++------------ ui/app.go | 1 + ui/handlers.go | 94 ++++++++++++++++++++++++++++++-------------------- ui/views.go | 4 ++- 4 files changed, 78 insertions(+), 63 deletions(-) diff --git a/config/mod.go b/config/mod.go index f27bf2a..9ac3cab 100644 --- a/config/mod.go +++ b/config/mod.go @@ -13,19 +13,16 @@ type GeneralConfig struct { } type KeybindingsConfig struct { - FocusGuildsList []string `json:"focusGuildsList"` - FocusChannelsTreeView []string `json:"focusChannelsTreeView"` - FocusMessagesTextView []string `json:"focusMessagesTextView"` - FocusMessageInputField []string `json:"focusMessageInputField"` + FocusGuildsList []string `json:"focusGuildsList"` + FocusChannelsTreeView []string `json:"focusChannelsTreeView"` + FocusMessagesTextView []string `json:"focusMessagesTextView"` + FocusMessageInputField []string `json:"focusMessageInputField"` + FocusMessageActionsList []string `json:"focusMessageActionsList"` - SelectPreviousMessage []string `json:"selectPreviousMessage"` - SelectNextMessage []string `json:"selectNextMessage"` - SelectFirstMessage []string `json:"selectFirstMessage"` - SelectLastMessage []string `json:"selectLastMessage"` - SelectMessageReference []string `json:"selectMessageReference"` - ReplySelectedMessage []string `json:"replySelectedMessage"` - MentionReplySelectedMessage []string `json:"mentionReplySelectedMessage"` - CopySelectedMessage []string `json:"copySelectedMessage"` + SelectPreviousMessage []string `json:"selectPreviousMessage"` + SelectNextMessage []string `json:"selectNextMessage"` + SelectFirstMessage []string `json:"selectFirstMessage"` + SelectLastMessage []string `json:"selectLastMessage"` } type Config struct { @@ -42,19 +39,16 @@ func New() *Config { Notifications: true, }, Keybindings: KeybindingsConfig{ - FocusGuildsList: []string{"Alt+Rune[g]"}, - FocusChannelsTreeView: []string{"Alt+Rune[t]"}, - FocusMessagesTextView: []string{"Alt+Rune[m]"}, - FocusMessageInputField: []string{"Alt+Rune[i]"}, + FocusGuildsList: []string{"Alt+Rune[g]"}, + FocusChannelsTreeView: []string{"Alt+Rune[t]"}, + FocusMessagesTextView: []string{"Alt+Rune[m]"}, + FocusMessageInputField: []string{"Alt+Rune[i]"}, + FocusMessageActionsList: []string{"Alt+Rune[a]"}, - SelectPreviousMessage: []string{"Up"}, - SelectNextMessage: []string{"Down"}, - SelectFirstMessage: []string{"Home"}, - SelectLastMessage: []string{"End"}, - SelectMessageReference: []string{"Rune[m]"}, - ReplySelectedMessage: []string{"Rune[r]"}, - MentionReplySelectedMessage: []string{"Rune[R]"}, - CopySelectedMessage: []string{"Rune[c]"}, + SelectPreviousMessage: []string{"Up"}, + SelectNextMessage: []string{"Down"}, + SelectFirstMessage: []string{"Home"}, + SelectLastMessage: []string{"End"}, }, } } diff --git a/ui/app.go b/ui/app.go index 3ae288a..0a97bb1 100644 --- a/ui/app.go +++ b/ui/app.go @@ -12,6 +12,7 @@ import ( type App struct { *tview.Application + MainFlex *tview.Flex GuildsList *tview.List ChannelsTreeView *tview.TreeView MessagesTextView *tview.TextView diff --git a/ui/handlers.go b/ui/handlers.go index 087f695..788b9a9 100644 --- a/ui/handlers.go +++ b/ui/handlers.go @@ -187,59 +187,77 @@ func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey Highlight(ms[app.SelectedMessage].ID). ScrollToHighlight() return nil - } else if util.HasKeybinding(app.Config.Keybindings.SelectMessageReference, e.Name()) { + } else if util.HasKeybinding(app.Config.Keybindings.FocusMessageActionsList, e.Name()) { + messageActionsList := tview.NewList() + hs := app.MessagesTextView.GetHighlights() if len(hs) == 0 { return nil } _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0]) + if m == nil { + return nil + } + + if util.HasPermission(app.Session.State, app.SelectedChannel.ID, discordgo.PermissionManageMessages) || m.Author.ID == app.Session.State.User.ID { + messageActionsList.AddItem("Edit", "", 'e', nil) + } + + if util.HasPermission(app.Session.State, app.SelectedChannel.ID, discordgo.PermissionSendMessages) { + messageActionsList.AddItem("Reply", "", 'r', nil) + messageActionsList.AddItem("Mention Reply", "", 'R', nil) + } + if m.ReferencedMessage != nil { - app.SelectedMessage, _ = util.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID) - app.MessagesTextView. - Highlight(m.ReferencedMessage.ID). - ScrollToHighlight() + messageActionsList.AddItem("Select Reply", "", 'r', nil) } - return nil - } else if util.HasKeybinding(app.Config.Keybindings.ReplySelectedMessage, e.Name()) { - hs := app.MessagesTextView.GetHighlights() - if len(hs) == 0 { - return nil - } + messageActionsList. + ShowSecondaryText(false). + AddItem("Copy Content", "", 'c', nil). + AddItem("Copy ID", "", 'i', nil). + SetDoneFunc(func() { + app.SetRoot(app.MainFlex, true) + }). + SetSelectedFunc(func(_ int, mainText string, _ string, _ rune) { + onMessageActionsListSelected(app, mainText, m) + }). + SetTitle("Press the Escape key to close"). + SetBorder(true) - _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0]) - app.MessageInputField.SetTitle("Replying to " + m.Author.String()) - app.SetFocus(app.MessageInputField) - return nil - } else if util.HasKeybinding(app.Config.Keybindings.MentionReplySelectedMessage, e.Name()) { - hs := app.MessagesTextView.GetHighlights() - if len(hs) == 0 { - return nil - } - - _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0]) - app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String()) - app.SetFocus(app.MessageInputField) - return nil - } else if util.HasKeybinding(app.Config.Keybindings.CopySelectedMessage, e.Name()) { - hs := app.MessagesTextView.GetHighlights() - if len(hs) == 0 { - return nil - } - - _, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0]) - err := clipboard.WriteAll(m.Content) - if err != nil { - return nil - } - - return nil + app.SetRoot(messageActionsList, true) } return e } +func onMessageActionsListSelected(app *App, mainText string, m *discordgo.Message) { + switch mainText { + case "Copy Content": + if err := clipboard.WriteAll(m.Content); err != nil { + return + } + case "Copy ID": + if err := clipboard.WriteAll(m.ID); err != nil { + return + } + case "Reply": + app.SetFocus(app.MessageInputField) + app.MessageInputField.SetTitle("Replying to " + m.Author.String()) + case "Mention Reply": + app.SetFocus(app.MessageInputField) + app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String()) + case "Select Reply": + app.SelectedMessage, _ = util.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID) + app.MessagesTextView. + Highlight(m.ReferencedMessage.ID). + ScrollToHighlight() + } + + app.SetRoot(app.MainFlex, true) +} + func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey { // The default global navigation shortcut for guilds list is Alt+. if e.Modifiers() == tcell.ModAlt { diff --git a/ui/views.go b/ui/views.go index c1df5a4..9ef1477 100644 --- a/ui/views.go +++ b/ui/views.go @@ -63,9 +63,11 @@ func NewMainFlex(app *App) *tview.Flex { AddItem(app.MessagesTextView, 0, 1, false). AddItem(app.MessageInputField, 3, 1, false) - return tview.NewFlex(). + app.MainFlex = tview.NewFlex(). AddItem(leftFlex, 0, 1, false). AddItem(rightFlex, 0, 4, false) + + return app.MainFlex } func NewLoginForm(mfa bool) *tview.Form {