Toast
Brief notifications that stack at an edge of the window, with optional actions.
Loading the live example…
import (
"fmt"
"time"
"github.com/egoist/mygo/ui"
"github.com/oneo-me/onevra/source/components"
)
type notifications struct {
items int
}
func (n *notifications) View(c *ui.Context) {
t := c.Theme()
ui.Column(c).Fill().Center().Gap(t.Space(3)).Children(func() {
ui.Row(c).Gap(8).Wrap().Justify(ui.Center).Children(func() {
if components.Button(c, "Saved", components.ButtonSecondary).Clicked() {
c.AddToast(ui.Toast{Title: "Changes saved", Type: components.ToastSuccess})
}
if components.Button(c, "Offline", components.ButtonSecondary).Clicked() {
c.AddToast(ui.Toast{Title: "You are offline", Description: "Changes sync when you reconnect.",
Type: components.ToastWarning})
}
if components.Button(c, "Delete item", components.ButtonSecondary).Disabled(n.items == 0).Clicked() {
n.items--
c.AddToast(ui.Toast{Title: "Item deleted", Action: "Undo", Timeout: 8 * time.Second,
OnAction: func() { n.items++ }})
}
})
components.Text(c, fmt.Sprintf("%d items", n.items)).TextColor(t.TextMuted)
})
}Add toasts with MyGo's c.AddToast and close them with c.CloseToast; MyGo owns the queue, timing, pausing on hover and Escape. Onevra draws them: set Type to ToastInfo, ToastSuccess, ToastWarning or ToastError for a status icon, and Action with OnAction for a button such as Undo.
Shells build the toasts themselves; place them with ShellOptions.Toasts. A window without a shell calls components.Toasts after its content. Toasts with the same ID replace each other, which suits progress.
API
Toasts
func Toasts(c *ui.Context, options ...ToastOptions) ui.ElementToasts builds the window's MyGo toasts in the Onevra style, replacing MyGo's default look, and returns the viewport. Applications show and close toasts with MyGo's Context.Toast, ToastAction, AddToast and CloseToast; MyGo owns the queue, timing, pausing, Escape dismissal and announcements.
Shell calls Toasts after its content with ShellOptions.Toasts, so applications call it only in standalone hosts; the first options value applies. A view that builds its own ToastViewportBase earlier in the frame keeps its look. Toasts stack at the top center by default, each with an optional action and a close button named by the "onevra.closeToast" message. Older toasts tuck behind the newest one, each peeking out a little further and drawn a little smaller, and fan out while the pointer rests on the stack or the keyboard focus is in it.
pkg.go.devToastOptions
ToastOptions customizes where Toasts places the window's toasts. The zero value stacks at the top center. Inset adds DIPs at the placement's edge beyond the standard margin, for example to clear a title bar; Shell adds its header height to top placements itself.
Prop
Type
ToastPlacement
type ToastPlacement intToastPlacement selects the window edge and side where toasts stack. The zero value stacks at the top center. Top placements put the newest toast first, nearest the top edge, and stack older ones downward; bottom placements put it last, nearest the bottom edge.
const (
ToastTop ToastPlacement = iota
ToastTopStart
ToastTopEnd
ToastBottom
ToastBottomStart
ToastBottomEnd
)Methods
func (p ToastPlacement) Top() boolTop reports whether p places toasts at the top edge.
ToastContent
ToastContent is optional ui.Toast.Data for content slots. Leading replaces the type's status icon, for example with an image or a progress indicator; it runs synchronously while Toasts builds the toast.
Prop
Type