Switch forms to use advanced inputfields and use user-friendly panics for UI errors

This commit is contained in:
Tulir Asokan 2018-03-19 10:57:31 +02:00
parent 7a4b108b37
commit 43127dad0f
3 changed files with 15 additions and 8 deletions

View File

@ -99,6 +99,7 @@ func (gmx *Gomuks) Recover() {
} }
func (gmx *Gomuks) Start() { func (gmx *Gomuks) Start() {
defer gmx.Recover()
if err := gmx.app.Run(); err != nil { if err := gmx.app.Run(); err != nil {
panic(err) panic(err)
} }

View File

@ -28,11 +28,13 @@ func (ui *GomuksUI) NewLoginView() tview.Primitive {
hs = "https://matrix.org" hs = "https://matrix.org"
} }
homeserver := widget.NewAdvancedInputField().SetLabel("Homeserver").SetText(hs).SetFieldWidth(30)
username := widget.NewAdvancedInputField().SetLabel("Username").SetText(ui.gmx.Config().MXID).SetFieldWidth(30)
password := widget.NewAdvancedInputField().SetLabel("Password").SetMaskCharacter('*').SetFieldWidth(30)
ui.loginView = tview.NewForm() ui.loginView = tview.NewForm()
ui.loginView. ui.loginView.
AddInputField("Homeserver", hs, 30, nil, nil). AddFormItem(homeserver).AddFormItem(username).AddFormItem(password).
AddInputField("Username", ui.gmx.Config().MXID, 30, nil, nil).
AddPasswordField("Password", "", 30, '*', nil).
AddButton("Log in", ui.login). AddButton("Log in", ui.login).
AddButton("Quit", ui.gmx.Stop). AddButton("Quit", ui.gmx.Stop).
SetButtonsAlign(tview.AlignCenter). SetButtonsAlign(tview.AlignCenter).
@ -41,9 +43,9 @@ func (ui *GomuksUI) NewLoginView() tview.Primitive {
} }
func (ui *GomuksUI) login() { func (ui *GomuksUI) login() {
hs := ui.loginView.GetFormItem(0).(*tview.InputField).GetText() hs := ui.loginView.GetFormItem(0).(*widget.AdvancedInputField).GetText()
mxid := ui.loginView.GetFormItem(1).(*tview.InputField).GetText() mxid := ui.loginView.GetFormItem(1).(*widget.AdvancedInputField).GetText()
password := ui.loginView.GetFormItem(2).(*tview.InputField).GetText() password := ui.loginView.GetFormItem(2).(*widget.AdvancedInputField).GetText()
debug.Printf("Logging into %s as %s...", hs, mxid) debug.Printf("Logging into %s as %s...", hs, mxid)
ui.gmx.Config().HS = hs ui.gmx.Config().HS = hs

View File

@ -75,7 +75,7 @@ type AdvancedInputField struct {
changed func(text string) changed func(text string)
// An optional function which is called when the user indicated that they // An optional function which is called when the user indicated that they
// are done entering text. The key which was pressed is provided (enter or escape). // are done entering text. The key which was pressed is provided (enter, tab, backtab or escape).
done func(tcell.Key) done func(tcell.Key)
// An optional function which is called when the user presses tab. // An optional function which is called when the user presses tab.
@ -200,6 +200,8 @@ func (field *AdvancedInputField) SetChangedFunc(handler func(text string)) *Adva
// //
// - KeyEnter: Done entering text. // - KeyEnter: Done entering text.
// - KeyEscape: Abort text input. // - KeyEscape: Abort text input.
// - KeyTab: Tab
// - KeyBacktab: Shift + Tab
func (field *AdvancedInputField) SetDoneFunc(handler func(key tcell.Key)) *AdvancedInputField { func (field *AdvancedInputField) SetDoneFunc(handler func(key tcell.Key)) *AdvancedInputField {
field.done = handler field.done = handler
return field return field
@ -435,8 +437,10 @@ func (field *AdvancedInputField) InputHandler() func(event *tcell.EventKey, setF
if oldWidth != newWidth { if oldWidth != newWidth {
field.cursorOffset += newWidth - oldWidth field.cursorOffset += newWidth - oldWidth
} }
break
} }
case tcell.KeyEnter, tcell.KeyEscape: // We're done. fallthrough
case tcell.KeyEnter, tcell.KeyEscape, tcell.KeyBacktab: // We're done.
if field.done != nil { if field.done != nil {
field.done(key) field.done(key)
} }