IT/Electron

[Electron] exe 창 하나만 뜨게하기

월공 2021. 4. 2. 16:39
728x90
300x250

requestSingleInstanceLock() 라는것으로 창이 하나인지 그 이상인지 체크할수있다

 

main.js

const { app, BrowserWindow, Menu } = require('electron');

app.on('ready', () => {
    let mainWindow =  new BrowserWindow({
        width: 500,
        height: 500,
        resizable: false,
        webPreferences: {      
          preload: path.join(__dirname, 'preload.js'),
          nodeIntegration: true,
          contextIsolation: false,
          enableRemoteModule: true,
          webviewTag: true
        }
    });
  
    const onlyOne = app.requestSingleInstanceLock();

    if (!onlyOne) {      
      app.quit();
      app.exit();
    } else {
      app.on('second-instance', () => {    
      if (mainWindow) {
        if (mainWindow.isMinimized() || !mainWindow.isVisible()) {
          mainWindow.show();
        }
        mainWindow.focus();
      }
    });
            
    mainWindow.on("close", e => {
    if (mainWindow.isVisible()) {
        mainWindow.hide();
        e.preventDefault();
      }
    });

    //mainWindow.webContents.openDevTools()
    mainWindow.loadFile('index.html')    
} 
});

 

728x90
300x250