index.js 7.0 KB

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