Eu me interessei, então aqui está o meu código em Python:
#!/usr/bin/python
from time import time, ctime
from gi.repository import Gtk
from gi.repository import AppIndicator3 as Indicator
MY_FILENAME = '/home/your-username/myfile.log'
MY_INDICATOR_ID = 'my-indicator-example'
STOP_ICON_NAME = 'indicator-messages'
START_ICON_NAME = 'indicator-messages-new'
class MyIndicator(object):
def __init__(self, filename):
self.indicator = Indicator.Indicator.new(
MY_INDICATOR_ID,
STOP_ICON_NAME,
Indicator.IndicatorCategory.APPLICATION_STATUS)
self.indicator.set_status(Indicator.IndicatorStatus.ACTIVE)
self.indicator.set_attention_icon(START_ICON_NAME)
self.has_started = False
self.menu = Gtk.Menu()
start_item = Gtk.MenuItem('Start')
start_item.connect('activate', self.start_activated, filename)
self.menu.append(start_item)
start_item.show()
stop_item = Gtk.MenuItem('Stop')
stop_item.connect('activate', self.stop_activated, filename)
self.menu.append(stop_item)
stop_item.show()
self.indicator.set_menu(self.menu)
def main(self):
Gtk.main()
def start_activated (self, menu_item, filename):
if not self.has_started:
myfile = open(filename, 'a')
myfile.write('Start <%s>\n' % ctime(time()))
self.indicator.set_status(Indicator.IndicatorStatus.ATTENTION)
self.has_started = True
def stop_activated (self, menu_item, filename):
if self.has_started:
myfile = open(filename, 'a')
myfile.write('End <%s>\n' % ctime(time()))
self.indicator.set_status(Indicator.IndicatorStatus.ACTIVE)
self.has_started = False
if __name__ == '__main__':
indicator = MyIndicator(MY_FILENAME)
indicator.main()
Eu usei as informações fornecidas por gpaste-indicator e o Ubuntu Wiki .
Não tenho certeza sobre o recurso "manter o estado entre reinicializações", o mais simples seria ler a última linha de myfile.log
e definir self.has_started
de acordo.
Boa sorte.