electron launcher
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.

433 lines
15 KiB

2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
4 years ago
  1. const {BrowserWindow, desktopCapturer, session} = require('electron');
  2. // const {net} = require('electron');
  3. const electron = require('electron');
  4. const ipc = require('electron').ipcMain;
  5. // const screen = require('electron').screen;
  6. const store = require('./windows/lib/store');
  7. const {create: createMainWindow} = require('./windows/main')
  8. // const { create: createLoginWindow } = require('./windows/login')
  9. // const { create: createAppWindow } = require('./windows/app')
  10. const childProcess = require('./child_process')
  11. const {create: openWindow} = require('./windows/application')
  12. // const querystring = require("querystring");
  13. const {MAIN_WINDOW_FLAG, SUSPENSION_WINDOW_FLAG} = require('./constant');
  14. const { ipcRenderer } = require('electron/renderer');
  15. // const installApp = require('./ipc/installApp');
  16. // const getApps = require('./ipc/getApps');
  17. // const removeApp = require('./ipc/removeApp');
  18. // connect redis
  19. // const {createClient} = require('redis')
  20. // ;(async () => {
  21. // /**
  22. // * createClient({
  23. // * url: 'redis://alice:foobared@awesome.redis.server:6380'
  24. // * });
  25. // */
  26. // const client = createClient(
  27. // // {
  28. // // url: "redis://localhost:3807"
  29. // // }
  30. // );
  31. // // client.on('error', (err) => console.log('Redis Client Error', err));
  32. // await client.connect()
  33. // // 订阅者
  34. // const subscriber = client.duplicate();
  35. // await subscriber.connect();
  36. // // 发布者
  37. // const publisher = client.duplicate()
  38. // await publisher.connect()
  39. // // 通过main向外发布消息
  40. // ipc.on('publishMessage', (events, {channel, ...message}) => {
  41. // publisher.publish(channel, JSON.stringify(message))
  42. // })
  43. // // 订阅外部消息
  44. // subscriber.subscribe('openMap', message => {
  45. // const windows = BrowserWindow.getAllWindows()
  46. // const found = windows.find((window) => {
  47. // return window[MAIN_WINDOW_FLAG]
  48. // });
  49. // found.webContents.send('openMap', JSON.parse(message))
  50. // })
  51. // // 订阅外部消息(正则)
  52. // subscriber.pSubscribe('openMap*', (message, channel) => {
  53. // const windows = BrowserWindow.getAllWindows()
  54. // const found = windows.find((window) => {
  55. // return window[MAIN_WINDOW_FLAG]
  56. // });
  57. // found.webContents.send(channel, JSON.parse(message))
  58. // })
  59. // })();
  60. module.exports = () => {
  61. let winStartPostion = {x: 0, y: 0};
  62. let mouseStartPosition = {x: 0, y: 0};
  63. let movingInterVal = null;
  64. ipc.on('getUnReadMessage', (events, callback) => {
  65. const msg = store.get('unReadMessage');
  66. // console.log('msg', msg);
  67. const windows = BrowserWindow.getAllWindows()
  68. const found = windows.find((window) => {
  69. return window[SUSPENSION_WINDOW_FLAG]
  70. });
  71. found.webContents.send("getUnReadMessage", [msg])
  72. });
  73. ipc.on('setUnReadMessage', (events, message) => {
  74. // console.log('set', message);
  75. store.set('unReadMessage', message);
  76. // 消息需要实时同步
  77. const windows = BrowserWindow.getAllWindows()
  78. const win = windows.find((window) => {
  79. return window[SUSPENSION_WINDOW_FLAG]
  80. });
  81. if (win) {
  82. // let winWidth = win.getSize()[0];
  83. let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
  84. const x = win.getPosition()[0];
  85. const y = win.getPosition()[1];
  86. let sWidth = areaSize.width;
  87. // console.log(sWidth - x);
  88. if (sWidth - x === 130) {
  89. win.setSize(410, 130, true);
  90. win.setPosition(sWidth - 660, y, true);
  91. }
  92. win.webContents.send("getUnReadMessage", [message])
  93. }
  94. });
  95. ipc.on("showSuspensionWindow", () => {
  96. let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
  97. const windows = BrowserWindow.getAllWindows()
  98. const win = windows.find((window) => {
  99. return window[SUSPENSION_WINDOW_FLAG]
  100. });
  101. console.log('恢复原始大小')
  102. win.setSize(410, 130, true);
  103. const y = win.getPosition()[1];
  104. win.setPosition(areaSize.width - win.getSize()[0], y, true);
  105. win.webContents.send("winResize", [win.getSize()])
  106. })
  107. ipc.on('resizeWindow', () => {
  108. let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
  109. const windows = BrowserWindow.getAllWindows()
  110. const win = windows.find((window) => {
  111. return window[SUSPENSION_WINDOW_FLAG]
  112. });
  113. const startWidth = win.getSize()[0];
  114. if (startWidth < 410) {
  115. const x = win.getPosition()[0];
  116. const y = win.getPosition()[1];
  117. win.setSize(410, 130, true);
  118. if (x > areaSize.width - win.getSize()[0]) {
  119. win.setPosition(areaSize.width - win.getSize()[0] - 50, y, true);
  120. }
  121. }
  122. });
  123. ipc.on('windowMoveHandle', (events, canMoving) => {
  124. const windows = BrowserWindow.getAllWindows()
  125. const win = windows.find((window) => {
  126. return window[SUSPENSION_WINDOW_FLAG]
  127. });
  128. const screen = require('electron').screen;
  129. /**
  130. * 窗口移动事件
  131. */
  132. if (!win) return;
  133. if (canMoving) {
  134. //读取原位置
  135. const winPosition = win.getPosition();
  136. winStartPostion = {x: winPosition[0], y: winPosition[1]};
  137. mouseStartPosition = screen.getCursorScreenPoint();
  138. //清除
  139. if (movingInterVal) {
  140. clearInterval(movingInterVal);
  141. }
  142. //新开
  143. movingInterVal = setInterval(() => {
  144. if(!win){
  145. clearInterval(movingInterVal);
  146. return;
  147. }
  148. const cursorPosition = screen.getCursorScreenPoint();
  149. const x = winStartPostion.x + cursorPosition.x - mouseStartPosition.x;
  150. const y = winStartPostion.y + cursorPosition.y - mouseStartPosition.y;
  151. win.setPosition(x, y, true)
  152. }, 20)
  153. } else {
  154. clearInterval(movingInterVal);
  155. let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
  156. const x = win.getPosition()[0];
  157. const y = win.getPosition()[1];
  158. store.set("position", {x, y});
  159. let sWidth = areaSize.width;
  160. let winWidth = win.getSize()[0];
  161. // console.log(x + winWidth >= sWidth, sWidth - x - winWidth);
  162. // 靠边吸附效果
  163. console.log("0000000", x + winWidth >= sWidth, sWidth - x - winWidth <= 20)
  164. if (x + winWidth >= sWidth || sWidth - x - winWidth <= 20) {
  165. win.setResizable(true);
  166. win.setSize(130, 130, true)
  167. win.setPosition(sWidth - 130, y, true);
  168. win.setResizable(false);
  169. console.log("靠边吸附", win.getSize(), win.getPosition())
  170. //通知渲染进程大小发生改变
  171. } else {
  172. win.setSize(410, 130, true)
  173. }
  174. win.webContents.send("winResize", [win.getSize()])
  175. movingInterVal = null;
  176. }
  177. });
  178. ipc.on('showMainWindow', (event, arg) => {
  179. const windows = BrowserWindow.getAllWindows()
  180. const found = windows.find((window) => {
  181. return window[MAIN_WINDOW_FLAG]
  182. })
  183. if (found) {
  184. found.setFullScreen(true)
  185. found.restore();
  186. found.show();
  187. } else {
  188. createMainWindow()
  189. }
  190. return true
  191. });
  192. ipc.on('hideMainWindow', () => {
  193. console.log('最小化hideMainWindow');
  194. const windows = BrowserWindow.getAllWindows()
  195. const found = windows.find((window) => {
  196. return window[MAIN_WINDOW_FLAG]
  197. })
  198. if (found) {
  199. found.setFullScreen(false)
  200. found.hide();
  201. }
  202. return true
  203. });
  204. ipc.on('minimize', () => {
  205. // const windows = BrowserWindow.getAllWindows()
  206. // const found = windows.find((window) => {
  207. // return window[MAIN_WINDOW_FLAG]
  208. // })
  209. // if (found) {
  210. // found.minimize();
  211. // }
  212. // return true
  213. try {
  214. const windows = BrowserWindow.getAllWindows()
  215. for (let win of windows) {
  216. win && win.minimize()
  217. }
  218. }catch (e) {}
  219. });
  220. ipc.on('hideSuspensionWindow', (event, arg) => {
  221. const windows = BrowserWindow.getAllWindows()
  222. const found = windows.find((window) => {
  223. return window[SUSPENSION_WINDOW_FLAG]
  224. });
  225. if (found) {
  226. found.close()
  227. }
  228. return true
  229. });
  230. ipc.on('openDevTools', (event, arg) => {
  231. event.sender.webContents.openDevTools()
  232. return true
  233. });
  234. ipc.on('exitSystem', () => {
  235. console.log('global',global.worker);
  236. try {
  237. const windows = BrowserWindow.getAllWindows()
  238. for (let win of windows) {
  239. win && win.close()
  240. }
  241. global.worker === 'stop'
  242. store.delete("unReadMessage");
  243. }catch (e) {
  244. }
  245. });
  246. // ipc.handle('openWidget', (event, widget = {}) => {
  247. // createAppWindow(widget)
  248. // return true
  249. // });
  250. //
  251. // ipc.handle('openDevTools', (event, arg) => {
  252. // event.sender.webContents.openDevTools()
  253. // return true
  254. // });
  255. // ipc.handle('installApp', installApp);
  256. //
  257. // ipc.handle('getApps', getApps);
  258. //
  259. // ipc.handle('removeApp', removeApp);
  260. /* ipcRenderer
  261. ipcRenderer.send('spawn', {
  262. command: '微信.exe',
  263. args: null || ['--disable-cache'],
  264. key: + new Date // 唯一key,用来做关闭识别,
  265. })
  266. */
  267. ipc.on('spawn', (event, arg) => {
  268. const {pid, promise} = childProcess.start(arg.command, arg.args, arg.key)
  269. event.sender.send('spawn-success', {
  270. key: arg.key,
  271. pid
  272. })
  273. promise.then(data => {
  274. event.sender.send('spawn-success', {
  275. key: arg.key,
  276. data
  277. })
  278. })
  279. promise.catch(err => {
  280. event.sender.send('spawn-error', {
  281. key: arg.key,
  282. err
  283. })
  284. })
  285. })
  286. // ipcRenderer.send('kill-process', [pid])
  287. ipc.on('kill-process', function (event, arg) {
  288. childProcess.kill(arg[0])
  289. })
  290. // 通信桥梁
  291. ipc.on('bridge', function (event, {channel, targetId, ...message}) {
  292. console.log('bridge message', channel, message)
  293. const windows = BrowserWindow.getAllWindows()
  294. // const found = windows.find((window) => {
  295. // return window[SUSPENSION_WINDOW_FLAG]
  296. // });
  297. for (let win of windows) {
  298. if (!targetId || win[targetId]) {
  299. win.webContents.send(channel, message);
  300. }
  301. }
  302. })
  303. const windowMap = new Map();
  304. // 通过main打开新窗口
  305. ipc.on('openWindow', function (event, arg) {
  306. //如果存在第二块屏幕,在第二块屏幕打开
  307. let displays = electron.screen.getAllDisplays();
  308. let externalDisplay = displays.find((display)=>{
  309. return display.bounds.x !== 0 || display.bounds.y !== 0
  310. });
  311. let options = {};
  312. if(externalDisplay){
  313. options = {
  314. fullscreen:true,
  315. x:externalDisplay.bounds.x,
  316. y:externalDisplay.bounds.y,
  317. };
  318. //关闭上一个,同一时间在拓展屏只能打开两个,打开多个或引起卡顿
  319. if(windowMap.size > 2){
  320. const win = windowMap.values().next().value;
  321. win.close();
  322. windowMap.delete(windowMap.keys().next().value);
  323. }
  324. }
  325. console.log('openWindow request from renderer process', arg);
  326. const windows = BrowserWindow.getAllWindows();
  327. const found = windows.find((window) => {
  328. return window[arg.id]
  329. });
  330. if (!arg.id || !arg.entry) {
  331. return;
  332. }
  333. //未打开过,新建窗口
  334. if(!found){
  335. const temp = openWindow(arg,options)
  336. windowMap.set(arg.id,temp);
  337. }else{//打开且未关闭,切换至前台
  338. found.focus()
  339. }
  340. })
  341. //返回上一个窗口
  342. ipc.on("backPreviousApp",function (event,arg) {
  343. const keys = [...windowMap.keys()];
  344. const curKey = keys.findIndex(v=>v === arg.id);
  345. if(keys.length > 1){
  346. let preKey = curKey > 0? curKey - 1 : keys.length - 1;
  347. const preId = keys[preKey];
  348. windowMap.get(preId).focus();
  349. }
  350. });
  351. //根据指定id关闭窗口
  352. ipc.on('closeWindowById', function (event, arg) {
  353. const windows = BrowserWindow.getAllWindows();
  354. const found = windows.find((window) => {
  355. return window[arg.id]
  356. });
  357. found.close();
  358. windowMap.delete(arg.id);
  359. })
  360. // 监听调用截屏事件
  361. ipc.on('PrtSc', function (event, arg) {
  362. const windows = BrowserWindow.getAllWindows()
  363. const found = windows.find((window) => {
  364. return window[MAIN_WINDOW_FLAG]
  365. })
  366. if (found) {
  367. found.webContents.send('PrtSc');
  368. }
  369. })
  370. // 监听下载截屏的图片
  371. ipc.on('downloadImg', function (event, arg) {
  372. const windows = BrowserWindow.getAllWindows()
  373. const found = windows.find((window) => {
  374. return window[MAIN_WINDOW_FLAG]
  375. })
  376. if (found) {
  377. found.webContents.send('downloadImg');
  378. }
  379. })
  380. // 取消截屏显示
  381. ipc.on('removeCanvas', function (event, arg) {
  382. const windows = BrowserWindow.getAllWindows()
  383. const found = windows.find((window) => {
  384. return window[MAIN_WINDOW_FLAG]
  385. })
  386. if (found) {
  387. found.webContents.send('removeCanvas');
  388. }
  389. })
  390. // 获取cookie
  391. ipc.on('getCookies', function (event, arg){
  392. console.log('getCookies--',session.defaultSession.cookies.get)
  393. session.defaultSession.cookies.get({}, function(error, cookies) {
  394. console.log('cookies--', cookies);
  395. });
  396. })
  397. // 获取文件路径
  398. ipc.on('getFilePath', function (event, arg) {
  399. const windows = BrowserWindow.getAllWindows()
  400. const found = windows.find((window) => {
  401. return window[MAIN_WINDOW_FLAG]
  402. })
  403. if (found) {
  404. found.webContents.send('getFilePath', arg);
  405. }
  406. })
  407. }