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.
master
Mike Massonnet 2012-07-15 14:30:51 +02:00
parent 005d94bf49
commit dd0c73ff94
4 changed files with 187 additions and 16 deletions

View File

@ -5,6 +5,8 @@ LIBS = @LIBS@ @SOLLIBS@
xfce4_netload_plugin_SOURCES = \
netload.c \
monitor-label.c \
monitor-label.h \
utils.c \
utils.h \
net.h \

View File

@ -0,0 +1,137 @@
/*
* Copyright (c) 2012 Mike Massonnet <mmassonnet@xfce.org>
*
* 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 <config.h>
#endif
#include <gtk/gtk.h>
#include "monitor-label.h"
typedef struct _XnlpMonitorLabelClass XnlpMonitorLabelClass;
struct _XnlpMonitorLabelClass
{
GtkLabelClass parent_class;
};
struct _XnlpMonitorLabel
{
GtkLabel parent;
/*<private>*/
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);
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2012 Mike Massonnet <mmassonnet@xfce.org>
*
* 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 <config.h>
#endif
#include <glib-object.h>
#include <gtk/gtk.h>
#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 */

View File

@ -33,9 +33,10 @@
#include <libxfce4ui/libxfce4ui.h>
#include <libxfce4panel/libxfce4panel.h>
#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);