Skip to content

Commit

Permalink
refactor window implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoecheza committed Feb 13, 2025
1 parent 750ac0f commit 0112dd2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
8 changes: 4 additions & 4 deletions packages/main/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { app } from 'electron';
import { restoreOrCreateWindow } from '/@/mainWindow';
import { restoreOrCreateMainWindow } from '/@/mainWindow';
import { platform } from 'node:process';
import updater from 'electron-updater';
import log from 'electron-log/main';
Expand All @@ -21,7 +21,7 @@ if (!isSingleInstance) {
app.quit();
process.exit(0);
}
app.on('second-instance', restoreOrCreateWindow);
app.on('second-instance', restoreOrCreateMainWindow);

/**
* Shut down background process if all windows was closed
Expand All @@ -36,7 +36,7 @@ app.on('window-all-closed', async () => {
/**
* @see https://www.electronjs.org/docs/latest/api/app#event-activate-macos Event: 'activate'.
*/
app.on('activate', restoreOrCreateWindow);
app.on('activate', restoreOrCreateMainWindow);

/**
* Create the application window when app is ready.
Expand All @@ -49,7 +49,7 @@ app
log.info(`[App] Ready v${app.getVersion()}`);
initIpc();
log.info('[IPC] Ready');
await restoreOrCreateWindow();
await restoreOrCreateMainWindow();
log.info('[BrowserWindow] Ready');
const analytics = await getAnalytics();
if (analytics) {
Expand Down
55 changes: 29 additions & 26 deletions packages/main/src/mainWindow.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import { app, BrowserWindow } from 'electron';
import { app } from 'electron';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';

async function createWindow() {
const browserWindow = new BrowserWindow({
show: false, // Use the 'ready-to-show' event to show the instantiated BrowserWindow.
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
sandbox: false, // Sandbox disabled because the demo of preload script depend on the Node.js api
webviewTag: false, // The webview tag is not recommended. Consider alternatives like an iframe or Electron's BrowserView. @see https://www.electronjs.org/docs/latest/api/webview-tag#warning
preload: join(app.getAppPath(), 'packages/preload/dist/index.mjs'),
},
});
import { createWindow, getWindowById, WindowId } from './modules/window';

browserWindow.setMenuBarVisibility(false);
browserWindow.maximize();
async function createMainWindow() {
const window = createWindow(WindowId.Main);
window.setMenuBarVisibility(false);
window.maximize();

/**
* If the 'show' property of the BrowserWindow's constructor is omitted from the initialization options,
Expand All @@ -25,11 +17,11 @@ async function createWindow() {
*
* @see https://github.com/electron/electron/issues/25012 for the afford mentioned issue.
*/
browserWindow.on('ready-to-show', () => {
browserWindow?.show();
window.on('ready-to-show', () => {
window?.show();

if (import.meta.env.DEV) {
browserWindow?.webContents.openDevTools();
window?.webContents.openDevTools();
}
});

Expand All @@ -40,7 +32,7 @@ async function createWindow() {
/**
* Load from the Vite dev server for development.
*/
await browserWindow.loadURL(import.meta.env.VITE_DEV_SERVER_URL);
await window.loadURL(import.meta.env.VITE_DEV_SERVER_URL);
} else {
/**
* Load from the local file system for production and test.
Expand All @@ -51,22 +43,33 @@ async function createWindow() {
* @see https://github.com/nodejs/node/issues/12682
* @see https://github.com/electron/electron/issues/6869
*/
await browserWindow.loadFile(
fileURLToPath(new URL('./../../renderer/dist/index.html', import.meta.url)),
);
await window.loadFile(
fileURLToPath(
new URL(join(app.getAppPath(), 'packages/renderer/dist/index.html'), import.meta.url),
),
);
}

return browserWindow;
return window;
}

/**
* Restore an existing BrowserWindow or Create a new BrowserWindow.
* Restores an existing main window or creates a new one if none exists.
* This function ensures only one main window is active at a time.
*
* The function will:
* 1. Check if a main window already exists
* 2. Create a new window if none exists
* 3. Restore the window if it's minimized
* 4. Focus the window to bring it to the foreground
*
* @returns {Promise<Electron.BrowserWindow>} A promise that resolves to the main window instance
*/
export async function restoreOrCreateWindow() {
let window = BrowserWindow.getAllWindows().find(w => !w.isDestroyed());
export async function restoreOrCreateMainWindow() {
let window = getWindowById(WindowId.Main);

if (window === undefined) {
window = await createWindow();
window = await createMainWindow();
}

if (window.isMinimized()) {
Expand Down
35 changes: 35 additions & 0 deletions packages/main/src/modules/window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { app, BrowserWindow, type BrowserWindowConstructorOptions } from 'electron';
import { join } from 'node:path';

export enum WindowId {
Main = 'main'
}

const windowMap = new Map<WindowId, BrowserWindow>();

export function createWindow(id: WindowId, options?: BrowserWindowConstructorOptions) {
const browserWindow = new BrowserWindow({
show: false, // Use the 'ready-to-show' event to show the instantiated BrowserWindow.
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
sandbox: false, // Sandbox disabled because the demo of preload script depend on the Node.js api
webviewTag: false, // The webview tag is not recommended. Consider alternatives like an iframe or Electron's BrowserView. @see https://www.electronjs.org/docs/latest/api/webview-tag#warning
preload: join(app.getAppPath(), 'packages/preload/dist/index.mjs'),
...options,
},
});

// Setup window map
windowMap.set(id, browserWindow);
browserWindow.on('closed', () => {
windowMap.delete(id);
});
// Setup window map

return browserWindow;
}

export function getWindowById(id: WindowId): BrowserWindow | undefined {
return windowMap.get(id);
}

0 comments on commit 0112dd2

Please sign in to comment.