diff --git a/LunaTranslator/LunaTranslator/gui/settin.py b/LunaTranslator/LunaTranslator/gui/settin.py index 446002d2..fb0eb35b 100644 --- a/LunaTranslator/LunaTranslator/gui/settin.py +++ b/LunaTranslator/LunaTranslator/gui/settin.py @@ -15,7 +15,7 @@ from PyQt5.QtWidgets import ( from PyQt5.QtGui import QResizeEvent from PyQt5.QtWidgets import QTabWidget import qtawesome, darkdetect, gobject -import functools +import functools, threading, windows, os, winsharedutils from traceback import print_exc from myutils.config import globalconfig, _TR from myutils.utils import wavmp3player @@ -47,6 +47,7 @@ class Settin(closeashidewindow): clicksourcesignal = pyqtSignal(str) opensolvetextsig = pyqtSignal() showandsolvesig = pyqtSignal(str) + setstylesheetsignal = pyqtSignal() def resizefunction(self): @@ -138,6 +139,8 @@ class Settin(closeashidewindow): setTab_about_dicrect(self) self.setstylesheet() + self.setstylesheetsignal.connect(self.setstylesheet) + threading.Thread(target=self.darklistener).start() def inittray(self): @@ -231,6 +234,15 @@ class Settin(closeashidewindow): setTab_about(self) self.isfirstshow = False + def darklistener(self): + sema = winsharedutils.startdarklistener() + while True: + # 会触发两次 + windows.WaitForSingleObject(sema, windows.INFINITE) + windows.WaitForSingleObject(sema, windows.INFINITE) + if globalconfig["darklight"] == 2: + self.setstylesheetsignal.emit() + def setstylesheet(self): dl = globalconfig["darklight"] diff --git a/LunaTranslator/LunaTranslator/winsharedutils.py b/LunaTranslator/LunaTranslator/winsharedutils.py index 63c206f7..338d0459 100644 --- a/LunaTranslator/LunaTranslator/winsharedutils.py +++ b/LunaTranslator/LunaTranslator/winsharedutils.py @@ -18,7 +18,7 @@ from ctypes import ( windll, c_char, ) -from ctypes.wintypes import WORD +from ctypes.wintypes import WORD, HANDLE import gobject utilsdll = CDLL(gobject.GetDllpath(("winsharedutils32.dll", "winsharedutils64.dll"))) @@ -309,3 +309,7 @@ def queryversion(exe): if succ: return _1.value, _2.value, _3.value, _4.value return None + + +startdarklistener = utilsdll.startdarklistener +startdarklistener.restype = HANDLE diff --git a/plugins/winsharedutils/CMakeLists.txt b/plugins/winsharedutils/CMakeLists.txt index 13ac89b9..23979673 100644 --- a/plugins/winsharedutils/CMakeLists.txt +++ b/plugins/winsharedutils/CMakeLists.txt @@ -2,7 +2,7 @@ project(winsharedutils) -add_library(winsharedutils MODULE version.cpp otsu.cpp cinterface.cpp clipboard.cpp lnk.cpp dllmain.cpp levenshtein.cpp muteprocess.cpp sapi_dll.cpp simplemecab.cpp SimpleBrowser.cpp MWebBrowser.cpp icon.cpp) +add_library(winsharedutils MODULE darklistener.cpp version.cpp otsu.cpp cinterface.cpp clipboard.cpp lnk.cpp dllmain.cpp levenshtein.cpp muteprocess.cpp sapi_dll.cpp simplemecab.cpp SimpleBrowser.cpp MWebBrowser.cpp icon.cpp) if(${CMAKE_SIZEOF_VOID_P} EQUAL 8) set_target_properties(winsharedutils PROPERTIES OUTPUT_NAME "winsharedutils64") diff --git a/plugins/winsharedutils/darklistener.cpp b/plugins/winsharedutils/darklistener.cpp new file mode 100644 index 00000000..6d56ab58 --- /dev/null +++ b/plugins/winsharedutils/darklistener.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include "define.h" +HANDLE hsema; +void IsColorSchemeChangeMessage(LPARAM lParam) +{ + if (lParam && CompareStringOrdinal(reinterpret_cast(lParam), -1, L"ImmersiveColorSet", -1, TRUE) == CSTR_EQUAL) + { + ReleaseSemaphore(hsema, 1, 0); + } +} +void startdarklistener_1() +{ + const wchar_t CLASS_NAME[] = L"LunaDarkListener"; + + WNDCLASS wc = {}; + wc.lpfnWndProc = [](HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) + { + if (WM_SETTINGCHANGE == message) + IsColorSchemeChangeMessage(lParam); + return DefWindowProc(hWnd, message, wParam, lParam); + }; + wc.hInstance = GetModuleHandle(0); + wc.lpszClassName = CLASS_NAME; + + static auto _ = RegisterClass(&wc); + HWND hWnd = CreateWindowEx( + WS_EX_CLIENTEDGE, CLASS_NAME, CLASS_NAME, WS_OVERLAPPEDWINDOW, + 0, 0, 0, 0, + NULL, NULL, GetModuleHandle(0), 0); + MSG msg = {}; + while (GetMessage(&msg, NULL, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } +} +HANDLE startdarklistener() +{ + hsema = CreateSemaphoreW(0, 0, 10, 0); + std::thread(startdarklistener_1).detach(); + return hsema; +} \ No newline at end of file diff --git a/plugins/winsharedutils/define.h b/plugins/winsharedutils/define.h index 4d1f967a..ec907a71 100644 --- a/plugins/winsharedutils/define.h +++ b/plugins/winsharedutils/define.h @@ -2,6 +2,7 @@ #include extern "C" { + __declspec(dllexport) HANDLE startdarklistener(); __declspec(dllexport) bool queryversion(const wchar_t *exe, WORD *_1, WORD *_2, WORD *_3, WORD *_4); __declspec(dllexport) bool SAPI_Speak(const wchar_t *Content, int version, int voiceid, int rate, int volume, const wchar_t *Filename);