You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

106 lines
2.1 KiB

package dialogs
import (
"fmt"
"git.brettb.xyz/goinv/client/internal/ui/style"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"go.uber.org/zap"
)
type ErrorDialog struct {
*tview.Box
logger *zap.Logger
modal *tview.Modal
title string
message string
display bool
}
func NewErrorDialog(logger *zap.Logger) *ErrorDialog {
bgColor := style.ErrorDialogBgColor
dialog := ErrorDialog{
Box: tview.NewBox(),
logger: logger,
modal: tview.NewModal().SetBackgroundColor(bgColor).AddButtons([]string{"OK"}),
display: false,
}
dialog.modal.SetButtonBackgroundColor(style.ErrorDialogButtonBgColor)
dialog.modal.SetDoneFunc(func(buttonIndex int, buttonLabel string) {
dialog.Hide()
})
return &dialog
}
func (e *ErrorDialog) Display() {
e.display = true
}
func (e *ErrorDialog) Hide() {
e.SetText("")
e.title = ""
e.message = ""
e.display = false
}
func (e *ErrorDialog) IsDisplay() bool {
return e.display
}
func (e *ErrorDialog) SetText(message string) {
e.message = message
}
func (e *ErrorDialog) SetTitle(title string) {
e.title = title
}
func (e *ErrorDialog) HasFocus() bool {
return e.modal.HasFocus()
}
func (e *ErrorDialog) Focus(delegate func(tview.Primitive)) {
delegate(e.modal)
}
func (e *ErrorDialog) InputHandler() func(*tcell.EventKey, func(tview.Primitive)) {
return e.WrapInputHandler(func(event *tcell.EventKey, setFocus func(tview.Primitive)) {
e.logger.Sugar().Debugf("error dialog event %v received", event)
if modalHandler := e.modal.InputHandler(); modalHandler != nil {
modalHandler(event, setFocus)
return
}
})
}
func (e *ErrorDialog) SetRect(x, y, width, height int) {
e.Box.SetRect(x, y, width, height)
}
func (e *ErrorDialog) Draw(screen tcell.Screen) {
hFgColor := style.FgColor
headerColor := style.GetColorHex(hFgColor)
var errorMessage string
if e.title != "" {
errorMessage = fmt.Sprintf("[%s::b]%s[-::-]\n", headerColor, e.title)
}
errorMessage += e.message
e.modal.SetText(errorMessage)
e.modal.Draw(screen)
}
func (e *ErrorDialog) SetDoneFunc(handler func()) *ErrorDialog {
e.modal.SetDoneFunc(func(buttonIndex int, buttonLabel string) {
handler()
})
return e
}