OrcaSlicer latest
OrcaSlicer
Loading...
Searching...
No Matches
Slic3r::PluginPage Class Reference

#include </home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/plugin/host/PluginPages.hpp>

Inheritance diagram for Slic3r::PluginPage:
Collaboration diagram for Slic3r::PluginPage:

Public Member Functions

 PluginPage (wxWindow *parent, std::shared_ptr< PagesPluginCapability > capability)
 ~PluginPage () override
 PluginPage ()=delete
void detach_capability ()
void push_message (const std::string &message)

Protected Member Functions

std::optional< std::string > page_html () override
bool on_page_message (const std::string &kind, const nlohmann::json &data) override
wxWebView * browser () const
void post_to_page (const std::string &json)

Private Member Functions

void on_new_window (wxWebViewEvent &event)
void on_load_event (wxWebViewEvent &event)
void on_navigated (wxWebViewEvent &event)
void on_script_message (wxWebViewEvent &event)
void on_webview_recreated (wxCommandEvent &event)
void apply_theme ()
void load_page_html ()

Private Attributes

std::shared_ptr< PagesPluginCapability > m_cap
std::shared_ptr< std::atomic< PluginPage * > > m_lifetime
wxWebView * m_browser {nullptr}
bool m_content_loaded {false}
bool m_own_page_load {false}
bool m_content_navigated {false}

Detailed Description

Constructor & Destructor Documentation

◆ PluginPage() [1/2]

Slic3r::PluginPage::PluginPage ( wxWindow * parent,
std::shared_ptr< PagesPluginCapability > capability )
81 : GUI::WebPanel(parent, PLUGIN_PAGE_BRIDGE_JS)
82 , m_cap(std::move(capability))
83 , m_lifetime(std::make_shared<std::atomic<PluginPage*>>(this))
84{
85 browser()->Bind(wxEVT_WEBVIEW_NEWWINDOW, &PluginPage::on_new_window, this);
86
87 const std::shared_ptr<std::atomic<PluginPage*>> lifetime = m_lifetime;
88 m_cap->set_message_sender([lifetime](const std::string& message) {
89 if (wxTheApp == nullptr)
90 return;
91
92 GUI::wxGetApp().CallAfter([lifetime, message] {
93 if (PluginPage* page = lifetime->load(std::memory_order_acquire))
94 page->push_message(message);
95 });
96 });
97
98}
wxWebView * browser() const
Definition WebPanel.hpp:28
void on_new_window(wxWebViewEvent &event)
Definition PluginPages.cpp:132
std::shared_ptr< std::atomic< PluginPage * > > m_lifetime
Definition PluginPages.hpp:45
std::shared_ptr< PagesPluginCapability > m_cap
Definition PluginPages.hpp:44
PluginPage(wxWindow *parent, std::shared_ptr< PagesPluginCapability > capability)
Definition PluginPages.cpp:80

References PluginPage(), Slic3r::GUI::WebPanel::WebPanel(), Slic3r::GUI::WebPanel::browser(), m_cap, m_lifetime, and on_new_window().

Referenced by PluginPage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~PluginPage()

Slic3r::PluginPage::~PluginPage ( )
override
101{
103 if (m_lifetime)
104 m_lifetime->store(nullptr, std::memory_order_release);
105}
void detach_capability()
Definition PluginPages.cpp:107

References detach_capability(), and m_lifetime.

Here is the call graph for this function:

◆ PluginPage() [2/2]

Slic3r::PluginPage::PluginPage ( )
delete

Member Function Documentation

◆ apply_theme()

void Slic3r::GUI::WebPanel::apply_theme ( )
privateinherited
104{
106}
wxWebView * m_browser
Definition WebPanel.hpp:48
static std::string theme_apply_script()
Definition WebViewHostDialog.cpp:88
static bool RunScript(wxWebView *webView, wxString const &msg)
Definition WebView.cpp:491

References m_browser, WebView::RunScript(), and Slic3r::GUI::WebViewHostDialog::theme_apply_script().

Referenced by on_load_event(), and on_webview_recreated().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ browser()

wxWebView * Slic3r::GUI::WebPanel::browser ( ) const
inlineprotectedinherited
28{ return m_browser; }

References m_browser.

Referenced by Slic3r::GUI::DockPanel::DockPanel(), Slic3r::PluginPage::PluginPage(), and Slic3r::PluginPage::on_new_window().

