handlers.go 7.6 KB

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