QIDISlicer1.0.0

This commit is contained in:
sunsets
2023-06-10 10:14:12 +08:00
parent f2e20e1a90
commit b4cd486f2d
3475 changed files with 1973675 additions and 0 deletions

View File

@@ -0,0 +1,189 @@
#include "StateColor.hpp"
static bool gDarkMode = false;
static bool operator<(wxColour const &l, wxColour const &r) { return l.GetRGBA() < r.GetRGBA(); }
static std::map<wxColour, wxColour> gDarkColors{
{"#00AE42", "#21A452"},
{"#1F8EEA", "#2778D2"},
{"#FF6F00", "#D15B00"},
{"#D01B1B", "#BB2A3A"},
{"#262E30", "#EFEFF0"},
{"#2C2C2E", "#B3B3B4"},
{"#6B6B6B", "#818183"},
{"#ACACAC", "#54545A"},
{"#EEEEEE", "#4C4C55"},
{"#E8E8E8", "#3E3E45"},
{"#323A3D", "#E5E5E4"},
{"#FFFFFF", "#2D2D31"},
{"#F8F8F8", "#36363C"},
{"#F1F1F1", "#36363B"},
{"#3B4446", "#2D2D30"},
{"#CECECE", "#54545B"},
{"#DBFDD5", "#3B3B40"},
{"#000000", "#FFFFFE"},
{"#F4F4F4", "#36363D"},
{"#DBDBDB", "#4A4A51"},
{"#EDFAF2", "#283232"},
{"#323A3C", "#E5E5E6"},
{"#6B6B6A", "#B3B3B5"},
{"#303A3C", "#E5E5E5"},
{"#FEFFFF", "#242428"},
{"#A6A9AA", "#2D2D29"},
{"#363636", "#B2B3B5"},
{"#F0F0F1", "#404040"},
{"#9E9E9E", "#53545A"},
{"#D7E8DE", "#1F2B27"},
{"#2B3436", "#808080"},
{"#ABABAB", "#ABABAB"},
{"#D9D9D9", "#2D2D32"},
//{"#F0F0F0", "#4C4C54"},
};
std::map<wxColour, wxColour> const & StateColor::GetDarkMap()
{
return gDarkColors;
}
void StateColor::SetDarkMode(bool dark) { gDarkMode = dark; }
inline wxColour darkModeColorFor2(wxColour const &color)
{
if (!gDarkMode)
return color;
auto iter = gDarkColors.find(color);
wxFAIL(iter != gDarkColors.end());
if (iter != gDarkColors.end()) return iter->second;
return color;
}
std::map<wxColour, wxColour> revert(std::map<wxColour, wxColour> const & map)
{
std::map<wxColour, wxColour> map2;
for (auto &p : map) map2.emplace(p.second, p.first);
return map2;
}
wxColour StateColor::lightModeColorFor(wxColour const &color)
{
static std::map<wxColour, wxColour> gLightColors = revert(gDarkColors);
auto iter = gLightColors.find(color);
wxFAIL(iter != gLightColors.end());
if (iter != gLightColors.end()) return iter->second;
return color;
}
wxColour StateColor::darkModeColorFor(wxColour const &color) { return darkModeColorFor2(color); }
StateColor::StateColor(wxColour const &color) { append(color, 0); }
StateColor::StateColor(wxString const &color) { append(color, 0); }
StateColor::StateColor(unsigned long color) { append(color, 0); }
void StateColor::append(wxColour const & color, int states)
{
statesList_.push_back(states);
colors_.push_back(color);
}
void StateColor::append(wxString const & color, int states)
{
wxColour c1(color);
append(c1, states);
}
void StateColor::append(unsigned long color, int states)
{
if ((color & 0xff000000) == 0)
color |= 0xff000000;
wxColour cl; cl.SetRGBA(color & 0xff00ff00 | ((color & 0xff) << 16) | ((color >> 16) & 0xff));
append(cl, states);
}
void StateColor::clear()
{
statesList_.clear();
colors_.clear();
}
int StateColor::states() const
{
int states = 0;
for (auto s : statesList_) states |= s;
states = (states & 0xffff) | (states >> 16);
if (takeFocusedAsHovered_ && (states & Hovered))
states |= Focused;
return states;
}
wxColour StateColor::defaultColor() {
return colorForStates(0);
}
wxColour StateColor::colorForStates(int states)
{
bool focused = takeFocusedAsHovered_ && (states & Focused);
for (int i = 0; i < statesList_.size(); ++i) {
int s = statesList_[i];
int on = s & 0xffff;
int off = s >> 16;
if ((on & states) == on && (off & ~states) == off) {
return darkModeColorFor2(colors_[i]);
}
if (focused && (on & Hovered)) {
on |= Focused;
on &= ~Hovered;
if ((on & states) == on && (off & ~states) == off) {
return darkModeColorFor2(colors_[i]);
}
}
}
return wxColour(0, 0, 0, 0);
}
wxColour StateColor::colorForStatesNoDark(int states)
{
bool focused = takeFocusedAsHovered_ && (states & Focused);
for (int i = 0; i < statesList_.size(); ++i) {
int s = statesList_[i];
int on = s & 0xffff;
int off = s >> 16;
if ((on & states) == on && (off & ~states) == off) {
return colors_[i];
}
if (focused && (on & Hovered)) {
on |= Focused;
on &= ~Hovered;
if ((on & states) == on && (off & ~states) == off) {
return colors_[i];
}
}
}
return wxColour(0, 0, 0, 0);
}
int StateColor::colorIndexForStates(int states)
{
for (int i = 0; i < statesList_.size(); ++i) {
int s = statesList_[i];
int on = s & 0xffff;
int off = s >> 16;
if ((on & states) == on && (off & ~states) == off) { return i; }
}
return -1;
}
bool StateColor::setColorForStates(wxColour const &color, int states)
{
for (int i = 0; i < statesList_.size(); ++i) {
if (statesList_[i] == states) {
colors_[i] = color;
return true;
}
}
return false;
}
void StateColor::setTakeFocusedAsHovered(bool set) { takeFocusedAsHovered_ = set; }