Here is the caller graph for this function:

◆ detach_capability()

void Slic3r::PluginPage::detach_capability ( )
108{
109 if (m_lifetime)
110 m_lifetime->store(nullptr, std::memory_order_release);
111 if (m_cap)
112 m_cap->clear_message_sender();
113 m_cap.reset();
114}

References m_cap, and m_lifetime.

Referenced by ~PluginPage(), and page_html().

Here is the caller graph for this function:

◆ load_page_html()

void Slic3r::GUI::WebPanel::load_page_html ( )
privateinherited
76{
77 if (const std::optional<std::string> html = page_html()) {
78 m_own_page_load = true;
79 m_browser->SetPage(wxString::FromUTF8(*html), web_hosting::content_base_url());
80 }
81}
bool m_own_page_load
Definition WebPanel.hpp:50
virtual std::optional< std::string > page_html()=0
wxString content_base_url()
Definition WebHosting.cpp:55

References Slic3r::GUI::web_hosting::content_base_url(), m_browser, m_own_page_load, and page_html().

Referenced by on_load_event().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_load_event()

void Slic3r::GUI::WebPanel::on_load_event ( wxWebViewEvent & event)
privateinherited
43{
44 const bool loaded = event.GetEventType() == wxEVT_WEBVIEW_LOADED;
45 if (!m_content_loaded) {
46 // The first bootstrap load (or its error) triggers the swap to the plugin HTML.
47 m_content_loaded = true;
49 } else if (!web_hosting::is_content_url(event.GetURL())) {
50 // Not our document (a linked page, a substituted error page), or any document on Edge, which ignores
51 // the base URL and restores SetPage content on a reload itself; either way it takes the app theme.
52 if (loaded)
54 } else if (m_own_page_load) {
55 m_own_page_load = false;
56 // The document-start theme script is fixed at creation, so re-apply the app theme.
57 if (loaded)
59 } else if (loaded && m_content_navigated) {
60 // WebKit reloads the SetPage base URL, so a committed load of it that we did not start is a
61 // reload. A failed navigation is reported against the page that stayed but never commits.
63 }
64 if (loaded)
65 m_content_navigated = false;
66 event.Skip();
67}
bool m_content_loaded
Definition WebPanel.hpp:49
void load_page_html()
Definition WebPanel.cpp:75
bool m_content_navigated
Definition WebPanel.hpp:51
void apply_theme()
Definition WebPanel.cpp:103
bool is_content_url(const wxString &url)
Definition WebHosting.cpp:60

References apply_theme(), Slic3r::GUI::web_hosting::is_content_url(), load_page_html(), m_content_loaded, m_content_navigated, and m_own_page_load.

Referenced by WebPanel().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_navigated()

void Slic3r::GUI::WebPanel::on_navigated ( wxWebViewEvent & event)
privateinherited
70{
72 event.Skip();
73}

References Slic3r::GUI::web_hosting::is_content_url(), and m_content_navigated.

Referenced by WebPanel().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_new_window()

void Slic3r::PluginPage::on_new_window ( wxWebViewEvent & event)
private
133{
134 const wxString url = event.GetURL();
135 if (!url.empty())
136 browser()->LoadURL(url);
137 event.Veto();
138}

References Slic3r::GUI::WebPanel::browser().

Referenced by PluginPage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_page_message()

bool Slic3r::PluginPage::on_page_message ( const std::string & kind,
const nlohmann::json & data )
overrideprotectedvirtual

Implements Slic3r::GUI::WebPanel.

141{
142 if (kind != "message")
143 return false;
144 if (!m_cap)
145 return true;
146
147 try {
148 m_cap->on_message(data.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace));
149 } catch (const std::exception& error) {
150 BOOST_LOG_TRIVIAL(error) << "Plugin page message handler failed for '" << m_cap->name() << "': " << error.what();
151 } catch (...) {
152 BOOST_LOG_TRIVIAL(error) << "Plugin page message handler failed for '" << m_cap->name() << "'";
153 }
154 return true;
155}

References m_cap.

◆ on_script_message()

