model.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package root
  2. import (
  3. "os"
  4. "github.com/ayn2op/discordo/internal/config"
  5. "github.com/ayn2op/discordo/internal/consts"
  6. "github.com/ayn2op/discordo/internal/ui/chat"
  7. "github.com/ayn2op/discordo/internal/ui/login"
  8. "github.com/ayn2op/discordo/internal/ui/login/qr"
  9. "github.com/ayn2op/discordo/internal/ui/login/token"
  10. "github.com/ayn2op/tview"
  11. "github.com/ayn2op/tview/help"
  12. "github.com/ayn2op/tview/keybind"
  13. "github.com/gdamore/tcell/v3"
  14. )
  15. const tokenEnvVarKey = "DISCORDO_TOKEN"
  16. type Model struct {
  17. app *tview.Application
  18. rootFlex *tview.Flex // inner + help
  19. inner tview.Primitive
  20. help *help.Help
  21. cfg *config.Config
  22. }
  23. func NewModel(cfg *config.Config, app *tview.Application) *Model {
  24. m := &Model{
  25. app: app,
  26. rootFlex: tview.NewFlex(),
  27. help: help.New(),
  28. cfg: cfg,
  29. }
  30. m.rootFlex.SetDirection(tview.FlexRow)
  31. styles := help.DefaultStyles()
  32. styles.ShortKeyStyle = cfg.Theme.Help.ShortKeyStyle.Style
  33. styles.ShortDescStyle = cfg.Theme.Help.ShortDescStyle.Style
  34. styles.FullKeyStyle = cfg.Theme.Help.FullKeyStyle.Style
  35. styles.FullDescStyle = cfg.Theme.Help.FullDescStyle.Style
  36. m.help.SetStyles(styles)
  37. m.help.SetKeyMap(m)
  38. m.help.SetCompactModifiers(cfg.Help.CompactModifiers)
  39. m.help.SetShortSeparator(cfg.Help.Separator)
  40. m.help.SetBorderPadding(0, 0, cfg.Help.Padding[0], cfg.Help.Padding[1])
  41. m.buildLayout()
  42. return m
  43. }
  44. func (m *Model) showLogin() tview.Command {
  45. m.inner = login.NewModel(m.cfg)
  46. m.buildLayout()
  47. return tview.Batch(m.inner.HandleEvent(&tview.InitEvent{}), tview.SetFocus(m))
  48. }
  49. func (m *Model) showChat(token string) tview.Command {
  50. m.inner = chat.NewView(m.app, m.cfg, token)
  51. m.buildLayout()
  52. return tview.Batch(m.inner.HandleEvent(&tview.InitEvent{}), tview.SetFocus(m))
  53. }
  54. func (m *Model) buildLayout() {
  55. m.rootFlex.Clear()
  56. if m.inner != nil {
  57. m.rootFlex.AddItem(m.inner, 0, 1, true)
  58. }
  59. m.rootFlex.AddItem(m.help, 1, 0, false)
  60. m.updateHelpHeight()
  61. }
  62. var _ tview.Primitive = (*Model)(nil)
  63. func (m *Model) Draw(screen tcell.Screen) {
  64. m.rootFlex.Draw(screen)
  65. }
  66. func (m *Model) HandleEvent(event tcell.Event) tview.Command {
  67. switch event := event.(type) {
  68. case *tview.InitEvent:
  69. var cmd tview.Command
  70. if token := os.Getenv(tokenEnvVarKey); token != "" {
  71. cmd = tokenCommand(token)
  72. } else {
  73. cmd = getToken()
  74. }
  75. return tview.Batch(
  76. tview.SetTitle(consts.Name),
  77. initClipboard(),
  78. cmd,
  79. )
  80. case *loginEvent:
  81. return m.showLogin()
  82. case *tokenEvent:
  83. return m.showChat(event.token)
  84. case *token.TokenEvent:
  85. return tview.Batch(m.showChat(event.Token), setToken(event.Token))
  86. case *qr.TokenEvent:
  87. return tview.Batch(m.showChat(event.Token), setToken(event.Token))
  88. case *chat.LogoutEvent:
  89. return tview.Batch(
  90. m.showLogin(),
  91. deleteToken(),
  92. )
  93. case *tview.KeyEvent:
  94. switch {
  95. case keybind.Matches(event, m.cfg.Keybinds.ToggleHelp.Keybind):
  96. m.help.SetShowAll(!m.help.ShowAll())
  97. m.updateHelpHeight()
  98. return nil
  99. case keybind.Matches(event, m.cfg.Keybinds.Suspend.Keybind):
  100. m.suspend()
  101. return nil
  102. case keybind.Matches(event, m.cfg.Keybinds.Quit.Keybind):
  103. var innerCmd tview.Command
  104. if m.inner != nil {
  105. innerCmd = m.inner.HandleEvent(&chat.QuitEvent{})
  106. }
  107. return tview.Batch(innerCmd, tview.Quit())
  108. }
  109. }
  110. if m.inner != nil {
  111. return m.inner.HandleEvent(event)
  112. }
  113. return nil
  114. }
  115. func (m *Model) updateHelpHeight() {
  116. height := 1
  117. if m.help.ShowAll() {
  118. height = max(len(m.help.FullHelpLines(m.FullHelp(), 0)), 1)
  119. }
  120. m.rootFlex.ResizeItem(m.help, height, 0)
  121. }
  122. func (m *Model) GetRect() (int, int, int, int) {
  123. return m.rootFlex.GetRect()
  124. }
  125. func (m *Model) SetRect(x int, y int, width int, height int) {
  126. m.rootFlex.SetRect(x, y, width, height)
  127. }
  128. func (m *Model) Focus(delegate func(p tview.Primitive)) {
  129. if m.inner != nil {
  130. delegate(m.inner)
  131. }
  132. }
  133. func (m *Model) HasFocus() bool {
  134. if m.inner != nil {
  135. return m.inner.HasFocus()
  136. }
  137. return true
  138. }
  139. func (m *Model) Blur() {
  140. if m.inner != nil {
  141. m.inner.Blur()
  142. }
  143. }