LOCKING盒子版
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.
 
 
 
 

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