1.67 <AdwToast>

Class: <AdwToast>

It derives from <GObject>.

A helper object for ToastOverlay.

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

adw_toast_overlay_add_toast (overlay, adw_toast_new (_("Simple Toast")));

Toasts always have a close button. They emit the "dismissed" signal when disappearing.

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

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

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

Properties

Property: toast:action-name self

The name of the associated action.

It will be activated when clicking the button.

See Toast:action-target.

Its value is a string, or #f.

Set it with (set! (toast:action-name self) value).

Property: toast:action-target self

The parameter for action invocations.

Its value is a <GVariant>, or #f.

Set it with (set! (toast:action-target self) value).

Property: toast:button-label self

The label to show on the button.

Underlines in the button text can be used to indicate a mnemonic.

If set to NULL, the button won’t be shown.

See Toast:action-name.

Its value is a string, or #f.

Set it with (set! (toast:button-label self) value).

Property: toast:custom-title self

The custom title widget.

It will be displayed instead of the title if set. In this case, Toast:title is ignored.

Setting a custom title will unset Toast:title.

Its value is a <GtkWidget>, or #f.

Set it with (set! (toast:custom-title self) value).

Property: toast:priority self

The priority of the toast.

Priority controls how the toast behaves when another toast is already being displayed.

If the priority is Adw.ToastPriority.normal, the toast will be queued.

If the priority is Adw.ToastPriority.high, the toast will be displayed immediately, pushing the previous toast into the queue instead.

Its value is a <AdwToastPriority> (or, to set it, one of its symbols).

Set it with (set! (toast:priority self) value).

Property: toast:timeout self

The timeout of the toast, in seconds.

If timeout is 0, the toast is displayed indefinitely until manually dismissed.

Toasts cannot disappear while being hovered, pressed (on touchscreen), or have keyboard focus inside them.

Its value is an unsigned 32-bit integer.

Set it with (set! (toast:timeout self) value).

Property: toast:title self

The title of the toast.

The title can be marked up with the Pango text markup language.

Setting a title will unset Toast:custom-title.

If Toast:custom-title is set, it will be used instead.

Its value is a string, or #f.

Set it with (set! (toast:title self) value).

Property: toast:use-markup self

Whether to use Pango markup for the toast title.

See also parse-markup.

Its value is a boolean.

Set it with (set! (toast:use-markup self) value).

Procedures

Method: toast:dismiss self
Method: dismiss self

Dismisses self.

Does nothing if self has already been dismissed, or hasn’t been added to an ToastOverlay.

self

Passed: a <AdwToast>.

a toast

Method: toast:get-action-name self ⇒ %return
Method: get-action-name self ⇒ %return

Gets the name of the associated action.

self

Passed: a <AdwToast>.

a toast

%return

Returned: a string, or #f.

the action name

Method: toast:get-action-target-value self ⇒ %return
Method: get-action-target-value self ⇒ %return

Gets the parameter for action invocations.

self

Passed: a <AdwToast>.

a toast

%return

Returned: a <GVariant>, or #f.

the action target

Method: toast:get-button-label self ⇒ %return
Method: get-button-label self ⇒ %return

Gets the label to show on the button.

self

Passed: a <AdwToast>.

a toast

%return

Returned: a string, or #f.

the button label

Method: toast:get-custom-title self ⇒ %return
Method: get-custom-title self ⇒ %return

Gets the custom title widget of self.

self

Passed: a <AdwToast>.

a toast

%return

Returned: a <GtkWidget>, or #f.

the custom title widget

Method: toast:get-priority self ⇒ %return
Method: get-priority self ⇒ %return

Gets priority for self.

self

Passed: a <AdwToast>.

a toast

%return

Returned: a <AdwToastPriority>.

the priority

Method: toast:get-timeout self ⇒ %return
Method: get-timeout self ⇒ %return

Gets timeout for self.

self

Passed: a <AdwToast>.

a toast

%return

Returned: an unsigned 32-bit integer.

the timeout

Method: toast:get-title self ⇒ %return
Method: get-title self ⇒ %return

Gets the title that will be displayed on the toast.

If a custom title has been set with toast:set-custom-title the return value will be #f.

