Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 103 lines (83 sloc) 3.042 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
from gi.repository import Gtk, Gdk, GdkPixbuf, Peas, GObject
import cairo

iconsPath = "/usr/share/icons/"
rhythmboxIcon = iconsPath + "hicolor/32x32/apps/rhythmbox.png"
playIcon = iconsPath + "gnome/32x32/actions/media-playback-start.png"

class TrayIcon(GObject.Object, Peas.Activatable):

__gtype_name = 'TrayIcon'
object = GObject.property(type=GObject.Object)

def popup_menu(self, icon, button, time, data = None):
self.popup.popup(None, None, None, None, button, time)

def toggle(self, icon, event, data = None):
if event.button == 1: # left button
if self.wind.get_visible():
self.wind.hide()
else:
self.wind.show()
self.wind.present()

def play(self, widget):
self.player.playpause(True) # does nothing argument

def nextItem(self, widget):
self.player.do_next()

def previous(self, widget):
self.player.do_previous()

  def quit(self, widget):
self.shell.quit()

def hide_on_delete(self, widget, event):
self.wind.hide()
return True # don't actually delete

def set_playing_icon(self, player, playing):
if playing:
self.icon.set_property("pixbuf", self.playIcon)
else:
self.icon.set_property("pixbuf", self.normalIcon)

def do_activate(self):
self.shell = self.object
self.wind = self.shell.get_property("window")
self.player = self.shell.props.shell_player

self.wind.connect("delete-event", self.hide_on_delete)

ui = Gtk.UIManager()
ui.add_ui_from_string(
"""
<ui>
<popup name='PopupMenu'>
<menuitem action='PlayPause' />
<menuitem action='Next' />
<menuitem action='Previous' />
<separator />
<menuitem action='Quit' />
</popup>
</ui>
""")

ag = Gtk.ActionGroup("actions")
ag.add_actions([
("PlayPause",Gtk.STOCK_MEDIA_PLAY,"Play/Pause",None, None, self.play),
("Next",Gtk.STOCK_MEDIA_NEXT,"Next",None, None, self.nextItem),
("Previous",Gtk.STOCK_MEDIA_PREVIOUS,"Previous",None, None, self.previous),
("Quit",None,"Quit",None, None, self.quit)
])
ui.insert_action_group(ag)
self.popup = ui.get_widget("/PopupMenu")

s1 = cairo.ImageSurface.create_from_png(rhythmboxIcon)
s2 = cairo.ImageSurface.create_from_png(playIcon)
ctx = cairo.Context(s1)
ctx.set_source_surface(s2, 0, 0)
ctx.paint()
self.playIcon = Gdk.pixbuf_get_from_surface(s1, 0, 0, s1.get_width(), s1.get_height())

self.normalIcon = GdkPixbuf.Pixbuf.new_from_file(rhythmboxIcon)
self.icon = Gtk.StatusIcon.new_from_pixbuf(self.normalIcon)
self.icon.connect("scroll-event", self.scroll)
self.icon.connect("popup-menu", self.popup_menu)
self.icon.connect("button-press-event", self.toggle)
self.player.connect("playing-changed", self.set_playing_icon)

def scroll(self, widget, event):
if self.player.playpause(True):
# scroll up for previous track
if event.direction == Gdk.ScrollDirection.UP:
self.previous(widget)
# scroll down for next track
elif event.direction == Gdk.ScrollDirection.DOWN:
self.nextItem(widget)

def do_deactivate(self):
self.icon.set_visible(False)
del self.icon
Something went wrong with that request. Please try again.