From dd0c73ff94c9697760831a13f0d12cbb33f5cabf Mon Sep 17 00:00:00 2001 From: Mike Massonnet Date: Sun, 15 Jul 2012 14:30:51 +0200 Subject: [PATCH] Bug 8842: Update monitor labels sizes gracefully New label class XnlpMonitorLabel that sets the requisition size dynamically, in order to avoid flickering in the panel. This class is only useful for labels that change periodically. When the 10 last changes requested a smaller size than the actual it will adapt to this new size. --- panel-plugin/Makefile.am | 14 ++-- panel-plugin/monitor-label.c | 137 +++++++++++++++++++++++++++++++++++ panel-plugin/monitor-label.h | 36 +++++++++ panel-plugin/netload.c | 16 ++-- 4 files changed, 187 insertions(+), 16 deletions(-) create mode 100644 panel-plugin/monitor-label.c create mode 100644 panel-plugin/monitor-label.h diff --git a/panel-plugin/Makefile.am b/panel-plugin/Makefile.am index 2163277..92e1d26 100644 --- a/panel-plugin/Makefile.am +++ b/panel-plugin/Makefile.am @@ -5,13 +5,15 @@ LIBS = @LIBS@ @SOLLIBS@ xfce4_netload_plugin_SOURCES = \ netload.c \ - utils.c \ - utils.h \ + monitor-label.c \ + monitor-label.h \ + utils.c \ + utils.h \ net.h \ - net.c \ - os.h \ - wormulon.h \ - global.h \ + net.c \ + os.h \ + wormulon.h \ + global.h \ slurm.h xfce4_netload_plugin_CFLAGS = \ diff --git a/panel-plugin/monitor-label.c b/panel-plugin/monitor-label.c new file mode 100644 index 0000000..0e0d409 --- /dev/null +++ b/panel-plugin/monitor-label.c @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2012 Mike Massonnet + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * See the file COPYING for the full license text. + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include "monitor-label.h" + + + +typedef struct _XnlpMonitorLabelClass XnlpMonitorLabelClass; +struct _XnlpMonitorLabelClass +{ + GtkLabelClass parent_class; +}; +struct _XnlpMonitorLabel +{ + GtkLabel parent; + /**/ + gint count_width; + gint count_height; + gint width; + gint height; +}; + +G_DEFINE_TYPE (XnlpMonitorLabel, xnlp_monitor_label, GTK_TYPE_LABEL) + +static void xnlp_monitor_label_constructed (GObject *object); + +static void cb_label_changed (GObject *object, + GParamSpec *pspec, + gpointer user_data); + + + +static void +xnlp_monitor_label_class_init (XnlpMonitorLabelClass *klass) +{ + GObjectClass *class = G_OBJECT_CLASS (klass); + xnlp_monitor_label_parent_class = g_type_class_peek_parent (klass); +} + +static void +xnlp_monitor_label_init (XnlpMonitorLabel *label) +{ + label->count_width = 0; + label->count_height = 0; + label->width = 0; + label->height = 0; + + g_signal_connect (label, "notify::label", G_CALLBACK (cb_label_changed), NULL); +} + + + +static void +cb_label_changed (GObject *object, GParamSpec *pspec, gpointer user_data) +{ + XnlpMonitorLabel *label = XNLP_MONITOR_LABEL (object); + GtkWidget *widget = GTK_WIDGET (object); + GtkRequisition req; + + gtk_widget_set_size_request (widget, -1, -1); + gtk_widget_size_request (widget, &req); + + if (req.width >= label->width) + { + label->width = req.width; + label->count_width = 0; + } + else if (label->count_width > 10) + { + label->width = req.width; + label->count_width = 0; + } + else + { + req.width = label->width; + label->count_width++; + } + + if (req.height >= label->height) + { + label->height = req.height; + label->count_height = 0; + } + else if (label->count_height > 10) + { + label->height = req.height; + label->count_height = 0; + } + else + { + req.height = label->height; + label->count_height++; + } + + gtk_widget_set_size_request (label, req.width, req.height); +} + + + +GtkWidget * +xnlp_monitor_label_new (const gchar *str) +{ + GtkLabel *label; + + label = g_object_new (XNLP_TYPE_MONITOR_LABEL, NULL); + + if (str && *str) + gtk_label_set_text (label, str); + + return GTK_WIDGET (label); +} + +void +xnlp_monitor_label_reinit_size_request (XnlpMonitorLabel *label) +{ + label->count_width = 0; + label->count_height = 0; + label->width = 0; + label->height = 0; + + gtk_widget_set_size_request (label, -1, -1); +} + diff --git a/panel-plugin/monitor-label.h b/panel-plugin/monitor-label.h new file mode 100644 index 0000000..4f8e267 --- /dev/null +++ b/panel-plugin/monitor-label.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2012 Mike Massonnet + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * See the file COPYING for the full license text. + */ + +#ifndef MONITOR_LABEL_H +#define MONITOR_LABEL_H + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include + +#define XNLP_TYPE_MONITOR_LABEL (xnlp_monitor_label_get_type ()) +#define XNLP_MONITOR_LABEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), XNLP_TYPE_MONITOR_LABEL, XnlpMonitorLabel)) +#define XNLP_MONITOR_LABEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), XNLP_TYPE_MONITOR_LABEL, XnlpMonitorLabelClass)) +#define XNLP_IS_MONITOR_LABEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), XNLP_TYPE_MONITOR_LABEL)) +#define XNLP_IS_MONITOR_LABEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), XNLP_TYPE_MONITOR_LABEL)) +#define XNLP_MONITOR_LABEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), XNLP_TYPE_MONITOR_LABEL, XnlpMonitorLabelClass)) + +typedef struct _XnlpMonitorLabel XnlpMonitorLabel; + +GType xnlp_monitor_label_get_type (void); +GtkWidget * xnlp_monitor_label_new (const gchar *str); +void xnlp_monitor_label_reinit_size_request (XnlpMonitorLabel *label); + +#endif /* !MONITOR_LABEL_H */ + diff --git a/panel-plugin/netload.c b/panel-plugin/netload.c index 20480b0..015ff06 100644 --- a/panel-plugin/netload.c +++ b/panel-plugin/netload.c @@ -33,9 +33,10 @@ #include #include +#include "monitor-label.h" + #define BORDER 8 -#define LABEL_SIZE 60 /* Defaults */ #define DEFAULT_TEXT "Net" @@ -258,7 +259,6 @@ static gboolean update_monitors(t_global_monitor *global) { g_snprintf(received, sizeof(received), "%s", buffer_panel[IN]); gtk_label_set_text(GTK_LABEL(global->monitor->rcv_label), received); - g_snprintf(sent, sizeof(sent), "%s", buffer_panel[OUT]); gtk_label_set_text(GTK_LABEL(global->monitor->sent_label), sent); } @@ -297,27 +297,23 @@ static gboolean monitor_set_size(XfcePanelPlugin *plugin, int size, t_global_mon { for (i = 0; i < SUM; i++) gtk_widget_set_size_request(GTK_WIDGET(global->monitor->status[i]), BORDER, BORDER); - gtk_widget_set_size_request(GTK_WIDGET(global->monitor->rcv_label), -1, -1); - gtk_widget_set_size_request(GTK_WIDGET(global->monitor->sent_label), -1, -1); gtk_widget_set_size_request(GTK_WIDGET(plugin), size, -1); } else if (mode == XFCE_PANEL_PLUGIN_MODE_VERTICAL) { for (i = 0; i < SUM; i++) gtk_widget_set_size_request(GTK_WIDGET(global->monitor->status[i]), -1, BORDER); - gtk_widget_set_size_request(GTK_WIDGET(global->monitor->rcv_label), -1, LABEL_SIZE); - gtk_widget_set_size_request(GTK_WIDGET(global->monitor->sent_label), -1, LABEL_SIZE); gtk_widget_set_size_request(GTK_WIDGET(plugin), size, -1); } else /* mode == XFCE_PANEL_PLUGIN_MODE_HORIZONTAL */ { for (i = 0; i < SUM; i++) gtk_widget_set_size_request(GTK_WIDGET(global->monitor->status[i]), BORDER, -1); - gtk_widget_set_size_request(GTK_WIDGET(global->monitor->rcv_label), LABEL_SIZE, -1); - gtk_widget_set_size_request(GTK_WIDGET(global->monitor->sent_label), LABEL_SIZE, -1); gtk_widget_set_size_request(GTK_WIDGET(plugin), -1, size); } + xnlp_monitor_label_reinit_size_request(XNLP_MONITOR_LABEL(global->monitor->rcv_label)); + xnlp_monitor_label_reinit_size_request(XNLP_MONITOR_LABEL(global->monitor->sent_label)); gtk_container_set_border_width(GTK_CONTAINER(global->box), size > 26 ? 2 : 1); return TRUE; @@ -469,8 +465,8 @@ static t_global_monitor * monitor_new(XfcePanelPlugin *plugin) TRUE, FALSE, 2); /* Create sent and received labels */ - global->monitor->rcv_label = gtk_label_new("-"); - global->monitor->sent_label = gtk_label_new("-"); + global->monitor->rcv_label = xnlp_monitor_label_new("-"); + global->monitor->sent_label = xnlp_monitor_label_new("-"); gtk_box_pack_start(GTK_BOX(global->box), GTK_WIDGET(global->monitor->rcv_label), TRUE, FALSE, 2);