login-app: Use a pure-Go tty interface instead of curses
This commit is contained in:
parent
af53a37d33
commit
8ab758ac9a
8 changed files with 376 additions and 298 deletions
124
pkg/login-app/cmd/dialog-login.go
Normal file
124
pkg/login-app/cmd/dialog-login.go
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
ui "github.com/VladimirMarkelov/clui"
|
||||
term "github.com/nsf/termbox-go"
|
||||
)
|
||||
|
||||
const (
|
||||
LoginOk = iota
|
||||
LoginCanceled
|
||||
LoginInvalid
|
||||
)
|
||||
|
||||
type LoginDialog struct {
|
||||
View *ui.Window
|
||||
Username string
|
||||
Password string
|
||||
Action int
|
||||
|
||||
result int
|
||||
beforeClose func()
|
||||
onClose func()
|
||||
onCheck func(string, string) bool
|
||||
}
|
||||
|
||||
func CreateLoginDialog(title string) *LoginDialog {
|
||||
dlg := new(LoginDialog)
|
||||
|
||||
sWidth, sHeight := ui.ScreenSize()
|
||||
wWidth, wHeight := 40, 15
|
||||
dlg.View = ui.AddWindow(sWidth/2-wWidth/2, sHeight/2-wHeight/2, wWidth, wHeight, title)
|
||||
ui.WindowManager().BeginUpdate()
|
||||
defer ui.WindowManager().EndUpdate()
|
||||
|
||||
dlg.View.SetModal(true)
|
||||
dlg.View.SetSizable(false)
|
||||
dlg.View.SetTitleButtons(ui.ButtonDefault)
|
||||
dlg.View.SetPack(ui.Vertical)
|
||||
|
||||
userfrm := ui.CreateFrame(dlg.View, 1, 1, ui.BorderNone, ui.Fixed)
|
||||
userfrm.SetPaddings(1, 1)
|
||||
userfrm.SetPack(ui.Horizontal)
|
||||
userfrm.SetGaps(1, 0)
|
||||
ui.CreateLabel(userfrm, ui.AutoSize, ui.AutoSize, " Login", ui.Fixed)
|
||||
edUser := ui.CreateEditField(userfrm, 20, "", 1)
|
||||
|
||||
passfrm := ui.CreateFrame(dlg.View, 1, 1, ui.BorderNone, 1)
|
||||
passfrm.SetPaddings(1, 1)
|
||||
passfrm.SetPack(ui.Horizontal)
|
||||
passfrm.SetGaps(1, 0)
|
||||
ui.CreateLabel(passfrm, ui.AutoSize, ui.AutoSize, "Password", ui.Fixed)
|
||||
edPass := ui.CreateEditField(passfrm, 20, "", 1)
|
||||
edPass.SetPasswordMode(true)
|
||||
|
||||
filler := ui.CreateFrame(dlg.View, 1, 1, ui.BorderNone, 1)
|
||||
filler.SetPack(ui.Horizontal)
|
||||
lbRes := ui.CreateLabel(filler, ui.AutoSize, ui.AutoSize, "", 1)
|
||||
|
||||
blist := ui.CreateFrame(dlg.View, 1, 1, ui.BorderNone, ui.Fixed)
|
||||
blist.SetPack(ui.Horizontal)
|
||||
blist.SetPaddings(1, 1)
|
||||
btnOk := ui.CreateButton(blist, 20, 4, "Authenticate me", 1)
|
||||
|
||||
btnOk.OnClick(func(ev ui.Event) {
|
||||
if dlg.onCheck != nil && !dlg.onCheck(edUser.Title(), edPass.Title()) {
|
||||
lbRes.SetTitle("Invalid username or password")
|
||||
dlg.Action = LoginInvalid
|
||||
return
|
||||
}
|
||||
|
||||
dlg.Action = LoginOk
|
||||
if dlg.onCheck == nil {
|
||||
dlg.Username = edUser.Title()
|
||||
dlg.Password = edPass.Title()
|
||||
}
|
||||
|
||||
if dlg.beforeClose != nil {
|
||||
dlg.beforeClose()
|
||||
}
|
||||
ui.WindowManager().DestroyWindow(dlg.View)
|
||||
ui.WindowManager().BeginUpdate()
|
||||
|
||||
closeFunc := dlg.onClose
|
||||
ui.WindowManager().EndUpdate()
|
||||
if closeFunc != nil {
|
||||
closeFunc()
|
||||
}
|
||||
})
|
||||
|
||||
dlg.View.OnKeyDown(func(ev ui.Event, data interface{}) bool {
|
||||
if ev.Key == term.KeyEnter {
|
||||
if edUser.Title() == "" {
|
||||
ui.ActivateControl(dlg.View, edUser)
|
||||
} else if edPass.Title() == "" {
|
||||
ui.ActivateControl(dlg.View, edPass)
|
||||
} else {
|
||||
if dlg.beforeClose != nil {
|
||||
dlg.beforeClose()
|
||||
}
|
||||
ui.WindowManager().DestroyWindow(dlg.View)
|
||||
ui.WindowManager().BeginUpdate()
|
||||
|
||||
closeFunc := dlg.onClose
|
||||
ui.WindowManager().EndUpdate()
|
||||
if closeFunc != nil {
|
||||
closeFunc()
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}, nil)
|
||||
|
||||
edUser.OnChange(func(ev ui.Event) {
|
||||
lbRes.SetTitle("")
|
||||
})
|
||||
edPass.OnChange(func(ev ui.Event) {
|
||||
lbRes.SetTitle("")
|
||||
})
|
||||
|
||||
ui.ActivateControl(dlg.View, edUser)
|
||||
|
||||
return dlg
|
||||
}
|
||||
Reference in a new issue