Make sure Windows shutdown is handled when minimized too

This commit is contained in:
DevilXD committed 2022-09-03 18:50:36 +02:00
1 parent 5068b5429c
commit c12a691a54
1 file changed
+25 -12
+25 -12
View File
@@ -1,5 +1,7 @@
from __future__ import annotations from __future__ import annotations
import sys
import ctypes
import asyncio import asyncio
import logging import logging
import webbrowser import webbrowser
@@ -1727,18 +1729,29 @@ class GUIManager:
self._root.after_idle(self.tray.minimize) self._root.after_idle(self.tray.minimize)
else: else:
self._root.deiconify() self._root.deiconify()
# NOTE: this root.update() is required for the below to work - don't remove if sys.platform == "win32":
root.update() # gracefully handle Windows shutdown closing the application
self._message_map = { # NOTE: this root.update() is required for the below to work - don't remove
# window close request root.update()
win32con.WM_CLOSE: self.close, self._message_map = {
# shutdown request # window close request
win32con.WM_QUERYENDSESSION: self.close, win32con.WM_CLOSE: self.close,
} # shutdown request
# This hooks up the wnd_proc function as the message processor for the root window. win32con.WM_QUERYENDSESSION: self.close,
self.old_wnd_proc = win32gui.SetWindowLong( }
self._handle, win32con.GWL_WNDPROC, self.wnd_proc # This hooks up the wnd_proc function as the message processor for the root window.
) self.old_wnd_proc = win32gui.SetWindowLong(
self._handle, win32con.GWL_WNDPROC, self.wnd_proc
)
# This ensures all of this works when the application is withdrawn or iconified
ctypes.windll.user32.ShutdownBlockReasonCreate(
self._handle, ctypes.c_wchar_p(_("gui", "status", "exiting"))
)
# DEV NOTE: use this to remove the reason in the future
# ctypes.windll.user32.ShutdownBlockReasonDestroy(self._handle)
else:
# use old-style window closing protocol for non-windows platforms
root.protocol("WM_DESTROY_WINDOW", self.close)
# https://stackoverflow.com/questions/56329342/tkinter-treeview-background-tag-not-working # https://stackoverflow.com/questions/56329342/tkinter-treeview-background-tag-not-working
def _fixed_map(self, option): def _fixed_map(self, option):