Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electron19 #160

Merged
merged 6 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
build:
strategy:
matrix:
node-version: [14.x, 16.x, 18.x, 20.x]
node-version: [16.x, 18.x, 20.x]
os: [windows-2022, ubuntu-22.04]
runs-on: ${{ matrix.os }}
steps:
Expand Down
42 changes: 25 additions & 17 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
const electron = require('electron')
const { Menu, ipcMain } = electron
const { app, ipcMain, BrowserWindow, screen } = require('electron')
const fs = require('graceful-fs');
const path = require('path')
const os = require('os')
const url = require('url')

// Module to control application life.
const app = electron.app
require('@electron/remote/main').initialize()

app.commandLine.appendSwitch('--enable-precise-memory-info');

// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow

const path = require('path')
const os = require('os')
const url = require('url')
/*
// main menu for mac
const template = [
Expand Down Expand Up @@ -83,7 +78,7 @@ if (!gotTheLock) {
function createMainWindow() {

// Create the browser window.
const {width, height} = electron.screen.getPrimaryDisplay().workAreaSize;
const {width, height} = screen.getPrimaryDisplay().workAreaSize;

var frameless = process.platform == 'darwin';
//var frameless = true;
Expand All @@ -96,10 +91,13 @@ function createMainWindow() {
webPreferences: {
contextIsolation: false,
enableRemoteModule: true,
nodeIntegration: true
nodeIntegration: true,
nativeWindowOpen: true
}
})

require('@electron/remote/main').enable(mainWindow.webContents)

// and load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, './main/index.html'),
Expand Down Expand Up @@ -142,9 +140,12 @@ function createBackgroundWindows() {
webPreferences: {
contextIsolation: false,
enableRemoteModule: true,
nodeIntegration: true
nodeIntegration: true,
nativeWindowOpen: true
}
});

require('@electron/remote/main').enable(back.webContents)

if (process.env["deepnest_debug"] === '1')
back.webContents.openDevTools();
Expand All @@ -169,8 +170,6 @@ function createBackgroundWindows() {
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', () => {
// https://www.electronjs.org/docs/latest/breaking-changes#planned-breaking-api-changes-90
app.allowRendererProcessReuse = false;
createMainWindow();
mainWindow.once('ready-to-show', () => {
mainWindow.show();
Expand Down Expand Up @@ -256,6 +255,15 @@ ipcMain.on('background-stop', function(event){
console.log('stopped!', backgroundWindows);
});

// Backward compat with https://electron-settings.js.org/index.html#configure
const configPath = path.resolve(app.getPath('userData'), 'settings.json');
ipcMain.handle('read-config', () => {
return fs.existsSync(configPath) ? JSON.parse(fs.readFileSync(configPath).toString()) : {};
});
ipcMain.handle('write-config', (event, stringifiedConfig) => {
fs.writeFileSync(configPath, stringifiedConfig);
});

ipcMain.on('login-success', function(event, payload){
mainWindow.webContents.send('login-success', payload);
});
Expand All @@ -266,8 +274,8 @@ ipcMain.on('purchase-success', function(event){

ipcMain.on("setPlacements", (event, payload) => {
global.exportedPlacements = payload;
} );
});

ipcMain.on("test", (event, payload) => {
global.test = payload;
} );
});
16 changes: 5 additions & 11 deletions main/deepnest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@
(function(root){
'use strict';

const { ipcRenderer } = require('electron');
const path = require('path')
const url = require('url')
root.DeepNest = new DeepNest(require('electron').ipcRenderer);

root.DeepNest = new DeepNest();

function DeepNest(){
var self = this;

function DeepNest(eventEmitter){
var svg = null;

var config = {
Expand Down Expand Up @@ -1021,8 +1015,8 @@
}
}

ipcRenderer.on('background-response', (event, payload) => {
ipcRenderer.send("setPlacements", payload);
eventEmitter.on('background-response', (event, payload) => {
eventEmitter.send("setPlacements", payload);
console.log('ipc response',payload);
if(!GA){
// user might have quit while we're away
Expand Down Expand Up @@ -1158,7 +1152,7 @@
filenames[j] = filename;
}

ipcRenderer.send('background-start', {index: i, sheets: sheets, sheetids: sheetids, sheetsources: sheetsources, sheetchildren: sheetchildren, individual: GA.population[i], config: config, ids: ids, sources: sources, children: children, filenames: filenames});
eventEmitter.send('background-start', {index: i, sheets: sheets, sheetids: sheetids, sheetsources: sheetsources, sheetchildren: sheetchildren, individual: GA.population[i], config: config, ids: ids, sources: sources, children: children, filenames: filenames});
running++;
}
}
Expand Down
62 changes: 30 additions & 32 deletions main/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
}
}

const { ipcRenderer, remote } = require('electron');
const { ipcRenderer } = require('electron');
const remote = require('@electron/remote');
const { dialog } = remote;
const fs = require('graceful-fs');
const request = require('request');
const http = require('http');
const path = require('path');

ready(function(){
ready(async function(){
// main navigation
var tabs = document.querySelectorAll('#sidenav li');

Expand Down Expand Up @@ -66,8 +68,9 @@
});

// config form
const config = require('electron-settings');
window.config = config;


const defaultConversionServer = 'http://convert.deepnest.io';

var defaultconfig = {
units: 'inch',
Expand All @@ -85,18 +88,29 @@
dxfImportScale: "1",
dxfExportScale: "72",
endpointTolerance: 0.36,
conversionServer: 'http://convert.deepnest.io'
conversionServer: defaultConversionServer
};

//config.defaults(defaultconfig);

const defaultConversionServer = 'http://convert.deepnest.io';

// set to default if not set (for people with old configs stored)
for (var key in defaultconfig) {
if(typeof config.getSync(key) === 'undefined'){
config.setSync(key, defaultconfig[key]);
}

// Removed `electron-settings` while keeping the same interface to minimize changes
const config = window.config = {
...defaultconfig,
...(await ipcRenderer.invoke('read-config')),
getSync(k){
return typeof k==='undefined'? this: this[k];
},
setSync(arg0, v){
if(typeof arg0 === 'object') {
for (const key in arg0) {
this[key] = arg0[key];
}
} else if(typeof arg0 === 'string'){
this[arg0] = v;
}
ipcRenderer.invoke('write-config', JSON.stringify(this, null, 2));
},
resetToDefaultsSync(){
this.setSync(defaultconfig);
}
}

var cfgvalues = config.getSync();
Expand Down Expand Up @@ -559,9 +573,6 @@
}

// file import
var electron = require('electron');
var app = electron.remote;
var fs = require('fs');

var files = fs.readdirSync(remote.getGlobal('NEST_DIRECTORY'));
var svgs = files.map(file => file.includes('.svg') ? file : undefined).filter(file => file !== undefined).sort();
Expand All @@ -578,7 +589,6 @@

importbutton.className = 'button import disabled';

var dialog = app.dialog;
dialog.showOpenDialog({ filters: [

{ name: 'CAD formats', extensions: ['svg', 'dxf', 'cdr'] }
Expand Down Expand Up @@ -732,7 +742,7 @@
// add sheet
document.querySelector('#addsheet').onclick = function(){
var tools = document.querySelector('#partstools');
var dialog = document.querySelector('#sheetdialog');
// var dialog = document.querySelector('#sheetdialog');

tools.className = 'active';
};
Expand Down Expand Up @@ -800,7 +810,6 @@
nestwindow.setAlwaysOnTop(true);
nestwindow.open();*/

