mirror of
https://github.com/xiaoyiweb/YiAi.git
synced 2025-11-13 20:53:47 +08:00
初始化
This commit is contained in:
63
chat/electron/appMenu.js
Normal file
63
chat/electron/appMenu.js
Normal file
@@ -0,0 +1,63 @@
|
||||
// appMenu.js
|
||||
const { Menu, app, Tray } = require('electron');
|
||||
const path = require('path')
|
||||
|
||||
function configureAppMenu(mainWindow) {
|
||||
|
||||
let tray = new Tray(path.join(__dirname, '../icons/16x16.png'));
|
||||
// tray.setToolTip('Nine Ai');
|
||||
|
||||
const template = [
|
||||
{
|
||||
label: 'NineAi',
|
||||
submenu: [
|
||||
{
|
||||
label: '退出应用',
|
||||
accelerator: 'CmdOrCtrl+Q',
|
||||
click: () => {
|
||||
app.quit();
|
||||
},
|
||||
}
|
||||
],
|
||||
}
|
||||
];
|
||||
|
||||
if (process.platform === 'darwin') {
|
||||
template.unshift({
|
||||
label: 'Edit',
|
||||
submenu: [
|
||||
{ role: 'undo' },
|
||||
{ role: 'redo' },
|
||||
{ type: 'separator' },
|
||||
{ role: 'cut' },
|
||||
{ role: 'copy' },
|
||||
{ role: 'paste' },
|
||||
{ role: 'pasteandmatchstyle' },
|
||||
{ role: 'delete' },
|
||||
{ role: 'selectall' }
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
const menu = Menu.buildFromTemplate(template);
|
||||
Menu.setApplicationMenu(menu);
|
||||
|
||||
tray.on('click', () => {
|
||||
mainWindow.isVisible() ? mainWindow.hide() : mainWindow.show();
|
||||
});
|
||||
|
||||
tray.on('right-click', () => {
|
||||
const contextMenuWindows = Menu.buildFromTemplate([
|
||||
{
|
||||
label: '退出应用',
|
||||
click: () => {
|
||||
app.quit();
|
||||
},
|
||||
},
|
||||
]);
|
||||
tray.popUpContextMenu(contextMenuWindows);
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
module.exports = { configureAppMenu };
|
||||
40
chat/electron/ipcManager.js
Normal file
40
chat/electron/ipcManager.js
Normal file
@@ -0,0 +1,40 @@
|
||||
// ipcManager.js
|
||||
const { ipcMain, BrowserWindow, app } = require('electron');
|
||||
|
||||
function handleIpc(mainWindow) {
|
||||
ipcMain.handle('minimizeWindow', () => {
|
||||
const mainWindow = BrowserWindow.getFocusedWindow();
|
||||
mainWindow?.minimize();
|
||||
});
|
||||
|
||||
ipcMain.handle('maxmizeWindow', () => {
|
||||
const win = BrowserWindow.getFocusedWindow();
|
||||
if (win) {
|
||||
if (process.platform === 'darwin') mainWindow?.setFullScreen(true);
|
||||
else mainWindow?.maximize();
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('closeWindow', () => {
|
||||
// const mainWindow = BrowserWindow.getFocusedWindow();
|
||||
// mainWindow?.close();
|
||||
app.quit()
|
||||
});
|
||||
|
||||
ipcMain.handle('unmaximizeWindow', () => {
|
||||
const win = BrowserWindow.getFocusedWindow();
|
||||
if (win) {
|
||||
if (process.platform === 'darwin') mainWindow?.setFullScreen(false);
|
||||
else mainWindow?.unmaximize();
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.on('check-window-maximized', (event) => {
|
||||
const win = BrowserWindow.getFocusedWindow();
|
||||
if (win) {
|
||||
event.reply('window-maximized-status', win.isFullScreen());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { handleIpc };
|
||||
28
chat/electron/main.js
Normal file
28
chat/electron/main.js
Normal file
@@ -0,0 +1,28 @@
|
||||
// main.js
|
||||
const { app, BrowserWindow } = require('electron');
|
||||
const { createMainWindow } = require('./windowManager');
|
||||
const { handleIpc } = require('./ipcManager');
|
||||
const { registerShortcuts } = require('./shortcutManager');
|
||||
const { configureAppMenu } = require('./appMenu');
|
||||
|
||||
app.commandLine.appendSwitch('--ignore-certificate-errors', 'true');
|
||||
app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors');
|
||||
|
||||
app.whenReady().then(() => {
|
||||
const mainWindow = createMainWindow();
|
||||
handleIpc(mainWindow);
|
||||
registerShortcuts(mainWindow);
|
||||
configureAppMenu(mainWindow);
|
||||
});
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
createMainWindow();
|
||||
}
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') {
|
||||
app.quit();
|
||||
}
|
||||
});
|
||||
5
chat/electron/menu/index.js
Normal file
5
chat/electron/menu/index.js
Normal file
@@ -0,0 +1,5 @@
|
||||
if(process.platform === 'darwin') {
|
||||
require('./darwin.js')
|
||||
}else if( process.platform === "win32") {
|
||||
require('./win32.js')
|
||||
}
|
||||
19
chat/electron/shortcutManager.js
Normal file
19
chat/electron/shortcutManager.js
Normal file
@@ -0,0 +1,19 @@
|
||||
// shortcutManager.js
|
||||
const { globalShortcut, BrowserWindow } = require('electron');
|
||||
|
||||
let isWindowVisible = true;
|
||||
|
||||
function registerShortcuts(mainWindow) {
|
||||
globalShortcut.register('Ctrl+L', () => {
|
||||
if (mainWindow && !mainWindow.isFullScreen()) {
|
||||
if (isWindowVisible) {
|
||||
mainWindow.hide();
|
||||
} else {
|
||||
mainWindow.show();
|
||||
}
|
||||
isWindowVisible = !isWindowVisible;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { registerShortcuts };
|
||||
51
chat/electron/windowManager.js
Normal file
51
chat/electron/windowManager.js
Normal file
@@ -0,0 +1,51 @@
|
||||
// windowManager.js
|
||||
const { BrowserWindow, globalShortcut, clipboard, app } = require('electron');
|
||||
const { handleIpc } = require('./ipcManager');
|
||||
|
||||
let mainWindow = null;
|
||||
let isWindowVisible = true;
|
||||
let lastClipboardContent = '';
|
||||
|
||||
function createMainWindow() {
|
||||
mainWindow = new BrowserWindow({
|
||||
width: 1300,
|
||||
height: 860,
|
||||
minWidth: 1300,
|
||||
minHeight: 820,
|
||||
center: true,
|
||||
frame: false,
|
||||
show: true,
|
||||
webPreferences: {
|
||||
nodeIntegration: true,
|
||||
contextIsolation: false,
|
||||
nodeIntegrationInWorker: true,
|
||||
webSecurity: true,
|
||||
allowRunningInsecureContent: false,
|
||||
},
|
||||
})
|
||||
|
||||
if (app.isPackaged) {
|
||||
// mainWindow.loadFile(filePath)
|
||||
mainWindow.loadURL('https://ai.jiangly.com')
|
||||
}
|
||||
else {
|
||||
mainWindow.loadURL('http://127.0.0.1:1002')
|
||||
// mainWindow.loadURL('https://ai.jiangly.com')
|
||||
mainWindow.webContents.openDevTools()
|
||||
}
|
||||
|
||||
mainWindow.on('show', () => {
|
||||
const clipboardContent = clipboard.readText();
|
||||
if (clipboardContent === lastClipboardContent) return;
|
||||
mainWindow.webContents.send('clipboard-content', clipboardContent);
|
||||
lastClipboardContent = clipboardContent;
|
||||
});
|
||||
|
||||
globalShortcut.register('CommandOrControl+Shift+i', () => {
|
||||
mainWindow.webContents.openDevTools();
|
||||
});
|
||||
|
||||
return mainWindow;
|
||||
}
|
||||
|
||||
module.exports = { createMainWindow };
|
||||
Reference in New Issue
Block a user