void Slic3r::GUI::WebPanel::on_script_message ( wxWebViewEvent & event)
privateinherited
84{
85 const nlohmann::json payload = nlohmann::json::parse(event.GetString().utf8_string(), nullptr, false);
86 if (!payload.is_object() || payload.value("channel", std::string()) != "orca")
87 return;
88
89 const std::string kind = payload.value("kind", std::string());
90 if (!on_page_message(kind, payload.contains("data") ? payload["data"] : nlohmann::json()))
91 BOOST_LOG_TRIVIAL(warning) << "WebPanel ignored a window.orca '" << kind << "' call; this host does not support it";
92}
virtual bool on_page_message(const std::string &kind, const nlohmann::json &data)=0

References on_page_message().

Referenced by WebPanel().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_webview_recreated()

void Slic3r::GUI::WebPanel::on_webview_recreated ( wxCommandEvent & event)
privateinherited
95{
96 SetBackgroundColour(wxGetApp().get_window_default_clr());
97 m_browser->SetBackgroundColour(GetBackgroundColour());
98 Refresh();
99 // Handled without Skip(), so WebView::RecreateAll() does not reload the plugin page.
100 apply_theme();
101}

References apply_theme(), and m_browser.

Referenced by WebPanel().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ page_html()

std::optional< std::string > Slic3r::PluginPage::page_html ( )
overrideprotectedvirtual

Implements Slic3r::GUI::WebPanel.

117{
118 if (m_cap == nullptr)
119 return std::nullopt;
120
121 try {
122 return m_cap->get_ui();
123 } catch (const std::exception& error) {
124 BOOST_LOG_TRIVIAL(error) << "Failed to load plugin page '" << m_cap->name() << "': " << error.what();
125 } catch (...) {
126 BOOST_LOG_TRIVIAL(error) << "Failed to load plugin page '" << m_cap->name() << "'";
127 }
129 return std::nullopt;
130}

References detach_capability(), and m_cap.

Here is the call graph for this function:

◆ post_to_page()

void Slic3r::GUI::WebPanel::post_to_page ( const std::string & json)
protectedinherited
109{
110 WebView::RunScript(m_browser, wxString::Format(
111 "(function dispatch(payload, attempts) {\n"
112 " if (typeof window.__orcaDispatch === 'function') { window.__orcaDispatch(payload); return; }\n"
113 " if (attempts < 100) window.setTimeout(function() { dispatch(payload, attempts + 1); }, 25);\n"
114 "})({data: %s}, 0);",
115 wxString::FromUTF8(json)));
116}
nlohmann::json json
Definition AssembleList.cpp:42

References m_browser, and WebView::RunScript().

Referenced by Slic3r::GUI::DockPanel::push_message(), and Slic3r::PluginPage::push_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ push_message()

void Slic3r::PluginPage::push_message ( const std::string & message)
158{
159 // PagesPluginCapability::post_message() already dumps JSON, so accept it as-is; only a
160 // non-JSON payload needs wrapping as a string literal.
161 post_to_page(nlohmann::json::accept(message)
162 ? message
163 : nlohmann::json(message).dump(-1, ' ', false, nlohmann::json::error_handler_t::replace));
164}
void post_to_page(const std::string &json)
Definition WebPanel.cpp:108
size_t dump(const PresetBundle &bundle, const Sections &sections, std::ostream &out)
Definition profile_include_dump.cpp:96

References Slic3r::GUI::WebPanel::post_to_page().

Here is the call graph for this function:

Member Data Documentation

◆ m_browser

wxWebView* Slic3r::GUI::WebPanel::m_browser {nullptr}
privateinherited

◆ m_cap

std::shared_ptr<PagesPluginCapability> Slic3r::PluginPage::m_cap
private

◆ m_content_loaded

bool Slic3r::GUI::WebPanel::m_content_loaded {false}
privateinherited
49{false};

Referenced by on_load_event().

◆ m_content_navigated

bool Slic3r::GUI::WebPanel::m_content_navigated {false}
privateinherited
51{false}; // a navigation to the base URL has committed

Referenced by on_load_event(), and on_navigated().

◆ m_lifetime

std::shared_ptr<std::atomic<PluginPage*> > Slic3r::PluginPage::m_lifetime
private

◆ m_own_page_load

bool Slic3r::GUI::WebPanel::m_own_page_load {false}
privateinherited
50{false}; // a SetPage of the plugin HTML is in flight

Referenced by load_page_html(), and on_load_event().


The documentation for this class was generated from the following files:
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/plugin/host/PluginPages.hpp
  • /home/runner/work/OrcaSlicer/OrcaSlicer/src/slic3r/plugin/host/PluginPages.cpp