[rhythmbox: 1/2] widgets: use tabular figures for text with numeric data




commit 8bd1a25fca425ca783d0383cafb75b2399706c3b
Author: crvi <crvisqr gmail com>
Date:   Mon Aug 24 02:39:16 2020 +0530

    widgets: use tabular figures for text with numeric data
    
    This should fix text label wiggling in track time label of rb-header
    widget, task detail label in rb-task-list, primary label of
    nautilus-floating-bar etc. Also, tabular figures are used for columns
    with numeric data in rb-entry-view, and the columns are right aligned.
    
    Cantarell-Regular, Cantarell-Light and Cantarell-Bold support tabular
    figure font feature. For fonts which do not support this feature, the
    'tnum' attribute will be silently ignored.

 lib/rb-text-helpers.c           | 20 ++++++++++++++++++++
 lib/rb-text-helpers.h           |  3 +++
 widgets/nautilus-floating-bar.c |  3 +++
 widgets/rb-entry-view.c         | 22 ++++++++++++++++++++++
 widgets/rb-header.c             |  2 ++
 widgets/rb-import-dialog.c      |  4 ++++
 widgets/rb-task-list-display.c  |  2 ++
 7 files changed, 56 insertions(+)
---
diff --git a/lib/rb-text-helpers.c b/lib/rb-text-helpers.c
index 2d4472ed4..a8787cfb6 100644
--- a/lib/rb-text-helpers.c
+++ b/lib/rb-text-helpers.c
@@ -227,3 +227,23 @@ rb_text_cat (PangoDirection base_dir, ...)
 
        return g_string_free (result, FALSE);
 }
+
+PangoAttrList *
+rb_text_numeric_get_pango_attr_list (void)
+{
+       static PangoAttrList *attr_list = NULL;
+
+       if (attr_list == NULL) {
+               /*
+                * use tabular figures to ensure constant width for
+                * numeric data in text widgets / renderers. this
+                * ensures that numeric data which gets updated ( like
+                * track time label ) doesn't cause wiggle to the
+                * widgets / renderers that display them.
+                */
+               attr_list = pango_attr_list_new ();
+               pango_attr_list_insert (attr_list, pango_attr_font_features_new ("tnum=1"));
+       }
+
+       return attr_list;
+}
diff --git a/lib/rb-text-helpers.h b/lib/rb-text-helpers.h
index 90e9b1f7a..07a93aa18 100644
--- a/lib/rb-text-helpers.h
+++ b/lib/rb-text-helpers.h
@@ -30,6 +30,7 @@
 
 #include <glib.h>
 #include <pango/pango-bidi-type.h>
+#include <pango/pango-attributes.h>
 
 G_BEGIN_DECLS
 
@@ -39,6 +40,8 @@ PangoDirection rb_text_common_direction (const char *first, ...);
 
 char *rb_text_cat (PangoDirection base_dir, ...);
 
+PangoAttrList *rb_text_numeric_get_pango_attr_list (void);
+
 G_END_DECLS
 
 #endif /* __RB_TEXT_HELPERS_H */
diff --git a/widgets/nautilus-floating-bar.c b/widgets/nautilus-floating-bar.c
index 0d8101282..008e2cd2a 100644
--- a/widgets/nautilus-floating-bar.c
+++ b/widgets/nautilus-floating-bar.c
@@ -24,6 +24,7 @@
 #include <string.h>
 
 #include "nautilus-floating-bar.h"
+#include "rb-text-helpers.h"
 
 #define HOVER_HIDE_TIMEOUT_INTERVAL 100
 
@@ -418,6 +419,8 @@ nautilus_floating_bar_constructed (GObject *obj)
     self->primary_label_widget = w;
     gtk_widget_show (w);
 
+    gtk_label_set_attributes (GTK_LABEL (w), rb_text_numeric_get_pango_attr_list ());
+
     w = gtk_label_new (NULL);
     gtk_label_set_single_line_mode (GTK_LABEL (w), TRUE);
     gtk_container_add (GTK_CONTAINER (labels_box), w);
diff --git a/widgets/rb-entry-view.c b/widgets/rb-entry-view.c
index 2839909cf..889c8176c 100644
--- a/widgets/rb-entry-view.c
+++ b/widgets/rb-entry-view.c
@@ -88,6 +88,7 @@
 #include "rb-dialog.h"
 #include "rb-debug.h"
 #include "rb-util.h"
