2026-08-17 16:03:48 +03:00
|
|
|
const { contextBridge, ipcRenderer } = require("electron");
|
|
|
|
|
|
2026-11-12 10:12:35 +03:00
|
|
|
function toUint8Array(data) {
|
|
|
|
|
if (data instanceof Uint8Array) return data;
|
|
|
|
|
if (data instanceof ArrayBuffer) return new Uint8Array(data);
|
|
|
|
|
if (ArrayBuffer.isView(data)) {
|
|
|
|
|
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
|
|
|
}
|
|
|
|
|
return new Uint8Array(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function subscribeChannel(channel, transform) {
|
|
|
|
|
return (listener) => {
|
|
|
|
|
if (typeof listener !== "function") return () => {};
|
|
|
|
|
const wrapped = (_event, payload) => {
|
|
|
|
|
listener(transform ? transform(payload) : payload);
|
|
|
|
|
};
|
|
|
|
|
ipcRenderer.on(channel, wrapped);
|
|
|
|
|
return () => {
|
|
|
|
|
ipcRenderer.removeListener(channel, wrapped);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function normalizeOpenedFile(payload) {
|
|
|
|
|
if (!payload || typeof payload.filePath !== "string" || typeof payload.fileName !== "string") {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
filePath: payload.filePath,
|
|
|
|
|
fileName: payload.fileName,
|
|
|
|
|
bytes: toUint8Array(payload.bytes),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const desktopBridge = Object.freeze({
|
|
|
|
|
isDesktop: true,
|
|
|
|
|
platform: process.platform,
|
2026-08-17 16:03:48 +03:00
|
|
|
getVersion: () => ipcRenderer.invoke("sketchforge:get-version"),
|
|
|
|
|
checkForUpdates: () => ipcRenderer.invoke("sketchforge:check-for-updates"),
|
|
|
|
|
installUpdate: () => ipcRenderer.invoke("sketchforge:install-update"),
|
2026-11-12 10:12:35 +03:00
|
|
|
getUpdateState: () => ipcRenderer.invoke("snapcad:get-update-state"),
|
|
|
|
|
onUpdateState: subscribeChannel("snapcad:update-state"),
|
|
|
|
|
getDesktopPrefs: () => ipcRenderer.invoke("snapcad:get-desktop-prefs"),
|
|
|
|
|
setDesktopPrefs: (patch) =>
|
|
|
|
|
ipcRenderer.invoke("snapcad:set-desktop-prefs", patch && typeof patch === "object" ? patch : {}),
|
|
|
|
|
hideToTray: () => ipcRenderer.invoke("snapcad:hide-to-tray"),
|
|
|
|
|
onDesktopPrefsChanged: subscribeChannel("snapcad:prefs-changed"),
|
|
|
|
|
startYakNetLogin: () => ipcRenderer.invoke("snapcad:yaknet-login"),
|
|
|
|
|
logoutYakNet: () => ipcRenderer.invoke("snapcad:yaknet-logout"),
|
|
|
|
|
cloudRequest: (options) =>
|
|
|
|
|
ipcRenderer.invoke("snapcad:cloud-request", options && typeof options === "object" ? options : {}),
|
|
|
|
|
onAuthUpdated: subscribeChannel("snapcad:auth-updated"),
|
|
|
|
|
openSkfDialog: async () => normalizeOpenedFile(await ipcRenderer.invoke("snapcad:open-skf-dialog")),
|
|
|
|
|
saveSkfDialog: (options) =>
|
|
|
|
|
ipcRenderer.invoke("snapcad:save-skf-dialog", {
|
|
|
|
|
defaultFileName: options?.defaultFileName,
|
|
|
|
|
targetPath: options?.targetPath ?? null,
|
|
|
|
|
forceDialog: Boolean(options?.forceDialog),
|
|
|
|
|
bytes: toUint8Array(options?.bytes),
|
|
|
|
|
}),
|
|
|
|
|
saveExportFileDialog: (options) =>
|
|
|
|
|
ipcRenderer.invoke("snapcad:save-export-dialog", {
|
|
|
|
|
defaultFileName: options?.defaultFileName,
|
|
|
|
|
filters: Array.isArray(options?.filters) ? options.filters : undefined,
|
|
|
|
|
bytes: toUint8Array(options?.bytes),
|
|
|
|
|
}),
|
|
|
|
|
consumePendingOpenFile: async () =>
|
|
|
|
|
normalizeOpenedFile(await ipcRenderer.invoke("snapcad:consume-pending-open-file")),
|
|
|
|
|
onOpenFile: subscribeChannel("snapcad:open-file", normalizeOpenedFile),
|
|
|
|
|
onMenuCommand: subscribeChannel("snapcad:menu-command"),
|
|
|
|
|
setMenuState: (state) =>
|
|
|
|
|
ipcRenderer.invoke("snapcad:set-menu-state", state && typeof state === "object" ? state : {}),
|
|
|
|
|
setDirty: (dirty) => ipcRenderer.invoke("snapcad:set-dirty", Boolean(dirty)),
|
|
|
|
|
setWindowTitle: (title) => ipcRenderer.invoke("snapcad:set-window-title", String(title ?? "")),
|
|
|
|
|
confirmCloseAfterSave: () => ipcRenderer.invoke("snapcad:confirm-close-after-save"),
|
|
|
|
|
openExternal: (url) => ipcRenderer.invoke("snapcad:open-external", String(url ?? "")),
|
2026-08-17 16:03:48 +03:00
|
|
|
});
|
2026-11-12 10:12:35 +03:00
|
|
|
|
|
|
|
|
contextBridge.exposeInMainWorld("snapcadDesktop", desktopBridge);
|
|
|
|
|
contextBridge.exposeInMainWorld("sketchforgeDesktop", desktopBridge);
|