From 884e193384ee34bbcb2a7cc7aa39b40e1c3334a9 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Mon, 15 Apr 2019 19:56:30 -0400 Subject: rename main.js and don't open dev tools --- electron/src/main.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 electron/src/main.js (limited to 'electron/src/main.js') diff --git a/electron/src/main.js b/electron/src/main.js new file mode 100644 index 00000000..f78680e0 --- /dev/null +++ b/electron/src/main.js @@ -0,0 +1,50 @@ +import { app, BrowserWindow, ipcMain, dialog } from 'electron'; +var fs = require('fs'); + +// Keep a global reference of the window object, if you don't, the window will +// be closed automatically when the JavaScript object is garbage collected. +let mainWindow; + +const createWindow = () => { + // Create the browser window. + mainWindow = new BrowserWindow(); + + mainWindow.maximize(); + + // and load the index.html of the app. + if (process.argv[1] == ".") { + // run in development mode with `electron . ` + var url = process.argv[2]; + } else { + var url = process.argv[1]; + } + mainWindow.loadURL(url); + + //mainWindow.webContents.openDevTools(); + + // Emitted when the window is closed. + mainWindow.on('closed', () => { + // Dereference the window object, usually you would store windows + // in an array if your app supports multi windows, this is the time + // when you should delete the corresponding element. + mainWindow = null; + }); +}; + +// This method will be called when Electron has finished +// initialization and is ready to create browser windows. +// Some APIs can only be used after this event occurs. +app.on('ready', createWindow); + +// Quit when all windows are closed. +app.on('window-all-closed', () => { + app.quit(); +}); + +ipcMain.on('print', function (event, pageSize) { + mainWindow.webContents.printToPDF({"pageSize": pageSize}, function(error, data) { + dialog.showSaveDialog(mainWindow, {"defaultPath": "inkstitch.pdf"}, function(filename, bookmark) { + fs.writeFileSync(filename, data, 'utf-8'); + }) + }) +}) \ No newline at end of file -- cgit v1.2.3 From 0a41ad6f71dfbf30c35bdaaa5c40e953282378a3 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 18 Apr 2019 09:33:53 -0400 Subject: handle cancel in save pdf dialog --- electron/src/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'electron/src/main.js') diff --git a/electron/src/main.js b/electron/src/main.js index f78680e0..2774ddc6 100644 --- a/electron/src/main.js +++ b/electron/src/main.js @@ -44,7 +44,8 @@ app.on('window-all-closed', () => { ipcMain.on('print', function (event, pageSize) { mainWindow.webContents.printToPDF({"pageSize": pageSize}, function(error, data) { dialog.showSaveDialog(mainWindow, {"defaultPath": "inkstitch.pdf"}, function(filename, bookmark) { - fs.writeFileSync(filename, data, 'utf-8'); + if (typeof filename !== 'undefined') + fs.writeFileSync(filename, data, 'utf-8'); }) }) }) \ No newline at end of file -- cgit v1.2.3 From bf43633560252c4b11c00cb028676d9a7b1968b8 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 18 Apr 2019 10:14:51 -0400 Subject: add icons --- electron/src/main.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'electron/src/main.js') diff --git a/electron/src/main.js b/electron/src/main.js index 2774ddc6..210e26ed 100644 --- a/electron/src/main.js +++ b/electron/src/main.js @@ -1,5 +1,7 @@ import { app, BrowserWindow, ipcMain, dialog } from 'electron'; var fs = require('fs'); +var path = require('path') +var tmp = require('tmp') // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -7,7 +9,7 @@ let mainWindow; const createWindow = () => { // Create the browser window. - mainWindow = new BrowserWindow(); + mainWindow = new BrowserWindow({icon: path.join(__dirname, 'assets/icons/png/512x512.png')}); mainWindow.maximize(); -- cgit v1.2.3 From 09c46e0fb499987899a2e67819099eba2c121487 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 18 Apr 2019 10:15:13 -0400 Subject: add 'save pdf' button and print opens PDF --- electron/src/main.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'electron/src/main.js') diff --git a/electron/src/main.js b/electron/src/main.js index 210e26ed..db52ac94 100644 --- a/electron/src/main.js +++ b/electron/src/main.js @@ -1,4 +1,4 @@ -import { app, BrowserWindow, ipcMain, dialog } from 'electron'; +import { app, BrowserWindow, ipcMain, dialog, shell } from 'electron'; var fs = require('fs'); var path = require('path') var tmp = require('tmp') @@ -43,11 +43,20 @@ app.on('window-all-closed', () => { app.quit(); }); -ipcMain.on('print', function (event, pageSize) { +ipcMain.on('save-pdf', function (event, pageSize) { mainWindow.webContents.printToPDF({"pageSize": pageSize}, function(error, data) { dialog.showSaveDialog(mainWindow, {"defaultPath": "inkstitch.pdf"}, function(filename, bookmark) { if (typeof filename !== 'undefined') fs.writeFileSync(filename, data, 'utf-8'); }) }) -}) \ No newline at end of file +}) + +ipcMain.on('open-pdf', function (event, pageSize) { + mainWindow.webContents.printToPDF({"pageSize": pageSize}, function(error, data) { + tmp.file({keep: true, discardDescriptor: true}, function(err, path, fd, cleanupCallback) { + fs.writeFileSync(path, data, 'utf-8'); + shell.openItem(path); + }) + }) +}) -- cgit v1.2.3