How to close an electronic application through javascript?

I am launching an express application via electron.

Below is main.js

   const electron = require("electron"),
          app = electron.app,
          BrowserWindow = electron.BrowserWindow;

    let mainWindow;

    function createWindow () {
      mainWindow = new BrowserWindow({
          width: 1200,
        height: 800,
        frame: false,
        kiosk: true
      });
      mainWindow.loadURL(`file://${__dirname}/index.html`)
     mainWindow.webContents.openDevTools();

      mainWindow.on("closed", function () {
        mainWindow = null;
      })
    }

    app.on("ready", createWindow);
    app.on("browser-window-created",function(e,window) {
      window.setMenu(null);
    });

    app.on("window-all-closed", function () {
      if (process.platform !== "darwin") {
        app.quit();
      }
    });



    app.on("activate", function () {
      if (mainWindow === null) {
        createWindow();
      }
    });

and below - a button in the view that when I click I would like to close the application

<button id="close-btn"><i class="fa fa-cube" aria-hidden="true"></i>&nbsp; Close application</button>

Essentially, I want to activate this part of the code after clicking the button

  app.on("window-all-closed", function () {
          if (process.platform !== "darwin") {
            app.quit();
          }
        });
+10
source share
4 answers

you can use

const remote = require('electron').remote
let w = remote.getCurrentWindow()
w.close()

to close your application.

if you use jQuery

const remote = require('electron').remote
$('#close-btn').on('click', e => {
    remote.getCurrentWindow().close()
})

if you use Vue.js

<template>
    <button @click="close"><i class="fa fa-cube" aria-hidden="true"></i>&nbsp; Close application</button>
</template>

<script>
    const remote = require('electron').remote

    export default{
        data(){
            return {
                w: remote.getCurrentWindow(),
            }
        },
        methods: {
            close() {
                this.w.close()
            }
        }
    }
</script>
+17
source

I use the line below to close the application.

window.close()

+6
source

main.js

function createWindow(){
win =new BrowserWindow({
    icon:'./img/code_file.ico',
    frame:false
});
win.maximize();

win.loadURL(url.format({
    pathname:path.join(__dirname,'./login.html'),
    protocol:'file:',
    slashes:true
}));
  // Emitted when the window is closed.
  win.on('closed', function () {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    win = null
  });
}
app.on('ready',createWindow);
app.on('Window-all-closed',()=>{
if(process.platfrom!=='drawin'){
app.quit();
}
});
}

, app.quit() ;

0

app.exit:

app.exit(0)

This seems to bypass any event listeners and cause the whole application to exit immediately. (Be sure, this is what you want, though!)

0
source

Source: https://habr.com/ru/post/1016430/


All Articles