index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const {app, BrowserWindow} = require('electron')
  2. const path = require('path')
  3. const url = require('url')
  4. const fs = require('fs')
  5. const ejse = require('ejs-electron')
  6. // Keep a global reference of the window object, if you don't, the window will
  7. // be closed automatically when the JavaScript object is garbage collected.
  8. let win
  9. function createWindow() {
  10. win = new BrowserWindow({
  11. width: 980,
  12. height: 552,
  13. icon: getPlatformIcon('WesterosSealSquare'),
  14. frame: false,
  15. webPreferences: {
  16. preload: path.join(__dirname, 'app', 'assets', 'js', 'preloader.js')
  17. }
  18. })
  19. ejse.data('bkid', Math.floor((Math.random() * fs.readdirSync(path.join(__dirname, 'app', 'assets', 'images', 'backgrounds')).length)))
  20. win.loadURL(url.format({
  21. pathname: path.join(__dirname, 'app', 'index.ejs'),
  22. protocol: 'file:',
  23. slashes: true
  24. }))
  25. win.setMenu(null)
  26. win.setResizable(true)
  27. win.on('closed', () => {
  28. win = null
  29. })
  30. }
  31. function getPlatformIcon(filename){
  32. const opSys = process.platform
  33. if (opSys === 'darwin') {
  34. filename = filename + '.icns'
  35. } else if (opSys === 'win32') {
  36. filename = filename + '.ico'
  37. } else {
  38. filename = filename + '.png'
  39. }
  40. return path.join(__dirname, 'app', 'assets', 'images', filename)
  41. }
  42. app.on('ready', createWindow);
  43. app.on('window-all-closed', () => {
  44. // On macOS it is common for applications and their menu bar
  45. // to stay active until the user quits explicitly with Cmd + Q
  46. if (process.platform !== 'darwin') {
  47. app.quit()
  48. }
  49. })
  50. app.on('activate', () => {
  51. // On macOS it's common to re-create a window in the app when the
  52. // dock icon is clicked and there are no other windows open.
  53. if (win === null) {
  54. createWindow()
  55. }
  56. })