main.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* jshint esversion: 6 */
  2. const electron = require('electron');
  3. // Module to control application life.
  4. const app = electron.app;
  5. // Module to create native browser window.
  6. const BrowserWindow = electron.BrowserWindow;
  7. // const newWindowBtn = document.getElementById('frameless-window')
  8. const path = require('path');
  9. const url = require('url');
  10. // Keep a global reference of the window object, if you don't, the window will
  11. // be closed automatically when the JavaScript object is garbage collected.
  12. let mainWindow;
  13. function createWindow() {
  14. // 创建一个窗口,大小 800 * 600
  15. mainWindow = new BrowserWindow({
  16. width: 800,
  17. height: 600
  18. });
  19. // 在窗口内要展示的内容为 ./dist/index.html,即打包生成的index.html
  20. mainWindow.loadURL(url.format({
  21. pathname: path.join(__dirname, './dist', 'index.html'),
  22. protocol: 'file:',
  23. slashes: true
  24. }));
  25. // 自动打开调试台
  26. mainWindow.webContents.openDevTools({
  27. detach: true
  28. });
  29. // Open the DevTools.
  30. // mainWindow.webContents.openDevTools()
  31. // Emitted when the window is closed.
  32. mainWindow.on('closed', function () {
  33. // Dereference the window object, usually you would store windows
  34. // in an array if your app supports multi windows, this is the time
  35. // when you should delete the corresponding element.
  36. // 回收BrowserWindow对象
  37. mainWindow = null;
  38. });
  39. }
  40. // This method will be called when Electron has finished
  41. // initialization and is ready to create browser windows.
  42. // Some APIs can only be used after this event occurs.
  43. app.on('ready', createWindow);
  44. // Quit when all windows are closed.
  45. app.on('window-all-closed', function () {
  46. // On OS X it is common for applications and their menu bar
  47. // to stay active until the user quits explicitly with Cmd + Q
  48. if (process.platform !== 'darwin') {
  49. app.quit();
  50. }
  51. });
  52. app.on('activate', function () {
  53. // On OS X it's common to re-create a window in the app when the
  54. // dock icon is clicked and there are no other windows open.
  55. if (mainWindow === null) {
  56. createWindow();
  57. }
  58. });
  59. // In this file you can include the rest of your app's specific main process
  60. // code. You can also put them in separate files and require them here.