1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-12-23 04:36:49 +00:00

auth fail now preserves entry

This commit is contained in:
diamondburned 2020-10-25 20:36:00 -07:00
parent 8a8423d33f
commit 18c9e9535c

View file

@ -28,6 +28,10 @@ type Dialog struct {
// is authenticated successfully inside the Gtk main thread. // is authenticated successfully inside the Gtk main thread.
func NewDialog(name text.Rich, auther cchat.Authenticator, auth func(cchat.Session)) *Dialog { func NewDialog(name text.Rich, auther cchat.Authenticator, auth func(cchat.Session)) *Dialog {
label, _ := gtk.LabelNew("") label, _ := gtk.LabelNew("")
label.SetMarginStart(10)
label.SetMarginEnd(10)
label.SetMarginTop(10)
label.SetMarginBottom(10)
label.Show() label.Show()
box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0) box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
@ -73,11 +77,6 @@ func (d *Dialog) runOnAuth(ses cchat.Session) {
} }
func (d *Dialog) spin(err error) { func (d *Dialog) spin(err error) {
// Remove old request.
if d.request != nil {
d.body.Remove(d.request)
}
// Print the error. // Print the error.
if err != nil { if err != nil {
d.label.SetMarkup(`<span color="red">` + html.EscapeString(err.Error()) + `</span>`) d.label.SetMarkup(`<span color="red">` + html.EscapeString(err.Error()) + `</span>`)
@ -89,7 +88,19 @@ func (d *Dialog) spin(err error) {
d.stack.SetVisibleChildName("main") d.stack.SetVisibleChildName("main")
d.Dialog.SetSensitive(true) d.Dialog.SetSensitive(true)
d.request = NewRequest(d.Auther.AuthenticateForm()) form := d.Auther.AuthenticateForm()
// See if we need to remove the current request page. We should keep
// everything the same if the key matches, as then forms aren't reset.
if d.request != nil {
if d.request.equalEntries(form) {
return
}
d.body.Remove(d.request)
}
d.request = NewRequest(form)
d.body.Add(d.request) d.body.Add(d.request)
} }
@ -115,8 +126,9 @@ func (d *Dialog) ok(m *dialog.Modal) {
type Request struct { type Request struct {
*gtk.Grid *gtk.Grid
entries []cchat.AuthenticateEntry
labels []*gtk.Label labels []*gtk.Label
entries []Texter texts []Texter
} }
func NewRequest(authEntries []cchat.AuthenticateEntry) *Request { func NewRequest(authEntries []cchat.AuthenticateEntry) *Request {
@ -128,27 +140,44 @@ func NewRequest(authEntries []cchat.AuthenticateEntry) *Request {
req := &Request{ req := &Request{
Grid: grid, Grid: grid,
entries: authEntries,
labels: make([]*gtk.Label, len(authEntries)), labels: make([]*gtk.Label, len(authEntries)),
entries: make([]Texter, len(authEntries)), texts: make([]Texter, len(authEntries)),
} }
for i, authEntry := range authEntries { for i, authEntry := range req.entries {
label, entry := newEntry(authEntry) label, texter := newEntry(authEntry)
req.labels[i] = label req.labels[i] = label
req.entries[i] = entry req.texts[i] = texter
grid.Attach(label, 0, i, 1, 1) grid.Attach(label, 0, i, 1, 1)
grid.Attach(entry, 1, i, 3, 1) // triple the width grid.Attach(texter, 1, i, 3, 1) // triple the width
} }
return req return req
} }
// equalEntries compares the current request with a list of entries. It returns
// false if there are inequalities.
func (r *Request) equalEntries(entries []cchat.AuthenticateEntry) bool {
if len(r.entries) != len(entries) {
return false
}
for i, entry := range r.entries {
if entry != entries[i] {
return false
}
}
return true
}
func (r *Request) values() []string { func (r *Request) values() []string {
var values = make([]string, len(r.entries)) var values = make([]string, len(r.entries))
for i, entry := range r.entries { for i, texter := range r.texts {
values[i] = entry.GetText() values[i] = texter.GetText()
} }
return values return values