main.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. const { app, BrowserWindow, Menu, shell } = require('electron');
  2. const path = require('path');
  3. // Keep a global reference of the window object
  4. let mainWindow;
  5. function createWindow() {
  6. // Create the browser window
  7. mainWindow = new BrowserWindow({
  8. width: 1200,
  9. height: 800,
  10. minWidth: 800,
  11. minHeight: 600,
  12. icon: path.join(__dirname, 'img/logo.png'),
  13. webPreferences: {
  14. nodeIntegration: false,
  15. contextIsolation: true,
  16. enableRemoteModule: false,
  17. webSecurity: true
  18. },
  19. show: false // Don't show until ready
  20. });
  21. // Load the home page
  22. mainWindow.loadFile(path.join(__dirname, 'home.html'));
  23. // Show window when ready to prevent visual flash
  24. mainWindow.once('ready-to-show', () => {
  25. mainWindow.show();
  26. });
  27. // Handle external links
  28. mainWindow.webContents.setWindowOpenHandler(({ url }) => {
  29. shell.openExternal(url);
  30. return { action: 'deny' };
  31. });
  32. // Emitted when the window is closed
  33. mainWindow.on('closed', () => {
  34. mainWindow = null;
  35. });
  36. // Create application menu
  37. createMenu();
  38. }
  39. function createMenu() {
  40. const template = [
  41. {
  42. label: 'File',
  43. submenu: [
  44. {
  45. label: 'New Note',
  46. accelerator: 'CmdOrCtrl+N',
  47. click: () => {
  48. mainWindow.loadFile(path.join(__dirname, 'index.html'));
  49. }
  50. },
  51. {
  52. label: 'Home',
  53. accelerator: 'CmdOrCtrl+H',
  54. click: () => {
  55. mainWindow.loadFile(path.join(__dirname, 'home.html'));
  56. }
  57. },
  58. { type: 'separator' },
  59. {
  60. label: 'Quit',
  61. accelerator: process.platform === 'darwin' ? 'Cmd+Q' : 'Ctrl+Q',
  62. click: () => {
  63. app.quit();
  64. }
  65. }
  66. ]
  67. },
  68. {
  69. label: 'Edit',
  70. submenu: [
  71. { role: 'undo' },
  72. { role: 'redo' },
  73. { type: 'separator' },
  74. { role: 'cut' },
  75. { role: 'copy' },
  76. { role: 'paste' },
  77. { role: 'selectall' }
  78. ]
  79. },
  80. {
  81. label: 'View',
  82. submenu: [
  83. { role: 'reload' },
  84. { role: 'forceReload' },
  85. { role: 'toggleDevTools' },
  86. { type: 'separator' },
  87. { role: 'resetZoom' },
  88. { role: 'zoomIn' },
  89. { role: 'zoomOut' },
  90. { type: 'separator' },
  91. { role: 'togglefullscreen' }
  92. ]
  93. },
  94. {
  95. label: 'Window',
  96. submenu: [
  97. { role: 'minimize' },
  98. { role: 'close' }
  99. ]
  100. }
  101. ];
  102. // macOS specific menu adjustments
  103. if (process.platform === 'darwin') {
  104. template.unshift({
  105. label: app.getName(),
  106. submenu: [
  107. { role: 'about' },
  108. { type: 'separator' },
  109. { role: 'services' },
  110. { type: 'separator' },
  111. { role: 'hide' },
  112. { role: 'hideothers' },
  113. { role: 'unhide' },
  114. { type: 'separator' },
  115. { role: 'quit' }
  116. ]
  117. });
  118. // Window menu
  119. template[4].submenu = [
  120. { role: 'close' },
  121. { role: 'minimize' },
  122. { role: 'zoom' },
  123. { type: 'separator' },
  124. { role: 'front' }
  125. ];
  126. }
  127. const menu = Menu.buildFromTemplate(template);
  128. Menu.setApplicationMenu(menu);
  129. }
  130. // This method will be called when Electron has finished initialization
  131. app.whenReady().then(() => {
  132. createWindow();
  133. app.on('activate', () => {
  134. // On macOS, re-create a window when the dock icon is clicked
  135. if (BrowserWindow.getAllWindows().length === 0) {
  136. createWindow();
  137. }
  138. });
  139. });
  140. // Quit when all windows are closed
  141. app.on('window-all-closed', () => {
  142. // On macOS, keep the app running even when all windows are closed
  143. if (process.platform !== 'darwin') {
  144. app.quit();
  145. }
  146. });
  147. // Security: Prevent new window creation
  148. app.on('web-contents-created', (event, contents) => {
  149. contents.on('new-window', (event, url) => {
  150. event.preventDefault();
  151. shell.openExternal(url);
  152. });
  153. });