Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions components/sonner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
82 changes: 50 additions & 32 deletions components/toast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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{}
}
Expand Down Expand Up @@ -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()}
Expand All @@ -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()
Expand Down
Loading
Loading