view.go 8.4 KB

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