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