view.go 11 KB

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