LOCKING盒子版
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

123 rader
3.3 KiB

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