Skip to content

Commit 306b7e2

Browse files
lukefromdcraveit65
authored andcommitted
Inhibit Applet: Fix excessive CPU use
Fix #248 Use code from GNOME version to draw icon. The code from gnome-applets/inhibit uses a GtkImage to draw the icon but does not constantly re-run gtk_widget_show so it works without loading down the CPU
1 parent d29f9d6 commit 306b7e2

1 file changed

Lines changed: 49 additions & 129 deletions

File tree

applets/inhibit/inhibit-applet.c

Lines changed: 49 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ typedef struct{
4848
MatePanelApplet parent;
4949
/* applet state */
5050
guint cookie;
51-
/* the icon, a GtkImage for it, and a cache for size*/
52-
GdkPixbuf *icon;
51+
/* the icon */
5352
GtkWidget *image;
54-
gint icon_width, icon_height;
5553
/* connection to g-p-m */
5654
DBusGProxy *proxy;
5755
DBusGConnection *connection;
@@ -76,9 +74,8 @@ static void gpm_inhibit_applet_init (GpmInhibitApplet *applet);
7674

7775
G_DEFINE_TYPE (GpmInhibitApplet, gpm_inhibit_applet, PANEL_TYPE_APPLET)
7876

79-
static void gpm_applet_get_icon (GpmInhibitApplet *applet);
80-
static void gpm_applet_check_size (GpmInhibitApplet *applet);
81-
static gboolean gpm_applet_draw_cb (GpmInhibitApplet *applet);
77+
static void gpm_applet_update_icon (GpmInhibitApplet *applet);
78+
static void gpm_applet_size_allocate_cb (GtkWidget *widget, GdkRectangle *allocation);;
8279
static void gpm_applet_update_tooltip (GpmInhibitApplet *applet);
8380
static gboolean gpm_applet_click_cb (GpmInhibitApplet *applet, GdkEventButton *event);
8481
static void gpm_applet_dialog_about_cb (GtkAction *action, gpointer data);
@@ -163,26 +160,16 @@ gpm_applet_uninhibit (GpmInhibitApplet *applet,
163160
}
164161

165162
/**
166-
* gpm_applet_get_icon:
163+
* gpm_applet_update_icon:
167164
* @applet: Inhibit applet instance
168165
*
169-
* retrieve an icon from stock with a size adapted to panel
166+
* sets an icon from stock
170167
**/
171168
static void
172-
gpm_applet_get_icon (GpmInhibitApplet *applet)
169+
gpm_applet_update_icon (GpmInhibitApplet *applet)
173170
{
174171
const gchar *icon;
175172

176-
/* free */
177-
if (applet->icon != NULL) {
178-
g_object_unref (applet->icon);
179-
applet->icon = NULL;
180-
}
181-
182-
if (applet->size <= 2) {
183-
return;
184-
}
185-
186173
/* get icon */
187174
if (applet->proxy == NULL) {
188175
icon = GPM_INHIBIT_APPLET_ICON_INVALID;
@@ -191,100 +178,52 @@ gpm_applet_get_icon (GpmInhibitApplet *applet)
191178
} else {
192179
icon = GPM_INHIBIT_APPLET_ICON_UNINHIBIT;
193180
}
194-
applet->icon = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
195-
icon,
196-
applet->size - 2,
197-
GTK_ICON_LOOKUP_FORCE_SIZE, /*prevent oversize icons*/
198-
NULL);
199-
200-
/* update size cache */
201-
applet->icon_height = gdk_pixbuf_get_height (applet->icon);
202-
applet->icon_width = gdk_pixbuf_get_width (applet->icon);
181+
gtk_image_set_from_icon_name (GTK_IMAGE(applet->image),
182+
icon,
183+
GTK_ICON_SIZE_BUTTON);
203184
}
204185

