Thursday, December 4, 2008

Walk through a small modification to Sugar

Some weeks ago, Sugar gained a general way for inspecting the source of any activity:



On OLPC's XO, that dialog is triggered by hitting Fn+space, and the space bar is conveniently tagged with a gear to indicate that. But we want Sugar to run equally well on other machines, so if we use a shortcut like alt-ctrl-v, we need to display it in the UI somehow so it's discoverable. One solution is to add an entry to the activity palette and show the shortcut there. This is the palette before the modification:



I'm going to do this modification and will try to explain here the process I followed. Please ask in the comments below any clarifications you need, though I'm going to assume that you have managed to install and run successfully sugar in jhbuild, see http://sugarlabs.org/go/DevelopmentTeam/Jhbuild for more details.

Also, you will need to have some knowledge of PyGTK in order to do similar modifications of Sugar, though going through the PyGTK tutorial should be enough: http://www.pygtk.org/pygtk2tutorial/index.html

So, first thing is to check out where is the palette (menu) that we want to add the item to. For that, one way is to see a string that appears in that palette and grep the shell sources for it:


$ cd ~/sugar-jhbuild/source/sugar
$ grep -R Resume src


That results in four files, so we need to decide which one is the right one:


src/jarabe/journal/palettes.py: resume_label = _('Resume')
src/jarabe/journal/journaltoolbox.py: self._resume.set_tooltip(_('Resume'))
src/jarabe/desktop/meshbox.py: item = MenuItem(_('Resume'), 'activity-start')
src/jarabe/view/palettes.py: menu_item = MenuItem(_('Resume'), 'activity-start')


The modification we want to do is not in the journal nor in the mesh view, so we decide we are going to look into src/jarabe/view/palettes.py.

If we search inside that file, we can see that that line is inside the constructor of CurrentActivityPalette, so that seems indeed the place where we want to add the view source menu item.

So with a bit of copy pasting, we can base on the Resume menu item and add our View source one:


diff --git a/src/jarabe/view/palettes.py b/src/jarabe/view/palettes.py
index 5ba2cc2..ca7b079 100644
--- a/src/jarabe/view/palettes.py
+++ b/src/jarabe/view/palettes.py
@@ -63,6 +63,12 @@ class CurrentActivityPalette(BasePalette):
self.menu.append(menu_item)
menu_item.show()

+ # TODO: ask Eben for a nice icon for this menu entry
+ menu_item = MenuItem(_('View source'), 'activity-start')
+ menu_item.connect('activate', self.__view_source_activate_cb)
+ self.menu.append(menu_item)
+ menu_item.show()
+
# TODO: share-with, keep

separator = gtk.SeparatorMenuItem()
@@ -77,6 +83,9 @@ class CurrentActivityPalette(BasePalette):
def __resume_activate_cb(self, menu_item):
self._home_activity.get_window().activate(gtk.get_current_event_time())

+ def __view_source_activate_cb(self, menu_item):
+ logging.debug('View source menu item was activated!')
+
def __stop_activate_cb(self, menu_item):
self._home_activity.get_window().close(1)



So, after running make install and sugar-emulator, we can see how the launched activities have that extra menu item:



And what happens when we click on it? An error :(


Traceback (most recent call last):
File "/home/tomeu/sugar-jhbuild/install/lib/python2.5/site-packages/jarabe/view/palettes.py", line 87, in __view_source_activate_cb
logging.debug('View source menu item was activated!')
NameError: global name 'logging' is not defined


You can see those lines printed in the same terminal window where you ran sugar-emulator. The problem is a missing import logging so we can just add that and try again.

The next step is to add an accelerator to that menu item, and finally actually do something when the user clicks on that menu item, but that will have tomorrow as I _really_ need to go cook dinner.

No comments: