Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Commit

Permalink
Improvements to file opening on macOS; 0.5.2
Browse files Browse the repository at this point in the history
Opening with "Open with" doesn't work yet.
  • Loading branch information
krzysdz committed Feb 22, 2019
1 parent 4dd20e5 commit aa8ce39
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dist
node_modules
publish_gh*
.DS_Store
*/.DS_Store
42 changes: 37 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ const {autoUpdater} = require("electron-updater");
// display notifications on Windows 10 and 8/8.1 used eg. by autoUpdater.
app.setAppUserModelId("pl.dziembala-mazur.szkola-puzzle");

// 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.
// If the window doesn't exist yet add the filename to argv
function firstOpenListener(e, p) {
e.preventDefault();
process.argv.push(p);
}
app.on("open-file", firstOpenListener);

/**
* 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.
* @type {Electron.BrowserWindow}
*/
let win;

function createWindow () {
Expand Down Expand Up @@ -48,12 +58,30 @@ function createWindow () {
// when you should delete the corresponding element.
win = null;
});

// Remove all open file event listeners
app.removeAllListeners("open-file");
// Now the window exists, so message can be sent to the renderer
app.on("open-file", (e, p) => {
e.preventDefault();
win.webContents.send("openFile", p);
});
}

app.on("open-file", (e, p) => {
/**
* Listen to open-file events while all windows are closed on macOS
* @param {Electron.Event} e "open-file" event
* @param {string} p path to file
*/
function windowClosedOpenFile(e, p){
e.preventDefault();
process.argv.push(p);
});
if(process.argv[1] === "." || process.argv[1] === "./" || process.argv[1] === ".\\"){ // if run from CLI (`electron .` or `electron ./`)
process.argv[2] = p;
} else {
process.argv[1] = p;
}
createWindow();
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
Expand All @@ -66,6 +94,10 @@ app.on("window-all-closed", () => {
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
app.quit();
} else {
// If on macOS remove open-file event listeners
app.removeAllListeners("open-file");
app.addListener("open-file", windowClosedOpenFile);
}
});

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "szkola-puzzle",
"version": "0.5.1",
"version": "0.5.2",
"description": "Puzzle dla p. Cwołek",
"author": {
"name": "krzysdz",
Expand Down
9 changes: 8 additions & 1 deletion renderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
const remote = require("electron").remote;
const {remote, ipcRenderer} = require("electron");
const fs = require("fs");
const path = require("path");
const gameScr = require("./game");
Expand Down Expand Up @@ -353,6 +353,13 @@ function showVersion(){
document.getElementById("appVer").appendChild(document.createTextNode(appVersion));
}

// Listen to openFile messages from main
ipcRenderer.on("openFile", (_event, filePath) => {
// Check if the file has supported extension
if (supportedFileTypes.includes(path.extname(filePath).toLowerCase())) {
loadFile([filePath]);
}
});

/**
* @typedef {Object} Tile
Expand Down

0 comments on commit aa8ce39

Please sign in to comment.