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

toast-simple

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.

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

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

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

Hierarchy

hierarchy this AdwToast ancestor_0 GObject ancestor_0--this

Ancestors

Constructors

adw_toast_new

Creates a new AdwToast.

adw_toast_new_format

Creates a new AdwToast.

since: 1.2

Instance methods

adw_toast_dismiss

Dismisses self.

adw_toast_get_action_name

Gets the name of the associated action.

adw_toast_get_action_target_value

Gets the parameter for action invocations.

adw_toast_get_button_label

Gets the label to show on the button.

adw_toast_get_custom_title

Gets the custom title widget of self.

since: 1.2

adw_toast_get_priority

Gets priority for self.

adw_toast_get_timeout

Gets timeout for self.

adw_toast_get_title

Gets the title that will be displayed on the toast.

adw_toast_get_use_markup

Gets whether to use Pango markup for the toast title.

since: 1.4

adw_toast_set_action_name

Sets the name of the associated action.

adw_toast_set_action_target

Sets the parameter for action invocations.

adw_toast_set_action_target_value

Sets the parameter for action invocations.

adw_toast_set_button_label

Sets the label to show on the button.

adw_toast_set_custom_title

Sets the custom title widget of self.

since: 1.2

adw_toast_set_detailed_action_name

Sets the action name and its parameter.

adw_toast_set_priority

Sets priority for self.

adw_toast_set_timeout

Sets timeout for self.

adw_toast_set_title

Sets the title that will be displayed on the toast.

adw_toast_set_use_markup

Whether to use Pango markup for the toast title.

since: 1.4

Methods inherited from GObject (43)

Please see GObject for a full list of methods.

Properties

Adw.Toast:action-name

The name of the associated action.

Adw.Toast:action-target

The parameter for action invocations.

Adw.Toast:button-label

The label to show on the button.

Adw.Toast:custom-title

The custom title widget.

since: 1.2

Adw.Toast:priority

The priority of the toast.

Adw.Toast:timeout

The timeout of the toast, in seconds.

Adw.Toast:title

The title of the toast.

Adw.Toast:use-markup

Whether to use Pango markup for the toast title.

since: 1.4

Signals

Adw.Toast::button-clicked

Emitted after the button has been clicked.

since: 1.2

Adw.Toast::dismissed

Emitted when the toast has been dismissed.

Signals inherited from GObject (1)
GObject::notify

The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.

Class structure

struct AdwToastClass {
  GObjectClass parent_class;
  
}

No description available.

Class members
parent_class: GObjectClass

No description available.