itd/cmd/itgui/error.go

58 lines
1.5 KiB
Go
Raw Normal View History

2021-08-25 21:18:24 -07:00
package main
import (
"image/color"
2021-08-27 08:47:24 -07:00
"os"
2021-08-25 21:18:24 -07:00
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
)
2021-08-27 08:47:24 -07:00
func guiErr(err error, msg string, fatal bool, parent fyne.Window) {
2021-08-26 08:47:17 -07:00
// Create new label containing message
2021-08-25 21:18:24 -07:00
msgLbl := widget.NewLabel(msg)
2021-08-26 08:47:17 -07:00
// Text formatting settings
2021-08-25 21:18:24 -07:00
msgLbl.Wrapping = fyne.TextWrapWord
msgLbl.Alignment = fyne.TextAlignCenter
2021-08-26 08:47:17 -07:00
// Create new rectangle to set the size of the dialog
2021-08-25 21:18:24 -07:00
rect := canvas.NewRectangle(color.Transparent)
2021-08-26 08:47:17 -07:00
// Set minimum size of rectangle to 350x0
2021-08-25 21:18:24 -07:00
rect.SetMinSize(fyne.NewSize(350, 0))
2021-08-26 08:47:17 -07:00
// Create new container containing message and rectangle
2021-08-25 21:18:24 -07:00
content := container.NewVBox(
msgLbl,
rect,
)
if err != nil {
2021-08-26 08:47:17 -07:00
// Create new label containing error text
2022-05-05 14:00:49 -07:00
errEntry := widget.NewEntry()
errEntry.SetText(err.Error())
// If text changed, change it back
errEntry.OnChanged = func(string) {
errEntry.SetText(err.Error())
}
2021-08-26 08:47:17 -07:00
// Create new dropdown containing error label
2021-08-25 21:18:24 -07:00
content.Add(widget.NewAccordion(
2022-05-05 14:00:49 -07:00
widget.NewAccordionItem("More Details", errEntry),
2021-08-25 21:18:24 -07:00
))
}
2021-08-27 08:47:24 -07:00
if fatal {
// Create new error dialog
errDlg := dialog.NewCustom("Error", "Close", content, parent)
// On close, exit with code 1
errDlg.SetOnClosed(func() {
os.Exit(1)
})
// Show dialog
errDlg.Show()
// Run app prematurely to stop further execution
parent.ShowAndRun()
} else {
// Show error dialog
dialog.NewCustom("Error", "Ok", content, parent).Show()
}
2021-08-25 21:18:24 -07:00
}