app.go 3.0 KB

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