Fix webview freezing issue

This commit is contained in:
SoftFever 2023-09-04 20:02:26 +08:00
parent f3e4016795
commit e97ab55e83
3 changed files with 36 additions and 17 deletions

View file

@ -19,6 +19,7 @@
#include "libslic3r/Print.hpp" #include "libslic3r/Print.hpp"
#include "libslic3r/Polygon.hpp" #include "libslic3r/Polygon.hpp"
#include "libslic3r/PrintConfig.hpp"
#include "libslic3r/SLAPrint.hpp" #include "libslic3r/SLAPrint.hpp"
#include "libslic3r/PresetBundle.hpp" #include "libslic3r/PresetBundle.hpp"
@ -3505,7 +3506,8 @@ void MainFrame::load_printer_url()
auto cfg = preset_bundle.printers.get_edited_preset().config; auto cfg = preset_bundle.printers.get_edited_preset().config;
wxString url = cfg.opt_string("print_host_webui").empty() ? cfg.opt_string("print_host") : cfg.opt_string("print_host_webui"); wxString url = cfg.opt_string("print_host_webui").empty() ? cfg.opt_string("print_host") : cfg.opt_string("print_host_webui");
wxString apikey; wxString apikey;
if (cfg.has("printhost_apikey")) if (cfg.has("printhost_apikey") && (cfg.option<ConfigOptionEnum<PrintHostType>>("host_type")->value == htPrusaLink ||
cfg.option<ConfigOptionEnum<PrintHostType>>("host_type")->value == htPrusaConnect))
apikey = cfg.opt_string("printhost_apikey"); apikey = cfg.opt_string("printhost_apikey");
if (!url.empty()) { if (!url.empty()) {
if (!url.Lower().starts_with("http")) if (!url.Lower().starts_with("http"))

View file

@ -13,6 +13,7 @@
#include <wx/textdlg.h> #include <wx/textdlg.h>
#include <slic3r/GUI/Widgets/WebView.hpp> #include <slic3r/GUI/Widgets/WebView.hpp>
#include <wx/webview.h>
namespace pt = boost::property_tree; namespace pt = boost::property_tree;
@ -32,7 +33,8 @@ PrinterWebView::PrinterWebView(wxWindow *parent)
return; return;
} }
Bind(wxEVT_WEBVIEW_ERROR, &PrinterWebView::OnError, this); m_browser->Bind(wxEVT_WEBVIEW_ERROR, &PrinterWebView::OnError, this);
m_browser->Bind(wxEVT_WEBVIEW_LOADED, &PrinterWebView::OnLoaded, this);
SetSizer(topsizer); SetSizer(topsizer);
@ -69,9 +71,10 @@ void PrinterWebView::load_url(wxString& url, wxString apikey)
// this->Raise(); // this->Raise();
if (m_browser == nullptr) if (m_browser == nullptr)
return; return;
m_apikey = apikey;
m_apikey_sent = false;
m_browser->LoadURL(url); m_browser->LoadURL(url);
if(!apikey.IsEmpty())
SendAPIKey(apikey);
//m_browser->SetFocus(); //m_browser->SetFocus();
UpdateState(); UpdateState();
} }
@ -89,21 +92,27 @@ void PrinterWebView::OnClose(wxCloseEvent& evt)
this->Hide(); this->Hide();
} }
void PrinterWebView::SendAPIKey(wxString apikey) void PrinterWebView::SendAPIKey()
{ {
// After the page is fully loaded, run: if (m_apikey_sent || m_apikey.IsEmpty())
return;
m_apikey_sent = true;
wxString script = wxString::Format(R"( wxString script = wxString::Format(R"(
// Example for overriding the global fetch method // Check if window.fetch exists before overriding
if (window.fetch) {
const originalFetch = window.fetch; const originalFetch = window.fetch;
window.fetch = function(input, init = {}) { window.fetch = function(input, init = {}) {
init.headers = init.headers || {}; init.headers = init.headers || {};
init.headers['X-API-Key'] = '%s'; init.headers['X-API-Key'] = '%s';
return originalFetch(input, init); return originalFetch(input, init);
}; };
}
)", )",
apikey); m_apikey);
m_browser->RemoveAllUserScripts();
m_browser->RunScript(script); m_browser->AddUserScript(script);
m_browser->Reload();
} }
void PrinterWebView::OnError(wxWebViewEvent &evt) void PrinterWebView::OnError(wxWebViewEvent &evt)
@ -138,7 +147,12 @@ void PrinterWebView::OnError(wxWebViewEvent &evt)
BOOST_LOG_TRIVIAL(info) << __FUNCTION__<< boost::format(": error loading page %1% %2% %3% %4%") %evt.GetURL() %evt.GetTarget() %e %evt.GetString(); BOOST_LOG_TRIVIAL(info) << __FUNCTION__<< boost::format(": error loading page %1% %2% %3% %4%") %evt.GetURL() %evt.GetTarget() %e %evt.GetString();
} }
void PrinterWebView::OnLoaded(wxWebViewEvent &evt)
{
if (evt.GetURL().IsEmpty())
return;
SendAPIKey();
}
} // GUI } // GUI
} // Slic3r } // Slic3r

View file

@ -40,11 +40,14 @@ public:
void UpdateState(); void UpdateState();
void OnClose(wxCloseEvent& evt); void OnClose(wxCloseEvent& evt);
void OnError(wxWebViewEvent& evt); void OnError(wxWebViewEvent& evt);
void OnLoaded(wxWebViewEvent& evt);
private: private:
void SendAPIKey(wxString apikey); void SendAPIKey();
wxWebView* m_browser; wxWebView* m_browser;
long m_zoomFactor; long m_zoomFactor;
wxString m_apikey;
bool m_apikey_sent;
// DECLARE_EVENT_TABLE() // DECLARE_EVENT_TABLE()
}; };