Compare commits
2 Commits
292d8c27f2
...
54f4b46755
| Author | SHA1 | Date | |
|---|---|---|---|
| 54f4b46755 | |||
| 5843d3f8ca |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
node_modules/
|
||||
dist/
|
||||
|
||||
46
README.md
46
README.md
@@ -2,6 +2,16 @@
|
||||
|
||||
Electron desktop shell for Fabric.Frontend.
|
||||
|
||||
## 功能(当前)
|
||||
- BrowserWindow 基础配置(尺寸/最小尺寸/标题)
|
||||
- Dev/Prod 加载策略(dev server / 本地 offline.html)
|
||||
- 基础菜单与快捷键(刷新、开发者工具、退出)
|
||||
- 安全基线:`contextIsolation` + `sandbox` + 禁止任意新窗口/导航
|
||||
- `preload + IPC` 白名单:
|
||||
- `fabric:config:get`
|
||||
- `fabric:config:set`
|
||||
- `fabric:notify`
|
||||
|
||||
## Dev
|
||||
|
||||
```bash
|
||||
@@ -16,3 +26,39 @@ npm run start:dev
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
|
||||
## Build / Release
|
||||
|
||||
先安装依赖(包含 `electron-builder`):
|
||||
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
仅打包目录(不生成安装包):
|
||||
|
||||
```bash
|
||||
npm run pack
|
||||
```
|
||||
|
||||
跨平台构建入口:
|
||||
|
||||
```bash
|
||||
npm run dist
|
||||
```
|
||||
|
||||
按平台构建:
|
||||
|
||||
```bash
|
||||
npm run dist:linux
|
||||
npm run dist:mac
|
||||
npm run dist:win
|
||||
```
|
||||
|
||||
构建产物输出到:
|
||||
|
||||
- `dist/`
|
||||
|
||||
产物命名规范:
|
||||
|
||||
- `Fabric-Desktop-${version}-${os}-${arch}.${ext}`
|
||||
|
||||
166
main.js
166
main.js
@@ -1,25 +1,179 @@
|
||||
const { app, BrowserWindow } = require('electron')
|
||||
const { app, BrowserWindow, Menu, Tray, ipcMain, Notification, nativeImage, shell } = require('electron')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
|
||||
const isDev = !!process.env.FABRIC_DESKTOP_URL
|
||||
const DEFAULT_DEV_URL = 'http://localhost:5173'
|
||||
const DEFAULT_PROD_ENTRY = path.join(__dirname, 'offline.html')
|
||||
|
||||
let mainWindow = null
|
||||
let tray = null
|
||||
let isQuitting = false
|
||||
|
||||
function configPath() {
|
||||
return path.join(app.getPath('userData'), 'fabric-desktop.config.json')
|
||||
}
|
||||
|
||||
function readConfig() {
|
||||
try {
|
||||
const raw = fs.readFileSync(configPath(), 'utf8')
|
||||
return JSON.parse(raw)
|
||||
} catch {
|
||||
return {
|
||||
centerApiBase: 'http://localhost:7001/api',
|
||||
guildApiBase: 'http://localhost:7002/api',
|
||||
guildSocketBase: 'http://localhost:7002/realtime',
|
||||
apiKey: '',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function writeConfig(next) {
|
||||
fs.mkdirSync(path.dirname(configPath()), { recursive: true })
|
||||
fs.writeFileSync(configPath(), JSON.stringify(next, null, 2), 'utf8')
|
||||
return next
|
||||
}
|
||||
|
||||
function createMenu() {
|
||||
const template = [
|
||||
{
|
||||
label: 'Fabric',
|
||||
submenu: [
|
||||
{ role: 'reload', accelerator: 'CmdOrCtrl+R' },
|
||||
{ role: 'toggleDevTools', accelerator: 'CmdOrCtrl+Shift+I' },
|
||||
{ type: 'separator' },
|
||||
{ role: 'quit', accelerator: 'CmdOrCtrl+Q' },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: 'Edit',
|
||||
submenu: [{ role: 'undo' }, { role: 'redo' }, { type: 'separator' }, { role: 'copy' }, { role: 'paste' }],
|
||||
},
|
||||
]
|
||||
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
|
||||
}
|
||||
|
||||
function createWindow() {
|
||||
const win = new BrowserWindow({
|
||||
width: 1280,
|
||||
height: 840,
|
||||
minWidth: 1024,
|
||||
minHeight: 700,
|
||||
title: 'Fabric Desktop',
|
||||
autoHideMenuBar: false,
|
||||
webPreferences: {
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false,
|
||||
sandbox: true,
|
||||
preload: path.join(__dirname, 'preload.js'),
|
||||
},
|
||||
})
|
||||
|
||||
const url = process.env.FABRIC_DESKTOP_URL || 'file://' + __dirname + '/offline.html'
|
||||
win.loadURL(url)
|
||||
win.webContents.setWindowOpenHandler(() => ({ action: 'deny' }))
|
||||
win.webContents.on('will-navigate', (event, url) => {
|
||||
const allowedDev = isDev && url.startsWith((process.env.FABRIC_DESKTOP_URL || DEFAULT_DEV_URL))
|
||||
const allowedFile = url.startsWith('file://')
|
||||
if (!allowedDev && !allowedFile) {
|
||||
event.preventDefault()
|
||||
shell.openExternal(url)
|
||||
}
|
||||
})
|
||||
|
||||
const devUrl = process.env.FABRIC_DESKTOP_URL || DEFAULT_DEV_URL
|
||||
if (isDev) {
|
||||
win.loadURL(devUrl)
|
||||
} else {
|
||||
win.loadFile(DEFAULT_PROD_ENTRY)
|
||||
}
|
||||
|
||||
win.on('close', (event) => {
|
||||
if (!isQuitting) {
|
||||
event.preventDefault()
|
||||
win.hide()
|
||||
}
|
||||
})
|
||||
|
||||
return win
|
||||
}
|
||||
|
||||
function createTrayIcon() {
|
||||
if (tray) return tray
|
||||
|
||||
const iconDataUrl =
|
||||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAfElEQVR4AWNw4PjPwMDA8J+BgYHhP4MDA8N/BgYGhv8MDAwM/2dgYGB4R2JgYGB4w8DAwPCfgYGB4R8DAwPDf4aGhob/BgwMDAz/Z2BgYHhHYmBgYHjDwMDA8J+BgYHhHwMDA8N/hoYGhv8GDAwMDP9nYGBgeEdiYGAAAB7RImfVq6X8AAAAAElFTkSuQmCC'
|
||||
const icon = nativeImage.createFromDataURL(iconDataUrl)
|
||||
|
||||
tray = new Tray(icon)
|
||||
tray.setToolTip('Fabric Desktop')
|
||||
|
||||
const buildTrayMenu = () =>
|
||||
Menu.buildFromTemplate([
|
||||
{
|
||||
label: mainWindow?.isVisible() ? '隐藏窗口' : '显示窗口',
|
||||
click: () => {
|
||||
if (!mainWindow) {
|
||||
mainWindow = createWindow()
|
||||
return
|
||||
}
|
||||
if (mainWindow.isVisible()) mainWindow.hide()
|
||||
else {
|
||||
mainWindow.show()
|
||||
mainWindow.focus()
|
||||
}
|
||||
tray.setContextMenu(buildTrayMenu())
|
||||
},
|
||||
},
|
||||
{
|
||||
label: '退出',
|
||||
click: () => {
|
||||
isQuitting = true
|
||||
app.quit()
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
tray.setContextMenu(buildTrayMenu())
|
||||
|
||||
tray.on('double-click', () => {
|
||||
if (!mainWindow) {
|
||||
mainWindow = createWindow()
|
||||
return
|
||||
}
|
||||
mainWindow.show()
|
||||
mainWindow.focus()
|
||||
tray.setContextMenu(buildTrayMenu())
|
||||
})
|
||||
|
||||
return tray
|
||||
}
|
||||
|
||||
app.whenReady().then(() => {
|
||||
createWindow()
|
||||
createMenu()
|
||||
mainWindow = createWindow()
|
||||
createTrayIcon()
|
||||
|
||||
ipcMain.handle('fabric:config:get', () => readConfig())
|
||||
ipcMain.handle('fabric:config:set', (_evt, next) => writeConfig(next || {}))
|
||||
ipcMain.handle('fabric:notify', (_evt, payload) => {
|
||||
const title = payload?.title || 'Fabric'
|
||||
const body = payload?.body || ''
|
||||
if (Notification.isSupported()) {
|
||||
new Notification({ title, body }).show()
|
||||
return { ok: true }
|
||||
}
|
||||
return { ok: false }
|
||||
})
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
||||
if (BrowserWindow.getAllWindows().length === 0) mainWindow = createWindow()
|
||||
else if (mainWindow && !mainWindow.isVisible()) mainWindow.show()
|
||||
})
|
||||
|
||||
app.on('before-quit', () => {
|
||||
isQuitting = true
|
||||
})
|
||||
})
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') app.quit()
|
||||
// 保持后台驻留,由托盘控制退出
|
||||
})
|
||||
|
||||
3239
package-lock.json
generated
3239
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
53
package.json
53
package.json
@@ -2,12 +2,61 @@
|
||||
"name": "fabric-desktop",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"description": "Electron desktop shell for Fabric frontend.",
|
||||
"homepage": "https://github.com/hangman0414/Fabric",
|
||||
"author": {
|
||||
"name": "Hangman",
|
||||
"email": "hangman@example.com"
|
||||
},
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"start": "electron .",
|
||||
"start:dev": "FABRIC_DESKTOP_URL=http://localhost:5173 electron ."
|
||||
"start:dev": "FABRIC_DESKTOP_URL=http://localhost:5173 electron .",
|
||||
"pack": "electron-builder --dir",
|
||||
"dist": "electron-builder",
|
||||
"dist:linux": "electron-builder --linux AppImage deb tar.gz",
|
||||
"dist:mac": "electron-builder --mac dmg zip",
|
||||
"dist:win": "electron-builder --win nsis zip"
|
||||
},
|
||||
"devDependencies": {
|
||||
"electron": "^37.2.1"
|
||||
"electron": "^37.2.1",
|
||||
"electron-builder": "^24.13.3"
|
||||
},
|
||||
"build": {
|
||||
"appId": "ai.hangman.fabric.desktop",
|
||||
"productName": "Fabric Desktop",
|
||||
"artifactName": "Fabric-Desktop-${version}-${os}-${arch}.${ext}",
|
||||
"directories": {
|
||||
"output": "dist"
|
||||
},
|
||||
"files": [
|
||||
"main.js",
|
||||
"preload.js",
|
||||
"offline.html",
|
||||
"package.json"
|
||||
],
|
||||
"asar": true,
|
||||
"linux": {
|
||||
"target": [
|
||||
"AppImage",
|
||||
"deb",
|
||||
"tar.gz"
|
||||
],
|
||||
"category": "Utility",
|
||||
"maintainer": "Hangman <hangman@example.com>"
|
||||
},
|
||||
"mac": {
|
||||
"target": [
|
||||
"dmg",
|
||||
"zip"
|
||||
],
|
||||
"category": "public.app-category.productivity"
|
||||
},
|
||||
"win": {
|
||||
"target": [
|
||||
"nsis",
|
||||
"zip"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
7
preload.js
Normal file
7
preload.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const { contextBridge, ipcRenderer } = require('electron')
|
||||
|
||||
contextBridge.exposeInMainWorld('fabricDesktop', {
|
||||
getConfig: () => ipcRenderer.invoke('fabric:config:get'),
|
||||
setConfig: (next) => ipcRenderer.invoke('fabric:config:set', next),
|
||||
notify: (title, body) => ipcRenderer.invoke('fabric:notify', { title, body }),
|
||||
})
|
||||
Reference in New Issue
Block a user