Skip to content
This repository was archived by the owner on Jul 30, 2025. It is now read-only.

Commit 5c2bc1b

Browse files
committed
fix(packages/core): second windows should not be stuck with env vars from initial window
Electron finally has support, via the `additionalData` protocol, for us to fix this.
1 parent b1226c7 commit 5c2bc1b

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

packages/core/src/main/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export const main = async (argv: string[], env = process.env, execOptions?: Exec
4242
// then spawn the electron graphics
4343
debug('shortcut to graphics', argv)
4444
const { getCommand, initElectron } = await import(/* webpackChunkName: "electron-main" */ './spawn-electron')
45-
const { argv: strippedArgv, subwindowPlease, subwindowPrefs } = getCommand(argv, async () => import('electron'))
45+
const { argv: strippedArgv, subwindowPlease, subwindowPrefs } = getCommand(argv, process.cwd(), env, async () =>
46+
import('electron')
47+
)
4648
initElectron(
4749
strippedArgv,
4850
{ isRunningHeadless },

packages/core/src/main/spawn-electron.ts

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ interface App extends EventEmitter {
5353
hide(): void
5454
quit(): void
5555
exit(exitCode?: number): void
56-
requestSingleInstanceLock(): boolean
56+
requestSingleInstanceLock(additionalData?: Record<any, any>): boolean
5757
}
5858

5959
/**
@@ -500,7 +500,12 @@ interface Command {
500500
subwindowPlease: boolean
501501
subwindowPrefs: ISubwindowPrefs
502502
}
503-
export const getCommand = (argv: string[], screen: () => Promise<{ screen: Screen; BrowserWindow: BW }>): Command => {
503+
export const getCommand = (
504+
argv: string[],
505+
cwd: string,
506+
env: Record<any, any>,
507+
screen: () => Promise<{ screen: Screen; BrowserWindow: BW }>
508+
): Command => {
504509
debug('getCommand', argv)
505510
const dashDash = argv.lastIndexOf('--')
506511
argv = dashDash === -1 ? argv.slice(1) : argv.slice(dashDash + 1)
@@ -528,6 +533,8 @@ export const getCommand = (argv: string[], screen: () => Promise<{ screen: Scree
528533

529534
let subwindowPlease = true
530535
let subwindowPrefs: ISubwindowPrefs = {
536+
cwd,
537+
env,
531538
fullscreen: true,
532539
width: windowDefaults.width,
533540
height: windowDefaults.height
@@ -537,13 +544,16 @@ export const getCommand = (argv: string[], screen: () => Promise<{ screen: Scree
537544
// use a full window for 'shell'
538545
argv = ['shell']
539546
subwindowPlease = false
540-
subwindowPrefs = {}
547+
subwindowPrefs = { cwd, env }
541548
} else if (process.env.KUI_POPUP) {
542549
argv = JSON.parse(process.env.KUI_POPUP)
550+
subwindowPrefs = { cwd, env }
543551
}
544552

545553
if (process.env.KUI_POPUP_WINDOW_RESIZE) {
546554
subwindowPrefs = {
555+
cwd,
556+
env,
547557
fullscreen: true,
548558
position: async () => getPositionForPopup(await screen()),
549559
width: popupWindowDefaults.width,
@@ -628,23 +638,31 @@ export async function initElectron(
628638
const widthFromCaller = subwindowPrefs && subwindowPrefs.width
629639
const heightFromCaller = subwindowPrefs && subwindowPrefs.height
630640

631-
app.on('second-instance', (event: Electron.Event, commandLine: string[]) => {
632-
// Someone tried to run a second instance, open a new window
633-
// to handle it
634-
635-
const { argv, subwindowPlease, subwindowPrefs } = getCommand(commandLine, async () => import('electron'))
641+
app.on(
642+
'second-instance',
643+
(event: Electron.Event, commandLine: string[], cwd: string, additionalData: { env: string }) => {
644+
// Someone tried to run a second instance, open a new window
645+
// to handle it
646+
647+
const { argv, subwindowPlease, subwindowPrefs } = getCommand(
648+
commandLine,
649+
cwd,
650+
JSON.parse(additionalData.env),
651+
async () => import('electron')
652+
)
653+
654+
if (widthFromCaller) {
655+
subwindowPrefs.width = widthFromCaller
656+
}
657+
if (heightFromCaller) {
658+
subwindowPrefs.height = heightFromCaller
659+
}
636660

637-
if (widthFromCaller) {
638-
subwindowPrefs.width = widthFromCaller
639-
}
640-
if (heightFromCaller) {
641-
subwindowPrefs.height = heightFromCaller
661+
debug('opening window for second instance', commandLine, subwindowPlease, subwindowPrefs)
662+
createWindow(true, argv, subwindowPlease, subwindowPrefs)
642663
}
643-
644-
debug('opening window for second instance', commandLine, subwindowPlease, subwindowPrefs)
645-
createWindow(true, argv, subwindowPlease, subwindowPrefs)
646-
})
647-
if (!app.requestSingleInstanceLock()) {
664+
)
665+
if (!app.requestSingleInstanceLock({ env: JSON.stringify(process.env) })) {
648666
// The primary instance of app failed to optain the lock, which means another instance of app is already running with the lock
649667
debug('exiting, since we are not the first instance')
650668
return app.exit(0)

packages/core/src/models/SubwindowPrefs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616

1717
interface SubwindowPrefs {
18+
cwd?: string
19+
env?: Record<any, any>
1820
fullscreen?: boolean
1921
useContentSize?: boolean
2022
synonymFor?: object

packages/core/src/webapp/bootstrap/init-electron.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { BrowserWindow } from 'electron'
1919

2020
interface KuiWindow extends BrowserWindow {
2121
subwindow?: {
22+
cwd?: string
23+
env?: Record<any, any>
2224
fullscreen?: boolean
2325
viewName?: string
2426
title?: string
@@ -58,6 +60,13 @@ export async function preinit() {
5860
const remote = await import('@electron/remote')
5961
const window = remote && (remote.getCurrentWindow() as KuiWindow)
6062
const subwindow = window.subwindow
63+
64+
// so that electron's "second window" protocol can pass through
65+
// the environment variables from second+ windows
66+
if (subwindow && subwindow.env) {
67+
process.env = subwindow.env
68+
}
69+
6170
if (subwindow && subwindow.fullscreen === true) {
6271
const titleOverride = typeof subwindow === 'string' ? subwindow : subwindow.title
6372
if (titleOverride && typeof titleOverride === 'string') {

0 commit comments

Comments
 (0)