Merge branch 'master' of git+ssh://dev.laptop.org/git/sugar

This commit is contained in:
Marco Pesenti Gritti 2007-01-15 11:55:35 +01:00
commit c30451b19e
2 changed files with 140 additions and 66 deletions

View File

@ -18,7 +18,6 @@ import hippo
import math import math
import gobject import gobject
import colorsys import colorsys
import logging
from sugar.graphics.canvasicon import CanvasIcon from sugar.graphics.canvasicon import CanvasIcon
from sugar.graphics import style from sugar.graphics import style
@ -45,19 +44,19 @@ def html_to_rgb(html_color):
return (r, g, b) return (r, g, b)
class ActivityIcon(CanvasIcon): class ActivityIcon(CanvasIcon):
_LEVEL_MAX = 1.6 _INTERVAL = 150
_LEVEL_MIN = 0.0
_INTERVAL = 100
def __init__(self, activity): def __init__(self, activity):
icon_name = activity.get_icon_name() icon_name = activity.get_icon_name()
self._orig_color = profile.get_color() self._orig_color = profile.get_color()
self._icon_colors = self._compute_icon_colors()
self._direction = 0 self._direction = 0
self._level = self._LEVEL_MAX self._level_max = len(self._icon_colors) - 1
color = self._get_icon_color_for_level() self._level = self._level_max
color = self._icon_colors[self._level]
CanvasIcon.__init__(self, icon_name=icon_name, color=color) CanvasIcon.__init__(self, icon_name=icon_name, color=color, cache=True)
style.apply_stylesheet(self, 'ring.ActivityIcon') style.apply_stylesheet(self, 'ring.ActivityIcon')
self._activity = activity self._activity = activity
@ -68,32 +67,44 @@ class ActivityIcon(CanvasIcon):
self.cleanup() self.cleanup()
def cleanup(self): def cleanup(self):
logging.debug("removing source %s" % self._pulse_id)
if self._pulse_id > 0: if self._pulse_id > 0:
gobject.source_remove(self._pulse_id) gobject.source_remove(self._pulse_id)
self._pulse_id = 0 self._pulse_id = 0
# dispose of all rendered icons from launch feedback
self._clear_buffers()
def _get_icon_color_for_level(self): def _compute_icon_colors(self):
factor = math.sin(self._level) _LEVEL_MAX = 1.6
_LEVEL_STEP = 0.16
_LEVEL_MIN = 0.0
icon_colors = {}
level = _LEVEL_MIN
for i in range(0, int(_LEVEL_MAX / _LEVEL_STEP)):
icon_colors[i] = self._get_icon_color_for_level(level)
level += _LEVEL_STEP
return icon_colors
def _get_icon_color_for_level(self, level):
factor = math.sin(level)
h, s, v = colorsys.rgb_to_hsv(*html_to_rgb(self._orig_color.get_fill_color())) h, s, v = colorsys.rgb_to_hsv(*html_to_rgb(self._orig_color.get_fill_color()))
new_fill = rgb_to_html(*colorsys.hsv_to_rgb(h, s * factor, v)) new_fill = rgb_to_html(*colorsys.hsv_to_rgb(h, s * factor, v))
h, s, v = colorsys.rgb_to_hsv(*html_to_rgb(self._orig_color.get_stroke_color())) h, s, v = colorsys.rgb_to_hsv(*html_to_rgb(self._orig_color.get_stroke_color()))
new_stroke = rgb_to_html(*colorsys.hsv_to_rgb(h, s * factor, v)) new_stroke = rgb_to_html(*colorsys.hsv_to_rgb(h, s * factor, v))
return iconcolor.IconColor("%s,%s" % (new_fill, new_stroke)) return iconcolor.IconColor("%s,%s" % (new_stroke, new_fill))
def _pulse_cb(self): def _pulse_cb(self):
if self._direction == 1: if self._direction == 1:
self._level += 0.1 self._level += 1
if self._level >= self._LEVEL_MAX: if self._level > self._level_max:
self._direction = 0 self._direction = 0
self._level = self._LEVEL_MAX self._level = self._level_max
elif self._direction == 0: elif self._direction == 0:
self._level -= 0.1 self._level -= 1
if self._level <= self._LEVEL_MIN: if self._level <= 0:
self._direction = 1 self._direction = 1
self._level = self._LEVEL_MIN self._level = 0
self.props.color = self._get_icon_color_for_level() self.props.color = self._icon_colors[self._level]
self.emit_paint_needed(0, 0, -1, -1) self.emit_paint_needed(0, 0, -1, -1)
return True return True

