FIX: [STUDIO-3255] edge webview2 color scheme

Change-Id: I04d5b59dbfdc1e1007fc5f4239abdd0104dbb2a2
This commit is contained in:
chunmao.guo 2023-06-19 18:23:28 +08:00 committed by Lane.Wei
parent 14cb2449c6
commit 0fa98a9878
1 changed files with 34 additions and 1 deletions

View File

@ -44,6 +44,9 @@ class WebViewEdge : public wxWebViewEdge
public: public:
bool SetUserAgent(const wxString &userAgent) bool SetUserAgent(const wxString &userAgent)
{ {
bool dark = userAgent.Contains("dark");
SetColorScheme(dark ? COREWEBVIEW2_PREFERRED_COLOR_SCHEME_DARK : COREWEBVIEW2_PREFERRED_COLOR_SCHEME_LIGHT);
ICoreWebView2 *webView2 = (ICoreWebView2 *) GetNativeBackend(); ICoreWebView2 *webView2 = (ICoreWebView2 *) GetNativeBackend();
if (webView2) { if (webView2) {
ICoreWebView2Settings *settings; ICoreWebView2Settings *settings;
@ -57,12 +60,34 @@ public:
return true; return true;
} }
} }
settings->Release();
return false; return false;
} }
pendingUserAgent = userAgent; pendingUserAgent = userAgent;
return true; return true;
} }
bool SetColorScheme(COREWEBVIEW2_PREFERRED_COLOR_SCHEME colorScheme)
{
ICoreWebView2 *webView2 = (ICoreWebView2 *) GetNativeBackend();
if (webView2) {
ICoreWebView2_13 * webView2_13;
HRESULT hr = webView2->QueryInterface(&webView2_13);
if (hr == S_OK) {
ICoreWebView2Profile *profile;
hr = webView2_13->get_Profile(&profile);
if (hr == S_OK) {
profile->put_PreferredColorScheme(colorScheme);
profile->Release();
return true;
}
}
return false;
}
pendingColorScheme = colorScheme;
return true;
}
void DoGetClientSize(int *x, int *y) const override void DoGetClientSize(int *x, int *y) const override
{ {
if (!pendingUserAgent.empty()) { if (!pendingUserAgent.empty()) {
@ -71,10 +96,17 @@ public:
thiz->pendingUserAgent.clear(); thiz->pendingUserAgent.clear();
thiz->SetUserAgent(userAgent); thiz->SetUserAgent(userAgent);
} }
if (pendingColorScheme) {
auto thiz = const_cast<WebViewEdge *>(this);
auto colorScheme = pendingColorScheme;
thiz->pendingColorScheme = COREWEBVIEW2_PREFERRED_COLOR_SCHEME_AUTO;
thiz->SetColorScheme(colorScheme);
}
wxWebViewEdge::DoGetClientSize(x, y); wxWebViewEdge::DoGetClientSize(x, y);
}; };
private: private:
wxString pendingUserAgent; wxString pendingUserAgent;
COREWEBVIEW2_PREFERRED_COLOR_SCHEME pendingColorScheme = COREWEBVIEW2_PREFERRED_COLOR_SCHEME_AUTO;
}; };
#elif defined __WXOSX__ #elif defined __WXOSX__
@ -280,9 +312,10 @@ bool WebView::RunScript(wxWebView *webView, wxString const &javascript)
void WebView::RecreateAll() void WebView::RecreateAll()
{ {
auto dark = Slic3r::GUI::wxGetApp().dark_mode();
for (auto webView : g_webviews) { for (auto webView : g_webviews) {
webView->SetUserAgent(wxString::Format("BBL-Slicer/v%s (%s) Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko)", SLIC3R_VERSION, webView->SetUserAgent(wxString::Format("BBL-Slicer/v%s (%s) Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko)", SLIC3R_VERSION,
Slic3r::GUI::wxGetApp().dark_mode() ? "dark" : "light")); dark ? "dark" : "light"));
webView->Reload(); webView->Reload();
} }
} }