index.ts 7.9 KB

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