Start and end sound support

This commit is contained in:
Abhorrent_Anger 2021-01-15 23:20:48 +02:00 committed by Mārtiņš Kurmis
parent d9e5cdc6c7
commit fd1f560f74
3 changed files with 40 additions and 13 deletions

View File

@ -18,63 +18,76 @@ MILLISECOND = 1000
MINUTE = 60 MINUTE = 60
DIR = os.path.dirname(os.path.abspath(__file__)) DIR = os.path.dirname(os.path.abspath(__file__))
icon = mute = disable_notifs = invert_icons = notify_thread = sound = None icon = mute = disable_notifs = invert_icons = notify_thread = start_sound = end_sound = None
def init(): def init():
print("Initializing EyeStrainReminder...") print("Initializing EyeStrainReminder...")
notify2.init(NAME) notify2.init(NAME)
def create_tray_icon(): def create_tray_icon():
global icon global icon
image = get_image() image = get_image()
icon = pystray.Icon(NAME, image, NAME, generate_menu_items()) icon = pystray.Icon(NAME, image, NAME, generate_menu_items())
icon.run() icon.run()
def update_icon(): def update_icon():
global icon global icon
image = get_image() image = get_image()
icon.icon = image icon.icon = image
def get_image(): def get_image():
theme = [[DIR + "/files/eye_light.png", DIR + "/files/eye_dark.png"], theme = [[DIR + "/files/eye_light.png", DIR + "/files/eye_dark.png"],
[DIR + "/files/eye_dark.png", DIR + "/files/eye_light.png"]] [DIR + "/files/eye_dark.png", DIR + "/files/eye_light.png"]]
return Image.open(theme[invert_icons][disable_notifs]) return Image.open(theme[invert_icons][disable_notifs])
def generate_menu_items(): def generate_menu_items():
menu_items = [] menu_items = []
menu_items.append(item('Mute' if not mute else 'Unmute', toggle_mute)) menu_items.append(item('Mute' if not mute else 'Unmute', toggle_mute))
menu_items.append(item('Do Not Disturb' if not disable_notifs else 'Enable Notifications', toggle_notifications)) menu_items.append(item(
'Do Not Disturb' if not disable_notifs else 'Enable Notifications', toggle_notifications))
menu_items.append(menu.SEPARATOR) menu_items.append(menu.SEPARATOR)
menu_items.append(item('Exit', exit)) menu_items.append(item('Exit', exit))
return menu_items return menu_items
def toggle_mute(): def toggle_mute():
global mute global mute
mute = not mute mute = not mute
update_menu() update_menu()
def toggle_notifications(): def toggle_notifications():
global disable_notifs global disable_notifs
disable_notifs = not disable_notifs disable_notifs = not disable_notifs
update_menu() update_menu()
update_icon() update_icon()
def exit(): def exit():
global icon global icon
icon.stop() icon.stop()
os._exit(0) os._exit(0)
def update_menu(): def update_menu():
global icon global icon
icon.menu = generate_menu_items() icon.menu = generate_menu_items()
icon.update_menu() icon.update_menu()
def uninit(): def uninit():
notify2.uninit() notify2.uninit()
def wait(): def wait():
sleep(TIMEOUT*MINUTE) sleep(TIMEOUT*MINUTE)
def send_notification(): def send_notification():
if not disable_notifs: if not disable_notifs:
notify(MESSAGE) notify(MESSAGE)
@ -84,30 +97,41 @@ def send_notification():
sleep(10) sleep(10)
notify(MESSAGE_MISSED) notify(MESSAGE_MISSED)
def notify(message): def notify(message):
print(message) print(message)
notification = notify2.Notification(NAME, message, "notification-device-eject") notification = notify2.Notification(
NAME, message, "notification-device-eject")
notification.set_timeout(TIMEOUT * MILLISECOND) notification.set_timeout(TIMEOUT * MILLISECOND)
notification.set_urgency(notify2.URGENCY_LOW) notification.set_urgency(notify2.URGENCY_LOW)
notification.show() notification.show()
play_sound() play_sound(start_sound)
sleep(TIMEOUT) sleep(TIMEOUT)
notification.close() notification.close()
play_sound() play_sound(end_sound)
def play_sound():
def play_sound(sound):
if not mute: if not mute:
playsound(sound) playsound(sound)
def parse_args(): def parse_args():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-i", "--invert_icons", action="store_true", default=False, help="Default to the dark icon for light themes") parser.add_argument("-i", "--invert_icons", action="store_true",
parser.add_argument("-m", "--mute", action="store_true", default=False, help="Mute notification sounds by default") default=False, help="Default to the dark icon for light themes")
parser.add_argument("-n", "--disable_notifs", action="store_true", default=False, help="Disable notifications by default") parser.add_argument("-m", "--mute", action="store_true",
parser.add_argument("-s", "--sound", action="store", default=DIR + "/files/default.wav", help="Custom notification sound") default=False, help="Mute notification sounds by default")
parser.add_argument("-n", "--disable_notifs", action="store_true",
default=False, help="Disable notifications by default")
parser.add_argument("-ss", "--start_sound", action="store", default=DIR +
"/files/ptt-s.wav", help="Custom start notification sound")
parser.add_argument("-es", "--end_sound", action="store", default=DIR +
"/files/ptt-e.wav", help="Custom end notification sound")
args = parser.parse_args() args = parser.parse_args()
globals().update(args.__dict__) globals().update(args.__dict__)
def notification_loop(): def notification_loop():
init() init()
while 1: while 1:
@ -115,12 +139,15 @@ def notification_loop():
send_notification() send_notification()
uninit() uninit()
def main(): def main():
parse_args() parse_args()
notify_thread = threading.Thread(target=notification_loop, name="Notify Thread") notify_thread = threading.Thread(
target=notification_loop, name="Notify Thread")
notify_thread.start() notify_thread.start()
create_tray_icon() create_tray_icon()
notify_thread.join() notify_thread.join()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Binary file not shown.

Binary file not shown.