application.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. EnablePaste(true)
  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.focusMessageInput()
  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.DeleteToken(); 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) focusMessageInput() bool {
  143. if !a.messageInput.GetDisabled() {
  144. a.SetFocus(a.messageInput)
  145. return true
  146. }
  147. return false
  148. }
  149. func (a *application) focusPrevious() {
  150. switch a.GetFocus() {
  151. case a.guildsTree:
  152. a.SetFocus(a.messageInput)
  153. case a.messagesList: // Handle both a.messagesList and a.flex as well as other edge cases (if there is).
  154. if ok := a.focusGuildsTree(); !ok {
  155. a.SetFocus(a.messageInput)
  156. }
  157. case a.messageInput:
  158. a.SetFocus(a.messagesList)
  159. }
  160. }
  161. func (a *application) focusNext() {
  162. switch a.GetFocus() {
  163. case a.guildsTree:
  164. a.SetFocus(a.messagesList)
  165. case a.messagesList:
  166. a.SetFocus(a.messageInput)
  167. case a.messageInput: // Handle both a.messageInput and a.flex as well as other edge cases (if there is).
  168. if ok := a.focusGuildsTree(); !ok {
  169. a.SetFocus(a.messagesList)
  170. }
  171. }
  172. }
  173. func (a *application) showConfirmModal(prompt string, buttons []string, onDone func(label string)) {
  174. previousFocus := a.GetFocus()
  175. modal := tview.NewModal().
  176. SetText(prompt).
  177. AddButtons(buttons).
  178. SetDoneFunc(func(_ int, buttonLabel string) {
  179. a.pages.RemovePage(confirmModalPageName).SwitchToPage(flexPageName)
  180. a.SetFocus(previousFocus)
  181. if onDone != nil {
  182. onDone(buttonLabel)
  183. }
  184. })
  185. a.pages.
  186. AddAndSwitchToPage(confirmModalPageName, ui.Centered(modal, 0, 0), true).
  187. ShowPage(flexPageName)
  188. }