application.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package cmd
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "strings"
  6. "github.com/ayn2op/discordo/internal/config"
  7. "github.com/ayn2op/discordo/internal/keyring"
  8. "github.com/ayn2op/discordo/internal/login"
  9. "github.com/ayn2op/discordo/internal/ui"
  10. "github.com/ayn2op/tview"
  11. "github.com/gdamore/tcell/v2"
  12. "golang.design/x/clipboard"
  13. )
  14. const (
  15. flexPageName = "flex"
  16. mentionsListPageName = "mentionsList"
  17. attachmentsListPageName = "attachmentsList"
  18. modalPageName = "modalPageName"
  19. )
  20. type application struct {
  21. cfg *config.Config
  22. *tview.Application
  23. pages *tview.Pages
  24. flex *tview.Flex
  25. guildsTree *guildsTree
  26. messagesList *messagesList
  27. messageInput *messageInput
  28. }
  29. func newApplication(cfg *config.Config) *application {
  30. app := &application{
  31. cfg: cfg,
  32. Application: tview.NewApplication(),
  33. pages: tview.NewPages(),
  34. flex: tview.NewFlex(),
  35. guildsTree: newGuildsTree(cfg),
  36. messagesList: newMessagesList(cfg),
  37. messageInput: newMessageInput(cfg),
  38. }
  39. if err := clipboard.Init(); err != nil {
  40. app.onError("Failed to init clipboard", err)
  41. }
  42. app.pages.SetInputCapture(app.onPagesInputCapture)
  43. app.
  44. EnableMouse(cfg.Mouse).
  45. SetInputCapture(app.onInputCapture).
  46. EnablePaste(true)
  47. return app
  48. }
  49. func (a *application) run(token string) error {
  50. if token == "" {
  51. loginForm := login.NewForm(a.Application, a.cfg, func(token string) {
  52. if err := a.run(token); err != nil {
  53. slog.Error("failed to run application", "err", err)
  54. }
  55. })
  56. return a.SetRoot(loginForm, true).Run()
  57. }
  58. if err := openState(token); err != nil {
  59. return err
  60. }
  61. a.init()
  62. return a.SetRoot(a.pages, true).Run()
  63. }
  64. func (a *application) quit() {
  65. if discordState != nil {
  66. if err := discordState.Close(); err != nil {
  67. slog.Error("failed to close the session", "err", err)
  68. }
  69. }
  70. a.Stop()
  71. }
  72. func (a *application) init() {
  73. a.pages.Clear()
  74. a.flex.Clear()
  75. right := tview.NewFlex().
  76. SetDirection(tview.FlexRow).
  77. AddItem(a.messagesList, 0, 1, false).
  78. AddItem(a.messageInput, 3, 1, false)
  79. // The guilds tree is always focused first at start-up.
  80. a.flex.
  81. AddItem(a.guildsTree, 0, 1, true).
  82. AddItem(right, 0, 4, false)
  83. a.pages.AddAndSwitchToPage(flexPageName, a.flex, true)
  84. }
  85. func (a *application) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
  86. switch event.Name() {
  87. case a.cfg.Keys.Quit:
  88. a.quit()
  89. return nil
  90. case "Ctrl+C":
  91. // https://github.com/ayn2op/tview/blob/a64fc48d7654432f71922c8b908280cdb525805c/application.go#L153
  92. return tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone)
  93. }
  94. return event
  95. }
  96. func (a *application) onPagesInputCapture(event *tcell.EventKey) *tcell.EventKey {
  97. switch event.Name() {
  98. case a.cfg.Keys.FocusGuildsTree:
  99. a.messageInput.removeMentionsList()
  100. a.focusGuildsTree()
  101. return nil
  102. case a.cfg.Keys.FocusMessagesList:
  103. a.messageInput.removeMentionsList()
  104. a.SetFocus(a.messagesList)
  105. return nil
  106. case a.cfg.Keys.FocusMessageInput:
  107. a.focusMessageInput()
  108. return nil
  109. case a.cfg.Keys.FocusPrevious:
  110. a.focusPrevious()
  111. return nil
  112. case a.cfg.Keys.FocusNext:
  113. a.focusNext()
  114. return nil
  115. case a.cfg.Keys.Logout:
  116. a.quit()
  117. if err := keyring.DeleteToken(); err != nil {
  118. a.onError("Failed to delete token from keyring", err)
  119. return nil
  120. }
  121. return nil
  122. case a.cfg.Keys.ToggleGuildsTree:
  123. a.toggleGuildsTree()
  124. return nil
  125. }
  126. return event
  127. }
  128. func (a *application) toggleGuildsTree() {
  129. // The guilds tree is visible if the number of items is two.
  130. if a.flex.GetItemCount() == 2 {
  131. a.flex.RemoveItem(a.guildsTree)
  132. if a.guildsTree.HasFocus() {
  133. a.SetFocus(a.flex)
  134. }
  135. } else {
  136. a.init()
  137. a.SetFocus(a.guildsTree)
  138. }
  139. }
  140. func (a *application) focusGuildsTree() bool {
  141. // The guilds tree is not hidden if the number of items is two.
  142. if a.flex.GetItemCount() == 2 {
  143. a.SetFocus(a.guildsTree)
  144. return true
  145. }
  146. return false
  147. }
  148. func (a *application) focusMessageInput() bool {
  149. if !a.messageInput.GetDisabled() {
  150. a.SetFocus(a.messageInput)
  151. return true
  152. }
  153. return false
  154. }
  155. func (a *application) focusPrevious() {
  156. switch a.GetFocus() {
  157. case a.guildsTree:
  158. a.SetFocus(a.messageInput)
  159. case a.messagesList: // Handle both a.messagesList and a.flex as well as other edge cases (if there is).
  160. if ok := a.focusGuildsTree(); !ok {
  161. a.SetFocus(a.messageInput)
  162. }
  163. case a.messageInput:
  164. a.SetFocus(a.messagesList)
  165. }
  166. }
  167. func (a *application) focusNext() {
  168. switch a.GetFocus() {
  169. case a.guildsTree:
  170. a.SetFocus(a.messagesList)
  171. case a.messagesList:
  172. a.SetFocus(a.messageInput)
  173. case a.messageInput: // Handle both a.messageInput and a.flex as well as other edge cases (if there is).
  174. if ok := a.focusGuildsTree(); !ok {
  175. a.SetFocus(a.messagesList)
  176. }
  177. }
  178. }
  179. func (a *application) onError(msg string, err error, info ...any) {
  180. slog.Error(msg, append(info, "err", err)...)
  181. a.showErrorModal(msg, err.Error(), info...)
  182. }
  183. func (a *application) showModal(title, prompt string, buttons []string, onDone func(label string)) {
  184. previousFocus := a.GetFocus()
  185. modal := tview.NewModal()
  186. modal.Box = ui.ConfigureBox(modal.Box, &a.cfg.Theme)
  187. modal.
  188. SetText(prompt).
  189. AddButtons(buttons).
  190. SetDoneFunc(func(_ int, buttonLabel string) {
  191. a.pages.
  192. RemovePage(modalPageName).
  193. SwitchToPage(flexPageName)
  194. a.SetFocus(previousFocus)
  195. if onDone != nil {
  196. onDone(buttonLabel)
  197. }
  198. }).
  199. SetTitle(title).
  200. SetTitleAlignment(tview.AlignmentCenter)
  201. a.pages.
  202. AddAndSwitchToPage(modalPageName, ui.Centered(modal, 0, 0), true).
  203. ShowPage(flexPageName)
  204. a.SetFocus(modal)
  205. }
  206. func (a *application) showConfirmModal(prompt string, buttons []string, onDone func(label string)) {
  207. a.showModal("", prompt, buttons, onDone)
  208. }
  209. func (a *application) showErrorModal(msg, err string, info ...any) {
  210. a.showModal("[ ERROR ]", msg+"\nReason: "+err, []string{"Copy", "OK"}, func(label string) {
  211. if label != "Copy" {
  212. return
  213. }
  214. res := &strings.Builder{}
  215. fmt.Fprintf(res, "%s\nReason: %s", msg, err)
  216. for i := 0; i < len(info)-1; i += 2 {
  217. fmt.Fprintf(res, "\n%v: %#v", info[i], info[i+1])
  218. }
  219. // Do as log/slog does. "!BADKEY" for last odd argument.
  220. if (len(info) % 2) == 1 {
  221. fmt.Fprintf(res, "\n!BADKEY: %#v", info[len(info)-1])
  222. }
  223. go clipboard.Write(clipboard.FmtText, []byte(res.String()))
  224. })
  225. }
  226. func (a *application) showInfoModal(text string) {
  227. a.showModal("[ INFO ]", text, []string{"OK"}, nil)
  228. }