-
Notifications
You must be signed in to change notification settings - Fork 726
Expand file tree
/
Copy pathDeviceWebPage.cpp
More file actions
179 lines (145 loc) · 5.12 KB
/
DeviceWebPage.cpp
File metadata and controls
179 lines (145 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "slic3r/GUI/wxExtensions.hpp"
#include "slic3r/GUI/GUI_App.hpp"
#include "slic3r/GUI/MainFrame.hpp"
#include "libslic3r/Utils.hpp"
#include "libslic3r_version.h"
#include "DeviceWebPage.hpp"
#include <stdexcept>
#if defined(__WXOSX__)
#include "slic3r/Utils/MacDarkMode.hpp"
#endif
#include <wx/sizer.h>
#include <wx/toolbar.h>
#include <wx/textdlg.h>
namespace Slic3r { namespace GUI {
// In release builds, always use file:// protocol.
// In debug builds, enable HTTP server toolbar for hot-reload development.
#if !BBL_RELEASE_TO_PUBLIC
#define DEVICE_USE_HTTP_SERVER
#endif
DeviceWebPage::DeviceWebPage(wxWindow *parent): wxPanel(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize)
{
#ifdef DEVICE_USE_HTTP_SERVER
m_device_http_server = std::make_unique<DeviceHttpServer>();
#endif
m_device_webview = new PrinterWebView(this);
m_device_web_bridge = std::make_unique<DeviceWebBridge>(m_device_webview->GetWebView());
// Create ViewModel infrastructure
m_device_web_mgr = std::make_unique<DeviceWebManager>();
m_device_web_mgr->Register(std::make_unique<FilaManagerVM>());
m_device_web_mgr->SetBridge(m_device_web_bridge.get());
m_device_web_bridge->SetManager(m_device_web_mgr.get());
auto web_sizer = new wxBoxSizer(wxVERTICAL);
web_sizer->Add(m_device_webview, 1, wxEXPAND);
// Restore saved zoom once the page has finished loading.
// Binding to GetWebView() here rather than inside PrinterWebView keeps
// this behaviour exclusive to the filament manager (PrinterWebView is
// reused by the printer-web panel in MainFrame).
if (wxWebView* wv = GetWebView())
wv->Bind(wxEVT_WEBVIEW_LOADED, &DeviceWebPage::OnWebLoaded, this);
LoadUrl();
SetSizer(web_sizer);
Layout();
Fit();
}
DeviceWebPage::~DeviceWebPage()
{
// Disconnect cross-references before destruction
if (m_device_web_bridge) m_device_web_bridge->SetManager(nullptr);
if (m_device_web_mgr) m_device_web_mgr->SetBridge(nullptr);
// unique_ptr members are released automatically in reverse declaration order
}
void DeviceWebPage::LoadUrl()
{
// Get current studio language for frontend i18n
std::string lang = wxGetApp().app_config->get("language");
if (lang.empty()) lang = "en";
// Load the web app
#ifdef DEVICE_USE_HTTP_SERVER
if(!m_device_http_server->is_started()){
m_device_http_server->start();
}
wxString url = wxString::Format("http://localhost:13628/index.html?lang=%s", lang);
#else
wxString url = wxString::Format("file://%s/web/device_page/dist/index.html?lang=%s", from_u8(resources_dir()), lang);
#endif
m_device_webview->load_url(url);
}
void DeviceWebPage::on_sys_color_changed()
{
if (m_device_web_mgr)
m_device_web_mgr->NotifyColorChanged();
}
void DeviceWebPage::msw_rescale()
{
// WebView content scales via browser zoom — no native wx controls to rescale
}
void DeviceWebPage::NavigateTo(const std::string& path)
{
#if defined(__WXOSX__)
// macOS: wxWebView::RunScript can block the UI thread for a long time on WKWebView (STUDIO-18111).
// Use async evaluateJavaScript; other platforms keep synchronous RunScript.
wxWebView* wv = m_device_webview ? m_device_webview->GetWebView() : nullptr;
if (!wv)
return;
const wxString p = wxString::FromUTF8(path);
const wxString script = wxString::Format(
"try{var t='#%s';if(window.location.hash!==t)window.location.hash=t;}catch(e){}", p);
void* native = wv->GetNativeBackend();
if (native)
WKWebView_evaluateJavaScript(native, script, nullptr);
else
wv->RunScript(script);
#else
if (auto* wv = m_device_webview->GetWebView()) {
const wxString p = wxString::FromUTF8(path);
wv->RunScript(wxString::Format(
"try{var t='#%s';if(window.location.hash!==t)window.location.hash=t;}catch(e){}", p));
}
#endif
}
void DeviceWebPage::NotifyFilamentSessionState()
{
if (!m_device_web_mgr)
return;
m_device_web_mgr->NotifyState("filament", "sync", "state");
m_device_web_mgr->NotifyState("filament", "spool", "list");
}
void DeviceWebPage::NotifyFilamentMachineChanged()
{
if (!m_device_web_mgr)
return;
m_device_web_mgr->NotifyState("filament", "machine", "selected_changed");
}
void DeviceWebPage::OnWebLoaded(wxWebViewEvent& evt)
{
evt.Skip();
auto* config = wxGetApp().app_config;
if (!config || !config->has("filament_manager_zoom_factor"))
return;
wxWebView* wv = GetWebView();
if (!wv)
return;
try {
float zoom = std::stof(config->get("filament_manager_zoom_factor"));
if (zoom >= 0.25f && zoom <= 5.0f)
wv->SetZoomFactor(zoom);
} catch (const std::exception&) {
// Ignore malformed config values; browser default (1.0) will be used.
}
}
void DeviceWebPage::SaveZoom()
{
wxWebView* wv = GetWebView();
if (!wv)
return;
float zoom = wv->GetZoomFactor();
if (zoom <= 0.0f)
return;
auto* config = wxGetApp().app_config;
if (!config)
return;
config->set("filament_manager_zoom_factor", std::to_string(zoom));
config->save();
}
}}