80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"math"
|
||
|
"time"
|
||
|
|
||
|
ui "github.com/VladimirMarkelov/clui"
|
||
|
)
|
||
|
|
||
|
type CheckDialog struct {
|
||
|
View *ui.Window
|
||
|
|
||
|
result int
|
||
|
beforeClose func(ui.Event)
|
||
|
onClose func()
|
||
|
}
|
||
|
|
||
|
func CreateCheckDialog(title, username, password string) *CheckDialog {
|
||
|
dlg := new(CheckDialog)
|
||
|
|
||
|
sWidth, sHeight := ui.ScreenSize()
|
||
|
wWidth, wHeight := 40, 5
|
||
|
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)
|
||
|
|
||
|
textfrm := ui.CreateFrame(dlg.View, 1, 1, ui.BorderNone, ui.Fixed)
|
||
|
textfrm.SetPaddings(1, 1)
|
||
|
textfrm.SetPack(ui.Vertical)
|
||
|
textfrm.SetGaps(2, 1)
|
||
|
ui.CreateLabel(textfrm, ui.AutoSize, ui.AutoSize, " Please wait", ui.Fixed)
|
||
|
|
||
|
progress := ui.CreateProgressBar(textfrm, ui.AutoSize, ui.AutoSize, 1)
|
||
|
progress.SetLimits(0, 100)
|
||
|
|
||
|
ui.CreateLabel(textfrm, ui.AutoSize, ui.AutoSize, "Connecting to login server...", ui.Fixed)
|
||
|
|
||
|
dlg.View.OnKeyDown(func(ev ui.Event, data interface{}) bool {
|
||
|
if ev.Key == 0 && ev.Msg == "login-complete" {
|
||
|
if dlg.beforeClose != nil {
|
||
|
dlg.beforeClose(ev)
|
||
|
}
|
||
|
|
||
|
ui.WindowManager().DestroyWindow(dlg.View)
|
||
|
ui.WindowManager().BeginUpdate()
|
||
|
|
||
|
closeFunc := dlg.onClose
|
||
|
ui.WindowManager().EndUpdate()
|
||
|
if closeFunc != nil {
|
||
|
closeFunc()
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}, nil)
|
||
|
|
||
|
go func() {
|
||
|
if ok, err := checkLogin(username, password); ok {
|
||
|
ui.PutEvent(ui.Event{Type: ui.EventKey, Msg: "login-complete"})
|
||
|
} else {
|
||
|
ui.PutEvent(ui.Event{Type: ui.EventKey, Msg: "login-complete", Err: err})
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
go func() {
|
||
|
for i := 0; i < 422; i += 1 {
|
||
|
progress.SetValue(int(math.Floor(math.Log(float64(i)*8)*16 - 30)))
|
||
|
time.Sleep(64 * time.Millisecond)
|
||
|
ui.PutEvent(ui.Event{Type: ui.EventRedraw})
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
return dlg
|
||
|
}
|