EyeStrainReminder/eyestrainreminder/__main__.py

77 lines
1.6 KiB
Python
Executable File

#!/usr/bin/python3
import notify2
from time import sleep
from playsound import playsound
import os
from pystray import MenuItem as item
import pystray
from PIL import Image
NAME = 'Eye Strain Reminder'
MESSAGE = 'Friendly reminder to observe the 20-20-20 rule'
TIMEOUT = 20
MILLISECOND = 1000
MINUTE = 60
DIR = os.path.dirname(os.path.abspath(__file__))
icon = None
mute = False
disable_notifications = False
def toggle_mute():
global mute
mute = True if not mute else False
update_menu()
def generate_menu_items():
menu_items = []
if not mute:
menu_items.append(item('Mute', toggle_mute))
else:
menu_items.append(item('Unmute', toggle_mute))
return menu_items
def update_menu():
global icon
icon.menu = generate_menu_items()
icon.update_menu()
def init():
global icon
image = Image.open(f"{DIR}/files/eye_light.png")
icon = pystray.Icon(NAME, image, NAME, generate_menu_items())
icon.run()
print("Initializing EyeStrainReminder...")
notify2.init(NAME)
def uninit():
notify2.uninit()
def wait():
#sleep(TIMEOUT*MINUTE)
pass
def play_sound():
if not mute:
playsound(f"{DIR}/files/default.wav")
def send_notification():
print(MESSAGE)
notification = notify2.Notification(NAME, MESSAGE, "notification-device-eject")
notification.set_timeout(TIMEOUT * MILLISECOND)
notification.set_urgency(notify2.URGENCY_LOW)
notification.show()
play_sound()
sleep(TIMEOUT)
notification.close()
play_sound()
def main():
init()
while 1:
wait()
send_notification()
uninit()
if __name__ == '__main__':
main()