Skip to content

Commit

Permalink
fix: now electron app handle macos app reopening correctly (perhaps) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthecat authored Nov 8, 2024
1 parent 5c798f2 commit c044633
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 51 deletions.
4 changes: 1 addition & 3 deletions src/main/factories/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export function registerDeepLinkProtocol() {
}
}

export function processUrl(url: string, mainWindow?: BrowserWindow) {
if (!mainWindow) return;

export function processUrl(url: string, mainWindow: BrowserWindow) {
const parsed = new URL(url);
if (parsed.protocol !== `${APP_CONFIG.ELECTRON_PROTOCOL}:`) return;

Expand Down
30 changes: 0 additions & 30 deletions src/main/factories/setup.ts

This file was deleted.

55 changes: 37 additions & 18 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import 'source-map-support/register';

import { type BrowserWindow, app } from 'electron';

import { APP_CONFIG } from '../../app.config';
import { type BrowserWindow, app, session } from 'electron';
import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer';

import { runAppSingleInstance } from './factories/instance';
import { setupLogger } from './factories/logs';
import { processUrl, registerDeepLinkProtocol } from './factories/protocol';
import { setupApplication } from './factories/setup';
import { setupAutoUpdater } from './factories/updater';
import { createWindow } from './factories/window';
import { ENVIRONMENT } from './shared/constants/environment';
Expand All @@ -18,47 +16,68 @@ runAppSingleInstance(async () => {
setupAutoUpdater();
registerDeepLinkProtocol();

app.commandLine.appendSwitch('force-color-profile', 'srgb');
app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');
if (ENVIRONMENT.IS_DEV || ENVIRONMENT.IS_STAGE) {
app.commandLine.appendSwitch('ignore-certificate-errors');
}
app.commandLine.appendSwitch('force-color-profile', 'srgb');

app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true';
delete process.env.ELECTRON_ENABLE_SECURITY_WARNINGS;

if (PLATFORM.IS_LINUX) {
app.disableHardwareAcceleration();
}

// eslint-disable-next-line prefer-const
let mainWindow: BrowserWindow | undefined;
await app.whenReady();

let mainWindow: BrowserWindow | null = createWindow();

if (PLATFORM.IS_MAC) {
// Protocol handler for macos
app.on('open-url', (event, url) => {
event.preventDefault();
processUrl(url, mainWindow);
if (mainWindow) {
processUrl(url, mainWindow);
}
});
}

if (PLATFORM.IS_WINDOWS || PLATFORM.IS_LINUX) {
// Protocol handler for win32/Linux
app.on('second-instance', (_, commandLine) => {
app.on('second-instance', () => {
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}

const url = commandLine[commandLine.length - 1];
if (!url.startsWith(APP_CONFIG.ELECTRON_PROTOCOL + '://')) return;

processUrl(url, mainWindow);
});
}

await app.whenReady();
app.on('activate', async () => {
if (mainWindow === null) {
mainWindow = createWindow();
}
});

app.on('web-contents-created', (_, contents) =>
contents.on('will-navigate', (event) => !ENVIRONMENT.IS_DEV && event.preventDefault()),
);

app.on('window-all-closed', () => {
if (!PLATFORM.IS_MAC) {
app.quit();
}

mainWindow = createWindow();
await setupApplication(mainWindow);
mainWindow?.destroy();
mainWindow = null;
});

if (ENVIRONMENT.IS_DEV) {
await installExtension(REACT_DEVELOPER_TOOLS);

// Reloading extensions for correct initialization in dev tools
session.defaultSession.getAllExtensions().map((e) => {
session.defaultSession.loadExtension(e.path);
});
}
});

0 comments on commit c044633

Please sign in to comment.