application.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package cmd
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "github.com/ayn2op/discordo/internal/config"
  6. "github.com/ayn2op/discordo/internal/consts"
  7. "github.com/ayn2op/discordo/internal/login"
  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. )
  17. type application struct {
  18. cfg *config.Config
  19. *tview.Application
  20. pages *tview.Pages
  21. flex *tview.Flex
  22. guildsTree *guildsTree
  23. messagesText *messagesText
  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. messagesText: newMessagesText(cfg),
  34. messageInput: newMessageInput(cfg),
  35. }
  36. app.EnableMouse(cfg.Mouse)
  37. app.SetInputCapture(app.onInputCapture)
  38. app.flex.SetInputCapture(app.onFlexInputCapture)
  39. return app
  40. }
  41. func (a *application) show(token string) error {
  42. if token == "" {
  43. loginForm := login.NewForm(a.cfg, func(token string) {
  44. if err := a.show(token); err != nil {
  45. slog.Error("failed to show app", "err", err)
  46. return
  47. }
  48. })
  49. a.SetRoot(loginForm, true)
  50. } else {
  51. if err := openState(token); err != nil {
  52. return err
  53. }
  54. a.init()
  55. a.SetRoot(a.pages, true)
  56. }
  57. return nil
  58. }
  59. func (a *application) run(token string) error {
  60. if err := a.show(token); err != nil {
  61. return err
  62. }
  63. if err := a.Run(); err != nil {
  64. return fmt.Errorf("failed to run application: %w", err)
  65. }
  66. return nil
  67. }
  68. func (a *application) quit() {
  69. if discordState != nil {
  70. if err := discordState.Close(); err != nil {
  71. slog.Error("failed to close the session", "err", err)
  72. }
  73. }
  74. a.Stop()
  75. }
  76. func (a *application) init() {
  77. a.pages.ClearPages()
  78. a.flex.Clear()
  79. right := tview.NewFlex().
  80. SetDirection(tview.FlexRow).
  81. AddItem(a.messagesText, 0, 1, false).
  82. AddItem(a.messageInput, 3, 1, false)
  83. // The guilds tree is always focused first at start-up.
  84. a.flex.
  85. AddItem(a.guildsTree, 0, 1, true).
  86. AddItem(right, 0, 4, false)
  87. a.pages.AddAndSwitchToPage(flexPageName, a.flex, true)
  88. }
  89. func (a *application) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
  90. switch event.Name() {
  91. case a.cfg.Keys.Quit:
  92. a.quit()
  93. return nil
  94. case "Ctrl+C":
  95. // https://github.com/ayn2op/tview/blob/a64fc48d7654432f71922c8b908280cdb525805c/application.go#L153
  96. return tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone)
  97. }
  98. return event
  99. }
  100. func (a *application) onFlexInputCapture(event *tcell.EventKey) *tcell.EventKey {
  101. switch event.Name() {
  102. case a.cfg.Keys.FocusGuildsTree:
  103. a.messageInput.removeMentionsList()
  104. a.SetFocus(app.guildsTree)
  105. return nil
  106. case a.cfg.Keys.FocusMessagesText:
  107. a.messageInput.removeMentionsList()
  108. a.SetFocus(app.messagesText)
  109. return nil
  110. case a.cfg.Keys.FocusMessageInput:
  111. a.SetFocus(app.messageInput)
  112. return nil
  113. case a.cfg.Keys.Logout:
  114. a.quit()
  115. if err := keyring.Delete(consts.Name, "token"); 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. // The guilds tree is visible if the numbers of items is two.
  122. if a.flex.GetItemCount() == 2 {
  123. a.flex.RemoveItem(a.guildsTree)
  124. if a.guildsTree.HasFocus() {
  125. a.SetFocus(a.flex)
  126. }
  127. } else {
  128. a.init()
  129. a.SetFocus(a.guildsTree)
  130. }
  131. return nil
  132. }
  133. return event
  134. }