index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Requirements
  2. const { app, BrowserWindow, ipcMain, Menu } = require('electron')
  3. const autoUpdater = require('electron-updater').autoUpdater
  4. const ejse = require('ejs-electron')
  5. const fs = require('fs')
  6. const isDev = require('./app/assets/js/isdev')
  7. const path = require('path')
  8. const semver = require('semver')
  9. const url = require('url')
  10. // Setup auto updater.
  11. function initAutoUpdater(event, data) {
  12. if(data){
  13. autoUpdater.allowPrerelease = true
  14. } else {
  15. // Defaults to true if application version contains prerelease components (e.g. 0.12.1-alpha.1)
  16. // autoUpdater.allowPrerelease = true
  17. }
  18. if(isDev){
  19. autoUpdater.autoInstallOnAppQuit = false
  20. autoUpdater.updateConfigPath = path.join(__dirname, 'dev-app-update.yml')
  21. }
  22. if(process.platform === 'darwin'){
  23. autoUpdater.autoDownload = false
  24. }
  25. autoUpdater.on('update-available', (info) => {
  26. event.sender.send('autoUpdateNotification', 'update-available', info)
  27. })
  28. autoUpdater.on('update-downloaded', (info) => {
  29. event.sender.send('autoUpdateNotification', 'update-downloaded', info)
  30. })
  31. autoUpdater.on('update-not-available', (info) => {
  32. event.sender.send('autoUpdateNotification', 'update-not-available', info)
  33. })
  34. autoUpdater.on('checking-for-update', () => {
  35. event.sender.send('autoUpdateNotification', 'checking-for-update')
  36. })
  37. autoUpdater.on('error', (err) => {
  38. event.sender.send('autoUpdateNotification', 'realerror', err)
  39. })
  40. }
  41. // Open channel to listen for update actions.
  42. ipcMain.on('autoUpdateAction', (event, arg, data) => {
  43. switch(arg){
  44. case 'initAutoUpdater':
  45. console.log('Initializing auto updater.')
  46. initAutoUpdater(event, data)
  47. event.sender.send('autoUpdateNotification', 'ready')
  48. break
  49. case 'checkForUpdate':
  50. autoUpdater.checkForUpdates()
  51. .catch(err => {
  52. event.sender.send('autoUpdateNotification', 'realerror', err)
  53. })
  54. break
  55. case 'allowPrereleaseChange':
  56. if(!data){
  57. const preRelComp = semver.prerelease(app.getVersion())
  58. if(preRelComp != null && preRelComp.length > 0){
  59. autoUpdater.allowPrerelease = true
  60. } else {
  61. autoUpdater.allowPrerelease = data
  62. }
  63. } else {
  64. autoUpdater.allowPrerelease = data
  65. }
  66. break
  67. case 'installUpdateNow':
  68. autoUpdater.quitAndInstall()
  69. break
  70. default:
  71. console.log('Unknown argument', arg)
  72. break
  73. }
  74. })
  75. // Redirect distribution index event from preloader to renderer.
  76. ipcMain.on('distributionIndexDone', (event, res) => {
  77. event.sender.send('distributionIndexDone', res)
  78. })
  79. // Disable hardware acceleration.
  80. // https://electronjs.org/docs/tutorial/offscreen-rendering
  81. app.disableHardwareAcceleration()
  82. // https://github.com/electron/electron/issues/18397
  83. app.allowRendererProcessReuse = true
  84. // Keep a global reference of the window object, if you don't, the window will
  85. // be closed automatically when the JavaScript object is garbage collected.
  86. let win
  87. function createWindow() {
  88. win = new BrowserWindow({
  89. width: 980,
  90. height: 552,
  91. icon: getPlatformIcon('SealCircle'),
  92. frame: false,
  93. webPreferences: {
  94. preload: path.join(__dirname, 'app', 'assets', 'js', 'preloader.js'),
  95. nodeIntegration: true,
  96. contextIsolation: false,
  97. enableRemoteModule: true,
  98. worldSafeExecuteJavaScript: true
  99. },
  100. backgroundColor: '#171614'
  101. })
  102. ejse.data('bkid', Math.floor((Math.random() * fs.readdirSync(path.join(__dirname, 'app', 'assets', 'images', 'backgrounds')).length)))
  103. win.loadURL(url.format({
  104. pathname: path.join(__dirname, 'app', 'app.ejs'),
  105. protocol: 'file:',
  106. slashes: true
  107. }))
  108. /*win.once('ready-to-show', () => {
  109. win.show()
  110. })*/
  111. win.removeMenu()
  112. win.resizable = true
  113. win.on('closed', () => {
  114. win = null
  115. })
  116. }
  117. function createMenu() {
  118. if(process.platform === 'darwin') {
  119. // Extend default included application menu to continue support for quit keyboard shortcut
  120. let applicationSubMenu = {
  121. label: 'Application',
  122. submenu: [{
  123. label: 'About Application',
  124. selector: 'orderFrontStandardAboutPanel:'
  125. }, {
  126. type: 'separator'
  127. }, {
  128. label: 'Quit',
  129. accelerator: 'Command+Q',
  130. click: () => {
  131. app.quit()
  132. }
  133. }]
  134. }
  135. // New edit menu adds support for text-editing keyboard shortcuts
  136. let editSubMenu = {
  137. label: 'Edit',
  138. submenu: [{
  139. label: 'Undo',
  140. accelerator: 'CmdOrCtrl+Z',
  141. selector: 'undo:'
  142. }, {
  143. label: 'Redo',
  144. accelerator: 'Shift+CmdOrCtrl+Z',
  145. selector: 'redo:'
  146. }, {
  147. type: 'separator'
  148. }, {
  149. label: 'Cut',
  150. accelerator: 'CmdOrCtrl+X',
  151. selector: 'cut:'
  152. }, {
  153. label: 'Copy',
  154. accelerator: 'CmdOrCtrl+C',
  155. selector: 'copy:'
  156. }, {
  157. label: 'Paste',
  158. accelerator: 'CmdOrCtrl+V',
  159. selector: 'paste:'
  160. }, {
  161. label: 'Select All',
  162. accelerator: 'CmdOrCtrl+A',
  163. selector: 'selectAll:'
  164. }]
  165. }
  166. // Bundle submenus into a single template and build a menu object with it
  167. let menuTemplate = [applicationSubMenu, editSubMenu]
  168. let menuObject = Menu.buildFromTemplate(menuTemplate)
  169. // Assign it to the application
  170. Menu.setApplicationMenu(menuObject)
  171. }
  172. }
  173. function getPlatformIcon(filename){
  174. let ext
  175. switch(process.platform) {
  176. case 'win32':
  177. ext = 'ico'
  178. break
  179. case 'darwin':
  180. case 'linux':
  181. default:
  182. ext = 'png'
  183. break
  184. }
  185. return path.join(__dirname, 'app', 'assets', 'images', `${filename}.${ext}`)
  186. }
  187. app.on('ready', createWindow)
  188. app.on('ready', createMenu)
  189. app.on('window-all-closed', () => {
  190. // On macOS it is common for applications and their menu bar
  191. // to stay active until the user quits explicitly with Cmd + Q
  192. if (process.platform !== 'darwin') {
  193. app.quit()
  194. }
  195. })
  196. app.on('activate', () => {
  197. // On macOS it's common to re-create a window in the app when the
  198. // dock icon is clicked and there are no other windows open.
  199. if (win === null) {
  200. createWindow()
  201. }
  202. })