index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. // Keep a global reference of the window object, if you don't, the window will
  84. // be closed automatically when the JavaScript object is garbage collected.
  85. let win
  86. function createWindow() {
  87. win = new BrowserWindow({
  88. width: 980,
  89. height: 552,
  90. icon: getPlatformIcon('SealCircle'),
  91. frame: false,
  92. webPreferences: {
  93. preload: path.join(__dirname, 'app', 'assets', 'js', 'preloader.js'),
  94. nodeIntegration: true,
  95. contextIsolation: false
  96. },
  97. backgroundColor: '#171614'
  98. })
  99. ejse.data('bkid', Math.floor((Math.random() * fs.readdirSync(path.join(__dirname, 'app', 'assets', 'images', 'backgrounds')).length)))
  100. win.loadURL(url.format({
  101. pathname: path.join(__dirname, 'app', 'app.ejs'),
  102. protocol: 'file:',
  103. slashes: true
  104. }))
  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. const opSys = process.platform
  172. if (opSys === 'darwin') {
  173. filename = filename + '.icns'
  174. } else if (opSys === 'win32') {
  175. filename = filename + '.ico'
  176. } else {
  177. filename = filename + '.png'
  178. }
  179. return path.join(__dirname, 'app', 'assets', 'images', filename)
  180. }
  181. app.on('ready', createWindow)
  182. app.on('ready', createMenu)
  183. app.on('window-all-closed', () => {
  184. // On macOS it is common for applications and their menu bar
  185. // to stay active until the user quits explicitly with Cmd + Q
  186. if (process.platform !== 'darwin') {
  187. app.quit()
  188. }
  189. })
  190. app.on('activate', () => {
  191. // On macOS it's common to re-create a window in the app when the
  192. // dock icon is clicked and there are no other windows open.
  193. if (win === null) {
  194. createWindow()
  195. }
  196. })