Elements are now pass-scoped value handles: tests that read bounds after a frame bind a ui.Handle. pixel tells frames apart by the build pass's root, since the Tester clock no longer moves on its own. Fix the regressions the upgrade brought: - Clipboard reads in input handlers go through c.Services(). - MyGo notes an overlay's opener after building it, so AutoFocus inside lost it; pixel.AutoFocus focuses through a Handle, and archtest bans the raw call. Dialogs hand the focus back again. - Elements around a press stay hovered: Tooltip tells a press-given focus by focus arriving under the pointer. - The web host moves the Tester clock with real time, at most 500 ms a call, and gives components the real clock. The gallery's source index also accepts CRLF checkouts. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package feedback
|
||
|
||
import (
|
||
"fmt"
|
||
"log/slog"
|
||
|
||
"github.com/egoist/mygo/ui"
|
||
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/display"
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
||
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/text"
|
||
)
|
||
|
||
// Kind is a message's tone.
|
||
type Kind uint8
|
||
|
||
const (
|
||
Info Kind = iota
|
||
Success
|
||
Warning
|
||
Danger
|
||
)
|
||
|
||
// look is a kind's slot and icon.
|
||
func (k Kind) look() (pixel.Slot, *pixel.Sprite) {
|
||
switch k {
|
||
case Info:
|
||
return pixel.Info, icon.Info
|
||
case Success:
|
||
return pixel.Good, icon.Check
|
||
case Warning:
|
||
return pixel.Warn, icon.Warning
|
||
case Danger:
|
||
return pixel.Bad, icon.Error
|
||
}
|
||
panic(fmt.Sprintf("feedback: unknown Kind %d", k))
|
||
}
|
||
|
||
type AlertOptions struct {
|
||
Kind Kind
|
||
// Closable adds a × that reports Closed.
|
||
Closable bool
|
||
Disabled bool
|
||
}
|
||
|
||
type AlertResult struct {
|
||
Element ui.Element
|
||
Closed bool
|
||
}
|
||
|
||
// Alert is a framed callout: icon, title and text.
|
||
func Alert(c *ui.Context, title, message string, o AlertOptions) AlertResult {
|
||
k := pixel.K(c)
|
||
slot, sp := o.Kind.look()
|
||
res := AlertResult{}
|
||
res.Element = ui.Row(c).Gap(k.D(6)).Padding(k.D(6), k.D(8), k.D(6), k.D(10)).AlignItems(ui.Start).Role(ui.RoleStatus).Label(title).Draw(func(p *ui.Painter, r ui.Rect) {
|
||
g := k.Canvas(p, r)
|
||
g.Frame(0, 0, g.W, g.H, pixel.Window, pixel.Face)
|
||
g.Rect(3, 3, 3, g.H-6, slot)
|
||
}).Children(func() {
|
||
display.Icon(c, sp, display.IconOptions{Slot: slot})
|
||
ui.Column(c).Grow(1).Gap(k.D(2)).Children(func() {
|
||
pixel.Text(c, title, pixel.TextOptions{Slot: slot})
|
||
text.Paragraph(c, message, text.ParagraphOptions{})
|
||
})
|
||
if o.Closable {
|
||
res.Closed = input.IconButton(c, icon.Close, k.Msg("close"), input.IconButtonOptions{Variant: input.Ghost, Size: input.Small, Disabled: o.Disabled}).Clicked
|
||
}
|
||
})
|
||
if res.Closed {
|
||
slog.Debug("8bit: alert closed", "title", title)
|
||
}
|
||
return res
|
||
}
|