[gnome-software/1391-style-storage-units-differently: 6/6] gs-storage-context-dialog: Format size value and size unit differently




commit 22003bc592c9cb097740a02db7d52a30bc7bd0db
Author: Milan Crha <mcrha redhat com>
Date:   Mon Apr 11 14:11:33 2022 +0200

    gs-storage-context-dialog: Format size value and size unit differently
    
    This follows the new design for the dialog. It depends on a new glib API,
    thus checks for it in configure time, instead of bumping the glib version.
    
    Closes https://gitlab.gnome.org/GNOME/gnome-software/-/issues/1391

 meson.build                     |  5 +++++
 src/gs-storage-context-dialog.c | 32 +++++++++++++++++++++++++++++---
 2 files changed, 34 insertions(+), 3 deletions(-)
---
diff --git a/meson.build b/meson.build
index aeeb72a06..b336a3b8e 100644
--- a/meson.build
+++ b/meson.build
@@ -157,6 +157,11 @@ libadwaita = dependency('libadwaita-1',
   ]
 )
 
+# This should be available in GLib 2.74
+if meson.get_compiler('c').has_header_symbol('glib.h', 'G_FORMAT_SIZE_ONLY_VALUE', dependencies: glib)
+  conf.set('HAVE_G_FORMAT_SIZE_ONLY_VALUE', '1')
+endif
+
 libsysprof_capture_dep = dependency('sysprof-capture-4',
   required: get_option('sysprof'),
   default_options: [
diff --git a/src/gs-storage-context-dialog.c b/src/gs-storage-context-dialog.c
index 4699f5c65..03dc53afe 100644
--- a/src/gs-storage-context-dialog.c
+++ b/src/gs-storage-context-dialog.c
@@ -69,6 +69,26 @@ typedef enum {
        MATCH_STATE_UNKNOWN,
 } MatchState;
 
+static gchar *
+format_size (guint64 size_bytes,
+            gboolean *out_is_markup)
+{
+#ifdef HAVE_G_FORMAT_SIZE_ONLY_VALUE
+       g_autofree gchar *value_str = g_format_size_full (size_bytes, G_FORMAT_SIZE_ONLY_VALUE);
+       g_autofree gchar *unit_str = g_format_size_full (size_bytes, G_FORMAT_SIZE_ONLY_UNIT);
+       g_autofree gchar *value_escaped = g_markup_escape_text (value_str, -1);
+       g_autofree gchar *unit_escaped = g_markup_printf_escaped ("<span font_size='x-small'>%s</span>", 
unit_str);
+       *out_is_markup = TRUE;
+       /* Translators: This is to construct a disk size string consisting of the value and its unit, while
+        * the unit is drawn with a smaller font. If you need to flip the order, then you can use "%2$s %1$s".
+        * Example result: "13.0 MB" */
+       return g_strdup_printf (C_("format-size", "%s %s"), value_escaped, unit_escaped);
+#else /* HAVE_G_FORMAT_SIZE_ONLY_VALUE */
+       *out_is_markup = FALSE;
+       return g_format_size (size_bytes);
+#endif /* HAVE_G_FORMAT_SIZE_ONLY_VALUE */
+}
+
 /* The arguments are all non-nullable. */
 static void
 add_size_row (GtkListBox   *list_box,
@@ -79,6 +99,7 @@ add_size_row (GtkListBox   *list_box,
 {
        GtkListBoxRow *row;
        g_autofree gchar *size_bytes_str = NULL;
+       gboolean is_markup = FALSE;
 
        if (size_bytes == GS_APP_SIZE_UNKNOWABLE)
                /* Translators: This is shown in a bubble if the storage
@@ -92,10 +113,11 @@ add_size_row (GtkListBox   *list_box,
                 * possible. */
                size_bytes_str = g_strdup (_("None"));
        else
-               size_bytes_str = g_format_size (size_bytes);
+               size_bytes_str = format_size (size_bytes, &is_markup);
 
        row = gs_context_dialog_row_new_text (size_bytes_str, GS_CONTEXT_DIALOG_ROW_IMPORTANCE_NEUTRAL,
                                              title, description);
+       gs_context_dialog_row_set_content_is_markup (GS_CONTEXT_DIALOG_ROW (row), is_markup);
        gs_context_dialog_row_set_size_groups (GS_CONTEXT_DIALOG_ROW (row), lozenge_size_group, NULL, NULL);
        gtk_list_box_append (list_box, GTK_WIDGET (row));
 }
@@ -107,6 +129,7 @@ update_sizes_list (GsStorageContextDialog *self)
        g_autofree gchar *title_size_bytes_str = NULL;
        const gchar *title;
        gboolean cache_row_added = FALSE;
+       gboolean is_markup = FALSE;
 
        gs_widget_remove_all (GTK_WIDGET (self->sizes_list), (GsRemoveFunc) gtk_list_box_remove);
 
@@ -170,8 +193,11 @@ update_sizes_list (GsStorageContextDialog *self)
                /* FIXME: Addons, Potential Additional Downloads */
        }
 
-       title_size_bytes_str = g_format_size (title_size_bytes);
-       gtk_label_set_text (self->lozenge_content, title_size_bytes_str);
+       title_size_bytes_str = format_size (title_size_bytes, &is_markup);
+       if (is_markup)
+               gtk_label_set_markup (self->lozenge_content, title_size_bytes_str);
+       else
+               gtk_label_set_text (self->lozenge_content, title_size_bytes_str);
        gtk_label_set_text (self->title, title);
 
        /* Update the Manage Storage label. */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]