From 9ccf2f552be246f7912e9dc923f8da8ae47fe544 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Wed, 10 Apr 2019 23:37:16 -0400 Subject: switch print pdf gui to electron --- print/resources/inkstitch.js | 24 +----------------------- print/templates/index.html | 2 ++ 2 files changed, 3 insertions(+), 23 deletions(-) (limited to 'print') diff --git a/print/resources/inkstitch.js b/print/resources/inkstitch.js index 86bc213e..c58aed8a 100644 --- a/print/resources/inkstitch.js +++ b/print/resources/inkstitch.js @@ -11,12 +11,6 @@ var realistic_rendering = {}; var realistic_cache = {}; var normal_rendering = {}; -function ping() { - $.get("/ping") - .done(function() { setTimeout(ping, 1000) }) - .fail(function() { $('#errors').attr('class', 'show') }); -} - //function to chunk opd view into pieces // source: https://stackoverflow.com/questions/3366529/wrap-every-3-divs-in-a-div $.fn.chunk = function(size) { @@ -199,7 +193,6 @@ function setSVGTransform(figure, transform) { } $(function() { - setTimeout(ping, 1000); /* SCALING AND MOVING SVG */ /* Mousewheel scaling */ @@ -369,22 +362,7 @@ $(function() { /* Settings Bar */ $('button.close').click(function() { - $.post('/shutdown', {}) - .always(function(data) { - window.close(); - - /* Chrome and Firefox both have a rule: scripts can only close windows - * that they opened. Chrome seems to have an exception for windows that - * were opened by an outside program, so the above works fine. Firefox - * steadfastly refuses to allow us to close the window, so we'll tell - * the user (in their language) that they can close it. - */ - setTimeout(function() { - document.open(); - document.write("" + data + ""); - document.close(); - }, 1000); - }); + window.close(); }); $('button.print').click(function() { diff --git a/print/templates/index.html b/print/templates/index.html index d0ab848f..375fdc77 100644 --- a/print/templates/index.html +++ b/print/templates/index.html @@ -2,6 +2,8 @@ Ink/Stitch Print Preview + + -- cgit v1.3.1 From 9b2a47136b34a4b9cfcc42282a39fdd909e9f8af Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Fri, 12 Apr 2019 17:45:57 -0400 Subject: enable print button --- electron/package.json | 15 +++++++++++---- electron/src/index.js | 21 +++++++++++++++++++-- lib/extensions/print_pdf.py | 12 ------------ lib/gui/electron.py | 22 +++++++--------------- print/resources/inkstitch.js | 11 ++++------- 5 files changed, 41 insertions(+), 40 deletions(-) (limited to 'print') diff --git a/electron/package.json b/electron/package.json index 1306eb68..c902b205 100644 --- a/electron/package.json +++ b/electron/package.json @@ -6,12 +6,19 @@ "main": "src/index.js", "scripts": { "pack": "electron-builder --dir", - "dist": "electron-builder" + "dist": "electron-builder", + "dev": "electron ." }, "build": { - "linux": { "target": "dir" }, - "win": { "target": "dir" }, - "mac": { "target": "dir" } + "linux": { + "target": "dir" + }, + "win": { + "target": "dir" + }, + "mac": { + "target": "dir" + } }, "keywords": [], "author": "lex", diff --git a/electron/src/index.js b/electron/src/index.js index 398a9580..90aaea63 100644 --- a/electron/src/index.js +++ b/electron/src/index.js @@ -1,4 +1,5 @@ -import { app, BrowserWindow } from 'electron'; +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. @@ -11,8 +12,16 @@ const createWindow = () => { mainWindow.maximize(); // and load the index.html of the app. - mainWindow.loadURL(process.argv[1]); + 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 @@ -31,3 +40,11 @@ app.on('ready', createWindow); 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 diff --git a/lib/extensions/print_pdf.py b/lib/extensions/print_pdf.py index 4d8ffbd4..0cbce479 100644 --- a/lib/extensions/print_pdf.py +++ b/lib/extensions/print_pdf.py @@ -88,18 +88,6 @@ class PrintPreviewServer(Thread): def resources(resource): return send_from_directory(self.resources_path, resource, cache_timeout=1) - @self.app.route('/printing/start') - def printing_start(): - # temporarily turn off the watcher while the print dialog is up, - # because javascript will be frozen - self.last_request_time = None - return "OK" - - @self.app.route('/printing/end') - def printing_end(): - # nothing to do here -- request_started() will restart the watcher - return "OK" - @self.app.route('/settings/', methods=['POST']) def set_field(field_name): self.metadata[field_name] = request.json['value'] diff --git a/lib/gui/electron.py b/lib/gui/electron.py index ced3ad66..f1bdeb61 100644 --- a/lib/gui/electron.py +++ b/lib/gui/electron.py @@ -1,4 +1,3 @@ -from glob import glob import os import subprocess import sys @@ -14,21 +13,14 @@ def open_url(url): if getattr(sys, 'frozen', None) is not None: electron_path = os.path.join(get_bundled_dir("electron"), "inkstitch-gui") - else: - # It's a bit trickier to find the electron app in a development environment. - base_dir = get_bundled_dir("electron") - - try: - package_dir = glob(os.path.join(base_dir, 'dist', '*-unpacked'))[0] - except IndexError: - raise Exception("Electron app not found. Be sure to run 'yarn; yarn dist' in %s." % base_dir) - - electron_path = os.path.join(base_dir, package_dir, "inkstitch-gui") - if sys.platform == "darwin": - electron_path += ".app/Contents/MacOS/inkstitch-gui" - app_process = subprocess.Popen(["open", "-a", electron_path, "--args", url]) + if sys.platform == "darwin": + electron_path += ".app/Contents/MacOS/inkstitch-gui" + subprocess.Popen(["open", "-a", electron_path, "--args", url]) + else: + app_process = subprocess.Popen([electron_path, url]) else: - app_process = subprocess.Popen([electron_path, url]) + # if we're not running in a pyinstaller bundle, run electron directly + app_process = subprocess.Popen(["yarn", "dev", url], cwd=get_bundled_dir("electron")) return app_process diff --git a/print/resources/inkstitch.js b/print/resources/inkstitch.js index c58aed8a..32a9a207 100644 --- a/print/resources/inkstitch.js +++ b/print/resources/inkstitch.js @@ -1,3 +1,5 @@ +var electron = require('electron'); + $.postJSON = function(url, data, success=null) { return $.ajax(url, { type: 'POST', @@ -366,13 +368,8 @@ $(function() { }); $('button.print').click(function() { - // printing halts all javascript activity, so we need to tell the backend - // not to shut down until we're done. - $.get("/printing/start") - .done(function() { - window.print(); - $.get("/printing/end"); - }); + var pageSize = $('select#printing-size').find(':selected').text(); + electron.ipcRenderer.send('print', pageSize) }); $('button.settings').click(function(){ -- cgit v1.3.1 From 1edd5c86355bb3bb02754fe729a2b51a3a8b1a60 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Mon, 15 Apr 2019 19:56:44 -0400 Subject: remove last vestige of /printing/start --- print/resources/inkstitch.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'print') diff --git a/print/resources/inkstitch.js b/print/resources/inkstitch.js index 32a9a207..7c972c7a 100644 --- a/print/resources/inkstitch.js +++ b/print/resources/inkstitch.js @@ -661,15 +661,11 @@ $(function() { // rendering slows down the browser enough that we can miss sending // pings, so tell the server side to wait for us - $.get("/printing/start") - .done(function() { - ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height); - realistic_cache[item] = '' + - '' + - ''; - finalize(realistic_cache[item]); - $.get("/printing/end"); - }); + ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height); + realistic_cache[item] = '' + + '' + + ''; + finalize(realistic_cache[item]); }; image.src = '/realistic/' + item; } else { -- cgit v1.3.1 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 ++++++++++++--- print/resources/inkstitch.js | 7 ++++++- print/resources/style.css | 2 +- print/templates/ui.html | 1 + 4 files changed, 20 insertions(+), 5 deletions(-) (limited to 'print') 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); + }) + }) +}) diff --git a/print/resources/inkstitch.js b/print/resources/inkstitch.js index 7c972c7a..57f62b1a 100644 --- a/print/resources/inkstitch.js +++ b/print/resources/inkstitch.js @@ -369,9 +369,14 @@ $(function() { $('button.print').click(function() { var pageSize = $('select#printing-size').find(':selected').text(); - electron.ipcRenderer.send('print', pageSize) + electron.ipcRenderer.send('open-pdf', pageSize) }); + $('button.save-pdf').click(function() { + var pageSize = $('select#printing-size').find(':selected').text(); + electron.ipcRenderer.send('save-pdf', pageSize) + }); + $('button.settings').click(function(){ $('#settings-ui').show(); }); diff --git a/print/resources/style.css b/print/resources/style.css index 9ffff07d..3e96e2ba 100644 --- a/print/resources/style.css +++ b/print/resources/style.css @@ -123,7 +123,7 @@ body { color: white; } - .ui button.print { + .ui button.print, .ui button.save-pdf { border: 1px solid rgb(50,132,50); } diff --git a/print/templates/ui.html b/print/templates/ui.html index 23e39145..57c17205 100644 --- a/print/templates/ui.html +++ b/print/templates/ui.html @@ -2,6 +2,7 @@

{{ _('Ink/Stitch Print Preview') }}

+
-- cgit v1.3.1 From 953990739b742bdad5221c39b35d47fd530502bb Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Tue, 23 Apr 2019 19:58:51 -0400 Subject: remove obselete comment --- print/resources/inkstitch.js | 3 --- 1 file changed, 3 deletions(-) (limited to 'print') diff --git a/print/resources/inkstitch.js b/print/resources/inkstitch.js index 57f62b1a..a5d57e26 100644 --- a/print/resources/inkstitch.js +++ b/print/resources/inkstitch.js @@ -663,9 +663,6 @@ $(function() { canvas.height = image.height / 96 * 600; var ctx = canvas.getContext('2d'); - - // rendering slows down the browser enough that we can miss sending - // pings, so tell the server side to wait for us ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height); realistic_cache[item] = '' + '' + -- cgit v1.3.1