This commit is contained in:
恍兮惚兮 2024-04-16 20:16:24 +08:00
parent 21aa04382e
commit d9c80e5761
4 changed files with 24 additions and 91 deletions

View File

@ -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"

View File

@ -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):

View File

@ -344,3 +344,6 @@ getpidhwndfirst.restype = HWND
Is64bit = utilsdll.Is64bit
Is64bit.argtypes = (DWORD,)
Is64bit.restype = c_bool
isDark = utilsdll.isDark
isDark.restype = c_bool

View File

@ -1,6 +1,7 @@
#include <Windows.h>
#include <stdint.h>
#include <dwmapi.h>
#include <string>
#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;
}