LOCKING盒子版
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

main.js 6.1 KiB

3年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // if (!mainWindow.isVisible()) { // 在主窗口不可见时才给消息窗口返送消息
  141. notifyWindow.show();
  142. notifyWindow.webContents.send('on-notify', messageObj);
  143. // }
  144. });
  145. // 消息窗口给主窗口发送重新同步文件的命令
  146. ipcMain.handle('re-sync-file', (event, messageObj) => {
  147. mainWindow.webContents.send('request-resync-file', messageObj);
  148. });
  149. ipcMain.handle('download-file', (event, messageObj) => {
  150. mainWindow.webContents.send('request-download-file', messageObj);
  151. });
  152. // 初始化electron-store相关API
  153. initialStorageEvents(ipcMain);
  154. app.on('ready', () => {
  155. const { width: windowWidth, height: windowHeight } =
  156. screen.getPrimaryDisplay().workAreaSize;
  157. createWindow();
  158. createNotifycationWindow(windowWidth, windowHeight);
  159. app.on('activate', function () {
  160. // On macOS it's common to re-create a window in the app when the
  161. // dock icon is clicked and there are no other windows open.
  162. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  163. });
  164. });
  165. app.on('second-instance', (event, commandLine, workingDirectory) => {
  166. // 当运行第二个实例时,将会聚焦到mainWindow这个窗口
  167. if (mainWindow) {
  168. if (mainWindow.isMinimized()) mainWindow.restore();
  169. mainWindow.focus();
  170. mainWindow.show();
  171. }
  172. });
  173. app.on('window-all-closed', () => {
  174. if (process.platform !== 'darwin') {
  175. app.quit();
  176. }
  177. });
  178. let notifyWindow;
  179. function createNotifycationWindow(windowWidth, windowHeight) {
  180. if (notifyWindow) return notifyWindow;
  181. //创建窗口
  182. notifyWindow = new BrowserWindow({
  183. width: 504,
  184. height: 219, // 184,
  185. x: windowWidth - 480 - 20,
  186. y: windowHeight - 219 + 10,
  187. webPreferences: {
  188. preload: path.join(__dirname, 'preload.js'),
  189. webSecurity: false,
  190. nodeIntegration: true,
  191. },
  192. frame: false,
  193. resizable: false,
  194. transparent: true,
  195. alwaysOnTop: true,
  196. });
  197. // 这里的路径是umi输出的html路径,如果没有修改过,路径和下面是一样的
  198. if (isDev) {
  199. notifyWindow.webContents.openDevTools();
  200. }
  201. notifyWindow.loadFile(path.join(__dirname, 'notifycation.html'));
  202. notifyWindow.setSkipTaskbar(true);
  203. notifyWindow.hide();
  204. // return notifyWindow;
  205. }