LOCKING盒子版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

101 行
2.6 KiB

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