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

79 lines
2.1 KiB
Go
Raw Normal View History

2023-01-12 17:51:42 +00:00
package popups
import "git.tebibyte.media/sashakoshka/tomo"
2023-03-05 04:09:46 +00:00
import "git.tebibyte.media/sashakoshka/tomo/theme"
2023-02-02 06:48:16 +00:00
import "git.tebibyte.media/sashakoshka/tomo/elements"
import "git.tebibyte.media/sashakoshka/tomo/layouts/basic"
2023-01-12 17:51:42 +00:00
import "git.tebibyte.media/sashakoshka/tomo/elements/basic"
2023-01-12 19:12:24 +00:00
// DialogKind defines the semantic role of a dialog window.
2023-01-12 17:51:42 +00:00
type DialogKind int
const (
DialogKindInfo DialogKind = iota
DialogKindQuestion
DialogKindWarning
DialogKindError
)
2023-01-12 19:12:24 +00:00
// Button represents a dialog response button.
2023-01-12 17:51:42 +00:00
type Button struct {
2023-01-12 19:12:24 +00:00
// Name contains the text to display on the button.
2023-01-12 17:51:42 +00:00
Name string
2023-01-12 19:12:24 +00: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 17:51:42 +00:00
OnPress func ()
}
2023-01-12 19:12:24 +00:00
// NewDialog creates a new dialog window and returns it.
func NewDialog (
kind DialogKind,
title, message string,
buttons ...Button,
) (
2023-02-02 06:48:16 +00:00
window elements.Window,
2023-01-12 19:12:24 +00:00
) {
window, _ = tomo.NewWindow(2, 2)
2023-01-12 17:51:42 +00:00
window.SetTitle(title)
2023-03-05 04:09:46 +00:00
2023-02-02 06:48:16 +00:00
container := basicElements.NewContainer(basicLayouts.Dialog { true, true })
2023-01-12 17:51:42 +00:00
window.Adopt(container)
2023-03-05 04:09:46 +00:00
messageContainer := basicElements.NewContainer(basicLayouts.Horizontal { true, false })
iconId := theme.IconInformation
switch kind {
case DialogKindInfo: iconId = theme.IconInformation
case DialogKindQuestion: iconId = theme.IconQuestion
case DialogKindWarning: iconId = theme.IconWarning
case DialogKindError: iconId = theme.IconError
}
2023-03-10 23:53:27 +00:00
messageContainer.Adopt(basicElements.NewIcon(iconId, theme.IconSizeLarge), false)
2023-03-05 04:09:46 +00:00
messageContainer.Adopt(basicElements.NewLabel(message, false), true)
container.Adopt(messageContainer, true)
2023-01-12 17:51:42 +00:00
if len(buttons) == 0 {
2023-02-02 06:48:16 +00:00
button := basicElements.NewButton("OK")
2023-03-05 05:05:56 +00:00
button.SetIcon(theme.IconYes)
2023-01-12 17:51:42 +00:00
button.OnClick(window.Close)
container.Adopt(button, false)
button.Focus()
2023-01-12 17:51:42 +00:00
} else {
2023-02-02 06:48:16 +00:00
var button *basicElements.Button
2023-01-12 17:51:42 +00:00
for _, buttonDescriptor := range buttons {
2023-02-02 06:48:16 +00:00
button = basicElements.NewButton(buttonDescriptor.Name)
2023-01-12 17:51:42 +00:00
button.SetEnabled(buttonDescriptor.OnPress != nil)
button.OnClick (func () {
buttonDescriptor.OnPress()
window.Close()
})
container.Adopt(button, false)
}
button.Focus()
2023-01-12 17:51:42 +00:00
}
window.Show()
2023-01-12 19:12:24 +00:00
return
2023-01-12 17:51:42 +00:00
}