application.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package cmd
  2. import (
  3. "log/slog"
  4. "github.com/ayn2op/discordo/internal/config"
  5. "github.com/ayn2op/discordo/internal/consts"
  6. "github.com/ayn2op/discordo/internal/login"
  7. "github.com/ayn2op/discordo/internal/ui"
  8. "github.com/ayn2op/tview"
  9. "github.com/gdamore/tcell/v2"
  10. "github.com/zalando/go-keyring"
  11. )
  12. const (
  13. flexPageName = "flex"
  14. mentionsListPageName = "mentionsList"
  15. attachmentsListPageName = "attachmentsList"
  16. confirmModalPageName = "confirmModal"
  17. )
  18. type application struct {
  19. cfg *config.Config
  20. *tview.Application
  21. pages *tview.Pages
  22. flex *tview.Flex
  23. guildsTree *guildsTree
  24. messagesList *messagesList
  25. messageInput *messageInput
  26. }
  27. func newApplication(cfg *config.Config) *application {
  28. app := &application{
  29. cfg: cfg,
  30. Application: tview.NewApplication(),
  31. pages: tview.NewPages(),
  32. flex: tview.NewFlex(),
  33. guildsTree: newGuildsTree(cfg),
  34. messagesList: newMessagesList(cfg),
  35. messageInput: newMessageInput(cfg),
  36. }
  37. app.pages.SetInputCapture(app.onPagesInputCapture)
  38. app.
  39. EnableMouse(cfg.Mouse).
  40. SetInputCapture(app.onInputCapture)
  41. return app
  42. }
  43. func (a *application) run(token string) error {
  44. if token == "" {
  45. loginForm := login.NewForm(a.Application, a.cfg, func(token string) {
  46. if err := a.run(token); err != nil {
  47. slog.Error("failed to run application", "err", err)
  48. }
  49. })
  50. return a.SetRoot(loginForm, true).Run()
  51. }
  52. if err := openState(token); err != nil {
  53. return err
  54. }
  55. a.init()
  56. return a.SetRoot(a.pages, true).Run()
  57. }
  58. func (a *application) quit() {
  59. if discordState != nil {
  60. if err := discordState.Close(); err != nil {
  61. slog.Error("failed to close the session", "err", err)
  62. }
  63. }
  64. a.Stop()
  65. }
  66. func (a *application) init() {
  67. a.pages.Clear()
  68. a.flex.Clear()
  69. right := tview.NewFlex().
  70. SetDirection(tview.FlexRow).
  71. AddItem(a.messagesList, 0, 1, false).
  72. AddItem(a.messageInput, 3, 1, false)
  73. // The guilds tree is always focused first at start-up.
  74. a.flex.
  75. AddItem(a.guildsTree, 0, 1, true).
  76. AddItem(right, 0, 4, false)
  77. a.pages.AddAndSwitchToPage(flexPageName, a.flex, true)
  78. }
  79. func (a *application) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
  80. switch event.Name() {
  81. case a.cfg.Keys.Quit:
  82. a.quit()
  83. return nil
  84. case "Ctrl+C":
  85. // https://github.com/ayn2op/tview/blob/a64fc48d7654432f71922c8b908280cdb525805c/application.go#L153
  86. return tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone)
  87. }
  88. return event
  89. }
  90. func (a *application) onPagesInputCapture(event *tcell.EventKey) *tcell.EventKey {
  91. switch event.Name() {
  92. case a.cfg.Keys.FocusGuildsTree:
  93. a.messageInput.removeMentionsList()
  94. _ = a.focusGuildsTree()
  95. return nil
  96. case a.cfg.Keys.FocusMessagesList:
  97. a.messageInput.removeMentionsList()
  98. a.SetFocus(a.messagesList)
  99. return nil
  100. case a.cfg.Keys.FocusMessageInput:
  101. a.SetFocus(a.messageInput)
  102. return nil
  103. case a.cfg.Keys.FocusPrevious:
  104. a.focusPrevious()
  105. return nil
  106. case a.cfg.Keys.FocusNext:
  107. a.focusNext()
  108. return nil
  109. case a.cfg.Keys.Logout:
  110. a.quit()
  111. if err := keyring.Delete(consts.Name, "token"); err != nil {
  112. slog.Error("failed to delete token from keyring", "err", err)
  113. return nil
  114. }
  115. return nil
  116. case a.cfg.Keys.ToggleGuildsTree:
  117. a.toggleGuildsTree()
  118. return nil
  119. }
  120. return event
  121. }
  122. func (a *application) toggleGuildsTree() {
  123. // The guilds tree is visible if the number of items is two.
  124. if a.flex.GetItemCount() == 2 {
  125. a.flex.RemoveItem(a.guildsTree)
  126. if a.guildsTree.HasFocus() {
  127. a.SetFocus(a.flex)
  128. }
  129. } else {
  130. a.init()
  131. a.SetFocus(a.guildsTree)
  132. }
  133. }
  134. func (a *application) focusGuildsTree() bool {
  135. // The guilds tree is not hidden if the number of items is two.
  136. if a.flex.GetItemCount() == 2 {
  137. a.SetFocus(a.guildsTree)
  138. return true
  139. }
  140. return false
  141. }
  142. func (a *application) focusPrevious() {
  143. switch a.GetFocus() {
  144. case a.guildsTree:
  145. a.SetFocus(a.messageInput)
  146. case a.messagesList: // Handle both a.messagesList and a.flex as well as other edge cases (if there is).
  147. if ok := a.focusGuildsTree(); !ok {
  148. a.SetFocus(a.messageInput)
  149. }
  150. case a.messageInput:
  151. a.SetFocus(a.messagesList)
  152. }
  153. }
  154. func (a *application) focusNext() {
  155. switch a.GetFocus() {
  156. case a.guildsTree:
  157. a.SetFocus(a.messagesList)
  158. case a.messagesList:
  159. a.SetFocus(a.messageInput)
  160. case a.messageInput: // Handle both a.messageInput and a.flex as well as other edge cases (if there is).
  161. if ok := a.focusGuildsTree(); !ok {
  162. a.SetFocus(a.messagesList)
  163. }
  164. }
  165. }
  166. func (a *application) showConfirmModal(prompt string, buttons []string, onDone func(label string)) {
  167. previousFocus := a.GetFocus()
  168. modal := tview.NewModal().
  169. SetText(prompt).
  170. AddButtons(buttons).
  171. SetDoneFunc(func(_ int, buttonLabel string) {
  172. a.pages.RemovePage(confirmModalPageName).SwitchToPage(flexPageName)
  173. a.SetFocus(previousFocus)
  174. if onDone != nil {
  175. onDone(buttonLabel)
  176. }
  177. })
  178. a.pages.
  179. AddAndSwitchToPage(confirmModalPageName, ui.Centered(modal, 0, 0), true).
  180. ShowPage(flexPageName)
  181. }