+#include "rb-text-helpers.h"
 #include "rhythmdb.h"
 #include "rhythmdb-query-model.h"
 #include "rb-cell-renderer-pixbuf.h"
@@ -1418,6 +1419,7 @@ rb_entry_view_append_column (RBEntryView *view,
        gboolean ellipsize = FALSE;
        gboolean resizable = TRUE;
        gint column_width = -1;
+       gboolean has_numeric_data = FALSE;
 
        column = gtk_tree_view_column_new ();
 
@@ -1427,6 +1429,7 @@ rb_entry_view_append_column (RBEntryView *view,
        switch (coltype) {
        case RB_ENTRY_VIEW_COL_TRACK_NUMBER:
                propid = RHYTHMDB_PROP_TRACK_NUMBER;
+               has_numeric_data = TRUE;
                cell_data->propid = propid;
                cell_data_func = (GtkTreeCellDataFunc) rb_entry_view_long_cell_data_func;
                sort_func = (GCompareDataFunc) rhythmdb_query_model_track_sort_func;
@@ -1497,6 +1500,7 @@ rb_entry_view_append_column (RBEntryView *view,
                break;
        case RB_ENTRY_VIEW_COL_DURATION:
                propid = RHYTHMDB_PROP_DURATION;
+               has_numeric_data = TRUE;
                cell_data->propid = propid;
                cell_data_func = (GtkTreeCellDataFunc) rb_entry_view_duration_cell_data_func;
                sort_propid = cell_data->propid;
@@ -1509,6 +1513,7 @@ rb_entry_view_append_column (RBEntryView *view,
                break;
        case RB_ENTRY_VIEW_COL_YEAR:
                propid = RHYTHMDB_PROP_DATE;
+               has_numeric_data = TRUE;
                cell_data->propid = propid;
                cell_data_func = (GtkTreeCellDataFunc) rb_entry_view_year_cell_data_func;
                sort_propid = cell_data->propid;
@@ -1521,6 +1526,7 @@ rb_entry_view_append_column (RBEntryView *view,
                break;
        case RB_ENTRY_VIEW_COL_QUALITY:
                propid = RHYTHMDB_PROP_BITRATE;
+               has_numeric_data = TRUE;
                cell_data->propid = propid;
                cell_data_func = (GtkTreeCellDataFunc) rb_entry_view_quality_cell_data_func;
                sort_propid = cell_data->propid;
@@ -1557,6 +1563,7 @@ rb_entry_view_append_column (RBEntryView *view,
                break;
        case RB_ENTRY_VIEW_COL_PLAY_COUNT:
                propid = RHYTHMDB_PROP_PLAY_COUNT;
+               has_numeric_data = TRUE;
                cell_data->propid = propid;
                cell_data_func = (GtkTreeCellDataFunc) rb_entry_view_play_count_cell_data_func;
                sort_propid = cell_data->propid;
@@ -1569,6 +1576,7 @@ rb_entry_view_append_column (RBEntryView *view,
                break;
        case RB_ENTRY_VIEW_COL_LAST_PLAYED:
                propid = RHYTHMDB_PROP_LAST_PLAYED;
+               has_numeric_data = TRUE;
                cell_data->propid = RHYTHMDB_PROP_LAST_PLAYED_STR;
                cell_data_func = (GtkTreeCellDataFunc) rb_entry_view_string_cell_data_func;
                sort_propid = RHYTHMDB_PROP_LAST_PLAYED;
@@ -1581,6 +1589,7 @@ rb_entry_view_append_column (RBEntryView *view,
                break;
        case RB_ENTRY_VIEW_COL_FIRST_SEEN:
                propid = RHYTHMDB_PROP_FIRST_SEEN;
+               has_numeric_data = TRUE;
                cell_data->propid = RHYTHMDB_PROP_FIRST_SEEN_STR;
                cell_data_func = (GtkTreeCellDataFunc) rb_entry_view_string_cell_data_func;
                sort_propid = RHYTHMDB_PROP_FIRST_SEEN;
@@ -1592,6 +1601,7 @@ rb_entry_view_append_column (RBEntryView *view,
                break;
        case RB_ENTRY_VIEW_COL_LAST_SEEN:
                propid = RHYTHMDB_PROP_LAST_SEEN;
+               has_numeric_data = TRUE;
                cell_data->propid = RHYTHMDB_PROP_LAST_SEEN_STR;
                cell_data_func = (GtkTreeCellDataFunc) rb_entry_view_string_cell_data_func;
                sort_propid = RHYTHMDB_PROP_LAST_SEEN;
@@ -1613,6 +1623,7 @@ rb_entry_view_append_column (RBEntryView *view,
                break;
        case RB_ENTRY_VIEW_COL_BPM:
                propid = RHYTHMDB_PROP_BPM;
+               has_numeric_data = TRUE;
                cell_data->propid = propid;
                cell_data_func = (GtkTreeCellDataFunc) rb_entry_view_bpm_cell_data_func;
                sort_func = (GCompareDataFunc) rhythmdb_query_model_double_ceiling_sort_func;
@@ -1644,6 +1655,17 @@ rb_entry_view_append_column (RBEntryView *view,
                gtk_tree_view_column_set_cell_data_func (column, renderer,
                                                         cell_data_func, cell_data, g_free);
 
+               /* use tabular figures and right align columns
+                * with numeric data.
+                */
+               if (has_numeric_data) {
+                       g_object_set (renderer,
+                                     "attributes", rb_text_numeric_get_pango_attr_list (),
+                                     "xalign", 1.0,
+                                     NULL);
+                       gtk_tree_view_column_set_alignment (column, 1.0);
+               }
+
                g_object_set_data (G_OBJECT (renderer), CELL_PROPID_ITEM, GINT_TO_POINTER (propid));
                g_signal_connect_object (renderer, "edited",
                                         G_CALLBACK (rb_entry_view_cell_edited_cb),
diff --git a/widgets/rb-header.c b/widgets/rb-header.c
index 77e8433e9..934deb986 100644
--- a/widgets/rb-header.c
+++ b/widgets/rb-header.c
@@ -382,6 +382,8 @@ rb_header_constructed (GObject *object)
 
        /* elapsed time / duration display */
        header->priv->timelabel = gtk_label_new ("");
+       gtk_label_set_attributes (GTK_LABEL (header->priv->timelabel),
+                                 rb_text_numeric_get_pango_attr_list ());
        gtk_widget_set_halign (header->priv->timelabel, GTK_ALIGN_END);
        gtk_widget_set_no_show_all (header->priv->timelabel, TRUE);
 
diff --git a/widgets/rb-import-dialog.c b/widgets/rb-import-dialog.c
index 86c36823f..30b1840a6 100644
--- a/widgets/rb-import-dialog.c
+++ b/widgets/rb-import-dialog.c
@@ -36,6 +36,7 @@
 #include "rb-builder-helpers.h"
 #include "rb-debug.h"
 #include "rb-util.h"
+#include "rb-text-helpers.h"
 #include "rb-cut-and-paste-code.h"
 #include "rb-search-entry.h"
 #include "rhythmdb-entry-type.h"
@@ -469,6 +470,9 @@ update_status_idle (RBImportDialog *dialog)
        }
        text = g_strdup_printf (fmt, count);
        gtk_button_set_label (GTK_BUTTON (dialog->priv->import_button), text);
+       /* a new child label is created each time button label is set */
+       gtk_label_set_attributes (GTK_LABEL (gtk_bin_get_child (GTK_BIN (dialog->priv->import_button))),
+                                 rb_text_numeric_get_pango_attr_list ());
        g_free (text);
 
        /* hack to get these strings marked for translation */
diff --git a/widgets/rb-task-list-display.c b/widgets/rb-task-list-display.c
index 90e608f8a..c0a48b1ee 100644
--- a/widgets/rb-task-list-display.c
+++ b/widgets/rb-task-list-display.c
@@ -32,6 +32,7 @@
 #include <lib/rb-task-progress.h>
 #include <lib/rb-list-model.h>
 #include <lib/rb-builder-helpers.h>
+#include <lib/rb-text-helpers.h>
 #include <lib/rb-util.h>
 
 #define TASK_REMOVE_DELAY      15
@@ -112,6 +113,7 @@ task_list_changed_cb (RBListModel *model, int position, int removed, int added,
 
                widget = GTK_WIDGET (gtk_builder_get_object (b, "task-detail"));
                g_object_bind_property (task, "task-detail", widget, "label", G_BINDING_SYNC_CREATE);
+               gtk_label_set_attributes (GTK_LABEL (widget), rb_text_numeric_get_pango_attr_list ());
 
                widget = GTK_WIDGET (gtk_builder_get_object (b, "task-progress"));
                g_object_bind_property (task, "task-progress", widget, "fraction", G_BINDING_SYNC_CREATE);


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