application.go 3.4 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. flexPage int
  21. autocompletePage int
  22. }
  23. func newApplication(cfg *config.Config) *application {
  24. app := &application{
  25. Application: tview.NewApplication(),
  26. cfg: cfg,
  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) init() {
  66. a.pages.Clear()
  67. a.flex.Clear()
  68. right := tview.NewFlex()
  69. right.SetDirection(tview.FlexRow)
  70. right.AddItem(a.messagesText, 0, 1, false)
  71. right.AddItem(a.messageInput, 3, 1, false)
  72. // The guilds tree is always focused first at start-up.
  73. a.flex.AddItem(a.guildsTree, 0, 1, true)
  74. a.flex.AddItem(right, 0, 4, false)
  75. a.flexPage = a.pages.AddAndSwitchToPage(a.flex, true)
  76. a.autocompletePage = a.pages.AddPage(a.messageInput.autocomplete, false, false)
  77. }
  78. func (a *application) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
  79. switch event.Name() {
  80. case a.cfg.Keys.Quit:
  81. if discordState != nil {
  82. if err := discordState.Close(); err != nil {
  83. slog.Error("failed to close the session", "err", err)
  84. }
  85. }
  86. a.Stop()
  87. case "Ctrl+C":
  88. // https://github.com/ayn2op/tview/blob/a64fc48d7654432f71922c8b908280cdb525805c/application.go#L153
  89. return tcell.NewEventKey(tcell.KeyCtrlC, 0, tcell.ModNone)
  90. }
  91. return event
  92. }
  93. func (a *application) onFlexInputCapture(event *tcell.EventKey) *tcell.EventKey {
  94. switch event.Name() {
  95. case a.cfg.Keys.FocusGuildsTree:
  96. a.pages.HidePage(a.autocompletePage)
  97. a.SetFocus(app.guildsTree)
  98. return nil
  99. case a.cfg.Keys.FocusMessagesText:
  100. a.pages.HidePage(a.autocompletePage)
  101. a.SetFocus(app.messagesText)
  102. return nil
  103. case a.cfg.Keys.FocusMessageInput:
  104. a.SetFocus(app.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. }