[ref:] https://www.electronjs.org/docs/latest/api/browser-window#event-close

 

BrowserWindow | Electron

Create and control browser windows.

www.electronjs.org

const dialogPop = require('electron').remote.dialog;
// it starts with onbeforeunload with the boolean value true
let doNotCloseWindow = true;

window.onbeforeunload = (event) => {

    //if the boolean value is true, the window do not close
    if(doNotCloseWindow)
    {
        // equivalent to `return false` but not recommended
        // it will prevent the window to be closed
        event.returnValue = true; 
   
        //open popup
        const dialogOpts = {
            type: 'info',                
            title: 'close',
            message: 'r u sure?',
            buttons: ['OK', 'Cancel']
        };

        dialogPop.showMessageBox(dialogOpts).then((response) => {

            // if OK, response.resonse = 0, else 1
            if(response.response === 0){
                doNotCloseWindow = false;
                // it will call onbeforeunload again
                window.close();
            }      

        });
    }
    else
    {
        console.log('It should be closed')

        //the window closes no matter what.. so not sure if destroy() works
        window.destroy();
    }
}
Posted by matrim

댓글을 달아 주세요