This repository has been archived on 2024-03-03. You can view files and clone it, but cannot push or open issues or pull requests.
adlin/pkg/login-app/cmd/dialog-errmsg.go

86 lines
1.9 KiB
Go
Raw Normal View History

package main
import (
ui "github.com/VladimirMarkelov/clui"
term "github.com/nsf/termbox-go"
)
type ErrMsgDialog struct {
View *ui.Window
result int
beforeClose func()
onClose func()
}
func CreateErrMsgDialog(title string, err error) *ErrMsgDialog {
dlg := new(ErrMsgDialog)
sWidth, sHeight := ui.ScreenSize()
wWidth, wHeight := 60, 10
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)
lbl := ui.CreateLabel(textfrm, ui.AutoSize, ui.AutoSize, "An error occurs:", ui.Fixed)
lbl.SetTextColor(ui.ColorRedBold)
tv := ui.CreateTextView(textfrm, ui.AutoSize, 4, ui.Fixed)
tv.SetWordWrap(true)
tv.SetText([]string{err.Error()})
tv.SetTextColor(ui.ColorWhite)
tv.SetBackColor(ui.ColorBlack)
ui.CreateLabel(textfrm, ui.AutoSize, ui.AutoSize, "Please try again.", ui.Fixed)
btnOk := ui.CreateButton(textfrm, 20, 4, "Ok", 1)
btnOk.OnClick(func(ev ui.Event) {
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 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)
ui.ActivateControl(dlg.View, btnOk)
return dlg
}