WIP essentials 10

This commit is contained in:
ZacharyZhang-NY committed 2026-10-05 14:25:28 -04:00
1 parent d9e210ffa4
commit cd2b4f3fca
5 files changed
+229

No files matched your search

+2
View File
@@ -26,6 +26,8 @@ func init() {
"signature_pad.empty": {"Sign here", "请在此签名"},
"signature_pad.clear": {"Clear", "清除"},
"undo_toast.undo": {"Undo", "撤销"},
"qr_code.label": {"QR code", "二维码"},
"qr_code.too_long": {"Text too long for a QR code", "文字过长,无法生成二维码"},
})
Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

+136
View File
@@ -0,0 +1,136 @@
package mujica
import (
"cmp"
"fmt"
"log/slog"
"math"
"time"
"github.com/egoist/mygo/ui"
"mujicaui/icons"
"mujicaui/theme"
)
// UndoToastOptions configure an UndoToast.
type UndoToastOptions struct {
// OnUndo is required; it runs when the user undoes.
OnUndo func()
// Duration is the countdown; zero is ToastDuration.
Duration time.Duration
// Action labels the button; empty uses "Undo".
Action string
}
// UndoToastResult reports how an UndoToast ended this frame, if it did.
type UndoToastResult struct {
Element *ui.Element
Undone bool
// Expired: the countdown ran out, or the user closed it; the action stands.
Expired bool
}
// undoToastClock counts the time shown, stopped while hovered or focused.
type undoToastClock struct {
message string
spent time.Duration
runFrom time.Time
}
// UndoToast shows message at the bottom of the window with a countdown while *open; it never takes the focus.
func UndoToast(c *ui.Context, open *bool, message string, opts UndoToastOptions) UndoToastResult {
if open == nil || message == "" || opts.OnUndo == nil {
panic("mujica: UndoToast needs open, a message and OnUndo")
}
if opts.Duration < 0 {
panic("mujica: negative UndoToast Duration")
}
clock := ui.Local(c.Root(), "undo_toast", func() *undoToastClock { return &undoToastClock{} })
s := *clock
var res UndoToastResult
if !*open {
*s = undoToastClock{}
return res
}
now := c.Now()
life := cmp.Or(opts.Duration, ToastDuration)
if s.message != message {
*s = undoToastClock{message: message, runFrom: now}
c.Announce(message)
}
spent := s.spent
if !s.runFrom.IsZero() {
spent += now.Sub(s.runFrom)
}
if spent >= life {
*open, res.Expired = false, true
*s = undoToastClock{}
slog.Debug("mujica: undo_toast expired")
return res
}
k := Tokens(c)
u := Density(c).Unit()
ww, _ := c.Size()
left := life - spent
ui.Overlay(c, func() {
dock := ui.Row(c).Absolute().Left(0).Right(0).Bottom(u * 4).Justify(ui.Center).PassThrough()
dock.Children(func() {
card := ui.Column(c).Width(min(420, ww-8*u)).Radius(theme.PopupRadius).Background(k.Surface).
Border(theme.BorderWidth, k.ControlBorder).Role(ui.RoleStatus).Label(message).Clip()
card.Shadow(0, 4, 12, 0, ui.RGBA(0, 0, 0, 0.14))
card.Transition(ui.ElementTransition{Duration: theme.PopupDuration, Enter: &ui.Motion{Y: 8}, Exit: &ui.Motion{}})
res.Element = card
card.Children(func() {
ui.Row(c).AlignItems(ui.Center).Gap(u*3).Padding(u*2, u*2, u*2, u*4).Children(func() {
ui.Text(c, message).TextColor(k.Text).Grow(1).Shrink(1)
secs := int(math.Ceil(left.Seconds()))
label := fmt.Sprintf("%s (%d)", cmp.Or(opts.Action, msg(c, "undo_toast.undo")), secs)
if Button(c, label, ButtonOptions{Variant: Ghost, Icon: icons.Must("refresh-cw")}).Clicked() {
res.Undone = true
}
if iconAction(c, icons.Must("x"), msg(c, "toast.close")).Clicked() {
res.Expired = true
}
})
still := c.Preferences().ReduceMotion
ui.Box(c).FillWidth().Height(2).Role(ui.RoleNone).Draw(func(p *ui.Painter, r ui.Rect) {
if still {
return
}
rest := left
if !s.runFrom.IsZero() {
rest = life - s.spent - p.Now().Sub(s.runFrom)
p.AnimationFrame()
}
p.Fill(ui.Rect{X: r.X, Y: r.Y, W: r.W * max(0, float32(rest)/float32(life)), H: r.H}, k.AccentText, 0)
})
})
if card.Shortcut(0, ui.KeyEscape) {
res.Expired = true
}
// Hover or focus holds the countdown.
switch held := card.Hovered() || card.FocusWithin(); {
case held && !s.runFrom.IsZero():
s.spent, s.runFrom = spent, time.Time{}
case !held && s.runFrom.IsZero():
s.runFrom = now
}
})
})
switch {
case res.Undone:
*open, res.Expired = false, false
*s = undoToastClock{}
slog.Debug("mujica: undo_toast undo")
opts.OnUndo()
case res.Expired:
*open = false
*s = undoToastClock{}
slog.Debug("mujica: undo_toast closed")
case !s.runFrom.IsZero():
// Wake for the next whole second of the count, and for the end.
c.After(min(left, left-left.Truncate(time.Second)+time.Millisecond))
}
return res
}
+91
View File
@@ -0,0 +1,91 @@
package mujica
import (
"slices"
"testing"
"time"
"github.com/egoist/mygo/ui"
)
func undoTester(open *bool, life time.Duration, undos *int, last *UndoToastResult) *ui.Tester {
return ui.NewTester(func(c *ui.Context) {
Use(c, Settings{})
if Button(c, "Delete", ButtonOptions{}).Clicked() {
*open = true
}
if r := UndoToast(c, open, "File deleted", UndoToastOptions{Duration: life, OnUndo: func() { *undos++ }}); r.Undone || r.Expired {
*last = r
}
}, 600, 400)
}
func TestUndoToastUndoAndFocus(t *testing.T) {
open, undos := false, 0
var last UndoToastResult
tt := undoTester(&open, 0, &undos, &last)
tt.Key(0, ui.KeyTab)
tt.Key(0, ui.KeyEnter)
if !tt.HasText("File deleted") || !tt.HasText("Undo (5)") || !tt.Focused("Delete") {
t.Fatalf("shown %q, focus kept %v", tt.Texts(), tt.Focused("Delete"))
}
if !slices.Contains(tt.Announcements(), "File deleted") {
t.Fatal("not announced")
}
if err := tt.Click("Undo (5)"); err != nil {
t.Fatal(err)
}
if undos != 1 || open || !last.Undone || tt.HasText("File deleted") {
t.Fatalf("undo: %d %v %+v", undos, open, last)
}
}
func TestUndoToastCountdownPauseAndClose(t *testing.T) {
open, undos := false, 0
var last UndoToastResult
tt := undoTester(&open, 60*time.Millisecond, &undos, &last)
_ = tt.Click("Delete")
r, _ := tt.Find("File deleted")
tt.Move(r.X+r.W/2, r.Y+r.H/2)
time.Sleep(90 * time.Millisecond)
tt.Frame()
if !open {
t.Fatal("hovered countdown ran out")
}
tt.Move(5, 5)
time.Sleep(90 * time.Millisecond)
tt.Frame()
if open || !last.Expired || undos != 0 {
t.Fatalf("expiry: %v %+v %d", open, last, undos)
}
last = UndoToastResult{}
_ = tt.Click("Delete")
_ = tt.Click("Dismiss notification")
if open || !last.Expired || undos != 0 {
t.Fatalf("close: %v %+v", open, last)
}
}
func TestUndoToastRejects(t *testing.T) {
open := true
for name, f := range map[string]func(c *ui.Context){
"no undo": func(c *ui.Context) { UndoToast(c, &open, "x", UndoToastOptions{}) },
"negative": func(c *ui.Context) { UndoToast(c, &open, "x", UndoToastOptions{OnUndo: func() {}, Duration: -1}) },
} {
func() {
defer func() {
if recover() == nil {
t.Errorf("%s: no panic", name)
}
}()
ui.NewTester(func(c *ui.Context) { Use(c, Settings{}); f(c) }, 300, 200)
}()
}
}
func TestUndoToastSnapshot(t *testing.T) {
open := true
snapshot(t, "undo_toast", 520, 160, func(c *ui.Context) {
UndoToast(c, &open, "3 messages archived", UndoToastOptions{OnUndo: func() {}, Duration: time.Hour})
})
}