Skip to content

Commit 55aee12

Browse files
committed
Add timer for switching theme
1 parent 556344f commit 55aee12

File tree

2 files changed

+53
-22
lines changed

2 files changed

+53
-22
lines changed

yin_yang/__main__.py

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
#!/bin/env python3
22

3-
import sys
43
import logging
4+
import sys
55
from argparse import ArgumentParser
66
from logging.handlers import RotatingFileHandler
77
from pathlib import Path
88

99
from PySide6 import QtWidgets
10-
from PySide6.QtCore import QTranslator, QLibraryInfo, QLocale, QObject
10+
from PySide6.QtCore import QLibraryInfo, QLocale, QTranslator
1111
from PySide6.QtGui import QIcon
12-
from PySide6.QtWidgets import QSystemTrayIcon, QMenu
12+
from PySide6.QtWidgets import QMenu, QSystemTrayIcon
1313
from systemd import journal
1414

15-
from yin_yang.notification_handler import NotificationHandler
16-
from yin_yang import daemon_handler
15+
from yin_yang import daemon_handler, theme_switcher
16+
from yin_yang.config import Modes, config
17+
from yin_yang.helpers import is_flatpak
1718
from yin_yang.meta import ConfigEvent
18-
from yin_yang import theme_switcher
19-
from yin_yang.config import config, Modes
19+
from yin_yang.notification_handler import NotificationHandler
20+
from yin_yang.scheduler import Scheduler
2021
from yin_yang.ui import main_window_connector
2122

2223
logger = logging.getLogger()
@@ -36,7 +37,7 @@ def setup_logger(use_systemd_journal: bool):
3637
# noinspection SpellCheckingInspection
3738
logging.basicConfig(
3839
level=logging.DEBUG,
39-
format='%(asctime)s %(levelname)s - %(name)s: %(message)s'
40+
format='%(asctime)s %(levelname)s - %(name)s: %(message)s',
4041
)
4142
else:
4243
# if you run it with "python -O __main__.py" instead, debug is false
@@ -45,12 +46,13 @@ def setup_logger(use_systemd_journal: bool):
4546
# noinspection SpellCheckingInspection
4647
logging.basicConfig(
4748
level=logging.WARNING,
48-
format='%(asctime)s %(levelname)s - %(name)s: %(message)s'
49+
format='%(asctime)s %(levelname)s - %(name)s: %(message)s',
4950
)
5051
# and add a handler that limits the size to 1 GB
5152
file_handler = RotatingFileHandler(
5253
str(Path.home()) + '/.local/share/yin_yang.log',
53-
maxBytes=10**9, backupCount=1
54+
maxBytes=10**9,
55+
backupCount=1,
5456
)
5557
logging.root.addHandler(file_handler)
5658

@@ -65,11 +67,15 @@ def systray_icon_clicked(reason: QSystemTrayIcon.ActivationReason):
6567

6668
# using ArgumentParser for parsing arguments
6769
parser = ArgumentParser()
68-
parser.add_argument('-t', '--toggle',
69-
help='toggles Yin-Yang',
70-
action='store_true')
71-
parser.add_argument('--systemd', help='uses systemd journal handler and applies desired theme', action='store_true')
72-
parser.add_argument('--minimized', help='starts the program to tray bar', action='store_true')
70+
parser.add_argument('-t', '--toggle', help='toggles Yin-Yang', action='store_true')
71+
parser.add_argument(
72+
'--systemd',
73+
help='uses systemd journal handler and applies desired theme',
74+
action='store_true',
75+
)
76+
parser.add_argument(
77+
'--minimized', help='starts the program to tray bar', action='store_true'
78+
)
7379
arguments = parser.parse_args()
7480
setup_logger(arguments.systemd)
7581

@@ -126,20 +132,34 @@ def systray_icon_clicked(reason: QSystemTrayIcon.ActivationReason):
126132

127133
menu = QMenu('Yin & Yang')
128134
menu.addAction(
129-
app.translate('systray', 'Open Yin Yang', 'Context menu action in the systray'),
130-
lambda: window.show())
135+
app.translate(
136+
'systray', 'Open Yin Yang', 'Context menu action in the systray'
137+
),
138+
lambda: window.show(),
139+
)
131140
menu.addAction(
132-
app.translate('systray', 'Toggle theme', 'Context menu action in the systray'),
133-
lambda: theme_switcher.set_mode(not config.dark_mode))
134-
menu.addAction(QIcon.fromTheme('application-exit'),
135-
app.translate('systray', 'Quit', 'Context menu action in the systray'),
136-
app.quit)
141+
app.translate(
142+
'systray', 'Toggle theme', 'Context menu action in the systray'
143+
),
144+
lambda: theme_switcher.set_mode(not config.dark_mode),
145+
)
146+
menu.addAction(
147+
QIcon.fromTheme('application-exit'),
148+
app.translate('systray', 'Quit', 'Context menu action in the systray'),
149+
app.quit,
150+
)
137151

138152
icon.setContextMenu(menu)
139153
icon.show()
140154
else:
141155
logger.debug('System tray is unsupported')
142156

157+
# Start the timer to handle automatic theme switching. We only need this
158+
# currently if running in a flatpak as systemd units are not supported.
159+
if is_flatpak:
160+
timer = Scheduler(45, theme_switcher.set_desired_theme)
161+
timer.start()
162+
143163
if arguments.minimized:
144164
sys.exit(app.exec())
145165
else:

yin_yang/scheduler.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import logging
2+
from threading import Timer
3+
4+
logger = logging.getLogger(__name__)
5+
6+
7+
class Scheduler(Timer):
8+
def run(self):
9+
while not self.finished.wait(self.interval):
10+
logger.debug("Timer expired, checking theme.")
11+
self.function(*self.args, **self.kwargs)

0 commit comments

Comments
 (0)