feat(linux): 支持 Linux / Wayland 桌面运行
- 新增 src/main/platform.ts:Ozone 后端选择(Wayland 优先 XWayland)、
能力降级封装、XDG 数据目录、缺参 relaunch
- 主进程窗口定位/置顶/跨工作区/拖拽/视线跟随改为平台能力封装
- 托盘失败容错、Linux 窗口图标、pet:// 缺失文件返回 404
- 新增 scripts/linux/{install,run,uninstall}.sh 与启动器、desktop 菜单项、自启动
- 新增 assets/linux 应用图标;README 增加 Linux/Wayland 文档
- 清理仓库根目录误提交的 Chromium 日志文件
This commit is contained in:
+103
-53
@@ -1,4 +1,4 @@
|
||||
import {
|
||||
import {
|
||||
app,
|
||||
BrowserWindow,
|
||||
Tray,
|
||||
@@ -30,15 +30,34 @@ import { DESIGN_H, DESIGN_W, type PetConfig } from '../shared/petState'
|
||||
import { pickBark, pickClickBark, type BarkState } from '../shared/barks'
|
||||
import { ChatService } from './llm'
|
||||
import { chatEffect, suggestions } from '../shared/dialogue'
|
||||
import {
|
||||
bootstrapPlatform,
|
||||
capabilities,
|
||||
ensureOzoneBackend,
|
||||
defaultHomeCandidates,
|
||||
globalCursor,
|
||||
placeWindow,
|
||||
platformSummary,
|
||||
raiseWindow,
|
||||
showOnAllWorkspaces,
|
||||
IS_LINUX
|
||||
} from './platform'
|
||||
|
||||
// Linux 下需要在 app ready 前指定 Ozone 后端(Wayland 会话默认走 XWayland)
|
||||
bootstrapPlatform()
|
||||
// 启动参数里没带 --ozone-platform 时,用一次 relaunch 补上(否则原生 Wayland 会直接崩)
|
||||
ensureOzoneBackend()
|
||||
|
||||
// ---------------------------------------------------------------- 数据目录
|
||||
// 默认把「软件主体」放到 E:\SodiumPet,只有 Windows 临时/GPU 缓存留在 C 盘。
|
||||
// Windows:默认把「软件主体」放到 E:\SodiumPet,只有临时/GPU 缓存留在 C 盘。
|
||||
// Linux:放到 $XDG_DATA_HOME/SodiumPet(~/.local/share/SodiumPet)。
|
||||
// 沙箱里写不进去时,逐级回退,保证开发时也能跑。
|
||||
function resolveHome(): string {
|
||||
const candidates = [
|
||||
process.env.SODIUM_PET_HOME,
|
||||
'E:\\SodiumPet',
|
||||
path.dirname(app.getPath('exe')),
|
||||
...defaultHomeCandidates(),
|
||||
// Windows 上仍然保留「exe 同级目录」这个习惯
|
||||
...(IS_LINUX ? [] : [path.dirname(app.getPath('exe'))]),
|
||||
path.join(app.getPath('appData'), 'SodiumPet'),
|
||||
path.join(app.getAppPath(), '.devdata')
|
||||
].filter(Boolean) as string[]
|
||||
@@ -239,6 +258,9 @@ function registerProtocol(): void {
|
||||
return new Response('not found', { status: 404 })
|
||||
}
|
||||
if (!isInside(root, abs)) return new Response('forbidden', { status: 403 })
|
||||
// 文件不存在时直接返回 404:net.fetch(file://) 会对缺失文件抛 ERR_FILE_NOT_FOUND,
|
||||
// 日志里会刷一堆红色堆栈(比如首次启动还没有 data/catalog.json)
|
||||
if (!fs.existsSync(abs)) return new Response('not found', { status: 404 })
|
||||
|
||||
// 音频需要 Range 支持,才能拖动进度条 / 正常播放
|
||||
if (url.host === 'music' && fs.existsSync(abs) && fs.statSync(abs).isFile()) {
|
||||
@@ -331,10 +353,12 @@ function createWindow(): void {
|
||||
const { workArea } = screen.getPrimaryDisplay()
|
||||
const width = 190
|
||||
const height = 450
|
||||
const icon = appIcon()
|
||||
|
||||
mainWindow = new BrowserWindow({
|
||||
width,
|
||||
height,
|
||||
// 原生 Wayland 会忽略坐标,由合成器决定位置;这里只对 X11/XWayland 生效
|
||||
x: Math.round(workArea.x + workArea.width - width - 24),
|
||||
y: Math.round(workArea.y + workArea.height - height),
|
||||
show: false,
|
||||
@@ -348,6 +372,7 @@ function createWindow(): void {
|
||||
skipTaskbar: true,
|
||||
hasShadow: false,
|
||||
alwaysOnTop: true,
|
||||
...(IS_LINUX ? { icon } : {}),
|
||||
webPreferences: {
|
||||
preload: path.join(__dirname, '../preload/index.js'),
|
||||
contextIsolation: true,
|
||||
@@ -356,8 +381,10 @@ function createWindow(): void {
|
||||
}
|
||||
})
|
||||
|
||||
mainWindow.setAlwaysOnTop(true, 'screen-saver')
|
||||
mainWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true })
|
||||
raiseWindow(mainWindow, true)
|
||||
showOnAllWorkspaces(mainWindow)
|
||||
// 部分 X11 窗口管理器不会在创建时应用 skipTaskbar,显式再设一次(Linux 桌宠不该出现在任务栏)
|
||||
if (IS_LINUX) mainWindow.setSkipTaskbar(true)
|
||||
|
||||
mainWindow.loadFile(path.join(__dirname, '../renderer/index.html'))
|
||||
|
||||
@@ -365,19 +392,21 @@ function createWindow(): void {
|
||||
// 系统 DPI 缩放可能被带进页面,强制 1:1,避免内容被放大/裁切
|
||||
mainWindow?.webContents.setZoomFactor(1)
|
||||
mainWindow?.show()
|
||||
mainWindow?.setAlwaysOnTop(true, 'screen-saver')
|
||||
raiseWindow(mainWindow, true)
|
||||
})
|
||||
|
||||
mainWindow.on('show', () => {
|
||||
mainWindow?.setAlwaysOnTop(true, 'screen-saver')
|
||||
mainWindow?.moveTop()
|
||||
raiseWindow(mainWindow, true)
|
||||
if (capabilities.topmost) mainWindow?.moveTop()
|
||||
})
|
||||
|
||||
// 有些全屏程序会抢置顶,定期重新申明一次(很轻,不影响性能)
|
||||
setInterval(() => {
|
||||
if (!mainWindow?.isVisible()) return
|
||||
mainWindow.setAlwaysOnTop(true, 'screen-saver')
|
||||
}, 15000)
|
||||
if (capabilities.topmost) {
|
||||
setInterval(() => {
|
||||
if (!mainWindow?.isVisible()) return
|
||||
raiseWindow(mainWindow, true)
|
||||
}, 15000)
|
||||
}
|
||||
|
||||
// 调试:把窗口内容截一张(不受窗口是否被遮挡影响)
|
||||
if (process.env.PET_CAPTURE_ONCE) {
|
||||
@@ -669,15 +698,17 @@ function applyConfig(cfg: PetConfig): void {
|
||||
const height = Math.max(80, Math.round(DESIGN_H * cfg.scale - cfg.cropTop - cfg.cropBottom))
|
||||
const b = mainWindow.getBounds()
|
||||
// 右下角锚定,缩放/裁剪时不会跑位
|
||||
mainWindow.setBounds({
|
||||
placeWindow(mainWindow, {
|
||||
x: Math.round(b.x + (b.width - width)),
|
||||
y: Math.round(b.y + (b.height - height)),
|
||||
width,
|
||||
height
|
||||
})
|
||||
console.log('[config] bounds', mainWindow.getBounds(), 'dpr', screen.getPrimaryDisplay().scaleFactor)
|
||||
mainWindow.setAlwaysOnTop(cfg.topmost, 'screen-saver')
|
||||
mainWindow.setIgnoreMouseEvents(cfg.clickThrough, { forward: true })
|
||||
raiseWindow(mainWindow, cfg.topmost)
|
||||
if (capabilities.clickThrough) {
|
||||
mainWindow.setIgnoreMouseEvents(cfg.clickThrough, { forward: true })
|
||||
}
|
||||
for (const w of BrowserWindow.getAllWindows()) {
|
||||
if (!w.isDestroyed()) w.webContents.send('pet:config:update', cfg)
|
||||
}
|
||||
@@ -711,8 +742,8 @@ function createBubbleWindow(): void {
|
||||
sandbox: false
|
||||
}
|
||||
})
|
||||
bubbleWindow.setIgnoreMouseEvents(true, { forward: true })
|
||||
bubbleWindow.setAlwaysOnTop(true, 'screen-saver')
|
||||
if (capabilities.clickThrough) bubbleWindow.setIgnoreMouseEvents(true, { forward: true })
|
||||
raiseWindow(bubbleWindow, true)
|
||||
bubbleWindow.loadFile(path.join(__dirname, '../renderer/index.html'), {
|
||||
query: { view: 'bubble' }
|
||||
})
|
||||
@@ -740,7 +771,7 @@ function positionBubble(): void {
|
||||
if (!mainWindow || !bubbleWindow) return
|
||||
const b = mainWindow.getBounds()
|
||||
const h = bubbleWindow.getBounds().height
|
||||
bubbleWindow.setBounds({
|
||||
placeWindow(bubbleWindow, {
|
||||
x: Math.round(b.x + (b.width - 440) / 2),
|
||||
y: b.y - (h - 10),
|
||||
width: 440,
|
||||
@@ -750,7 +781,7 @@ function positionBubble(): void {
|
||||
|
||||
function say(text: string, duration = 6500): void {
|
||||
createBubbleWindow()
|
||||
bubbleWindow?.setBounds({ width: 440, height: 96 })
|
||||
if (bubbleWindow) placeWindow(bubbleWindow, { ...bubbleWindow.getBounds(), width: 440, height: 96 })
|
||||
positionBubble()
|
||||
const payload = { text, duration }
|
||||
currentBubble = { text, duration, at: Date.now() }
|
||||
@@ -780,7 +811,7 @@ function bubbleStreamStart(): void {
|
||||
createBubbleWindow()
|
||||
bubbleStreamLen = 0
|
||||
if (bubbleTimer) clearTimeout(bubbleTimer)
|
||||
bubbleWindow?.setBounds({ width: 440, height: 96 })
|
||||
if (bubbleWindow) placeWindow(bubbleWindow, { ...bubbleWindow.getBounds(), width: 440, height: 96 })
|
||||
positionBubble()
|
||||
bubbleWindow?.showInactive()
|
||||
sendBubbleStream('pet:bubble:stream:start')
|
||||
@@ -793,7 +824,7 @@ function bubbleStreamChunk(chunk: string): void {
|
||||
const h = Math.min(300, Math.max(96, 44 + lines * 26))
|
||||
const b = mainWindow?.getBounds()
|
||||
if (b) {
|
||||
bubbleWindow.setBounds({
|
||||
placeWindow(bubbleWindow, {
|
||||
x: Math.round(b.x + (b.width - 440) / 2),
|
||||
y: b.y - (h - 10),
|
||||
width: 440,
|
||||
@@ -1059,7 +1090,7 @@ function createMenuWindow(): void {
|
||||
sandbox: false
|
||||
}
|
||||
})
|
||||
menuWindow.setAlwaysOnTop(true, 'screen-saver')
|
||||
raiseWindow(menuWindow, true)
|
||||
menuWindow.loadFile(path.join(__dirname, '../renderer/index.html'), {
|
||||
query: { view: 'menu' }
|
||||
})
|
||||
@@ -1236,8 +1267,8 @@ function createLyricWindow(): void {
|
||||
sandbox: false
|
||||
}
|
||||
})
|
||||
lyricWindow.setAlwaysOnTop(true, 'screen-saver')
|
||||
lyricWindow.setIgnoreMouseEvents(false)
|
||||
raiseWindow(lyricWindow, true)
|
||||
if (capabilities.clickThrough) lyricWindow.setIgnoreMouseEvents(false)
|
||||
lyricWindow.loadFile(path.join(__dirname, '../renderer/index.html'), {
|
||||
query: { view: 'lyric' }
|
||||
})
|
||||
@@ -1476,7 +1507,7 @@ function currentFlavor(): string {
|
||||
function positionMenu(): void {
|
||||
if (!mainWindow || !menuWindow) return
|
||||
const b = mainWindow.getBounds()
|
||||
menuWindow.setBounds({
|
||||
placeWindow(menuWindow, {
|
||||
x: Math.round(b.x + b.width / 2 - 200),
|
||||
y: Math.round(b.y + b.height - 388),
|
||||
width: 400,
|
||||
@@ -1525,7 +1556,7 @@ function createChatBar(): void {
|
||||
sandbox: false
|
||||
}
|
||||
})
|
||||
chatBar.setAlwaysOnTop(true, 'screen-saver')
|
||||
raiseWindow(chatBar, true)
|
||||
chatBar.loadFile(path.join(__dirname, '../renderer/index.html'), {
|
||||
query: { view: 'chat' }
|
||||
})
|
||||
@@ -1548,7 +1579,7 @@ function positionChatBar(): void {
|
||||
if (!mainWindow || !chatBar) return
|
||||
const b = mainWindow.getBounds()
|
||||
const h = chatExpanded ? 274 : 54
|
||||
chatBar.setBounds({
|
||||
placeWindow(chatBar, {
|
||||
x: Math.round(b.x + b.width / 2 - 210),
|
||||
y: Math.max(0, b.y - 118) + 54 - h,
|
||||
width: 420,
|
||||
@@ -1592,24 +1623,34 @@ function createPanel(tab?: string, arg?: string): void {
|
||||
})
|
||||
|
||||
// 面板放在宠物左边,避免挡住她
|
||||
const saved = store.getConfig().panelPos
|
||||
if (saved) {
|
||||
panelWindow.setPosition(saved.x, saved.y)
|
||||
} else if (mainWindow) {
|
||||
const b = mainWindow.getBounds()
|
||||
panelWindow.setPosition(Math.max(20, b.x - 420), Math.max(20, b.y - 100))
|
||||
}
|
||||
if (capabilities.positionWindows) {
|
||||
const saved = store.getConfig().panelPos
|
||||
if (saved) {
|
||||
panelWindow.setPosition(saved.x, saved.y)
|
||||
} else if (mainWindow) {
|
||||
const b = mainWindow.getBounds()
|
||||
panelWindow.setPosition(Math.max(20, b.x - 420), Math.max(20, b.y - 100))
|
||||
}
|
||||
|
||||
panelWindow.on('moved', () => {
|
||||
if (!panelWindow) return
|
||||
const [x, y] = panelWindow.getPosition()
|
||||
store.setConfig({ panelPos: { x, y } })
|
||||
})
|
||||
// 原生 Wayland 下窗口位置由合成器决定,记录「移动」没有意义
|
||||
panelWindow.on('moved', () => {
|
||||
if (!panelWindow) return
|
||||
const [x, y] = panelWindow.getPosition()
|
||||
store.setConfig({ panelPos: { x, y } })
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 托盘
|
||||
function createTray(): void {
|
||||
tray = new Tray(buildTrayIcon())
|
||||
// Linux 下托盘依赖 StatusNotifierItem(KDE/GNOME 扩展),个别精简桌面可能不可用,
|
||||
// 失败时不要让整个应用起不来——右键菜单依然可以打开面板/退出。
|
||||
try {
|
||||
tray = new Tray(buildTrayIcon())
|
||||
} catch (err) {
|
||||
console.warn('[tray] 当前桌面不支持托盘,已跳过:', err)
|
||||
return
|
||||
}
|
||||
tray.setToolTip('小钠 · 桌宠')
|
||||
tray.setContextMenu(
|
||||
Menu.buildFromTemplate([
|
||||
@@ -2270,8 +2311,11 @@ let dragStartWin: [number, number] = [0, 0]
|
||||
|
||||
function startDrag(): void {
|
||||
if (!mainWindow) return
|
||||
// 原生 Wayland 下既读不到全局光标,也不能由客户端移动窗口,拖拽只能禁用
|
||||
if (!capabilities.positionWindows || !capabilities.cursor) return
|
||||
dragStartWin = mainWindow.getPosition() as [number, number]
|
||||
const p = screen.getCursorScreenPoint()
|
||||
const p = globalCursor()
|
||||
if (!p) return
|
||||
dragStartCursor = [p.x, p.y]
|
||||
if (dragTimer) clearInterval(dragTimer)
|
||||
|
||||
@@ -2280,7 +2324,8 @@ function startDrag(): void {
|
||||
let lastY = dragStartWin[1]
|
||||
dragTimer = setInterval(() => {
|
||||
if (!mainWindow) return
|
||||
const c = screen.getCursorScreenPoint()
|
||||
const c = globalCursor()
|
||||
if (!c) return
|
||||
const x = Math.round(dragStartWin[0] + (c.x - dragStartCursor[0]))
|
||||
const y = Math.round(dragStartWin[1] + (c.y - dragStartCursor[1]))
|
||||
if (x === lastX && y === lastY) return // 没动就不调用,减少无效 setPosition
|
||||
@@ -2308,6 +2353,7 @@ if (!gotLock) {
|
||||
app.on('second-instance', () => mainWindow?.show())
|
||||
|
||||
app.whenReady().then(() => {
|
||||
console.log('[pet] platform =', platformSummary())
|
||||
console.log('[pet] home =', HOME)
|
||||
registerProtocol()
|
||||
registerIpc()
|
||||
@@ -2335,16 +2381,20 @@ if (!gotLock) {
|
||||
}, 3000)
|
||||
|
||||
// 全屏视线跟随:主进程按 50ms 轮询光标位置,发给宠物窗口
|
||||
setInterval(() => {
|
||||
const cfgNow = store.getConfig()
|
||||
if (!cfgNow.eyeFollow || !mainWindow || !mainWindow.isVisible()) return
|
||||
const p = screen.getCursorScreenPoint()
|
||||
const b = mainWindow.getBounds()
|
||||
mainWindow.webContents.send('pet:look', {
|
||||
x: p.x - b.x + cfgNow.cropLeft,
|
||||
y: p.y - b.y + cfgNow.cropTop
|
||||
})
|
||||
}, 50)
|
||||
// 原生 Wayland 读不到全局光标,改为只跟随窗口内的鼠标(渲染层已处理)
|
||||
if (capabilities.cursor) {
|
||||
setInterval(() => {
|
||||
const cfgNow = store.getConfig()
|
||||
if (!cfgNow.eyeFollow || !mainWindow || !mainWindow.isVisible()) return
|
||||
const p = globalCursor()
|
||||
if (!p) return
|
||||
const b = mainWindow.getBounds()
|
||||
mainWindow.webContents.send('pet:look', {
|
||||
x: p.x - b.x + cfgNow.cropLeft,
|
||||
y: p.y - b.y + cfgNow.cropTop
|
||||
})
|
||||
}, 50)
|
||||
}
|
||||
|
||||
app.on('activate', () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) createWindow()
|
||||
|
||||
Reference in New Issue
Block a user