Class Toast

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public final class Toast extends GObject

A helper object for ToastOverlay.

Toasts are meant to be passed into ToastOverlay.addToast(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 Toast::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 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
  • Constructor Details

    • Toast

      public Toast(MemorySegment address)
      Create a Toast instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • 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
    • Toast

      public Toast()
      Create a new Toast.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the Toast class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected Toast asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • format

      public static Toast format(String format, Object... varargs)

      Creates a new AdwToast.

      The toast will use the format string as its title.

      See also: Toast()

      Parameters:
      format - the formatted string for the toast title
      varargs - the parameters to insert into the format string
      Returns:
      the newly created toast object
      Since:
      1.2
    • dismiss

      public void dismiss()

      Dismisses self.

      Does nothing if this Toast has already been dismissed, or hasn't been added to an ToastOverlay.

    • getActionName

      public @Nullable String getActionName()
      Gets the name of the associated action.
      Returns:
      the action name
    • getActionTargetValue

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

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

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

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

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

      public @Nullable String getTitle()

      Gets the title that will be displayed on the toast.

      If a custom title has been set with setCustomTitle(Widget) the return value will be null.

      Returns:
      the title
    • getUseMarkup

      public boolean getUseMarkup()
      Gets whether to use Pango markup for the toast title.
      Returns:
      whether the toast uses markup
      Since:
      1.4
    • setActionName

      public void setActionName(@Nullable String actionName)

      Sets the name of the associated action.

      It will be activated when clicking the button.

      See Toast:action-target.

      Parameters:
      actionName - the action name
    • setActionTarget

      public void setActionTarget(@Nullable String formatString, Object... varargs)

      Sets the parameter for action invocations.

      This is a convenience function that calls Variant(String, Object...) for formatString and uses the result to call setActionTargetValue(Variant).

      If you are setting a string-valued target and want to set the action name at the same time, you can use setDetailedActionName(String).

      Parameters:
      formatString - a variant format string
      varargs - arguments appropriate for targetFormat
    • setActionTargetValue

      public void setActionTargetValue(@Nullable Variant actionTarget)

      Sets the parameter for action invocations.

      If the actionTarget variant has a floating reference this function will sink it.

      Parameters:
      actionTarget - the action target
    • setButtonLabel

      public void setButtonLabel(@Nullable String buttonLabel)

      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.

      Parameters:
      buttonLabel - 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, Toast:title is ignored.

      Setting a custom title will unset Toast:title.

      Parameters:
      widget - the custom title widget
      Since:
      1.2
    • setDetailedActionName

      public void setDetailedActionName(@Nullable String detailedActionName)

      Sets the action name and its parameter.

      detailedActionName is a string in the format accepted by Action#parseDetailedName.

      Parameters:
      detailedActionName - the detailed action name
    • setPriority

      public void setPriority(ToastPriority 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.

      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(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 Toast:custom-title.

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

      Parameters:
      title - a title
    • setUseMarkup

      public void setUseMarkup(boolean useMarkup)

      Whether to use Pango markup for the toast title.

      See also Pango#parseMarkup.

      Parameters:
      useMarkup - whether to use markup
      Since:
      1.4
    • onButtonClicked

      Emitted after the button has been clicked.

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

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      Since:
      1.2
      See Also:
    • emitButtonClicked

      public void emitButtonClicked()
      Emits the "button-clicked" signal. See onButtonClicked(Toast.ButtonClickedCallback).
    • onDismissed

      Emitted when the toast has been dismissed.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitDismissed

      public void emitDismissed()
      Emits the "dismissed" signal. See onDismissed(Toast.DismissedCallback).
    • builder

      public static Toast.Builder<? extends Toast.Builder> builder()
      A Toast.Builder object constructs a Toast with the specified properties. Use the various set...() methods to set properties, and finish construction with Toast.Builder.build().
      Returns:
      the builder object