diff --git a/LunaTranslator/LunaTranslator/darkdetect.py b/LunaTranslator/LunaTranslator/darkdetect.py deleted file mode 100644 index e7c9ffcf..00000000 --- a/LunaTranslator/LunaTranslator/darkdetect.py +++ /dev/null @@ -1,88 +0,0 @@ -from winreg import ( - HKEY_CURRENT_USER as hkey, - QueryValueEx as getSubkeyValue, - OpenKey as getKey, -) - -import ctypes -import ctypes.wintypes - -advapi32 = ctypes.windll.advapi32 - -# LSTATUS RegOpenKeyExA( -# HKEY hKey, -# LPCSTR lpSubKey, -# DWORD ulOptions, -# REGSAM samDesired, -# PHKEY phkResult -# ); -advapi32.RegOpenKeyExA.argtypes = ( - ctypes.wintypes.HKEY, - ctypes.wintypes.LPCSTR, - ctypes.wintypes.DWORD, - ctypes.wintypes.DWORD, - ctypes.POINTER(ctypes.wintypes.HKEY), -) -advapi32.RegOpenKeyExA.restype = ctypes.wintypes.LONG - -# LSTATUS RegQueryValueExA( -# HKEY hKey, -# LPCSTR lpValueName, -# LPDWORD lpReserved, -# LPDWORD lpType, -# LPBYTE lpData, -# LPDWORD lpcbData -# ); -advapi32.RegQueryValueExA.argtypes = ( - ctypes.wintypes.HKEY, - ctypes.wintypes.LPCSTR, - ctypes.wintypes.LPDWORD, - ctypes.wintypes.LPDWORD, - ctypes.wintypes.LPBYTE, - ctypes.wintypes.LPDWORD, -) -advapi32.RegQueryValueExA.restype = ctypes.wintypes.LONG - -# LSTATUS RegNotifyChangeKeyValue( -# HKEY hKey, -# WINBOOL bWatchSubtree, -# DWORD dwNotifyFilter, -# HANDLE hEvent, -# WINBOOL fAsynchronous -# ); -advapi32.RegNotifyChangeKeyValue.argtypes = ( - ctypes.wintypes.HKEY, - ctypes.wintypes.BOOL, - ctypes.wintypes.DWORD, - ctypes.wintypes.HANDLE, - ctypes.wintypes.BOOL, -) -advapi32.RegNotifyChangeKeyValue.restype = ctypes.wintypes.LONG - - -def theme(): - """Uses the Windows Registry to detect if the user is using Dark Mode""" - # Registry will return 0 if Windows is in Dark Mode and 1 if Windows is in Light Mode. This dictionary converts that output into the text that the program is expecting. - valueMeaning = {0: "Dark", 1: "Light"} - # In HKEY_CURRENT_USER, get the Personalisation Key. - try: - key = getKey( - hkey, "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize" - ) - # In the Personalisation Key, get the AppsUseLightTheme subkey. This returns a tuple. - # The first item in the tuple is the result we want (0 or 1 indicating Dark Mode or Light Mode); the other value is the type of subkey e.g. DWORD, QWORD, String, etc. - subkey = getSubkeyValue(key, "AppsUseLightTheme")[0] - except FileNotFoundError: - # some headless Windows instances (e.g. GitHub Actions or Docker images) do not have this key - return None - return valueMeaning[subkey] - - -def isDark(): - if theme() is not None: - return theme() == "Dark" - - -def isLight(): - if theme() is not None: - return theme() == "Light" diff --git a/LunaTranslator/LunaTranslator/gui/settin.py b/LunaTranslator/LunaTranslator/gui/settin.py index c979a058..6dc26fb9 100644 --- a/LunaTranslator/LunaTranslator/gui/settin.py +++ b/LunaTranslator/LunaTranslator/gui/settin.py @@ -17,6 +17,7 @@ from PyQt5.QtWidgets import QTabWidget import qtawesome, darkdetect, gobject import functools, threading, windows, os, winsharedutils from traceback import print_exc +from winsharedutils import isDark from myutils.config import globalconfig, _TR from myutils.utils import wavmp3player from myutils.config import static_data @@ -253,9 +254,7 @@ class Settin(closeashidewindow): elif dl == 1: dark = True elif dl == 2: - dark = darkdetect.isDark() - if dark is None: - dark = False + dark = isDark() darklight = ["light", "dark"][dark] class WindowEventFilter(QObject): diff --git a/LunaTranslator/LunaTranslator/winsharedutils.py b/LunaTranslator/LunaTranslator/winsharedutils.py index bb925697..946f88d1 100644 --- a/LunaTranslator/LunaTranslator/winsharedutils.py +++ b/LunaTranslator/LunaTranslator/winsharedutils.py @@ -344,3 +344,6 @@ getpidhwndfirst.restype = HWND Is64bit = utilsdll.Is64bit Is64bit.argtypes = (DWORD,) Is64bit.restype = c_bool + +isDark = utilsdll.isDark +isDark.restype = c_bool diff --git a/plugins/winsharedutils/theme.cpp b/plugins/winsharedutils/theme.cpp index 1d2973ec..bfa01254 100644 --- a/plugins/winsharedutils/theme.cpp +++ b/plugins/winsharedutils/theme.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "define.h" // https://github.com/Blinue/Xaml-Islands-Cpp/blob/main/src/XamlIslandsCpp/XamlWindow.h @@ -169,5 +170,23 @@ bool _SetTheme( DWM_SYSTEMBACKDROP_TYPE value = BACKDROP_MAP[(int)backdrop]; DwmSetWindowAttribute(_hWnd, DWMWA_SYSTEMBACKDROP_TYPE, &value, sizeof(value)); + return false; +} + +DECLARE bool isDark() +{ + HKEY hKey; + const char *subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; + if (RegOpenKeyExA(HKEY_CURRENT_USER, subKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS) + { + DWORD value; + DWORD dataSize = sizeof(DWORD); + if (RegQueryValueExA(hKey, "AppsUseLightTheme", 0, NULL, (LPBYTE)&value, &dataSize) == ERROR_SUCCESS) + { + RegCloseKey(hKey); + return 1 - value; + } + RegCloseKey(hKey); + } return false; } \ No newline at end of file