ENH: webview dark mode

Change-Id: I0b58e759268d057d5942dd8726c37d1abbdd5930
This commit is contained in:
chunmao.guo 2022-11-11 09:48:51 +08:00 committed by Lane.Wei
parent 65ee16f676
commit bf24a71b60
7 changed files with 87 additions and 10 deletions

View file

@ -67,6 +67,23 @@ class FakeWebView : public wxWebView
virtual void DoSetPage(const wxString& html, const wxString& baseUrl) override { }
};
wxDEFINE_EVENT(EVT_WEBVIEW_RECREATED, wxCommandEvent);
static std::vector<wxWebView*> g_webviews;
class WebViewRef : public wxObjectRefData
{
public:
WebViewRef(wxWebView *webView) : m_webView(webView) {}
~WebViewRef() {
auto iter = std::find(g_webviews.begin(), g_webviews.end(), m_webView);
assert(iter != g_webviews.end());
if (iter != g_webviews.end())
g_webviews.erase(iter);
}
wxWebView *m_webView;
};
wxWebView* WebView::CreateWebView(wxWindow * parent, wxString const & url)
{
#if wxUSE_WEBVIEW_EDGE
@ -90,11 +107,13 @@ wxWebView* WebView::CreateWebView(wxWindow * parent, wxString const & url)
auto webView = wxWebView::New();
if (webView) {
#ifdef __WIN32__
webView->SetUserAgent(wxString::Format("BBL-Slicer/v%s", SLIC3R_VERSION));
webView->SetUserAgent(wxString::Format("BBL-Slicer/v%s (%s) Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.52", SLIC3R_VERSION,
Slic3r::GUI::wxGetApp().dark_mode() ? "dark" : "light"));
webView->Create(parent, wxID_ANY, url2, wxDefaultPosition, wxDefaultSize);
//We register the wxfs:// protocol for testing purposes
// We register the wxfs:// protocol for testing purposes
webView->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("bbl")));
//And the memory: file system
// And the memory: file system
webView->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewFSHandler("memory")));
#else
// With WKWebView handlers need to be registered before creation
@ -102,7 +121,8 @@ wxWebView* WebView::CreateWebView(wxWindow * parent, wxString const & url)
// And the memory: file system
webView->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewFSHandler("memory")));
webView->Create(parent, wxID_ANY, url2, wxDefaultPosition, wxDefaultSize);
webView->SetUserAgent(wxString::Format("BBL-Slicer/v%s", 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"));
#endif
#ifndef __WIN32__
Slic3r::GUI::wxGetApp().CallAfter([webView] {
@ -121,6 +141,8 @@ wxWebView* WebView::CreateWebView(wxWindow * parent, wxString const & url)
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": failed. Use fake web view.";
webView = new FakeWebView;
}
webView->SetRefData(new WebViewRef(webView));
g_webviews.push_back(webView);
return webView;
}
@ -172,3 +194,35 @@ bool WebView::RunScript(wxWebView *webView, wxString const &javascript)
return false;
}
}
void WebView::RecreateAll()
{
#ifdef __WXMSW__
auto webviews = g_webviews;
std::vector<wxWindow*> parents;
std::vector<wxString> urls;
for (auto web : webviews) {
parents.push_back(web->GetParent());
urls.push_back(web->GetCurrentURL());
delete web;
}
assert(g_webviews.empty());
for (int i = 0; i < parents.size(); ++i) {
auto webView = CreateWebView(parents[i], urls[i]);
if (webView) {
wxCommandEvent evt(EVT_WEBVIEW_RECREATED);
evt.SetEventObject(webView);
wxPostEvent(parents[i], evt);
}
}
#else
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,
Slic3r::GUI::wxGetApp().dark_mode() ? "dark" : "light"));
webView->Reload();
wxCommandEvent evt(EVT_WEBVIEW_RECREATED);
evt.SetEventObject(webView);
wxPostEvent(webView->GetParent(), evt);
}
#endif
}