Переглянути джерело

feat(ui): implement message actions list (#111)

ayntgl 4 роки тому
батько
коміт
5b362d7c4f
4 змінених файлів з 73 додано та 58 видалено
  1. 18 24
      config/mod.go
  2. 1 0
      ui/app.go
  3. 51 33
      ui/handlers.go
  4. 3 1
      ui/views.go

+ 18 - 24
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"},
 		},
 	}
 }

+ 1 - 0
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

+ 51 - 33
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.ReferencedMessage != nil {
-			app.SelectedMessage, _ = util.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
-			app.MessagesTextView.
-				Highlight(m.ReferencedMessage.ID).
-				ScrollToHighlight()
-		}
-
-		return nil
-	} else if util.HasKeybinding(app.Config.Keybindings.ReplySelectedMessage, e.Name()) {
-		hs := app.MessagesTextView.GetHighlights()
-		if len(hs) == 0 {
+		if m == nil {
 			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.MentionReplySelectedMessage, e.Name()) {
-		hs := app.MessagesTextView.GetHighlights()
-		if len(hs) == 0 {
-			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)
 		}
 
-		_, 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
+		if util.HasPermission(app.Session.State, app.SelectedChannel.ID, discordgo.PermissionSendMessages) {
+			messageActionsList.AddItem("Reply", "", 'r', nil)
+			messageActionsList.AddItem("Mention Reply", "", 'R', nil)
 		}
 
-		_, m := util.FindMessageByID(app.SelectedChannel.Messages, hs[0])
-		err := clipboard.WriteAll(m.Content)
-		if err != nil {
-			return nil
+		if m.ReferencedMessage != nil {
+			messageActionsList.AddItem("Select Reply", "", 'r', nil)
 		}
 
-		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)
+
+		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+<rune>.
 	if e.Modifiers() == tcell.ModAlt {

+ 3 - 1
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 {