Class Toast

All Implemented Interfaces:
PointerInterface

public class Toast extends Object
A helper object for [class@ToastOverlay].

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

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

<picture>
<source srcset="toast-simple-dark.png" media="(prefers-color-scheme: dark)">
<img src="toast-simple.png" alt="toast-simple">
</picture>

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.

[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].

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

<picture>
<source srcset="toast-action-dark.png" media="(prefers-color-scheme: dark)">
<img src="toast-action.png" alt="toast-action">
</picture>

## 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:

```c

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

<picture>
<source srcset="toast-undo-dark.png" media="(prefers-color-scheme: dark)">
<img src="toast-undo.png" alt="toast-undo">
</picture>

https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.Toast.html

  • Field Details

  • Constructor Details

    • Toast

      public Toast(PointerContainer pointer)
    • Toast

      public Toast(@Nonnull Str title)
      Creates a new `AdwToast`.

      The toast will use @title as its title.

      @title can be marked up with the Pango text markup language.
      Parameters:
      title - the title to be displayed
    • Toast

      public Toast(String title)
      Creates a new `AdwToast`.

      The toast will use @title as its title.

      @title can be marked up with the Pango text markup language.
      Parameters:
      title - the title to be displayed
  • Method Details

    • getClassHandler

      public static ClassHandler getClassHandler()
    • newFormatToast

      public static Toast newFormatToast(@Nonnull Str format, Object... _elipse)
      Creates a new `AdwToast`.

      The toast will use the format string as its title.

      See also: [ctor@Toast.new]
      Parameters:
      format - the formatted string for the toast title
      _elipse - the parameters to insert into the format string
      Returns:
      the newly created toast object
    • newFormatToast

      public static Toast newFormatToast(String format, Object... _elipse)
      Creates a new `AdwToast`.

      The toast will use the format string as its title.

      See also: [ctor@Toast.new]
      Parameters:
      format - the formatted string for the toast title
      _elipse - the parameters to insert into the format string
      Returns:
      the newly created toast object
    • dismiss

      public void dismiss()
      Dismisses @self.

      Does nothing if @self has already been dismissed, or hasn't been added to an
      [class@ToastOverlay].
    • getActionName

      public Str getActionName()
      Gets the name of the associated action.
      Returns:
      the action name
    • getActionTargetValue

      public Variant getActionTargetValue()
      Gets the parameter for action invocations.
      Returns:
      the action target
    • getButtonLabel

      public Str getButtonLabel()
      Gets the label to show on the button.
      Returns:
      the button label
    • getCustomTitle

      public Widget getCustomTitle()
      Gets the custom title widget of @self.
      Returns:
      the custom title widget
    • getPriority

      public int getPriority()
      Gets priority for @self.
      Returns:
      the priority
    • getTimeout

      public int getTimeout()
      Gets timeout for @self.
      Returns:
      the timeout
    • getTitle

      public Str getTitle()
      Gets the title that will be displayed on the toast.

      If a custom title has been set with [method@Adw.Toast.set_custom_title]
      the return value will be %NULL.
      Returns:
      the title
    • setActionName

      public void setActionName(@Nullable Str action_name)
      Sets the name of the associated action.

      It will be activated when clicking the button.

      See [property@Toast:action-target].
      Parameters:
      action_name - the action name
    • setActionName

      public void setActionName(String action_name)
      Sets the name of the associated action.

      It will be activated when clicking the button.

      See [property@Toast:action-target].
      Parameters:
      action_name - the action name
    • setActionTarget

      public void setActionTarget(@Nullable Str format_string, Object... _elipse)
      Sets the parameter for action invocations.

      This is a convenience function that calls [ctor@GLib.Variant.new] for
      @format_string and uses the result to call
      [method@Toast.set_action_target_value].

      If you are setting a string-valued target and want to set
      the action name at the same time, you can use
      [method@Toast.set_detailed_action_name].
      Parameters:
      format_string - a variant format string
      _elipse - arguments appropriate for @target_format
    • setActionTarget

      public void setActionTarget(String format_string, Object... _elipse)
      Sets the parameter for action invocations.

      This is a convenience function that calls [ctor@GLib.Variant.new] for
      @format_string and uses the result to call
      [method@Toast.set_action_target_value].

      If you are setting a string-valued target and want to set
      the action name at the same time, you can use
      [method@Toast.set_detailed_action_name].
      Parameters:
      format_string - a variant format string
      _elipse - arguments appropriate for @target_format
    • setActionTargetValue

      public void setActionTargetValue(@Nullable Variant action_target)
      Sets the parameter for action invocations.

      If the @action_target variant has a floating reference this function
      will sink it.
      Parameters:
      action_target - the action target
    • setButtonLabel

      public void setButtonLabel(@Nullable Str 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 [property@Toast:action-name].
      Parameters:
      button_label - a button label
    • setButtonLabel

      public void setButtonLabel(String 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 [property@Toast:action-name].
      Parameters:
      button_label - a button label
    • setCustomTitle

      public void setCustomTitle(@Nullable Widget widget)
      Sets the custom title widget of @self.

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

      Setting a custom title will unset [property@Toast:title].
      Parameters:
      widget - the custom title widget
    • setDetailedActionName

      public void setDetailedActionName(@Nullable Str detailed_action_name)
      Sets the action name and its parameter.

      @detailed_action_name is a string in the format accepted by
      [func@Gio.Action.parse_detailed_name].
      Parameters:
      detailed_action_name - the detailed action name
    • setDetailedActionName

      public void setDetailedActionName(String detailed_action_name)
      Sets the action name and its parameter.

      @detailed_action_name is a string in the format accepted by
      [func@Gio.Action.parse_detailed_name].
      Parameters:
      detailed_action_name - the detailed action name
    • setPriority

      public void setPriority(int priority)
      Sets priority for @self.

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

      If @priority is `ADW_TOAST_PRIORITY_NORMAL`, the toast will be queued.

      If @priority is `ADW_TOAST_PRIORITY_HIGH`, the toast will be displayed
      immediately, pushing the previous toast into the queue instead.
      Parameters:
      priority - the priority
    • setTimeout

      public void setTimeout(int 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.
      Parameters:
      timeout - the timeout
    • setTitle

      public void setTitle(@Nonnull Str 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 [property@Toast:custom-title].

      If [property@Toast:custom-title] is set, it will be used instead.
      Parameters:
      title - a title
    • setTitle

      public void setTitle(String 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 [property@Toast:custom-title].

      If [property@Toast:custom-title] is set, it will be used instead.
      Parameters:
      title - a title
    • onButtonClicked

      public SignalHandler onButtonClicked(Toast.OnButtonClicked signal)
      Connect to signal "button-clicked".
      See Toast.OnButtonClicked.onButtonClicked() for signal description.
      Field SIGNAL_ON_BUTTON_CLICKED contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onDismissed

      public SignalHandler onDismissed(Toast.OnDismissed signal)
      Connect to signal "dismissed".
      See Toast.OnDismissed.onDismissed() for signal description.
      Field SIGNAL_ON_DISMISSED contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • getTypeID

      public static long getTypeID()
    • getParentTypeID

      public static long getParentTypeID()
    • getTypeSize

      public static TypeSystem.TypeSize getTypeSize()
    • getParentTypeSize

      public static TypeSystem.TypeSize getParentTypeSize()
    • getInstanceSize

      public static int getInstanceSize()