Class
AdwToast
Description [src]
final class Adw.Toast : GObject.Object {
  /* No available fields */
}A helper object for AdwToastOverlay.
Toasts are meant to be passed into adw_toast_overlay_add_toast() as follows:
adw_toast_overlay_add_toast (overlay, adw_toast_new (_("Simple Toast"));
 
Toasts always have a close button. They emit the AdwToast::dismissed
signal when disappearing.
AdwToast:timeout determines how long the toast stays on screen, while
AdwToast:priority determines how it behaves if another toast is
already being displayed.
Actions
Toasts can have one button on them, with a label and an attached
GAction.
AdwToast *toast = adw_toast_new (_("Toast with Action"));
adw_toast_set_button_label (toast, _("_Example"));
adw_toast_set_action_name (toast, "win.example");
adw_toast_overlay_add_toast (overlay, toast);
 
Modifying toasts
Toasts can be modified after they have been shown. For this, an AdwToast
reference must be kept around while the toast is visible.
A common use case for this is using toasts as undo prompts that stack with each other, allowing to batch undo the last deleted items:
static void
toast_undo_cb (GtkWidget  *sender,
               const char *action,
               GVariant   *param)
{
  // Undo the deletion
}
static void
dismissed_cb (MyWindow *self)
{
  self->undo_toast = NULL;
  // Permanently delete the items
}
static void
delete_item (MyWindow *self,
             MyItem   *item)
{
  g_autofree char *title = NULL;
  int n_items;
  // Mark the item as waiting for deletion
  n_items = ... // The number of waiting items
  if (!self->undo_toast) {
    title = g_strdup_printf (_("ā%sā deleted"), ...);
    self->undo_toast = adw_toast_new (title);
    adw_toast_set_priority (self->undo_toast, ADW_TOAST_PRIORITY_HIGH);
    adw_toast_set_button_label (self->undo_toast, _("_Undo"));
    adw_toast_set_action_name (self->undo_toast, "toast.undo");
    g_signal_connect_swapped (self->undo_toast, "dismissed",
                              G_CALLBACK (dismissed_cb), self);
    adw_toast_overlay_add_toast (self->toast_overlay, self->undo_toast);
    return;
  }
  title =
    g_strdup_printf (ngettext ("<span font_features='tnum=1'>%d</span> item deleted",
                               "<span font_features='tnum=1'>%d</span> items deleted",
                               n_items), n_items);
  adw_toast_set_title (self->undo_toast, title);
}
static void
my_window_class_init (MyWindowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
  gtk_widget_class_install_action (widget_class, "toast.undo", NULL, toast_undo_cb);
}
 
| Available since: | 1.0 |