Skip to content

Commit

Permalink
single instance app lock
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanff committed May 8, 2024
1 parent 58a0ec8 commit c2238dc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
64 changes: 40 additions & 24 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ let window: any;
// let loadingWindow: any;
let isQuiting = false;

app.on("web-contents-created", (_event, contents) => {
contents.on("will-navigate", (event, _navigationUrl) => {
event.preventDefault();
});
});
const gotTheLock = app.requestSingleInstanceLock();

if (!gotTheLock) {
app.quit();
}

protocol.registerSchemesAsPrivileged([
{
Expand All @@ -35,13 +35,15 @@ protocol.registerSchemesAsPrivileged([
app.enableSandbox();

app.whenReady().then(async () => {
// Load react devtools in development if REACT_DEVTOOLS is set to true
if (is.dev && process.env["REACT_DEVTOOLS"] === "true") {
const { REACT_DEVELOPER_TOOLS, default: installExtension } = await import("electron-devtools-assembler");
await installExtension(REACT_DEVELOPER_TOOLS);
}
electronApp.setAppUserModelId("gf.anime");

// Set the update config path to the dev-app-update.yml in development
// Set the autoUpdater config path to the dev-app-update.yml in development
// This allows us to test the autoUpdater with releases from another repo
if (is.dev) {
autoUpdater.updateConfigPath = path.join(process.cwd(), "dev-app-update.yml");
}
Expand Down Expand Up @@ -69,11 +71,25 @@ app.whenReady().then(async () => {
tray.on("double-click", () => {
window.show();
});

app.on("before-quit", () => {
isQuiting = true;
});

app.on("second-instance", () => {
// Someone tried to run a second instance, we should focus our window.
if (window) {
if (window.isMinimized()) window.restore();
window.focus();
}
});

// Disable navigation for security purposes
app.on("web-contents-created", (_event, contents) => {
contents.on("will-navigate", (event, _navigationUrl) => {

Check warning on line 88 in src/main/index.ts

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

'_navigationUrl' is defined but never used
event.preventDefault();
});
});

/**
* An electron protocol that handles requests from the renderer process to agf:///host/path
* https://www.electronjs.org/docs/latest/api/protocol
Expand Down Expand Up @@ -277,23 +293,6 @@ app.whenReady().then(async () => {
createWindow();
});

// function createLoadingWindow() {
// loadingWindow = new BrowserWindow({
// width: 400,
// height: 300,
// frame: false,
// transparent: true,
// alwaysOnTop: true
// });
// if (is.dev) {
// loadingWindow.loadURL(join(process.env["ELECTRON_RENDERER_URL"] || "", "loading.html"));
// } else {
// loadingWindow.loadFile(join(__dirname, "../renderer/loading.html"));
// }

// loadingWindow.on("closed", () => (loadingWindow = null));
// }

function createWindow(): void {
window = new BrowserWindow({
title: "anime.gf",
Expand Down Expand Up @@ -348,3 +347,20 @@ function createWindow(): void {
window.loadFile(join(__dirname, "../renderer/index.html"));
}
}

// function createLoadingWindow() {
// loadingWindow = new BrowserWindow({
// width: 400,
// height: 300,
// frame: false,
// transparent: true,
// alwaysOnTop: true
// });
// if (is.dev) {
// loadingWindow.loadURL(join(process.env["ELECTRON_RENDERER_URL"] || "", "loading.html"));
// } else {
// loadingWindow.loadFile(join(__dirname, "../renderer/loading.html"));
// }

// loadingWindow.on("closed", () => (loadingWindow = null));
// }
2 changes: 1 addition & 1 deletion src/main/lib/store/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async function init() {
// // So we'll just create the schema_migrations table and insert the first migration
// if schema migrations table doesn't exist and a table exists "cards"
// create schema_migrations table
// insert the first migration
// insert the init migration
// return
await update();
}
Expand Down

0 comments on commit c2238dc

Please sign in to comment.