application.go 4.8 KB

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