Skip to content

Commit e0ebd10

Browse files
Merge pull request #15 from KingWaffleIII/dev
v1.2.5
2 parents 8e0e69a + 34801cf commit e0ebd10

File tree

8 files changed

+87
-18
lines changed

8 files changed

+87
-18
lines changed

AntiProcrastinator.spec

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ main = Analysis(
44
["main.py"],
55
pathex=[],
66
binaries=[],
7-
datas=[("annoying.mp3", "."), ("icon.png", "."), ("icon2.png", ".")],
7+
datas=[("annoying.mp3", "."), ("icon.png", ".")],
88
hiddenimports=[],
99
hookspath=[],
1010
hooksconfig={},
@@ -32,13 +32,14 @@ main_exe = EXE(
3232
target_arch=None,
3333
codesign_identity=None,
3434
entitlements_file=None,
35+
icon="./icon.ico",
3536
)
3637

3738
configurator = Analysis(
3839
["configurator.py"],
3940
pathex=[],
40-
binaries=[("icon.png", "."), ("icon2.png", ".")],
41-
datas=[],
41+
binaries=[],
42+
datas=[("icon.png", ".")],
4243
hiddenimports=[],
4344
hookspath=[],
4445
hooksconfig={},
@@ -66,4 +67,5 @@ configurator_exe = EXE(
6667
target_arch=None,
6768
codesign_identity=None,
6869
entitlements_file=None,
70+
icon="./icon.ico",
6971
)

actions/notify.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from util import icon
1+
from util import functions
22

33
from .action import Action
44

@@ -19,10 +19,9 @@ def to_json(self):
1919
Converts the action to JSON.
2020
:return: JSON representation of the action.
2121
"""
22-
import util
2322

2423
if self.condition_func is not None:
25-
condition_func_str = util.functions.deconstruct_condition_function(
24+
condition_func_str = functions.deconstruct_condition_function(
2625
self.raw_condition_func
2726
)
2827
condition_func = {
@@ -38,7 +37,8 @@ async def execute(self):
3837
if not await super().execute():
3938
return False
4039

41-
import util
40+
text = functions.replace_wildcards(self.text)
4241

43-
text = util.functions.replace_wildcards(self.text)
44-
icon.notify(text)
42+
functions.show_notif(text)
43+
44+
return True

icon.ico

263 KB
Binary file not shown.

icon.png

1.8 MB
Loading

icon2.png

-1.82 MB
Binary file not shown.

main.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import multiprocessing.popen_spawn_win32 as forking
55
import os
66
import sys
7+
import threading
78
import time
89

910
import win32gui
@@ -62,16 +63,14 @@ async def startup():
6263
await OnStartupActionSet.execute()
6364

6465

65-
def procrastination():
66+
def procrastination(notif_conn):
67+
util.functions.set_notification_pipe(notif_conn)
6668
while True:
6769
asyncio.run(OnProcrastinationActionSet.execute())
6870

6971

70-
procrastination_proc: multiprocessing.Process | None = None
71-
72-
7372
async def watch(break_event):
74-
global procrastination_proc
73+
procrastination_proc: multiprocessing.Process | None = None
7574

7675
while True:
7776
if break_event.is_set():
@@ -90,7 +89,9 @@ async def watch(break_event):
9089
util.functions.start_timer()
9190
util.functions.set_window(window)
9291
procrastination_proc = Process(
93-
target=procrastination, name="procrastination"
92+
target=procrastination,
93+
args=(util.notif_send_conn,),
94+
name="procrastination",
9495
)
9596
procrastination_proc.start()
9697
else:
@@ -105,7 +106,8 @@ async def watch(break_event):
105106
time.sleep(1)
106107

107108

108-
def run_watchdog(break_event):
109+
def run_watchdog(break_event, notif_conn):
110+
util.functions.set_notification_pipe(notif_conn)
109111
while True:
110112
if not break_event.is_set():
111113
try:
@@ -121,9 +123,18 @@ def run_watchdog(break_event):
121123

122124
asyncio.run(startup())
123125

124-
watchdog = Process(target=run_watchdog, args=(util.break_event,), name="watchdog")
126+
watchdog = Process(
127+
target=run_watchdog,
128+
args=(util.break_event, util.notif_send_conn),
129+
name="watchdog",
130+
)
125131
watchdog.start()
126132

133+
util.functions.set_notification_pipe(util.notif_send_conn)
134+
135+
notifs = threading.Thread(target=util.notify_worker, daemon=True, name="notifs")
136+
notifs.start()
137+
127138
try:
128139
util.icon.run()
129140
finally:

util/__init__.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import sys
55
import threading
6+
import time
67

78
import pystray
89
from PIL import Image
@@ -51,9 +52,38 @@ def run_break():
5152
threading.Thread(target=run_break, daemon=True).start()
5253

5354

55+
notif_recv_conn, notif_send_conn = multiprocessing.Pipe()
56+
57+
58+
def notify_worker():
59+
while True:
60+
try:
61+
if notif_recv_conn.poll(0.1):
62+
notif = notif_recv_conn.recv()
63+
print(f"Got notification: {notif}")
64+
65+
# Try using pystray notification
66+
if hasattr(icon, "_thread") and getattr(icon, "visible", False):
67+
try:
68+
print("Trying to notify with pystray...")
69+
icon.notify(notif)
70+
print("Notification sent with pystray")
71+
continue # Skip to next notification if successful
72+
except Exception as e:
73+
print(f"Pystray notification failed: {e}")
74+
else:
75+
print("Icon not ready for notification")
76+
else:
77+
time.sleep(0.5)
78+
79+
except Exception as e:
80+
print(f"Notification worker error: {e}")
81+
time.sleep(1)
82+
83+
5484
icon = pystray.Icon(
5585
"AntiProcrastinator",
56-
Image.open(functions.eval_file_path(r"{runtime_dir}\icon2.png")),
86+
Image.open(functions.eval_file_path(r"{runtime_dir}\icon.png")),
5787
title="AntiProcrastinator",
5888
menu=pystray.Menu(
5989
pystray.MenuItem(

util/functions.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,29 @@ def deconstruct_condition_function(condition_func: str) -> tuple[str, bool, list
250250
if args == [""]:
251251
args = []
252252
return function, inverse, args
253+
254+
255+
_process_notif_conn = None
256+
257+
258+
def set_notification_pipe(conn):
259+
"""
260+
Set the notification pipe connection for this process.
261+
"""
262+
global _process_notif_conn
263+
_process_notif_conn = conn
264+
265+
266+
def show_notif(notif: str) -> None:
267+
"""
268+
Shows a notification via the tray icon.
269+
:param notif: the notification to show.
270+
"""
271+
# Use the process-specific connection if available
272+
if _process_notif_conn is not None:
273+
try:
274+
_process_notif_conn.send(notif)
275+
print(f"Process {os.getpid()} sent notification: {notif}")
276+
return
277+
except Exception as e:
278+
print(f"Failed to send notification via process pipe: {e}")

0 commit comments

Comments
 (0)