app.go 3.0 KB

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