Skip to content

setupWindowHooks parameter to include context for better window management #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ vitest.config.*.timestamp*
.history

build
out
out
pnpm-lock.yaml
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ app.whenReady().then(() => {
setupWindowRouter({
preload: join(__dirname, '../preload/index.js'),
mainWindow,
setupWindowHooks: (browserWindow) => {
setupWindowHooks: (browserWindow, context) => {
browserWindow.webContents.setWindowOpenHandler((details) => {
shell.openExternal(details.url);
return { action: 'deny' };
});

console.log(context); // To get the window config and id

if (process.env['ELECTRON_RENDERER_URL']) {
browserWindow.loadURL(process.env['ELECTRON_RENDERER_URL']);
} else {
Expand Down
12 changes: 10 additions & 2 deletions src/main/setupWindowRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ type Windows = Record<
}
>;

export type WindowHooksContext = {
id?: string;
windowConfig?: PublicWindowConfig;
};

export function setupWindowRouter({
preload,
mainWindow,
Expand All @@ -26,7 +31,7 @@ export function setupWindowRouter({
preload: string;
mainWindow: BrowserWindow;
windows?: Windows;
setupWindowHooks?: (browserWindow: BrowserWindow) => unknown;
setupWindowHooks?: (browserWindow: BrowserWindow, context?: WindowHooksContext) => unknown;
}) {
const windowMap = new Map<string, BrowserWindow>();
const windowConfigsFromRenderer = new Map<string, PublicWindowConfig>();
Expand Down Expand Up @@ -59,7 +64,10 @@ export function setupWindowRouter({
...constructorOptions?.webPreferences,
},
});
setupWindowHooks?.(newWindow);
setupWindowHooks?.(newWindow, {
id: arg.id,
windowConfig: windowConfigFromArg,
});
windowConfig?.extraWindowHooks?.(newWindow);
newWindow.on('ready-to-show', () => {
newWindow.webContents.send(
Expand Down