index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. // 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. remoteMain.enable(win.webContents)
  101. ejse.data('bkid', Math.floor((Math.random() * fs.readdirSync(path.join(__dirname, 'app', 'assets', 'images', 'backgrounds')).length)))
  102. win.loadURL(pathToFileURL(path.join(__dirname, 'app', 'app.ejs')).toString())
  103. /*win.once('ready-to-show', () => {
  104. win.show()
  105. })*/
  106. win.removeMenu()
  107. win.resizable = true
  108. win.on('closed', () => {
  109. win = null
  110. })
  111. }
  112. function createMenu() {
  113. if(process.platform === 'darwin') {
  114. // Extend default included application menu to continue support for quit keyboard shortcut
  115. let applicationSubMenu = {
  116. label: 'Application',
  117. submenu: [{
  118. label: 'About Application',
  119. selector: 'orderFrontStandardAboutPanel:'
  120. }, {
  121. type: 'separator'
  122. }, {
  123. label: 'Quit',
  124. accelerator: 'Command+Q',
  125. click: () => {
  126. app.quit()
  127. }
  128. }]
  129. }
  130. // New edit menu adds support for text-editing keyboard shortcuts
  131. let editSubMenu = {
  132. label: 'Edit',
  133. submenu: [{
  134. label: 'Undo',
  135. accelerator: 'CmdOrCtrl+Z',
  136. selector: 'undo:'
  137. }, {
  138. label: 'Redo',
  139. accelerator: 'Shift+CmdOrCtrl+Z',
  140. selector: 'redo:'
  141. }, {
  142. type: 'separator'
  143. }, {
  144. label: 'Cut',
  145. accelerator: 'CmdOrCtrl+X',
  146. selector: 'cut:'
  147. }, {
  148. label: 'Copy',
  149. accelerator: 'CmdOrCtrl+C',
  150. selector: 'copy:'
  151. }, {
  152. label: 'Paste',
  153. accelerator: 'CmdOrCtrl+V',
  154. selector: 'paste:'
  155. }, {
  156. label: 'Select All',
  157. accelerator: 'CmdOrCtrl+A',
  158. selector: 'selectAll:'
  159. }]
  160. }
  161. // Bundle submenus into a single template and build a menu object with it
  162. let menuTemplate = [applicationSubMenu, editSubMenu]
  163. let menuObject = Menu.buildFromTemplate(menuTemplate)
  164. // Assign it to the application
  165. Menu.setApplicationMenu(menuObject)
  166. }
  167. }
  168. function getPlatformIcon(filename){
  169. let ext
  170. switch(process.platform) {
  171. case 'win32':
  172. ext = 'ico'
  173. break
  174. case 'darwin':
  175. case 'linux':
  176. default:
  177. ext = 'png'
  178. break
  179. }
  180. return path.join(__dirname, 'app', 'assets', 'images', `${filename}.${ext}`)
  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. })