main.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Modules to control application life and create native browser window
  2. const {app, BrowserWindow, ipcMain, dialog, Menu } = require('electron')
  3. const path = require('path')
  4. const titleBarStyle = (process.platform === 'darwin') ? 'hiddenInset' : 'default';
  5. const frameWin = (process.platform === 'darwin') ? false : true;
  6. const isMac = process.platform === 'darwin'
  7. let mainWindow;
  8. function createWindow () {
  9. // Create the browser window.
  10. mainWindow = new BrowserWindow({
  11. titleBarStyle: titleBarStyle,
  12. autoHideMenuBar: true,
  13. vibrancy: 'dark',
  14. width: 640,
  15. height: 600,
  16. minWidth: 400,
  17. minHeight: 400,
  18. show: true,
  19. skipTaskbar: true,
  20. transparent: false,
  21. frame: frameWin,
  22. fullScreenable: true,
  23. resizable: true,
  24. backgroundColor: "black", //background,
  25. paintWhenInitiallyHidden: true,
  26. webPreferences: {
  27. preload: path.join(__dirname, 'preload.js'),
  28. // contextIsolation: false,
  29. // nodeIntegration: true,
  30. }
  31. })
  32. // and load the index.html of the app.
  33. mainWindow.loadFile('index.html')
  34. // Open the DevTools.
  35. // mainWindow.webContents.openDevTools()
  36. }
  37. // This method will be called when Electron has finished
  38. // initialization and is ready to create browser windows.
  39. // Some APIs can only be used after this event occurs.
  40. app.whenReady().then(() => {
  41. createWindow()
  42. ipcMain.handle('dialog', (event, method, params) => {
  43. return dialog[method](params);
  44. });
  45. app.on('activate', function () {
  46. // On macOS it's common to re-create a window in the app when the
  47. // dock icon is clicked and there are no other windows open.
  48. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  49. })
  50. })
  51. // Quit when all windows are closed, except on macOS. There, it's common
  52. // for applications and their menu bar to stay active until the user quits
  53. // explicitly with Cmd + Q.
  54. app.on('window-all-closed', function () {
  55. if (process.platform !== 'darwin') app.quit()
  56. })
  57. // In this file you can include the rest of your app's specific main process
  58. // code. You can also put them in separate files and require them here.
  59. function toggleFullscreen() {
  60. const currWind = BrowserWindow.getFocusedWindow().name
  61. if (mainWindow.isFullScreen()) {
  62. mainWindow.setFullScreen(false);
  63. } else {
  64. mainWindow.setFullScreen(true);
  65. }
  66. }
  67. const template = [
  68. // { role: 'appMenu' }
  69. ...(isMac ? [{
  70. label: app.name,
  71. submenu: [
  72. { role: 'about', },
  73. { type: 'separator' },
  74. { role: 'services' },
  75. { type: 'separator' },
  76. { role: 'hide' },
  77. { role: 'hideothers' },
  78. { role: 'unhide' },
  79. { type: 'separator' },
  80. { role: 'quit' }
  81. ]
  82. }] : []),
  83. // { role: 'fileMenu' }
  84. {
  85. label: 'File',
  86. submenu: [
  87. isMac ? { role: 'close' } : { role: 'quit' }
  88. ]
  89. },
  90. // { role: 'editMenu' }
  91. {
  92. label: 'Edit',
  93. submenu: [
  94. { role: 'undo' },
  95. { role: 'redo' },
  96. { type: 'separator' },
  97. { role: 'cut' },
  98. { role: 'copy' },
  99. { role: 'paste' },
  100. ...(isMac ? [
  101. { role: 'pasteAndMatchStyle' },
  102. { role: 'delete' },
  103. { role: 'selectAll' },
  104. { type: 'separator' },
  105. {
  106. label: 'Speech',
  107. submenu: [
  108. { role: 'startspeaking' },
  109. { role: 'stopspeaking' }
  110. ]
  111. }
  112. ] : [
  113. { role: 'delete' },
  114. { type: 'separator' },
  115. { role: 'selectAll' }
  116. ])
  117. ]
  118. },
  119. // { role: 'viewMenu' }
  120. {
  121. label: 'View',
  122. submenu: [
  123. { role: 'reload' },
  124. { role: 'forcereload' },
  125. { role: 'toggledevtools' },
  126. { type: 'separator' },
  127. { role: 'resetzoom' },
  128. { role: 'zoomin' },
  129. { role: 'zoomout' },
  130. { type: 'separator' },
  131. {
  132. label: 'Toggle Full Screen',
  133. click: () => { toggleFullscreen();},
  134. accelerator: 'CmdOrCtrl+f'
  135. }
  136. ]
  137. },
  138. // { role: 'windowMenu' }
  139. {
  140. label: 'Window',
  141. submenu: [
  142. { role: 'minimize' },
  143. { role: 'zoom' },
  144. ...(isMac ? [
  145. { type: 'separator' },
  146. { role: 'front' },
  147. { type: 'separator' },
  148. { role: 'window' }
  149. ] : [
  150. { role: 'close' }
  151. ])
  152. ]
  153. },
  154. {
  155. role: 'help',
  156. submenu: [
  157. {
  158. label: 'Learn More',
  159. click: async () => {
  160. const { shell } = require('electron')
  161. await shell.openExternal('https://niemes.info')
  162. }
  163. }
  164. ]
  165. }
  166. ]
  167. const menu = Menu.buildFromTemplate(template)
  168. Menu.setApplicationMenu(menu)