Skip to content

Commit c3e87ec

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 c3e87ec

File tree

1 file changed

+12
-5
lines changed
  • colcon_notification/desktop_notification

1 file changed

+12
-5
lines changed

colcon_notification/desktop_notification/win32.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Licensed under the Apache License, Version 2.0
33

44
import sys
5-
import time
65

76
from colcon_core.logging import colcon_logger
87
from colcon_core.plugin_system import satisfies_version
@@ -47,6 +46,12 @@ def __init__(self, title, message, icon_path=None): # noqa: D107
4746
logger.debug(
4847
'Failed to import win32gui: {e}'.format_map(locals()))
4948
return
49+
try:
50+
import win32.timer
51+
except ImportError as e: # noqa: F841
52+
logger.debug(
53+
'Failed to import win32.timer: {e}'.format_map(locals()))
54+
return
5055

5156
wc, class_atom = NotificationWindow._create_window_class()
5257

@@ -82,14 +87,16 @@ def __init__(self, title, message, icon_path=None): # noqa: D107
8287
win32gui.NIM_MODIFY, (
8388
hwnd, 0, win32gui.NIF_INFO, win32con.WM_USER + 20, hicon,
8489
'Balloon tooltip', message, 200, title))
90+
# wait a while before destroying the window
91+
timer_id = win32.timer.set_timer(
92+
5000, lambda *_: win32gui.DestroyWindow(hwnd))
8593
except Exception as e: # noqa: F841
8694
logger.debug(
8795
'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:
9296
win32gui.DestroyWindow(hwnd)
97+
else:
98+
win32gui.PumpMessages()
99+
win32.timer.kill_timer(timer_id)
93100

94101
_wc = None
95102
_class_atom = None

0 commit comments

Comments
 (0)