mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-07-11 11:26:34 +03:00
Compare commits
12 Commits
feature/pl
...
feat/plugi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
18388ffb6d | ||
|
|
05fa7ee56d | ||
|
|
fb2c0ef237 | ||
|
|
9a6b861ce3 | ||
|
|
e6008c0883 | ||
|
|
cfe3673b22 | ||
|
|
8ddb4db0de | ||
|
|
bc21551185 | ||
|
|
ee7339b64b | ||
|
|
a11e442f1d | ||
|
|
714fe54f77 | ||
|
|
b724cb6631 |
@@ -5,6 +5,8 @@
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>Plugins</title>
|
||||
<link rel="stylesheet" href="./styles.css" />
|
||||
<link rel="stylesheet" href="./plugin-sort.css" />
|
||||
<link rel="stylesheet" href="./plugin-search.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../../include/global.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../css/common.css" />
|
||||
<link rel="stylesheet" type="text/css" href="../css/theme.css" />
|
||||
@@ -14,10 +16,33 @@
|
||||
<script type="text/javascript" src="../js/globalapi.js"></script>
|
||||
<script type="text/javascript" src="../js/common.js"></script>
|
||||
<script src="./index.js"></script>
|
||||
<script src="./plugin-sort.js"></script>
|
||||
<script src="./plugin-search.js"></script>
|
||||
</head>
|
||||
<body onLoad="OnInit()">
|
||||
<div class="app">
|
||||
<div class="toolbar">
|
||||
<div id="pluginSearch" class="plugin-search">
|
||||
<span class="plugin-search-icon" aria-hidden="true">
|
||||
<svg viewBox="0 0 16 16" width="14" height="14" fill="none" stroke="currentColor" stroke-width="1.4">
|
||||
<circle cx="6.5" cy="6.5" r="4.5" />
|
||||
<line x1="10" y1="10" x2="14" y2="14" />
|
||||
</svg>
|
||||
</span>
|
||||
<input id="plugin_search_input" class="plugin-search-input" type="text"
|
||||
placeholder="Search plugins" autocomplete="off" spellcheck="false" aria-label="Search plugins" />
|
||||
<button id="plugin_search_clear" class="plugin-search-clear" type="button" title="Clear" aria-label="Clear search">
|
||||
<svg viewBox="0 0 16 16" width="12" height="12" fill="none" stroke="currentColor"
|
||||
stroke-width="1.6" stroke-linecap="round" aria-hidden="true">
|
||||
<line x1="5" y1="5" x2="11" y2="11" />
|
||||
<line x1="11" y1="5" x2="5" y2="11" />
|
||||
</svg>
|
||||
</button>
|
||||
<button id="plugin_search_cc" class="plugin-search-toggle" type="button"
|
||||
aria-pressed="false" title="Match case">Aa</button>
|
||||
<button id="plugin_search_w" class="plugin-search-toggle" type="button"
|
||||
aria-pressed="false" title="Match whole word"><span class="plugin-search-underline">ab</span></button>
|
||||
</div>
|
||||
<!-- <button id="open_terminal" class="ButtonStyleRegular ButtonTypeChoice left-btn">-->
|
||||
<!-- Open Terminal-->
|
||||
<!-- </button>-->
|
||||
@@ -43,9 +68,14 @@
|
||||
<section class="pane plugin-list-pane">
|
||||
<div class="hdr plugin-cols">
|
||||
<span>Activate</span>
|
||||
<span>Name</span>
|
||||
<span>Plugin Version</span>
|
||||
<span>Status</span>
|
||||
<span class="sort-th" data-sort-field="name" role="button" tabindex="0"
|
||||
title="Sort by name">Name<span class="sort-tri" aria-hidden="true"></span></span>
|
||||
<span class="sort-th" data-sort-field="version" role="button" tabindex="0"
|
||||
title="Sort by version">Plugin Version<span class="sort-tri" aria-hidden="true"></span></span>
|
||||
<span class="sort-th" data-sort-field="source" role="button" tabindex="0"
|
||||
title="Sort by source">Source<span class="sort-tri" aria-hidden="true"></span></span>
|
||||
<span class="sort-th" data-sort-field="status" role="button" tabindex="0"
|
||||
title="Sort by status">Status<span class="sort-tri" aria-hidden="true"></span></span>
|
||||
</div>
|
||||
<div id="pluginList" class="body thin-scroll"></div>
|
||||
</section>
|
||||
|
||||
@@ -11,6 +11,12 @@ const pluginInstallActions = {
|
||||
};
|
||||
|
||||
let expandedPluginIds = new Set();
|
||||
|
||||
// why: transient per-search override on top of expandedPluginIds. A search
|
||||
// auto-expands rows whose capabilities match, display-only. This lets a
|
||||
// triangle click during search collapse/reopen such a row without touching
|
||||
// the base (id -> bool).
|
||||
let searchExpandOverride = new Map();
|
||||
let selectedPluginId = "";
|
||||
let contextPluginId = "";
|
||||
let activeDetailTab = "plugin-info";
|
||||
@@ -214,6 +220,10 @@ function HandleStudio(value) {
|
||||
|
||||
if (payload.command === "list_plugins") {
|
||||
SetSelectedInstallAction(payload.install_action, false);
|
||||
if (typeof NormalizePluginSort === "function") {
|
||||
pluginSort = NormalizePluginSort(payload.sort_key, payload.sort_order);
|
||||
RenderSortHeaders();
|
||||
}
|
||||
ApplyPlugins(payload.data || []);
|
||||
} else if (payload.command === "status_message") {
|
||||
ShowStatusMessage(String(payload.message || ""), String(payload.level || "info"));
|
||||
@@ -261,7 +271,6 @@ function ApplyPlugins(plugins) {
|
||||
expandedPluginIds = new Set(Array.from(expandedPluginIds).filter((pluginKey) => pluginsById.has(pluginKey)));
|
||||
|
||||
RenderPlugins();
|
||||
SyncPluginListHeaderGutter();
|
||||
RenderDetails();
|
||||
}
|
||||
|
||||
@@ -295,6 +304,27 @@ function SyncPluginListHeaderGutter() {
|
||||
listPane.style.setProperty("--plugin-list-scrollbar-width", `${scrollbarWidth}px`);
|
||||
}
|
||||
|
||||
// why: paint matched-character ranges as <mark> without an innerHTML build
|
||||
// note: if no ranges -> return the plain text node
|
||||
function ApplyHighlight(container, text, ranges) {
|
||||
if (!ranges || !ranges.length) {
|
||||
container.appendChild(document.createTextNode(text));
|
||||
return;
|
||||
}
|
||||
let pos = 0;
|
||||
for (const [start, end] of ranges) {
|
||||
if (start > pos)
|
||||
container.appendChild(document.createTextNode(text.slice(pos, start)));
|
||||
const mark = document.createElement("mark");
|
||||
mark.className = "plugin-search-hit";
|
||||
mark.textContent = text.slice(start, end);
|
||||
container.appendChild(mark);
|
||||
pos = end;
|
||||
}
|
||||
if (pos < text.length)
|
||||
container.appendChild(document.createTextNode(text.slice(pos)));
|
||||
}
|
||||
|
||||
function RenderPlugins() {
|
||||
if (!pluginList)
|
||||
return;
|
||||
@@ -306,13 +336,32 @@ function RenderPlugins() {
|
||||
empty.className = "empty-state";
|
||||
empty.textContent = "No plugins found";
|
||||
pluginList.appendChild(empty);
|
||||
SyncPluginListHeaderGutter();
|
||||
return;
|
||||
}
|
||||
|
||||
// why: stable filter over the existing C++ sort order - no scoring, no reorder. The empty query
|
||||
// short-circuits (searching=false), leaving every existing render path untouched.
|
||||
const searching = typeof PluginSearchActive === "function" && PluginSearchActive();
|
||||
let shown = 0;
|
||||
|
||||
for (const plugin of pluginsById.values()) {
|
||||
const pluginKey = String(plugin.plugin_key || "");
|
||||
const capabilities = GetCapabilities(plugin);
|
||||
const isExpanded = expandedPluginIds.has(pluginKey) && capabilities.length > 0;
|
||||
const match = searching ? ComputePluginMatch(plugin) : null;
|
||||
if (searching && !match.matched)
|
||||
continue;
|
||||
shown++;
|
||||
|
||||
// why: transient override wins. Otherwise while searching start collapsed and auto-expand only
|
||||
// capability matches (the persistent expand state is ignored so unrelated caps don't clutter
|
||||
// results); when not searching use the persistent state. The base is never written while
|
||||
// searching, so clearing the search restores exactly what the user had.
|
||||
const open = searchExpandOverride.has(pluginKey)
|
||||
? searchExpandOverride.get(pluginKey)
|
||||
: (searching ? match.hasCapMatch : expandedPluginIds.has(pluginKey));
|
||||
const isExpanded = open && capabilities.length > 0;
|
||||
|
||||
const block = document.createElement("div");
|
||||
block.className = "plugin-block";
|
||||
block.dataset.pluginKey = pluginKey;
|
||||
@@ -327,15 +376,31 @@ function RenderPlugins() {
|
||||
row.classList.add("selected");
|
||||
|
||||
row.appendChild(CheckCell(row, plugin));
|
||||
row.appendChild(LabelCell(plugin, isExpanded, capabilities.length));
|
||||
row.appendChild(LabelCell(plugin, isExpanded, capabilities.length, match?.nameRanges));
|
||||
row.appendChild(VersionCell(plugin));
|
||||
row.appendChild(SourceCell(plugin));
|
||||
row.appendChild(StatusCell(plugin));
|
||||
|
||||
block.appendChild(row);
|
||||
if (isExpanded)
|
||||
block.appendChild(RenderCapabilityTree(plugin, capabilities));
|
||||
block.appendChild(RenderCapabilityTree(plugin, capabilities, match?.capRanges));
|
||||
pluginList.appendChild(block);
|
||||
}
|
||||
|
||||
// why: distinct from the size===0 "no plugins" state - here plugins exist but none match the query.
|
||||
if (searching && shown === 0) {
|
||||
const empty = document.createElement("div");
|
||||
empty.className = "empty-state";
|
||||
empty.appendChild(document.createTextNode('No plugins match "'));
|
||||
const term = document.createElement("b");
|
||||
term.textContent = pluginSearch.query;
|
||||
empty.appendChild(term);
|
||||
empty.appendChild(document.createTextNode('"'));
|
||||
pluginList.appendChild(empty);
|
||||
}
|
||||
|
||||
// why: recompute the scrollbar gutter on every render - search and sort re-render via RenderPlugins
|
||||
SyncPluginListHeaderGutter();
|
||||
}
|
||||
|
||||
function GetErrorText(plugin) {
|
||||
@@ -474,7 +539,7 @@ function CheckCell(row, plugin) {
|
||||
return checkCell;
|
||||
}
|
||||
|
||||
function LabelCell(plugin, isExpanded = false, capabilityCount = 0) {
|
||||
function LabelCell(plugin, isExpanded = false, capabilityCount = 0, nameRanges = null) {
|
||||
const labelCell = document.createElement("span");
|
||||
labelCell.className = "label-cell";
|
||||
|
||||
@@ -505,7 +570,7 @@ function LabelCell(plugin, isExpanded = false, capabilityCount = 0) {
|
||||
nameWrap.className = "plugin-name-wrap";
|
||||
|
||||
const labelElement = document.createElement(hasCloudLink ? "a" : "span");
|
||||
labelElement.textContent = pluginLabelText;
|
||||
ApplyHighlight(labelElement, pluginLabelText, nameRanges);
|
||||
labelElement.className = "plugin-name-text";
|
||||
|
||||
if (hasCloudLink) {
|
||||
@@ -523,25 +588,38 @@ function LabelCell(plugin, isExpanded = false, capabilityCount = 0) {
|
||||
nameWrap.appendChild(countBadge);
|
||||
}
|
||||
labelCell.appendChild(nameWrap);
|
||||
labelCell.appendChild(SourceBadge(plugin.source));
|
||||
|
||||
return labelCell;
|
||||
}
|
||||
|
||||
function RenderCapabilityTree(plugin, capabilities) {
|
||||
function SourceCell(plugin) {
|
||||
const cell = document.createElement("span");
|
||||
const normalized = String(plugin.source || "").toLowerCase();
|
||||
const variant = (normalized === "mine" || normalized === "subscribed") ? normalized : "local";
|
||||
cell.className = `source-cell source-${variant}`;
|
||||
|
||||
const sourceLabel = document.createElement("span");
|
||||
sourceLabel.className = "source-label";
|
||||
sourceLabel.textContent = SourceLabel(plugin.source);
|
||||
cell.appendChild(sourceLabel);
|
||||
|
||||
return cell;
|
||||
}
|
||||
|
||||
function RenderCapabilityTree(plugin, capabilities, capRanges = null) {
|
||||
const tree = document.createElement("div");
|
||||
tree.className = "capabilities-tree";
|
||||
tree.setAttribute("role", "group");
|
||||
tree.setAttribute("aria-label", "Capabilities");
|
||||
|
||||
capabilities.forEach((capability, index) => {
|
||||
tree.appendChild(RenderCapabilityRow(plugin, capability, index === capabilities.length - 1));
|
||||
tree.appendChild(RenderCapabilityRow(plugin, capability, index === capabilities.length - 1, capRanges));
|
||||
});
|
||||
|
||||
return tree;
|
||||
}
|
||||
|
||||
function RenderCapabilityRow(plugin, capability, isLast) {
|
||||
function RenderCapabilityRow(plugin, capability, isLast, capRanges = null) {
|
||||
const row = document.createElement("div");
|
||||
row.className = "capability-row plugin-cols";
|
||||
row.classList.toggle("is-last", isLast);
|
||||
@@ -556,7 +634,8 @@ function RenderCapabilityRow(plugin, capability, isLast) {
|
||||
branch.setAttribute("aria-hidden", "true");
|
||||
const name = document.createElement("span");
|
||||
name.className = "capability-name";
|
||||
name.textContent = String(capability?.name || "") || "-";
|
||||
const capabilityLabel = String(capability?.name || "");
|
||||
ApplyHighlight(name, capabilityLabel || "-", capRanges?.get(capabilityLabel));
|
||||
nameCell.appendChild(branch);
|
||||
nameCell.appendChild(name);
|
||||
row.appendChild(nameCell);
|
||||
@@ -566,6 +645,11 @@ function RenderCapabilityRow(plugin, capability, isLast) {
|
||||
typeCell.textContent = String(capability?.type || "-");
|
||||
row.appendChild(typeCell);
|
||||
|
||||
// why: empty placeholder for the new Source column so the run-action cell stays under Status.
|
||||
const sourceSpacer = document.createElement("span");
|
||||
sourceSpacer.className = "capability-source-cell";
|
||||
row.appendChild(sourceSpacer);
|
||||
|
||||
const actionsCell = document.createElement("span");
|
||||
actionsCell.className = "capability-actions-cell";
|
||||
const capabilityName = String(capability?.name || "");
|
||||
@@ -898,7 +982,12 @@ function OnPluginListClick(event) {
|
||||
|
||||
const pluginKey = String(block.dataset.pluginKey || "");
|
||||
selectedPluginId = pluginKey;
|
||||
if (expandedPluginIds.has(pluginKey))
|
||||
// why: during a search the triangle writes to the transient override (read from the on-screen open
|
||||
// state), so an auto-expanded row collapses without touching the saved layout. With no search
|
||||
// active, toggle the persistent base exactly as before.
|
||||
if (typeof PluginSearchActive === "function" && PluginSearchActive())
|
||||
searchExpandOverride.set(pluginKey, !block.classList.contains("expanded"));
|
||||
else if (expandedPluginIds.has(pluginKey))
|
||||
expandedPluginIds.delete(pluginKey);
|
||||
else
|
||||
expandedPluginIds.add(pluginKey);
|
||||
|
||||
138
resources/web/dialog/PluginsDialog/plugin-search.css
Normal file
138
resources/web/dialog/PluginsDialog/plugin-search.css
Normal file
@@ -0,0 +1,138 @@
|
||||
.plugin-search {
|
||||
--search-width: 300px;
|
||||
flex: 0 0 auto;
|
||||
width: var(--search-width);
|
||||
/* why: the search bar takes the auto margin (pinned far left); sort + Refresh + Install cluster right. */
|
||||
margin-right: auto;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 2px;
|
||||
/* why: match the compact toolbar buttons (.toolbar .ButtonTypeChoice is 26px) so the row aligns. */
|
||||
height: 26px;
|
||||
padding: 0 6px;
|
||||
box-sizing: border-box;
|
||||
background: var(--panel);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.plugin-search:focus-within {
|
||||
border-color: var(--main-color);
|
||||
box-shadow: 0 0 0 2px rgba(0, 150, 136, 0.25);
|
||||
}
|
||||
|
||||
.plugin-search-icon {
|
||||
display: inline-flex;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.plugin-search-input {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
background: transparent;
|
||||
border: 0;
|
||||
outline: 0;
|
||||
color: var(--text);
|
||||
font: inherit;
|
||||
}
|
||||
|
||||
.plugin-search-input::placeholder {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
/* why: compact squarish toggles - min-width keeps single-char W from collapsing while two-char Cc
|
||||
grows just enough to fit; tight horizontal padding keeps them from reading as wide pills. */
|
||||
.plugin-search-toggle {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-width: 22px;
|
||||
height: 22px;
|
||||
padding: 0 1px;
|
||||
border: 1px solid transparent;
|
||||
border-radius: 4px;
|
||||
background: transparent;
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
/* why: set the clear x apart from the Cc/W pair while keeping the pair itself tight - margins on the
|
||||
toggles only, so the icon-to-text gap is left as-is. clear+toggle = Cc, toggle+toggle = W. */
|
||||
.plugin-search-clear + .plugin-search-toggle {
|
||||
margin-left: 4px;
|
||||
}
|
||||
.plugin-search-toggle + .plugin-search-toggle {
|
||||
margin-left: -1px;
|
||||
}
|
||||
|
||||
/* why: subtle round clear affordance - a small muted disc, not a square button. The x is an inline SVG
|
||||
(not a text glyph) so it centers pixel-perfectly regardless of the platform font. JS flips its
|
||||
visibility (not display) so its reserved slot never reflows Cc/W. */
|
||||
.plugin-search-clear {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 10px;
|
||||
height: 10px;
|
||||
padding: 0;
|
||||
border: 0;
|
||||
border-radius: 50%;
|
||||
color: var(--muted);
|
||||
cursor: pointer;
|
||||
visibility: hidden;
|
||||
background: rgba(127, 127, 127, 0.20);
|
||||
}
|
||||
|
||||
.plugin-search-clear svg {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.plugin-search-clear:hover {
|
||||
color: var(--text);
|
||||
background: rgba(127, 127, 127, 0.45);
|
||||
}
|
||||
|
||||
.plugin-search-toggle:hover {
|
||||
color: var(--text);
|
||||
background: var(--row-hover);
|
||||
}
|
||||
|
||||
.plugin-search-toggle.on {
|
||||
color: var(--button-fg-light, #fff);
|
||||
background: var(--main-color);
|
||||
border-color: var(--main-color-hover);
|
||||
}
|
||||
|
||||
/* "ab" over a bracket drawn by ::after box draws it */
|
||||
.plugin-search-underline {
|
||||
position: relative;
|
||||
display: inline-block;
|
||||
padding-bottom: 2px;
|
||||
}
|
||||
.plugin-search-underline::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
/* why: ticks stick out past the outer edges of "a"/"b" on each side. */
|
||||
left: -1px;
|
||||
right: -1px;
|
||||
bottom: 0;
|
||||
/* why: short ticks + bottom rule, tucked near the baseline. */
|
||||
height: 2px;
|
||||
border: 1px solid currentColor;
|
||||
border-top: 0;
|
||||
/* why: soften the two joints where the ticks meet the bottom rule. */
|
||||
border-bottom-left-radius: 2px;
|
||||
border-bottom-right-radius: 2px;
|
||||
}
|
||||
|
||||
/* why: reuse the themed warn tokens so matched-char marks track light and dark automatically.
|
||||
note: no padding/margin/border - a highlight must not change text width, else rows reflow. */
|
||||
mark.plugin-search-hit {
|
||||
border-radius: 2px;
|
||||
background: var(--plugin-status-warn-bg);
|
||||
color: var(--plugin-status-warn);
|
||||
}
|
||||
156
resources/web/dialog/PluginsDialog/plugin-search.js
Normal file
156
resources/web/dialog/PluginsDialog/plugin-search.js
Normal file
@@ -0,0 +1,156 @@
|
||||
const pluginSearch = { query: "", caseSensitive: false, wholeWord: false };
|
||||
|
||||
function PluginSearchActive() {
|
||||
return pluginSearch.query.length > 0;
|
||||
}
|
||||
|
||||
// --- matcher: fold per-character on the fly so matched offsets stay in ORIGINAL coordinates ---
|
||||
// why: highlighting marks slices of the original string; a separate folded string would desync offsets.
|
||||
function FoldChar(ch) {
|
||||
return ch.normalize("NFD").replace(/\p{Diacritic}/gu, ""); // accents always folded (both Cc states)
|
||||
}
|
||||
function Norm(ch, caseSensitive) {
|
||||
const folded = FoldChar(ch);
|
||||
return caseSensitive ? folded : folded.toLowerCase(); // Cc controls case only
|
||||
}
|
||||
function EscapeRegExp(value) {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
function MatchText(text, query) {
|
||||
if (!query)
|
||||
return [];
|
||||
return pluginSearch.wholeWord ? WholeWordRanges(text, query) : FuzzyRanges(text, query);
|
||||
}
|
||||
|
||||
// Fuzzy: ordered subsequence. Builds ranges in original coordinates, merging adjacent runs on the fly.
|
||||
function FuzzyRanges(text, query) {
|
||||
const caseSensitive = pluginSearch.caseSensitive;
|
||||
const needle = Array.from(query).map((ch) => Norm(ch, caseSensitive)).join("");
|
||||
const ranges = [];
|
||||
let qi = 0;
|
||||
for (let i = 0; i < text.length && qi < needle.length; i++) {
|
||||
if (Norm(text[i], caseSensitive) === needle[qi]) {
|
||||
const last = ranges[ranges.length - 1];
|
||||
if (last && last[1] === i)
|
||||
last[1] = i + 1;
|
||||
else
|
||||
ranges.push([i, i + 1]);
|
||||
qi++;
|
||||
}
|
||||
}
|
||||
return qi === needle.length ? ranges : null;
|
||||
}
|
||||
|
||||
// Whole word: literal \b-bounded match that bypasses fuzzy; Cc still applies. The per-char fold keeps the
|
||||
// haystack length-aligned to the original text, so regex indices map straight back to original offsets.
|
||||
// note: one-to-many folds (ligatures, eszett) shift offsets by a char; rare in plugin names, cosmetic only.
|
||||
function WholeWordRanges(text, query) {
|
||||
const caseSensitive = pluginSearch.caseSensitive;
|
||||
const haystack = Array.from(text).map((ch) => Norm(ch, caseSensitive)).join("");
|
||||
const needle = Array.from(query).map((ch) => Norm(ch, caseSensitive)).join("");
|
||||
if (!needle)
|
||||
return null;
|
||||
const re = new RegExp(`\\b${EscapeRegExp(needle)}\\b`, "g");
|
||||
const ranges = [];
|
||||
let match;
|
||||
// why: needle is non-empty, so \b-bounded matches are never zero-length - no empty-match guard needed.
|
||||
while ((match = re.exec(haystack)) !== null)
|
||||
ranges.push([match.index, match.index + match[0].length]);
|
||||
return ranges.length > 0 ? ranges : null;
|
||||
}
|
||||
|
||||
// Per-plugin evaluator consumed by RenderPlugins. The name text mirrors LabelCell's pluginLabelText so
|
||||
// highlight offsets line up with what is rendered. Capability names exist for loaded plugins only.
|
||||
function ComputePluginMatch(plugin) {
|
||||
const name = plugin.label || plugin.name || plugin.plugin_id || "";
|
||||
const nameRanges = MatchText(name, pluginSearch.query);
|
||||
const capabilities = Array.isArray(plugin?.capabilities) ? plugin.capabilities : [];
|
||||
const capRanges = new Map();
|
||||
for (const capability of capabilities) {
|
||||
const key = String(capability?.name || "");
|
||||
const ranges = MatchText(key, pluginSearch.query);
|
||||
if (ranges)
|
||||
capRanges.set(key, ranges);
|
||||
}
|
||||
return {
|
||||
matched: !!nameRanges || capRanges.size > 0,
|
||||
nameRanges,
|
||||
capRanges,
|
||||
hasCapMatch: capRanges.size > 0,
|
||||
};
|
||||
}
|
||||
|
||||
// --- widget wiring ---
|
||||
let pluginSearchInput = null;
|
||||
let pluginSearchClear = null;
|
||||
let pluginSearchCc = null;
|
||||
let pluginSearchW = null;
|
||||
|
||||
function InitPluginSearch() {
|
||||
pluginSearchInput = document.getElementById("plugin_search_input");
|
||||
pluginSearchClear = document.getElementById("plugin_search_clear");
|
||||
pluginSearchCc = document.getElementById("plugin_search_cc");
|
||||
pluginSearchW = document.getElementById("plugin_search_w");
|
||||
if (!pluginSearchInput)
|
||||
return;
|
||||
|
||||
// why: common.js installs a document-level onkeydown that cancels the default action of every key
|
||||
// (returnValue=false) to block webview shortcuts; on the way up it also swallows typing. Stop the
|
||||
// field's keydowns from bubbling to it so the input stays editable, leaving the global guard intact.
|
||||
pluginSearchInput.addEventListener("keydown", (event) => event.stopPropagation());
|
||||
|
||||
pluginSearchInput.addEventListener("input", OnPluginSearchInput);
|
||||
pluginSearchClear?.addEventListener("click", ClearPluginSearch);
|
||||
pluginSearchCc?.addEventListener("click", () => TogglePluginSearchFlag(pluginSearchCc, "caseSensitive"));
|
||||
pluginSearchW?.addEventListener("click", () => TogglePluginSearchFlag(pluginSearchW, "wholeWord"));
|
||||
SyncPluginSearchClear();
|
||||
}
|
||||
|
||||
function OnPluginSearchInput() {
|
||||
pluginSearch.query = pluginSearchInput.value;
|
||||
// why: emptying the box by editing (not just the x) also ends the search - drop the transient vetoes.
|
||||
if (!pluginSearch.query)
|
||||
ClearSearchExpandOverride();
|
||||
SyncPluginSearchClear();
|
||||
RenderPluginsIfReady();
|
||||
}
|
||||
|
||||
function ClearPluginSearch() {
|
||||
pluginSearch.query = "";
|
||||
if (pluginSearchInput)
|
||||
pluginSearchInput.value = "";
|
||||
ClearSearchExpandOverride();
|
||||
SyncPluginSearchClear();
|
||||
RenderPluginsIfReady();
|
||||
pluginSearchInput?.focus();
|
||||
}
|
||||
|
||||
function TogglePluginSearchFlag(button, key) {
|
||||
pluginSearch[key] = !pluginSearch[key];
|
||||
button.classList.toggle("on", pluginSearch[key]);
|
||||
button.setAttribute("aria-pressed", String(pluginSearch[key]));
|
||||
RenderPluginsIfReady();
|
||||
}
|
||||
|
||||
// why: toggle visibility (not display / the hidden attribute) so the x keeps its reserved slot and
|
||||
// showing or hiding it never reflows the Cc / W buttons.
|
||||
function SyncPluginSearchClear() {
|
||||
if (pluginSearchClear)
|
||||
pluginSearchClear.style.visibility = pluginSearch.query.length ? "visible" : "hidden";
|
||||
}
|
||||
|
||||
// why: searchExpandOverride lives in index.js; guard so this module stays loadable on its own.
|
||||
function ClearSearchExpandOverride() {
|
||||
if (typeof searchExpandOverride !== "undefined")
|
||||
searchExpandOverride.clear();
|
||||
}
|
||||
|
||||
function RenderPluginsIfReady() {
|
||||
if (typeof RenderPlugins === "function")
|
||||
RenderPlugins();
|
||||
}
|
||||
|
||||
// why: guarded so the module can be loaded in headless syntax checks; mirrors plugin-sort.js.
|
||||
if (typeof document !== "undefined")
|
||||
document.addEventListener("DOMContentLoaded", InitPluginSearch);
|
||||
42
resources/web/dialog/PluginsDialog/plugin-sort.css
Normal file
42
resources/web/dialog/PluginsDialog/plugin-sort.css
Normal file
@@ -0,0 +1,42 @@
|
||||
/* why: sort affordance lives on the list column headers, not a toolbar dropdown. */
|
||||
|
||||
.hdr .sort-th {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
cursor: pointer;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.hdr .sort-th .sort-tri {
|
||||
width: 0;
|
||||
height: 0;
|
||||
flex: none;
|
||||
border-left: 4px solid transparent;
|
||||
border-right: 4px solid transparent;
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* faint up-triangle hint on hover, only while the column is not the active sort */
|
||||
.hdr .sort-th:hover .sort-tri {
|
||||
display: block;
|
||||
border-bottom: 5px solid var(--muted);
|
||||
}
|
||||
|
||||
/* active column wins over the hover hint (same specificity, declared later) */
|
||||
.hdr .sort-th[data-sort="asc"] .sort-tri {
|
||||
display: block;
|
||||
border-bottom: 5px solid var(--text);
|
||||
border-top: 0;
|
||||
}
|
||||
|
||||
.hdr .sort-th[data-sort="desc"] .sort-tri {
|
||||
display: block;
|
||||
border-top: 5px solid var(--text);
|
||||
border-bottom: 0;
|
||||
}
|
||||
|
||||
.hdr .sort-th[data-sort="asc"],
|
||||
.hdr .sort-th[data-sort="desc"] {
|
||||
color: var(--text);
|
||||
}
|
||||
67
resources/web/dialog/PluginsDialog/plugin-sort.js
Normal file
67
resources/web/dialog/PluginsDialog/plugin-sort.js
Normal file
@@ -0,0 +1,67 @@
|
||||
// why: C++ owns ordering; this file only sends and reflects sort state.
|
||||
|
||||
const DEFAULT_PLUGIN_SORT = { key: "none", order: "asc" };
|
||||
// note: SORT_FIELDS are the clickable columns. "none" is the baseline/cleared state, not a field -
|
||||
// it is special-cased in NormalizePluginSort and produced by CyclePluginSort's third click.
|
||||
const SORT_FIELDS = new Set(["status", "name", "source", "version"]);
|
||||
let pluginSort = { ...DEFAULT_PLUGIN_SORT };
|
||||
|
||||
// why: C++ returns canonical sort state; guard stale or malformed values before reflecting them.
|
||||
function NormalizePluginSort(sortKey, sortOrder) {
|
||||
const key = String(sortKey || "");
|
||||
return {
|
||||
key: key === "none" ? "none" : (SORT_FIELDS.has(key) ? key : DEFAULT_PLUGIN_SORT.key),
|
||||
order: sortOrder === "desc" ? "desc" : DEFAULT_PLUGIN_SORT.order,
|
||||
};
|
||||
}
|
||||
|
||||
function RequestPluginSort(sortKey, sortOrder) {
|
||||
pluginSort = NormalizePluginSort(sortKey, sortOrder);
|
||||
RenderSortHeaders();
|
||||
|
||||
if (typeof SendMessage === "function")
|
||||
SendMessage("set_plugin_sort", {
|
||||
sort_key: pluginSort.key,
|
||||
sort_order: pluginSort.order,
|
||||
});
|
||||
}
|
||||
|
||||
// why: one click per column cycles asc -> desc -> clear; setting any column clears the previous
|
||||
// one for free because C++ (and pluginSort) only ever hold a single key.
|
||||
function CyclePluginSort(field) {
|
||||
if (!SORT_FIELDS.has(field))
|
||||
return;
|
||||
if (pluginSort.key !== field)
|
||||
RequestPluginSort(field, "asc");
|
||||
else if (pluginSort.order === "asc")
|
||||
RequestPluginSort(field, "desc");
|
||||
else
|
||||
RequestPluginSort("none", "asc"); // third click: back to baseline
|
||||
}
|
||||
|
||||
// why: paints the sort indicator for headers
|
||||
// e.g., when user clicks triangle to change sort order, or change to sort by a new different field
|
||||
function RenderSortHeaders() {
|
||||
document.querySelectorAll(".hdr .sort-th").forEach((th) => {
|
||||
// "" | "asc" | "desc" - renders the triangle via plugin-sort.css [data-sort=...].
|
||||
th.dataset.sort = th.dataset.sortField === pluginSort.key ? pluginSort.order : "";
|
||||
});
|
||||
}
|
||||
|
||||
function InitSortHeaders() {
|
||||
document.querySelectorAll(".hdr .sort-th").forEach((th) => {
|
||||
th.addEventListener("click", () => CyclePluginSort(th.dataset.sortField));
|
||||
// note: role="button" cells need Enter/Space to match the old dropdown's keyboard access.
|
||||
th.addEventListener("keydown", (event) => {
|
||||
if (event.key === "Enter" || event.key === " ") {
|
||||
event.preventDefault();
|
||||
CyclePluginSort(th.dataset.sortField);
|
||||
}
|
||||
});
|
||||
});
|
||||
RenderSortHeaders(); // paint the initial state (baseline = no triangle)
|
||||
}
|
||||
|
||||
// why: guarded so the module can be loaded in headless syntax checks.
|
||||
if (typeof document !== "undefined")
|
||||
document.addEventListener("DOMContentLoaded", InitSortHeaders);
|
||||
@@ -187,7 +187,27 @@ body {
|
||||
}
|
||||
|
||||
.plugin-cols {
|
||||
grid-template-columns: 70px minmax(0, 2.8fr) minmax(120px, 0.9fr) minmax(140px, 1fr);
|
||||
grid-template-columns: 70px minmax(0, 2.4fr) minmax(110px, 0.85fr) minmax(96px, 0.7fr) minmax(130px, 0.95fr);
|
||||
}
|
||||
|
||||
/* Source is its own (sortable) column, shown as colored text (mirrors .status-cell), not a chip. */
|
||||
.source-cell {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.source-cell.source-mine {
|
||||
color: var(--plugin-source-mine-text);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.source-cell.source-subscribed {
|
||||
color: var(--plugin-source-subscribed-text);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.source-cell.source-local {
|
||||
color: var(--plugin-source-neutral-text);
|
||||
}
|
||||
|
||||
/* Center the "Activate" header over the centered checkbox in each row. */
|
||||
@@ -835,11 +855,6 @@ body {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
/* In a list row, sit at the right edge of the Name column (the name fills the rest). */
|
||||
.label-cell .plugin-source-badge {
|
||||
margin-right: 4px;
|
||||
}
|
||||
|
||||
.plugin-source-badge.source-local {
|
||||
background: var(--plugin-source-neutral-bg);
|
||||
color: var(--plugin-source-neutral-text);
|
||||
|
||||
@@ -109,6 +109,9 @@ set(SLIC3R_GUI_SOURCES
|
||||
GUI/Downloader.hpp
|
||||
GUI/DownloadProgressDialog.cpp
|
||||
GUI/DownloadProgressDialog.hpp
|
||||
GUI/PluginSource.hpp
|
||||
GUI/PluginSort.hpp
|
||||
GUI/PluginStatus.hpp
|
||||
GUI/PluginPickerDialog.cpp
|
||||
GUI/PluginPickerDialog.hpp
|
||||
GUI/PluginsDialog.cpp
|
||||
@@ -601,6 +604,8 @@ set(SLIC3R_GUI_SOURCES
|
||||
plugin/CloudPluginService.hpp
|
||||
plugin/PluginFsUtils.cpp
|
||||
plugin/PluginFsUtils.hpp
|
||||
plugin/PluginConfig.cpp
|
||||
plugin/PluginConfig.hpp
|
||||
plugin/PluginCatalog.cpp
|
||||
plugin/PluginCatalog.hpp
|
||||
plugin/PluginLoader.cpp
|
||||
|
||||
210
src/slic3r/GUI/PluginSort.hpp
Normal file
210
src/slic3r/GUI/PluginSort.hpp
Normal file
@@ -0,0 +1,210 @@
|
||||
#pragma once
|
||||
|
||||
#include "PluginSource.hpp"
|
||||
#include "PluginStatus.hpp"
|
||||
|
||||
#include "libslic3r/Semver.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Slic3r::GUI
|
||||
{
|
||||
enum class PluginSortKey
|
||||
{
|
||||
Status,
|
||||
Name,
|
||||
Source,
|
||||
Version,
|
||||
// why: neutral "no column selected" state - clearing a header sort returns here and the
|
||||
// list falls to compare_plugin_base_order only. Header UI reaches it via the asc/desc/clear cycle.
|
||||
None
|
||||
};
|
||||
|
||||
enum class PluginSortOrder
|
||||
{
|
||||
Asc,
|
||||
Desc
|
||||
};
|
||||
|
||||
inline std::string to_string(PluginSortKey sort_key)
|
||||
{
|
||||
switch (sort_key)
|
||||
{
|
||||
case PluginSortKey::Status: return "status";
|
||||
case PluginSortKey::Name: return "name";
|
||||
case PluginSortKey::Source: return "source";
|
||||
case PluginSortKey::Version: return "version";
|
||||
case PluginSortKey::None: return "none";
|
||||
}
|
||||
|
||||
return "status";
|
||||
}
|
||||
|
||||
inline std::string to_string(PluginSortOrder sort_order)
|
||||
{
|
||||
return sort_order == PluginSortOrder::Desc ? "desc" : "asc";
|
||||
}
|
||||
|
||||
inline PluginSortKey plugin_sort_key_from_string(const std::string& sort_key, PluginSortKey fallback)
|
||||
{
|
||||
if (sort_key == "status")
|
||||
return PluginSortKey::Status;
|
||||
if (sort_key == "name")
|
||||
return PluginSortKey::Name;
|
||||
if (sort_key == "source")
|
||||
return PluginSortKey::Source;
|
||||
if (sort_key == "version")
|
||||
return PluginSortKey::Version;
|
||||
if (sort_key == "none")
|
||||
return PluginSortKey::None;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
inline PluginSortOrder plugin_sort_order_from_string(const std::string& sort_order, PluginSortOrder fallback)
|
||||
{
|
||||
if (sort_order == "asc")
|
||||
return PluginSortOrder::Asc;
|
||||
if (sort_order == "desc")
|
||||
return PluginSortOrder::Desc;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
// Natural, case-insensitive ASCII compare returning -1 / 0 / +1. Digit runs compare by
|
||||
// numeric value; other chars compare lowercased; on a prefix tie the shorter string is less.
|
||||
// e.g. "item2" < "item10" (2 < 10, not '2' > '1')
|
||||
// "Camera" == "camera" (case ignored)
|
||||
// "app" < "apple" (prefix is shorter)
|
||||
// "1" < "01" (equal value, fewer leading zeros wins the tie)
|
||||
// note: ASCII only - no locale/Unicode; accented or non-Latin names fall back to byte order.
|
||||
inline int compare_ascii_case_insensitive_natural(const std::string& lhs, const std::string& rhs)
|
||||
{
|
||||
std::size_t li = 0;
|
||||
std::size_t ri = 0;
|
||||
|
||||
while (li < lhs.size() && ri < rhs.size())
|
||||
{
|
||||
const unsigned char lc = static_cast<unsigned char>(lhs[li]);
|
||||
const unsigned char rc = static_cast<unsigned char>(rhs[ri]);
|
||||
|
||||
if (std::isdigit(lc) && std::isdigit(rc))
|
||||
{
|
||||
const std::size_t lhs_digit_begin = li;
|
||||
const std::size_t rhs_digit_begin = ri;
|
||||
while (li < lhs.size() && std::isdigit(static_cast<unsigned char>(lhs[li])))
|
||||
++li;
|
||||
while (ri < rhs.size() && std::isdigit(static_cast<unsigned char>(rhs[ri])))
|
||||
++ri;
|
||||
|
||||
const std::string_view lhs_run(lhs.data() + lhs_digit_begin, li - lhs_digit_begin);
|
||||
const std::string_view rhs_run(rhs.data() + rhs_digit_begin, ri - rhs_digit_begin);
|
||||
// why: digit runs compare numerically; leading zeros only break exact ties ("1" < "01").
|
||||
const std::string_view lhs_num = lhs_run.substr(std::min(lhs_run.find_first_not_of('0'), lhs_run.size()));
|
||||
const std::string_view rhs_num = rhs_run.substr(std::min(rhs_run.find_first_not_of('0'), rhs_run.size()));
|
||||
if (lhs_num.size() != rhs_num.size())
|
||||
return lhs_num.size() < rhs_num.size() ? -1 : 1;
|
||||
if (const int cmp = lhs_num.compare(rhs_num); cmp != 0)
|
||||
return cmp;
|
||||
// note: fewer-leading-zeros-first is our convention, not an industry standard (impls
|
||||
// diverge here); it only matters as a deterministic total order for unstable std::sort.
|
||||
if (lhs_run.size() != rhs_run.size())
|
||||
return lhs_run.size() < rhs_run.size() ? -1 : 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
const int lower_lhs = std::tolower(lc);
|
||||
const int lower_rhs = std::tolower(rc);
|
||||
if (lower_lhs != lower_rhs)
|
||||
return lower_lhs < lower_rhs ? -1 : 1;
|
||||
|
||||
++li;
|
||||
++ri;
|
||||
}
|
||||
|
||||
if (li == lhs.size() && ri == rhs.size())
|
||||
return 0;
|
||||
return li == lhs.size() ? -1 : 1;
|
||||
}
|
||||
|
||||
// Neutral baseline order: the whole order when no column is sorted, and the tie-breaker under
|
||||
// every primary sort key. Name-first so the default view is intuitively alphabetical:
|
||||
// name, then source, then status, then type, with plugin_key as the final deterministic tie.
|
||||
// e.g. with no column sorted the list reads A..Z by name.
|
||||
template <class PluginItem>
|
||||
int compare_plugin_base_order(const PluginItem& lhs, const PluginItem& rhs)
|
||||
{
|
||||
if (const int cmp = compare_ascii_case_insensitive_natural(lhs.display_name, rhs.display_name); cmp != 0)
|
||||
return cmp;
|
||||
if (const int cmp = static_cast<int>(lhs.source) - static_cast<int>(rhs.source); cmp != 0)
|
||||
return cmp;
|
||||
if (const int cmp = static_cast<int>(lhs.status) - static_cast<int>(rhs.status); cmp != 0)
|
||||
return cmp;
|
||||
if (const int cmp = lhs.type_key.compare(rhs.type_key); cmp != 0)
|
||||
return cmp;
|
||||
return lhs.plugin_key.compare(rhs.plugin_key);
|
||||
}
|
||||
|
||||
// Compares two version strings returning -1 / 0 / +1. Uses Slic3r::Semver (the same parser the
|
||||
// plugin catalog's update-available check uses); on unparseable input falls back to the natural
|
||||
// compare so the order stays deterministic.
|
||||
// e.g. "1.2.0" < "1.10.0" (numeric), "1.0.0-rc1" < "1.0.0" (semver prerelease rule).
|
||||
inline int compare_plugin_version(const std::string& lhs, const std::string& rhs)
|
||||
{
|
||||
const auto lhs_semver = Semver::parse(lhs);
|
||||
const auto rhs_semver = Semver::parse(rhs);
|
||||
if (lhs_semver && rhs_semver)
|
||||
{
|
||||
if (*lhs_semver < *rhs_semver) return -1;
|
||||
if (*rhs_semver < *lhs_semver) return 1;
|
||||
return 0;
|
||||
}
|
||||
return compare_ascii_case_insensitive_natural(lhs, rhs);
|
||||
}
|
||||
|
||||
// Compares two items by the chosen primary key, returning -1 / 0 / +1. Status and Source
|
||||
// rank by enum ordinal (the declared dialog priority); Name uses the natural compare above.
|
||||
// e.g. Status: an enabled item (lower ordinal) sorts before a disabled one.
|
||||
// Name: "Plugin 2" sorts before "Plugin 10".
|
||||
template <class PluginItem>
|
||||
int compare_plugin_sort_key(const PluginItem& lhs, const PluginItem& rhs, PluginSortKey sort_key)
|
||||
{
|
||||
switch (sort_key)
|
||||
{
|
||||
case PluginSortKey::Status:
|
||||
// why: PluginStatus/PluginSource declare the dialog sort priority as their ordinal order.
|
||||
return static_cast<int>(lhs.status) - static_cast<int>(rhs.status);
|
||||
case PluginSortKey::Name:
|
||||
return compare_ascii_case_insensitive_natural(lhs.display_name, rhs.display_name);
|
||||
case PluginSortKey::Source:
|
||||
return static_cast<int>(lhs.source) - static_cast<int>(rhs.source);
|
||||
case PluginSortKey::Version:
|
||||
return compare_plugin_version(lhs.sort_version, rhs.sort_version);
|
||||
case PluginSortKey::None:
|
||||
// why: no primary key - every pair ties here so sort_plugin_items_for_dialog falls
|
||||
// straight to the ascending base order (direction is irrelevant for the baseline).
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Sorts the dialog list in place by primary key + direction. Ties always fall back to the
|
||||
// ascending base order, so the result is deterministic regardless of the primary direction.
|
||||
// e.g. sort_key=Name, order=Desc -> names Z..A, but equal names keep the stable base order.
|
||||
template <class PluginItem>
|
||||
void sort_plugin_items_for_dialog(std::vector<PluginItem>& items, PluginSortKey sort_key,
|
||||
PluginSortOrder sort_order)
|
||||
{
|
||||
std::sort(items.begin(), items.end(),
|
||||
[sort_key, sort_order](const PluginItem& lhs, const PluginItem& rhs)
|
||||
{
|
||||
if (const int cmp = compare_plugin_sort_key(lhs, rhs, sort_key); cmp != 0)
|
||||
return sort_order == PluginSortOrder::Asc ? cmp < 0 : cmp > 0;
|
||||
// why: ties fall back to ascending base order regardless of the primary direction.
|
||||
return compare_plugin_base_order(lhs, rhs) < 0;
|
||||
});
|
||||
}
|
||||
} // namespace Slic3r::GUI
|
||||
29
src/slic3r/GUI/PluginSource.hpp
Normal file
29
src/slic3r/GUI/PluginSource.hpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Slic3r
|
||||
{
|
||||
namespace GUI
|
||||
{
|
||||
enum class PluginSource
|
||||
{
|
||||
// IMPORTANT: ordinal order is the Plugins dialog Source sort priority.
|
||||
Mine,
|
||||
Subscribed,
|
||||
Local
|
||||
};
|
||||
|
||||
inline std::string to_string(PluginSource source)
|
||||
{
|
||||
switch (source)
|
||||
{
|
||||
case PluginSource::Mine: return "mine";
|
||||
case PluginSource::Subscribed: return "subscribed";
|
||||
case PluginSource::Local: return "local";
|
||||
}
|
||||
|
||||
return "local";
|
||||
}
|
||||
}
|
||||
} // namespace Slic3r::GUI
|
||||
31
src/slic3r/GUI/PluginStatus.hpp
Normal file
31
src/slic3r/GUI/PluginStatus.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Slic3r
|
||||
{
|
||||
namespace GUI
|
||||
{
|
||||
enum class PluginStatus
|
||||
{
|
||||
// IMPORTANT: ordinal order is the Plugins dialog Status sort priority.
|
||||
Activated,
|
||||
Error,
|
||||
Inactive,
|
||||
Loading
|
||||
};
|
||||
|
||||
inline std::string to_string(PluginStatus status)
|
||||
{
|
||||
switch (status)
|
||||
{
|
||||
case PluginStatus::Activated: return "Activated";
|
||||
case PluginStatus::Error: return "Error";
|
||||
case PluginStatus::Inactive: return "Inactive";
|
||||
case PluginStatus::Loading: return "Loading";
|
||||
}
|
||||
|
||||
return "Inactive";
|
||||
}
|
||||
}
|
||||
} // namespace Slic3r::GUI
|
||||
@@ -25,7 +25,6 @@
|
||||
#include <boost/log/trivial.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
@@ -90,6 +89,7 @@ struct PluginDialogItem
|
||||
std::string version;
|
||||
std::string installed_version;
|
||||
std::string latest_version;
|
||||
std::string sort_version; // Version shown in the row (installed if installed, else latest); used by the Version sort.
|
||||
std::string type_label;
|
||||
std::string type_key;
|
||||
std::string sharing_token;
|
||||
@@ -103,7 +103,7 @@ struct PluginDialogItem
|
||||
PluginUpdateStatus update_status = PluginUpdateStatus::Normal;
|
||||
std::string error_text;
|
||||
bool has_error = false;
|
||||
bool loaded = false;
|
||||
bool is_loaded = false;
|
||||
bool loading = false;
|
||||
|
||||
// Installation and capability flags
|
||||
@@ -187,27 +187,62 @@ void refresh_plugin_catalog_blocking(bool fetch_cloud)
|
||||
}
|
||||
}
|
||||
|
||||
std::string to_string(PluginSource source)
|
||||
std::string to_string(PluginUpdateStatus status);
|
||||
nlohmann::json build_context_actions_payload(const PluginAvailableActions& available_actions);
|
||||
|
||||
nlohmann::json build_plugin_payload_item(const PluginDialogItem& dialog_item)
|
||||
{
|
||||
switch (source) {
|
||||
case PluginSource::Local: return "local";
|
||||
case PluginSource::Mine: return "mine";
|
||||
case PluginSource::Subscribed: return "subscribed";
|
||||
nlohmann::json payload_item;
|
||||
payload_item["plugin_key"] = dialog_item.plugin_key;
|
||||
payload_item["plugin_id"] = dialog_item.plugin_id;
|
||||
payload_item["name"] = dialog_item.display_name;
|
||||
payload_item["description"] = dialog_item.description;
|
||||
payload_item["author"] = dialog_item.author;
|
||||
payload_item["version"] = dialog_item.version;
|
||||
payload_item["type"] = dialog_item.type_label;
|
||||
payload_item["type_key"] = dialog_item.type_key;
|
||||
payload_item["types"] = dialog_item.type_labels;
|
||||
|
||||
nlohmann::json caps = nlohmann::json::array();
|
||||
for (const PluginCapabilityView& capability : dialog_item.capabilities) {
|
||||
nlohmann::json c;
|
||||
c["name"] = capability.name;
|
||||
c["type"] = capability.type_label;
|
||||
c["type_key"] = capability.type_key;
|
||||
c["enabled"] = capability.enabled;
|
||||
c["can_toggle"] = capability.can_toggle;
|
||||
c["can_run"] = capability.can_run;
|
||||
caps.push_back(std::move(c));
|
||||
}
|
||||
payload_item["capabilities"] = std::move(caps);
|
||||
|
||||
return "local";
|
||||
}
|
||||
|
||||
std::string to_string(PluginStatus status)
|
||||
{
|
||||
switch (status) {
|
||||
case PluginStatus::Inactive: return "Inactive";
|
||||
case PluginStatus::Error: return "Error";
|
||||
case PluginStatus::Loading: return "Loading";
|
||||
case PluginStatus::Activated: return "Activated";
|
||||
nlohmann::json changelog = nlohmann::json::array();
|
||||
for (const PluginChangelogView& entry : dialog_item.changelog) {
|
||||
nlohmann::json c;
|
||||
c["version"] = entry.version;
|
||||
c["changelog"] = entry.changelog;
|
||||
c["created_time"] = entry.created_time;
|
||||
changelog.push_back(std::move(c));
|
||||
}
|
||||
payload_item["changelog"] = std::move(changelog);
|
||||
|
||||
return "Inactive";
|
||||
payload_item["label"] = dialog_item.display_name;
|
||||
payload_item["source"] = to_string(dialog_item.source);
|
||||
payload_item["status"] = to_string(dialog_item.status);
|
||||
payload_item["error"] = dialog_item.error_text;
|
||||
payload_item["update_status"] = to_string(dialog_item.update_status);
|
||||
payload_item["unauthorized"] = dialog_item.unauthorized;
|
||||
payload_item["context_actions"] = build_context_actions_payload(dialog_item.available_actions);
|
||||
payload_item["update_available"] = dialog_item.update_status == PluginUpdateStatus::UpdateAvailable;
|
||||
payload_item["can_toggle"] = dialog_item.available_actions.can_toggle;
|
||||
payload_item["has_script_capability"] = dialog_item.has_script_capability;
|
||||
payload_item["can_run_script"] = dialog_item.can_run_script;
|
||||
payload_item["sharing_token"] = dialog_item.sharing_token;
|
||||
payload_item["thumbnail_url"] = dialog_item.thumbnail_url;
|
||||
payload_item["installed"] = dialog_item.has_local_package;
|
||||
payload_item["installed_version"] = dialog_item.installed_version;
|
||||
payload_item["latest_version"] = dialog_item.latest_version;
|
||||
return payload_item;
|
||||
}
|
||||
|
||||
std::string to_string(PluginUpdateStatus status)
|
||||
@@ -285,6 +320,9 @@ PluginDialogItem build_plugin_dialog_item(const PluginDescriptor& descriptor)
|
||||
(descriptor.installed_version.empty() ? descriptor.version : descriptor.installed_version) :
|
||||
std::string{};
|
||||
item.latest_version = descriptor.latest_available_version();
|
||||
// why: sort by the same version the row displays (GetDisplayVersion in index.js) - installed when
|
||||
// installed, otherwise latest - so the Version sort matches what the user sees.
|
||||
item.sort_version = item.installed_version.empty() ? item.latest_version : item.installed_version;
|
||||
item.type_label = descriptor.type_label();
|
||||
item.type_key = plugin_capability_type_to_string(descriptor.primary_capability_type());
|
||||
// "types" is the display-only compatibility list. Cloud plugins show the raw labels the
|
||||
@@ -311,9 +349,9 @@ PluginDialogItem build_plugin_dialog_item(const PluginDescriptor& descriptor)
|
||||
item.has_local_package = descriptor.has_local_package();
|
||||
item.unauthorized = descriptor.is_unauthorized();
|
||||
item.has_script_capability = descriptor.has_capability_type(Slic3r::PluginCapabilityType::Script);
|
||||
item.loaded = loader.is_plugin_loaded(descriptor.plugin_key);
|
||||
item.is_loaded = loader.is_plugin_loaded(descriptor.plugin_key);
|
||||
item.loading = loader.is_plugin_load_in_progress(descriptor.plugin_key);
|
||||
if (item.loaded) {
|
||||
if (item.is_loaded) {
|
||||
for (const auto& cap : loader.get_loaded_plugin_capabilities(descriptor.plugin_key)) {
|
||||
if (cap) {
|
||||
item.capabilities.push_back({cap->name, plugin_capability_type_display_name(cap->type),
|
||||
@@ -342,7 +380,7 @@ PluginDialogItem build_plugin_dialog_item(const PluginDescriptor& descriptor)
|
||||
item.status = PluginStatus::Loading;
|
||||
else if (item.has_error)
|
||||
item.status = PluginStatus::Error;
|
||||
else if (item.loaded)
|
||||
else if (item.is_loaded)
|
||||
item.status = PluginStatus::Activated;
|
||||
else
|
||||
item.status = PluginStatus::Inactive;
|
||||
@@ -352,7 +390,7 @@ PluginDialogItem build_plugin_dialog_item(const PluginDescriptor& descriptor)
|
||||
[](const PluginCapabilityView& capability) {
|
||||
return capability.type_key == "script" && capability.enabled;
|
||||
});
|
||||
item.can_run_script = descriptor.is_metadata_valid() && !descriptor.has_error() && item.has_script_capability && item.loaded &&
|
||||
item.can_run_script = descriptor.is_metadata_valid() && !descriptor.has_error() && item.has_script_capability && item.is_loaded &&
|
||||
!item.loading && has_enabled_script;
|
||||
for (PluginCapabilityView& capability : item.capabilities) {
|
||||
capability.can_run = item.can_run_script && capability.type_key == "script" && capability.enabled;
|
||||
@@ -478,6 +516,8 @@ void PluginsDialog::on_script_message(const nlohmann::json& payload)
|
||||
open_plugin_on_cloud(payload.value("sharing_token", ""));
|
||||
} else if (command == "open_plugin_hub") {
|
||||
open_plugin_hub();
|
||||
} else if (command == "set_plugin_sort") {
|
||||
set_plugin_sort(payload.value("sort_key", ""), payload.value("sort_order", ""));
|
||||
} else if (command == "set_plugin_install_action") {
|
||||
const std::string action = payload.value("action", "");
|
||||
if (action == "explore" || action == "install-local")
|
||||
@@ -487,92 +527,41 @@ void PluginsDialog::on_script_message(const nlohmann::json& payload)
|
||||
|
||||
void PluginsDialog::send_plugins() { call_web_handler(build_plugins_payload()); }
|
||||
|
||||
void PluginsDialog::set_plugin_sort(const std::string& sort_key, const std::string& sort_order)
|
||||
{
|
||||
m_plugin_sort_key = plugin_sort_key_from_string(sort_key, m_plugin_sort_key);
|
||||
m_plugin_sort_order = plugin_sort_order_from_string(sort_order, m_plugin_sort_order);
|
||||
send_plugins();
|
||||
}
|
||||
|
||||
nlohmann::json PluginsDialog::build_plugins_payload() const
|
||||
{
|
||||
nlohmann::json response;
|
||||
response["command"] = "list_plugins";
|
||||
response["install_action"] = s_selected_plugin_install_action;
|
||||
response["sort_key"] = to_string(m_plugin_sort_key);
|
||||
response["sort_order"] = to_string(m_plugin_sort_order);
|
||||
response["data"] = nlohmann::json::array();
|
||||
|
||||
auto append_plugin = [&response](const PluginDescriptor& row) {
|
||||
const PluginDialogItem dialog_item = build_plugin_dialog_item(row);
|
||||
|
||||
nlohmann::json payload_item;
|
||||
payload_item["plugin_key"] = dialog_item.plugin_key;
|
||||
payload_item["plugin_id"] = dialog_item.plugin_id;
|
||||
payload_item["name"] = row.name;
|
||||
payload_item["description"] = dialog_item.description;
|
||||
payload_item["author"] = dialog_item.author;
|
||||
payload_item["version"] = dialog_item.version;
|
||||
payload_item["installed_version"] = dialog_item.installed_version;
|
||||
payload_item["latest_version"] = dialog_item.latest_version;
|
||||
payload_item["installed"] = dialog_item.has_local_package;
|
||||
payload_item["type"] = dialog_item.type_label;
|
||||
payload_item["type_key"] = dialog_item.type_key;
|
||||
payload_item["types"] = dialog_item.type_labels;
|
||||
nlohmann::json caps = nlohmann::json::array();
|
||||
for (const PluginCapabilityView& capability : dialog_item.capabilities) {
|
||||
nlohmann::json c;
|
||||
c["name"] = capability.name;
|
||||
c["type"] = capability.type_label;
|
||||
c["type_key"] = capability.type_key;
|
||||
c["enabled"] = capability.enabled;
|
||||
c["can_toggle"] = capability.can_toggle;
|
||||
c["can_run"] = capability.can_run;
|
||||
caps.push_back(std::move(c));
|
||||
}
|
||||
payload_item["capabilities"] = std::move(caps);
|
||||
nlohmann::json changelog = nlohmann::json::array();
|
||||
for (const PluginChangelogView& entry : dialog_item.changelog) {
|
||||
nlohmann::json c;
|
||||
c["version"] = entry.version;
|
||||
c["changelog"] = entry.changelog;
|
||||
c["created_time"] = entry.created_time;
|
||||
changelog.push_back(std::move(c));
|
||||
}
|
||||
payload_item["changelog"] = std::move(changelog);
|
||||
payload_item["label"] = dialog_item.display_name;
|
||||
payload_item["source"] = to_string(dialog_item.source);
|
||||
payload_item["status"] = to_string(dialog_item.status);
|
||||
payload_item["error"] = dialog_item.error_text;
|
||||
payload_item["update_status"] = to_string(dialog_item.update_status);
|
||||
payload_item["unauthorized"] = dialog_item.unauthorized;
|
||||
payload_item["context_actions"] = build_context_actions_payload(dialog_item.available_actions);
|
||||
payload_item["update_available"] = dialog_item.update_status == PluginUpdateStatus::UpdateAvailable;
|
||||
payload_item["can_toggle"] = dialog_item.available_actions.can_toggle;
|
||||
payload_item["has_script_capability"] = dialog_item.has_script_capability;
|
||||
payload_item["can_run_script"] = dialog_item.can_run_script;
|
||||
payload_item["sharing_token"] = dialog_item.sharing_token;
|
||||
payload_item["thumbnail_url"] = dialog_item.thumbnail_url;
|
||||
response["data"].push_back(std::move(payload_item));
|
||||
};
|
||||
|
||||
const auto& catalog = PluginManager::instance().get_catalog();
|
||||
const auto valid = catalog.get_all_plugin_descriptors();
|
||||
const auto invalid = catalog.get_invalid_plugins();
|
||||
BOOST_LOG_TRIVIAL(info) << "Prepared " << valid.size() + invalid.size() << " plugin rows for Plugins dialog";
|
||||
|
||||
std::vector<PluginDialogItem> items;
|
||||
items.reserve(valid.size() + invalid.size());
|
||||
|
||||
for (const PluginDescriptor& row : valid)
|
||||
append_plugin(row);
|
||||
items.push_back(build_plugin_dialog_item(row));
|
||||
|
||||
for (const PluginDescriptor& row : invalid)
|
||||
append_plugin(row);
|
||||
items.push_back(build_plugin_dialog_item(row));
|
||||
|
||||
auto sort_value = [](const nlohmann::json& payload_item, const char* key) { return payload_item.value(key, std::string{}); };
|
||||
// In-place sort
|
||||
sort_plugin_items_for_dialog(items, m_plugin_sort_key, m_plugin_sort_order);
|
||||
|
||||
std::sort(response["data"].begin(), response["data"].end(), [&sort_value](const nlohmann::json& lhs, const nlohmann::json& rhs) {
|
||||
const std::string lhs_source = sort_value(lhs, "source");
|
||||
const std::string rhs_source = sort_value(rhs, "source");
|
||||
if (lhs_source != rhs_source)
|
||||
return lhs_source < rhs_source;
|
||||
|
||||
const std::string lhs_type = sort_value(lhs, "type_key");
|
||||
const std::string rhs_type = sort_value(rhs, "type_key");
|
||||
if (lhs_type != rhs_type)
|
||||
return lhs_type < rhs_type;
|
||||
|
||||
return sort_value(lhs, "name") < sort_value(rhs, "name");
|
||||
});
|
||||
for (const PluginDialogItem& item : items)
|
||||
response["data"].push_back(build_plugin_payload_item(item));
|
||||
|
||||
return response;
|
||||
}
|
||||
@@ -1274,7 +1263,7 @@ void PluginsDialog::delete_mine_local_and_cloud_plugin(const std::string& plugin
|
||||
|
||||
// delete_mine_local_and_cloud_plugin already updated the in-memory catalog
|
||||
// (finalize_cloud_plugin_removal removes the row and, when a local package existed,
|
||||
// re-syncs the cloud list itself), so a UI refresh is sufficient here — an extra
|
||||
// re-syncs the cloud list itself), so a UI refresh is sufficient here - an extra
|
||||
// clearing rescan + cloud fetch would be redundant.
|
||||
send_plugins();
|
||||
show_status(wxString::Format(_L("Deleted \"%s\"."), plugin_name), "success");
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#define slic3r_PluginsDialog_hpp_
|
||||
|
||||
#include "Widgets/WebViewHostDialog.hpp"
|
||||
#include "PluginSource.hpp"
|
||||
#include "PluginStatus.hpp"
|
||||
#include "PluginSort.hpp"
|
||||
#include "slic3r/plugin/PluginDescriptor.hpp"
|
||||
|
||||
#include <exception>
|
||||
@@ -27,21 +30,6 @@ enum class PluginCapabilityType;
|
||||
|
||||
namespace GUI {
|
||||
|
||||
enum class PluginSource
|
||||
{
|
||||
Local,
|
||||
Mine,
|
||||
Subscribed
|
||||
};
|
||||
|
||||
enum class PluginStatus
|
||||
{
|
||||
Inactive,
|
||||
Error,
|
||||
Loading,
|
||||
Activated
|
||||
};
|
||||
|
||||
class PluginsDialog : public Slic3r::GUI::WebViewHostDialog
|
||||
{
|
||||
public:
|
||||
@@ -63,6 +51,7 @@ private:
|
||||
void on_script_message(const nlohmann::json& payload) override;
|
||||
|
||||
void send_plugins();
|
||||
void set_plugin_sort(const std::string& sort_key, const std::string& sort_order);
|
||||
nlohmann::json build_plugins_payload() const;
|
||||
|
||||
bool get_descriptor(const std::string& plugin_key, Slic3r::PluginDescriptor& descriptor) const;
|
||||
@@ -221,6 +210,8 @@ private:
|
||||
}
|
||||
|
||||
std::function<void()> m_open_terminal_dlg_fn;
|
||||
PluginSortKey m_plugin_sort_key = PluginSortKey::None;
|
||||
PluginSortOrder m_plugin_sort_order = PluginSortOrder::Asc;
|
||||
|
||||
// Serializes run_script_plugin. With main-thread execution a plugin's orca.host.ui modal
|
||||
// (message/show_dialog) or the result message box pumps a nested event loop, which could
|
||||
|
||||
@@ -68,17 +68,22 @@ bool is_inside_allowed_root(const std::filesystem::path& candidate, const std::f
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
thread_local std::string PluginAuditManager::m_current_plugin_key = "";
|
||||
thread_local std::string PluginAuditManager::m_current_capability_name = "";
|
||||
thread_local PluginAuditManager::AuditMode PluginAuditManager::m_audit_mode = PluginAuditManager::AuditMode::Loading;
|
||||
thread_local std::vector<std::filesystem::path> PluginAuditManager::m_scoped_allowed_roots;
|
||||
thread_local bool PluginAuditManager::m_has_last_violation = false;
|
||||
thread_local AuditViolation PluginAuditManager::m_last_violation;
|
||||
|
||||
ScopedPluginAuditContext::ScopedPluginAuditContext(const std::string& plugin_key, PluginAuditManager::AuditMode mode)
|
||||
ScopedPluginAuditContext::ScopedPluginAuditContext(const std::string& plugin_key,
|
||||
const std::string& capability_name,
|
||||
PluginAuditManager::AuditMode mode)
|
||||
: m_previous_id(PluginAuditManager::instance().current_plugin())
|
||||
, m_previous_capability(PluginAuditManager::instance().current_capability())
|
||||
, m_previous_mode(PluginAuditManager::instance().audit_mode())
|
||||
, m_previous_scoped_roots(PluginAuditManager::m_scoped_allowed_roots)
|
||||
{
|
||||
PluginAuditManager::instance().set_current_plugin(plugin_key);
|
||||
PluginAuditManager::instance().set_current_capability(capability_name);
|
||||
PluginAuditManager::instance().set_audit_mode(mode);
|
||||
PluginAuditManager::m_scoped_allowed_roots.clear();
|
||||
}
|
||||
@@ -86,6 +91,7 @@ ScopedPluginAuditContext::ScopedPluginAuditContext(const std::string& plugin_key
|
||||
ScopedPluginAuditContext::~ScopedPluginAuditContext()
|
||||
{
|
||||
PluginAuditManager::instance().set_current_plugin(m_previous_id);
|
||||
PluginAuditManager::instance().set_current_capability(m_previous_capability);
|
||||
PluginAuditManager::instance().set_audit_mode(m_previous_mode);
|
||||
PluginAuditManager::m_scoped_allowed_roots = std::move(m_previous_scoped_roots);
|
||||
}
|
||||
@@ -106,6 +112,12 @@ std::string PluginAuditManager::current_plugin() const { return m_current_plugin
|
||||
|
||||
void PluginAuditManager::clear_current_plugin() { m_current_plugin_key.clear(); }
|
||||
|
||||
void PluginAuditManager::set_current_capability(const std::string& capability_name) { m_current_capability_name = capability_name; }
|
||||
|
||||
std::string PluginAuditManager::current_capability() const { return m_current_capability_name; }
|
||||
|
||||
void PluginAuditManager::clear_current_capability() { m_current_capability_name.clear(); }
|
||||
|
||||
void PluginAuditManager::add_global_allowed_root(const std::filesystem::path& root)
|
||||
{
|
||||
if (root.empty())
|
||||
|
||||
@@ -39,6 +39,14 @@ public:
|
||||
std::string current_plugin() const;
|
||||
void clear_current_plugin();
|
||||
|
||||
// --- current-capability context (thread_local) ---
|
||||
// The capability whose method is currently executing, within the current plugin. Empty
|
||||
// while a plugin-wide call runs, and during capture (get_name/get_type), where the
|
||||
// capability has no cached name yet.
|
||||
void set_current_capability(const std::string& capability_name);
|
||||
std::string current_capability() const;
|
||||
void clear_current_capability();
|
||||
|
||||
// --- allowed-roots registry ---
|
||||
void add_global_allowed_root(const std::filesystem::path& root);
|
||||
void add_scoped_allowed_root(const std::filesystem::path& root);
|
||||
@@ -75,6 +83,7 @@ private:
|
||||
static int audit_hook(const char* event, PyObject* args, void* user_data);
|
||||
|
||||
static thread_local std::string m_current_plugin_key;
|
||||
static thread_local std::string m_current_capability_name;
|
||||
static thread_local AuditMode m_audit_mode;
|
||||
static thread_local std::vector<std::filesystem::path> m_scoped_allowed_roots;
|
||||
static thread_local bool m_has_last_violation;
|
||||
@@ -84,12 +93,15 @@ private:
|
||||
std::vector<std::filesystem::path> m_global_allowed_roots;
|
||||
};
|
||||
|
||||
// RAII guard that sets the current plugin key and restores the previous one.
|
||||
// RAII guard that sets the current plugin key and capability name, restoring the previous
|
||||
// pair on scope exit. `capability_name` may be empty for calls that are not scoped to a
|
||||
// single capability.
|
||||
class ScopedPluginAuditContext
|
||||
{
|
||||
public:
|
||||
explicit ScopedPluginAuditContext(
|
||||
const std::string& plugin_key,
|
||||
const std::string& capability_name = {},
|
||||
PluginAuditManager::AuditMode mode = PluginAuditManager::AuditMode::Loading);
|
||||
|
||||
~ScopedPluginAuditContext();
|
||||
@@ -99,6 +111,7 @@ public:
|
||||
|
||||
private:
|
||||
std::string m_previous_id;
|
||||
std::string m_previous_capability;
|
||||
PluginAuditManager::AuditMode m_previous_mode;
|
||||
std::vector<std::filesystem::path> m_previous_scoped_roots;
|
||||
};
|
||||
|
||||
@@ -279,7 +279,7 @@ std::vector<std::string> PluginCatalog::get_plugin_directories() const
|
||||
};
|
||||
|
||||
// Local plugins: {data_dir}/orca_plugins/
|
||||
add_or_create_dir(fs::path(data_dir()) / "orca_plugins");
|
||||
add_or_create_dir(get_orca_plugins_dir());
|
||||
|
||||
// Cloud plugins: {data_dir}/orca_plugins/_subscribed/{user_id}/
|
||||
if (!cloud_plugin_dir_name.empty())
|
||||
|
||||
255
src/slic3r/plugin/PluginConfig.cpp
Normal file
255
src/slic3r/plugin/PluginConfig.cpp
Normal file
@@ -0,0 +1,255 @@
|
||||
#include "PluginConfig.hpp"
|
||||
|
||||
#include <pybind11/pybind11.h>
|
||||
|
||||
#include <boost/format.hpp>
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <boost/nowide/fstream.hpp>
|
||||
|
||||
#include <slic3r/plugin/PluginAuditManager.hpp>
|
||||
#include <slic3r/plugin/PluginManager.hpp>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr const char* KEY_ENTRIES = "config";
|
||||
constexpr const char* KEY_PLUGIN = "plugin_key";
|
||||
constexpr const char* KEY_CAPABILITY = "capability";
|
||||
constexpr const char* KEY_VERSION = "plugin_version";
|
||||
constexpr const char* KEY_CAP_CONFIG = "cap_config";
|
||||
|
||||
std::string string_field(const nlohmann::json& entry, const char* key)
|
||||
{
|
||||
const auto it = entry.find(key);
|
||||
return it != entry.end() && it->is_string() ? it->get<std::string>() : std::string();
|
||||
}
|
||||
|
||||
// Rejects entries missing an identity, which could never be looked up again.
|
||||
bool entry_to_config(const nlohmann::json& entry, BaseConfig& out)
|
||||
{
|
||||
if (!entry.is_object())
|
||||
return false;
|
||||
|
||||
out.plugin_key = string_field(entry, KEY_PLUGIN);
|
||||
out.capability_name = string_field(entry, KEY_CAPABILITY);
|
||||
out.plugin_version = string_field(entry, KEY_VERSION);
|
||||
if (out.empty())
|
||||
return false;
|
||||
|
||||
const auto cap_config = entry.find(KEY_CAP_CONFIG);
|
||||
out.config = cap_config != entry.end() ? *cap_config : nlohmann::json::object();
|
||||
return true;
|
||||
}
|
||||
|
||||
nlohmann::json config_to_entry(const BaseConfig& config)
|
||||
{
|
||||
return nlohmann::json{
|
||||
{KEY_PLUGIN, config.plugin_key},
|
||||
{KEY_CAPABILITY, config.capability_name},
|
||||
{KEY_VERSION, config.plugin_version},
|
||||
{KEY_CAP_CONFIG, config.config},
|
||||
};
|
||||
}
|
||||
|
||||
// The version of the plugin package currently running. PluginDescriptor::version is
|
||||
// overwritten with the latest cloud version when a cloud merge happens, so it can name a
|
||||
// version that is not the one on disk; installed_version is what actually loaded.
|
||||
std::string running_plugin_version(const std::string& plugin_key)
|
||||
{
|
||||
PluginDescriptor descriptor;
|
||||
if (!PluginManager::instance().get_catalog().try_get_valid_plugin_descriptor(plugin_key, descriptor))
|
||||
return {};
|
||||
return descriptor.installed_version.empty() ? descriptor.version : descriptor.installed_version;
|
||||
}
|
||||
|
||||
// Identifies the capability whose Python method is currently on the stack. Both halves are
|
||||
// published by ScopedPluginAuditContext, which every C++ -> Python trampoline call opens.
|
||||
// A call arriving without them did not come through a capability, so it has no config to
|
||||
// address and we refuse it rather than reading or clobbering some other capability's entry.
|
||||
std::pair<std::string, std::string> calling_capability(const char* api_name)
|
||||
{
|
||||
const PluginAuditManager& audit = PluginAuditManager::instance();
|
||||
|
||||
std::pair<std::string, std::string> id{audit.current_plugin(), audit.current_capability()};
|
||||
if (id.first.empty() || id.second.empty())
|
||||
throw std::runtime_error(std::string(api_name) + "() must be called from a plugin capability method");
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void PluginConfig::load()
|
||||
{
|
||||
const std::string path = plugin_config_file();
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_storage.clear();
|
||||
m_dirty = false;
|
||||
|
||||
boost::system::error_code ec;
|
||||
if (!boost::filesystem::exists(path, ec))
|
||||
return;
|
||||
|
||||
nlohmann::json root;
|
||||
try {
|
||||
boost::nowide::ifstream ifs(path.c_str());
|
||||
ifs >> root;
|
||||
} catch (const std::exception& err) {
|
||||
BOOST_LOG_TRIVIAL(error) << "PluginConfig: cannot read " << path << ": " << err.what() << "; starting with an empty config";
|
||||
return;
|
||||
}
|
||||
|
||||
const auto entries = root.find(KEY_ENTRIES);
|
||||
if (entries == root.end() || !entries->is_array()) {
|
||||
BOOST_LOG_TRIVIAL(warning) << "PluginConfig: " << path << " has no \"" << KEY_ENTRIES << "\" array; starting with an empty config";
|
||||
return;
|
||||
}
|
||||
|
||||
for (const auto& entry : *entries) {
|
||||
BaseConfig config;
|
||||
if (!entry_to_config(entry, config)) {
|
||||
BOOST_LOG_TRIVIAL(warning) << "PluginConfig: skipping entry without a plugin key and capability name";
|
||||
continue;
|
||||
}
|
||||
m_storage[{config.plugin_key, config.capability_name}] = std::move(config);
|
||||
}
|
||||
}
|
||||
|
||||
void PluginConfig::save()
|
||||
{
|
||||
const std::string path = plugin_config_file();
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
|
||||
nlohmann::json root;
|
||||
root[KEY_ENTRIES] = nlohmann::json::array();
|
||||
for (const auto& [id, config] : m_storage)
|
||||
root[KEY_ENTRIES].push_back(config_to_entry(config));
|
||||
|
||||
boost::system::error_code ec;
|
||||
boost::filesystem::create_directories(boost::filesystem::path(path).parent_path(), ec);
|
||||
if (ec) {
|
||||
BOOST_LOG_TRIVIAL(error) << "PluginConfig: cannot create the plugin directory: " << ec.message();
|
||||
return;
|
||||
}
|
||||
|
||||
// Write to a PID-suffixed file and rename it into place, so a crash mid-write cannot
|
||||
// truncate an existing config. Same approach as AppConfig::save().
|
||||
const std::string path_pid = (boost::format("%1%.%2%") % path % get_current_pid()).str();
|
||||
|
||||
boost::nowide::ofstream file;
|
||||
file.open(path_pid, std::ios::out | std::ios::trunc);
|
||||
file << root.dump(1, '\t') << std::endl;
|
||||
file.close();
|
||||
if (file.fail()) {
|
||||
BOOST_LOG_TRIVIAL(error) << "PluginConfig: failed to write " << path_pid << "; keeping the existing config";
|
||||
return;
|
||||
}
|
||||
|
||||
if (const std::error_code rename_ec = rename_file(path_pid, path)) {
|
||||
BOOST_LOG_TRIVIAL(error) << "PluginConfig: failed to move " << path_pid << " onto " << path << ": " << rename_ec.message();
|
||||
return;
|
||||
}
|
||||
|
||||
m_dirty = false;
|
||||
}
|
||||
|
||||
void PluginConfig::save_config(const std::string& plugin_key,
|
||||
const std::string& capability_name,
|
||||
const std::string& version,
|
||||
const nlohmann::json& config)
|
||||
{ save_config({plugin_key, capability_name, version, config}); }
|
||||
|
||||
bool PluginConfig::save_config_text(const std::string& plugin_key,
|
||||
const std::string& capability_name,
|
||||
const std::string& version,
|
||||
const std::string& config)
|
||||
{
|
||||
nlohmann::json parsed = nlohmann::json::parse(config, nullptr, /* allow_exceptions */ false);
|
||||
if (parsed.is_discarded()) {
|
||||
BOOST_LOG_TRIVIAL(error) << "PluginConfig: capability '" << capability_name << "' of plugin '" << plugin_key
|
||||
<< "' supplied malformed JSON; config not saved";
|
||||
return false;
|
||||
}
|
||||
|
||||
save_config({plugin_key, capability_name, version, std::move(parsed)});
|
||||
return true;
|
||||
}
|
||||
|
||||
void PluginConfig::save_config(const BaseConfig& config)
|
||||
{
|
||||
if (config.empty()) {
|
||||
BOOST_LOG_TRIVIAL(error) << "PluginConfig: refusing to store a config without a plugin key and capability name";
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
m_storage[{config.plugin_key, config.capability_name}] = config;
|
||||
m_dirty = true;
|
||||
}
|
||||
|
||||
BaseConfig PluginConfig::get_config(const std::string& plugin_key, const std::string& capability_name) const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
const auto it = m_storage.find({plugin_key, capability_name});
|
||||
return it != m_storage.end() ? it->second : BaseConfig();
|
||||
}
|
||||
|
||||
bool PluginConfig::has_config(const std::string& plugin_key, const std::string& capability_name) const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_storage.count({plugin_key, capability_name}) != 0;
|
||||
}
|
||||
|
||||
bool PluginConfig::dirty() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
return m_dirty;
|
||||
}
|
||||
|
||||
void PluginConfig::RegisterBindings(pybind11::module_& m)
|
||||
{
|
||||
namespace py = pybind11;
|
||||
|
||||
auto config_submodule = m.def_submodule("config", "Per-capability configuration storage");
|
||||
|
||||
// Config crosses this boundary as a JSON string, not a dict: the host does not care about
|
||||
// the shape of cap_config, so it never builds Python objects out of it. Plugins hand the
|
||||
// string to json.loads/json.dumps themselves.
|
||||
config_submodule.def(
|
||||
"get_config",
|
||||
[]() -> py::object {
|
||||
const auto [plugin_key, capability] = calling_capability("get_config");
|
||||
|
||||
const BaseConfig config = PluginManager::instance().get_config().get_config(plugin_key, capability);
|
||||
if (config.empty())
|
||||
return py::none();
|
||||
|
||||
return py::str(config_to_entry(config).dump());
|
||||
},
|
||||
R"pbdoc(Return this capability's stored config as a JSON string, or None if it has never been saved.
|
||||
|
||||
The object mirrors the on-disk entry: "plugin_key", "capability", "plugin_version" and
|
||||
"cap_config". "plugin_version" is the version that last wrote the entry, so a plugin can
|
||||
compare it against its own and migrate "cap_config" before use.)pbdoc");
|
||||
|
||||
config_submodule.def(
|
||||
"save_config",
|
||||
[](const std::string& cap_config) {
|
||||
const auto [plugin_key, capability] = calling_capability("save_config");
|
||||
|
||||
return PluginManager::instance().get_config().save_config_text(plugin_key, capability, running_plugin_version(plugin_key),
|
||||
cap_config);
|
||||
},
|
||||
py::arg("cap_config"),
|
||||
R"pbdoc(Store this capability's config, given as a JSON string.
|
||||
|
||||
The plugin key, capability name and plugin version are supplied by the host. Returns False
|
||||
without storing anything if `cap_config` is not valid JSON.)pbdoc");
|
||||
}
|
||||
|
||||
} // namespace Slic3r
|
||||
117
src/slic3r/plugin/PluginConfig.hpp
Normal file
117
src/slic3r/plugin/PluginConfig.hpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#pragma once
|
||||
|
||||
#include <libslic3r/Utils.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <slic3r/plugin/PluginFsUtils.hpp>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#define PLUGIN_CONFIG_DIR "config.json"
|
||||
|
||||
namespace pybind11 {
|
||||
class module_;
|
||||
}
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
/*
|
||||
Example config.json shape
|
||||
{
|
||||
"config": [
|
||||
{
|
||||
"plugin_key": "some_name",
|
||||
"capability": "capability_name",
|
||||
"plugin_version": "1.0.0",
|
||||
"cap_config": {
|
||||
"some": "plugin",
|
||||
"capability": "specific",
|
||||
"stuff": "here"
|
||||
}
|
||||
},
|
||||
{
|
||||
"plugin_key": "some_name",
|
||||
"capability": "capability_name",
|
||||
"plugin_version": "1.0.0",
|
||||
"cap_config": {
|
||||
"some": "plugin",
|
||||
"capability": "specific",
|
||||
"stuff": "here"
|
||||
}
|
||||
},
|
||||
{
|
||||
"plugin_key": "some_name",
|
||||
"capability": "capability_name",
|
||||
"plugin_version": "1.0.0",
|
||||
"cap_config": {
|
||||
"some": "plugin",
|
||||
"capability": "specific",
|
||||
"stuff": "here"
|
||||
}
|
||||
},
|
||||
]
|
||||
}
|
||||
*/
|
||||
|
||||
struct BaseConfig {
|
||||
std::string plugin_key;
|
||||
std::string capability_name;
|
||||
std::string plugin_version;
|
||||
|
||||
nlohmann::json config;
|
||||
|
||||
// True for the default-constructed instance returned by get_config() on a miss.
|
||||
bool empty() const { return plugin_key.empty() || capability_name.empty(); }
|
||||
};
|
||||
|
||||
// Consolidated store for every plugin capability's configuration, persisted as a single
|
||||
// config.json alongside the installed plugins. The shape of `cap_config` belongs to the
|
||||
// plugin; this class only round-trips it.
|
||||
//
|
||||
// A capability is identified by (plugin_key, capability_name). `plugin_version` is metadata
|
||||
// recording which version last wrote the entry, letting an upgraded plugin spot a stale
|
||||
// config and migrate it. Version is deliberately not part of the identity, so upgrading a
|
||||
// plugin does not silently reset the user's settings.
|
||||
//
|
||||
// Plugin code runs on worker threads, so every entry point is mutex-guarded.
|
||||
class PluginConfig
|
||||
{
|
||||
public:
|
||||
static const std::string plugin_config_file() { return (boost::filesystem::path(get_orca_plugins_dir()) / PLUGIN_CONFIG_DIR).string(); }
|
||||
|
||||
// Replaces the in-memory store with what is on disk. A missing or malformed file leaves
|
||||
// the store empty rather than throwing: a bad plugin config must not block startup.
|
||||
void load();
|
||||
|
||||
// Rewrites config.json atomically. Clears the dirty flag only once the file is in place.
|
||||
void save();
|
||||
|
||||
void save_config(const std::string& plugin_key, const std::string& capability_name, const std::string& version, const nlohmann::json& config);
|
||||
void save_config(const BaseConfig& config);
|
||||
|
||||
// Parses `config` as a JSON document, storing nothing and returning false if it is
|
||||
// malformed. Spelled differently from save_config() on purpose: nlohmann::json converts
|
||||
// implicitly from const char*, so a `save_config(..., "{}")` overload pair would be
|
||||
// ambiguous, and a raw string would silently store as a JSON string rather than an object.
|
||||
bool save_config_text(const std::string& plugin_key, const std::string& capability_name, const std::string& version, const std::string& config);
|
||||
|
||||
// Returns a default-constructed BaseConfig (see BaseConfig::empty) when the capability has
|
||||
// no stored config.
|
||||
BaseConfig get_config(const std::string& plugin_key, const std::string& capability_name) const;
|
||||
bool has_config(const std::string& plugin_key, const std::string& capability_name) const;
|
||||
|
||||
bool dirty() const;
|
||||
|
||||
static void RegisterBindings(pybind11::module_& module);
|
||||
|
||||
private:
|
||||
// (plugin_key, capability_name) -> entry. Ordered, so config.json serializes stably.
|
||||
using CapabilityId = std::pair<std::string, std::string>;
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
std::map<CapabilityId, BaseConfig> m_storage;
|
||||
bool m_dirty = false;
|
||||
};
|
||||
} // namespace Slic3r
|
||||
@@ -13,10 +13,16 @@ namespace Slic3r {
|
||||
|
||||
const char* const INSTALL_STATE_FILE = ".install_state.json";
|
||||
|
||||
std::string get_orca_plugins_dir()
|
||||
{
|
||||
namespace fs = boost::filesystem;
|
||||
return (fs::path(data_dir()) / "orca_plugins").string();
|
||||
}
|
||||
|
||||
std::string get_cloud_plugin_dir(const std::string& user_id)
|
||||
{
|
||||
namespace fs = boost::filesystem;
|
||||
return (fs::path(data_dir()) / "orca_plugins" / PLUGIN_SUBSCRIBED_DIR / user_id).string();
|
||||
return (fs::path(get_orca_plugins_dir()) / PLUGIN_SUBSCRIBED_DIR / user_id).string();
|
||||
}
|
||||
|
||||
boost::filesystem::path resolve_plugin_root_from_descriptor(const PluginDescriptor& descriptor)
|
||||
@@ -30,8 +36,7 @@ boost::filesystem::path resolve_plugin_root_from_descriptor(const PluginDescript
|
||||
return {};
|
||||
}
|
||||
|
||||
bool is_plugin_root_allowed(const boost::filesystem::path& candidate_root,
|
||||
const std::vector<std::string>& allowed_dirs)
|
||||
bool is_plugin_root_allowed(const boost::filesystem::path& candidate_root, const std::vector<std::string>& allowed_dirs)
|
||||
{
|
||||
boost::system::error_code ec;
|
||||
boost::filesystem::path resolved_root = boost::filesystem::weakly_canonical(candidate_root, ec);
|
||||
@@ -44,8 +49,7 @@ bool is_plugin_root_allowed(const boost::filesystem::path& candidate_root,
|
||||
return false;
|
||||
|
||||
for (const auto& allowed_dir : allowed_dirs) {
|
||||
if (is_inside_allowed_root(std::filesystem::path(resolved_root.string()),
|
||||
std::filesystem::path(allowed_dir)))
|
||||
if (is_inside_allowed_root(std::filesystem::path(resolved_root.string()), std::filesystem::path(allowed_dir)))
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -85,9 +89,7 @@ bool resolve_allowed_plugin_root(const PluginDescriptor& descriptor,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool delete_plugin_root(const boost::filesystem::path& resolved_root,
|
||||
const std::string& plugin_id,
|
||||
std::string& error)
|
||||
bool delete_plugin_root(const boost::filesystem::path& resolved_root, const std::string& plugin_id, std::string& error)
|
||||
{
|
||||
namespace fs = boost::filesystem;
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ extern const char* const INSTALL_STATE_FILE;
|
||||
// Path: {data_dir}/orca_plugins/_subscribed/{user_id}/
|
||||
std::string get_cloud_plugin_dir(const std::string& user_id);
|
||||
|
||||
std::string get_orca_plugins_dir();
|
||||
|
||||
boost::filesystem::path resolve_plugin_root_from_descriptor(const PluginDescriptor& descriptor);
|
||||
|
||||
bool is_plugin_root_allowed(const boost::filesystem::path& candidate_root,
|
||||
|
||||
@@ -59,7 +59,7 @@ std::string plugin_package_extension(const boost::filesystem::path& path)
|
||||
|
||||
boost::filesystem::path local_plugin_root()
|
||||
{
|
||||
return boost::filesystem::path(data_dir()) / "orca_plugins";
|
||||
return boost::filesystem::path(get_orca_plugins_dir());
|
||||
}
|
||||
|
||||
boost::filesystem::path local_plugin_install_dir(const boost::filesystem::path& source_path)
|
||||
@@ -973,6 +973,7 @@ void PluginLoader::load_plugin_impl(PluginCatalog& catalog, const std::string& p
|
||||
}
|
||||
|
||||
loaded_cap->instance->set_audit_plugin_key(descriptor.plugin_key);
|
||||
loaded_cap->instance->set_audit_capability_name(loaded_cap->name);
|
||||
capability_types.push_back(loaded_cap->type);
|
||||
loaded.capabilities.push_back(capability_id);
|
||||
capabilities.emplace_back(std::move(loaded_cap));
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "PluginManager.hpp"
|
||||
#include "slic3r/GUI/GUI_App.hpp"
|
||||
|
||||
#include <boost/filesystem/path.hpp>
|
||||
#include <libslic3r/Utils.hpp>
|
||||
#include <pybind11/embed.h>
|
||||
|
||||
#include "PythonPluginBridge.hpp"
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "PluginCatalog.hpp"
|
||||
#include "PluginLoader.hpp"
|
||||
#include "PluginDescriptor.hpp"
|
||||
#include "PluginConfig.hpp"
|
||||
|
||||
namespace Slic3r {
|
||||
|
||||
@@ -58,6 +59,8 @@ public:
|
||||
const PluginCatalog& get_catalog() const { return m_catalog; }
|
||||
PluginLoader& get_loader() { return m_loader; }
|
||||
const PluginLoader& get_loader() const { return m_loader; }
|
||||
PluginConfig& get_config() { return m_config; }
|
||||
const PluginConfig& get_config() const { return m_config; }
|
||||
|
||||
void set_cloud_agent(std::shared_ptr<OrcaCloudServiceAgent> agent) { m_cloud_service.set_cloud_agent(std::move(agent)); }
|
||||
|
||||
@@ -88,6 +91,7 @@ private:
|
||||
CloudPluginService m_cloud_service;
|
||||
PluginCatalog m_catalog;
|
||||
PluginLoader m_loader;
|
||||
PluginConfig m_config;
|
||||
|
||||
mutable std::mutex m_mutex;
|
||||
|
||||
|
||||
@@ -29,13 +29,14 @@
|
||||
}
|
||||
|
||||
// Opens the plugin's filesystem audit scope for the duration of a C++ -> Python call
|
||||
// when this trampoline instance carries a non-empty audit plugin key. Declares a local
|
||||
// `_orca_audit_scope`.
|
||||
// when this trampoline instance carries a non-empty audit plugin key. Also publishes the
|
||||
// calling capability's name, so host APIs invoked from Python can tell which capability
|
||||
// they are serving. Declares a local `_orca_audit_scope`.
|
||||
#define ORCA_PY_AUDIT_SCOPE(mode) \
|
||||
std::optional<::Slic3r::ScopedPluginAuditContext> _orca_audit_scope; \
|
||||
if (const std::string& _orca_audit_key = this->audit_plugin_key(); \
|
||||
!_orca_audit_key.empty()) \
|
||||
_orca_audit_scope.emplace(_orca_audit_key, mode)
|
||||
_orca_audit_scope.emplace(_orca_audit_key, this->audit_capability_name(), mode)
|
||||
|
||||
#define ORCA_PY_OVERRIDE_AUDITED(mode, audit_setup, override_macro, ret, base, name, ...) \
|
||||
do { \
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <boost/log/trivial.hpp>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <slic3r/plugin/PluginAuditManager.hpp>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <pybind11/embed.h>
|
||||
@@ -10,6 +11,7 @@
|
||||
#include <pybind11/stl.h>
|
||||
|
||||
#include "PythonInterpreter.hpp"
|
||||
#include "PluginConfig.hpp"
|
||||
#include "PluginHostApi.hpp"
|
||||
#include "PyPluginPackage.hpp"
|
||||
#include "PyPluginTrampoline.hpp"
|
||||
@@ -338,6 +340,7 @@ void bind_python_api(pybind11::module_& m)
|
||||
PrinterAgentPluginCapability::RegisterBindings(m, pluginTypes);
|
||||
ScriptPluginCapability::RegisterBindings(m, pluginTypes);
|
||||
PluginHostApi::RegisterBindings(m);
|
||||
PluginConfig::RegisterBindings(m);
|
||||
BOOST_LOG_TRIVIAL(debug) << "Registered ScriptPluginCapability Python bindings";
|
||||
|
||||
m.def(
|
||||
|
||||
@@ -98,8 +98,13 @@ class PluginCapabilityInterface
|
||||
public:
|
||||
virtual ~PluginCapabilityInterface() = default;
|
||||
|
||||
virtual std::string get_name() const = 0; // required — overridden in Python
|
||||
virtual PluginCapabilityType get_type() const { return PluginCapabilityType::Unknown; } // optional — typed bases override
|
||||
// Required APIs
|
||||
virtual std::string get_name() const = 0;
|
||||
|
||||
// Optional APIs
|
||||
virtual PluginCapabilityType get_type() const { return PluginCapabilityType::Unknown; }
|
||||
virtual bool has_config() const { return false; }
|
||||
virtual std::string embed_config_ui() const { return ""; }
|
||||
|
||||
virtual void on_load() {}
|
||||
virtual void on_unload() {}
|
||||
@@ -110,8 +115,16 @@ public:
|
||||
void set_audit_plugin_key(std::string key) { m_audit_plugin_key = std::move(key); }
|
||||
const std::string& audit_plugin_key() const { return m_audit_plugin_key; }
|
||||
|
||||
// The cached get_name() captured at load, paired with the audit plugin key to identify
|
||||
// which capability a trampoline call belongs to. Cached rather than read live: get_name()
|
||||
// is itself a trampoline call, so calling it from inside a trampoline would recurse.
|
||||
// Empty until PluginLoader materializes the capability.
|
||||
void set_audit_capability_name(std::string name) { m_audit_capability_name = std::move(name); }
|
||||
const std::string& audit_capability_name() const { return m_audit_capability_name; }
|
||||
|
||||
private:
|
||||
std::string m_audit_plugin_key;
|
||||
std::string m_audit_capability_name;
|
||||
};
|
||||
|
||||
} // namespace Slic3r
|
||||
|
||||
@@ -4,6 +4,7 @@ add_executable(${_TEST_NAME}_tests
|
||||
test_plugin_host_api.cpp
|
||||
test_plugin_capability_identifier.cpp
|
||||
test_plugin_install.cpp
|
||||
test_plugin_sort.cpp
|
||||
)
|
||||
|
||||
if (MSVC)
|
||||
|
||||
189
tests/slic3rutils/test_plugin_sort.cpp
Normal file
189
tests/slic3rutils/test_plugin_sort.cpp
Normal file
@@ -0,0 +1,189 @@
|
||||
#include <catch2/catch_all.hpp>
|
||||
|
||||
#include <slic3r/GUI/PluginSort.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using Slic3r::GUI::compare_ascii_case_insensitive_natural;
|
||||
using Slic3r::GUI::PluginSortKey;
|
||||
using Slic3r::GUI::PluginSortOrder;
|
||||
using Slic3r::GUI::PluginSource;
|
||||
using Slic3r::GUI::PluginStatus;
|
||||
using Slic3r::GUI::plugin_sort_key_from_string;
|
||||
using Slic3r::GUI::plugin_sort_order_from_string;
|
||||
using Slic3r::GUI::sort_plugin_items_for_dialog;
|
||||
|
||||
namespace {
|
||||
|
||||
struct SortFixtureItem
|
||||
{
|
||||
std::string plugin_key;
|
||||
PluginSource source;
|
||||
PluginStatus status;
|
||||
std::string type_key;
|
||||
std::string display_name;
|
||||
std::string sort_version;
|
||||
};
|
||||
|
||||
std::vector<std::string> keys(const std::vector<SortFixtureItem>& items)
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
result.reserve(items.size());
|
||||
for (const SortFixtureItem& item : items)
|
||||
result.push_back(item.plugin_key);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("plugin dialog status sort uses requested priority and base-order ties", "[plugin][sort]")
|
||||
{
|
||||
std::vector<SortFixtureItem> items = {
|
||||
{"local_inactive", PluginSource::Local, PluginStatus::Inactive, "script", "Local Inactive"},
|
||||
{"mine_error", PluginSource::Mine, PluginStatus::Error, "script", "Mine Error"},
|
||||
{"mine_activated", PluginSource::Mine, PluginStatus::Activated, "script", "Mine Activated"},
|
||||
{"local_activated", PluginSource::Local, PluginStatus::Activated, "script", "Local Activated"},
|
||||
{"subscribed_loading", PluginSource::Subscribed, PluginStatus::Loading, "script", "Subscribed Loading"},
|
||||
};
|
||||
|
||||
sort_plugin_items_for_dialog(items, PluginSortKey::Status, PluginSortOrder::Asc);
|
||||
|
||||
// why: local_activated and mine_activated tie on Status, so base order breaks the tie by name
|
||||
// (case-insensitive) - "Local Activated" before "Mine Activated".
|
||||
const std::vector<std::string> expected = {
|
||||
"local_activated",
|
||||
"mine_activated",
|
||||
"mine_error",
|
||||
"local_inactive",
|
||||
"subscribed_loading",
|
||||
};
|
||||
CHECK(keys(items) == expected);
|
||||
|
||||
sort_plugin_items_for_dialog(items, PluginSortKey::Status, PluginSortOrder::Desc);
|
||||
|
||||
// why: Desc reverses the status ordinal, but the Activated tie still resolves by ascending
|
||||
// base order (name: "Local Activated" before "Mine Activated") - direction only flips the key.
|
||||
const std::vector<std::string> desc_expected = {
|
||||
"subscribed_loading",
|
||||
"local_inactive",
|
||||
"mine_error",
|
||||
"local_activated",
|
||||
"mine_activated",
|
||||
};
|
||||
CHECK(keys(items) == desc_expected);
|
||||
}
|
||||
|
||||
TEST_CASE("plugin dialog source sort uses enum priority", "[plugin][sort]")
|
||||
{
|
||||
std::vector<SortFixtureItem> items = {
|
||||
{"local", PluginSource::Local, PluginStatus::Activated, "script", "Local"},
|
||||
{"mine", PluginSource::Mine, PluginStatus::Activated, "script", "Mine"},
|
||||
{"subscribed", PluginSource::Subscribed, PluginStatus::Activated, "script", "Subscribed"},
|
||||
};
|
||||
|
||||
sort_plugin_items_for_dialog(items, PluginSortKey::Source, PluginSortOrder::Asc);
|
||||
const std::vector<std::string> asc_expected = {"mine", "subscribed", "local"};
|
||||
CHECK(keys(items) == asc_expected);
|
||||
|
||||
sort_plugin_items_for_dialog(items, PluginSortKey::Source, PluginSortOrder::Desc);
|
||||
const std::vector<std::string> desc_expected = {"local", "subscribed", "mine"};
|
||||
CHECK(keys(items) == desc_expected);
|
||||
}
|
||||
|
||||
TEST_CASE("plugin dialog version sort is semver-aware with base-order ties", "[plugin][sort]")
|
||||
{
|
||||
std::vector<SortFixtureItem> items = {
|
||||
{"v_1_2_0", PluginSource::Local, PluginStatus::Activated, "script", "B", "1.2.0"},
|
||||
{"v_1_10_0", PluginSource::Local, PluginStatus::Activated, "script", "A", "1.10.0"},
|
||||
{"v_0_9_3", PluginSource::Local, PluginStatus::Activated, "script", "C", "0.9.3"},
|
||||
};
|
||||
|
||||
sort_plugin_items_for_dialog(items, PluginSortKey::Version, PluginSortOrder::Asc);
|
||||
// why: semver numeric compare - 1.10.0 > 1.2.0 (not lexical "1.10" < "1.2"), so ascending is
|
||||
// 0.9.3 < 1.2.0 < 1.10.0.
|
||||
const std::vector<std::string> asc_expected = {"v_0_9_3", "v_1_2_0", "v_1_10_0"};
|
||||
CHECK(keys(items) == asc_expected);
|
||||
|
||||
sort_plugin_items_for_dialog(items, PluginSortKey::Version, PluginSortOrder::Desc);
|
||||
const std::vector<std::string> desc_expected = {"v_1_10_0", "v_1_2_0", "v_0_9_3"};
|
||||
CHECK(keys(items) == desc_expected);
|
||||
}
|
||||
|
||||
TEST_CASE("plugin dialog name sort is case-insensitive and numeric-aware", "[plugin][sort]")
|
||||
{
|
||||
std::vector<SortFixtureItem> items = {
|
||||
{"rig10", PluginSource::Local, PluginStatus::Activated, "script", "Rig 10"},
|
||||
{"ada_lower", PluginSource::Local, PluginStatus::Activated, "script", "ada"},
|
||||
{"rig2", PluginSource::Local, PluginStatus::Activated, "script", "Rig 2"},
|
||||
{"ada_upper", PluginSource::Local, PluginStatus::Activated, "script", "Ada"},
|
||||
};
|
||||
|
||||
sort_plugin_items_for_dialog(items, PluginSortKey::Name, PluginSortOrder::Asc);
|
||||
|
||||
// why: "Ada"/"ada" tie on the case-insensitive name (primary AND base name level), so the tie
|
||||
// falls through source/status/type to plugin_key: "ada_lower" before "ada_upper".
|
||||
const std::vector<std::string> expected = {"ada_lower", "ada_upper", "rig2", "rig10"};
|
||||
CHECK(keys(items) == expected);
|
||||
|
||||
sort_plugin_items_for_dialog(items, PluginSortKey::Name, PluginSortOrder::Desc);
|
||||
|
||||
// why: names reverse ("Rig 10" before "Rig 2"), but "Ada"/"ada" tie on the case-insensitive
|
||||
// key and keep ascending base order, which resolves by plugin_key ("ada_lower" < "ada_upper").
|
||||
const std::vector<std::string> desc_expected = {"rig10", "rig2", "ada_lower", "ada_upper"};
|
||||
CHECK(keys(items) == desc_expected);
|
||||
}
|
||||
|
||||
TEST_CASE("natural compare handles digits, case, prefixes and leading zeros", "[plugin][sort]")
|
||||
{
|
||||
// numeric runs compare by value, not lexically
|
||||
CHECK(compare_ascii_case_insensitive_natural("item2", "item10") < 0);
|
||||
CHECK(compare_ascii_case_insensitive_natural("item10", "item2") > 0);
|
||||
CHECK(compare_ascii_case_insensitive_natural("2", "10") < 0);
|
||||
|
||||
// case is ignored on the primary comparison
|
||||
CHECK(compare_ascii_case_insensitive_natural("Camera", "camera") == 0);
|
||||
|
||||
// a prefix is less than the longer string it prefixes
|
||||
CHECK(compare_ascii_case_insensitive_natural("app", "apple") < 0);
|
||||
CHECK(compare_ascii_case_insensitive_natural("apple", "app") > 0);
|
||||
|
||||
// equal numeric value: fewer leading zeros wins the tie
|
||||
CHECK(compare_ascii_case_insensitive_natural("1", "01") < 0);
|
||||
CHECK(compare_ascii_case_insensitive_natural("01", "1") > 0);
|
||||
|
||||
// reflexivity and empty-string boundaries
|
||||
CHECK(compare_ascii_case_insensitive_natural("plugin", "plugin") == 0);
|
||||
CHECK(compare_ascii_case_insensitive_natural("", "") == 0);
|
||||
CHECK(compare_ascii_case_insensitive_natural("", "a") < 0);
|
||||
}
|
||||
|
||||
TEST_CASE("plugin dialog None sort key falls to ascending base order in both directions", "[plugin][sort]")
|
||||
{
|
||||
std::vector<SortFixtureItem> items = {
|
||||
{"z_mine", PluginSource::Mine, PluginStatus::Activated, "script", "Zebra"},
|
||||
{"a_local", PluginSource::Local, PluginStatus::Activated, "script", "Apple"},
|
||||
{"m_sub", PluginSource::Subscribed, PluginStatus::Error, "script", "Mango"},
|
||||
};
|
||||
|
||||
// why: no primary key -> pure name-first base order (Apple < Mango < Zebra). A source-first
|
||||
// baseline would instead give {z_mine, m_sub, a_local}, so this pins the name-first order.
|
||||
const std::vector<std::string> base_expected = {"a_local", "m_sub", "z_mine"};
|
||||
|
||||
sort_plugin_items_for_dialog(items, PluginSortKey::None, PluginSortOrder::Asc);
|
||||
CHECK(keys(items) == base_expected);
|
||||
|
||||
// why: None has no direction - Desc must not reverse the baseline.
|
||||
sort_plugin_items_for_dialog(items, PluginSortKey::None, PluginSortOrder::Desc);
|
||||
CHECK(keys(items) == base_expected);
|
||||
}
|
||||
|
||||
TEST_CASE("plugin dialog sort request parsing keeps previous state on invalid values", "[plugin][sort]")
|
||||
{
|
||||
CHECK(plugin_sort_key_from_string("source", PluginSortKey::Status) == PluginSortKey::Source);
|
||||
CHECK(plugin_sort_key_from_string("none", PluginSortKey::Status) == PluginSortKey::None);
|
||||
CHECK(plugin_sort_key_from_string("missing", PluginSortKey::Name) == PluginSortKey::Name);
|
||||
|
||||
CHECK(plugin_sort_order_from_string("desc", PluginSortOrder::Asc) == PluginSortOrder::Desc);
|
||||
CHECK(plugin_sort_order_from_string("down", PluginSortOrder::Asc) == PluginSortOrder::Asc);
|
||||
}
|
||||
Reference in New Issue
Block a user