LOCKING盒子版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.js 6.1 KiB

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