Add support for PrusaLink webview

This commit is contained in:
SoftFever 2023-08-30 23:38:33 +08:00
parent 4564945b48
commit 80082464cb
6 changed files with 71 additions and 20 deletions

View file

@ -2,6 +2,7 @@
#define slic3r_Events_hpp_ #define slic3r_Events_hpp_
#include <array> #include <array>
#include <wx/debug.h>
#include <wx/event.h> #include <wx/event.h>
@ -86,6 +87,27 @@ template<class T> struct Event : public wxEvent
} }
}; };
class LoadPrinterViewEvent : public wxCommandEvent
{
public:
LoadPrinterViewEvent(wxEventType commandType = wxEVT_NULL, int winid = 0)
: wxCommandEvent(commandType, winid)
{ }
LoadPrinterViewEvent(const LoadPrinterViewEvent& event)
: wxCommandEvent(event)
{ m_APIkey = event.m_APIkey; }
const wxString& GetAPIkey() const { return m_APIkey; }
void SetAPIkey(const wxString& apikey) { m_APIkey = apikey; }
virtual wxEvent *Clone() const wxOVERRIDE { return new LoadPrinterViewEvent(*this); }
private:
wxString m_APIkey;
};
} }
} }

View file

@ -84,7 +84,7 @@ wxDEFINE_EVENT(EVT_UPDATE_PRESET_CB, SimpleEvent);
// BBS: backup // BBS: backup
wxDEFINE_EVENT(EVT_BACKUP_POST, wxCommandEvent); wxDEFINE_EVENT(EVT_BACKUP_POST, wxCommandEvent);
wxDEFINE_EVENT(EVT_LOAD_URL, wxCommandEvent); wxDEFINE_EVENT(EVT_LOAD_URL, wxCommandEvent);
wxDEFINE_EVENT(EVT_LOAD_PRINTER_URL, wxCommandEvent); wxDEFINE_EVENT(EVT_LOAD_PRINTER_URL, LoadPrinterViewEvent);
enum class ERescaleTarget enum class ERescaleTarget
{ {
@ -1051,13 +1051,14 @@ void MainFrame::init_tabpanel() {
m_tabpanel->AddPage(m_monitor, _L("Device"), std::string("tab_monitor_active"), std::string("tab_monitor_active")); m_tabpanel->AddPage(m_monitor, _L("Device"), std::string("tab_monitor_active"), std::string("tab_monitor_active"));
m_printer_view = new PrinterWebView(m_tabpanel); m_printer_view = new PrinterWebView(m_tabpanel);
Bind(EVT_LOAD_PRINTER_URL, [this](wxCommandEvent &evt) { Bind(EVT_LOAD_PRINTER_URL, [this](LoadPrinterViewEvent &evt) {
wxString url = evt.GetString(); wxString url = evt.GetString();
wxString key = evt.GetAPIkey();
//select_tab(MainFrame::tpMonitor); //select_tab(MainFrame::tpMonitor);
m_printer_view->load_url(url); m_printer_view->load_url(url, key);
}); });
m_printer_view->Hide(); m_printer_view->Hide();
m_project = new ProjectPanel(m_tabpanel, wxID_ANY, wxDefaultPosition, wxDefaultSize); m_project = new ProjectPanel(m_tabpanel, wxID_ANY, wxDefaultPosition, wxDefaultSize);
m_project->SetBackgroundColour(*wxWHITE); m_project->SetBackgroundColour(*wxWHITE);
m_tabpanel->AddPage(m_project, _L("Project"), std::string("tab_auxiliary_avtice"), std::string("tab_auxiliary_avtice")); m_tabpanel->AddPage(m_project, _L("Project"), std::string("tab_auxiliary_avtice"), std::string("tab_auxiliary_avtice"));
@ -1090,22 +1091,22 @@ void MainFrame::show_device(bool bBBLPrinter) {
if (bBBLPrinter) { if (bBBLPrinter) {
if (m_tabpanel->GetPage(tpMonitor) != m_monitor) { if (m_tabpanel->GetPage(tpMonitor) != m_monitor) {
m_printer_view->Hide(); m_printer_view->Hide();
m_monitor->Show(true); m_monitor->Show(true);
m_tabpanel->RemovePage(tpMonitor); m_tabpanel->RemovePage(tpMonitor);
m_tabpanel->InsertPage(tpMonitor, m_monitor, _L("Device"), m_tabpanel->InsertPage(tpMonitor, m_monitor, _L("Device"),
std::string("tab_monitor_active"), std::string("tab_monitor_active"),
std::string("tab_monitor_active")); std::string("tab_monitor_active"));
m_tabpanel->SetSelection(tp3DEditor); //m_tabpanel->SetSelection(tp3DEditor);
} }
} else { } else {
if (m_tabpanel->GetPage(tpMonitor) != m_printer_view) { if (m_tabpanel->GetPage(tpMonitor) != m_printer_view) {
m_printer_view->Show(); m_printer_view->Show();
m_monitor->Show(false); m_monitor->Show(false);
m_tabpanel->RemovePage(tpMonitor); m_tabpanel->RemovePage(tpMonitor);
m_tabpanel->InsertPage(tpMonitor, m_printer_view, _L("Device"), m_tabpanel->InsertPage(tpMonitor, m_printer_view, _L("Device"),
std::string("tab_monitor_active"), std::string("tab_monitor_active"),
std::string("tab_monitor_active")); std::string("tab_monitor_active"));
m_tabpanel->SetSelection(tp3DEditor); //m_tabpanel->SetSelection(tp3DEditor);
} }
} }
@ -3486,11 +3487,12 @@ void MainFrame::load_url(wxString url)
wxQueueEvent(this, evt); wxQueueEvent(this, evt);
} }
void MainFrame::load_printer_url(wxString url) void MainFrame::load_printer_url(wxString url, wxString apikey)
{ {
BOOST_LOG_TRIVIAL(trace) << "load_printer_url:" << url; BOOST_LOG_TRIVIAL(trace) << "load_printer_url:" << url;
auto evt = new wxCommandEvent(EVT_LOAD_PRINTER_URL, this->GetId()); auto evt = new LoadPrinterViewEvent(EVT_LOAD_PRINTER_URL, this->GetId());
evt->SetString(url); evt->SetString(url);
evt->SetAPIkey(apikey);
wxQueueEvent(this, evt); wxQueueEvent(this, evt);
} }
@ -3499,15 +3501,17 @@ void MainFrame::load_printer_url()
PresetBundle &preset_bundle = *wxGetApp().preset_bundle; PresetBundle &preset_bundle = *wxGetApp().preset_bundle;
if (preset_bundle.is_bbl_vendor()) if (preset_bundle.is_bbl_vendor())
return; return;
auto cfg = preset_bundle.printers.get_edited_preset().config; auto cfg = preset_bundle.printers.get_edited_preset().config;
wxString url = wxString url = cfg.opt_string("print_host_webui").empty() ? cfg.opt_string("print_host") : cfg.opt_string("print_host_webui");
cfg.opt_string("print_host_webui").empty() ? cfg.opt_string("print_host") : cfg.opt_string("print_host_webui"); wxString apikey;
if (cfg.has("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"))
url = wxString::Format("http://%s", url); url = wxString::Format("http://%s", url);
load_printer_url(url); load_printer_url(url, apikey);
} }
} }

