handlers.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. package ui
  2. import (
  3. "sort"
  4. "strings"
  5. "github.com/atotto/clipboard"
  6. "github.com/ayntgl/discordgo"
  7. "github.com/ayntgl/discordo/discord"
  8. "github.com/gdamore/tcell/v2"
  9. "github.com/rivo/tview"
  10. )
  11. func onAppInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  12. if hasKeybinding(app.Config.Keybindings.FocusGuildsList, e.Name()) {
  13. app.SetFocus(app.GuildsList)
  14. return nil
  15. } else if hasKeybinding(app.Config.Keybindings.FocusChannelsTreeView, e.Name()) {
  16. app.SetFocus(app.ChannelsTreeView)
  17. return nil
  18. } else if hasKeybinding(app.Config.Keybindings.FocusMessagesTextView, e.Name()) {
  19. app.SetFocus(app.MessagesTextView)
  20. return nil
  21. } else if hasKeybinding(app.Config.Keybindings.FocusMessageInputField, e.Name()) {
  22. app.SetFocus(app.MessageInputField)
  23. return nil
  24. }
  25. return e
  26. }
  27. func onGuildsListSelected(app *App, guildIdx int) {
  28. rootTreeNode := app.ChannelsTreeView.GetRoot()
  29. rootTreeNode.ClearChildren()
  30. app.MessagesTextView.
  31. Highlight().
  32. Clear().
  33. SetTitle("")
  34. app.MessageInputField.SetText("")
  35. if guildIdx == 0 { // Direct Messages
  36. cs := app.Session.State.PrivateChannels
  37. sort.Slice(cs, func(i, j int) bool {
  38. return cs[i].LastMessageID > cs[j].LastMessageID
  39. })
  40. for _, c := range cs {
  41. channelTreeNode := tview.NewTreeNode(channelToString(c)).
  42. SetReference(c.ID)
  43. rootTreeNode.AddChild(channelTreeNode)
  44. }
  45. } else { // Guild
  46. cs := app.Session.State.Guilds[guildIdx-1].Channels
  47. sort.Slice(cs, func(i, j int) bool {
  48. return cs[i].Position < cs[j].Position
  49. })
  50. for _, c := range cs {
  51. if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID == "") {
  52. channelTreeNode := tview.NewTreeNode(channelToString(c)).
  53. SetReference(c.ID)
  54. rootTreeNode.AddChild(channelTreeNode)
  55. }
  56. }
  57. CATEGORY:
  58. for _, c := range cs {
  59. if c.Type == discordgo.ChannelTypeGuildCategory {
  60. for _, nestedChannel := range cs {
  61. if nestedChannel.ParentID == c.ID {
  62. channelTreeNode := tview.NewTreeNode(c.Name).
  63. SetReference(c.ID)
  64. rootTreeNode.AddChild(channelTreeNode)
  65. continue CATEGORY
  66. }
  67. }
  68. channelTreeNode := tview.NewTreeNode(c.Name).
  69. SetReference(c.ID)
  70. rootTreeNode.AddChild(channelTreeNode)
  71. }
  72. }
  73. for _, c := range cs {
  74. if (c.Type == discordgo.ChannelTypeGuildText || c.Type == discordgo.ChannelTypeGuildNews) && (c.ParentID != "") {
  75. var parentTreeNode *tview.TreeNode
  76. rootTreeNode.Walk(func(node, _ *tview.TreeNode) bool {
  77. if node.GetReference() == c.ParentID {
  78. parentTreeNode = node
  79. return false
  80. }
  81. return true
  82. })
  83. if parentTreeNode != nil {
  84. channelTreeNode := tview.NewTreeNode(channelToString(c)).
  85. SetReference(c.ID)
  86. parentTreeNode.AddChild(channelTreeNode)
  87. }
  88. }
  89. }
  90. }
  91. app.ChannelsTreeView.SetCurrentNode(rootTreeNode)
  92. app.SetFocus(app.ChannelsTreeView)
  93. }
  94. func onChannelsTreeViewSelected(app *App, n *tview.TreeNode) {
  95. c, err := app.Session.State.Channel(n.GetReference().(string))
  96. if err != nil {
  97. return
  98. }
  99. if c.Type == discordgo.ChannelTypeGuildCategory {
  100. n.SetExpanded(!n.IsExpanded())
  101. return
  102. }
  103. app.SelectedChannel = c
  104. app.MessagesTextView.SetTitle(channelToString(c))
  105. app.SetFocus(app.MessageInputField)
  106. go func() {
  107. ms, err := app.Session.ChannelMessages(c.ID, app.Config.General.FetchMessagesLimit, "", "", "")
  108. if err != nil {
  109. return
  110. }
  111. for i := len(ms) - 1; i >= 0; i-- {
  112. app.SelectedChannel.Messages = ms
  113. app.MessagesTextView.Write(buildMessage(app, ms[i]))
  114. }
  115. app.MessagesTextView.ScrollToEnd()
  116. }()
  117. }
  118. func onMessagesTextViewInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  119. if app.SelectedChannel == nil {
  120. return nil
  121. }
  122. ms := app.SelectedChannel.Messages
  123. if len(ms) == 0 {
  124. return nil
  125. }
  126. if hasKeybinding(app.Config.Keybindings.SelectPreviousMessage, e.Name()) {
  127. if len(app.MessagesTextView.GetHighlights()) == 0 {
  128. app.SelectedMessage = len(ms) - 1
  129. } else {
  130. app.SelectedMessage--
  131. if app.SelectedMessage < 0 {
  132. app.SelectedMessage = 0
  133. }
  134. }
  135. app.MessagesTextView.
  136. Highlight(ms[app.SelectedMessage].ID).
  137. ScrollToHighlight()
  138. return nil
  139. } else if hasKeybinding(app.Config.Keybindings.SelectNextMessage, e.Name()) {
  140. if len(app.MessagesTextView.GetHighlights()) == 0 {
  141. app.SelectedMessage = len(ms) - 1
  142. } else {
  143. app.SelectedMessage++
  144. if app.SelectedMessage >= len(ms) {
  145. app.SelectedMessage = len(ms) - 1
  146. }
  147. }
  148. app.MessagesTextView.
  149. Highlight(ms[app.SelectedMessage].ID).
  150. ScrollToHighlight()
  151. return nil
  152. } else if hasKeybinding(app.Config.Keybindings.SelectFirstMessage, e.Name()) {
  153. app.SelectedMessage = 0
  154. app.MessagesTextView.
  155. Highlight(ms[app.SelectedMessage].ID).
  156. ScrollToHighlight()
  157. return nil
  158. } else if hasKeybinding(app.Config.Keybindings.SelectLastMessage, e.Name()) {
  159. app.SelectedMessage = len(ms) - 1
  160. app.MessagesTextView.
  161. Highlight(ms[app.SelectedMessage].ID).
  162. ScrollToHighlight()
  163. return nil
  164. } else if hasKeybinding(app.Config.Keybindings.SelectMessageReference, e.Name()) {
  165. hs := app.MessagesTextView.GetHighlights()
  166. if len(hs) == 0 {
  167. return nil
  168. }
  169. _, m := discord.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  170. if m.ReferencedMessage != nil {
  171. app.SelectedMessage, _ = discord.FindMessageByID(app.SelectedChannel.Messages, m.ReferencedMessage.ID)
  172. app.MessagesTextView.
  173. Highlight(m.ReferencedMessage.ID).
  174. ScrollToHighlight()
  175. }
  176. return nil
  177. } else if hasKeybinding(app.Config.Keybindings.ReplySelectedMessage, e.Name()) {
  178. hs := app.MessagesTextView.GetHighlights()
  179. if len(hs) == 0 {
  180. return nil
  181. }
  182. _, m := discord.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  183. app.MessageInputField.SetTitle("Replying to " + m.Author.String())
  184. app.SetFocus(app.MessageInputField)
  185. return nil
  186. } else if hasKeybinding(app.Config.Keybindings.MentionReplySelectedMessage, e.Name()) {
  187. hs := app.MessagesTextView.GetHighlights()
  188. if len(hs) == 0 {
  189. return nil
  190. }
  191. _, m := discord.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  192. app.MessageInputField.SetTitle("[@] Replying to " + m.Author.String())
  193. app.SetFocus(app.MessageInputField)
  194. return nil
  195. } else if hasKeybinding(app.Config.Keybindings.CopySelectedMessage, e.Name()) {
  196. hs := app.MessagesTextView.GetHighlights()
  197. if len(hs) == 0 {
  198. return nil
  199. }
  200. _, m := discord.FindMessageByID(app.SelectedChannel.Messages, hs[0])
  201. err := clipboard.WriteAll(m.Content)
  202. if err != nil {
  203. return nil
  204. }
  205. return nil
  206. }
  207. return e
  208. }
  209. func onMessageInputFieldInputCapture(app *App, e *tcell.EventKey) *tcell.EventKey {
  210. // The default global navigation shortcut for guilds list is Alt+<rune>.
  211. if e.Modifiers() == tcell.ModAlt {
  212. return nil
  213. }
  214. switch e.Key() {
  215. case tcell.KeyEnter:
  216. if app.SelectedChannel == nil {
  217. return nil
  218. }
  219. t := strings.TrimSpace(app.MessageInputField.GetText())
  220. if t == "" {
  221. return nil
  222. }
  223. if len(app.MessagesTextView.GetHighlights()) != 0 {
  224. _, m := discord.FindMessageByID(app.SelectedChannel.Messages, app.MessagesTextView.GetHighlights()[0])
  225. d := &discordgo.MessageSend{
  226. Content: t,
  227. Reference: m.Reference(),
  228. AllowedMentions: &discordgo.MessageAllowedMentions{RepliedUser: false},
  229. }
  230. if strings.HasPrefix(app.MessageInputField.GetTitle(), "[@]") {
  231. d.AllowedMentions.RepliedUser = true
  232. } else {
  233. d.AllowedMentions.RepliedUser = false
  234. }
  235. go app.Session.ChannelMessageSendComplex(m.ChannelID, d)
  236. app.SelectedMessage = -1
  237. app.MessagesTextView.Highlight()
  238. app.MessageInputField.SetTitle("")
  239. } else {
  240. go app.Session.ChannelMessageSend(app.SelectedChannel.ID, t)
  241. }
  242. app.MessageInputField.SetText("")
  243. return nil
  244. case tcell.KeyCtrlV:
  245. text, _ := clipboard.ReadAll()
  246. text = app.MessageInputField.GetText() + text
  247. app.MessageInputField.SetText(text)
  248. return nil
  249. case tcell.KeyEscape:
  250. app.MessageInputField.SetText("")
  251. app.MessageInputField.SetTitle("")
  252. app.SelectedMessage = -1
  253. app.MessagesTextView.Highlight()
  254. return nil
  255. }
  256. return e
  257. }