application.go 3.3 KB

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