From 4cbc2bf38408133487746e558c7eb666b2713dff Mon Sep 17 00:00:00 2001 From: Michael Ochmann Date: Sun, 2 Oct 2022 11:13:43 +0200 Subject: [PATCH] made opening files asynchronous between render and bg process addresses #2 --- contextAPI.js | 3 ++- src/Ation.js | 8 ++++++++ src/WindowManager.js | 16 ++++++++++++---- src/ui/src/components/Ation.js | 19 ++++++++++++------- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/contextAPI.js b/contextAPI.js index 568d22c..c8d17fb 100644 --- a/contextAPI.js +++ b/contextAPI.js @@ -3,5 +3,6 @@ const {contextBridge, ipcRenderer} = require("electron"); contextBridge.exposeInMainWorld("api", { - openFile : async () => ipcRenderer.invoke("WindowManager::openFile") + openFileDialog : () => ipcRenderer.send("WindowManager::openFileDialog"), + openFile : callback => ipcRenderer.on("Ation::openFile", (_, presentation) => callback(presentation)) }); \ No newline at end of file diff --git a/src/Ation.js b/src/Ation.js index 43852cb..78caad7 100644 --- a/src/Ation.js +++ b/src/Ation.js @@ -10,14 +10,22 @@ const {parser} = require("./Parser"); class Ation { windowManager; mainMenu; + fileToOpen; constructor() { if (Ation.Instances > 0) throw new Error("Only one Instance of Ation possible"); + this.fileToOpen = null; this.windowManager = new WindowManager(this); this.mainMenu = new MainMenu(this); + app.on("open-file", (_, path) => { + this.fileToOpen = path; + }); + app.whenReady().then(async () => { + if (this.fileToOpen) + dialog.showMessageBox(this.windowManager.mainWindow, {message : this.fileToOpen}); protocol.registerFileProtocol("slideimg", (request, callback) => { const uri = request.url.replace(/^slideimg:\/\//, ""); const extension = path.extname(uri).replace(/^\./, ""); diff --git a/src/WindowManager.js b/src/WindowManager.js index 11a6967..558e510 100644 --- a/src/WindowManager.js +++ b/src/WindowManager.js @@ -1,9 +1,8 @@ "use strict"; -const {app, BrowserWindow, ipcMain, dialog, Menu} = require("electron"); -const path = require("path"); -const fs = require("fs/promises"); -const util = require("util"); +const {app, BrowserWindow, ipcMain, globalShortcut} = require("electron"); +const path = require("path"); +const util = require("util"); const {isDevelopment} = require("./Util"); @@ -28,6 +27,15 @@ class WindowManager { titleBarStyle : "hiddenInset" }); + if (isDevelopment) { + globalShortcut.register("CommandOrControl+I", () => { + this.mainWindow.toggleDevTools(); + }); + globalShortcut.register("CommandOrControl+R", () => { + this.mainWindow.reload(); + }); + } + if (isDevelopment()) this.mainWindow.loadURL("http://localhost:3000"); else diff --git a/src/ui/src/components/Ation.js b/src/ui/src/components/Ation.js index bb3562f..5945a0f 100644 --- a/src/ui/src/components/Ation.js +++ b/src/ui/src/components/Ation.js @@ -18,13 +18,18 @@ const Ation = () => { const [basePath, setBasePath] = useState(""); const [showTips, setShowTips] = useState(false); - const openFile = async () => { - const [basePath, slideDeck] = await window.api.openFile(); - - if (!slideDeck) - return; - setBasePath(basePath); - setDeck(slideDeck); + useEffect(() => { + window.api.openFile(presentation => { + const [basePath, slideDeck] = presentation; + if (!slideDeck) + return; + setBasePath(basePath); + setDeck(slideDeck); + }); + }, []); + + const openFile = () => { + window.api.openFileDialog(); } if (deck.length < 1)