index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. },
  98. backgroundColor: '#171614'
  99. })
  100. ejse.data('bkid', Math.floor((Math.random() * fs.readdirSync(path.join(__dirname, 'app', 'assets', 'images', 'backgrounds')).length)))
  101. win.loadURL(url.format({
  102. pathname: path.join(__dirname, 'app', 'app.ejs'),
  103. protocol: 'file:',
  104. slashes: true
  105. }))
  106. /*win.once('ready-to-show', () => {
  107. win.show()
  108. })*/
  109. win.removeMenu()
  110. win.resizable = true
  111. win.on('closed', () => {
  112. win = null
  113. })
  114. }
  115. function createMenu() {
  116. if(process.platform === 'darwin') {
  117. // Extend default included application menu to continue support for quit keyboard shortcut
  118. let applicationSubMenu = {
  119. label: 'Application',
  120. submenu: [{
  121. label: 'About Application',
  122. selector: 'orderFrontStandardAboutPanel:'
  123. }, {
  124. type: 'separator'
  125. }, {
  126. label: 'Quit',
  127. accelerator: 'Command+Q',
  128. click: () => {
  129. app.quit()
  130. }
  131. }]
  132. }
  133. // New edit menu adds support for text-editing keyboard shortcuts
  134. let editSubMenu = {
  135. label: 'Edit',
  136. submenu: [{
  137. label: 'Undo',
  138. accelerator: 'CmdOrCtrl+Z',
  139. selector: 'undo:'
  140. }, {
  141. label: 'Redo',
  142. accelerator: 'Shift+CmdOrCtrl+Z',
  143. selector: 'redo:'
  144. }, {
  145. type: 'separator'
  146. }, {
  147. label: 'Cut',
  148. accelerator: 'CmdOrCtrl+X',
  149. selector: 'cut:'
  150. }, {
  151. label: 'Copy',
  152. accelerator: 'CmdOrCtrl+C',
  153. selector: 'copy:'
  154. }, {
  155. label: 'Paste',
  156. accelerator: 'CmdOrCtrl+V',
  157. selector: 'paste:'
  158. }, {
  159. label: 'Select All',
  160. accelerator: 'CmdOrCtrl+A',
  161. selector: 'selectAll:'
  162. }]
  163. }
  164. // Bundle submenus into a single template and build a menu object with it
  165. let menuTemplate = [applicationSubMenu, editSubMenu]
  166. let menuObject = Menu.buildFromTemplate(menuTemplate)
  167. // Assign it to the application
  168. Menu.setApplicationMenu(menuObject)
  169. }
  170. }
  171. function getPlatformIcon(filename){
  172. const opSys = process.platform
  173. if (opSys === 'darwin') {
  174. filename = filename + '.icns'
  175. } else if (opSys === 'win32') {
  176. filename = filename + '.ico'
  177. } else {
  178. filename = filename + '.png'
  179. }
  180. return path.join(__dirname, 'app', 'assets', 'images', filename)
  181. }
  182. app.on('ready', createWindow)
  183. app.on('ready', createMenu)
  184. app.on('window-all-closed', () => {
  185. // On macOS it is common for applications and their menu bar
  186. // to stay active until the user quits explicitly with Cmd + Q
  187. if (process.platform !== 'darwin') {
  188. app.quit()
  189. }
  190. })
  191. app.on('activate', () => {
  192. // On macOS it's common to re-create a window in the app when the
  193. // dock icon is clicked and there are no other windows open.
  194. if (win === null) {
  195. createWindow()
  196. }
  197. })