chore: implemented multiple instances instead of multiple appications? (#4733)
This commit is contained in:
		
							
								
								
									
										33
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								src/main.ts
									
									
									
									
									
								
							@ -23,6 +23,15 @@ import argvFromYargs from './commandLineArgs'
 | 
			
		||||
 | 
			
		||||
let mainWindow: BrowserWindow | null = null
 | 
			
		||||
 | 
			
		||||
// Supporting multiple instances instead of multiple applications
 | 
			
		||||
let cmdQPressed = false
 | 
			
		||||
const instances: BrowserWindow[] = []
 | 
			
		||||
const gotTheLock = app.requestSingleInstanceLock()
 | 
			
		||||
if (!gotTheLock) {
 | 
			
		||||
  app.quit()
 | 
			
		||||
  process.exit(0)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Check the command line arguments for a project path
 | 
			
		||||
const args = parseCLIArgs()
 | 
			
		||||
 | 
			
		||||
@ -117,16 +126,34 @@ const createWindow = (filePath?: string): BrowserWindow => {
 | 
			
		||||
 | 
			
		||||
  newWindow.show()
 | 
			
		||||
 | 
			
		||||
  instances.push(newWindow)
 | 
			
		||||
  return newWindow
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// before-quit with multiple instances
 | 
			
		||||
if (process.platform === 'darwin') {
 | 
			
		||||
  // Quit from the dock context menu should quit the application directly
 | 
			
		||||
  app.on('before-quit', () => {
 | 
			
		||||
    cmdQPressed = true
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Quit when all windows are closed, even on macOS. There, it's common
 | 
			
		||||
// for applications and their menu bar to stay active until the user quits
 | 
			
		||||
// explicitly with Cmd + Q, but it is a really weird behavior with our app.
 | 
			
		||||
// app.on('window-all-closed', () => {
 | 
			
		||||
//   app.quit()
 | 
			
		||||
// })
 | 
			
		||||
app.on('window-all-closed', () => {
 | 
			
		||||
  app.quit()
 | 
			
		||||
  if (cmdQPressed || process.platform !== 'darwin') {
 | 
			
		||||
    app.quit()
 | 
			
		||||
  }
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
// Various actions can trigger this event, such as launching the application for the first time,
 | 
			
		||||
// attempting to re-launch the application when it's already running, or clicking on the application's dock or taskbar icon.
 | 
			
		||||
app.on('activate', () => createWindow())
 | 
			
		||||
 | 
			
		||||
// This method will be called when Electron has finished
 | 
			
		||||
// initialization and is ready to create browser windows.
 | 
			
		||||
// Some APIs can only be used after this event occurs.
 | 
			
		||||
@ -135,6 +162,10 @@ app.on('ready', (event, data) => {
 | 
			
		||||
  mainWindow = createWindow()
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
// This event will be emitted inside the primary instance of your application when a second instance
 | 
			
		||||
// has been executed and calls app.requestSingleInstanceLock().
 | 
			
		||||
app.on('second-instance', (event, argv, workingDirectory) => createWindow())
 | 
			
		||||
 | 
			
		||||
// For now there is no good reason to separate these out to another file(s)
 | 
			
		||||
// There is just not enough code to warrant it and further abstracts everything
 | 
			
		||||
// which is already quite abstracted
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user