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ů.
 
 
 
 

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