Rework examples with a class that encapsulate all generic monitor stuff

This commit is contained in:
2020-05-07 08:42:14 +02:00
parent 3b7247f97e
commit 33e74eccd8
4 changed files with 272 additions and 209 deletions

View File

@@ -25,27 +25,21 @@ Display two timers with interactive behaviour :
* Right click : switch timer
'''
import json
import time
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, GLib, Gst
from threading import Thread
from signal import signal, SIGINT
import sys
from dbusitem import DBUSItem
from genericmonitor import GenericMonitor, GenericMonitorGroup, GenericMonitorItem
class TimerThread(Thread):
class TimerThread(Thread,GenericMonitor):
def stop(self):
self._stopLoop = True
self.stopMainLoop()
def _displayTimerValue(self):
encoder = json.JSONEncoder()
res = {'group':'Timer', 'items':[]}
item = DBUSItem('timer', onClick='signal', box='right')
item = GenericMonitorItem('timer', onClick='signal', box='right')
group = GenericMonitorGroup('Timer', item)
curValue = self.timers[self.curTimer]
item.text = '%02d:%02d' % (int(curValue/60)%60, curValue%60)
if curValue >= (60*60):
@@ -57,13 +51,10 @@ class TimerThread(Thread):
else:
style = 'color:#215D9C'
item.style = style
res['items'] += item.toMap()
try:
systray_proxy.notify(encoder.encode(res), dbus_interface='com.soutade.GenericMonitor')
except:
pass
self.notify(group)
def run(self):
self.setupMonitor()
self.timers = [0, 0]
self.curTimer = 0
self.timerPaused = False
@@ -98,48 +89,27 @@ class TimerThread(Thread):
self.timerPaused = True
self._displayTimerValue()
def onDblRightClick(self, sender):
pass
def onActivate(self):
super().onActivate()
self.timerPaused = self._lastState
self._displayTimerValue()
def onClick(sender):
timerThread.onClick(sender)
def onRightClick(sender):
timerThread.onRightClick(sender)
def onDblClick(sender):
timerThread.onDblClick(sender)
def onDblRightClick(sender):
timerThread.onDblRightClick(sender)
def onDeactivate(self):
super().onDeactivate()
self._lastState = self.timerPaused
if not self.timerPaused and self.curTimer == 0:
self.timerPaused = True
def signalHandler(signal_received, frame):
timerThread.stop()
mainLoop.quit()
timerThread.join()
encoder = json.JSONEncoder()
res = {'groups':['Timer']}
try:
systray_proxy.deleteGroups(encoder.encode(res), dbus_interface='com.soutade.GenericMonitor')
except Exception as e:
pass
groups = {'groups':['Timer']}
timerThread.deleteGroups(groups)
sys.exit(0)
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
systray_proxy = bus.get_object('org.gnome.Shell', '/com/soutade/GenericMonitor')
bus.add_signal_receiver(onClick, 'onClick', 'com.soutade.GenericMonitor')
bus.add_signal_receiver(onDblClick, 'onDblClick', 'com.soutade.GenericMonitor')
bus.add_signal_receiver(onRightClick, 'onRightClick', 'com.soutade.GenericMonitor')
bus.add_signal_receiver(onDblRightClick, 'onDblRightClick', 'com.soutade.GenericMonitor')
timerThread = TimerThread()
timerThread.start()
signal(SIGINT, signalHandler)
mainLoop = GLib.MainLoop()
mainLoop.run()
timerThread.runMainLoop()