[libadwaita/ebassi/toast-format: 1/3] toast: Add convenience format constructor
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libadwaita/ebassi/toast-format: 1/3] toast: Add convenience format constructor
- Date: Mon, 28 Feb 2022 17:20:31 +0000 (UTC)
commit 600f81a894b33a3e3c7cfcf2f23bb2135ea31a5a
Author: Emmanuele Bassi <ebassi gnome org>
Date: Mon Feb 28 17:10:32 2022 +0000
toast: Add convenience format constructor
Callers of adw_toast_new() will likely try and pass a
formatted string; instead of requiring an additional step
outside the constructor to allocate a formatted string using
g_strdup_printf(), it might be more convenient to provide
that kind of functionality out of the box.
An example is:
adw_toast_overlay_add_toast (overlay,
adw_toast_new_format ("The answer is: %d", answer));
src/adw-toast.c | 44 +++++++++++++++++++++++++++++++++++++++-----
src/adw-toast.h | 5 ++++-
2 files changed, 43 insertions(+), 6 deletions(-)
---
diff --git a/src/adw-toast.c b/src/adw-toast.c
index e05d940b..681fd698 100644
--- a/src/adw-toast.c
+++ b/src/adw-toast.c
@@ -10,6 +10,8 @@
#include "adw-macros-private.h"
+#include <stdarg.h>
+
/**
* AdwToastPriority:
* @ADW_TOAST_PRIORITY_NORMAL: the toast will be queued if another toast is
@@ -102,9 +104,7 @@
* n_items = ... // The number of waiting items
*
* if (!self->undo_toast) {
- * title = g_strdup_printf (_("ā%sā deleted"), ...);
- *
- * self->undo_toast = adw_toast_new (title);
+ * self->undo_toast = adw_toast_new ("");
*
* adw_toast_set_priority (self->undo_toast, ADW_TOAST_PRIORITY_HIGH);
* adw_toast_set_button_label (self->undo_toast, _("_Undo"));
@@ -114,8 +114,6 @@
* G_CALLBACK (dismissed_cb), self);
*
* adw_toast_overlay_add_toast (self->toast_overlay, self->undo_toast);
- *
- * return;
* }
*
* title =
@@ -436,6 +434,42 @@ adw_toast_new (const char *title)
NULL);
}
+/**
+ * adw_toast_new_format:
+ * @format: the formatted string for the toast title
+ * @...: the parameters to insert into the format string
+ *
+ * Creates a new `AdwToast`.
+ *
+ * The toast will use the format string as its title.
+ *
+ * See also: [ctor Toast new]
+ *
+ * Returns: the newly created toast object
+ *
+ * Since: 1.2
+ */
+AdwToast *
+adw_toast_new_format (const char *format,
+ ...)
+{
+ AdwToast *res;
+ va_list args;
+ char *title;
+
+ va_start (args, format);
+ title = g_strdup_vprintf (format, args);
+ va_end (args);
+
+ res = g_object_new (ADW_TYPE_TOAST,
+ "title", title,
+ NULL);
+
+ g_free (title);
+
+ return res;
+}
+
/**
* adw_toast_get_title: (attributes org.gtk.Method.get_property=title)
* @self: a toast
diff --git a/src/adw-toast.h b/src/adw-toast.h
index a6adfe39..a6a47e67 100644
--- a/src/adw-toast.h
+++ b/src/adw-toast.h
@@ -28,7 +28,10 @@ ADW_AVAILABLE_IN_ALL
G_DECLARE_FINAL_TYPE (AdwToast, adw_toast, ADW, TOAST, GObject)
ADW_AVAILABLE_IN_ALL
-AdwToast *adw_toast_new (const char *title) G_GNUC_WARN_UNUSED_RESULT;
+AdwToast *adw_toast_new (const char *title) G_GNUC_WARN_UNUSED_RESULT;
+ADW_AVAILABLE_IN_ALL
+AdwToast *adw_toast_new_format (const char *format,
+ ...) G_GNUC_WARN_UNUSED_RESULT;
ADW_AVAILABLE_IN_ALL
const char *adw_toast_get_title (AdwToast *self);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]