main.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const electron = require('electron');
  2. const app = electron.app;
  3. const BrowserWindow = electron.BrowserWindow;
  4. const path = require('path');
  5. const url = require('url');
  6. let mainWindow;
  7. const Menu = electron.Menu;
  8. function createWindow () {
  9. mainWindow = new BrowserWindow({width: 1200, height: 800});
  10. mainWindow.loadURL(url.format({
  11. pathname: path.join(__dirname, 'index.html'),
  12. protocol: 'file:',
  13. slashes: true
  14. }));
  15. //mainWindow.openDevTools();
  16. mainWindow.setMenu(null);
  17. mainWindow.webContents.on('context-menu', (e, props) => {
  18. const { x, y } = props;
  19. Menu.buildFromTemplate([{
  20. label: 'Inspect element',
  21. click() {
  22. mainWindow.inspectElement(x, y);
  23. }
  24. }]).popup(mainWindow);
  25. });
  26. mainWindow.on('closed', function () {
  27. mainWindow = null
  28. })
  29. }
  30. app.disableHardwareAcceleration();
  31. // Some APIs can only be used after this event occurs.
  32. app.on('ready', createWindow);
  33. app.on('window-all-closed', function () {
  34. if (process.platform !== 'darwin') {
  35. app.quit()
  36. }
  37. });
  38. app.on('activate', function () {
  39. if (mainWindow === null) {
  40. createWindow()
  41. }
  42. });
  43. const sendResponse = (success) => {
  44. mainWindow.webContents.send('auth-response', success ? success : '');
  45. };
  46. app.on('open-url', function (e, url) {
  47. sendResponse(url);
  48. });
  49. const shouldQuit = app.makeSingleInstance((commandLine, workingDirectory) => {
  50. const uri = commandLine[commandLine.length - 1];
  51. if (commandLine.length >= 2 && uri) {
  52. sendResponse(uri);
  53. }
  54. // Someone tried to run a second instance, we should focus our window.
  55. if (mainWindow) {
  56. if (mainWindow.isMinimized()) mainWindow.restore();
  57. mainWindow.focus()
  58. }
  59. });
  60. if (shouldQuit) {
  61. app.quit()
  62. }