mainWin.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { BrowserWindow, screen } from "electron";
  2. import { join } from "node:path";
  3. import { ICON, DIST, preload, url } from "../main/utils";
  4. import log from "electron-log/main";
  5. const NODE_ENV = process.env.NODE_ENV;
  6. const indexHtml = join(DIST, "./index.html");
  7. let mainWindow: BrowserWindow | null;
  8. const extra = {
  9. activeClose: false,
  10. };
  11. // 主窗口
  12. const createWindow = (): BrowserWindow => {
  13. //动态适应宽高
  14. const { width } = screen.getPrimaryDisplay().workAreaSize;
  15. // 设置窗口的宽度和高度
  16. const winWidth = 420;
  17. const winHeight = 700;
  18. // 计算窗口在右上角的位置
  19. const x = width - winWidth;
  20. const y = 10;
  21. // 创建浏览器窗口
  22. mainWindow = new BrowserWindow({
  23. title: "菁苗健康",
  24. icon: ICON,
  25. x,
  26. y,
  27. width: winWidth,
  28. height: winHeight,
  29. frame: false,
  30. show: false,
  31. // transparent: true,
  32. alwaysOnTop: true, // 始终在顶部
  33. backgroundColor: "#fff",
  34. webPreferences: {
  35. preload,
  36. webviewTag: true, // 启用webview标签
  37. // webSecurity: false,
  38. },
  39. });
  40. mainWindow.setMenu(null); // 去掉顶部菜单
  41. // log.info("Log from the main process");
  42. // const args = process.argv.slice(2); // 去掉前两个默认参数(node 和 script 路径)
  43. // console.log("args==>", args); // 输出实际传递的参数
  44. if (url) {
  45. mainWindow.loadURL(url);
  46. } else {
  47. mainWindow.loadFile(indexHtml);
  48. }
  49. // mainWindow.webContents.openDevTools();
  50. // mainWindow.on("closed", () => {
  51. // mainWindow = null;
  52. // });
  53. mainWindow.once("ready-to-show", () => {
  54. log.info("启动参数process.argv==> hidden表示开机重启成功", process.argv);
  55. if (process.argv.indexOf("--hidden") < 0) {
  56. showMainWin();
  57. }
  58. });
  59. // 监听窗口的关闭事件
  60. mainWindow.on("close", (e) => {
  61. if (!extra.activeClose) {
  62. e.preventDefault();
  63. hideMainWin();
  64. } else {
  65. extra.activeClose = true;
  66. }
  67. });
  68. return mainWindow;
  69. };
  70. // 关闭窗口
  71. function closeMainWin() {
  72. if (mainWindow?.isDestroyed()) {
  73. mainWindow!.close();
  74. }
  75. mainWindow = null;
  76. }
  77. // 打开窗口
  78. function openMainWin() {
  79. mainWindow = createWindow();
  80. mainWindow!.show();
  81. }
  82. // 显示窗口
  83. function showMainWin() {
  84. if (!mainWindow || mainWindow?.isDestroyed()) {
  85. mainWindow = createWindow();
  86. }
  87. mainWindow!.show();
  88. }
  89. // 隐藏窗口
  90. function hideMainWin() {
  91. mainWindow!.hide();
  92. // app.quit();
  93. }
  94. // 最小化窗口
  95. function minimizeMainWin() {
  96. mainWindow!.minimize();
  97. }
  98. // 最大化窗口
  99. function maximizeMainWin() {
  100. mainWindow!.maximize();
  101. }
  102. function middleMainWin() {
  103. const { width } = screen.getPrimaryDisplay().workAreaSize;
  104. const currentWidth = mainWindow?.getBounds().width;
  105. if (currentWidth && currentWidth > 420) {
  106. mainWindow?.setBounds({
  107. width: 420,
  108. x: width - 420,
  109. });
  110. return;
  111. }
  112. mainWindow?.setBounds({
  113. width: 840,
  114. x: width - 840,
  115. });
  116. }
  117. // 取消最大化窗口
  118. function unmaximizeMainWin() {
  119. mainWindow!.unmaximize();
  120. }
  121. // 是否最大化
  122. function isMaximized() {
  123. return mainWindow!.isMaximized();
  124. }
  125. function focusMainWin() {
  126. if (mainWindow) {
  127. // Focus on the main window if the user tried to open another
  128. if (mainWindow.isMinimized()) mainWindow.restore();
  129. mainWindow.focus();
  130. }
  131. }
  132. export {
  133. mainWindow,
  134. extra,
  135. createWindow,
  136. closeMainWin,
  137. openMainWin,
  138. hideMainWin,
  139. showMainWin,
  140. focusMainWin,
  141. minimizeMainWin,
  142. isMaximized,
  143. maximizeMainWin,
  144. unmaximizeMainWin,
  145. middleMainWin,
  146. };