2020-09-20 18:54:23 +00:00
|
|
|
import { app, BrowserWindow, Menu, ipcMain, screen } from 'electron'
|
2020-02-16 18:30:00 +00:00
|
|
|
import { productName } from '../../package.json'
|
2020-09-20 18:54:23 +00:00
|
|
|
import Datastore from 'nedb'
|
2020-02-16 18:30:00 +00:00
|
|
|
|
2020-04-21 20:22:41 +00:00
|
|
|
require('electron-context-menu')({
|
|
|
|
showSearchWithGoogle: false,
|
|
|
|
showSaveImageAs: true,
|
|
|
|
showCopyImageAddress: true,
|
|
|
|
prepend: (params, browserWindow) => []
|
|
|
|
})
|
|
|
|
|
2020-09-20 18:54:23 +00:00
|
|
|
const localDataStorage = app.getPath('userData') // Grabs the userdata directory based on the user's OS
|
|
|
|
|
|
|
|
const settingsDb = new Datastore({
|
|
|
|
filename: localDataStorage + '/settings.db',
|
|
|
|
autoload: true
|
|
|
|
})
|
|
|
|
|
2020-02-16 18:30:00 +00:00
|
|
|
// set app name
|
|
|
|
app.setName(productName)
|
|
|
|
|
|
|
|
// disable electron warning
|
|
|
|
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = 'true'
|
2020-08-21 23:15:18 +00:00
|
|
|
const path = require('path')
|
2020-02-16 18:30:00 +00:00
|
|
|
const isDev = process.env.NODE_ENV === 'development'
|
|
|
|
const isDebug = process.argv.includes('--debug')
|
|
|
|
let mainWindow
|
|
|
|
|
2020-06-19 22:11:21 +00:00
|
|
|
// CORS somehow gets re-enabled in Electron v9.0.4
|
|
|
|
// This line disables it.
|
|
|
|
// This line can possible be removed if the issue is fixed upstream
|
|
|
|
app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors')
|
|
|
|
|
2020-10-04 20:31:07 +00:00
|
|
|
app.setAsDefaultProtocolClient('freetube')
|
|
|
|
|
2020-06-22 03:03:51 +00:00
|
|
|
// TODO: Uncomment if needed
|
2020-02-16 18:30:00 +00:00
|
|
|
// only allow single instance of application
|
2020-10-04 20:31:07 +00:00
|
|
|
if (!isDev) {
|
|
|
|
const gotTheLock = app.requestSingleInstanceLock()
|
|
|
|
|
|
|
|
if (gotTheLock) {
|
|
|
|
app.on('second-instance', (event, commandLine, workingDirectory) => {
|
|
|
|
// Someone tried to run a second instance, we should focus our window.
|
|
|
|
if (mainWindow && typeof (commandLine) !== 'undefined') {
|
|
|
|
if (mainWindow.isMinimized()) mainWindow.restore()
|
|
|
|
mainWindow.focus()
|
|
|
|
|
|
|
|
mainWindow.webContents.send('ping', commandLine)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('ready', (event, commandLine, workingDirectory) => {
|
|
|
|
createWindow()
|
|
|
|
|
|
|
|
if (isDev) {
|
|
|
|
installDevTools()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDebug) {
|
|
|
|
mainWindow.webContents.openDevTools()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
require('electron-debug')({
|
|
|
|
showDevTools: !(process.env.RENDERER_REMOTE_DEBUGGING === 'true')
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('ready', () => {
|
|
|
|
createWindow()
|
|
|
|
|
|
|
|
if (isDev) {
|
|
|
|
installDevTools()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDebug) {
|
|
|
|
mainWindow.webContents.openDevTools()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-02-16 18:30:00 +00:00
|
|
|
|
|
|
|
async function installDevTools () {
|
|
|
|
try {
|
|
|
|
/* eslint-disable */
|
|
|
|
require('devtron').install()
|
|
|
|
require('vue-devtools').install()
|
|
|
|
/* eslint-enable */
|
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createWindow () {
|
|
|
|
/**
|
|
|
|
* Initial window options
|
|
|
|
*/
|
|
|
|
mainWindow = new BrowserWindow({
|
|
|
|
backgroundColor: '#fff',
|
2020-08-21 23:15:18 +00:00
|
|
|
icon: isDev
|
|
|
|
? path.join(__dirname, '../../_icons/iconColor.png')
|
|
|
|
: `${__dirname}/_icons/iconColor.png`,
|
2020-06-21 09:16:03 +00:00
|
|
|
autoHideMenuBar: true,
|
2020-02-16 18:30:00 +00:00
|
|
|
// useContentSize: true,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true,
|
|
|
|
nodeIntegrationInWorker: false,
|
2020-06-06 21:54:40 +00:00
|
|
|
webSecurity: false,
|
2020-08-24 21:52:39 +00:00
|
|
|
backgroundThrottling: false,
|
|
|
|
enableRemoteModule: true
|
2020-02-16 18:30:00 +00:00
|
|
|
},
|
2020-06-25 02:02:23 +00:00
|
|
|
show: false
|
2020-02-16 18:30:00 +00:00
|
|
|
})
|
|
|
|
|
2020-09-20 18:54:23 +00:00
|
|
|
mainWindow.setBounds({
|
|
|
|
width: 1200,
|
|
|
|
height: 800
|
|
|
|
})
|
|
|
|
|
|
|
|
settingsDb.findOne({
|
|
|
|
_id: 'bounds'
|
|
|
|
}, function (err, doc) {
|
|
|
|
if (doc === null || err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof doc !== 'object' || typeof doc.value !== 'object') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const { maximized, ...bounds } = doc.value
|
|
|
|
const allDisplaysSummaryWidth = screen
|
|
|
|
.getAllDisplays()
|
|
|
|
.reduce((accumulator, { size: { width } }) => accumulator + width, 0)
|
|
|
|
|
|
|
|
if (allDisplaysSummaryWidth >= bounds.x) {
|
|
|
|
mainWindow.setBounds({
|
|
|
|
x: bounds.x,
|
|
|
|
y: bounds.y,
|
|
|
|
width: bounds.width,
|
|
|
|
height: bounds.height
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (maximized) {
|
|
|
|
mainWindow.maximize()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-02-16 18:30:00 +00:00
|
|
|
// eslint-disable-next-line
|
|
|
|
setMenu()
|
|
|
|
|
|
|
|
// load root file/url
|
|
|
|
if (isDev) {
|
|
|
|
mainWindow.loadURL('http://localhost:9080')
|
|
|
|
} else {
|
|
|
|
mainWindow.loadFile(`${__dirname}/index.html`)
|
|
|
|
|
2020-08-21 23:15:18 +00:00
|
|
|
global.__static = path
|
2020-02-16 18:30:00 +00:00
|
|
|
.join(__dirname, '/static')
|
|
|
|
.replace(/\\/g, '\\\\')
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show when loaded
|
|
|
|
mainWindow.on('ready-to-show', () => {
|
|
|
|
mainWindow.show()
|
|
|
|
mainWindow.focus()
|
|
|
|
})
|
|
|
|
|
|
|
|
mainWindow.on('closed', () => {
|
|
|
|
console.log('closed')
|
|
|
|
})
|
2020-09-20 18:54:23 +00:00
|
|
|
|
|
|
|
ipcMain.on('setBounds', (_e, data) => {
|
|
|
|
const value = {
|
|
|
|
...mainWindow.getBounds(),
|
|
|
|
maximized: mainWindow.isMaximized()
|
|
|
|
}
|
|
|
|
|
|
|
|
settingsDb.findOne({
|
|
|
|
_id: 'bounds'
|
|
|
|
}, function (err, doc) {
|
|
|
|
if (err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (doc !== null) {
|
|
|
|
settingsDb.update({
|
|
|
|
_id: 'bounds'
|
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
value
|
|
|
|
}
|
|
|
|
}, {})
|
|
|
|
} else {
|
|
|
|
settingsDb.insert({
|
|
|
|
_id: 'bounds',
|
|
|
|
value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2020-02-16 18:30:00 +00:00
|
|
|
|
2020-10-04 20:31:07 +00:00
|
|
|
ipcMain.on('appReady', () => {
|
|
|
|
const param = process.argv[1]
|
|
|
|
if (typeof (param) !== 'undefined' && param !== null) {
|
|
|
|
mainWindow.webContents.send('ping', process.argv)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-02-16 18:30:00 +00:00
|
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
2020-08-21 01:20:22 +00:00
|
|
|
|
|
|
|
mainWindow.webContents.session.clearCache()
|
|
|
|
mainWindow.webContents.session.clearStorageData({
|
|
|
|
storages: [
|
|
|
|
'appcache',
|
|
|
|
'cookies',
|
|
|
|
'filesystem',
|
|
|
|
'indexdb',
|
|
|
|
'shadercache',
|
|
|
|
'websql',
|
|
|
|
'serviceworkers',
|
|
|
|
'cachestorage'
|
|
|
|
]
|
|
|
|
})
|
2020-02-16 18:30:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
if (mainWindow === null) {
|
|
|
|
createWindow()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auto Updater
|
|
|
|
*
|
|
|
|
* Uncomment the following code below and install `electron-updater` to
|
|
|
|
* support auto updating. Code Signing with a valid certificate is required.
|
|
|
|
* https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
import { autoUpdater } from 'electron-updater'
|
|
|
|
autoUpdater.on('update-downloaded', () => {
|
|
|
|
autoUpdater.quitAndInstall()
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('ready', () => {
|
|
|
|
if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
|
|
|
|
})
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* eslint-disable-next-line */
|
|
|
|
const sendMenuEvent = async data => {
|
|
|
|
mainWindow.webContents.send('change-view', data)
|
|
|
|
}
|
|
|
|
|
|
|
|
const template = [{
|
|
|
|
label: 'File',
|
|
|
|
submenu: [
|
|
|
|
{
|
|
|
|
role: 'quit'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Edit',
|
|
|
|
submenu: [{
|
|
|
|
role: 'cut'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'copy',
|
|
|
|
accelerator: 'CmdOrCtrl+C',
|
|
|
|
selector: 'copy:'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'paste',
|
|
|
|
accelerator: 'CmdOrCtrl+V',
|
|
|
|
selector: 'paste:'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'pasteandmatchstyle'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'delete'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'selectall'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'View',
|
|
|
|
submenu: [{
|
|
|
|
role: 'reload'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'forcereload',
|
|
|
|
accelerator: 'CmdOrCtrl+Shift+R'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'toggledevtools'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'resetzoom'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'zoomin'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'zoomout'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'togglefullscreen'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'window',
|
|
|
|
submenu: [{
|
|
|
|
role: 'minimize'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: 'close'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
function setMenu () {
|
|
|
|
if (process.platform === 'darwin') {
|
|
|
|
template.unshift({
|
|
|
|
label: app.getName(),
|
|
|
|
submenu: [
|
|
|
|
{ role: 'about' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'services' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'hide' },
|
|
|
|
{ role: 'hideothers' },
|
|
|
|
{ role: 'unhide' },
|
|
|
|
{ type: 'separator' },
|
|
|
|
{ role: 'quit' }
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
template.push({
|
|
|
|
role: 'window'
|
|
|
|
})
|
|
|
|
|
|
|
|
template.push({
|
|
|
|
role: 'help'
|
|
|
|
})
|
|
|
|
|
|
|
|
template.push({ role: 'services' })
|
|
|
|
}
|
|
|
|
|
|
|
|
const menu = Menu.buildFromTemplate(template)
|
|
|
|
Menu.setApplicationMenu(menu)
|
|
|
|
}
|