Files
OrcaSlicer/src/slic3r/GUI/Widgets/WebViewHostDialog.hpp
T
Hanif Koh 2403855d93 Add a Dockable HTML Panel API for Plugins
orca.host.ui.create_dock_panel(html, title, width, height, on_message,
on_close, dock) hosts plugin HTML in a pane of the Plater's dock manager,
next to the sidebar, and returns a UiDockPanel handle
(post/show/hide/close/is_open). The arguments follow create_window(). The
panel uses the window.orca bridge of plugin windows, restores its position
and size from the saved window layout, hides with the Plater off the Prepare
and Preview tabs when floating, and is closed with its plugin; plugin panes
are removed in MainFrame::shutdown().

The web view hosting moves out of PluginPage into a shared PluginWebPanel
base: bootstrap page and swap to the plugin HTML, theme, element-default and
bridge scripts, window.orca message parsing, delivery to the page, and live
re-theming, also re-applied on every load after the swap. Pages tabs and
docked panels both derive from it, and it now owns the bootstrap page, base
URL and plugin-window bridge that PluginWebDialog uses. Pages tabs now
re-theme in place on a theme change instead of being reloaded, and a
window.orca call a host does not support is logged.

Includes a sample plugin (sandboxes/orca_dock_panel_plugin_any.py) and
binding and layout-helper tests in slic3rutils.
2026-09-17 17:46:50 +08:00

95 lines
4.4 KiB
C++

#ifndef slic3r_GUI_Widgets_WebViewHostDialog_hpp_
#define slic3r_GUI_Widgets_WebViewHostDialog_hpp_
#include <nlohmann/json.hpp>
#include <slic3r/GUI/GUI_Utils.hpp>
#include <exception>
#include <string>
#include <wx/string.h>
#include <wx/webview.h>
namespace Slic3r { namespace GUI {
// Shared shell for local HTML dialogs that communicate through window.wx.postMessage().
class WebViewHostDialog : public Slic3r::GUI::DPIDialog
{
public:
WebViewHostDialog(wxWindow* parent,
wxWindowID id = wxID_ANY,
const wxString& title = wxT(""),
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
// wxRESIZE_BORDER is required for a resizable frame on MSW/GTK; macOS derives
// one from wxMAXIMIZE_BOX alone, which is why these dialogs used to resize only there.
long style = wxSYSTEM_MENU | wxCAPTION | wxCLOSE_BOX | wxMAXIMIZE_BOX | wxRESIZE_BORDER);
~WebViewHostDialog() override = default;
bool create_webview(const std::string& resource_path,
const wxString& title,
const wxSize& dialog_size = wxSize(820, 660),
const wxSize& min_size = wxSize(640, 640));
void load_url(const wxString& url);
bool run_script(const wxString& script);
void call_web_handler(const nlohmann::json& payload, const wxString& handler = wxT("HandleStudio"));
// Wraps `markup` (an HTML fragment, usually a <style> block) in a document-start user
// script that inserts it once — guarded by element id `dom_id`, at `position` (an
// insertAdjacentHTML target such as "afterbegin"/"beforeend") — retrying via a
// MutationObserver until a root node exists. On WebView2 a document-start script can run
// before <html> exists (document.head and document.documentElement both null), so a bare
// insert would throw and silently never apply. `prelude` is emitted once before the
// injector (extra var/flag declarations); `on_inject` runs inside inject() after each
// successful insert. Both default to empty.
static std::string document_start_injector(const std::string& markup,
const char* dom_id,
const char* position,
const std::string& prelude = {},
const std::string& on_inject = {});
// Shared by modeless Pages tabs and PluginWebDialog.
static std::string theme_user_script();
static std::string plugin_defaults_user_script();
// Re-themes an already-loaded page in place, for web views hosted outside a dialog.
static std::string theme_apply_script();
protected:
wxWebView* browser() const { return m_browser; }
wxString build_resource_url(const std::string& resource_path) const;
bool handle_common_script_command(const nlohmann::json& payload, int close_return_code = wxID_CANCEL);
void on_dpi_changed(const wxRect& suggested_rect) override;
virtual void on_script_message(const nlohmann::json& payload) = 0;
virtual void on_script_message_parse_error(const wxString& payload, const std::exception& error);
virtual bool append_language_to_url() const { return true; }
// Registers all document-start user scripts: the shared host theme contract first,
// then subclass scripts from add_user_scripts(). Called ONCE, at creation. Live
// re-theme goes through apply_theme_live() (RunScript), not a re-registration —
// calling this again would append duplicate scripts.
void register_theme_user_scripts();
// Subclasses override to add page-specific document-start user scripts (e.g. the
// plugin bridge / unstyled-content defaults). Called AFTER the theme contract is
// added, by register_theme_user_scripts(). Default: none.
virtual void add_user_scripts() {}
// Pushes the current app theme into the already-loaded document without a reload
// (updates the injected :root variables and the data-orca-theme attribute).
void apply_theme_live();
private:
void on_script_message_event(wxWebViewEvent& event);
void on_webview_recreated(wxCommandEvent& event);
wxWebView* m_browser{nullptr};
};
}} // namespace Slic3r::GUI
#endif