189 lines
5.1 KiB
JavaScript
189 lines
5.1 KiB
JavaScript
const { app, BrowserWindow, screen, Tray, nativeImage, Menu, ipcMain, clipboard, dialog } = require('electron');
|
|
const path = require('node:path')
|
|
app.allowRendererProcessReuse = false;
|
|
let mainWindow = null;
|
|
let isMaximized = false;
|
|
|
|
class AppWindow extends BrowserWindow {
|
|
constructor(config) {
|
|
const basicConfig = {
|
|
webPreferences: {
|
|
contextIsolation: true,
|
|
nodeIntegration: true,
|
|
enableRemoteModule: true,
|
|
nodeIntegrationInWorker: true,
|
|
allowMediaDevices: true,
|
|
preload: path.join(__dirname, 'preload.js')
|
|
},
|
|
show: false,
|
|
frame: false,
|
|
icon: '',
|
|
icon: '',
|
|
backgroundColor: '#00000000',
|
|
transparent: true,
|
|
};
|
|
const finalConfig = { ...basicConfig, ...config };
|
|
super(finalConfig);
|
|
const env = process.argv.find((arg) => arg.startsWith('--env='))?.split('=')[1];
|
|
if (env) {
|
|
// 开发
|
|
this.loadURL('http://localhost:3000');
|
|
} else {
|
|
// 打包
|
|
this.loadFile(path.resolve(__dirname, './dist/index.html'));
|
|
}
|
|
this.once('ready-to-show', () => {
|
|
this.show();
|
|
});
|
|
}
|
|
}
|
|
|
|
function showWindow() {
|
|
// 如果主窗口已经存在但被最小化了,则恢复显示
|
|
if (mainWindow && mainWindow.isMinimized()) {
|
|
mainWindow.show();
|
|
}
|
|
// 如果主窗口已存在但不是焦点窗口,则将其置为焦点
|
|
if (mainWindow && !mainWindow.isFocused()) {
|
|
mainWindow.show();
|
|
mainWindow.focus();
|
|
}
|
|
// 如果主窗口还没有被创建,则创建它
|
|
if (!mainWindow) {
|
|
createWindow();
|
|
}
|
|
}
|
|
function createTray() {
|
|
const iconPath = `${__dirname}/src/assets/icon.png`;
|
|
const trayIcon = nativeImage.createFromPath(iconPath);
|
|
const tray = new Tray(trayIcon);
|
|
const contextMenu = Menu.buildFromTemplate([
|
|
{
|
|
label: '打开', click: () => {
|
|
showWindow()
|
|
},
|
|
icon: iconPath,
|
|
},
|
|
{
|
|
label: '退出', click: () => {
|
|
app.quit();
|
|
mainWindow = null;
|
|
},
|
|
icon: iconPath,
|
|
},
|
|
{
|
|
label: '退出到系统托盘', click: () => {
|
|
mainWindow.hide();
|
|
},
|
|
icon: iconPath,
|
|
},
|
|
]);
|
|
tray.setToolTip('智汇享');
|
|
tray.setContextMenu(contextMenu);
|
|
tray.on('click', () => {
|
|
if (mainWindow.isVisible()) {
|
|
mainWindow.hide()
|
|
} else {
|
|
mainWindow.show()
|
|
}
|
|
});
|
|
}
|
|
|
|
function createWindow() {
|
|
mainWindow = new AppWindow();
|
|
mainWindow.focus();
|
|
}
|
|
|
|
app.on('ready', () => {
|
|
createWindow()
|
|
createTray()
|
|
// 监听f12打开控制台
|
|
mainWindow.webContents.on('before-input-event', (event, input) => {
|
|
if (input.key === 'F12') {
|
|
mainWindow.webContents.openDevTools()
|
|
}
|
|
});
|
|
|
|
// 监听移动
|
|
mainWindow.on('move', () => {
|
|
// 如果是全屏自动恢复到上次窗口大小
|
|
if (isMaximized) {
|
|
mainWindow.setResizable(true)
|
|
mainWindow.unmaximize()
|
|
isMaximized = false;
|
|
}
|
|
if (mainWindow.isMaximized()) {
|
|
isMaximized = true;
|
|
}
|
|
});
|
|
// 放大缩小退出窗口
|
|
ipcMain.handle('setViewStatus', (event, status) => {
|
|
switch (status) {
|
|
case 'quit':
|
|
app.quit();
|
|
mainWindow = null;
|
|
break;
|
|
case 'maximize':
|
|
mainWindow.maximize()
|
|
mainWindow.setResizable(false)
|
|
break;
|
|
case 'unmaximize':
|
|
mainWindow.setResizable(true)
|
|
mainWindow.unmaximize()
|
|
break;
|
|
case 'minimize':
|
|
mainWindow.minimize()
|
|
break;
|
|
}
|
|
});
|
|
// 下载文件并放置选择的文件夹
|
|
ipcMain.handle('dwFile', (event, url) => {
|
|
// dialog.showOpenDialog(mainWindow, {
|
|
// properties: ['openDirectory']
|
|
// }).then(result => {
|
|
// if (!result.canceled) {
|
|
// const selectedPath = result.filePaths[0];
|
|
// win.webContents.on('will-download', (event, item, webContents) => {
|
|
// console.log('Selected download folder:', selectedPath);
|
|
// console.log(url);
|
|
// item.setSavePath(`${selectedPath}/${url}`);
|
|
// item.resume();
|
|
// });
|
|
// }
|
|
// }).catch(err => {
|
|
|
|
// });
|
|
});
|
|
// 导出是否全屏
|
|
ipcMain.handle('getIsMaximized', () => {
|
|
return mainWindow.isMaximized();
|
|
});
|
|
|
|
// 复制文字
|
|
ipcMain.handle('setWriteText', (event, text) => {
|
|
clipboard.writeText(text)
|
|
});
|
|
|
|
// 设置桌面应用基础属性
|
|
ipcMain.handle('setMainWindowSize', (event, config) => {
|
|
// 设置最小窗口尺寸
|
|
mainWindow.setMinimumSize(config.width, config.height);
|
|
// 设置最大尺寸
|
|
const primaryDisplay = screen.getPrimaryDisplay()
|
|
const { width, height } = primaryDisplay.workAreaSize
|
|
if (config.key === 'login') {
|
|
mainWindow.setMaximumSize(config.width, config.height);
|
|
} else {
|
|
mainWindow.setMaximumSize(width, height);
|
|
}
|
|
// 设置窗口尺寸
|
|
mainWindow.setSize(config.width, config.height)
|
|
// 设置窗口位置使其居中于当前屏幕
|
|
const display = screen.getDisplayMatching({ ...mainWindow.getBounds() });
|
|
const x = Math.round((display.workArea.width - mainWindow.getSize()[0]) / 2);
|
|
const y = Math.round((display.workArea.height - mainWindow.getSize()[1]) / 2);
|
|
mainWindow.setPosition(x, y);
|
|
});
|
|
});
|
|
|