application.go 3.3 KB

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