From 82bfe395a94ce982bcc26e5bcb050cf22ad57392 Mon Sep 17 00:00:00 2001 From: monsta Date: Fri, 22 Sep 2017 11:43:53 +0300 Subject: cpufreq: fix calculating max width of frequency/unit/percent labels fixes https://github.com/mate-desktop/mate-applets/issues/268 --- cpufreq/src/cpufreq-applet.c | 45 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/cpufreq/src/cpufreq-applet.c b/cpufreq/src/cpufreq-applet.c index 94f54b2b..641330e2 100644 --- a/cpufreq/src/cpufreq-applet.c +++ b/cpufreq/src/cpufreq-applet.c @@ -290,6 +290,24 @@ cpufreq_applet_size_allocate (GtkWidget *widget, GtkAllocation *allocation) GTK_WIDGET_CLASS (cpufreq_applet_parent_class)->size_allocate (widget, allocation); } +static gint +get_max_text_width (GtkWidget *widget, + const char *text) +{ + PangoContext *context; + PangoLayout *layout; + PangoRectangle logical_rect; + + context = gtk_widget_get_pango_context (widget); + layout = pango_layout_new (context); + pango_layout_set_text (layout, text, -1); + pango_layout_get_pixel_extents (layout, NULL, &logical_rect); + + g_object_unref (layout); + + return logical_rect.width; +} + static gint cpufreq_applet_get_max_label_width (CPUFreqApplet *applet) { @@ -304,7 +322,6 @@ cpufreq_applet_get_max_label_width (CPUFreqApplet *applet) available_freqs = cpufreq_monitor_get_available_frequencies (applet->monitor); while (available_freqs) { - GtkWidget *label; gint label_width; const gchar *text; gchar *freq_text; @@ -314,12 +331,10 @@ cpufreq_applet_get_max_label_width (CPUFreqApplet *applet) freq = atoi (text); freq_text = cpufreq_utils_get_frequency_label (freq); - label = gtk_label_new (freq_text); - gtk_widget_get_preferred_width (applet->label, &label_width, NULL); + label_width = get_max_text_width (applet->label, freq_text); width = MAX (width, label_width); g_free (freq_text); - gtk_widget_destroy (label); available_freqs = g_list_next (available_freqs); } @@ -332,37 +347,21 @@ cpufreq_applet_get_max_label_width (CPUFreqApplet *applet) static gint cpufreq_applet_get_max_perc_width (CPUFreqApplet *applet) { - GtkWidget *label; - gint width; - if (applet->max_perc_width > 0) return applet->max_perc_width; - label = gtk_label_new ("100%"); - gtk_widget_get_preferred_width (applet->label, &width,&width); - applet->max_perc_width = width +20; /*for some reason width always comes up 2 characters*/ - gtk_widget_destroy (label); - + applet->max_perc_width = get_max_text_width (applet->label, "100%"); return applet->max_perc_width; } static gint cpufreq_applet_get_max_unit_width (CPUFreqApplet *applet) { - GtkWidget *label; - gint w1, w2; - if (applet->max_unit_width > 0) return applet->max_unit_width; - label = gtk_label_new ("GHz"); - gtk_widget_get_preferred_width (applet->label, &w1, NULL); - - gtk_label_set_text (GTK_LABEL (label), "MHz"); - gtk_widget_get_preferred_width (applet->label, &w2, NULL); - - gtk_widget_destroy (label); - applet->max_unit_width = MAX (w1, w2)-1; + applet->max_unit_width = MAX (get_max_text_width (applet->unit_label, "GHz"), + get_max_text_width (applet->unit_label, "MHz")); return applet->max_unit_width; } -- cgit v1.2.1