Skip to content

Commit

Permalink
feat: Handling windows.squirrel events in main process
Browse files Browse the repository at this point in the history
  • Loading branch information
Lalit3716 committed Oct 21, 2022
1 parent 9e34c4a commit 4211292
Showing 1 changed file with 98 additions and 22 deletions.
120 changes: 98 additions & 22 deletions electron/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,71 @@ const {
const isDev = require("electron-is-dev");
const { wrap: catchError } = require("./utils");

function handleSquirrelEvent() {
if (process.argv.length === 1) {
return false;
}

const ChildProcess = require('child_process');
const path = require('path');

const appFolder = path.resolve(process.execPath, '..');
const rootAtomFolder = path.resolve(appFolder, '..');
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'));
const exeName = path.basename(process.execPath);

const spawn = function(command, args) {
let spawnedProcess, error;

try {
spawnedProcess = ChildProcess.spawn(command, args, {detached: true});
} catch (error) {}

return spawnedProcess;
};

const spawnUpdate = function(args) {
return spawn(updateDotExe, args);
};

const squirrelEvent = process.argv[1];
switch (squirrelEvent) {
case '--squirrel-install':
case '--squirrel-updated':
// Optionally do things such as:
// - Add your .exe to the PATH
// - Write to the registry for things like file associations and
// explorer context menus

// Install desktop and start menu shortcuts
spawnUpdate(['--createShortcut', exeName]);

setTimeout(app.quit, 1000);
return true;

case '--squirrel-uninstall':
// Undo anything you did in the --squirrel-install and
// --squirrel-updated handlers

// Remove desktop and start menu shortcuts
spawnUpdate(['--removeShortcut', exeName]);

setTimeout(app.quit, 1000);
return true;

case '--squirrel-obsolete':
// This is called on the outgoing version of your app before
// we update to the new version - it's the opposite of
// --squirrel-updated

app.quit();
return true;

default:
return false;
}
};

const createWindow = () => {
const win = new BrowserWindow({
minWidth: 1280,
Expand Down Expand Up @@ -55,28 +120,39 @@ const addHandlers = () => {
ipcMain.handle("createVault", catchError(createTable));
};

app.whenReady().then(async () => {
try {
await connect(app);
await createTable();
createWindow();

// Renderer-to-main two-way process communication
addHandlers();
} catch (err) {
dialog.showErrorBox("Error", err.message);
app.quit();
}
});
const main = async () => {
if (require('electron-squirrel-startup')) return app.quit();

app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
if (handleSquirrelEvent()) {
return;
}
});

app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
app.whenReady().then(async () => {
try {
await connect(app);
await createTable();
createWindow();

// Renderer-to-main two-way process communication
addHandlers();
} catch (err) {
dialog.showErrorBox("Error", err.message);
app.quit();
}
});

app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});

app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});

}

main();

0 comments on commit 4211292

Please sign in to comment.