LOCKING盒子版
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

main.js 4.0 KiB

3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const { app, BrowserWindow, dialog, ipcMain, shell } = require('electron');
  2. const path = require('path');
  3. const url = require('url');
  4. const { initialStorageEvents, storage } = require('./storage');
  5. const { initialWebsocketEvents } = require('./socket');
  6. const { Subject } = require('./tool');
  7. let mainWindow;
  8. const socketSubjects = {
  9. onMessage: new Subject(),
  10. onError: new Subject(),
  11. onClose: new Subject(),
  12. };
  13. function createWindow() {
  14. //创建窗口
  15. mainWindow = new BrowserWindow({
  16. height: 900,
  17. webPreferences: {
  18. preload: path.join(__dirname, 'preload.js'),
  19. webSecurity: false,
  20. nodeIntegration: true,
  21. },
  22. backgroundColor: '#2e2c29',
  23. darkTheme: true,
  24. title: 'Locking',
  25. width: 1700,
  26. frame: false,
  27. minWidth: 1300,
  28. minHeight: 900,
  29. });
  30. // 隐藏菜单栏
  31. mainWindow.setMenuBarVisibility(false);
  32. if (process.env.NODE_ENV === 'development') {
  33. // 开发环境
  34. // 加载页面并打开调试工具,根据 NODE_ENV
  35. // umijs 在dev时会给出相应的url,直接加载即可
  36. mainWindow.loadURL('http://localhost:8000/');
  37. mainWindow.webContents.openDevTools();
  38. } else {
  39. //生产环境
  40. // 加载html文件
  41. // 这里的路径是umi输出的html路径,如果没有修改过,路径和下面是一样的
  42. mainWindow.webContents.openDevTools();
  43. // mainWindow.loadFile(path.join(__dirname, '../dist/index.html'));
  44. mainWindow.loadURL(
  45. url.format({
  46. pathname: path.join(__dirname, '../dist/index.html'),
  47. protocol: 'file:',
  48. slashes: true,
  49. }),
  50. );
  51. }
  52. const onMessageReceive = (message) => {
  53. mainWindow.webContents.send('socket:on-message', message);
  54. };
  55. const onSocketError = (...args) => {
  56. mainWindow.webContents.send('socket:on-error', ...args);
  57. };
  58. const onSocketClose = (...args) => {
  59. mainWindow.webContents.send('socket:on-close', ...args);
  60. };
  61. mainWindow.webContents.on('did-finish-load', () => {
  62. // 加载初始localStorage数据
  63. mainWindow.webContents.send('initialStorageData', storage.getAllItem());
  64. socketSubjects.onMessage.add(onMessageReceive);
  65. socketSubjects.onError.add(onSocketError);
  66. socketSubjects.onClose.add(onSocketClose);
  67. });
  68. mainWindow.on('closed', () => {
  69. mainWindow = null;
  70. socketSubjects.onMessage.remove(onMessageReceive);
  71. socketSubjects.onError.remove(onSocketError);
  72. socketSubjects.onClose.remove(onSocketClose);
  73. });
  74. }
  75. // 监听必要的自定义事件
  76. ipcMain.handle('manipulate-window', (event, { action }) => {
  77. // console.log('manipulate-window', event, action);
  78. const iWindow = BrowserWindow.fromId(event.frameId);
  79. if (!iWindow) {
  80. return;
  81. }
  82. if (action === 'close') {
  83. // 关闭窗口
  84. iWindow.close();
  85. }
  86. if (action === 'zoom') {
  87. // 最大化/恢复窗口
  88. if (iWindow.isMaximized()) {
  89. iWindow.unmaximize();
  90. } else {
  91. iWindow.maximize();
  92. }
  93. }
  94. if (action === 'minimize') {
  95. // 最小化窗口
  96. iWindow.minimize();
  97. }
  98. });
  99. // 选择文件夹
  100. ipcMain.handle('project-choose-folders', async (event, args) => {
  101. const res = await dialog.showOpenDialog({
  102. properties: ['multiSelections', 'openDirectory'],
  103. });
  104. return res;
  105. });
  106. // 打开浏览器
  107. ipcMain.handle('open-browser', (event, url) => {
  108. shell.openExternal(url);
  109. });
  110. // 初始化electron-store相关API
  111. initialStorageEvents(ipcMain);
  112. app.on('ready', () => {
  113. createWindow();
  114. app.on('activate', function () {
  115. // On macOS it's common to re-create a window in the app when the
  116. // dock icon is clicked and there are no other windows open.
  117. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  118. });
  119. });
  120. app.on('window-all-closed', () => {
  121. if (process.platform !== 'darwin') {
  122. app.quit();
  123. }
  124. });
  125. initialWebsocketEvents(
  126. ipcMain,
  127. function onMessage(message) {
  128. socketSubjects.onMessage.notify(message);
  129. },
  130. function onError(...args) {
  131. socketSubjects.onError.notify(...args);
  132. },
  133. function onClose(...args) {
  134. socketSubjects.onClose.notify(...args);
  135. },
  136. );