[libadwaita/ebassi/toast-format: 28/31] toast: Add convenience format constructor




commit 4459dba4aff21cc072e08b177c0f5110bc662c6c
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 | 42 +++++++++++++++++++++++++++++++++++++++---
 src/adw-toast.h |  5 ++++-
 2 files changed, 43 insertions(+), 4 deletions(-)
---
diff --git a/src/adw-toast.c b/src/adw-toast.c
index e0de2d93..9ecf6730 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_format (_("ā€˜%sā€™ deleted"), ...);
  *
  *     adw_toast_set_priority (self->undo_toast, ADW_TOAST_PRIORITY_HIGH);
  *     adw_toast_set_button_label (self->undo_toast, _("_Undo"));
@@ -436,6 +436,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]