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.

233 lines
8.0 KiB

4 years ago
4 years ago
4 years ago
  1. const {BrowserWindow} = require('electron');
  2. const ipc = require('electron').ipcMain;
  3. // const screen = require('electron').screen;
  4. const store = require('./windows/lib/store');
  5. const {create: createMainWindow} = require('./windows/main')
  6. // const { create: createLoginWindow } = require('./windows/login')
  7. // const { create: createAppWindow } = require('./windows/app')
  8. const childProcess = require('./child_process')
  9. const {MAIN_WINDOW_FLAG, SUSPENSION_WINDOW_FLAG} = require('./constant');
  10. // const installApp = require('./ipc/installApp');
  11. // const getApps = require('./ipc/getApps');
  12. // const removeApp = require('./ipc/removeApp');
  13. module.exports = () => {
  14. let winStartPostion = {x: 0, y: 0};
  15. let mouseStartPosition = {x: 0, y: 0};
  16. let movingInterVal = null;
  17. ipc.on('getUnReadMessage', (events, callback) => {
  18. const msg = store.get('unReadMessage');
  19. // console.log('msg', msg);
  20. const windows = BrowserWindow.getAllWindows()
  21. const found = windows.find((window) => {
  22. return window[SUSPENSION_WINDOW_FLAG]
  23. });
  24. found.webContents.send("getUnReadMessage", [msg])
  25. });
  26. ipc.on('setUnReadMessage', (events, message) => {
  27. // console.log('set', message);
  28. store.set('unReadMessage', message);
  29. // 消息需要实时同步
  30. const windows = BrowserWindow.getAllWindows()
  31. const win = windows.find((window) => {
  32. return window[SUSPENSION_WINDOW_FLAG]
  33. });
  34. if (win) {
  35. // let winWidth = win.getSize()[0];
  36. let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
  37. const x = win.getPosition()[0];
  38. const y = win.getPosition()[1];
  39. let sWidth = areaSize.width;
  40. // console.log(sWidth - x);
  41. if (sWidth - x === 130) {
  42. win.setSize(640, 130, true);
  43. win.setPosition(sWidth - 660, y, true);
  44. }
  45. win.webContents.send("getUnReadMessage", [message])
  46. }
  47. });
  48. ipc.on("showSuspensionWindow",()=>{
  49. let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
  50. const windows = BrowserWindow.getAllWindows()
  51. const win = windows.find((window) => {
  52. return window[SUSPENSION_WINDOW_FLAG]
  53. });
  54. console.log('恢复原始大小')
  55. win.setSize(640, 130, true);
  56. const y = win.getPosition()[1];
  57. win.setPosition(areaSize.width - win.getSize()[0], y, true);
  58. win.webContents.send("winResize", [win.getSize()])
  59. })
  60. ipc.on('resizeWindow', () => {
  61. let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
  62. const windows = BrowserWindow.getAllWindows()
  63. const win = windows.find((window) => {
  64. return window[SUSPENSION_WINDOW_FLAG]
  65. });
  66. const startWidth = win.getSize()[0];
  67. if(startWidth < 640){
  68. const x = win.getPosition()[0];
  69. const y = win.getPosition()[1];
  70. win.setSize(640, 130, true);
  71. if(x > areaSize.width - win.getSize()[0] ){
  72. win.setPosition(areaSize.width - win.getSize()[0] - 50, y, true);
  73. }
  74. }
  75. });
  76. ipc.on('windowMoveHandle', (events, canMoving) => {
  77. const windows = BrowserWindow.getAllWindows()
  78. const win = windows.find((window) => {
  79. return window[SUSPENSION_WINDOW_FLAG]
  80. });
  81. const screen = require('electron').screen;
  82. /**
  83. * 窗口移动事件
  84. */
  85. if (canMoving) {
  86. if (!win) return;
  87. //读取原位置
  88. const winPosition = win.getPosition();
  89. winStartPostion = {x: winPosition[0], y: winPosition[1]};
  90. mouseStartPosition = screen.getCursorScreenPoint();
  91. //清除
  92. if (movingInterVal) {
  93. clearInterval(movingInterVal);
  94. }
  95. //新开
  96. movingInterVal = setInterval(() => {
  97. const cursorPosition = screen.getCursorScreenPoint();
  98. const x = winStartPostion.x + cursorPosition.x - mouseStartPosition.x;
  99. const y = winStartPostion.y + cursorPosition.y - mouseStartPosition.y;
  100. win.setPosition(x, y, true)
  101. }, 20)
  102. } else {
  103. clearInterval(movingInterVal);
  104. let areaSize = require('electron').screen.getPrimaryDisplay().workAreaSize;
  105. const x = win.getPosition()[0];
  106. const y = win.getPosition()[1];
  107. store.set("position",{x,y});
  108. let sWidth = areaSize.width;
  109. let winWidth = win.getSize()[0];
  110. // console.log(x + winWidth >= sWidth, sWidth - x - winWidth);
  111. // 靠边吸附效果
  112. console.log("0000000",x + winWidth >= sWidth,sWidth - x - winWidth <= 20)
  113. if (x + winWidth >= sWidth || sWidth - x - winWidth <= 20) {
  114. win.setResizable(true);
  115. win.setSize(130,130,true)
  116. win.setPosition(sWidth - 130, y, true);
  117. win.setResizable(false);
  118. console.log("靠边吸附",win.getSize(),win.getPosition())
  119. //通知渲染进程大小发生改变
  120. }else{
  121. win.setSize(650,130,true)
  122. }
  123. win.webContents.send("winResize", [win.getSize()])
  124. movingInterVal = null;
  125. }
  126. });
  127. ipc.on('showMainWindow', (event, arg) => {
  128. const windows = BrowserWindow.getAllWindows()
  129. const found = windows.find((window) => {
  130. return window[MAIN_WINDOW_FLAG]
  131. })
  132. if (found) {
  133. found.restore();
  134. found.show();
  135. } else {
  136. createMainWindow()
  137. }
  138. return true
  139. });
  140. ipc.on('hideMainWindow', () => {
  141. const windows = BrowserWindow.getAllWindows()
  142. const found = windows.find((window) => {
  143. return window[MAIN_WINDOW_FLAG]
  144. })
  145. if (found) {
  146. found.hide();
  147. }
  148. return true
  149. });
  150. ipc.on('minimize', () => {
  151. const windows = BrowserWindow.getAllWindows()
  152. const found = windows.find((window) => {
  153. return window[MAIN_WINDOW_FLAG]
  154. })
  155. if (found) {
  156. found.minimize();
  157. }
  158. return true
  159. });
  160. ipc.on('hideSuspensionWindow', (event, arg) => {
  161. const windows = BrowserWindow.getAllWindows()
  162. const found = windows.find((window) => {
  163. return window[SUSPENSION_WINDOW_FLAG]
  164. });
  165. if (found) {
  166. found.close()
  167. }
  168. return true
  169. });
  170. ipc.on('openDevTools', (event, arg) => {
  171. event.sender.webContents.openDevTools()
  172. return true
  173. });
  174. ipc.on('exitSystem', () => {
  175. const windows = BrowserWindow.getAllWindows()
  176. const mainWindow = windows.find((window) => {
  177. return window[MAIN_WINDOW_FLAG]
  178. })
  179. const suspWindow = windows.find((window) => {
  180. return window[SUSPENSION_WINDOW_FLAG]
  181. });
  182. store.delete("unReadMessage");
  183. mainWindow && mainWindow.close();
  184. suspWindow && suspWindow.close();
  185. });
  186. // ipcMain.handle('openWidget', (event, widget = {}) => {
  187. // createAppWindow(widget)
  188. // return true
  189. // });
  190. //
  191. // ipcMain.handle('openDevTools', (event, arg) => {
  192. // event.sender.webContents.openDevTools()
  193. // return true
  194. // });
  195. // ipcMain.handle('installApp', installApp);
  196. //
  197. // ipcMain.handle('getApps', getApps);
  198. //
  199. // ipcMain.handle('removeApp', removeApp);
  200. /*
  201. ipcRenderer.send('spawn', {
  202. command: '微信.exe',
  203. args: null || ['--disable-cache'],
  204. key: + new Date // 唯一key,用来做关闭识别
  205. })
  206. */
  207. ipcMain.on('spawn', (event, arg) => {
  208. const pid = childProcess.start(arg.command, arg.args)
  209. event.sender.send('spawn-success', {
  210. key: arg.key,
  211. pid
  212. })
  213. })
  214. // ipcRenderer.send('kill-process', [pid])
  215. ipcMain.on('kill-process', function (event, arg) {
  216. childProcess.kill(arg[0])
  217. })
  218. }