errorgen/dialog.go

94 lines
1.8 KiB
Go

package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
)
// Dialog settings for an individual Error Message popup.
type Dialog struct {
Icon Icon
Title string
Message string
Buttons []*Button
}
// Button settings.
type Button struct {
Label string
Disabled bool
}
func (d Dialog) Show() {
// Validate and set defaults.
if d.Title == "" {
d.Title = DefaultTitle
}
if d.Message == "" {
d.Message = DefaultMessage
}
if d.Icon.Empty() {
d.Icon = icons[DefaultIcon]
}
// If no buttons have text, set a default button.
var hasButtons bool
for _, btn := range d.Buttons {
if btn.Label != "" {
hasButtons = true
break
}
}
if !hasButtons {
d.Buttons = []*Button{
{
Label: "Ok",
},
}
}
// Create the dialog window.
w := fyne.CurrentApp().NewWindow(d.Title)
// Icon + Message row.
icon := canvas.NewImageFromResource(d.Icon)
icon.FillMode = canvas.ImageFillOriginal
icon.Resize(fyne.NewSize(32, 32))
message := widget.NewLabel(d.Message)
messageRow := container.New(layout.NewHBoxLayout(), layout.NewSpacer(), icon, message, layout.NewSpacer())
// After clicked handler for buttons.
onClick := func() {
MainWindow.Show()
w.Close()
}
w.SetOnClosed(onClick)
// Buttons
var buttons = []fyne.CanvasObject{
layout.NewSpacer(),
}
for _, b := range d.Buttons {
if b.Label == "" {
continue
}
button := widget.NewButton(b.Label, func() {
onClick()
})
if b.Disabled {
button.Disable()
}
buttons = append(buttons, button, layout.NewSpacer())
}
buttonRow := container.New(layout.NewHBoxLayout(), buttons...)
vbox := container.New(layout.NewVBoxLayout(), messageRow, buttonRow)
w.SetContent(container.New(layout.NewPaddedLayout(), vbox))
w.Show()
}