From 287c97b26ed40085f4fd3eeca03d44bca9d40916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Mo=C5=A1ko?= Date: Thu, 5 Oct 2017 12:52:00 +0200 Subject: [PATCH] Positioning bugs, better async calls, logging fixes. --- js/console-script.js | 35 ++++++++--- main.js | 141 ++++++++++++++++++++++++------------------- 2 files changed, 107 insertions(+), 69 deletions(-) diff --git a/js/console-script.js b/js/console-script.js index 79a5680..d02a3bc 100644 --- a/js/console-script.js +++ b/js/console-script.js @@ -28,7 +28,7 @@ debugMode = false; setupSettings(); - if (arguments[0] == "debug"){ + if (arguments[0] == "debug") { debugMode = true; } @@ -104,7 +104,7 @@ ipc.send('toggleRules'); }); - document.getElementById("projectorBtn").addEventListener("click", function() { // Toggle visibility og Projector Window + document.getElementById("projectorBtn").addEventListener("click", function() { // Toggle visibility of Projector Window ipc.send('toggleProjector'); }); @@ -131,13 +131,19 @@ document.getElementById("openFile").addEventListener("click", function() { dialog.showOpenDialog({ defaultPath: settings.get("defaultPath") || app.getPath("home"), - filters: [ - { name: 'impress.js presentations', extensions: ['md', 'mkd', 'markdown', 'html', 'htm', 'zip'] }, - { name: 'All Files', extensions: ['*'] } - ] + filters: [{ + name: 'impress.js presentations', + extensions: ['md', 'mkd', 'markdown', 'html', 'htm', 'zip'] + }, + { + name: 'All Files', + extensions: ['*'] + } + ] }, function(fileNames) { settings.set("defaultPath", path.dirname(fileNames[0])) if (fileNames === undefined) { + saveLogs(i18n.__("No file selected")); console.log(i18n.__("No file selected")); return; } @@ -188,6 +194,7 @@ // Add the error event listener unzipper.on('error', function(err) { + saveLogs(i18n.__('Unzip with decompress-zip failed'), err); console.log(i18n.__('Unzip with decompress-zip failed'), err); }); @@ -203,6 +210,7 @@ }); break; default: + saveLogs(i18n.__("Something went wrong. Wrong file is loaded.")); console.log(i18n.__("Something went wrong. Wrong file is loaded.")); } }); @@ -232,6 +240,7 @@ try { html = el.getElementById("impress").outerHTML; // Grab
...
and place it inside our template. } catch (err) { + saveLogs("There is a problem with a file you selected"); console.log("There is a problem with a file you selected"); return; } @@ -267,7 +276,9 @@ saveViewer(); showTimer('projection'); totalSeconds = 0; - document.getElementById("projectionTimer").addEventListener("click", function() { totalSeconds = 0; }); + document.getElementById("projectionTimer").addEventListener("click", function() { + totalSeconds = 0; + }); running = true; document.body.classList.add('running'); mousetrap.bind(['space'], function() { @@ -279,6 +290,7 @@ if (debugMode) { webview0.openDevTools(); webview0.addEventListener('console-message', (e) => { + saveLogs('Guest page logged a message:', e.message); console.log('Guest page logged a message:', e.message); }) } @@ -331,7 +343,13 @@ `; let rendered = ms.render(tplSlideList, { "slides": slideList, - "isCurrent": function() { if (this.step == current) { return "current"; } else { return "future"; } } + "isCurrent": function() { + if (this.step == current) { + return "current"; + } else { + return "future"; + } + } }); document.getElementById("impressOverview").innerHTML = rendered; @@ -436,6 +454,7 @@ } break; default: + saveLogs(i18n.__("There is something new coming from impress.js.")); console.log(i18n.__("There is something new coming from impress.js.")); } }); diff --git a/main.js b/main.js index 0ba5166..bcb4a1b 100644 --- a/main.js +++ b/main.js @@ -84,7 +84,7 @@ function saveTemplates() { "jsPath": path.resolve(app.getAppPath(), "./js"), "consolePath": JSON.stringify(path.resolve(app.getAppPath(), "./js/console-script.js")), "projectorPath": JSON.stringify(path.resolve(app.getAppPath(), "./js/projector-script.js")), - "i18n": function() { // This is a function that translates i18n found in templates. + "i18n": function() { // This is a function that translates {{{i18n}}} strings found in templates. return function(text, render) { return render(i18n.__(text)); }; @@ -94,36 +94,39 @@ function saveTemplates() { localeViewerFake = ms.render(tplViewerFake, mustacheOptions); fs.writeFile(path.resolve(app.getPath('userData'), './viewer.html'), localeViewerFake, (err) => { if (err) throw err; - fs.readFile(path.resolve(__dirname, './templates/console.tpl'), 'utf8', (err, tplConsole) => { // Read console.tpl (main console interface) asynchronously - if (err) throw err; - let localeConsole = ms.render(tplConsole, mustacheOptions); - fs.writeFile(path.resolve(app.getPath('userData'), './console.html'), localeConsole, (err) => { - if (err) throw err; - createWindow(); - }); - }); - fs.readFile(path.resolve(__dirname, './templates/projector.tpl'), 'utf8', (err, tplProjector) => { - let localeProjector = ms.render(tplProjector, mustacheOptions); - fs.writeFile(path.resolve(app.getPath('userData'), './projector.html'), localeProjector, (err) => { - if (err) throw err; - createProjector(); - }); - }); + }); + }); + fs.readFile(path.resolve(__dirname, './templates/console.tpl'), 'utf8', (err, tplConsole) => { // Read console.tpl (main console interface) asynchronously + if (err) throw err; + let localeConsole = ms.render(tplConsole, mustacheOptions); + fs.writeFile(path.resolve(app.getPath('userData'), './console.html'), localeConsole, (err) => { + if (err) throw err; + }); + }); + fs.readFile(path.resolve(__dirname, './templates/projector.tpl'), 'utf8', (err, tplProjector) => { + let localeProjector = ms.render(tplProjector, mustacheOptions); + fs.writeFile(path.resolve(app.getPath('userData'), './projector.html'), localeProjector, (err) => { + if (err) throw err; }); }); } function storeWindowState() { - windowState.main.isMaximized = impWindows.main.isMaximized(); - windowState.projector.isMaximized = impWindows.projector.isMaximized(); - if (!windowState.main.isMaximized) { - // only update bounds if the window isn't currently maximized - windowState.main.bounds = impWindows.main.getBounds(); + if (typeof(impWindows.main) === "object") { + windowState.main.isMaximized = impWindows.main.isMaximized(); + if (!windowState.main.isMaximized) { + // only update bounds if the window isn't currently maximized + windowState.main.bounds = impWindows.main.getBounds(); + } } - if (!windowState.projector.isMaximized) { - // only update bounds if the window isn't currently maximized - windowState.projector.bounds = impWindows.projector.getBounds(); + if (typeof(impWindows.projector) === "object") { + windowState.projector.isMaximized = impWindows.projector.isMaximized(); + if (!windowState.projector.isMaximized) { + // only update bounds if the window isn't currently maximized + windowState.projector.bounds = impWindows.projector.getBounds(); + } } + settings.set('windowstate', windowState); }; @@ -142,7 +145,7 @@ function createWindow() { backgroundColor: '#13132A' }); - if (windowState.main !== "undefined" && windowState.main.isMaximized) { + if (typeof(windowState.main) === "object" && windowState.main.isMaximized) { impWindows.main.maximize(); } @@ -156,6 +159,12 @@ function createWindow() { impWindows.main.on('ready-to-show', function() { impWindows.main.show(); impWindows.main.focus(); + impWindows.main.on('resize', function() { + storeWindowState(); + }); + impWindows.main.on('move', function() { + storeWindowState(); + }); }); impWindows.main.webContents.on('did-frame-finish-load', function() { @@ -163,11 +172,10 @@ function createWindow() { }); impWindows.main.on('close', event => { - storeWindowState(); event.preventDefault(); //this prevents it from closing. The `closed` event will not fire now impWindows.main.webContents.send('quitModal'); - ipcMain.on('reallyQuit', (event) => { + storeWindowState(); app.exit(); }); }); @@ -178,12 +186,7 @@ function createWindow() { // when you should delete the corresponding element. impWindows.main = null; }); - impWindows.main.on('resize', function() { - storeWindowState(); - }); - impWindows.main.on('move', function() { - storeWindowState(); - }); + if (debugMode) { impWindows.main.webContents.openDevTools() } @@ -204,10 +207,6 @@ function createProjector() { show: false }); - if (windowState.projector !== "undefined" && windowState.projector.isMaximized) { - impWindows.projector.maximize(); - } - // and load the index.html of the app. impWindows.projector.loadURL(url.format({ pathname: path.resolve(app.getPath('userData'), './projector.html'), @@ -223,22 +222,7 @@ function createProjector() { // when you should delete the corresponding element. impWindows.projector = null; }); - impWindows.projector.on('close', event => { - event.preventDefault(); //this prevents it from closing. The `closed` event will not fire now - impWindows.main.webContents.send('buttonSwitch', "projectorBtn", false); - impWindows.main.webContents.send('buttonSwitch', "fullscreenBtn", false); - impWindows.projector.hide(); - }); - impWindows.projector.on('leave-full-screen', () => { - impWindows.main.webContents.send('buttonSwitch', "fullscreenBtn", false); - }); - impWindows.projector.on('enter-full-screen', () => { - impWindows.main.webContents.send('buttonSwitch', "fullscreenBtn", true); - impWindows.main.webContents.send('buttonSwitch', "projectorBtn", true); - }); - impWindows.projector.webContents.on('did-finish-load', () => { - - }); + // Window positioning and size impWindows.projector.on('resize', () => { storeWindowState(); const [width, height] = impWindows.projector.getContentSize(); @@ -258,6 +242,11 @@ function createProjector() { impWindows.projector.on('move', function() { storeWindowState(); }); + + if (typeof(windowState.projector) === "object" && windowState.projector.isMaximized) { + impWindows.projector.maximize(); + } + ipcMain.on('toggleFullscreen', (event) => { if (impWindows.projector.isFullScreen()) { impWindows.projector.setFullScreen(false); @@ -266,24 +255,56 @@ function createProjector() { } }); + impWindows.projector.webContents.on('did-finish-load', () => { + impWindows.projector.hide(); + }); + if (debugMode) { + impWindows.projector.webContents.openDevTools() + } +} + +function setupProjectorButtons() { + // Button events + impWindows.projector.on('hide', event => { + impWindows.main.webContents.send('buttonSwitch', "projectorBtn", false); + impWindows.main.webContents.send('buttonSwitch', "fullscreenBtn", false); + }); + + impWindows.projector.on('show', event => { + impWindows.main.webContents.send('buttonSwitch', "projectorBtn", true); + }); + + impWindows.projector.on('close', event => { + event.preventDefault(); //this prevents it from closing. The `closed` event will not fire now + impWindows.projector.hide(); + }); + impWindows.projector.on('leave-full-screen', () => { + impWindows.main.webContents.send('buttonSwitch', "fullscreenBtn", false); + }); + impWindows.projector.on('enter-full-screen', () => { + impWindows.main.webContents.send('buttonSwitch', "fullscreenBtn", true); + impWindows.main.webContents.send('buttonSwitch', "projectorBtn", true); + }); + ipcMain.on('toggleProjector', (event) => { if (impWindows.projector.isVisible()) { impWindows.projector.hide(); - impWindows.main.webContents.send('buttonSwitch', "projectorBtn", false); } else { impWindows.projector.show(); - impWindows.main.webContents.send('buttonSwitch', "projectorBtn", true); + } }); - if (debugMode) { - impWindows.projector.webContents.openDevTools() - } } // 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', saveTemplates); +app.on('ready', function() { + saveTemplates(); + createWindow(); + createProjector(); + setupProjectorButtons(); +}); //app.on('ready', ); // Quit when all windows are closed. @@ -295,8 +316,6 @@ app.on('window-all-closed', function() { } }); - - app.on('activate', function() { // On OS X it's common to re-create a window in the app when the // dock icon is clicked and there are no other windows open.