application.go 3.3 KB

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