2023-01-12 10:51:42 -07:00
|
|
|
package popups
|
|
|
|
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
2023-03-30 21:19:04 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/layouts"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements"
|
2023-03-16 12:22:56 -06:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/containers"
|
2023-01-12 10:51:42 -07:00
|
|
|
|
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-03-23 22:47:04 -06:00
|
|
|
// NewDialog creates a new modal dialog window and returns it. If parent is nil,
|
|
|
|
// the dialog will just be a normal window
|
2023-01-12 12:12:24 -07:00
|
|
|
func NewDialog (
|
|
|
|
kind DialogKind,
|
2023-03-30 21:19:04 -06:00
|
|
|
parent tomo.Window,
|
2023-01-12 12:12:24 -07:00
|
|
|
title, message string,
|
|
|
|
buttons ...Button,
|
|
|
|
) (
|
2023-03-30 21:19:04 -06:00
|
|
|
window tomo.Window,
|
2023-01-12 12:12:24 -07:00
|
|
|
) {
|
2023-03-23 22:47:04 -06:00
|
|
|
if parent == nil {
|
|
|
|
window, _ = tomo.NewWindow(2, 2)
|
|
|
|
} else {
|
|
|
|
window, _ = parent.NewModal(2, 2)
|
|
|
|
}
|
2023-01-12 10:51:42 -07:00
|
|
|
window.SetTitle(title)
|
2023-03-04 21:09:46 -07:00
|
|
|
|
2023-03-30 21:19:04 -06:00
|
|
|
container := containers.NewContainer(layouts.Dialog { true, true })
|
2023-01-12 10:51:42 -07:00
|
|
|
window.Adopt(container)
|
|
|
|
|
2023-03-30 21:19:04 -06:00
|
|
|
messageContainer := containers.NewContainer(layouts.Horizontal { true, false })
|
2023-03-30 23:06:29 -06:00
|
|
|
iconId := tomo.IconInformation
|
2023-03-04 21:09:46 -07:00
|
|
|
switch kind {
|
2023-03-30 23:06:29 -06:00
|
|
|
case DialogKindInfo: iconId = tomo.IconInformation
|
|
|
|
case DialogKindQuestion: iconId = tomo.IconQuestion
|
|
|
|
case DialogKindWarning: iconId = tomo.IconWarning
|
|
|
|
case DialogKindError: iconId = tomo.IconError
|
2023-03-04 21:09:46 -07:00
|
|
|
}
|
|
|
|
|
2023-03-30 23:06:29 -06:00
|
|
|
messageContainer.Adopt(elements.NewIcon(iconId, tomo.IconSizeLarge), false)
|
2023-03-30 21:19:04 -06:00
|
|
|
messageContainer.Adopt(elements.NewLabel(message, false), true)
|
2023-03-04 21:09:46 -07:00
|
|
|
container.Adopt(messageContainer, true)
|
|
|
|
|
2023-01-12 10:51:42 -07:00
|
|
|
if len(buttons) == 0 {
|
2023-03-30 21:19:04 -06:00
|
|
|
button := elements.NewButton("OK")
|
2023-03-30 23:06:29 -06:00
|
|
|
button.SetIcon(tomo.IconYes)
|
2023-01-12 10:51:42 -07:00
|
|
|
button.OnClick(window.Close)
|
|
|
|
container.Adopt(button, false)
|
2023-01-30 15:25:09 -07:00
|
|
|
button.Focus()
|
2023-01-12 10:51:42 -07:00
|
|
|
} else {
|
2023-03-30 21:19:04 -06:00
|
|
|
var button *elements.Button
|
2023-01-12 10:51:42 -07:00
|
|
|
for _, buttonDescriptor := range buttons {
|
2023-03-30 21:19:04 -06:00
|
|
|
button = elements.NewButton(buttonDescriptor.Name)
|
2023-01-12 10:51:42 -07:00
|
|
|
button.SetEnabled(buttonDescriptor.OnPress != nil)
|
|
|
|
button.OnClick (func () {
|
|
|
|
buttonDescriptor.OnPress()
|
|
|
|
window.Close()
|
|
|
|
})
|
|
|
|
container.Adopt(button, false)
|
|
|
|
}
|
2023-01-30 15:25:09 -07:00
|
|
|
button.Focus()
|
2023-01-12 10:51:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
window.Show()
|
2023-01-12 12:12:24 -07:00
|
|
|
return
|
2023-01-12 10:51:42 -07:00
|
|
|
}
|