form.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package login
  2. import (
  3. "errors"
  4. "log/slog"
  5. "github.com/ayn2op/tview/layers"
  6. "github.com/gdamore/tcell/v3"
  7. "github.com/ayn2op/discordo/internal/config"
  8. "github.com/ayn2op/discordo/internal/keyring"
  9. "github.com/ayn2op/discordo/internal/ui"
  10. "github.com/ayn2op/tview"
  11. "golang.design/x/clipboard"
  12. )
  13. const (
  14. formLayerName = "form"
  15. errorLayerName = "error"
  16. qrLayerName = "qr"
  17. )
  18. type DoneFn = func(token string)
  19. type Form struct {
  20. *layers.Layers
  21. app *tview.Application
  22. cfg *config.Config
  23. form *tview.Form
  24. done DoneFn
  25. }
  26. func NewForm(app *tview.Application, cfg *config.Config, done DoneFn) *Form {
  27. f := &Form{
  28. Layers: layers.New(),
  29. app: app,
  30. cfg: cfg,
  31. form: tview.NewForm(),
  32. done: done,
  33. }
  34. f.form.
  35. AddPasswordField("Token", "", 0, 0, nil).
  36. AddButton("Login", f.login).
  37. AddButton("Login with QR", f.loginWithQR)
  38. f.SetBackgroundLayerStyle(f.cfg.Theme.Dialog.BackgroundStyle.Style)
  39. f.AddLayer(f.form, layers.WithName(formLayerName), layers.WithResize(true), layers.WithVisible(true))
  40. return f
  41. }
  42. func (f *Form) login() {
  43. token := f.form.GetFormItem(0).(*tview.InputField).GetText()
  44. if token == "" {
  45. f.onError(errors.New("token required"))
  46. return
  47. }
  48. go keyring.SetToken(token)
  49. if f.done != nil {
  50. f.done(token)
  51. }
  52. }
  53. func (f *Form) onError(err error) {
  54. slog.Error("failed to login", "err", err)
  55. message := err.Error()
  56. modal := tview.NewModal().
  57. SetText(message).
  58. AddButtons([]string{"Copy", "Close"}).
  59. SetDoneFunc(func(buttonIndex int, _ string) {
  60. if buttonIndex == 0 {
  61. go clipboard.Write(clipboard.FmtText, []byte(message))
  62. } else {
  63. f.RemoveLayer(errorLayerName)
  64. }
  65. })
  66. {
  67. bg := f.cfg.Theme.Dialog.Style.GetBackground()
  68. if bg != tcell.ColorDefault {
  69. modal.SetBackgroundColor(bg)
  70. modal.SetButtonBackgroundColor(bg)
  71. }
  72. fg := f.cfg.Theme.Dialog.Style.GetForeground()
  73. if fg != tcell.ColorDefault {
  74. modal.SetTextColor(fg)
  75. modal.SetButtonTextColor(fg)
  76. }
  77. // Keep button styles aligned with dialog content without hiding text.
  78. modal.SetButtonStyle(f.cfg.Theme.Dialog.Style.Style)
  79. modal.SetButtonActivatedStyle(f.cfg.Theme.Dialog.Style.Style)
  80. }
  81. f.
  82. AddLayer(
  83. ui.Centered(modal, 0, 0),
  84. layers.WithName(errorLayerName),
  85. layers.WithResize(true),
  86. layers.WithVisible(true),
  87. layers.WithOverlay(),
  88. ).
  89. SendToFront(errorLayerName)
  90. }
  91. func (f *Form) loginWithQR() {
  92. qr := newQRLogin(f.app, f.cfg, func(token string, err error) {
  93. if err != nil {
  94. f.onError(err)
  95. return
  96. }
  97. if token == "" {
  98. f.RemoveLayer(qrLayerName)
  99. return
  100. }
  101. go keyring.SetToken(token)
  102. f.RemoveLayer(qrLayerName)
  103. if f.done != nil {
  104. f.done(token)
  105. }
  106. })
  107. f.AddLayer(qr, layers.WithName(qrLayerName), layers.WithResize(true), layers.WithVisible(true))
  108. qr.start()
  109. }