From c742cff64dde66ca7d8f2f68b1c41a023640d154 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Mon, 27 Aug 2007 22:10:18 +0200 Subject: [PATCH] Show arrows only when the icons does not fit. --- sugar/graphics/tray.py | 33 +++++++++++++++++++++++++++++++-- tests/graphics/tray.py | 9 +++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/sugar/graphics/tray.py b/sugar/graphics/tray.py index bfc4ad1a..cf646a6f 100644 --- a/sugar/graphics/tray.py +++ b/sugar/graphics/tray.py @@ -22,7 +22,14 @@ from sugar.graphics.toolbutton import ToolButton from sugar.graphics.icon import Icon class _TrayViewport(gtk.Viewport): + __gproperties__ = { + 'can-scroll' : (bool, None, None, False, + gobject.PARAM_READABLE), + } + def __init__(self): + self._can_scroll = False + gobject.GObject.__init__(self) self.set_shadow_type(gtk.SHADOW_NONE) @@ -32,6 +39,8 @@ class _TrayViewport(gtk.Viewport): self.add(self.traybar) self.traybar.show() + self.connect('size_allocate', self._size_allocate_cb) + def scroll_right(self): adj = self.get_hadjustment() new_value = adj.value + self.allocation.width @@ -42,6 +51,21 @@ class _TrayViewport(gtk.Viewport): new_value = adj.value - self.allocation.width adj.value = max(adj.lower, new_value) + def do_get_property(self, pspec): + if pspec.name == 'can-scroll': + return self._can_scroll + + def _size_allocate_cb(self, viewport, allocation): + bar_requisition = self.traybar.get_child_requisition() + if bar_requisition[0] < allocation.width: + can_scroll = False + else: + can_scroll = True + + if can_scroll != self._can_scroll: + self._can_scroll = can_scroll + self.notify('can-scroll') + class HTray(gtk.HBox): def __init__(self, **kwargs): gobject.GObject.__init__(self, **kwargs) @@ -55,9 +79,10 @@ class HTray(gtk.HBox): icon.show() self.pack_start(self._scroll_left, False) - self._scroll_left.show() self._viewport = _TrayViewport() + self._viewport.connect('notify::can-scroll', + self._viewport_can_scroll_changed_cb) self.pack_start(self._viewport) self._viewport.show() @@ -70,7 +95,11 @@ class HTray(gtk.HBox): icon.show() self.pack_start(self._scroll_right, False) - self._scroll_right.show() + + def _viewport_can_scroll_changed_cb(self, viewport, pspec): + if self._viewport.props.can_scroll: + self._scroll_left.show() + self._scroll_right.show() def _scroll_left_cb(self, button): self._viewport.scroll_left() diff --git a/tests/graphics/tray.py b/tests/graphics/tray.py index 8088fde7..9bbf0b22 100644 --- a/tests/graphics/tray.py +++ b/tests/graphics/tray.py @@ -41,6 +41,15 @@ for i in range(0, 100): tray.add_item(button) button.show() +tray = HTray() +box.pack_start(tray, False) +tray.show() + +for i in range(0, 10): + button = TrayButton(icon_name=theme_icons[i]) + tray.add_item(button) + button.show() + test.pack_start(box) box.show()