EyeStrainReminder/eyestrainreminder/__main__.py

87 lines
1.9 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'
MESSAGE_MISSED = 'Missed notification(s), resuming countdown'
TIMEOUT = 20
MILLISECOND = 1000
MINUTE = 60
DIR = os.path.dirname(os.path.abspath(__file__))
icon = None
mute = False
disable_notifications = False
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 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 toggle_mute():
global mute
mute = True if not mute else False
update_menu()
def update_menu():
global icon
icon.menu = generate_menu_items()
icon.update_menu()
def uninit():
notify2.uninit()
def wait():
sleep(TIMEOUT*MINUTE)
#pass
def send_notification():
if not disable_notifications:
notify(MESSAGE)
else:
print("Notifications are disabled, waiting for an enable...")
while disable_notifications:
sleep(10)
notify(MESSAGE_MISSED)
def notify(message):
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 play_sound():
if not mute:
playsound(f"{DIR}/files/default.wav")
def main():
init()
while 1:
wait()
send_notification()
uninit()
if __name__ == '__main__':
main()