205186
/**
206-
* gpm_applet_check_size:
187+
* gpm_applet_size_allocate_cb:
207188
* @applet: Inhibit applet instance
208189
*
209-
* check if panel size has changed and applet adapt size
190+
* resize icon when panel size changed
210191
**/
211192
static void
212-
gpm_applet_check_size (GpmInhibitApplet *applet)
193+
gpm_applet_size_allocate_cb (GtkWidget *widget,
194+
GdkRectangle *allocation)
213195
{
214-
GtkAllocation allocation;
215-
216-
/* we don't use the size function here, but the yet allocated size because the
217-
size value is false (kind of rounded) */
218-
gtk_widget_get_allocation (GTK_WIDGET (applet), &allocation);
219-
if (MATE_PANEL_APPLET_VERTICAL(mate_panel_applet_get_orient (MATE_PANEL_APPLET (applet)))) {
220-
if (applet->size != allocation.width) {
221-
applet->size = allocation.width;
222-
gpm_applet_get_icon (applet);
223-
gtk_widget_set_size_request (GTK_WIDGET(applet), applet->size, applet->icon_height + 2);
224-
}
225-
/* Adjusting incase the icon size has changed */
226-
if (allocation.height < applet->icon_height + 2) {
227-
gtk_widget_set_size_request (GTK_WIDGET(applet), applet->size, applet->icon_height + 2);
228-
}
229-
} else {
230-
if (applet->size != allocation.height) {
231-
applet->size = allocation.height;
232-
gpm_applet_get_icon (applet);
233-
gtk_widget_set_size_request (GTK_WIDGET(applet), applet->icon_width + 2, applet->size);
234-
}
235-
/* Adjusting incase the icon size has changed */
236-
if (allocation.width < applet->icon_width + 2) {
237-
gtk_widget_set_size_request (GTK_WIDGET(applet), applet->icon_width + 2, applet->size);
238-
}
196+
GpmInhibitApplet *applet = GPM_INHIBIT_APPLET (widget);
197+
int size = 0;
198+
199+
switch (mate_panel_applet_get_orient (MATE_PANEL_APPLET (applet))) {
200+
case MATE_PANEL_APPLET_ORIENT_LEFT:
201+
case MATE_PANEL_APPLET_ORIENT_RIGHT:
202+
size = allocation->width;
203+
break;
204+
205+
case MATE_PANEL_APPLET_ORIENT_UP:
206+
case MATE_PANEL_APPLET_ORIENT_DOWN:
207+
size = allocation->height;
208+
break;
239209
}
240-
}
241210

242-
/**
243-
* gpm_applet_draw_cb:
244-
* @applet: Inhibit applet instance
245-
*
246-
* draws applet content (background + icon)
247-
**/
248-
static gboolean
249-
gpm_applet_draw_cb (GpmInhibitApplet *applet)
250-
{
251-
if (gtk_widget_get_window (GTK_WIDGET(applet)) == NULL) {
252-
return FALSE;
253-
}
254-
255-
/* retrieve applet size */
256-
gpm_applet_get_icon (applet);
257-
gpm_applet_check_size (applet);
258-
if (applet->size <= 2) {
259-
return FALSE;
260-
}
261-
262-
/* if no icon, then don't try to display */
263-
if (applet->icon == NULL) {
264-
return FALSE;
265-
}
266-
267-
/*draw icon */
268-
269-
gtk_image_set_from_pixbuf(GTK_IMAGE(applet->image),applet->icon);
270-
gtk_widget_show(GTK_WIDGET(applet->image));
271-
272-
return TRUE;
211+
/* copied from button-widget.c in the panel */
212+
if (size < 22)
213+
size = 16;
214+
else if (size < 24)
215+
size = 22;
216+
else if (size < 32)
217+
size = 24;
218+
else if (size < 48)
219+
size = 32;
220+
else
221+
size = 48;
222+
223+
/* GtkImage already contains a check to do nothing if it's the same */
224+
gtk_image_set_pixel_size (GTK_IMAGE(applet->image), size);
273225
}
274226

275-
/**
276-
* gpm_applet_change_background_cb:
277-
*
278-
* Enqueues an expose event (don't know why it's not the default behaviour)
279-
**/
280-
static void
281-
gpm_applet_change_background_cb (GpmInhibitApplet *applet,
282-
MatePanelAppletBackgroundType arg1,
283-
cairo_pattern_t *arg2,
284-
gpointer data)
285-
{
286-
gtk_widget_queue_draw (GTK_WIDGET (applet));
287-
}
288227

289228
/**
290229
* gpm_applet_update_tooltip:
@@ -334,10 +273,8 @@ gpm_applet_click_cb (GpmInhibitApplet *applet, GdkEventButton *event)
334273
&(applet->cookie));
335274
}
336275
/* update icon */
337-
gpm_applet_get_icon (applet);
338-
gpm_applet_check_size (applet);
276+
gpm_applet_update_icon (applet);
339277
gpm_applet_update_tooltip (applet);
340-
gpm_applet_draw_cb (applet);
341278

342279
return TRUE;
343280
}
@@ -435,8 +372,6 @@ gpm_applet_destroy_cb (GtkWidget *widget)
435372
GpmInhibitApplet *applet = GPM_INHIBIT_APPLET(widget);
436373

