LOCKING盒子版
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

216 řádky
6.0 KiB

  1. const {
  2. app,
  3. BrowserWindow,
  4. dialog,
  5. ipcMain,
  6. shell,
  7. Tray,
  8. Menu,
  9. screen,
  10. } = require('electron');
  11. const path = require('path');
  12. const url = require('url');
  13. const { initialStorageEvents, storage } = require('./storage');
  14. let mainWindow;
  15. function createWindow() {
  16. //创建窗口
  17. mainWindow = new BrowserWindow({
  18. height: 900,
  19. webPreferences: {
  20. preload: path.join(__dirname, 'preload.js'),
  21. webSecurity: false,
  22. nodeIntegration: true,
  23. },
  24. backgroundColor: '#2e2c29',
  25. darkTheme: true,
  26. title: 'Locking',
  27. width: 1700,
  28. frame: false,
  29. minWidth: 1300,
  30. minHeight: 900,
  31. });
  32. // 隐藏菜单栏
  33. mainWindow.setMenuBarVisibility(false);
  34. if (process.env.NODE_ENV === 'development') {
  35. // 开发环境
  36. // 加载页面并打开调试工具,根据 NODE_ENV
  37. // umijs 在dev时会给出相应的url,直接加载即可
  38. mainWindow.loadURL('http://localhost:8000/');
  39. mainWindow.webContents.openDevTools();
  40. } else {
  41. //生产环境
  42. // 加载html文件
  43. // 这里的路径是umi输出的html路径,如果没有修改过,路径和下面是一样的
  44. mainWindow.loadFile(path.join(__dirname, './dist/index.html'));
  45. // mainWindow.webContents.openDevTools();
  46. // mainWindow.loadURL(
  47. // url.format({
  48. // pathname: path.join(__dirname, '../dist/index.html'),
  49. // protocol: 'file:',
  50. // slashes: true,
  51. // }),
  52. // );
  53. }
  54. mainWindow.webContents.on('did-finish-load', () => {
  55. // 加载初始localStorage数据
  56. mainWindow.webContents.send('initialStorageData', storage.getAllItem());
  57. });
  58. mainWindow.on('closed', () => {
  59. mainWindow = null;
  60. });
  61. // 创建系统通知区菜单
  62. tray = new Tray(path.join(__dirname, 'logo.ico'));
  63. const contextMenu = Menu.buildFromTemplate([
  64. {
  65. label: '最大化',
  66. click: () => {
  67. mainWindow.maximize();
  68. },
  69. },
  70. {
  71. label: '最小化',
  72. click: () => {
  73. mainWindow.minimize();
  74. },
  75. },
  76. {
  77. label: '还原',
  78. click: () => {
  79. mainWindow.restore();
  80. },
  81. },
  82. {
  83. label: '退出',
  84. click: () => {
  85. mainWindow.destroy();
  86. notifyWindow.destroy();
  87. },
  88. }, //我们需要在这里有一个真正的退出(这里直接强制退出)
  89. ]);
  90. tray.setToolTip('LOCKING盒子');
  91. tray.setContextMenu(contextMenu);
  92. tray.on('click', () => {
  93. //我们这里模拟桌面程序点击通知区图标实现打开关闭应用的功能
  94. mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
  95. mainWindow.isVisible()
  96. ? mainWindow.setSkipTaskbar(false)
  97. : mainWindow.setSkipTaskbar(true);
  98. });
  99. }
  100. // 窗口操作事件
  101. ipcMain.handle('window:close', (event) => {
  102. const iWindow = BrowserWindow.fromId(event.sender.id);
  103. iWindow.hide();
  104. if (iWindow === mainWindow) {
  105. iWindow.setSkipTaskbar(true);
  106. }
  107. event.preventDefault();
  108. // BrowserWindow.fromId(event.sender.id)?.close();
  109. });
  110. ipcMain.handle('window:zoom', (event) => {
  111. const iWindow = BrowserWindow.fromId(event.sender.id);
  112. if (!iWindow) return;
  113. iWindow.isMaximized() ? iWindow.unmaximize() : iWindow.maximize();
  114. });
  115. ipcMain.handle('window:minimize', (event) => {
  116. BrowserWindow.fromId(event.sender.id)?.minimize();
  117. });
  118. ipcMain.handle('window:hide', (event) => {
  119. // console.log(event.sender.id, event.senderFrame, event);
  120. BrowserWindow.fromId(event.sender.id)?.hide();
  121. });
  122. // 选择文件夹
  123. ipcMain.handle('select-folder', async (event, args) => {
  124. const res = await dialog.showOpenDialog({
  125. properties: ['openDirectory'],
  126. });
  127. return res;
  128. });
  129. // 打开浏览器
  130. ipcMain.handle('open-browser', (event, url) => {
  131. shell.openExternal(url);
  132. });
  133. // 打开文件所在位置
  134. ipcMain.handle('open-file-position', (event, message) => {
  135. shell.showItemInFolder(message.absolutePath);
  136. });
  137. // 主窗口给消息窗口notifyWindow发送消息
  138. ipcMain.handle('notify', (event, messageObj) => {
  139. // if (!mainWindow.isVisible()) { // 在主窗口不可见时才给消息窗口返送消息
  140. notifyWindow.show();
  141. notifyWindow.webContents.send('on-notify', messageObj);
  142. // }
  143. });
  144. // 消息窗口给主窗口发送重新同步文件的命令
  145. ipcMain.handle('re-sync-file', (event, messageObj) => {
  146. mainWindow.webContents.send('request-resync-file', messageObj);
  147. });
  148. ipcMain.handle('download-file', (event, messageObj) => {
  149. mainWindow.webContents.send('request-download-file', messageObj);
  150. });
  151. // 初始化electron-store相关API
  152. initialStorageEvents(ipcMain);
  153. app.on('ready', () => {
  154. const { width: windowWidth, height: windowHeight } =
  155. screen.getPrimaryDisplay().workAreaSize;
  156. createWindow();
  157. createNotifycationWindow(windowWidth, windowHeight);
  158. app.on('activate', function () {
  159. // On macOS it's common to re-create a window in the app when the
  160. // dock icon is clicked and there are no other windows open.
  161. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  162. });
  163. });
  164. app.on('second-instance', (event, commandLine, workingDirectory) => {
  165. // 当运行第二个实例时,将会聚焦到mainWindow这个窗口
  166. if (mainWindow) {
  167. if (mainWindow.isMinimized()) mainWindow.restore();
  168. mainWindow.focus();
  169. mainWindow.show();
  170. }
  171. });
  172. app.on('window-all-closed', () => {
  173. if (process.platform !== 'darwin') {
  174. app.quit();
  175. }
  176. });
  177. let notifyWindow;
  178. function createNotifycationWindow(windowWidth, windowHeight) {
  179. if (notifyWindow) return notifyWindow;
  180. //创建窗口
  181. notifyWindow = new BrowserWindow({
  182. width: 504,
  183. height: 219, // 184,
  184. x: windowWidth - 480 - 20,
  185. y: windowHeight - 219 + 10,
  186. webPreferences: {
  187. preload: path.join(__dirname, 'preload.js'),
  188. webSecurity: false,
  189. nodeIntegration: true,
  190. },
  191. frame: false,
  192. resizable: false,
  193. transparent: true,
  194. alwaysOnTop: true,
  195. });
  196. // 这里的路径是umi输出的html路径,如果没有修改过,路径和下面是一样的
  197. notifyWindow.webContents.openDevTools();
  198. notifyWindow.loadFile(path.join(__dirname, 'notifycation.html'));
  199. notifyWindow.setSkipTaskbar(true);
  200. notifyWindow.hide();
  201. // return notifyWindow;
  202. }