Search code examples
gtkpygtkappindicator

Python AppIndicator to make tomate Unity compliant


I just discovered tomate : https://gitorious.org/tomate a very simple program that help get things done when you work on a computer.

But tomate use gtk.status_icon instead of appindicator so I would like to fix this.

However, I would like to keep the same behaviour with appindicator. It is very easy to use like this and I don't whan to create a menu and complicate its use.

The simple behaviour is that when you click on the icon you start the time counter and when you click again you stop it.

So you don't need a menu.

Is it possible to use appindicator without a menu or with a basic menu that just receive the click and don't have any items?

Thank for you help.


Solution

  • Actually the menu is what makes appindicator so different.

    So basically, I just created a menu and change the way I update the menu and the icon.

    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")