View File

@ -16,30 +16,29 @@
# Boston, MA 02111-1307, USA. # Boston, MA 02111-1307, USA.
import re import re
import logging
import gobject import gobject
import gtk import gtk
import hippo import hippo
import rsvg import rsvg
import cairo import cairo
import time
from sugar.graphics.iconcolor import IconColor from sugar.graphics.iconcolor import IconColor
class _IconCache: class _IconCacheIcon:
def __init__(self): def __init__(self, name, color, now):
self._icons = {} self.data_size = None
self._theme = gtk.icon_theme_get_default() self.handle = self._read_icon_data(name, color)
self.last_used = now
self.usage_count = 1
def _read_icon(self, filename, color): def _read_icon_data(self, filename, color):
icon_file = open(filename, 'r') icon_file = open(filename, 'r')
data = icon_file.read()
icon_file.close()
if color == None: if color:
return rsvg.Handle(file=filename)
else:
data = icon_file.read()
icon_file.close()
fill = color.get_fill_color() fill = color.get_fill_color()
stroke = color.get_stroke_color() stroke = color.get_stroke_color()
@ -49,32 +48,81 @@ class _IconCache:
entity = '<!ENTITY stroke_color "%s">' % stroke entity = '<!ENTITY stroke_color "%s">' % stroke
data = re.sub('<!ENTITY stroke_color .*>', entity, data) data = re.sub('<!ENTITY stroke_color .*>', entity, data)
return rsvg.Handle(data=data) self.data_size = len(data)
return rsvg.Handle(data=data)
class _IconCache:
_CACHE_MAX = 50000 # in bytes
def __init__(self):
self._icons = {}
self._theme = gtk.icon_theme_get_default()
self._cache_size = 0
def _get_real_name_from_theme(self, name, size):
info = self._theme.lookup_icon(name, size, 0)
if not info:
raise ValueError("Icon '" + name + "' not found.")
fname = info.get_filename()
del info
return fname
def _cache_cleanup(self, key, now):
while self._cache_size > self._CACHE_MAX:
evict_key = None
oldest_key = None
oldest_time = now
for icon_key, icon in self._icons.items():
# Don't evict the icon we are about to use if it's in the cache
if icon_key == key:
continue
# evict large icons first
if icon.data_size > self._CACHE_MAX:
evict_key = icon_key
break
# evict older icons next; those used over 2 minutes ago
if icon.last_used < now - 120:
evict_key = icon_key
break
# otherwise, evict the oldest
if oldest_time > icon.last_used:
oldest_time = icon.last_used
oldest_key = icon_key
# If there's nothing specific to evict, try evicting
# the oldest thing
if not evict_key:
if not oldest_key:
break
evict_key = oldest_key
self._cache_size -= self._icons[evict_key].data_size
del self._icons[evict_key]
def get_handle(self, name, color, size): def get_handle(self, name, color, size):
if name[0:6] == "theme:": if name[0:6] == "theme:":
icon = self._read_icon_from_name(name[6:], color, size) name = self._get_real_name_from_theme(name[6:], size)
else:
icon = self._read_icon(name, color)
return icon
def _read_icon_from_name(self, name, color, size):
info = self._theme.lookup_icon(name, size, 0)
if not info:
raise "Icon '" + name + "' not found."
if color: if color:
key = (info.get_filename(), color.to_string()) key = (name, color.to_string())
else: else:
key = info.get_filename() key = name
# If we're over the cache limit, evict something from the cache
now = time.time()
self._cache_cleanup(key, now)
if self._icons.has_key(key): if self._icons.has_key(key):
icon = self._icons[key] icon = self._icons[key]
icon.usage_count += 1
icon.last_used = now
else: else:
icon = self._read_icon(info.get_filename(), color) icon = _IconCacheIcon(name, color, now)
self._icons[key] = icon self._icons[key] = icon
return icon self._cache_size += icon.data_size
return icon.handle
class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem): class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
__gtype_name__ = 'CanvasIcon' __gtype_name__ = 'CanvasIcon'
@ -86,39 +134,48 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
gobject.PARAM_READWRITE), gobject.PARAM_READWRITE),
'size' : (int, None, None, 'size' : (int, None, None,
0, 1024, 24, 0, 1024, 24,
gobject.PARAM_READWRITE),
'cache' : (bool, None, None, False,
gobject.PARAM_READWRITE) gobject.PARAM_READWRITE)
} }
_cache = _IconCache() _cache = _IconCache()
def __init__(self, **kwargs): def __init__(self, **kwargs):
self._buffer = None self._buffers = {}
self._cur_buffer = None
self._size = 24 self._size = 24
self._color = None self._color = None
self._icon_name = None self._icon_name = None
self._cache = False
hippo.CanvasBox.__init__(self, **kwargs) hippo.CanvasBox.__init__(self, **kwargs)
self.connect('button-press-event', self._button_press_event_cb) self.connect('button-press-event', self._button_press_event_cb)
def _invalidate_buffer(self, new_buffer): def _clear_buffers(self):
if self._buffer: for key in self._buffers.keys():
del self._buffer del self._buffers[key]
self._buffer = new_buffer self._buffers = {}
def do_set_property(self, pspec, value): def do_set_property(self, pspec, value):
if pspec.name == 'icon-name': if pspec.name == 'icon-name':
if self._icon_name != value and not self._cache:
self._clear_buffers()
self._icon_name = value self._icon_name = value
self._invalidate_buffer(None)
self.emit_paint_needed(0, 0, -1, -1) self.emit_paint_needed(0, 0, -1, -1)
elif pspec.name == 'color': elif pspec.name == 'color':
self._invalidate_buffer(None) if self._color != value and not self._cache:
self._clear_buffers()
self._color = value self._color = value
self.emit_paint_needed(0, 0, -1, -1) self.emit_paint_needed(0, 0, -1, -1)
elif pspec.name == 'size': elif pspec.name == 'size':
self._invalidate_buffer(None) if self._size != value and not self._cache:
self._clear_buffers()
self._size = value self._size = value
self.emit_request_changed() self.emit_request_changed()
elif pspec.name == 'cache':
self._cache = value
def do_get_property(self, pspec): def do_get_property(self, pspec):
if pspec.name == 'size': if pspec.name == 'size':
@ -127,33 +184,39 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem):
return self._icon_name return self._icon_name
elif pspec.name == 'color': elif pspec.name == 'color':
return self._color return self._color
elif pspec.name == 'cache':
return self._cache
def _get_buffer(self, cr, handle, size): def _get_buffer(self, cr, handle):
if self._buffer == None: key = (self._icon_name, self._color, self._size)
buf = None
if self._buffers.has_key(key):
buf = self._buffers[key]
else:
target = cr.get_target() target = cr.get_target()
surface = target.create_similar(cairo.CONTENT_COLOR_ALPHA, size = int(self._size) + 1
int(size) + 1, int(size) + 1) buf = target.create_similar(cairo.CONTENT_COLOR_ALPHA, size, size)
dimensions = handle.get_dimension_data() dimensions = handle.get_dimension_data()
scale = float(size) / float(dimensions[0]) scale = float(size) / float(dimensions[0])
ctx = cairo.Context(surface) ctx = cairo.Context(buf)
ctx.scale(scale, scale) ctx.scale(scale, scale)
handle.render_cairo(ctx) handle.render_cairo(ctx)
del ctx del ctx
self._buffers[key] = buf
self._buffer = surface return buf
return self._buffer
def do_paint_below_children(self, cr, damaged_box): def do_paint_below_children(self, cr, damaged_box):
icon_name = self._icon_name icon_name = self._icon_name
if icon_name == None: if icon_name == None:
icon_name = 'stock-missing' icon_name = 'theme:stock-missing'
handle = CanvasIcon._cache.get_handle( handle = CanvasIcon._cache.get_handle(
icon_name, self._color, self._size) icon_name, self._color, self._size)
buf = self._get_buffer(cr, handle, self._size) buf = self._get_buffer(cr, handle)
[width, height] = self.get_allocation() [width, height] = self.get_allocation()
x = (width - self._size) / 2 x = (width - self._size) / 2