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

@ -57,6 +57,7 @@
#include "MarkdownTip.hpp"
#include "NetworkTestDialog.hpp"
#include "ConfigWizard.hpp"
#include "Widgets/WebView.hpp"
#ifdef _WIN32
#include <dbt.h>
@ -1783,6 +1784,8 @@ void MainFrame::on_sys_color_changed()
MenuFactory::sys_color_changed(m_menubar);
WebView::RecreateAll();
this->Refresh();
}

View file

@ -81,6 +81,16 @@ MarkdownTip::MarkdownTip()
_timer = new wxTimer;
_timer->Bind(wxEVT_TIMER, &MarkdownTip::OnTimer, this);
Bind(EVT_WEBVIEW_RECREATED, [this](auto &evt) {
Hide();
_lastTip.clear();
#ifdef __WXMSW__
_tipView = dynamic_cast<wxWebView *>(evt.GetEventObject());
GetSizer()->Add(_tipView, wxSizerFlags().Expand().Proportion(1));
Layout();
#endif
});
}
MarkdownTip::~MarkdownTip() { delete _timer; }

View file

@ -505,7 +505,6 @@ Sidebar::Sidebar(Plater *parent)
PlaterPresetComboBox* combo_printer = new PlaterPresetComboBox(p->m_panel_printer_content, Preset::TYPE_PRINTER);
ScalableButton* edit_btn = new ScalableButton(p->m_panel_printer_content, wxID_ANY, "edit");
edit_btn->SetBackgroundColour(wxColour(255, 255, 255));
edit_btn->SetToolTip(_L("Click to edit preset"));
edit_btn->Bind(wxEVT_BUTTON, [this, combo_printer](wxCommandEvent)
{
@ -863,7 +862,6 @@ void Sidebar::init_filament_combo(PlaterPresetComboBox **combo, const int filame
combo_and_btn_sizer->Add(del_btn, 0, wxALIGN_CENTER_VERTICAL, 5 * em / 10);
*/
ScalableButton* edit_btn = new ScalableButton(p->m_panel_filament_content, wxID_ANY, "edit");
edit_btn->SetBackgroundColour(wxColour(255, 255, 255));
edit_btn->SetToolTip(_L("Click to edit preset"));
PlaterPresetComboBox* combobox = (*combo);

View file

@ -35,7 +35,6 @@ WebViewPanel::WebViewPanel(wxWindow *parent)
std::string strlang = wxGetApp().app_config->get("language");
if (strlang != "")
url = wxString::Format("file://%s/web/homepage/index.html?lang=%s", from_u8(resources_dir()), strlang);
m_bbl_user_agent = wxString::Format("BBL-Slicer/v%s", SLIC3R_VERSION);
wxBoxSizer* topsizer = new wxBoxSizer(wxVERTICAL);
@ -87,6 +86,18 @@ WebViewPanel::WebViewPanel(wxWindow *parent)
topsizer->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
Bind(EVT_WEBVIEW_RECREATED, [this](auto &evt) {
#ifdef __WXMSW__
m_browser = dynamic_cast<wxWebView *>(evt.GetEventObject());
if (m_browser == nullptr) {
wxLogError("Could not recreate browser");
return;
}
GetSizer()->Add(m_browser, wxSizerFlags().Expand().Proportion(1));
GetSizer()->Layout();
#endif
});
// Log backend information
if (wxGetApp().get_mode() == comDevelop) {
wxLogMessage(wxWebView::GetBackendVersionInfo().ToString());
@ -95,7 +106,6 @@ WebViewPanel::WebViewPanel(wxWindow *parent)
wxLogMessage("User Agent: %s", m_browser->GetUserAgent());
}
// Create the Tools menu
m_tools_menu = new wxMenu();
wxMenuItem* viewSource = m_tools_menu->Append(wxID_ANY, _L("View Source"));

View file

@ -145,8 +145,6 @@ private:
wxString m_javascript;
wxString m_response_js;
wxString m_bbl_user_agent;
DECLARE_EVENT_TABLE()
};

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
}

View file

@ -3,6 +3,8 @@
#include <wx/webview.h>
wxDECLARE_EVENT(EVT_WEBVIEW_RECREATED, wxCommandEvent);
class WebView
{
public:
@ -11,6 +13,8 @@ public:
static void LoadUrl(wxWebView * webView, wxString const &url);
static bool RunScript(wxWebView * webView, wxString const & msg);
static void RecreateAll();
};
#endif // !slic3r_GUI_WebView_hpp_