index.js 7.0 KB

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