def __init__(self): Applet.__init__(self) self.default_size = 22 self.padding = 5 self.host = StatusNotifierHost() self.host.connect('item-added', self.item_added_cb) self.host.connect('item-removed', self.item_added_cb) self.indicators = [Indicator(item, self) for item in self.host.items] self.connect('click', self.click_cb)
class ApplicationIndicatorApplet(Applet): def __init__(self): Applet.__init__(self) self.default_size = 22 self.padding = 5 self.host = StatusNotifierHost() self.host.connect('item-added', self.item_added_cb) self.host.connect('item-removed', self.item_added_cb) self.indicators = [Indicator(item, self) for item in self.host.items] self.connect('click', self.click_cb) def item_added_cb(self, host, item): self.indicators = [Indicator(item, self) for item in self.host.items] self.allocate(self.get_allocation()[1]) self.draw() def item_removed_cb(self, host, item): self.indicators = [Indicator(item, self) for item in self.host.items] self.allocate(self.get_allocation()[1]) self.draw() def get_indicator_at_coords(self, x, y): for c, indicator in enumerate(self.indicators): w = h = self.default_size x0, y0 = PADDING + c * (w + PADDING), 1 x1, y1 = x0 + w, y0 + h if x >= x0 and x <= x1 and y >= y0 and y <= y1: return indicator return None def click_cb(self, applet, x, y): indicator = self.get_indicator_at_coords(x, y) if indicator: menu = indicator.item.dbusmenu_gtk.root_widget menu.popup(None, None, None, 1, 0) win = menu.get_parent() x, y = win.get_position() win.move(x, self.get_allocation()[1] + 1) def get_size(self): allocation = self.get_allocation() if allocation is not None: return allocation[1] else: return self.default_size def render(self, ctx): position = PADDING for indicator in self.indicators: icon_path = indicator.get_icon_path(self.get_size()) icon_surface = cairo.ImageSurface.create_from_png(icon_path) height = icon_surface.get_height() width = icon_surface.get_width() ctx.set_source_surface(icon_surface, position, (self.get_allocation()[1] - height) / 2) ctx.paint() position += width + self.padding def allocate(self, height): width = PADDING for indicator in self.indicators: icon_path = indicator.get_icon_path(self.get_size()) icon_surface = cairo.ImageSurface.create_from_png(icon_path) width += icon_surface.get_width() + PADDING self.set_allocation(width, height) return self.get_allocation()