model.go 3.9 KB

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