This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/popups/dialog.go

64 lines
1.5 KiB
Go
Raw Normal View History

2023-01-12 10:51:42 -07:00
package popups
import "git.tebibyte.media/sashakoshka/tomo"
2023-01-15 22:11:06 -07:00
import "git.tebibyte.media/sashakoshka/tomo/layouts"
2023-01-12 10:51:42 -07:00
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
2023-01-12 12:12:24 -07:00
// DialogKind defines the semantic role of a dialog window.
2023-01-12 10:51:42 -07:00
type DialogKind int
const (
DialogKindInfo DialogKind = iota
DialogKindQuestion
DialogKindWarning
DialogKindError
)
2023-01-12 12:12:24 -07:00
// Button represents a dialog response button.
2023-01-12 10:51:42 -07:00
type Button struct {
2023-01-12 12:12:24 -07:00
// Name contains the text to display on the button.
2023-01-12 10:51:42 -07:00
Name string
2023-01-12 12:12:24 -07:00
// OnPress specifies a callback to run when the button is pressed. If
// this callback is nil, the button will appear disabled.
2023-01-12 10:51:42 -07:00
OnPress func ()
}
2023-01-12 12:12:24 -07:00
// NewDialog creates a new dialog window and returns it.
func NewDialog (
kind DialogKind,
title, message string,
buttons ...Button,
) (
window tomo.Window,
) {
window, _ = tomo.NewWindow(2, 2)
2023-01-12 10:51:42 -07:00
window.SetTitle(title)
container := basic.NewContainer(layouts.Dialog { true, true })
window.Adopt(container)
container.Adopt(basic.NewLabel(message, false), true)
if len(buttons) == 0 {
button := basic.NewButton("OK")
button.OnClick(window.Close)
container.Adopt(button, false)
button.Select()
} else {
var button *basic.Button
for _, buttonDescriptor := range buttons {
button = basic.NewButton(buttonDescriptor.Name)
button.SetEnabled(buttonDescriptor.OnPress != nil)
button.OnClick (func () {
buttonDescriptor.OnPress()
window.Close()
})
container.Adopt(button, false)
}
button.Select()
}
window.Show()
2023-01-12 12:12:24 -07:00
return
2023-01-12 10:51:42 -07:00
}