123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import { BrowserWindow, screen } from "electron";
- import { join } from "node:path";
- import { ICON, DIST, preload, url } from "../main/utils";
- import log from "electron-log/main";
- const NODE_ENV = process.env.NODE_ENV;
- const indexHtml = join(DIST, "./index.html");
- let mainWindow: BrowserWindow | null;
- const extra = {
- activeClose: false,
- };
- const createWindow = (): BrowserWindow => {
-
- const { width } = screen.getPrimaryDisplay().workAreaSize;
-
- const winWidth = 420;
- const winHeight = 700;
-
- const x = width - winWidth;
- const y = 10;
-
- mainWindow = new BrowserWindow({
- title: "菁苗健康",
- icon: ICON,
- x,
- y,
- width: winWidth,
- height: winHeight,
- frame: false,
- show: false,
-
- backgroundColor: "#fff",
- webPreferences: {
- preload,
- webviewTag: true,
-
- },
- });
- mainWindow.setMenu(null);
-
-
-
- if (url) {
- mainWindow.loadURL(url);
- } else {
- mainWindow.loadFile(indexHtml);
- }
-
-
-
-
- mainWindow.once("ready-to-show", () => {
- log.info("启动参数process.argv==> hidden表示开机重启成功", process.argv);
- if (process.argv.indexOf("--hidden") < 0) {
- showMainWin();
- }
- });
-
- mainWindow.on("close", (e) => {
- if (!extra.activeClose) {
- e.preventDefault();
- hideMainWin();
- } else {
- extra.activeClose = true;
- }
- });
- return mainWindow;
- };
- function closeMainWin() {
- if (mainWindow?.isDestroyed()) {
- mainWindow!.close();
- }
- mainWindow = null;
- }
- function openMainWin() {
- mainWindow = createWindow();
- mainWindow!.show();
- }
- function showMainWin() {
- if (!mainWindow || mainWindow?.isDestroyed()) {
- mainWindow = createWindow();
- }
- mainWindow!.show();
- }
- function hideMainWin() {
- mainWindow!.hide();
-
- }
- function minimizeMainWin() {
- mainWindow!.minimize();
- }
- function maximizeMainWin() {
- mainWindow!.maximize();
- }
- function middleMainWin() {
- const { width } = screen.getPrimaryDisplay().workAreaSize;
- const currentWidth = mainWindow?.getBounds().width;
- if (currentWidth === 840) {
- mainWindow?.setBounds({
- width: 420,
- x: width - 420,
- });
- return;
- }
- mainWindow?.setBounds({
- width: 840,
- x: width - 840,
- });
- }
- function unmaximizeMainWin() {
- mainWindow!.unmaximize();
- }
- function isMaximized() {
- return mainWindow!.isMaximized();
- }
- function focusMainWin() {
- if (mainWindow) {
-
- if (mainWindow.isMinimized()) mainWindow.restore();
- mainWindow.focus();
- }
- }
- export {
- mainWindow,
- extra,
- createWindow,
- closeMainWin,
- openMainWin,
- hideMainWin,
- showMainWin,
- focusMainWin,
- minimizeMainWin,
- isMaximized,
- maximizeMainWin,
- unmaximizeMainWin,
- middleMainWin,
- };
|