Implement support for a systray

This commit is contained in:
Emma Nora Theuer 2024-09-01 19:29:04 +02:00
parent fa13c7f135
commit 138053d8d1

View file

@ -4,11 +4,10 @@ from os import chdir, getenv, system
import logging import logging
import tomllib import tomllib
from datetime import datetime, time from datetime import datetime, time
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.cron import CronTrigger from apscheduler.triggers.cron import CronTrigger
# setup logging # setup logging
logger = logging.getLogger(__name__) logger = logging.getLogger("wallman")
class ConfigError(Exception): class ConfigError(Exception):
pass pass
@ -36,9 +35,24 @@ class _ConfigLib:
except KeyError: except KeyError:
self.config_notify = False self.config_notify = False
logger.warning("'notify' is not set in dictionary general in the config file, defaulting to 'false'.") logger.warning("'notify' is not set in dictionary general in the config file, defaulting to 'false'.")
try:
self.config_systray = self.config_general["systray"]
except KeyError:
self.config_systray = True
logger.warning("'systray' is not set in the dictionary general in the config file, defaulting to 'true'.")
# Setup logging # Setup logging
self._set_log_level() self._set_log_level()
if self.config_systray:
self._initialize_systray()
def _initialize_systray(self):
try:
import wallman_systray
except (ImportError, FileNotFoundError):
self.config_systray = False
def _set_log_level(self): def _set_log_level(self):
global logging global logging
@ -212,11 +226,38 @@ class WallpaperLogic(_ConfigLib):
self._notify_user() self._notify_user()
return has_wallpaper_been_set return has_wallpaper_been_set
def schedule_wallpapers(self): def run_wallpapers(self):
def _schedule_background_wallpapers():
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
# Create a scheduled job for every changing time
for changing_time in range(len(self.config_changing_times)):
clean_time = self._clean_times(changing_time)
scheduler.add_job(self.set_wallpaper_by_time, trigger=CronTrigger(hour=clean_time[0], minute=clean_time[1], second=clean_time[2]))
scheduler.start()
logger.info("The background scheduler has been started.")
return scheduler
def _schedule_blocking_wallpapers():
from apscheduler.schedulers.blocking import BlockingScheduler
scheduler = BlockingScheduler() scheduler = BlockingScheduler()
# Create a scheduled job for every changing time # Create a scheduled job for every changing time
for changing_time in range(len(self.config_changing_times)): for changing_time in range(len(self.config_changing_times)):
clean_time = self._clean_times(changing_time) clean_time = self._clean_times(changing_time)
scheduler.add_job(self.set_wallpaper_by_time, trigger=CronTrigger(hour=clean_time[0], minute=clean_time[1], second=clean_time[2])) scheduler.add_job(self.set_wallpaper_by_time, trigger=CronTrigger(hour=clean_time[0], minute=clean_time[1], second=clean_time[2]))
scheduler.start() scheduler.start()
logger.info("The scheduler has been started.") logger.info("The blocking scheduler has been started.")
if self.config_systray:
import wallman_systray as systray
from functools import partial
scheduler = _schedule_background_wallpapers()
menu = systray.Menu (
systray.item("Re-Set Wallpaper", partial(systray.set_wallpaper_again, callback=self.set_wallpaper_by_time)),
systray.item("Reroll Wallpapers", partial(systray.reroll_wallpapers, first_callback=self._choose_wallpaper_set, second_callback=self.set_wallpaper_by_time)),
systray.item("Quit", partial(systray.on_quit, callback=scheduler.shutdown))
)
icon =systray.Icon("wallman_icon", systray.icon_image, "My Tray Icon", menu)
icon.run()
else:
_schedule_blocking_wallpapers()