From fd40c138c68dac58791596814c47b27511123e85 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Sat, 27 Jan 2007 01:36:31 +0100 Subject: [PATCH] Rework icon sizing (unfinished, but more or less functional) --- shell/view/stylesheet.py | 49 +++++++++++---------- sugar/graphics/canvasicon.py | 83 +++++++++++++++++++++++------------- sugar/graphics/style.py | 10 ++--- sugar/graphics/stylesheet.py | 6 +-- 4 files changed, 86 insertions(+), 62 deletions(-) diff --git a/shell/view/stylesheet.py b/shell/view/stylesheet.py index 031716b6..088875c7 100644 --- a/shell/view/stylesheet.py +++ b/shell/view/stylesheet.py @@ -19,62 +19,61 @@ import hippo from sugar.graphics.iconcolor import IconColor from sugar.graphics import style +from sugar.graphics.grid import Grid + +grid = Grid() frame_ActivityIcon = { - 'color' : IconColor('white'), - 'size' : style.standard_icon_size -} - -frame_ShutdownIcon = { - 'size' : style.standard_icon_size + 'color' : IconColor('white'), + 'box-width' : grid.dimension(1) } frame_OverlayIcon = { - 'size' : style.standard_icon_size + 'box-width' : grid.dimension(1), + 'box-height' : grid.dimension(1) } frame_ZoomIcon = { - 'size' : style.standard_icon_size + 'box-width' : grid.dimension(1), + 'box-height' : grid.dimension(1) } frame_BuddyIcon = { - 'size' : style.standard_icon_size + 'box-width' : grid.dimension(1), + 'box-height' : grid.dimension(1) } home_MyIcon = { - 'size' : style.xlarge_icon_size + 'scale' : style.xlarge_icon_scale } ring_ActivityIcon = { - 'size' : style.medium_icon_size + 'scale' : style.medium_icon_scale } friends_MyIcon = { - 'size' : style.large_icon_size + 'scale' : style.large_icon_scale } friends_FriendIcon = { - 'size' : style.large_icon_size + 'scale' : style.large_icon_scale } friends_ActivityIcon = { - 'size' : style.standard_icon_size + 'scale' : style.standard_icon_scale } -clipboard_bubble = { - 'fill-color' : 0x646464FF, - 'stroke-color' : 0x646464FF, - 'progress-color': 0x333333FF, +clipboard_Bubble = { + 'fill-color' : 0x646464FF, + 'stroke-color' : 0x646464FF, + 'progress-color' : 0x333333FF, 'spacing' : style.space_unit, 'padding' : style.space_unit * 1.5 } -clipboard_menu_item_title = { - 'xalign': hippo.ALIGNMENT_CENTER, +clipboard_MenuItem_Title = { + 'xalign' : hippo.ALIGNMENT_CENTER, 'padding-left': 5, - 'color' : 0xFFFFFFFF, - 'font' : style.get_font_description('Bold', 1.2) + 'color' : 0xFFFFFFFF, + 'font' : style.get_font_description('Bold', 1.2) } - -style.register_stylesheet("clipboard.Bubble", clipboard_bubble) -style.register_stylesheet("clipboard.MenuItem.Title", clipboard_menu_item_title) diff --git a/sugar/graphics/canvasicon.py b/sugar/graphics/canvasicon.py index 7da71f7a..f0b89397 100644 --- a/sugar/graphics/canvasicon.py +++ b/sugar/graphics/canvasicon.py @@ -59,8 +59,8 @@ class _IconCache: 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) + def _get_real_name_from_theme(self, name): + info = self._theme.lookup_icon(name, 50, 0) if not info: raise ValueError("Icon '" + name + "' not found.") fname = info.get_filename() @@ -100,9 +100,12 @@ class _IconCache: 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): + if name == None: + return None + if name[0:6] == "theme:": - name = self._get_real_name_from_theme(name[6:], size) + name = self._get_real_name_from_theme(name[6:]) if color: key = (name, color.to_string()) @@ -132,8 +135,8 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem): gobject.PARAM_READWRITE), 'color' : (object, None, None, gobject.PARAM_READWRITE), - 'size' : (int, None, None, - 0, 1024, 24, + 'scale' : (float, None, None, + 0.0, 1024.0, 1.0, gobject.PARAM_READWRITE), 'cache' : (bool, None, None, False, gobject.PARAM_READWRITE) @@ -144,10 +147,11 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem): def __init__(self, **kwargs): self._buffers = {} self._cur_buffer = None - self._size = 24 + self._scale = 1.0 self._color = None self._icon_name = None self._cache = False + self._handle = None hippo.CanvasBox.__init__(self, **kwargs) @@ -165,26 +169,34 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem): if self._icon_name != value and not self._cache: self._clear_buffers() self._icon_name = value + self._handle = None self.emit_paint_needed(0, 0, -1, -1) elif pspec.name == 'color': if self._color != value and not self._cache: self._clear_buffers() self._color = value + self._handle = None self.emit_paint_needed(0, 0, -1, -1) - elif pspec.name == 'size': - if self._size != value and not self._cache: + elif pspec.name == 'scale': + if self._scale != value and not self._cache: self._clear_buffers() - self._size = value + self._scale = value self.emit_request_changed() elif pspec.name == 'cache': self._cache = value + def _get_handle(self): + if not self._handle: + cache = CanvasIcon._cache + self._handle = cache.get_handle(self._icon_name, self._color) + return self._handle + def _get_current_buffer_key(self): - return (self._icon_name, self._color, self._size) + return (self._icon_name, self._color, self._scale) def do_get_property(self, pspec): - if pspec.name == 'size': - return self._size + if pspec.name == 'scale': + return self._scale elif pspec.name == 'icon-name': return self._icon_name elif pspec.name == 'color': @@ -192,21 +204,32 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem): elif pspec.name == 'cache': return self._cache + def _get_icon_size(self): + handle = self._get_handle() + if handle: + dimensions = handle.get_dimension_data() + + width = int(dimensions[0] * self._scale) + 1 + height = int(dimensions[1] * self._scale) + 1 + + return [width, height] + + return [0, 0] + def _get_buffer(self, cr, handle): key = self._get_current_buffer_key() buf = None + if self._buffers.has_key(key): buf = self._buffers[key] else: target = cr.get_target() - size = int(self._size) + 1 - buf = target.create_similar(cairo.CONTENT_COLOR_ALPHA, size, size) - dimensions = handle.get_dimension_data() - scale = float(size) / float(dimensions[0]) + [w, h] = self._get_icon_size() + buf = target.create_similar(cairo.CONTENT_COLOR_ALPHA, w, h) ctx = cairo.Context(buf) - ctx.scale(scale, scale) + ctx.scale(self._scale, self._scale) handle.render_cairo(ctx) del ctx self._buffers[key] = buf @@ -214,26 +237,28 @@ class CanvasIcon(hippo.CanvasBox, hippo.CanvasItem): return buf def do_paint_below_children(self, cr, damaged_box): - icon_name = self._icon_name - if icon_name == None: - icon_name = 'theme:stock-missing' + handle = self._get_handle() + if handle == None: + return - handle = CanvasIcon._cache.get_handle( - icon_name, self._color, self._size) buf = self._get_buffer(cr, handle) [width, height] = self.get_allocation() - x = (width - self._size) / 2 - y = (height - self._size) / 2 + [icon_width, icon_height] = self._get_icon_size() + x = (width - icon_width) / 2 + y = (height - icon_height) / 2 + print x, y, width, icon_width cr.set_source_surface(buf, x, y) cr.paint() - def do_get_width_request(self): - return self._size + def do_get_content_width_request(self): + [width, height] = self._get_icon_size() + return width - def do_get_height_request(self, for_width): - return self._size + def do_get_content_height_request(self, for_width): + [width, height] = self._get_icon_size() + return height def _button_press_event_cb(self, item, event): item.emit_activated() diff --git a/sugar/graphics/style.py b/sugar/graphics/style.py index 6c4bff32..07eb1cc4 100644 --- a/sugar/graphics/style.py +++ b/sugar/graphics/style.py @@ -24,11 +24,11 @@ _screen_factor = gtk.gdk.screen_width() / 1200.0 space_unit = 9 * _screen_factor separator_thickness = 3 * _screen_factor -standard_icon_size = int(75.0 * _screen_factor) -small_icon_size = standard_icon_size * 0.5 -medium_icon_size = standard_icon_size * 1.5 -large_icon_size = standard_icon_size * 2.0 -xlarge_icon_size = standard_icon_size * 3.0 +standard_icon_scale = 1.0 +small_icon_scale = 0.5 +medium_icon_scale = 1.5 +large_icon_scale = 2.0 +xlarge_icon_scale = 3.0 def load_stylesheet(module): for objname in dir(module): diff --git a/sugar/graphics/stylesheet.py b/sugar/graphics/stylesheet.py index ae9b4c80..af32a7d0 100644 --- a/sugar/graphics/stylesheet.py +++ b/sugar/graphics/stylesheet.py @@ -17,15 +17,15 @@ menu_Separator = { } menu_ActionIcon = { - 'size' : style.standard_icon_size + 'scale' : style.standard_icon_scale } menu_Item = { - 'color' : 0xFFFFFFFF, + 'color' : 0xFFFFFFFF, 'font' : style.get_font_description('Plain', 1.1) } menu_Text = { - 'color' : 0xFFFFFFFF, + 'color' : 0xFFFFFFFF, 'font' : style.get_font_description('Plain', 1.2) }