view.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package chat
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "sync"
  6. "time"
  7. "github.com/ayn2op/discordo/internal/config"
  8. "github.com/ayn2op/discordo/internal/ui"
  9. "github.com/ayn2op/tview"
  10. "github.com/ayn2op/tview/keybind"
  11. "github.com/ayn2op/tview/layers"
  12. "github.com/diamondburned/arikawa/v3/discord"
  13. "github.com/diamondburned/ningen/v3"
  14. "github.com/diamondburned/ningen/v3/states/read"
  15. "github.com/gdamore/tcell/v3"
  16. )
  17. const typingDuration = 10 * time.Second
  18. const (
  19. flexLayerName = "flex"
  20. mentionsListLayerName = "mentionsList"
  21. attachmentsListLayerName = "attachmentsList"
  22. confirmModalLayerName = "confirmModal"
  23. channelsPickerLayerName = "channelsPicker"
  24. )
  25. type View struct {
  26. *layers.Layers
  27. rootFlex *tview.Flex
  28. mainFlex *tview.Flex
  29. rightFlex *tview.Flex
  30. guildsTree *guildsTree
  31. messagesList *messagesList
  32. messageInput *messageInput
  33. channelsPicker *channelsPicker
  34. selectedChannel *discord.Channel
  35. selectedChannelMu sync.RWMutex
  36. typersMu sync.RWMutex
  37. typers map[discord.UserID]*time.Timer
  38. app *tview.Application
  39. cfg *config.Config
  40. state *ningen.State
  41. token string
  42. }
  43. func NewView(app *tview.Application, cfg *config.Config, token string) *View {
  44. v := &View{
  45. Layers: layers.New(),
  46. rootFlex: tview.NewFlex(),
  47. mainFlex: tview.NewFlex(),
  48. rightFlex: tview.NewFlex(),
  49. typers: make(map[discord.UserID]*time.Timer),
  50. app: app,
  51. cfg: cfg,
  52. token: token,
  53. }
  54. v.guildsTree = newGuildsTree(cfg, v)
  55. v.messagesList = newMessagesList(cfg, v)
  56. v.messageInput = newMessageInput(cfg, v)
  57. v.channelsPicker = newChannelsPicker(cfg, v)
  58. v.channelsPicker.SetCancelFunc(v.closePicker)
  59. v.SetBackgroundLayerStyle(v.cfg.Theme.Dialog.BackgroundStyle.Style)
  60. v.buildLayout()
  61. return v
  62. }
  63. func (v *View) SelectedChannel() *discord.Channel {
  64. v.selectedChannelMu.RLock()
  65. defer v.selectedChannelMu.RUnlock()
  66. return v.selectedChannel
  67. }
  68. func (v *View) SetSelectedChannel(channel *discord.Channel) {
  69. v.selectedChannelMu.Lock()
  70. v.selectedChannel = channel
  71. v.selectedChannelMu.Unlock()
  72. }
  73. func (v *View) buildLayout() {
  74. v.Clear()
  75. v.rootFlex.Clear()
  76. v.rightFlex.Clear()
  77. v.mainFlex.Clear()
  78. v.rightFlex.
  79. SetDirection(tview.FlexRow).
  80. AddItem(v.messagesList, 0, 1, false).
  81. AddItem(v.messageInput, 3, 1, false)
  82. // The guilds tree is always focused first at start-up.
  83. v.mainFlex.
  84. AddItem(v.guildsTree, 0, 1, true).
  85. AddItem(v.rightFlex, 0, 4, false)
  86. v.rootFlex.
  87. SetDirection(tview.FlexRow).
  88. AddItem(v.mainFlex, 0, 1, true)
  89. v.AddLayer(v.rootFlex, layers.WithName(flexLayerName), layers.WithResize(true), layers.WithVisible(true))
  90. v.AddLayer(v.messageInput.mentionsList, layers.WithName(mentionsListLayerName), layers.WithResize(false), layers.WithVisible(false))
  91. }
  92. func (v *View) togglePicker() {
  93. if v.HasLayer(channelsPickerLayerName) {
  94. v.closePicker()
  95. } else {
  96. v.openPicker()
  97. }
  98. }
  99. func (v *View) openPicker() {
  100. v.AddLayer(
  101. ui.Centered(v.channelsPicker, v.cfg.Picker.Width, v.cfg.Picker.Height),
  102. layers.WithName(channelsPickerLayerName),
  103. layers.WithResize(true),
  104. layers.WithVisible(true),
  105. layers.WithOverlay(),
  106. ).SendToFront(channelsPickerLayerName)
  107. v.channelsPicker.update()
  108. }
  109. func (v *View) closePicker() {
  110. v.RemoveLayer(channelsPickerLayerName)
  111. v.channelsPicker.Update()
  112. }
  113. func (v *View) toggleGuildsTree() {
  114. // The guilds tree is visible if the number of items is two.
  115. if v.mainFlex.GetItemCount() == 2 {
  116. v.mainFlex.RemoveItem(v.guildsTree)
  117. if v.guildsTree.HasFocus() {
  118. v.app.SetFocus(v.mainFlex)
  119. }
  120. } else {
  121. v.buildLayout()
  122. v.app.SetFocus(v.guildsTree)
  123. }
  124. }
  125. func (v *View) focusGuildsTree() bool {
  126. // The guilds tree is not hidden if the number of items is two.
  127. if v.mainFlex.GetItemCount() == 2 {
  128. v.app.SetFocus(v.guildsTree)
  129. return true
  130. }
  131. return false
  132. }
  133. func (v *View) focusMessageInput() bool {
  134. if !v.messageInput.GetDisabled() {
  135. v.app.SetFocus(v.messageInput)
  136. return true
  137. }
  138. return false
  139. }
  140. func (v *View) focusPrevious() {
  141. switch v.app.GetFocus() {
  142. case v.messagesList: // Handle both a.messagesList and a.flex as well as other edge cases (if there is).
  143. if v.focusGuildsTree() {
  144. return
  145. }
  146. fallthrough
  147. case v.guildsTree:
  148. if v.focusMessageInput() {
  149. return
  150. }
  151. fallthrough
  152. case v.messageInput:
  153. v.app.SetFocus(v.messagesList)
  154. }
  155. }
  156. func (v *View) focusNext() {
  157. switch v.app.GetFocus() {
  158. case v.messagesList:
  159. if v.focusMessageInput() {
  160. return
  161. }
  162. fallthrough
  163. case v.messageInput: // Handle both a.messageInput and a.flex as well as other edge cases (if there is).
  164. if v.focusGuildsTree() {
  165. return
  166. }
  167. fallthrough
  168. case v.guildsTree:
  169. v.app.SetFocus(v.messagesList)
  170. }
  171. }
  172. func (v *View) HandleEvent(event tcell.Event) tview.Command {
  173. switch event := event.(type) {
  174. case *tview.InitEvent:
  175. return tview.EventCommand(func() tcell.Event {
  176. if err := v.OpenState(v.token); err != nil {
  177. slog.Error("failed to open chat state", "err", err)
  178. return tcell.NewEventError(err)
  179. }
  180. return nil
  181. })
  182. case *QuitEvent:
  183. return tview.BatchCommand{
  184. tview.EventCommand(v.closeState),
  185. tview.QuitCommand{},
  186. }
  187. case *tview.KeyEvent:
  188. redraw := tview.RedrawCommand{}
  189. switch {
  190. case keybind.Matches(event, v.cfg.Keybinds.FocusGuildsTree.Keybind):
  191. v.messageInput.removeMentionsList()
  192. v.focusGuildsTree()
  193. return redraw
  194. case keybind.Matches(event, v.cfg.Keybinds.FocusMessagesList.Keybind):
  195. v.messageInput.removeMentionsList()
  196. v.app.SetFocus(v.messagesList)
  197. return redraw
  198. case keybind.Matches(event, v.cfg.Keybinds.FocusMessageInput.Keybind):
  199. v.focusMessageInput()
  200. return redraw
  201. case keybind.Matches(event, v.cfg.Keybinds.FocusPrevious.Keybind):
  202. v.focusPrevious()
  203. return redraw
  204. case keybind.Matches(event, v.cfg.Keybinds.FocusNext.Keybind):
  205. v.focusNext()
  206. return redraw
  207. case keybind.Matches(event, v.cfg.Keybinds.Logout.Keybind):
  208. return v.logout()
  209. case keybind.Matches(event, v.cfg.Keybinds.ToggleGuildsTree.Keybind):
  210. v.toggleGuildsTree()
  211. return redraw
  212. case keybind.Matches(event, v.cfg.Keybinds.ToggleChannelsPicker.Keybind):
  213. v.togglePicker()
  214. return redraw
  215. }
  216. }
  217. cmd := v.Layers.HandleEvent(event)
  218. return v.consumeLayerCommands(cmd)
  219. }
  220. func (v *View) consumeLayerCommands(command tview.Command) tview.Command {
  221. if command == nil {
  222. return nil
  223. }
  224. var commands []tview.Command
  225. switch c := command.(type) {
  226. case tview.BatchCommand:
  227. commands = c
  228. default:
  229. commands = []tview.Command{c}
  230. }
  231. remaining := make([]tview.Command, 0, len(commands))
  232. for _, cmd := range commands {
  233. switch c := cmd.(type) {
  234. case layers.OpenLayerCommand:
  235. if v.HasLayer(c.Name) {
  236. v.ShowLayer(c.Name).SendToFront(c.Name)
  237. }
  238. continue
  239. case layers.CloseLayerCommand:
  240. if v.HasLayer(c.Name) {
  241. v.HideLayer(c.Name)
  242. }
  243. continue
  244. case layers.ToggleLayerCommand:
  245. if v.HasLayer(c.Name) {
  246. if v.GetVisible(c.Name) {
  247. v.HideLayer(c.Name)
  248. } else {
  249. v.ShowLayer(c.Name).SendToFront(c.Name)
  250. }
  251. }
  252. continue
  253. }
  254. remaining = append(remaining, cmd)
  255. }
  256. if len(remaining) == 0 {
  257. return nil
  258. }
  259. if len(remaining) == 1 {
  260. return remaining[0]
  261. }
  262. return tview.BatchCommand(remaining)
  263. }
  264. func (v *View) showConfirmModal(prompt string, buttons []string, onDone func(label string)) {
  265. previousFocus := v.app.GetFocus()
  266. modal := tview.NewModal().
  267. SetText(prompt).
  268. AddButtons(buttons).
  269. SetDoneFunc(func(_ int, buttonLabel string) {
  270. v.RemoveLayer(confirmModalLayerName)
  271. v.app.SetFocus(previousFocus)
  272. if onDone != nil {
  273. onDone(buttonLabel)
  274. }
  275. })
  276. v.
  277. AddLayer(
  278. ui.Centered(modal, 0, 0),
  279. layers.WithName(confirmModalLayerName),
  280. layers.WithResize(true),
  281. layers.WithVisible(true),
  282. layers.WithOverlay(),
  283. ).
  284. SendToFront(confirmModalLayerName)
  285. }
  286. func (v *View) onReadUpdate(event *read.UpdateEvent) {
  287. v.app.QueueUpdateDraw(func() {
  288. // Use indexed node lookup to avoid walking the whole tree on every read
  289. // event. This runs frequently while reading/typing across channels.
  290. if event.GuildID.IsValid() {
  291. if guildNode := v.guildsTree.findNodeByReference(event.GuildID); guildNode != nil {
  292. v.guildsTree.setNodeLineStyle(guildNode, v.guildsTree.getGuildNodeStyle(event.GuildID))
  293. }
  294. }
  295. // Channel style is always updated for the target channel regardless of
  296. // whether it's in a guild or DM.
  297. if channelNode := v.guildsTree.findNodeByReference(event.ChannelID); channelNode != nil {
  298. v.guildsTree.setNodeLineStyle(channelNode, v.guildsTree.getChannelNodeStyle(event.ChannelID))
  299. }
  300. })
  301. }
  302. func (v *View) clearTypers() {
  303. v.typersMu.Lock()
  304. for _, timer := range v.typers {
  305. timer.Stop()
  306. }
  307. clear(v.typers)
  308. v.typersMu.Unlock()
  309. v.updateFooter()
  310. }
  311. func (v *View) addTyper(userID discord.UserID) {
  312. v.typersMu.Lock()
  313. typer, ok := v.typers[userID]
  314. if ok {
  315. typer.Reset(typingDuration)
  316. } else {
  317. v.typers[userID] = time.AfterFunc(typingDuration, func() {
  318. v.removeTyper(userID)
  319. })
  320. }
  321. v.typersMu.Unlock()
  322. v.updateFooter()
  323. }
  324. func (v *View) removeTyper(userID discord.UserID) {
  325. v.typersMu.Lock()
  326. if typer, ok := v.typers[userID]; ok {
  327. typer.Stop()
  328. delete(v.typers, userID)
  329. }
  330. v.typersMu.Unlock()
  331. v.updateFooter()
  332. }
  333. func (v *View) updateFooter() {
  334. selectedChannel := v.SelectedChannel()
  335. if selectedChannel == nil {
  336. return
  337. }
  338. guildID := selectedChannel.GuildID
  339. v.typersMu.RLock()
  340. defer v.typersMu.RUnlock()
  341. var footer string
  342. if len(v.typers) > 0 {
  343. var names []string
  344. for userID := range v.typers {
  345. var name string
  346. if guildID.IsValid() {
  347. member, err := v.state.Cabinet.Member(guildID, userID)
  348. if err != nil {
  349. slog.Error("failed to get member from state", "err", err, "guild_id", guildID, "user_id", userID)
  350. continue
  351. }
  352. if member.Nick != "" {
  353. name = member.Nick
  354. } else {
  355. name = member.User.DisplayOrUsername()
  356. }
  357. } else {
  358. for _, recipient := range selectedChannel.DMRecipients {
  359. if recipient.ID == userID {
  360. name = recipient.DisplayOrUsername()
  361. break
  362. }
  363. }
  364. }
  365. if name != "" {
  366. names = append(names, name)
  367. }
  368. }
  369. switch len(names) {
  370. case 1:
  371. footer = fmt.Sprintf("%s is typing...", names[0])
  372. case 2:
  373. footer = fmt.Sprintf("%s and %s are typing...", names[0], names[1])
  374. case 3:
  375. footer = fmt.Sprintf("%s, %s, and %s are typing...", names[0], names[1], names[2])
  376. default:
  377. footer = "Several people are typing..."
  378. }
  379. }
  380. go v.app.QueueUpdateDraw(func() { v.messagesList.SetFooter(footer) })
  381. }