GtkTrayUI.py 3.11 KB
Newer Older
1
import sys, gi
2 3
gi.require_version('Gtk', '3.0')
from gi.repository import GObject, Gtk
4

5
class UI(GObject.GObject):
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  __gsignals__ = {
    'command' : (GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE, (GObject.TYPE_STRING,))
  }

  def __init__(self,args, continuous):
    GObject.GObject.__init__(self)
    self.continuous = continuous

    self.statusicon = Gtk.StatusIcon()
    self.statusicon.set_title("Blather")
    self.statusicon.set_name("Blather")
    self.statusicon.set_tooltip_text("Blather - Idle")
    self.statusicon.set_has_tooltip(True)
    self.statusicon.connect("activate", self.continuous_toggle)
    self.statusicon.connect("popup-menu", self.popup_menu)

    self.menu = Gtk.Menu()
    self.menu_listen = Gtk.MenuItem('Listen')
    self.menu_continuous = Gtk.CheckMenuItem('Continuous')
    self.menu_quit = Gtk.MenuItem('Quit')
    self.menu.append(self.menu_listen)
    self.menu.append(self.menu_continuous)
    self.menu.append(self.menu_quit)
    self.menu_listen.connect("activate", self.toggle_listen)
    self.menu_continuous.connect("toggled", self.toggle_continuous)
    self.menu_quit.connect("activate", self.quit)
    self.menu.show_all()

  def continuous_toggle(self, item):
    checked = self.menu_continuous.get_active()
    self.menu_continuous.set_active(not checked)

  def toggle_continuous(self, item):
    checked = self.menu_continuous.get_active()
    self.menu_listen.set_sensitive(not checked)
    if checked:
      self.menu_listen.set_label("Listen")
      self.emit('command', "continuous_listen")
      self.statusicon.set_tooltip_text("Blather - Listening")
      self.set_icon_active()
    else:
      self.set_icon_inactive()
      self.statusicon.set_tooltip_text("Blather - Idle")
      self.emit('command', "continuous_stop")

  def toggle_listen(self, item):
    val = self.menu_listen.get_label()
    if val == "Listen":
      self.emit("command", "listen")
      self.menu_listen.set_label("Stop")
      self.statusicon.set_tooltip_text("Blather - Listening")
      self.set_icon_active()
    else:
      self.icon_inactive()
      self.menu_listen.set_label("Listen")
      self.emit("command", "stop")
      self.statusicon.set_tooltip_text("Blather - Idle")

  def popup_menu(self, item, button, time):
    self.menu.popup(None, None, None, None, button, time)

  def run(self):
    #set the icon
    self.set_icon_inactive()
    if self.continuous:
      self.menu_continuous.set_active(True)
      self.set_icon_active()
    else:
      self.menu_continuous.set_active(False)
    self.statusicon.set_visible(True)

  def quit(self, item):
    self.statusicon.set_visible(False)
    self.emit("command", "quit")

  def finished(self, text):
    if not self.menu_continuous.get_active():
      self.menu_listen.set_label("Listen")
      self.set_icon_inactive()
      self.statusicon.set_tooltip_text("Blather - Idle")

  def set_icon_active_asset(self, i):
    self.icon_active = i

  def set_icon_inactive_asset(self, i):
    self.icon_inactive = i

  def set_icon_active(self):
    self.statusicon.set_from_file( self.icon_active )

  def set_icon_inactive(self):
    self.statusicon.set_from_file( self.icon_inactive )
98