application.go 3.4 KB

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