[Sugar-devel] [sugar-toolkit PATCH] set window icon

Sascha Silbe sascha-pgp at silbe.org
Thu Aug 5 17:26:04 EDT 2010


Set the window icon to the activity icon for improved look-and-feel when
using non-Sugar ways of switching windows (e.g. by enabling the metacity key
bindings).

Tested on XO-1.5 running Debian Squeeze. Setting the icon takes << 0.1s
(0.058s, 0.026s, 0.021s).

Signed-off-by: Sascha Silbe <sascha-pgp at silbe.org>

---
 src/sugar/activity/activity.py |   36 ++++++++++++++++++++++++++++++++----
 1 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/src/sugar/activity/activity.py b/src/sugar/activity/activity.py
index 0e2ecc5..1b1503c 100644
--- a/src/sugar/activity/activity.py
+++ b/src/sugar/activity/activity.py
@@ -61,13 +61,15 @@ import dbus.service
 import cjson
 
 from sugar import util
+from sugar.bundle.activitybundle import ActivityBundle
 from sugar.presence import presenceservice
 from sugar.activity.activityservice import ActivityService
 from sugar.activity.namingalert import NamingAlert
 from sugar.graphics import style
 from sugar.graphics.window import Window
 from sugar.graphics.alert import Alert
-from sugar.graphics.icon import Icon
+from sugar.graphics.icon import Icon, get_surface
+from sugar.graphics.xocolor import XoColor
 from sugar.datastore import datastore
 from sugar.session import XSMPClient
 from sugar import wm
@@ -331,6 +333,8 @@ class Activity(Window, gtk.Container):
             else:
                 logging.debug('Unknown share scope %r', share_scope)
 
+        client = gconf.client_get_default()
+        icon_color = client.get_string('/desktop/sugar/user/color')
         if handle.object_id is None and create_jobject:
             logging.debug('Creating a jobject.')
             self._jobject = datastore.create()
@@ -345,9 +349,6 @@ class Activity(Window, gtk.Container):
             self._jobject.metadata['share-scope'] = SCOPE_PRIVATE
             if self.shared_activity is not None:
                 icon_color = self.shared_activity.props.color
-            else:
-                client = gconf.client_get_default()
-                icon_color = client.get_string('/desktop/sugar/user/color')
             self._jobject.metadata['icon-color'] = icon_color
 
             self._jobject.file_path = ''
@@ -355,6 +356,33 @@ class Activity(Window, gtk.Container):
             # https://dev.laptop.org/ticket/3071
             datastore.write(self._jobject)
 
+        icon_pixbuf = self._get_icon_pixbuf(icon_color)
+        gtk.window_set_default_icon(icon_pixbuf)
+        self.set_icon(icon_pixbuf)
+
+    def _get_icon_pixbuf(self, icon_color):
+        """Return colored activity icon as a Pixbuf."""
+        bundle = ActivityBundle(get_bundle_path())
+        xo_color = XoColor(icon_color)
+        # FIXME: without background_color there's garbage in the output
+        surface = get_surface(file_name=bundle.get_icon(),
+            fill_color=xo_color.get_fill_color(),
+            stroke_color=xo_color.get_stroke_color(),
+            background_color=style.COLOR_WHITE.get_gdk_color())
+        width, height = surface.get_width(), surface.get_height()
+        colormap = gtk.gdk.colormap_get_system()
+        pixmap = gtk.gdk.Pixmap(self.window, width, height,
+            colormap.get_visual().depth)
+        pixmap.set_colormap(colormap)
+        cr = pixmap.cairo_create()
+        cr.set_source_surface(surface, 0, 0)
+        cr.paint()
+        pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True,
+            colormap.get_visual().bits_per_rgb, width, height)
+        pixbuf = pixbuf.get_from_drawable(pixmap, colormap, 0, 0, 0, 0,
+            width, height)
+        return pixbuf
+
     def get_active(self):
         return self._active
 
-- 
tg: (29aa609..) t/set-window-icon (depends on: upstream/master)


More information about the Sugar-devel mailing list