electron.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const { app, Menu, BrowserWindow } = require('electron');
  2. // Module to control application life.
  3. // Module to create native browser window.
  4. const path = require('path');
  5. const url = require('url');
  6. const isMac = process.platform === 'darwin';
  7. const template = [
  8. // { role: 'appMenu' }
  9. ...(isMac
  10. ? [
  11. {
  12. label: app.name,
  13. submenu: [
  14. { role: 'about' },
  15. { type: 'separator' },
  16. { role: 'services' },
  17. { type: 'separator' },
  18. { role: 'hide' },
  19. { role: 'hideothers' },
  20. { role: 'unhide' },
  21. { type: 'separator' },
  22. { role: 'quit' },
  23. ],
  24. },
  25. ]
  26. : []),
  27. // { role: 'fileMenu' }
  28. {
  29. label: 'File',
  30. submenu: [isMac ? { role: 'close' } : { role: 'quit' }],
  31. },
  32. // { role: 'editMenu' }
  33. {
  34. label: 'Edit',
  35. submenu: [
  36. { role: 'undo' },
  37. { role: 'redo' },
  38. { type: 'separator' },
  39. { role: 'cut' },
  40. { role: 'copy' },
  41. { role: 'paste' },
  42. ...(isMac
  43. ? [
  44. { role: 'pasteAndMatchStyle' },
  45. { role: 'delete' },
  46. { role: 'selectAll' },
  47. { type: 'separator' },
  48. {
  49. label: 'Speech',
  50. submenu: [{ role: 'startspeaking' }, { role: 'stopspeaking' }],
  51. },
  52. ]
  53. : [{ role: 'delete' }, { type: 'separator' }, { role: 'selectAll' }]),
  54. ],
  55. },
  56. // { role: 'viewMenu' }
  57. {
  58. label: 'View',
  59. submenu: [
  60. { role: 'reload' },
  61. { role: 'forcereload' },
  62. { role: 'toggledevtools' },
  63. { type: 'separator' },
  64. { role: 'resetzoom' },
  65. { role: 'zoomin' },
  66. { role: 'zoomout' },
  67. { type: 'separator' },
  68. { role: 'togglefullscreen' },
  69. ],
  70. },
  71. // { role: 'windowMenu' }
  72. {
  73. label: 'Window',
  74. submenu: [
  75. { role: 'minimize' },
  76. { role: 'zoom' },
  77. ...(isMac
  78. ? [
  79. { type: 'separator' },
  80. { role: 'front' },
  81. { type: 'separator' },
  82. { role: 'window' },
  83. ]
  84. : [{ role: 'close' }]),
  85. ],
  86. },
  87. {
  88. role: 'help',
  89. submenu: [
  90. {
  91. label: 'Learn More',
  92. click: async () => {
  93. const { shell } = require('electron');
  94. await shell.openExternal('https://www.milvus.io');
  95. },
  96. },
  97. {
  98. label: 'Documentation',
  99. click: async () => {
  100. const { shell } = require('electron');
  101. await shell.openExternal(
  102. 'https://www.milvus.io/docs/about_milvus/overview.md'
  103. );
  104. },
  105. },
  106. {
  107. label: 'Github',
  108. click: async () => {
  109. const { shell } = require('electron');
  110. await shell.openExternal('https://github.com/milvus-io/milvus');
  111. },
  112. },
  113. ],
  114. },
  115. ];
  116. const menu = Menu.buildFromTemplate(template);
  117. Menu.setApplicationMenu(menu);
  118. // Keep a global reference of the window object, if you don't, the window will
  119. // be closed automatically when the JavaScript object is garbage collected.
  120. let mainWindow;
  121. function createWindow() {
  122. // Create the browser window.
  123. mainWindow = new BrowserWindow({
  124. width: 1200,
  125. height: 800,
  126. webPreferences: { devTools: false },
  127. icon: './attu.png',
  128. });
  129. // mainWindow.maximize();
  130. // mainWindow.show();
  131. // and load the index.html of the app.
  132. const startUrl =
  133. process.env.ELECTRON_START_URL ||
  134. url.format({
  135. pathname: path.join(__dirname, './index.html'),
  136. protocol: 'file:',
  137. slashes: true,
  138. });
  139. mainWindow.loadURL(startUrl);
  140. // Open the DevTools.
  141. // mainWindow.webContents.openDevTools();
  142. // Emitted when the window is closed.
  143. mainWindow.on('closed', function () {
  144. // Dereference the window object, usually you would store windows
  145. // in an array if your app supports multi windows, this is the time
  146. // when you should delete the corresponding element.
  147. mainWindow = null;
  148. });
  149. }
  150. // This method will be called when Electron has finished
  151. // initialization and is ready to create browser windows.
  152. // Some APIs can only be used after this event occurs.
  153. app.on('ready', createWindow);
  154. // Quit when all windows are closed.
  155. app.on('window-all-closed', function () {
  156. // On OS X it is common for applications and their menu bar
  157. // to stay active until the user quits explicitly with Cmd + Q
  158. if (process.platform !== 'darwin') {
  159. app.quit();
  160. }
  161. });
  162. app.on('activate', function () {
  163. // On OS X it's common to re-create a window in the app when the
  164. // dock icon is clicked and there are no other windows open.
  165. if (mainWindow === null) {
  166. createWindow();
  167. }
  168. });