Toast

class Toast(val adwToastPointer: <Error class: unknown class><<Error class: unknown class>>)(source)

A helper object for class@ToastOverlay.

Toasts are meant to be passed into method@ToastOverlay.add_toast as follows:

adw_toast_overlay_add_toast (overlay, adw_toast_new (_("Simple Toast")));
toast-simple

Toasts always have a close button. They emit the signal@Toast::dismissed signal when disappearing.

property@Toast:timeout determines how long the toast stays on screen, while property@Toast:priority determines how it behaves if another toast is already being displayed.

Toast titles use Pango markup by default, set property@Toast:use-markup to FALSE if this is unwanted.

property@Toast:custom-title can be used to replace the title label with a custom widget.

Actions

Toasts can have one button on them, with a label and an attached iface@Gio.Action.

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);
toast-action

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) {
    self->undo_toast = adw_toast_new_format (_("ā€˜%sā€™ deleted"), ...);

    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);

  // Bump the toast timeout
  adw_toast_overlay_add_toast (self->toast_overlay, g_object_ref (self->undo_toast));
}

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);
}
toast-undo

Skipped during bindings generation

  • method set_action_target: Varargs parameter is not supported

  • method title: Property TypeInfo of getter and setter do not match

  • constructor new_format: Varargs parameter is not supported

Constructors

Link copied to clipboard
constructor(title: String)

Creates a new AdwToast.

constructor(adwToastPointer: <Error class: unknown class><<Error class: unknown class>>)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

The name of the associated action.

Link copied to clipboard
var actionTarget: <Error class: unknown class>?

The parameter for action invocations.

Link copied to clipboard
val adwToastPointer: <Error class: unknown class><<Error class: unknown class>>
Link copied to clipboard

The label to show on the button.

Link copied to clipboard
var customTitle: <Error class: unknown class>?

The custom title widget.

Link copied to clipboard

The priority of the toast.

Link copied to clipboard
var timeout: <Error class: unknown class>

The timeout of the toast, in seconds.

Link copied to clipboard

Whether to use Pango markup for the toast title.

Functions

Link copied to clipboard
fun dismiss()

Dismisses @self.

Link copied to clipboard

Emits the "button-clicked" signal. See onButtonClicked.

Link copied to clipboard

Emits the "dismissed" signal. See onDismissed.

Link copied to clipboard

Gets the title that will be displayed on the toast.

Link copied to clipboard
fun onButtonClicked(connectFlags: <Error class: unknown class> = ConnectFlags(0u), handler: () -> Unit): <Error class: unknown class>

Emitted after the button has been clicked.

Link copied to clipboard
fun onDismissed(connectFlags: <Error class: unknown class> = ConnectFlags(0u), handler: () -> Unit): <Error class: unknown class>

Emitted when the toast has been dismissed.

Link copied to clipboard
fun setDetailedActionName(detailedActionName: String? = null)

Sets the action name and its parameter.

Link copied to clipboard
fun setTitle(title: String)

Sets the title that will be displayed on the toast.