form.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. buttonStyle := f.cfg.Theme.Dialog.Style.Style
  69. if bg != tcell.ColorDefault {
  70. modal.SetBackgroundColor(bg)
  71. buttonStyle = buttonStyle.Background(bg)
  72. }
  73. fg := f.cfg.Theme.Dialog.Style.GetForeground()
  74. if fg != tcell.ColorDefault {
  75. modal.SetTextColor(fg)
  76. buttonStyle = buttonStyle.Foreground(fg)
  77. }
  78. // Keep button styles aligned with dialog content without hiding text.
  79. modal.SetButtonStyle(buttonStyle)
  80. modal.SetButtonActivatedStyle(buttonStyle)
  81. }
  82. f.
  83. AddLayer(
  84. ui.Centered(modal, 0, 0),
  85. layers.WithName(errorLayerName),
  86. layers.WithResize(true),
  87. layers.WithVisible(true),
  88. layers.WithOverlay(),
  89. ).
  90. SendToFront(errorLayerName)
  91. }
  92. func (f *Form) loginWithQR() {
  93. qr := newQRLogin(f.app, f.cfg, func(token string, err error) {
  94. if err != nil {
  95. f.onError(err)
  96. return
  97. }
  98. if token == "" {
  99. f.RemoveLayer(qrLayerName)
  100. return
  101. }
  102. go keyring.SetToken(token)
  103. f.RemoveLayer(qrLayerName)
  104. if f.done != nil {
  105. f.done(token)
  106. }
  107. })
  108. f.AddLayer(qr, layers.WithName(qrLayerName), layers.WithResize(true), layers.WithVisible(true))
  109. qr.start()
  110. }