From fa22e9e0a3c820c63f1aee96c26b95ed272c4d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Mon, 17 Jun 2024 17:01:01 +0800 Subject: [PATCH] dark --- .../LunaTranslator/gui/setting_display_ui.py | 5 ++- .../LunaTranslator/gui/usefulwidget.py | 31 +++++++++++++------ .../LunaTranslator/myutils/utils.py | 8 ++--- .../LunaTranslator/winsharedutils.py | 4 +++ .../files/defaultconfig/config.json | 2 +- plugins/libs/libs.cmake | 2 +- plugins/scripts/fetchwebview2.py | 2 +- plugins/winsharedutils/webview2_extra.cpp | 19 +++++++++++- 8 files changed, 52 insertions(+), 21 deletions(-) diff --git a/LunaTranslator/LunaTranslator/gui/setting_display_ui.py b/LunaTranslator/LunaTranslator/gui/setting_display_ui.py index dfd32d00..4ba0d8f1 100644 --- a/LunaTranslator/LunaTranslator/gui/setting_display_ui.py +++ b/LunaTranslator/LunaTranslator/gui/setting_display_ui.py @@ -9,7 +9,6 @@ from gui.usefulwidget import ( D_getspinbox, D_getcolorbutton, getcolorbutton, - D_getsimpleswitch, selectcolor, ) @@ -316,9 +315,9 @@ def uisetting(self): ("明暗", 4), ( D_getsimplecombobox( - _TRL(["明亮", "黑暗", "跟随系统"]), + _TRL(["跟随系统", "明亮", "黑暗"]), globalconfig, - "darklight", + "darklight2", lambda _: gobject.baseobject.setcommonstylesheet(), ), 5, diff --git a/LunaTranslator/LunaTranslator/gui/usefulwidget.py b/LunaTranslator/LunaTranslator/gui/usefulwidget.py index 82b3fded..3dad73d8 100644 --- a/LunaTranslator/LunaTranslator/gui/usefulwidget.py +++ b/LunaTranslator/LunaTranslator/gui/usefulwidget.py @@ -683,6 +683,11 @@ class WebivewWidget(abstractwebview): self.__token, ) + def get_controller(self): + return self.webview.get_native_handle( + webview_native_handle_kind_t.WEBVIEW_NATIVE_HANDLE_KIND_BROWSER_CONTROLLER + ) + def __init__(self, parent=None, debug=False) -> None: super().__init__(parent) declare_library_path( @@ -700,25 +705,31 @@ class WebivewWidget(abstractwebview): self.on_ZoomFactorChanged.emit ) self.__token = winsharedutils.add_ZoomFactorChanged( - self.webview.get_native_handle( - webview_native_handle_kind_t.WEBVIEW_NATIVE_HANDLE_KIND_BROWSER_CONTROLLER - ), - zoomfunc, + self.get_controller(), zoomfunc ) self.keepref = [zoomfunc] self.webview.bind("__on_load", self._on_load) self.webview.init("""window.__on_load(window.location.href)""") + self.__darkstate = None + t = QTimer(self) + t.setInterval(100) + t.timeout.connect(self.__darkstatechecker) + t.timeout.emit() + t.start() + + def __darkstatechecker(self): + dl = globalconfig["darklight2"] + if dl == self.__darkstate: + return + self.__darkstate = dl + winsharedutils.put_PreferredColorScheme(self.get_controller(), dl) + def set_zoom(self, zoom): self.put_ZoomFactor(zoom) def put_ZoomFactor(self, zoom): - winsharedutils.put_ZoomFactor( - self.webview.get_native_handle( - webview_native_handle_kind_t.WEBVIEW_NATIVE_HANDLE_KIND_BROWSER_CONTROLLER - ), - zoom, - ) + winsharedutils.put_ZoomFactor(self.get_controller(), zoom) def _on_load(self, href): self.on_load.emit(href) diff --git a/LunaTranslator/LunaTranslator/myutils/utils.py b/LunaTranslator/LunaTranslator/myutils/utils.py index b15474e1..5c1256e2 100644 --- a/LunaTranslator/LunaTranslator/myutils/utils.py +++ b/LunaTranslator/LunaTranslator/myutils/utils.py @@ -84,12 +84,12 @@ def simplehtmlparser(text, tag, sign): def nowisdark(): - dl = globalconfig["darklight"] - if dl == 0: + dl = globalconfig["darklight2"] + if dl == 1: dark = False - elif dl == 1: - dark = True elif dl == 2: + dark = True + elif dl == 0: dark = winsharedutils.isDark() return dark diff --git a/LunaTranslator/LunaTranslator/winsharedutils.py b/LunaTranslator/LunaTranslator/winsharedutils.py index ddfd1df2..52ced67b 100644 --- a/LunaTranslator/LunaTranslator/winsharedutils.py +++ b/LunaTranslator/LunaTranslator/winsharedutils.py @@ -20,6 +20,7 @@ from ctypes import ( c_double, c_char, CFUNCTYPE, + c_long, ) from ctypes.wintypes import WORD, HANDLE, HWND, LONG, DWORD, RECT, BYTE from windows import WINDOWPLACEMENT @@ -409,6 +410,9 @@ get_ZoomFactor.argtypes = (c_void_p,) get_ZoomFactor.restype = c_double put_ZoomFactor = utilsdll.put_ZoomFactor put_ZoomFactor.argtypes = c_void_p, c_double +put_PreferredColorScheme = utilsdll.put_PreferredColorScheme +put_PreferredColorScheme.argtypes = c_void_p, c_int +put_PreferredColorScheme.restype = c_long clipboard_callback = utilsdll.clipboard_callback clipboard_callback.argtypes = (c_void_p,) diff --git a/LunaTranslator/files/defaultconfig/config.json b/LunaTranslator/files/defaultconfig/config.json index a5c5e94a..582c15af 100644 --- a/LunaTranslator/files/defaultconfig/config.json +++ b/LunaTranslator/files/defaultconfig/config.json @@ -1192,7 +1192,7 @@ "type": "offline" } }, - "darklight": 0, + "darklight2": 0, "darktheme": 0, "lighttheme": 0, "usesearchword": false, diff --git a/plugins/libs/libs.cmake b/plugins/libs/libs.cmake index 5d77995e..5c170295 100644 --- a/plugins/libs/libs.cmake +++ b/plugins/libs/libs.cmake @@ -11,7 +11,7 @@ include_directories(${CMAKE_CURRENT_LIST_DIR}/wil/include) include_directories(${CMAKE_CURRENT_LIST_DIR}/miniaudio) include_directories(${CMAKE_CURRENT_LIST_DIR}/tinymp3) -include_directories(${CMAKE_CURRENT_LIST_DIR}/webview2/Microsoft.Web.WebView2.1.0.1150.38/build/native/include) +include_directories(${CMAKE_CURRENT_LIST_DIR}/webview2/Microsoft.Web.WebView2.1.0.2478.35/build/native/include) if(${CMAKE_SIZEOF_VOID_P} EQUAL 4) set(LTLPlatform "Win32") diff --git a/plugins/scripts/fetchwebview2.py b/plugins/scripts/fetchwebview2.py index 0dc56b02..89785ac8 100644 --- a/plugins/scripts/fetchwebview2.py +++ b/plugins/scripts/fetchwebview2.py @@ -1,4 +1,4 @@ -mswebview2_version = "1.0.1150.38" +mswebview2_version = "1.0.2478.35" import os, subprocess diff --git a/plugins/winsharedutils/webview2_extra.cpp b/plugins/winsharedutils/webview2_extra.cpp index 4c9dc758..b195d374 100644 --- a/plugins/winsharedutils/webview2_extra.cpp +++ b/plugins/winsharedutils/webview2_extra.cpp @@ -7,10 +7,27 @@ #include using namespace Microsoft::WRL; #include +#define CHECK_FAILURE(x) \ + if (FAILED((x))) \ + return x; -DECLARE void *add_ZoomFactorChanged(void *m_host, void (*signal)(double)) +DECLARE HRESULT put_PreferredColorScheme(void *m_host, COREWEBVIEW2_PREFERRED_COLOR_SCHEME scheme) { + wil::com_ptr m_controller(reinterpret_cast(m_host)); + wil::com_ptr coreWebView2; + CHECK_FAILURE(m_controller->get_CoreWebView2(&coreWebView2)); + auto webView2_13 = coreWebView2.try_query(); + if (webView2_13) + { + wil::com_ptr profile; + CHECK_FAILURE(webView2_13->get_Profile(&profile)); + CHECK_FAILURE(profile->put_PreferredColorScheme(scheme)); + } + return S_FALSE; +} +DECLARE void *add_ZoomFactorChanged(void *m_host, void (*signal)(double)) +{ EventRegistrationToken *m_zoomFactorChangedToken = new EventRegistrationToken; // Register a handler for the ZoomFactorChanged event. // This handler just announces the new level of zoom on the window's title bar.