实际上,菜单是使 appindicator 如此不同的原因。
所以基本上,我只是创建了一个菜单并更改了更新菜单和图标的方式。
diff -r 13c201b1030a tomate.py
--- a/tomate.py Fri May 13 19:48:31 2011 +0200
+++ b/tomate.py Wed May 18 09:26:43 2011 +0200
@@ -1,53 +1,79 @@
#!/usr/bin/env python
from __future__ import division
import pygtk
pygtk.require('2.0')
import gtk
import os
from time import time
from math import floor
gtk.gdk.threads_init()
import gobject
+import appindicator
#Parameters
MIN_WORK_TIME = 60 * 10 # min work time in seconds
class Pomodoro:
def __init__(self):
- self.icon=gtk.status_icon_new_from_file(self.icon_directory()+"idle.png")
- self.icon.set_tooltip("Idle")
+ self.ind = appindicator.Indicator("tomate","tomate",
+ appindicator.CATEGORY_APPLICATION_STATUS)
+
+ self.ind.set_status (appindicator.STATUS_ACTIVE)
+ self.ind.set_icon(self.icon_directory()+"idle.png")
self.state = "idle"
self.tick_interval=10 #number of seconds between each poll
- self.icon.connect('activate',self.icon_click)
- self.icon.set_visible(True)
self.start_working_time = 0
+ self.menu = gtk.Menu()
+ # Tooltip item
+ self.item = gtk.MenuItem('Idle')
+ self.item.connect("activate", self.icon_click, None)
+ self.item.show()
+ self.menu.append(self.item)
+ # A separator
+ separator = gtk.SeparatorMenuItem()
+ separator.show()
+ self.menu.append(separator)
+ # A quit item
+ item = gtk.MenuItem('Quit')
+ item.connect("activate", gtk.main_quit, None)
+ item.show()
+ self.menu.append(item)
+ self.menu.show_all()
+ self.ind.set_menu(self.menu)
def format_time(self,seconds):
minutes = floor(seconds / 60)
if minutes > 1:
return "%d minutes" % minutes
else:
- return "%d minute" % minutes
def set_state(self,state):
old_state=self.state
- self.icon.set_from_file(self.icon_directory()+state+".png")
+ self.ind.set_icon(self.icon_directory()+state+".png")
if state == "idle":
delta = time() - self.start_working_time
if old_state == "ok":
- self.icon.set_tooltip("Good! Worked for %s." %
+ self.item.get_child().set_text("Good! Worked for %s." %
self.format_time(delta))
elif old_state == "working":
- self.icon.set_tooltip("Not good: worked for only %s." %
+ self.item.get_child().set_text("Not good: worked for only %s." %
self.format_time(delta))
else:
if state == "working":
self.start_working_time = time()
delta = time() - self.start_working_time
- self.icon.set_tooltip("Working for %s..." % self.format_time(delta))
+ self.item.get_child().set_text("Working for %s..." % self.format_time(delta))
self.state=state
def icon_directory(self):
return os.path.dirname(os.path.realpath(__file__)) + os.path.sep
- def icon_click(self,dummy):
+ def icon_click(self, *args):
delta = time() - self.start_working_time
if self.state == "idle":
self.set_state("working")
@@ -59,7 +85,7 @@
if self.state == "idle":
pass
else:
- self.icon.set_tooltip("Working for %s..." % self.format_time(delta))
+ self.item.get_child().set_text("Working for %s..." % self.format_time(delta))
if self.state == "working":
if delta > MIN_WORK_TIME:
self.set_state("ok")