main.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const {app, BrowserWindow} = require('electron')
  2. const path = require('path')
  3. const url = require('url')
  4. const pkg = require('./package.json')
  5. // 保持一个对于 window 对象的全局引用,如果你不这样做,
  6. // 当 JavaScript 对象被垃圾回收, window 会被自动地关闭
  7. let win
  8. function createWindow () {
  9. // 创建浏览器窗口。
  10. win = new BrowserWindow({width: 800, height: 600})
  11. // 然后加载应用的 index.html。
  12. // package中的DEV为true时,开启调试窗口。为false时使用编译发布版本
  13. if(pkg.DEV){
  14. win.loadURL('http://localhost:8000/')
  15. }else{
  16. win.loadURL(url.format({
  17. pathname: path.join(__dirname, './build/index.html'),
  18. protocol: 'file:',
  19. slashes: true
  20. }))
  21. }
  22. // 打开开发者工具。
  23. // win.webContents.openDevTools()
  24. // 当 window 被关闭,这个事件会被触发。
  25. win.on('closed', () => {
  26. // 取消引用 window 对象,如果你的应用支持多窗口的话,
  27. // 通常会把多个 window 对象存放在一个数组里面,
  28. // 与此同时,你应该删除相应的元素。
  29. win = null
  30. })
  31. }
  32. // Electron 会在初始化后并准备
  33. // 创建浏览器窗口时,调用这个函数。
  34. // 部分 API 在 ready 事件触发后才能使用。
  35. app.on('ready', createWindow)
  36. // 当全部窗口关闭时退出。
  37. app.on('window-all-closed', () => {
  38. // 在 macOS 上,除非用户用 Cmd + Q 确定地退出,
  39. // 否则绝大部分应用及其菜单栏会保持激活。
  40. if (process.platform !== 'darwin') {
  41. app.quit()
  42. }
  43. })
  44. app.on('activate', () => {
  45. // 在macOS上,当单击dock图标并且没有其他窗口打开时,
  46. // 通常在应用程序中重新创建一个窗口。
  47. if (win === null) {
  48. createWindow()
  49. }
  50. })
  51. // 在这文件,你可以续写应用剩下主进程代码。
  52. // 也可以拆分成几个文件,然后用 require 导入。
  53. // 在这里可以添加一些electron相关的其他模块,比如nodejs的一些原生模块
  54. // 文件模块
  55. // const BTFile = require('./sys_modules/BTFile')
  56. // BTFile.getAppPath()