57
rated 0 times
[
61]
[
4]
/ answers: 1 / hits: 28287
/ 7 Years ago, thu, march 23, 2017, 12:00:00
Instead of quit the application, I'd like to hide the main window when the system close button is clicked and show the main window when the application is clicked or activate. I'm using the following code to do this on my Electron app:
'use strict'
import { app, BrowserWindow } from 'electron'
let mainWindow
const winURL = process.env.NODE_ENV === 'development'
? `http://localhost:${require('../../../config').port}`
: `file://${__dirname}/index.html`
function createWindow () {
/**
* Initial window options
*/
mainWindow = new BrowserWindow({
height: 620,
width: 350,
resizable: true,
fullscreenable: true
})
mainWindow.loadURL(winURL)
mainWindow.on('closed', () => {
mainWindow.hide()
})
// eslint-disable-next-line no-console
console.log('mainWindow opened')
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
mainWindow.show()
})
But, hiding and showing the window from the activate
and closed
event shows the following error and never show the main window when the application is active.
Uncaught Exception:
Error: Object has been destroyed
at BrowserWindow.<anonymous> (/app/src/main/index.js:24:16): mainWindow.on('closed')
Not sure what else to do.
More From » electron