437374
g_bus_unwatch_name (applet->bus_watch_id);
438-
if (applet->icon != NULL)
439-
g_object_unref (applet->icon);
440375
}
441376

442377
/**
@@ -514,8 +449,7 @@ gpm_inhibit_applet_name_appeared_cb (GDBusConnection *connection,
514449
{
515450
gpm_inhibit_applet_dbus_connect (applet);
516451
gpm_applet_update_tooltip (applet);
517-
gpm_applet_get_icon (applet);
518-
gpm_applet_draw_cb (applet);
452+
gpm_applet_update_icon (applet);;
519453
}
520454

521455
/**
@@ -528,8 +462,7 @@ gpm_inhibit_applet_name_vanished_cb (GDBusConnection *connection,
528462
{
529463
gpm_inhibit_applet_dbus_disconnect (applet);
530464
gpm_applet_update_tooltip (applet);
531-
gpm_applet_get_icon (applet);
532-
gpm_applet_draw_cb (applet);
465+
gpm_applet_update_icon (applet);
533466
}
534467

535468
/**
@@ -542,12 +475,10 @@ gpm_inhibit_applet_init (GpmInhibitApplet *applet)
542475
DBusGConnection *connection;
543476

544477
/* initialize fields */
545-
applet->size = 0;
546-
applet->icon = NULL;
478+
applet->image = NULL;
547479
applet->cookie = 0;
548480
applet->connection = NULL;
549481
applet->proxy = NULL;
550-
applet->image = gtk_image_new();
551482

552483
/* Add application specific icons to search path */
553484
gtk_icon_theme_append_search_path (gtk_icon_theme_get_default (),
@@ -564,12 +495,11 @@ gpm_inhibit_applet_init (GpmInhibitApplet *applet)
564495

565496
/* prepare */
566497
mate_panel_applet_set_flags (MATE_PANEL_APPLET (applet), MATE_PANEL_APPLET_EXPAND_MINOR);
498+
applet->image = gtk_image_new();
499+
gtk_container_add (GTK_CONTAINER (applet), applet->image);
567500

568501
/* set appropriate size and load icon accordingly */
569-
gpm_applet_draw_cb (applet);
570-
571-
/*pack*/
572-
gtk_container_add(GTK_CONTAINER(applet), applet->image);
502+
gtk_widget_queue_draw (GTK_WIDGET (applet));
573503

574504
/* show */
575505
gtk_widget_show_all (GTK_WIDGET(applet));
@@ -578,17 +508,8 @@ gpm_inhibit_applet_init (GpmInhibitApplet *applet)
578508
g_signal_connect (G_OBJECT(applet), "button-press-event",
579509
G_CALLBACK(gpm_applet_click_cb), NULL);
580510

581-
/* We use g_signal_connect_after because letting the panel draw
582-
* the background is the only way to have the correct
583-
* background when a theme defines a background picture. */
584-
g_signal_connect_after (G_OBJECT(applet), "draw",
585-
G_CALLBACK(gpm_applet_draw_cb), NULL);
586-
587-
g_signal_connect (G_OBJECT(applet), "change-background",
588-
G_CALLBACK(gpm_applet_change_background_cb), NULL);
589-
590-
g_signal_connect (G_OBJECT(applet), "change-orient",
591-
G_CALLBACK(gpm_applet_draw_cb), NULL);
511+
g_signal_connect (G_OBJECT(applet), "size-allocate",
512+
G_CALLBACK(gpm_applet_size_allocate_cb), NULL);
592513

593514
g_signal_connect (G_OBJECT(applet), "destroy",
594515
G_CALLBACK(gpm_applet_destroy_cb), NULL);
@@ -632,7 +553,6 @@ gpm_applet_cb (MatePanelApplet *_applet, const gchar *iid, gpointer data)
632553
g_free (ui_path);
633554
g_object_unref (action_group);
634555

635-
gpm_applet_draw_cb (applet);
636556
return TRUE;
637557
}
638558

0 commit comments

Comments
 (0)