Singlet 0.3 adds Unity Previews

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'] ==
        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 = ',%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 =['title'], result_item['description'], None)
    preview.props.image_source_uri = result_item['image']

    some_action ="do_something", "Do Something", None)
    some_action.connect('activated', self.do_something)

    return preview
This entry was posted in OpenSource, Programming, Projects, Work and tagged , , , , , , . Bookmark the permalink.

10 Responses to Singlet 0.3 adds Unity Previews

  1. marc says:


    For a map I’d suggest an Open alternative ;-)

  2. Chris Wayne says:

    That’s awesome! I’ll see if I can add any cool previews to my singlet lenses now!

  3. Kevin H. says:

    Michael, thanks for your awesome singlet template. How do I make a lens to refresh data every time I click on the lens? Currently, it seems it reads from cache until I type something for the items to refresh. My workaround now is to simulate a space bar and backspace but it’s not a solid fix. Thanks in advance.

    • Michael Hall says:

      My understanding is that the Dash doesn’t call the search function in your scope/lens every time it is displayed, only when the user searches for something or when it is opened for the first time. This is for performance purposes, and I don’t think there’s any way around it.

    • Quentin Bérard says:

      Hi Kevin
      I’m very interesting with your workaround, I don’t find how to simulate keypress, I have googled a bit, and found some “solutions”, for example uinput who ask me su privileges…
      But nothing work as expected.

      I would like to refresh my search termes and empty the search bar


  4. Quentin Bérard says:

    I have found 2 solutions :

    sudo apt-get install xautomation

    escape='''key Escape
    from subprocess import Popen, PIPE
    p = Popen(['xte'], stdin=PIPE)

    sudo apt-get install xautomation

    from subprocess import Popen, PIPE
    p = Popen(['xdotool','key','Escape'])

    It’s erease all of the search field.

  5. Quentin Bérard says:

    erratum : For the second solution, install xdotool

Comments are closed.