self

Passed: a <AdwToast>.

a toast

%return

Returned: a string, or #f.

the title

Method: toast:get-use-markup? self ⇒ %return
Method: get-use-markup? self ⇒ %return

Gets whether to use Pango markup for the toast title.

self

Passed: a <AdwToast>.

a toast

%return

Returned: a boolean.

whether the toast uses markup

Procedure: toast:new title ⇒ %return

Creates a new <AdwToast>.

The toast will use title as its title.

title can be marked up with the Pango text markup language.

title

Passed: a string.

the title to be displayed

%return

Returned: a <AdwToast>.

the new created <AdwToast>

Method: toast:set-action-name self [action-name]
Method: set-action-name self [action-name]

Sets the name of the associated action.

It will be activated when clicking the button.

See Toast:action-target.

self

Passed: a <AdwToast>.

a toast

action-name

Passed, or left out: a string, or #f.

the action name

Method: toast:set-action-target-value self [action-target]
Method: set-action-target-value self [action-target]

Sets the parameter for action invocations.

If the action-target variant has a floating reference this function will sink it.

self

Passed: a <AdwToast>.

a toast

action-target

Passed, or left out: a <GVariant>, or #f.

the action target

Method: toast:set-button-label self [button-label]
Method: set-button-label self [button-label]

Sets the label to show on the button.

Underlines in the button text can be used to indicate a mnemonic.

If set to NULL, the button won’t be shown.

See Toast:action-name.

self

Passed: a <AdwToast>.

a toast

button-label

Passed, or left out: a string, or #f.

a button label

Method: toast:set-custom-title self [widget]
Method: set-custom-title self [widget]

Sets the custom title widget of self.

It will be displayed instead of the title if set. In this case, Toast:title is ignored.

Setting a custom title will unset Toast:title.

self

Passed: a <AdwToast>.

a toast

widget

Passed, or left out: a <GtkWidget>, or #f.

the custom title widget

Method: toast:set-detailed-action-name self [detailed-action-name]
Method: set-detailed-action-name self [detailed-action-name]

Sets the action name and its parameter.

detailed-action-name is a string in the format accepted by action:parse-detailed-name.

self

Passed: a <AdwToast>.

a toast

detailed-action-name

Passed, or left out: a string, or #f.

the detailed action name

Method: toast:set-priority self priority
Method: set-priority self priority

Sets priority for self.

Priority controls how the toast behaves when another toast is already being displayed.

If priority is Adw.ToastPriority.normal, the toast will be queued.

If priority is Adw.ToastPriority.high, the toast will be displayed immediately, pushing the previous toast into the queue instead.

self

Passed: a <AdwToast>.

a toast

priority

Passed: a <AdwToastPriority>, or one of its symbols.

the priority

Method: toast:set-timeout self timeout
Method: set-timeout self timeout

Sets timeout for self.

If timeout is 0, the toast is displayed indefinitely until manually dismissed.

Toasts cannot disappear while being hovered, pressed (on touchscreen), or have keyboard focus inside them.

self

Passed: a <AdwToast>.

a toast

timeout

Passed: an unsigned 32-bit integer.

the timeout

Method: toast:set-title self title
Method: set-title self title

Sets the title that will be displayed on the toast.

The title can be marked up with the Pango text markup language.

Setting a title will unset Toast:custom-title.

If Toast:custom-title is set, it will be used instead.

self

Passed: a <AdwToast>.

a toast

title

Passed: a string.

a title

Method: toast:set-use-markup self use-markup
Method: set-use-markup self use-markup

Whether to use Pango markup for the toast title.

See also parse-markup.

self

Passed: a <AdwToast>.

a toast

use-markup

Passed: a boolean.

whether to use markup

Signals

Signal: toast:button-clicked

Connect a handler with (connect object toast:button-clicked handler). It is called as (handler self).

Emitted after the button has been clicked.

It can be used as an alternative to setting an action.

self

Passed: a <AdwToast>.

Signal: toast:dismissed

Connect a handler with (connect object toast:dismissed handler). It is called as (handler self).

Emitted when the toast has been dismissed.

self

Passed: a <AdwToast>.