diff --git a/components/sonner.go b/components/sonner.go index 6a8d702a..42b66037 100644 --- a/components/sonner.go +++ b/components/sonner.go @@ -48,8 +48,8 @@ func PaintSonner(canvas *gfx.Canvas, theme gfx.Theme, viewport gfx.Rect, toasts if toast.Snapshot.Limited { continue } - var scratch overlayLines - geometry, _ := toastMeasure(canvas, theme, viewport, toast.Snapshot, sonnerToastOptions(options, toast, 0), &scratch) + var scratch toastLines + geometry := toastMeasure(canvas, theme, viewport, toast.Snapshot, sonnerToastOptions(options, toast, 0), &scratch) stackWidth = max(stackWidth, geometry.width) } var stack SonnerLayout @@ -64,7 +64,7 @@ func PaintSonner(canvas *gfx.Canvas, theme gfx.Theme, viewport gfx.Rect, toasts toastOptions.width = stackWidth if options.Expanded && !snapshot.Limited { // The toast sits after the ones in front of it, each with its own - // wrapped text at the stack's width. + // wrapped title and description at the stack's width. reach := sonnerReach(canvas, theme, viewport, toasts, options, index, stackWidth) if top { snapshot.OffsetY += reach diff --git a/components/toast.go b/components/toast.go index 4be86cfb..499f90bf 100644 --- a/components/toast.go +++ b/components/toast.go @@ -50,8 +50,9 @@ type ToastLayout struct { // Toast geometry: a compact message bar, as Arco's Message and react-hot-toast: // 10px above and below, 16px at the ends, an icon and text 8px apart, and a // stack that peeks 10px per toast behind the front one. The width follows the -// text up to 384px, and a description sits under the title as a smaller line. -// A small close button sits at the end of the row and keeps its room. +// text up to 384px; past that the title and the description wrap, as Sonner's +// do, and the description sits under the title as a smaller line. A small close +// button sits at the end of the row and keeps its room. const ( toastMaxWidth = float32(384) toastInset = float32(16) @@ -72,22 +73,30 @@ const ( toastStackMinScale = float32(0.80) ) +// toastLines is the scratch space for the wrapped title and description of a +// toast. The wrapped lines are slices of it, so a caller keeps it on its stack +// for as long as it uses the geometry. +type toastLines struct{ title, description overlayLines } + // toastGeometry measures a toast without painting it: the width, the wrapped -// description, the actions' room and the height those make. +// title and description, the actions' room and the height those make. type toastGeometry struct { width, height float32 text float32 // height of the title and description lines + titleHeight float32 // height of the title lines, which an empty title keeps one of bodyWidth float32 actionWidth float32 actionHeight float32 hasIcon bool + title []string // the title's lines; none for an empty title + description []string // the description's lines; none for an empty one } -func toastMeasure(canvas *gfx.Canvas, theme gfx.Theme, viewport gfx.Rect, snapshot ToastSnapshot, options ToastOptions, scratch *overlayLines) (toastGeometry, []string) { +func toastMeasure(canvas *gfx.Canvas, theme gfx.Theme, viewport gfx.Rect, snapshot ToastSnapshot, options ToastOptions, scratch *toastLines) toastGeometry { var geometry toastGeometry limit := min(toastMaxWidth, viewport.Width-2*toastInset) if limit <= 0 { - return geometry, nil + return geometry } metrics := theme.Metrics geometry.hasIcon = snapshot.Type != ToastDefault @@ -107,8 +116,8 @@ func toastMeasure(canvas *gfx.Canvas, theme gfx.Theme, viewport gfx.Rect, snapsh } bodyLimit := max(limit-chrome, 0) // The body takes the width its own text asks for, so a short title stays a - // compact bar, and the description wraps once that would pass the limit. A - // stack hands every card the same width instead, so its peeks line up. + // compact bar, and the title and the description wrap once that would pass the + // limit. A stack hands every card the same width instead, so its peeks line up. var bodyWidth float32 if options.width > 0 { bodyWidth = options.width - chrome @@ -122,14 +131,18 @@ func toastMeasure(canvas *gfx.Canvas, theme gfx.Theme, viewport gfx.Rect, snapsh } geometry.bodyWidth = min(max(bodyWidth, 0), bodyLimit) geometry.width = min(chrome+geometry.bodyWidth, limit) - lines := overlayWrap(canvas, scratch, snapshot.Description, metrics.FontSizeS, gfx.WeightRegular, geometry.bodyWidth) - geometry.text = overlayLineHeight(metrics) - if len(lines) > 0 { - geometry.text += toastTextGap + float32(len(lines))*toastDescriptionLineHeight(metrics) + geometry.title = overlayWrap(canvas, &scratch.title, snapshot.Title, metrics.FontSizeM, gfx.WeightMedium, geometry.bodyWidth) + geometry.description = overlayWrap(canvas, &scratch.description, snapshot.Description, metrics.FontSizeS, gfx.WeightRegular, geometry.bodyWidth) + // A title with no text still keeps its line, so the toast does not collapse + // onto its description. + geometry.titleHeight = float32(max(len(geometry.title), 1)) * overlayLineHeight(metrics) + geometry.text = geometry.titleHeight + if len(geometry.description) > 0 { + geometry.text += toastTextGap + float32(len(geometry.description))*toastDescriptionLineHeight(metrics) } rowHeight := max(max(geometry.text, toastCloseSize), geometry.actionHeight) geometry.height = rowHeight + 2*toastPaddingY - return geometry, lines + return geometry } // toastDescriptionLineHeight is the leading of the smaller description line. @@ -138,22 +151,22 @@ func toastDescriptionLineHeight(metrics gfx.Metrics) float32 { return metrics.Fo // ToastHeight is how tall PaintToast makes the toast, for a caller that stacks // toasts itself. func ToastHeight(canvas *gfx.Canvas, theme gfx.Theme, viewport gfx.Rect, snapshot ToastSnapshot, options ToastOptions) float32 { - var scratch overlayLines - geometry, _ := toastMeasure(canvas, theme, viewport, snapshot, options, &scratch) - return geometry.height + var scratch toastLines + return toastMeasure(canvas, theme, viewport, snapshot, options, &scratch).height } // PaintToast paints one toast snapshot at the viewport position the options // name, bottom right by default. Its width follows its text, up to 384px, and -// its height follows the description, which wraps at that width. It schedules -// no work and retains no lifetime state, except that a loading toast asks for a -// frame every 100ms to turn its spinner, until it is no longer painted. +// its height follows the title and the description, which wrap at that width. +// It schedules no work and retains no lifetime state, except that a loading +// toast asks for a frame every 100ms to turn its spinner, until it is no longer +// painted. func PaintToast(canvas *gfx.Canvas, theme gfx.Theme, viewport gfx.Rect, snapshot ToastSnapshot, options ToastOptions) ToastLayout { if canvas == nil || viewport.Width <= 0 || viewport.Height <= 0 || snapshot.Limited { return ToastLayout{} } - var scratch overlayLines - geometry, lines := toastMeasure(canvas, theme, viewport, snapshot, options, &scratch) + var scratch toastLines + geometry := toastMeasure(canvas, theme, viewport, snapshot, options, &scratch) if geometry.width <= 0 { return ToastLayout{} } @@ -218,22 +231,25 @@ func PaintToast(canvas *gfx.Canvas, theme gfx.Theme, viewport gfx.Rect, snapshot canvas.Rounded(bounds, metrics.RadiusSurface, theme.Popover) // The text and the action are centered on the row the padding leaves. The icon - // and the close button belong to the title: they center on its line, which is - // the whole row until a description puts a second line under it. + // and the close button belong to the title: they center on its first line, + // which is the whole row until a second title line or a description puts more + // text under it. row := gfx.Rect{X: bounds.X + toastPaddingX, Y: bounds.Y + toastPaddingY, Width: bounds.Width - 2*toastPaddingX, Height: bounds.Height - 2*toastPaddingY} textX := row.X - titleHeight, descriptionHeight := overlayLineHeight(metrics), toastDescriptionLineHeight(metrics) + titleLine, descriptionLine := overlayLineHeight(metrics), toastDescriptionLineHeight(metrics) titleTop := row.Y + (row.Height-geometry.text)/2 - if geometry.text <= titleHeight { - titleTop = row.Y + (row.Height-titleHeight)/2 - } if geometry.hasIcon { - layout.Icon = gfx.Rect{X: row.X, Y: titleTop + (titleHeight-toastIconSize)/2, Width: toastIconSize, Height: toastIconSize} + layout.Icon = gfx.Rect{X: row.X, Y: titleTop + (titleLine-toastIconSize)/2, Width: toastIconSize, Height: toastIconSize} paintToastIcon(canvas, theme, layout.Icon, snapshot.Type) textX += toastIconSize + toastItemGap } - layout.Close = gfx.Rect{X: row.X + row.Width - toastCloseSize, Y: titleTop + (titleHeight-toastCloseSize)/2, Width: toastCloseSize, Height: toastCloseSize} - right := layout.Close.X - toastItemGap + layout.Close = gfx.Rect{X: row.X + row.Width - toastCloseSize, Y: titleTop + (titleLine-toastCloseSize)/2, Width: toastCloseSize, Height: toastCloseSize} + // The body ends where the room measured for it ends: at the close button, or + // at the row's end when there is none, which keeps no room for one. + right := row.X + row.Width + if !options.DismissID.IsZero() { + right = layout.Close.X - toastItemGap + } if geometry.actionWidth > 0 { button := BeginButton(canvas, theme, options.ActionID, gfx.Rect{X: right - geometry.actionWidth, Y: row.Y, Width: geometry.actionWidth}).Small() layout.Action = gfx.Rect{X: right - geometry.actionWidth, Y: row.Y + (row.Height-button.BoundsHeight())/2, Width: geometry.actionWidth, Height: button.BoundsHeight()} @@ -259,9 +275,11 @@ func PaintToast(canvas *gfx.Canvas, theme gfx.Theme, viewport gfx.Rect, snapshot canvas.HitWithOptions(options.DismissID, layout.Close, "Close toast", gfx.HitOptions{Role: gfx.RoleButton, Disabled: options.Disabled}) } - layout.Body = gfx.Rect{X: textX, Y: row.Y + (row.Height-geometry.text)/2, Width: max(0, right-textX), Height: geometry.text} - canvas.Label(snapshot.Title, gfx.Rect{X: layout.Body.X, Y: layout.Body.Y, Width: layout.Body.Width, Height: titleHeight}, gfx.TextStyle{Size: metrics.FontSizeM, Weight: gfx.WeightMedium, Color: theme.PopoverForeground}) - overlayParagraph(canvas, lines, gfx.Rect{X: layout.Body.X, Y: layout.Body.Y + titleHeight + toastTextGap, Width: layout.Body.Width}, descriptionHeight, gfx.TextStyle{ + layout.Body = gfx.Rect{X: textX, Y: titleTop, Width: max(0, right-textX), Height: geometry.text} + overlayParagraph(canvas, geometry.title, gfx.Rect{X: layout.Body.X, Y: layout.Body.Y, Width: layout.Body.Width}, titleLine, gfx.TextStyle{ + Size: metrics.FontSizeM, Weight: gfx.WeightMedium, Color: theme.PopoverForeground, + }, false) + overlayParagraph(canvas, geometry.description, gfx.Rect{X: layout.Body.X, Y: layout.Body.Y + geometry.titleHeight + toastTextGap, Width: layout.Body.Width}, descriptionLine, gfx.TextStyle{ Size: metrics.FontSizeS, Weight: gfx.WeightRegular, Color: theme.MutedForeground, }, false) canvas.EndPresence() diff --git a/components/toast_test.go b/components/toast_test.go index a3df308b..bf4f9f17 100644 --- a/components/toast_test.go +++ b/components/toast_test.go @@ -2,6 +2,7 @@ package components import ( "slices" + "strings" "testing" "time" @@ -103,6 +104,277 @@ func TestToastIconAndCloseStayOnTheTitleLineWhenThereIsADescription(t *testing.T } } +// longToastTitle is a message wider than a whole toast, of the kind an +// application shows after an import: it was cut in the middle of a letter, with +// no ellipsis, when the title took one line. +const longToastTitle = "Imported into the library: 12 lines from the shared configuration file" + +// toastTitleTexts is the title lines a toast painted, top to bottom: the text +// labels in the body's column that are set in the title's weight. +func toastTitleTexts(canvas *gfx.Canvas, layout ToastLayout) []gfx.Text { + var lines []gfx.Text + for _, text := range canvas.Texts() { + if text.Style.Weight == gfx.WeightMedium && text.Bounds.X == layout.Body.X { + lines = append(lines, text) + } + } + return lines +} + +// requireTitleFitsBody fails when a title line is wider than the label it is +// drawn in, which the platform would cut in the middle of a character. +func requireTitleFitsBody(t *testing.T, name string, canvas *gfx.Canvas, layout ToastLayout) []gfx.Text { + t.Helper() + lines := toastTitleTexts(canvas, layout) + for _, line := range lines { + if width := canvas.TextWidthWeight(line.Value, line.Style.Size, line.Style.Weight); width > layout.Body.Width { + t.Errorf("%s: title line %q is %v wide in a body %v wide", name, line.Value, width, layout.Body.Width) + } + if line.Bounds.Width != layout.Body.Width { + t.Errorf("%s: title line %q is drawn in %v, not the body's %v", name, line.Value, line.Bounds.Width, layout.Body.Width) + } + } + return lines +} + +// A title wider than the body wraps as a description does, instead of being cut +// in the middle of a character, and the toast grows by the lines it gains. +func TestToastLongTitleWrapsInsideTheBodyAndTheToastGrows(t *testing.T) { + var canvas gfx.Canvas + theme := gfx.LightTheme() + viewport := gfx.Rect{Width: 1000, Height: 700} + line := overlayLineHeight(theme.Metrics) + if width := canvas.TextWidthWeight(longToastTitle, theme.Metrics.FontSizeM, gfx.WeightMedium); width <= toastMaxWidth { + t.Fatalf("the title is %v wide, no wider than a toast: the test would prove nothing", width) + } + options := ToastOptions{Layer: 3, DismissID: gfx.Item(1)} + + short := PaintToast(&canvas, theme, viewport, ToastSnapshot{Title: "Saved", Type: ToastSuccess}, options) + if lines := requireTitleFitsBody(t, "short", &canvas, short); len(lines) != 1 { + t.Fatalf("a short title painted %d lines", len(lines)) + } + // A short title stays the compact one-line bar it was. + if want := 2*toastPaddingY + line; short.Bounds.Height != want { + t.Fatalf("short toast is %v high, want %v", short.Bounds.Height, want) + } + + canvas.Reset() + long := PaintToast(&canvas, theme, viewport, ToastSnapshot{Title: longToastTitle, Type: ToastSuccess}, options) + lines := requireTitleFitsBody(t, "long", &canvas, long) + if len(lines) < 2 { + t.Fatalf("the long title painted %d lines, want it wrapped", len(lines)) + } + if long.Bounds.Width != toastMaxWidth { + t.Fatalf("a title wider than a toast makes it %v wide, want the maximum %v", long.Bounds.Width, toastMaxWidth) + } + if want := 2*toastPaddingY + float32(len(lines))*line; long.Bounds.Height != want { + t.Fatalf("long toast is %v high with %d title lines, want %v", long.Bounds.Height, len(lines), want) + } + // It grows up from the same bottom edge, and nothing is dropped or added to the text. + if long.Bounds.Y+long.Bounds.Height != short.Bounds.Y+short.Bounds.Height { + t.Fatalf("short %+v, long %+v: want the bottom edge kept", short.Bounds, long.Bounds) + } + var joined []string + for index, text := range lines { + joined = append(joined, strings.TrimSpace(text.Value)) + if want := long.Body.Y + float32(index)*line; text.Bounds.Y != want || text.Bounds.Height != line { + t.Errorf("title line %d is at %v..%v, want %v high from %v", index, text.Bounds.Y, text.Bounds.Height, line, want) + } + } + if got := strings.Join(joined, " "); got != longToastTitle { + t.Fatalf("wrapped title reads %q, want %q", got, longToastTitle) + } + if long.Body.Y < long.Bounds.Y+toastPaddingY || long.Body.Y+long.Body.Height > long.Bounds.Y+long.Bounds.Height-toastPaddingY { + t.Fatalf("body %+v is outside the toast %+v", long.Body, long.Bounds) + } +} + +// The description goes under the last line of a wrapped title, and the icon and +// the close button stay on the first line, as they do beside a one-line title. +func TestToastWrappedTitleKeepsTheDescriptionBelowAndIconAndCloseOnTheFirstLine(t *testing.T) { + var canvas gfx.Canvas + theme := gfx.LightTheme() + metrics := theme.Metrics + viewport := gfx.Rect{Width: 1000, Height: 700} + title, line := longToastTitle, overlayLineHeight(metrics) + layout := PaintToast(&canvas, theme, viewport, ToastSnapshot{Title: title, Description: "Nothing else changed.", Type: ToastInfo}, ToastOptions{Layer: 3, DismissID: gfx.Item(1)}) + lines := requireTitleFitsBody(t, "described", &canvas, layout) + if len(lines) < 2 { + t.Fatalf("the title painted %d lines", len(lines)) + } + var description gfx.Text + for _, text := range canvas.Texts() { + if text.Style.Weight == gfx.WeightRegular && text.Style.Size == metrics.FontSizeS { + description = text + } + } + if description.Value != "Nothing else changed." { + t.Fatalf("description line = %q", description.Value) + } + titleBottom := layout.Body.Y + float32(len(lines))*line + if description.Bounds.Y != titleBottom+toastTextGap { + t.Errorf("description starts at %v, want %v under the last title line", description.Bounds.Y, titleBottom+toastTextGap) + } + if want := 2*toastPaddingY + float32(len(lines))*line + toastTextGap + toastDescriptionLineHeight(metrics); layout.Bounds.Height != want { + t.Errorf("toast is %v high, want %v", layout.Bounds.Height, want) + } + firstLine := layout.Body.Y + line/2 + for name, rect := range map[string]gfx.Rect{"icon": layout.Icon, "close": layout.Close} { + if center := rect.Y + rect.Height/2; center != firstLine { + t.Errorf("%s %+v is centered at %v, not on the first title line's %v", name, rect, center, firstLine) + } + } +} + +// A title wraps at the body whatever the toast carries beside it, or however +// narrow the window leaves it. +func TestToastTitleNeverOverflowsItsLabelWhateverSitsBesideIt(t *testing.T) { + theme := gfx.LightTheme() + for _, test := range []struct { + name string + viewport gfx.Rect + kind ToastType + options ToastOptions + }{ + {"close", gfx.Rect{Width: 1000, Height: 700}, ToastDefault, ToastOptions{DismissID: gfx.Item(1)}}, + {"no close", gfx.Rect{Width: 1000, Height: 700}, ToastDefault, ToastOptions{}}, + {"icon and close", gfx.Rect{Width: 1000, Height: 700}, ToastSuccess, ToastOptions{DismissID: gfx.Item(1)}}, + {"icon, action and close", gfx.Rect{Width: 1000, Height: 700}, ToastWarning, ToastOptions{DismissID: gfx.Item(1), ActionID: gfx.Item(2), ActionLabel: "Open library"}}, + {"icon and action, no close", gfx.Rect{Width: 1000, Height: 700}, ToastError, ToastOptions{ActionID: gfx.Item(2), ActionLabel: "Undo"}}, + {"narrow window", gfx.Rect{Width: 220, Height: 700}, ToastSuccess, ToastOptions{DismissID: gfx.Item(1)}}, + {"top right", gfx.Rect{X: 40, Y: 30, Width: 1000, Height: 700}, ToastInfo, ToastOptions{DismissID: gfx.Item(1), Position: ToastTopRight}}, + } { + t.Run(test.name, func(t *testing.T) { + var canvas gfx.Canvas + options := test.options + options.Layer = 3 + layout := PaintToast(&canvas, theme, test.viewport, ToastSnapshot{Title: longToastTitle, Type: test.kind}, options) + lines := requireTitleFitsBody(t, test.name, &canvas, layout) + if len(lines) < 2 { + t.Fatalf("the title painted %d lines", len(lines)) + } + if got := ToastHeight(&canvas, theme, test.viewport, ToastSnapshot{Title: longToastTitle, Type: test.kind}, options); got != layout.Bounds.Height { + t.Errorf("ToastHeight = %v, the painted toast is %v high", got, layout.Bounds.Height) + } + if layout.Bounds.X < test.viewport.X || layout.Bounds.X+layout.Bounds.Width > test.viewport.X+test.viewport.Width { + t.Errorf("toast %+v is outside the viewport %+v", layout.Bounds, test.viewport) + } + }) + } +} + +// A title that needs more than the wrapping allows ends in an ellipsis, as a +// description does, and the toast is as high as the lines it paints. +func TestToastEndlessTitleIsCutToTheLineLimitWithAnEllipsis(t *testing.T) { + var canvas gfx.Canvas + theme := gfx.LightTheme() + viewport := gfx.Rect{Width: 1000, Height: 700} + title := strings.Repeat(longToastTitle+" ", 12) + layout := PaintToast(&canvas, theme, viewport, ToastSnapshot{Title: title}, ToastOptions{Layer: 3, DismissID: gfx.Item(1)}) + lines := requireTitleFitsBody(t, "endless", &canvas, layout) + if len(lines) != overlayMaxLines || !strings.HasSuffix(lines[len(lines)-1].Value, "…") { + t.Fatalf("an endless title painted %d lines, the last %q", len(lines), lines[len(lines)-1].Value) + } + if want := 2*toastPaddingY + float32(overlayMaxLines)*overlayLineHeight(theme.Metrics); layout.Bounds.Height != want { + t.Fatalf("toast is %v high, want %v", layout.Bounds.Height, want) + } +} + +// An empty title keeps its line: the description stays where it was, below it. +func TestToastEmptyTitleKeepsItsLine(t *testing.T) { + var canvas gfx.Canvas + theme := gfx.LightTheme() + layout := PaintToast(&canvas, theme, gfx.Rect{Width: 1000, Height: 700}, ToastSnapshot{Description: "Only a description."}, ToastOptions{Layer: 3, DismissID: gfx.Item(1)}) + if want := 2*toastPaddingY + overlayLineHeight(theme.Metrics) + toastTextGap + toastDescriptionLineHeight(theme.Metrics); layout.Bounds.Height != want { + t.Fatalf("toast is %v high, want %v", layout.Bounds.Height, want) + } +} + +// A stack hands every card one width, and each card wraps its own title at that +// width: a card with a short title is still a one-line bar, and a card behind a +// tall one sits after the whole of it. +func TestSonnerStackWrapsTitlesAtTheSharedWidthAndStacksTheirHeights(t *testing.T) { + theme := gfx.LightTheme() + line := overlayLineHeight(theme.Metrics) + viewport := gfx.Rect{Width: 1000, Height: 700} + toasts := []SonnerToast{ + {Snapshot: ToastSnapshot{Title: "Saved", Type: ToastSuccess}, DismissID: gfx.Item(1)}, + {Snapshot: ToastSnapshot{Title: longToastTitle, Type: ToastInfo}, DismissID: gfx.Item(2)}, + {Snapshot: ToastSnapshot{Title: "Synced"}, DismissID: gfx.Item(3)}, + } + var canvas gfx.Canvas + expanded := PaintSonner(&canvas, theme, viewport, toasts, SonnerOptions{Layer: 20, Expanded: true}) + if expanded.Painted != 3 { + t.Fatalf("painted = %d", expanded.Painted) + } + // The one long title is what the other cards are measured against: they take + // its width, and none of the titles is wider than the label it is drawn in. + titleLines := map[string]int{} + for _, text := range canvas.Texts() { + if text.Style.Weight != gfx.WeightMedium || text.Value == "" { + continue + } + if width := canvas.TextWidthWeight(text.Value, text.Style.Size, text.Style.Weight); width > text.Bounds.Width { + t.Errorf("title line %q is %v wide in a label %v wide", text.Value, width, text.Bounds.Width) + } + switch text.Value { + case "Saved", "Synced": + titleLines[text.Value]++ + default: + titleLines["long"]++ + } + } + if titleLines["Saved"] != 1 || titleLines["Synced"] != 1 || titleLines["long"] < 2 { + t.Fatalf("title lines per card = %v", titleLines) + } + card := 2 * toastPaddingY + want := (card + line) + toastStackGap + (card + float32(titleLines["long"])*line) + toastStackGap + (card + line) + if expanded.Bounds.Height != want || expanded.Bounds.Y+expanded.Bounds.Height != viewport.Height-toastInset { + t.Fatalf("expanded stack %+v, want %v high ending at the inset edge", expanded.Bounds, want) + } + if expanded.Bounds.Width != toastMaxWidth { + t.Fatalf("expanded stack is %v wide, want the long title's %v", expanded.Bounds.Width, toastMaxWidth) + } + + // Collapsed, the cards share the bottom edge, each behind the front one 5% + // smaller and 10px higher, so the tall card's wrapped height is what reaches + // highest: 95% of it, lifted one peek. + canvas.Reset() + collapsed := PaintSonner(&canvas, theme, viewport, toasts, SonnerOptions{Layer: 20}) + tall := card + float32(titleLines["long"])*line + reach := max(card+line, (1-toastStackScale)*tall+toastPeek, (1-2*toastStackScale)*(card+line)+2*toastPeek) + bottom := viewport.Height - toastInset + if diff := collapsed.Bounds.Y - (bottom - reach); collapsed.Painted != 3 || collapsed.Bounds.Y+collapsed.Bounds.Height != bottom || diff < -0.01 || diff > 0.01 { + t.Fatalf("collapsed stack %+v, want it %v high ending at %v", collapsed.Bounds, reach, bottom) + } + + // The expanded order and the top position keep the same arithmetic. + canvas.Reset() + top := PaintSonner(&canvas, theme, viewport, toasts, SonnerOptions{Layer: 20, Expanded: true, Position: ToastTopRight}) + if top.Bounds.Height != want || top.Bounds.Y != viewport.Y+toastInset { + t.Fatalf("expanded top stack %+v, want %v high from the inset edge", top.Bounds, want) + } +} + +// Wrapping a title builds no string and allocates nothing once warm, alone or +// in a stack. +func TestToastWrappedTitlePaintIsAllocationFreeAfterWarmup(t *testing.T) { + var canvas gfx.Canvas + toasts := []SonnerToast{ + {Snapshot: ToastSnapshot{Title: longToastTitle, Description: "Nothing else changed.", Type: ToastSuccess}, Scope: gfx.Item(20), DismissID: gfx.Item(1), ActionID: gfx.Item(3), ActionLabel: "Open"}, + {Snapshot: ToastSnapshot{Title: "Saved"}, Scope: gfx.Item(21), DismissID: gfx.Item(2)}, + } + paint := func() { + canvas.Reset() + PaintToast(&canvas, gfx.DarkTheme(), gfx.Rect{Width: 800, Height: 600}, toasts[0].Snapshot, ToastOptions{Layer: 10, DismissID: gfx.Item(1)}) + PaintSonner(&canvas, gfx.DarkTheme(), gfx.Rect{Width: 800, Height: 600}, toasts, SonnerOptions{Layer: 16, Expanded: true}) + } + paint() + if allocations := testing.AllocsPerRun(100, paint); allocations != 0 { + t.Fatalf("wrapped toast allocations = %v", allocations) + } +} + func TestToastPositionPicksTheCornerAndLoadingAsksForFramesOnlyWhileShown(t *testing.T) { viewport := gfx.Rect{Width: 1000, Height: 700} var canvas gfx.Canvas @@ -144,6 +416,16 @@ func TestToastWithoutDismissIDHasNoCloseButton(t *testing.T) { } } +// Without a close button the body runs to the end of the row, which is the +// width it was measured at: a title that fits its measured line must fit its label. +func TestToastBodyHoldsItsTitleWithoutACloseButton(t *testing.T) { + var canvas gfx.Canvas + layout := PaintToast(&canvas, gfx.LightTheme(), gfx.Rect{Width: 1000, Height: 700}, ToastSnapshot{Title: "Copied to the clipboard"}, ToastOptions{Layer: 1}) + if lines := requireTitleFitsBody(t, "no close", &canvas, layout); len(lines) != 1 { + t.Fatalf("a short title painted %d lines", len(lines)) + } +} + // The queue's default timeout is one policy; AddFor lets one toast carry its // own, and a loading toast still never expires. func TestToastQueueAddForUsesTheToastsOwnTimeout(t *testing.T) {