When the Unity developers introduced Dash Previews in Unity 6, I knew it was something I wanted to add to Singlet. I didn’t have time to get the feature added in time to get it into Quantal’s Universe archive, but thanks to Didier Roche and Iain Lane, I was able to get it into Quantal’s Backports archive before the actual release date, so it will be available to all Ubuntu users right away.
Previews for all!
One of the main goals of Singlet, second only to making it easy to write Unity lenses and scopes, was to automatically add new features to any lenses and scopes written with it. Previews are my first opportunity to put this into practice. Singlet 0.3 will add Preview information for any Scope or SingleScopeLens written for Singlet 0.2! To do this, Singlet 0.3 will use the same image, title and description used in the search results to populate the preview. This is a big improvement over having no preview at all, and there is absolutely nothing the developer needs to do. Even better, if you have a custom handle_uri method, it will also add an “Open” button to your preview which will call it.
Better, faster, simpler Previews
Getting previews for free is nice, but it does limit the preview to only the information you are giving to the result item. But the Previews API allows you to do so much more, and Singlet lenses and scopes can take full advantage of them.
The simplest way to add more data to your preview is to add a method to your Scope or SingleScopeLens class called add_preview_data. This method will be called whenever Unity needs to show a preview for one of your result items, and will be given the specific result item being previewed, as well as a reference to the Unity.Preview object itself.
def add_preview_data(self, result_item, preview): if result_item['category'] == self.lens.events: url_parts = result_item['uri'].split('/') event = self._ltp.getTeamEvent(url_parts[5]) venue = self._ltp.getVenue(event['venue']) if 'latitude' in venue and 'longitude' in venue: preview.props.image_source_uri = 'http://maps.googleapis.com/maps/api/staticmap?center=%s,%s&zoom=11&size=600x600&markers=%s,%s&sensor=false' % (venue['latitude'], venue['longitude'], venue['latitude'], venue['longitude'])
The result_item is a Python dict containing the keys ‘uri’, ‘image’, ‘category’, ‘mime-type’, ‘title’, ‘description’, and ‘dnd-uri’, the same fields you added to the results model in your search field. The code above, added to the LoCo Teams scope, sets the Preview image to a Google Maps view of the venue’s location. You can also add additional Preview Actions from within this method.
If you want even more control, you can instead add a method called simply preview to your class, which takes the result_item and the full result_model from your scope, letting you create a Unity.Preview object yourself, and doing whatever you want with it.
def preview(self, result_item, result_model): preview = Unity.GenericPreview.new(result_item['title'], result_item['description'], None) preview.props.image_source_uri = result_item['image'] some_action = Unity.PreviewAction.new("do_something", "Do Something", None) some_action.connect('activated', self.do_something) preview.add_action(some_action) return preview