view.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package root
  2. import (
  3. "github.com/ayn2op/discordo/internal/config"
  4. "github.com/ayn2op/discordo/internal/consts"
  5. "github.com/ayn2op/discordo/internal/ui/chat"
  6. "github.com/ayn2op/discordo/internal/ui/login"
  7. "github.com/ayn2op/tview"
  8. "github.com/ayn2op/tview/keybind"
  9. "github.com/gdamore/tcell/v3"
  10. )
  11. const tokenEnvVarKey = "DISCORDO_TOKEN"
  12. type View struct {
  13. app *tview.Application
  14. inner tview.Primitive
  15. cfg *config.Config
  16. }
  17. func NewView(cfg *config.Config, app *tview.Application) *View {
  18. return &View{
  19. app: app,
  20. cfg: cfg,
  21. }
  22. }
  23. func (v *View) showLoginView() tview.Command {
  24. v.inner = login.NewForm(v.app, v.cfg)
  25. return v.inner.HandleEvent(tview.NewInitEvent())
  26. }
  27. func (v *View) showChatView(token string) tview.Command {
  28. v.inner = chat.NewView(v.app, v.cfg, token)
  29. return v.inner.HandleEvent(tview.NewInitEvent())
  30. }
  31. var _ tview.Primitive = (*View)(nil)
  32. func (v *View) Draw(screen tcell.Screen) {
  33. if v.inner != nil {
  34. v.inner.Draw(screen)
  35. }
  36. }
  37. func (v *View) HandleEvent(event tcell.Event) tview.Command {
  38. switch event := event.(type) {
  39. case *tview.InitEvent:
  40. return tview.BatchCommand{
  41. tview.SetTitleCommand(consts.Name),
  42. tview.EventCommand(initClipboard),
  43. tview.EventCommand(getToken),
  44. }
  45. case *tokenEvent:
  46. if event.token == "" {
  47. return tview.BatchCommand{v.showLoginView(), tview.SetFocusCommand{Target: v.inner}}
  48. } else {
  49. return tview.BatchCommand{v.showChatView(event.token), tview.SetFocusCommand{Target: v.inner}}
  50. }
  51. case *login.TokenEvent:
  52. return tview.BatchCommand{v.showChatView(event.Token), tview.SetFocusCommand{Target: v.inner}}
  53. case *chat.LogoutEvent:
  54. v.showLoginView()
  55. return tview.BatchCommand{
  56. tview.EventCommand(deleteToken),
  57. tview.SetFocusCommand{Target: v.inner},
  58. }
  59. case *tview.KeyEvent:
  60. switch {
  61. case keybind.Matches(event, v.cfg.Keybinds.Suspend.Keybind):
  62. v.suspend()
  63. return nil
  64. case keybind.Matches(event, v.cfg.Keybinds.Quit.Keybind):
  65. if v.inner != nil {
  66. return v.inner.HandleEvent(chat.NewQuitEvent())
  67. }
  68. return tview.QuitCommand{}
  69. }
  70. }
  71. if v.inner != nil {
  72. return v.inner.HandleEvent(event)
  73. }
  74. return nil
  75. }
  76. func (v *View) GetRect() (int, int, int, int) {
  77. if v.inner != nil {
  78. return v.inner.GetRect()
  79. }
  80. return 0, 0, 0, 0
  81. }
  82. func (v *View) SetRect(x int, y int, width int, height int) {
  83. if v.inner != nil {
  84. v.inner.SetRect(x, y, width, height)
  85. }
  86. }
  87. func (v *View) Focus(delegate func(p tview.Primitive)) {
  88. if v.inner != nil {
  89. delegate(v.inner)
  90. }
  91. }
  92. func (v *View) HasFocus() bool {
  93. if v.inner != nil {
  94. return v.inner.HasFocus()
  95. }
  96. return true
  97. }
  98. func (v *View) Blur() {
  99. if v.inner != nil {
  100. v.inner.Blur()
  101. }
  102. }