form.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 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 clipboard.Write(clipboard.FmtText, []byte(message))
  57. } else {
  58. f.RemoveLayer(errorLayerName)
  59. }
  60. })
  61. {
  62. bg := f.cfg.Theme.Dialog.Style.GetBackground()
  63. buttonStyle := f.cfg.Theme.Dialog.Style.Style
  64. if bg != tcell.ColorDefault {
  65. modal.SetBackgroundColor(bg)
  66. buttonStyle = buttonStyle.Background(bg)
  67. }
  68. fg := f.cfg.Theme.Dialog.Style.GetForeground()
  69. if fg != tcell.ColorDefault {
  70. modal.SetTextColor(fg)
  71. buttonStyle = buttonStyle.Foreground(fg)
  72. }
  73. // Keep button styles aligned with dialog content without hiding text.
  74. modal.SetButtonStyle(buttonStyle)
  75. modal.SetButtonActivatedStyle(buttonStyle)
  76. }
  77. f.
  78. AddLayer(
  79. ui.Centered(modal, 0, 0),
  80. layers.WithName(errorLayerName),
  81. layers.WithResize(true),
  82. layers.WithVisible(true),
  83. layers.WithOverlay(),
  84. ).
  85. SendToFront(errorLayerName)
  86. }
  87. func (f *Form) loginWithQR() {
  88. qr := newQRLogin(f.app, f.cfg, func(token string, err error) {
  89. if err != nil {
  90. f.onError(err)
  91. return
  92. }
  93. if token == "" {
  94. f.RemoveLayer(qrLayerName)
  95. return
  96. }
  97. go keyring.SetToken(token)
  98. f.RemoveLayer(qrLayerName)
  99. f.app.QueueEvent(NewTokenEvent(token))
  100. })
  101. f.AddLayer(qr, layers.WithName(qrLayerName), layers.WithResize(true), layers.WithVisible(true))
  102. qr.start()
  103. }