application.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.flex.SetInputCapture(app.onFlexInputCapture)
  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.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) toggleGuildsTree() {
  91. // The guilds tree is visible if the number of items is two.
  92. if a.flex.GetItemCount() == 2 {
  93. a.flex.RemoveItem(a.guildsTree)
  94. if a.guildsTree.HasFocus() {
  95. a.SetFocus(a.flex)
  96. }
  97. } else {
  98. a.init()
  99. a.SetFocus(a.guildsTree)
  100. }
  101. }
  102. func (a *application) onFlexInputCapture(event *tcell.EventKey) *tcell.EventKey {
  103. switch event.Name() {
  104. case a.cfg.Keys.FocusGuildsTree:
  105. a.messageInput.removeMentionsList()
  106. // The guilds tree is not hidden if the number of items is two.
  107. if a.flex.GetItemCount() == 2 {
  108. a.SetFocus(a.guildsTree)
  109. }
  110. return nil
  111. case a.cfg.Keys.FocusMessagesList:
  112. a.messageInput.removeMentionsList()
  113. a.SetFocus(a.messagesList)
  114. return nil
  115. case a.cfg.Keys.FocusMessageInput:
  116. a.SetFocus(a.messageInput)
  117. return nil
  118. case a.cfg.Keys.Logout:
  119. a.quit()
  120. if err := keyring.Delete(consts.Name, "token"); err != nil {
  121. slog.Error("failed to delete token from keyring", "err", err)
  122. return nil
  123. }
  124. return nil
  125. case a.cfg.Keys.ToggleGuildsTree:
  126. a.toggleGuildsTree()
  127. return nil
  128. }
  129. return event
  130. }
  131. func (a *application) showConfirmModal(prompt string, buttons []string, onDone func(label string)) {
  132. previousFocus := a.GetFocus()
  133. modal := tview.NewModal().
  134. SetText(prompt).
  135. AddButtons(buttons).
  136. SetDoneFunc(func(_ int, buttonLabel string) {
  137. a.pages.RemovePage(confirmModalPageName).SwitchToPage(flexPageName)
  138. a.SetFocus(previousFocus)
  139. if onDone != nil {
  140. onDone(buttonLabel)
  141. }
  142. })
  143. a.pages.
  144. AddAndSwitchToPage(confirmModalPageName, ui.Centered(modal, 0, 0), true).
  145. ShowPage(flexPageName)
  146. }