Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm writing an app where I want a small window to open next to its launcher item. I can have it open at the mouse position (which will be near the launcher), but that isn't very precise or satisfying. How can I get the screen position of a launcher icon using python so I can set my Gtk.Window position? Thanks!

share|improve this question

1 Answer 1

up vote 0 down vote accepted

This is what I've been able to figure out, although it's not as satisfying as just getting x,y coordinates.

You can get an ordered list of the launcher items currently in the panel, then find your launchers index in the list and multiply by the icon size + some padding for the top panel and icon spacing to give approximate y coordinate.

See code below. I hope this helps anyone else who may be searching for a way to do this.

    from gi.repository import Unity
    import gconf

    #Get Icon size
    LF_ICONSIZE=gconf.client_get_default().get_int('/apps/compiz-1/plugins/unityshell/screen0/options/icon_size')

    LF_ICONPADDING=10 # Guesstimate
    PANEL_HEIGHT=16  # Guesstimate, will depend on fontsize. Don't know where to get this
    #Use unity api to get a list of launcher panel items  
    lf=Unity.LauncherFavorites.get_default().enumerate_ids()

    #Find the position of my .desktop file 
    #(add 2 for the dash icon and because lists start at 0)
    pos=lf.index('nautilus.desktop') + 2

    #calculate approximate y coordinate
    y = pos * (LF_ICONSIZE + LF_ICONPADDING) + PANEL_HEIGHT
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.