View File

@@ -0,0 +1,95 @@
#ifndef slic3r_GUI_StateColor_hpp_
#define slic3r_GUI_StateColor_hpp_
#include <wx/colour.h>
#include <map>
class StateColor
{
public:
enum State {
Normal = 0,
Enabled = 1,
Checked = 2,
Focused = 4,
Hovered = 8,
Pressed = 16,
Disabled = 1 << 16,
NotChecked = 2 << 16,
NotFocused = 4 << 16,
NotHovered = 8 << 16,
NotPressed = 16 << 16,
};
public:
static void SetDarkMode(bool dark);
static std::map<wxColour, wxColour> const & GetDarkMap();
static wxColour darkModeColorFor(wxColour const &color);
static wxColour lightModeColorFor(wxColour const &color);
public:
template<typename ...Colors>
StateColor(std::pair<Colors, int>... colors) {
fill(colors...);
}
// single color
StateColor(wxColour const & color);
// single color
StateColor(wxString const &color);
// single color
StateColor(unsigned long color);
public:
void append(wxColour const & color, int states);
void append(wxString const &color, int states);
void append(unsigned long color, int states);
void clear();
public:
int count() const { return statesList_.size(); }
int states() const;
public:
wxColour defaultColor();
wxColour colorForStates(int states);
wxColour colorForStatesNoDark(int states);
int colorIndexForStates(int states);
bool setColorForStates(wxColour const & color, int states);
void setTakeFocusedAsHovered(bool set);
private:
template<typename Color, typename ...Colors>
void fill(std::pair<Color, int> color, std::pair<Colors, int>... colors) {
fillOne(color);
fill(colors...);
}
template<typename Color>
void fillOne(std::pair<Color, int> color) {
append(color.first, color.second);
}
void fill() {
}
private:
std::vector<int> statesList_;
std::vector<wxColour> colors_;
bool takeFocusedAsHovered_ = true;
};
#endif // !slic3r_GUI_StateColor_hpp_

