- 新增 electron/main.js: 主进程(窗口管理、系统托盘、原生菜单、IPC) - 新增 electron/preload.js: contextBridge 安全 API 暴露 - 新增 electron/desktop-bridge.js: 渲染进程桥接层 + Web 降级 - 新增 electron/desktop-integration.js: 菜单/托盘事件响应、原生文件对话框 - 更新 index.html: 集成桌面脚本 - 新增 package.json: Electron + electron-builder 配置 - 新增 DESKTOP_README.md: 桌面版开发文档 - 支持: NSIS 安装包 + 便携版、系统托盘、原生菜单、窗口记忆 - 安全: contextIsolation + IPC 白名单、单实例锁
55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
/**
|
|
* Metona Ollama Desktop - Preload 脚本
|
|
*
|
|
* 通过 contextBridge 安全暴露 IPC 接口给渲染进程
|
|
* 所有 API 都是白名单模式,不暴露 Node.js 全局对象
|
|
*/
|
|
|
|
const { contextBridge, ipcRenderer } = require('electron');
|
|
|
|
// ── 暴露 Desktop API ──
|
|
contextBridge.exposeInMainWorld('metonaDesktop', {
|
|
// 是否在桌面环境中
|
|
isDesktop: true,
|
|
|
|
// 版本信息
|
|
info: () => ipcRenderer.invoke('app:info'),
|
|
|
|
// ── 文件对话框 ──
|
|
dialog: {
|
|
openFile: (filters) => ipcRenderer.invoke('dialog:openFile', { filters }),
|
|
saveFile: (options) => ipcRenderer.invoke('dialog:saveFile', options)
|
|
},
|
|
|
|
// ── 文件系统 ──
|
|
fs: {
|
|
readFile: (filePath) => ipcRenderer.invoke('fs:readFile', filePath),
|
|
writeFile: (filePath, content) => ipcRenderer.invoke('fs:writeFile', filePath, content)
|
|
},
|
|
|
|
// ── 系统通知 ──
|
|
notify: (title, body) => ipcRenderer.invoke('notify', title, body),
|
|
|
|
// ── 窗口控制 ──
|
|
window: {
|
|
minimize: () => ipcRenderer.invoke('window:minimize'),
|
|
maximize: () => ipcRenderer.invoke('window:maximize'),
|
|
close: () => ipcRenderer.invoke('window:close')
|
|
},
|
|
|
|
// ── 外部链接 ──
|
|
openExternal: (url) => ipcRenderer.invoke('shell:openExternal', url),
|
|
|
|
// ── 主进程事件监听 ──
|
|
onMenuAction: (callback) => {
|
|
ipcRenderer.on('menu-action', (_, action) => callback(action));
|
|
},
|
|
onTrayAction: (callback) => {
|
|
ipcRenderer.on('tray-action', (_, action) => callback(action));
|
|
},
|
|
// 移除监听
|
|
removeAllListeners: (channel) => {
|
|
ipcRenderer.removeAllListeners(channel);
|
|
}
|
|
});
|