Skip to content

Commit 9b57562

Browse files
committed
Use win32 timer to destroy notification window
A simple sleep doesn't allow win32gui to process any other notifications or callbacks. If we destroy the window using a win32 timer and process messages instead of sleeping, we can react to other user input (future work).
1 parent e535828 commit 9b57562

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

  • colcon_notification/desktop_notification

colcon_notification/desktop_notification/win32.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ def __init__(self, title, message, icon_path=None): # noqa: D107
4747
logger.debug(
4848
'Failed to import win32gui: {e}'.format_map(locals()))
4949
return
50+
try:
51+
import win32.timer
52+
except ImportError as e: # noqa: F841
53+
logger.debug(
54+
'Failed to import win32.timer: {e}'.format_map(locals()))
55+
return
5056

5157
wc, class_atom = NotificationWindow._create_window_class()
5258

@@ -82,14 +88,16 @@ def __init__(self, title, message, icon_path=None): # noqa: D107
8288
win32gui.NIM_MODIFY, (
8389
hwnd, 0, win32gui.NIF_INFO, win32con.WM_USER + 20, hicon,
8490
'Balloon tooltip', message, 200, title))
91+
# wait a while before destroying the window
92+
timer_id = win32.timer.set_timer(
93+
5000, lambda *_: win32gui.DestroyWindow(hwnd))
8594
except Exception as e: # noqa: F841
8695
logger.debug(
8796
'Failed to show the notification: {e}'.format_map(locals()))
88-
else:
89-
# wait a while before destroying the window
90-
time.sleep(5)
91-
finally:
9297
win32gui.DestroyWindow(hwnd)
98+
else:
99+
win32gui.PumpMessages()
100+
win32.timer.kill_timer(timer_id)
93101

94102
_wc = None
95103
_class_atom = None

0 commit comments

Comments
 (0)