//const remote = require('electron').remote;

/*window.nestwindow = new BrowserWindow({width: window.outerWidth*0.8, height: window.outerHeight*0.8, frame: true});

Expand Down Expand Up @@ -904,7 +913,6 @@


for(var i=0; i<DeepNest.parts.length; i++){
console.log(DeepNest.parts);
if(DeepNest.parts[i].sheet){
// need at least one sheet
document.querySelector('#main').className = '';
Expand Down Expand Up @@ -1015,7 +1023,6 @@
var exportsvg = document.querySelector('#exportsvg');
exportsvg.onclick = function(){

var dialog = app.dialog;
var fileName = dialog.showSaveDialogSync({title: 'Export Deepnest SVG'});

if(fileName === undefined){
Expand Down Expand Up @@ -1043,9 +1050,6 @@

var exportdxf = document.querySelector('#exportdxf');
exportdxf.onclick = function(){
// var electron = require('electron');
// var app = electron.remote;
var dialog = app.dialog;
var fileName = dialog.showSaveDialogSync({
title: 'Export Deepnest DXF'
})
Expand Down Expand Up @@ -1101,7 +1105,6 @@
/*
var exportgcode = document.querySelector('#exportgcode');
exportgcode.onclick = function(){
var dialog = app.dialog;
dialog.showSaveDialog({title: 'Export Deepnest Gcode'}, function (fileName) {
if(fileName === undefined){
console.log("No file selected");
Expand Down Expand Up @@ -1472,11 +1475,6 @@
document.body.ondrop = (ev) => {
ev.preventDefault();
}

var windowManager = app.require('electron-window-manager');

const BrowserWindow = app.BrowserWindow;
const url = require('url');

window.loginWindow = null;
});
Expand Down
4 changes: 2 additions & 2 deletions main/svgparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
var lastCommand = merged.pathSegList.getItem(merged.pathSegList.numberOfItems-1).pathSegTypeAsLetter;
if(lastCommand != 'z' && lastCommand != 'Z'){
// endpoints are actually far apart
console.log(merged);
// console.log(merged);
merged.pathSegList.appendItem(merged.createSVGPathSegClosePath());
}

Expand Down Expand Up @@ -544,7 +544,7 @@
else if(path.tagName == 'path'){
this.pathToAbsolute(path);
var split = this.splitPathSegments(path);
console.log(split);
// console.log(split);
split.forEach(function(e){
root.appendChild(e);
});
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "deepnest",
"version": "1.2.0",
"version": "1.2.1",
"description": "Deep nesting for Laser and CNC",
"main": "main.js",
"license": "MIT",
Expand All @@ -11,7 +11,7 @@
"build": "mkdir minkowski >nul 2>&1|echo . >nul && mkdir minkowski/Release >nul 2>&1|echo . >nul && electron-rebuild && npm run copy",
"clean": "rmdir /s /q build dist >nul 2>&1|echo . >nul",
"clean-all": "rmdir /s /q build dist node_modules minkowski bin >nul 2>&1|echo . >nul",
"dist": "electron-packager . deepnest-v%npm_package_version% --overwrite",
"dist": "npx @electron/packager . deepnest-v$npm_package_version --overwrite",
"dist-all": "npm run clean-all && npm install && npm run build-all && npm run dist",
"copy": "shx cp -r ./build/Release ./minkowski"
},
Expand All @@ -27,16 +27,16 @@
"email": "[email protected]"
},
"devDependencies": {
"electron": "^13.0.0",
"electron-packager": "^17.1.1",
"@electron/packager": "^18.3.6",
"@electron/rebuild": "^3.2.9",
"@types/node": "^22.10.1",
"electron": "^19.1.9",
"nan": "^2.22.0",
"shx": "^0.3.4"
},
"dependencies": {
"electron-settings": "^4.0.2",
"electron-window-manager": "^1.0.6",
"@electron/remote": "^2.1.2",
"graceful-fs": "^4.2.11",
"nan": "^2.17.0",
"request": "^2.88.2"
},
"build": {
Expand All @@ -52,4 +52,4 @@
"icon": "icon.ico"
}
}
}
}
19 changes: 8 additions & 11 deletions src/addon.cc
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#include <nan.h>
//#include "minkowski.h"
// #include "minkowski.h"

using Nan::GetFunction;
using Nan::New;
using Nan::Set;
using v8::FunctionTemplate;
using v8::Local;
using v8::Object;
using v8::String;
using Nan::GetFunction;
using Nan::New;
using Nan::Set;

NAN_METHOD(calculateNFP);

// Expose synchronous and asynchronous access to our
// Estimate() function
NAN_MODULE_INIT(InitAll) {
Set(target, New<String>("calculateNFP").ToLocalChecked(),
GetFunction(New<FunctionTemplate>(calculateNFP)).ToLocalChecked());
NAN_MODULE_INIT(Init)
{
Nan::SetMethod(target, "calculateNFP", calculateNFP);
}

NODE_MODULE(addon, InitAll);

NAN_MODULE_WORKER_ENABLED(addon, Init)