application.go 5.1 KB

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