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



commit 8881105858e5f89b175d077e2d39008a1829526f
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                     |  4 ++++
 src/gs-storage-context-dialog.c | 30 +++++++++++++++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)
---
diff --git a/meson.build b/meson.build
index aeeb72a06..0ea0faf6d 100644
--- a/meson.build
+++ b/meson.build
@@ -157,6 +157,10 @@ libadwaita = dependency('libadwaita-1',
   ]
 )
 
+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..948359e1a 100644
--- a/src/gs-storage-context-dialog.c
+++ b/src/gs-storage-context-dialog.c
@@ -69,6 +69,24 @@ typedef enum {
        MATCH_STATE_UNKNOWN,
 } MatchState;
 
+static gchar *
+format_size (guint64 size_bytes,
+            GtkWidget *widget,
+            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);
+       *out_is_markup = TRUE;
+       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
+               return g_markup_printf_escaped ("<span font_size='x-small'>%s</span> %s", unit_str, 
value_str);
+       return g_markup_printf_escaped ("%s <span font_size='x-small'>%s</span>", value_str, unit_str);
+#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 +97,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 +111,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, GTK_WIDGET (list_box), &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 +127,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 +191,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, GTK_WIDGET (self), &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]