view.go 9.9 KB

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