#!/usr/bin/env python # Lliurex taskmanager # Lliurex Team 2012 import os import time import sys from gi.repository import Gtk, Gdk, GLib, GObject, GdkPixbuf, Gio, Pango from gi.repository import Wnck from gi.repository import PanelApplet options_show_all_workspaces=True options_group_windows=True #TaskManager Buttons class WinButton(Gtk.Button): def __init__(self,win): super(WinButton,self).__init__() self.win=win self.set_size_request(60,60) img=Gtk.Image(pixbuf=win.get_icon()) self.set_tooltip_text(win.get_name()) self.add(img) self.show_all() class Group(Gtk.HBox): def __init__(self,app,win_list): super(Group,self).__init__() self.win_list=win_list self.grouped=True btn = Gtk.Button() img = Gtk.Image(pixbuf = app.get_icon()) btn.add(img) btn.connect("clicked",self.onclick) self.pack_start(btn,False,False,0) self.show_all() self.btns=[] for win in win_list: wbtn = WinButton(win) wbtn.connect("clicked",Panel.instance.onclick) wbtn.hide() self.pack_end(wbtn,False,False,0) self.btns.append(wbtn) def onclick(self,widget): print "group button click" self.grouped=not self.grouped if not self.grouped: for b in self.btns: b.show_all() else: for b in self.btns: b.hide() class Panel: instace=None #dbg: True/False def __init__(self,applet,dbg): """ window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL) window.set_title('lliurex-taskman') window.set_position(Gtk.WindowPosition.CENTER) window.resize(300,80) """ Panel.instance= self self.dbg=dbg self.hbox = Gtk.HBox(homogeneous = False, spacing = 0) applet.add(self.hbox) #window.add(self.hbox) self.screen = Wnck.Screen.get_default() self.screen.connect("window-opened",self.onWindowOpened) self.screen.connect("window-closed",self.onWindowClosed) self.screen.connect("application-opened",self.onopened) self.screen.connect("application-closed",self.onclosed) self.screen.connect("active-workspace-changed",self.onworkspace) self.screen.connect("viewports-changed",self.onviewport) #comment the following line for window testing if not self.dbg: applet.set_flags(PanelApplet.AppletFlags.HAS_HANDLE | PanelApplet.AppletFlags.EXPAND_MINOR) applet.show_all() def rebuild(self): wcount= self.screen.get_workspace_count() #build dictionary applications={} for win in self.screen.get_windows(): if win.get_window_type()==Wnck.WindowType.NORMAL: if win.get_application() in applications.keys(): applications[win.get_application()].append(win) else: applications[win.get_application()]=[win] #step1: free hbox cwin=self.hbox.get_children() for win in cwin: self.hbox.remove(win) #fill in for k in applications.keys(): if len(applications[k])==1: button = WinButton(applications[k][0]) button.connect("clicked",self.onclick) self.hbox.pack_start(button,False,False,0) else: grp = Group(k,applications[k]) self.hbox.pack_start(grp,False,False,0) """ #compiz case if wcount==1: x=self.screen.get_workspaces()[0].get_viewport_x() y=self.screen.get_workspaces()[0].get_viewport_y() w=self.screen.get_width() h=self.screen.get_height() #print "Screen width:",w #print "Screen height:",h #step1: free hbox cwin=self.hbox.get_children() for win in cwin: self.hbox.remove(win) #OPTION: show windows on all workspaces if options_show_all_workspaces: apps=[] for win in self.screen.get_windows(): #OPTION: group windows by app if options_group_windows: if not (win.get_application() in apps): #step 2: add to hbox if win.get_window_type()==Wnck.WindowType.NORMAL: apps.append(win.get_application()) button=WinButton(win) button.connect("clicked",self.onclick) self.hbox.pack_start(button,False,False,0) else: if win.get_window_type()==Wnck.WindowType.NORMAL: button=WinButton(win) button.connect("clicked",self.onclick) self.hbox.pack_start(button,False,False,0) else: apps=[] for win in self.screen.get_windows(): wx,wy,ww,wh=win.get_geometry() print "win: ",wx,wy,"-->",win.get_name() if(wx>=0 and wx<=w and wy>=0 and wy<=h): print "INSIDE: ",win.get_name() if options_group_windows: if not (win.get_application() in apps): if win.get_window_type()==Wnck.WindowType.NORMAL: apps.append(win.get_application()) button=WinButton(win) button.connect("clicked",self.onclick) self.hbox.pack_start(button,False,False,0) else: #step 2: add to hbox if win.get_window_type()==Wnck.WindowType.NORMAL: button=WinButton(win) button.connect("clicked",self.onclick) self.hbox.pack_start(button,False,False,0) """ def onviewport(self,scr): print "Viewport changed!" print "Workspaces:",self.screen.get_workspace_count() print "WM:",self.screen.get_window_manager_name() self.rebuild() def onworkspace(self,scr,wrk): if not wrk==None: print "Workspace->",wrk.get_number() else: print "Workspace?" def onclosed(self,scr,app): print "CA->",app.get_name() def onopened(self,scr,app): print "OA->",app.get_name() def onWindowOpened(self,scr,win): self.rebuild() """ print "WO->",win.get_name() wtype=win.get_window_type() wcount=self.screen.get_workspace_count() if wcount==1: #if not wtype==Wnck.WindowType.DESKTOP and not wtype==Wnck.WindowType.DOCK: if wtype==Wnck.WindowType.NORMAL: button=WinButton(win) button.connect("clicked",self.onclick) self.hbox.pack_start(button,False,False,0) """ def onWindowClosed(self,scr,win): self.rebuild() """ print "WC->",win.get_name() ws=self.hbox.get_children() for w in ws: if w.win==win: self.hbox.remove(w) break """ def onclick(self,wdg): print "click on ",wdg.win.get_name() wcount=self.screen.get_workspace_count() #compiz case if wcount==1: x,y,w,h=wdg.win.get_geometry() print "Pos:%d,%d"%(x,y) wrk=self.screen.get_workspaces()[0] if not wdg.win.is_in_viewport(wrk): print "moving on" self.screen.move_viewport(self.screen.get_workspaces()[0].get_viewport_x()+x,self.screen.get_workspaces()[0].get_viewport_y()+y) #wdg.win.activate(int(time.time())) #Applet factory callback def applet_factory(applet, iid, data): if iid != "LliurexTaskmanApplet": return False Panel(applet,False) return True if __name__=="__main__": print "Lliurex taskman" if(len(sys.argv)>1): if sys.argv[1]=="--test": window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL) window.set_title('lliurex-taskman') window.set_position(Gtk.WindowPosition.CENTER) window.connect("destroy",Gtk.main_quit) window.resize(300,80) p = Panel(window,True) Gtk.main() else: PanelApplet.Applet.factory_main("LliurexTaskmanFactory",PanelApplet.Applet.__gtype__,applet_factory, None)