-
-
Save jasonmnemonic/ecc7ea2aab420900dfe4c8d1278853ef to your computer and use it in GitHub Desktop.
Electron JS main App.js file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import path from 'path' | |
import url from 'url' | |
import { app, crashReporter, BrowserWindow, Menu, globalShortcut } from 'electron' | |
const isDevelopment = (process.env.NODE_ENV === 'development') | |
let mainWindow = null | |
let forceQuit = false | |
var shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) { | |
// Someone tried to run a second instance, we should focus our window. | |
if (mainWindow) { | |
if (mainWindow.isMinimized()) mainWindow.restore(); | |
mainWindow.focus(); | |
} | |
}); | |
if (shouldQuit) { | |
app.quit(); | |
} | |
const installExtensions = async () => { | |
const installer = require('electron-devtools-installer') | |
const extensions = [ | |
'REACT_DEVELOPER_TOOLS', | |
'REDUX_DEVTOOLS' | |
] | |
const forceDownload = !!process.env.UPGRADE_EXTENSIONS | |
for (const name of extensions) { | |
try { | |
await installer.default(installer[name], forceDownload) | |
} catch (e) { | |
console.log(`Error installing ${name} extension: ${e.message}`) | |
} | |
} | |
} | |
crashReporter.start({ | |
productName: 'YourName', | |
companyName: 'YourCompany', | |
submitURL: 'https://your-domain.com/url-to-submit', | |
uploadToServer: false | |
}) | |
app.on('window-all-closed', () => { | |
if (process.platform !== 'darwin') { | |
app.quit() | |
} | |
}) | |
function registerShortcuts() { | |
globalShortcut.register('Ctrl+Shift+A', () => { | |
var focusedWindow = BrowserWindow.getFocusedWindow(); | |
if (focusedWindow) { | |
focusedWindow.webContents.send('dashboard'); | |
} else { | |
if (mainWindow) | |
mainWindow.webContents.send('dashboard'); | |
} | |
}); | |
globalShortcut.register('Ctrl+Shift+U', () => { | |
var focusedWindow = BrowserWindow.getFocusedWindow(); | |
if (focusedWindow) { | |
focusedWindow.webContents.send('home'); | |
} else { | |
if (mainWindow) | |
mainWindow.webContents.send('home'); | |
} | |
}); | |
globalShortcut.register('ALT+ENTER', () => { | |
var focusedWindow = BrowserWindow.getFocusedWindow(); | |
if (focusedWindow) { | |
//focusedWindow.webContents.send('resize',focusedWindow.getBounds().width); | |
focusedWindow.webContents.send('fullscreen'); | |
} else { | |
// mainWindow.webContents.send('resize',mainWindow.getBounds().width); | |
if (mainWindow) | |
mainWindow.webContents.send('fullscreen'); | |
} | |
}); | |
globalShortcut.register('ESC', () => { | |
var focusedWindow = BrowserWindow.getFocusedWindow(); | |
if (focusedWindow) { | |
focusedWindow.webContents.send('exit'); | |
} else { | |
if (mainWindow) | |
mainWindow.webContents.send('exit'); | |
} | |
}); | |
} | |
function unregisterShortcuts() { | |
globalShortcut.unregister('Ctrl+Shift+A', () => { | |
}); | |
globalShortcut.unregister('Ctrl+Shift+U', () => { | |
}); | |
globalShortcut.unregister('ALT+ENTER', () => { | |
}); | |
globalShortcut.unregister('ESC', () => { | |
}); | |
} | |
app.on('ready', async () => { | |
if (isDevelopment) { | |
await installExtensions() | |
} | |
mainWindow = new BrowserWindow({ | |
width: 1024, | |
height: 768 | |
}) | |
mainWindow.on('focus', registerShortcuts) | |
mainWindow.on('blur', unregisterShortcuts) | |
mainWindow.on('resize', () => { | |
if (mainWindow) | |
mainWindow.webContents.send('resize', mainWindow.getBounds().width); | |
}); | |
mainWindow.loadURL(url.format({ | |
pathname: path.join(__dirname, 'index.html'), | |
protocol: 'file:', | |
slashes: true | |
})) | |
mainWindow.webContents.once('did-finish-load', () => { | |
mainWindow.show() | |
}) | |
mainWindow.webContents.on('did-finish-load', () => { | |
if (mainWindow) | |
mainWindow.webContents.send('resize', mainWindow.getBounds().width); | |
if (process.platform === 'darwin') { | |
mainWindow.on('close', function (e) { | |
app.quit(); | |
// //console.log(); | |
// if (!forceQuit) { | |
// e.preventDefault() | |
// mainWindow.hide() | |
// } | |
}) | |
app.on('activate', () => { | |
mainWindow.show() | |
}) | |
app.on('before-quit', () => { | |
forceQuit = true | |
}) | |
} else { | |
mainWindow.on('closed', () => { | |
mainWindow = null; | |
app.quit() | |
}) | |
} | |
}) | |
if (isDevelopment) { | |
mainWindow.webContents.openDevTools() | |
mainWindow.webContents.on('context-menu', (e, props) => { | |
Menu.buildFromTemplate([{ | |
label: 'Inspect element', | |
click() { | |
mainWindow.inspectElement(props.x, props.y) | |
} | |
}]).popup(mainWindow) | |
}) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment