2016-10-30 10:57:12 +00:00
|
|
|
const electron = require('electron'); // eslint-disable-line
|
2016-11-21 18:05:19 +00:00
|
|
|
const isDev = require('electron-is-dev');
|
2020-03-04 10:41:40 +00:00
|
|
|
const path = require('path');
|
2016-10-30 10:57:12 +00:00
|
|
|
|
|
|
|
const menu = require('./menu');
|
|
|
|
|
2018-02-17 14:15:30 +00:00
|
|
|
const { checkNewVersion } = require('./update-checker');
|
|
|
|
|
2018-09-30 20:08:36 +00:00
|
|
|
const { app } = electron;
|
|
|
|
const { BrowserWindow } = electron;
|
2016-10-30 10:57:12 +00:00
|
|
|
|
2020-02-11 14:16:03 +00:00
|
|
|
app.name = 'LosslessCut';
|
2016-11-05 20:22:31 +00:00
|
|
|
|
2016-10-30 10:57:12 +00:00
|
|
|
// Keep a global reference of the window object, if you don't, the window will
|
|
|
|
// be closed automatically when the JavaScript object is garbage collected.
|
|
|
|
let mainWindow;
|
|
|
|
|
2020-02-20 04:03:25 +00:00
|
|
|
let askBeforeClose = false;
|
|
|
|
|
2016-10-30 10:57:12 +00:00
|
|
|
function createWindow() {
|
|
|
|
mainWindow = new BrowserWindow({
|
|
|
|
darkTheme: true,
|
2019-11-04 03:32:03 +00:00
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
2020-03-04 10:41:40 +00:00
|
|
|
// https://github.com/electron/electron/issues/5107
|
|
|
|
webSecurity: !isDev,
|
2019-11-04 03:32:03 +00:00
|
|
|
},
|
2016-10-30 10:57:12 +00:00
|
|
|
});
|
2020-03-04 10:41:40 +00:00
|
|
|
|
|
|
|
mainWindow.loadURL(isDev ? 'http://localhost:3001' : `file://${path.join(__dirname, '../build/index.html')}`);
|
2016-10-30 10:57:12 +00:00
|
|
|
|
2020-02-24 10:18:44 +00:00
|
|
|
if (isDev) {
|
|
|
|
const { default: installExtension, REACT_DEVELOPER_TOOLS } = require('electron-devtools-installer'); // eslint-disable-line global-require,import/no-extraneous-dependencies
|
|
|
|
|
|
|
|
installExtension(REACT_DEVELOPER_TOOLS)
|
|
|
|
.then(name => console.log(`Added Extension: ${name}`))
|
|
|
|
.catch(err => console.log('An error occurred: ', err));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the DevTools.
|
|
|
|
// mainWindow.webContents.openDevTools()
|
|
|
|
|
2020-03-04 10:41:40 +00:00
|
|
|
// Emitted when the window is closed.
|
2016-10-30 10:57:12 +00:00
|
|
|
mainWindow.on('closed', () => {
|
|
|
|
// 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.
|
|
|
|
mainWindow = null;
|
|
|
|
});
|
2020-02-18 09:06:57 +00:00
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/39574636/prompt-to-save-quit-before-closing-window/47434365
|
|
|
|
mainWindow.on('close', (e) => {
|
2020-02-20 04:03:25 +00:00
|
|
|
if (!askBeforeClose) return;
|
|
|
|
|
2020-02-18 09:06:57 +00:00
|
|
|
const choice = electron.dialog.showMessageBoxSync(mainWindow, {
|
|
|
|
type: 'question',
|
|
|
|
buttons: ['Yes', 'No'],
|
|
|
|
title: 'Confirm quit',
|
|
|
|
message: 'Are you sure you want to quit? You will lose all unsaved work',
|
|
|
|
});
|
|
|
|
if (choice === 1) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2016-10-30 10:57:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2018-02-17 14:15:30 +00:00
|
|
|
app.on('ready', async () => {
|
2016-10-30 10:57:12 +00:00
|
|
|
createWindow();
|
2016-11-05 20:22:31 +00:00
|
|
|
menu(app, mainWindow);
|
2018-02-17 14:15:30 +00:00
|
|
|
|
2020-03-31 05:11:55 +00:00
|
|
|
if (!process.windowsStore && !process.mas) {
|
|
|
|
const newVersion = await checkNewVersion();
|
|
|
|
if (newVersion) {
|
|
|
|
menu(app, mainWindow, newVersion);
|
|
|
|
}
|
2018-02-17 14:15:30 +00:00
|
|
|
}
|
2016-10-30 10:57:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Quit when all windows are closed.
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
app.quit();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
// On OS X it's common to re-create a window in the app when the
|
|
|
|
// dock icon is clicked and there are no other windows open.
|
|
|
|
if (mainWindow === null) {
|
|
|
|
createWindow();
|
|
|
|
}
|
|
|
|
});
|
2016-11-21 18:05:48 +00:00
|
|
|
|
|
|
|
electron.ipcMain.on('renderer-ready', () => {
|
|
|
|
if (!isDev) {
|
|
|
|
const fileToOpen = process.argv[1];
|
2020-02-11 14:16:19 +00:00
|
|
|
// https://github.com/electron/electron/issues/3657
|
|
|
|
if (fileToOpen && !fileToOpen.startsWith('-psn_')) mainWindow.webContents.send('file-opened', [fileToOpen]);
|
2016-11-21 18:05:48 +00:00
|
|
|
}
|
|
|
|
});
|
2020-02-20 04:03:25 +00:00
|
|
|
|
|
|
|
electron.ipcMain.on('setAskBeforeClose', (e, val) => {
|
|
|
|
askBeforeClose = val;
|
|
|
|
});
|