1010- `120:` starts a 2 hours timer
1111"""
1212
13- import subprocess
1413import threading
1514from datetime import timedelta
1615from pathlib import Path
17- from sys import platform
1816from time import strftime , time , localtime
1917
2018from albert import *
2119
2220md_iid = '2.0'
23- md_version = "1.6 "
21+ md_version = "1.7 "
2422md_name = "Timer"
2523md_description = "Set up timers"
2624md_license = "BSD-2"
2725md_url = "https://github.com/albertlauncher/python/tree/master/timer"
2826md_maintainers = ["@manuelschneid3r" , "@googol42" , "@uztnus" ]
2927
28+
3029class Timer (threading .Timer ):
3130
3231 def __init__ (self , interval , name , callback ):
@@ -51,6 +50,7 @@ def __init__(self):
5150 self .iconUrls = [f"file:{ Path (__file__ ).parent } /time.svg" ]
5251 self .soundPath = Path (__file__ ).parent / "bing.wav"
5352 self .timers = []
53+ self .notification = None
5454
5555 def finalize (self ):
5656 for timer in self .timers :
@@ -65,15 +65,10 @@ def deleteTimer(self, timer):
6565 timer .cancel ()
6666
6767 def onTimerTimeout (self , timer ):
68- title = 'Timer "%s"' % timer .name if timer .name else 'Timer'
69- text = "Timed out at %s" % strftime ("%X" , localtime (timer .end ))
70- sendTrayNotification (title , text )
71-
72- if platform == "linux" :
73- subprocess .Popen (["aplay" , self .soundPath ])
74- elif platform == "darwin" :
75- subprocess .Popen (["afplay" , self .soundPath ])
76-
68+ self .notification = Notification (
69+ title = f"Timer '{ timer .name if timer .name else 'Timer' } '" ,
70+ subtitle = f"Timed out at { strftime ('%X' , localtime (timer .end ))} "
71+ )
7772 self .deleteTimer (timer )
7873
7974 def handleTriggerQuery (self , query ):
@@ -86,7 +81,7 @@ def handleTriggerQuery(self, query):
8681 name = args [1 ] if 1 < len (args ) else ''
8782 if not all (field .isdigit () or field == '' for field in fields ):
8883 return StandardItem (
89- id = self .name () ,
84+ id = self .name ,
9085 text = "Invalid input" ,
9186 subtext = "Enter a query in the form of '%s[[hours:]minutes:]seconds [name]'" % self .defaultTrigger (),
9287 iconUrls = self .iconUrls ,
@@ -98,7 +93,7 @@ def handleTriggerQuery(self, query):
9893 seconds += int (fields [i ] if fields [i ] else 0 )* (60 ** i )
9994
10095 query .add (StandardItem (
101- id = self .name () ,
96+ id = self .name ,
10297 text = str (timedelta (seconds = seconds )),
10398 subtext = 'Set a timer with name "%s"' % name if name else 'Set a timer' ,
10499 iconUrls = self .iconUrls ,
@@ -115,11 +110,11 @@ def handleTriggerQuery(self, query):
115110
116111 timer_name_with_quotes = '"%s"' % timer .name if timer .name else ''
117112 items .append (StandardItem (
118- id = self .name () ,
113+ id = self .name ,
119114 text = 'Delete timer %s [%s]' % (timer_name_with_quotes , identifier ),
120115 subtext = "Times out %s" % strftime ("%X" , localtime (timer .end )),
121116 iconUrls = self .iconUrls ,
122- actions = [Action ("delete-timer" , "Delete timer" , lambda timer = timer : self .deleteTimer (timer ))]
117+ actions = [Action ("delete-timer" , "Delete timer" , lambda t = timer : self .deleteTimer (t ))]
123118 ))
124119
125120 if items :
0 commit comments