A cross-cutting pass, each fix with a test that failed first. Veils: MyGo still hands a key to a base that turned off this frame, so every element key now goes through pixel.Shortcut, which an archtest enforces. Input handlers record and the build applies them only while enabled; the editor turns away a veil-frame letter or copy; value bases get a copy and keep only enabled turns. Popups give a held focus back to their opener when the veil lifts. Motion: S5 is enforced at the clock. Every Tick, Since, Step, Toward and Shot runs at 4-15 fps; blinks keep 1 Hz on 4 fps steps, fast animations step at 15, and drawn time is quantized to frames. Clocks stepping back: Since holds at zero without overflow, and k.Elapsed moves a stored start to now, so nothing goes negative, runs backward or indexes out of range. Also: logs count typed text instead of holding it, gallery names use glyphs the font has, removable rows are keyed, components take their own Options (archtest), and sweeps over all 508 demos check veiled keys, clicks, wheels, the clock stepping back and names.
85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package overlay
|
|
|
|
import (
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/layout"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/text"
|
|
)
|
|
|
|
type AlertDialogOptions struct {
|
|
// Icon heads the message; nil means a warning sign.
|
|
Icon *pixel.Sprite
|
|
// Buttons label the answers; nil means OK.
|
|
Buttons []string
|
|
// Default is the button Enter presses.
|
|
Default int
|
|
Disabled bool
|
|
}
|
|
|
|
type AlertDialogResult struct {
|
|
Element *ui.Element
|
|
// Pressed is the button pressed this frame, or -1.
|
|
Pressed int
|
|
Closed bool
|
|
}
|
|
|
|
// AlertDialog asks for an answer; Enter presses the default.
|
|
func AlertDialog(c *ui.Context, title, message string, open *bool, o AlertDialogOptions) AlertDialogResult {
|
|
k := pixel.K(c)
|
|
buttons, art := o.Buttons, o.Icon
|
|
if buttons == nil {
|
|
buttons = []string{k.Msg("ok")}
|
|
}
|
|
if art == nil {
|
|
art = icon.Warning
|
|
}
|
|
if o.Default < 0 || o.Default >= len(buttons) {
|
|
panic(fmt.Sprintf("overlay: AlertDialog Default %d of %d buttons", o.Default, len(buttons)))
|
|
}
|
|
was := *open
|
|
res := AlertDialogResult{Pressed: -1}
|
|
press := func(i int) { res.Pressed, *open = i, false }
|
|
res.Element = ui.DialogBase(c, open, func(back, panel *ui.Element) {
|
|
dim(k, back, pixel.Back, 8)
|
|
panel.Width(k.D(220)).Role(ui.RoleAlertDialog).Label(title).Description(message).Focusable().FocusRing(false)
|
|
if pixel.Shortcut(panel, 0, ui.KeyEnter) && !o.Disabled && !pixel.Current(c).Inert {
|
|
press(o.Default)
|
|
}
|
|
panel.Children(func() {
|
|
pixel.Harbor(c, panel, func() {
|
|
layout.Window(c, layout.WindowOptions{Title: title}, func() {
|
|
ui.Row(c).Gap(k.D(8)).Children(func() {
|
|
ui.Box(c).Size(k.D(art.W*2), k.D(art.H*2)).Shrink(0).Draw(func(p *ui.Painter, r ui.Rect) {
|
|
k.Canvas(p, r).Sprite(0, 0, art, 0, pixel.Warn, 2)
|
|
})
|
|
ui.Column(c).Grow(1).Children(func() { text.Paragraph(c, message, text.ParagraphOptions{}) })
|
|
})
|
|
ui.Row(c).Gap(k.D(4)).Justify(ui.End).Padding(k.D(6), 0, 0, 0).Children(func() {
|
|
for i, b := range buttons {
|
|
v := input.Normal
|
|
if i == o.Default {
|
|
v = input.Primary
|
|
}
|
|
if input.Button(c, b, input.ButtonOptions{Size: input.Small, Variant: v, Disabled: o.Disabled}).Clicked {
|
|
press(i)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
})
|
|
})
|
|
})
|
|
res.Closed = was && !*open && res.Pressed < 0
|
|
if res.Pressed >= 0 || res.Closed {
|
|
slog.Debug("8bit: alert", "title", title, "pressed", res.Pressed, "closed", res.Closed)
|
|
}
|
|
return res
|
|
}
|