View File

@@ -0,0 +1,293 @@
#include "WebView.hpp"
#include "slic3r/GUI/GUI_App.hpp"
#include "slic3r/Utils/MacDarkMode.hpp"
#include <wx/webviewarchivehandler.h>
#include <wx/webviewfshandler.h>
#if wxUSE_WEBVIEW_EDGE
#include <wx/msw/webview_edge.h>
#elif defined(__WXMAC__)
#include <wx/osx/webview_webkit.h>
#endif
#include <wx/uri.h>
#if defined(__WIN32__) || defined(__WXMAC__)
#include "wx/private/jsscriptwrapper.h"
#endif
#ifdef __WIN32__
#include <WebView2.h>
#elif defined __linux__
#include <gtk/gtk.h>
#define WEBKIT_API
struct WebKitWebView;
struct WebKitJavascriptResult;
extern "C" {
WEBKIT_API void webkit_web_view_run_javascript(
WebKitWebView *web_view, const gchar *script, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data);
WEBKIT_API WebKitJavascriptResult *webkit_web_view_run_javascript_finish(WebKitWebView *web_view, GAsyncResult *result, GError **error);
WEBKIT_API void webkit_javascript_result_unref(WebKitJavascriptResult *js_result);
}
#endif
#ifdef __WIN32__
class WebViewEdge : public wxWebViewEdge
{
public:
bool SetUserAgent(const wxString &userAgent)
{
ICoreWebView2 *webView2 = (ICoreWebView2 *) GetNativeBackend();
if (webView2) {
ICoreWebView2Settings *settings;
HRESULT hr = webView2->get_Settings(&settings);
if (hr == S_OK) {
ICoreWebView2Settings2 *settings2;
hr = settings->QueryInterface(&settings2);
if (hr == S_OK) {
settings2->put_UserAgent(userAgent.wc_str());
settings2->Release();
return true;
}
}
return false;
}
pendingUserAgent = userAgent;
return true;
}
void DoGetClientSize(int *x, int *y) const override
{
if (!pendingUserAgent.empty()) {
auto thiz = const_cast<WebViewEdge *>(this);
auto userAgent = std::move(thiz->pendingUserAgent);
thiz->pendingUserAgent.clear();
thiz->SetUserAgent(userAgent);
}
wxWebViewEdge::DoGetClientSize(x, y);
};
private:
wxString pendingUserAgent;
};
#elif defined __WXOSX__
class WebViewWebKit : public wxWebViewWebKit
{
~WebViewWebKit() override { RemoveScriptMessageHandler("wx"); }
};
#endif
class FakeWebView : public wxWebView
{
virtual bool Create(wxWindow * parent,
wxWindowID id,
const wxString &url,
const wxPoint & pos,
const wxSize & size,
long style,
const wxString &name) override
{
return false;
}
virtual wxString GetCurrentTitle() const override { return wxString(); }
virtual wxString GetCurrentURL() const override { return wxString(); }
virtual bool IsBusy() const override { return false; }
virtual bool IsEditable() const override { return false; }
virtual void LoadURL(const wxString &url) override {}
virtual void Print() override {}
virtual void RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) override {}
virtual void Reload(wxWebViewReloadFlags flags = wxWEBVIEW_RELOAD_DEFAULT) override {}
virtual bool RunScript(const wxString &javascript, wxString *output = NULL) const override { return false; }
virtual void SetEditable(bool enable = true) override {}
virtual void Stop() override {}
virtual bool CanGoBack() const override { return false; }
virtual bool CanGoForward() const override { return false; }
virtual void GoBack() override {}
virtual void GoForward() override {}
virtual void ClearHistory() override {}
virtual void EnableHistory(bool enable = true) override {}
virtual wxVector<wxSharedPtr<wxWebViewHistoryItem>> GetBackwardHistory() override { return {}; }
virtual wxVector<wxSharedPtr<wxWebViewHistoryItem>> GetForwardHistory() override { return {}; }
virtual void LoadHistoryItem(wxSharedPtr<wxWebViewHistoryItem> item) override {}
virtual bool CanSetZoomType(wxWebViewZoomType type) const override { return false; }
virtual float GetZoomFactor() const override { return 0.0f; }
virtual wxWebViewZoomType GetZoomType() const override { return wxWebViewZoomType(); }
virtual void SetZoomFactor(float zoom) override {}
virtual void SetZoomType(wxWebViewZoomType zoomType) override {}
virtual bool CanUndo() const override { return false; }
virtual bool CanRedo() const override { return false; }
virtual void Undo() override {}
virtual void Redo() override {}
virtual void * GetNativeBackend() const override { return nullptr; }
virtual void DoSetPage(const wxString &html, const wxString &baseUrl) override {}
};
wxDEFINE_EVENT(EVT_WEBVIEW_RECREATED, wxCommandEvent);
static std::vector<wxWebView *> g_webviews;
static std::vector<wxWebView *> g_delay_webviews;
class WebViewRef : public wxObjectRefData
{
public:
WebViewRef(wxWebView *webView) : m_webView(webView) {}
~WebViewRef()
{
auto iter = std::find(g_webviews.begin(), g_webviews.end(), m_webView);
assert(iter != g_webviews.end());
if (iter != g_webviews.end())
g_webviews.erase(iter);
}
wxWebView *m_webView;
};
wxWebView *WebView::CreateWebView(wxWindow *parent, wxString const &url)
{
#if wxUSE_WEBVIEW_EDGE
// Check if a fixed version of edge is present in
// $executable_path/edge_fixed and use it
wxFileName edgeFixedDir(wxStandardPaths::Get().GetExecutablePath());
edgeFixedDir.SetFullName("");
edgeFixedDir.AppendDir("edge_fixed");
if (edgeFixedDir.DirExists()) {
wxWebViewEdge::MSWSetBrowserExecutableDir(edgeFixedDir.GetFullPath());
wxLogMessage("Using fixed edge version");
}
#endif
auto url2 = url;
#ifdef __WIN32__
url2.Replace("\\", "/");
#endif
if (!url2.empty()) {
url2 = wxURI(url2).BuildURI();
}
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": " << url2.ToUTF8();
#ifdef __WIN32__
wxWebView *webView = new WebViewEdge;
#elif defined(__WXOSX__)
wxWebView *webView = new WebViewWebKit;
#else
auto webView = wxWebView::New();
#endif
if (webView) {
//webView->SetBackgroundColour(StateColor::darkModeColorFor(*wxWHITE));
#ifdef __WIN32__
webView->SetUserAgent(wxString::Format("BBL-Slicer/v%s (%s) Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.52",
SLIC3R_VERSION, Slic3r::GUI::wxGetApp().dark_mode() ? "dark" : "light"));
webView->Create(parent, wxID_ANY, url2, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
// We register the wxfs:// protocol for testing purposes
webView->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("bbl")));
// And the memory: file system
webView->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewFSHandler("memory")));
#else
// With WKWebView handlers need to be registered before creation
webView->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewArchiveHandler("wxfs")));
// And the memory: file system
webView->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewFSHandler("memory")));
webView->Create(parent, wxID_ANY, url2, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE);
webView->SetUserAgent(
wxString::Format("BBL-Slicer/v%s (%s) Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko)",
SLIC3R_VERSION, Slic3r::GUI::wxGetApp().dark_mode() ? "dark" : "light"));
#endif
#ifdef __WXMAC__
WKWebView *wkWebView = (WKWebView *) webView->GetNativeBackend();
Slic3r::GUI::WKWebView_setTransparentBackground(wkWebView);
#endif
auto addScriptMessageHandler = [](wxWebView *webView) {
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": begin to add script message handler for wx.";
Slic3r::GUI::wxGetApp().set_adding_script_handler(true);
if (!webView->AddScriptMessageHandler("wx"))
wxLogError("Could not add script message handler");
Slic3r::GUI::wxGetApp().set_adding_script_handler(false);
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": finished add script message handler for wx.";
};
#ifndef __WIN32__
webView->CallAfter([webView, addScriptMessageHandler] {
#endif
if (Slic3r::GUI::wxGetApp().is_adding_script_handler()) {
g_delay_webviews.push_back(webView);
} else {
addScriptMessageHandler(webView);
while (!g_delay_webviews.empty()) {
auto views = std::move(g_delay_webviews);
for (auto wv : views)
addScriptMessageHandler(wv);
}
}
#ifndef __WIN32__
});
#endif
webView->EnableContextMenu(false);
} else {
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ": failed. Use fake web view.";
webView = new FakeWebView;
}
webView->SetRefData(new WebViewRef(webView));
g_webviews.push_back(webView);
return webView;
}
void WebView::LoadUrl(wxWebView *webView, wxString const &url)
{
auto url2 = url;
#ifdef __WIN32__
url2.Replace("\\", "/");
#endif
if (!url2.empty()) {
url2 = wxURI(url2).BuildURI();
}
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << url2.ToUTF8();
webView->LoadURL(url2);
}
bool WebView::RunScript(wxWebView *webView, wxString const &javascript)
{
wxLogMessage("Running JavaScript:\n%s\n", javascript);
try {
#ifdef __WIN32__
ICoreWebView2 *webView2 = (ICoreWebView2 *) webView->GetNativeBackend();
if (webView2 == nullptr)
return false;
int count = 0;
wxJSScriptWrapper wrapJS(javascript, &count);
return webView2->ExecuteScript(wrapJS.GetWrappedCode(), NULL) == 0;
#elif defined __WXMAC__
WKWebView * wkWebView = (WKWebView *) webView->GetNativeBackend();
int count = 0;
wxJSScriptWrapper wrapJS(javascript, &count);
Slic3r::GUI::WKWebView_evaluateJavaScript(wkWebView, wrapJS.GetWrappedCode(), nullptr);
return true;
#else
WebKitWebView *wkWebView = (WebKitWebView *) webView->GetNativeBackend();
webkit_web_view_run_javascript(
wkWebView, javascript.utf8_str(), NULL,
[](GObject *wkWebView, GAsyncResult *res, void *) {
GError *error = NULL;
auto result = webkit_web_view_run_javascript_finish((WebKitWebView *) wkWebView, res, &error);
if (!result)
g_error_free(error);
else
webkit_javascript_result_unref(result);
},
NULL);
return true;
#endif
} catch (std::exception &e) {
return false;
}
}
void WebView::RecreateAll()
{
for (auto webView : g_webviews) {
webView->SetUserAgent(
wxString::Format("BBL-Slicer/v%s (%s) Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko)",
SLIC3R_VERSION, Slic3r::GUI::wxGetApp().dark_mode() ? "dark" : "light"));
webView->Reload();
}
}

View File

@@ -0,0 +1,18 @@
#ifndef slic3r_GUI_WebView_hpp_
#define slic3r_GUI_WebView_hpp_
#include <wx/webview.h>
class WebView
{
public:
static wxWebView *CreateWebView(wxWindow *parent, wxString const &url);
static void LoadUrl(wxWebView * webView, wxString const &url);
static bool RunScript(wxWebView * webView, wxString const & msg);
static void RecreateAll();
};
#endif // !slic3r_GUI_WebView_hpp_