form.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/clipboard"
  8. "github.com/ayn2op/discordo/internal/config"
  9. "github.com/ayn2op/discordo/internal/keyring"
  10. "github.com/ayn2op/discordo/internal/ui"
  11. "github.com/ayn2op/tview"
  12. )
  13. const (
  14. formLayerName = "form"
  15. errorLayerName = "error"
  16. qrLayerName = "qr"
  17. )
  18. type Form struct {
  19. *layers.Layers
  20. app *tview.Application
  21. cfg *config.Config
  22. form *tview.Form
  23. }
  24. func NewForm(app *tview.Application, cfg *config.Config) *Form {
  25. f := &Form{
  26. Layers: layers.New(),
  27. app: app,
  28. cfg: cfg,
  29. form: tview.NewForm(),
  30. }
  31. f.form.
  32. AddPasswordField("Token", "", 0, 0, nil).
  33. AddButton("Login", f.login).
  34. AddButton("Login with QR", f.loginWithQR)
  35. f.SetBackgroundLayerStyle(f.cfg.Theme.Dialog.BackgroundStyle.Style)
  36. f.AddLayer(f.form, layers.WithName(formLayerName), layers.WithResize(true), layers.WithVisible(true))
  37. return f
  38. }
  39. func (f *Form) login() {
  40. token := f.form.GetFormItem(0).(*tview.InputField).GetText()
  41. if token == "" {
  42. f.onError(errors.New("token required"))
  43. return
  44. }
  45. go keyring.SetToken(token)
  46. f.app.QueueEvent(NewTokenEvent(token))
  47. }
  48. func (f *Form) onError(err error) {
  49. slog.Error("failed to login", "err", err)
  50. message := err.Error()
  51. modal := tview.NewModal().
  52. SetText(message).
  53. AddButtons([]string{"Copy", "Close"}).
  54. SetDoneFunc(func(buttonIndex int, _ string) {
  55. if buttonIndex == 0 {
  56. go func() {
  57. if err := clipboard.Write(clipboard.FmtText, []byte(message)); err != nil {
  58. slog.Error("failed to copy error message", "err", err)
  59. }
  60. }()
  61. } else {
  62. f.RemoveLayer(errorLayerName)
  63. }
  64. })
  65. {
  66. bg := f.cfg.Theme.Dialog.Style.GetBackground()
  67. buttonStyle := f.cfg.Theme.Dialog.Style.Style
  68. if bg != tcell.ColorDefault {
  69. modal.SetBackgroundColor(bg)
  70. buttonStyle = buttonStyle.Background(bg)
  71. }
  72. fg := f.cfg.Theme.Dialog.Style.GetForeground()
  73. if fg != tcell.ColorDefault {
  74. modal.SetTextColor(fg)
  75. buttonStyle = buttonStyle.Foreground(fg)
  76. }
  77. // Keep button styles aligned with dialog content without hiding text.
  78. modal.SetButtonStyle(buttonStyle)
  79. modal.SetButtonActivatedStyle(buttonStyle)
  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. f.app.QueueEvent(NewTokenEvent(token))
  104. })
  105. f.AddLayer(qr, layers.WithName(qrLayerName), layers.WithResize(true), layers.WithVisible(true))
  106. qr.start()
  107. }