View file

@ -335,7 +335,7 @@ public:
//BBS //BBS
void load_url(wxString url); void load_url(wxString url);
void load_printer_url(wxString url); void load_printer_url(wxString url, wxString apikey = "");
void load_printer_url(); void load_printer_url();
bool is_printer_view() const; bool is_printer_view() const;
void refresh_plugin_tips(); void refresh_plugin_tips();

View file

@ -1067,7 +1067,10 @@ void Sidebar::update_all_preset_comboboxes()
if(!url.Lower().starts_with("http")) if(!url.Lower().starts_with("http"))
url = wxString::Format("http://%s",url); url = wxString::Format("http://%s",url);
p_mainframe->load_printer_url(url); wxString apikey;
if (cfg.has("printhost_apikey"))
apikey = cfg.opt_string("printhost_apikey");
p_mainframe->load_printer_url(url, apikey);
} }
m_bed_type_list->SelectAndNotify(btPEI-1); m_bed_type_list->SelectAndNotify(btPEI-1);

View file

@ -1,12 +1,14 @@
#include "PrinterWebView.hpp" #include "PrinterWebView.hpp"
#include "I18N.hpp" #include "I18N.hpp"
#include "slic3r/GUI/PrinterWebView.hpp"
#include "slic3r/GUI/wxExtensions.hpp" #include "slic3r/GUI/wxExtensions.hpp"
#include "slic3r/GUI/GUI_App.hpp" #include "slic3r/GUI/GUI_App.hpp"
#include "slic3r/GUI/MainFrame.hpp" #include "slic3r/GUI/MainFrame.hpp"
#include "libslic3r_version.h" #include "libslic3r_version.h"
#include <wx/sizer.h> #include <wx/sizer.h>
#include <wx/string.h>
#include <wx/toolbar.h> #include <wx/toolbar.h>
#include <wx/textdlg.h> #include <wx/textdlg.h>
@ -61,13 +63,15 @@ PrinterWebView::~PrinterWebView()
} }
void PrinterWebView::load_url(wxString& url) void PrinterWebView::load_url(wxString& url, wxString apikey)
{ {
// this->Show(); // this->Show();
// this->Raise(); // this->Raise();
if (m_browser == nullptr) if (m_browser == nullptr)
return; return;
m_browser->LoadURL(url); m_browser->LoadURL(url);
if(!apikey.IsEmpty())
SendAPIKey(apikey);
//m_browser->SetFocus(); //m_browser->SetFocus();
UpdateState(); UpdateState();
} }
@ -85,6 +89,23 @@ void PrinterWebView::OnClose(wxCloseEvent& evt)
this->Hide(); this->Hide();
} }
void PrinterWebView::SendAPIKey(wxString apikey)
{
// After the page is fully loaded, run:
wxString script = wxString::Format(R"(
// Example for overriding the global fetch method
const originalFetch = window.fetch;
window.fetch = function(input, init = {}) {
init.headers = init.headers || {};
init.headers['X-API-Key'] = '%s';
return originalFetch(input, init);
};
)",
apikey);
m_browser->RunScript(script);
}
void PrinterWebView::OnError(wxWebViewEvent &evt) void PrinterWebView::OnError(wxWebViewEvent &evt)
{ {
auto e = "unknown error"; auto e = "unknown error";

View file

@ -7,6 +7,7 @@
#include "wx/notifmsg.h" #include "wx/notifmsg.h"
#include "wx/settings.h" #include "wx/settings.h"
#include "wx/webview.h" #include "wx/webview.h"
#include <wx/string.h>
#if wxUSE_WEBVIEW_EDGE #if wxUSE_WEBVIEW_EDGE
#include "wx/msw/webview_edge.h" #include "wx/msw/webview_edge.h"
@ -35,12 +36,12 @@ public:
PrinterWebView(wxWindow *parent); PrinterWebView(wxWindow *parent);
virtual ~PrinterWebView(); virtual ~PrinterWebView();
void load_url(wxString& url); void load_url(wxString& url, wxString apikey = "");
void UpdateState(); void UpdateState();
void OnClose(wxCloseEvent& evt); void OnClose(wxCloseEvent& evt);
void OnError(wxWebViewEvent& evt); void OnError(wxWebViewEvent& evt);
private: private:
void SendAPIKey(wxString apikey);
wxWebView* m_browser; wxWebView* m_browser;
long m_zoomFactor; long m_zoomFactor;