- 新增 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 日志文件
164 lines
7.2 KiB
TypeScript
164 lines
7.2 KiB
TypeScript
/**
|
||
* 桌面平台适配层(Linux / Wayland 支持)
|
||
*
|
||
* 这个项目最初只面向 Windows。要在 Linux(尤其是 Wayland 会话)上运行,
|
||
* 需要处理几处平台差异:
|
||
*
|
||
* 1. **窗口后端**:Wayland 原生协议(xdg-toplevel)不允许客户端自己决定窗口位置,
|
||
* 也没有「始终置顶 / 鼠标穿透 / 读取全局光标」的通用协议——而桌宠恰恰全都依赖它们。
|
||
* 因此默认在 Wayland 会话里通过 XWayland(`--ozone-platform=x11`)运行:
|
||
* 窗口位置、置顶、穿透、拖拽、视线跟随都能正常工作,并仍然是同一个 Wayland 桌面。
|
||
* 也可以用 `SODIUM_PET_OZONE=wayland` 强制走原生 Wayland(功能会受限,见 README)。
|
||
* 2. **数据目录**:Windows 习惯把数据放在 exe 旁边(E:\SodiumPet),Linux 遵循
|
||
* XDG 规范放到 `$XDG_DATA_HOME/SodiumPet`。
|
||
* 3. **能力降级**:原生 Wayland 下 setPosition/setAlwaysOnTop/getCursorScreenPoint
|
||
* 都会被合成器忽略,所以要提前判断并跳过这些调用,避免窗口「看起来没反应」。
|
||
*/
|
||
import { app, screen, type BrowserWindow } from 'electron'
|
||
import * as os from 'node:os'
|
||
import * as path from 'node:path'
|
||
|
||
export type OzoneMode = 'x11' | 'wayland' | ''
|
||
|
||
export const IS_LINUX = process.platform === 'linux'
|
||
export const IS_WINDOWS = process.platform === 'win32'
|
||
export const IS_MAC = process.platform === 'darwin'
|
||
|
||
const sessionType = (process.env.XDG_SESSION_TYPE || '').toLowerCase()
|
||
const hasX11 = Boolean(process.env.DISPLAY)
|
||
const hasWayland = Boolean(process.env.WAYLAND_DISPLAY)
|
||
|
||
/** 当前是否运行在 Wayland 会话(无论后端是 XWayland 还是原生 Wayland) */
|
||
export const IS_WAYLAND_SESSION = IS_LINUX && (sessionType === 'wayland' || hasWayland)
|
||
|
||
/** 实际使用的 Chromium Ozone 后端 */
|
||
export const OZONE_MODE: OzoneMode = decideOzoneMode()
|
||
|
||
function decideOzoneMode(): OzoneMode {
|
||
if (!IS_LINUX) return ''
|
||
const forced = (process.env.SODIUM_PET_OZONE || 'auto').toLowerCase()
|
||
if (forced === 'x11') return hasX11 ? 'x11' : hasWayland ? 'wayland' : 'x11'
|
||
if (forced === 'wayland') return hasWayland ? 'wayland' : hasX11 ? 'x11' : 'wayland'
|
||
// auto:Wayland 会话下优先 XWayland(功能完整);没有 XWayland 才用原生 Wayland
|
||
if (IS_WAYLAND_SESSION) return hasX11 ? 'x11' : 'wayland'
|
||
return hasX11 ? 'x11' : ''
|
||
}
|
||
|
||
/** 原生 Wayland 下很多窗口操作是空操作,这里集中声明能力,便于上层降级 */
|
||
export const capabilities = {
|
||
/** 能否由程序指定窗口坐标(原生 Wayland:否) */
|
||
positionWindows: !IS_LINUX || OZONE_MODE !== 'wayland',
|
||
/** 能否置顶 / 跨虚拟桌面(原生 Wayland:否) */
|
||
topmost: !IS_LINUX || OZONE_MODE !== 'wayland',
|
||
/** 能否读取全局鼠标坐标(原生 Wayland:否) */
|
||
cursor: !IS_LINUX || OZONE_MODE !== 'wayland',
|
||
/** 能否设置鼠标穿透(原生 Wayland:否) */
|
||
clickThrough: !IS_LINUX || OZONE_MODE !== 'wayland'
|
||
}
|
||
|
||
/**
|
||
* 在 app ready 之前调用:设置应用名、Ozone 后端等启动参数。
|
||
* 注意必须早于 app.whenReady(),Chromium 在初始化 UI 平台时会读取这些开关。
|
||
*/
|
||
export function bootstrapPlatform(): void {
|
||
if (!IS_LINUX) return
|
||
// WM_CLASS / 托盘 / 通知里显示的名字
|
||
app.setName('SodiumPet')
|
||
// 注意:Ozone 后端在 JS 运行之前就已确定,appendSwitch('ozone-platform') 是无效的,
|
||
// 必须保证启动参数里带上它 —— 见 ensureOzoneBackend()。
|
||
// 部分 X11 合成器(含 XWayland)需要显式声明才能拿到 ARGB 视觉,否则透明窗口会黑底
|
||
if (OZONE_MODE === 'x11' && !app.commandLine.hasSwitch('enable-transparent-visuals')) {
|
||
app.commandLine.appendSwitch('enable-transparent-visuals')
|
||
}
|
||
// 软件渲染兜底:虚拟机 / 无 GPU 环境下可通过环境变量关闭硬件加速
|
||
if (process.env.SODIUM_PET_DISABLE_GPU === '1') app.disableHardwareAcceleration()
|
||
if (process.env.SODIUM_PET_GPU_COMPOSITING === 'off') {
|
||
app.disableHardwareAcceleration()
|
||
app.commandLine.appendSwitch('disable-gpu-compositing')
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 确保当前进程使用了正确的 Ozone 后端。
|
||
*
|
||
* Chromium 在选择 Wayland/X11 后端时早于主进程 JS,`app.commandLine.appendSwitch`
|
||
* 对它无效,所以启动参数里没有 `--ozone-platform` 时,用一次极短的 relaunch 补上。
|
||
* 正常安装后由启动脚本直接带参数,不会触发这里。
|
||
*/
|
||
export function ensureOzoneBackend(): void {
|
||
if (!IS_LINUX || !OZONE_MODE) return
|
||
if (app.commandLine.hasSwitch('ozone-platform')) return
|
||
console.log(`[platform] 缺少 --ozone-platform,重启为 ${OZONE_MODE} 后端`)
|
||
app.relaunch({ args: [...process.argv.slice(1), `--ozone-platform=${OZONE_MODE}`] })
|
||
app.exit(0)
|
||
}
|
||
|
||
/** 各平台默认的数据目录候选(第一个可写的会被使用) */
|
||
export function defaultHomeCandidates(): string[] {
|
||
if (IS_WINDOWS) {
|
||
return ['E:\\SodiumPet']
|
||
}
|
||
if (IS_MAC) {
|
||
return [path.join(os.homedir(), 'Library', 'Application Support', 'SodiumPet')]
|
||
}
|
||
// Linux:遵循 XDG 规范,避免往 /opt、/usr 这类只读目录里写数据
|
||
const dataHome = process.env.XDG_DATA_HOME || path.join(os.homedir(), '.local', 'share')
|
||
return [path.join(dataHome, 'SodiumPet')]
|
||
}
|
||
|
||
/** 把窗口放到指定位置;原生 Wayland 下只调整大小(合成器决定位置) */
|
||
export function placeWindow(
|
||
win: BrowserWindow | null,
|
||
bounds: { x: number; y: number; width: number; height: number }
|
||
): void {
|
||
if (!win || win.isDestroyed()) return
|
||
if (capabilities.positionWindows) {
|
||
win.setBounds(bounds)
|
||
} else {
|
||
win.setSize(Math.max(1, Math.round(bounds.width)), Math.max(1, Math.round(bounds.height)))
|
||
}
|
||
}
|
||
|
||
/** 置顶 / 取消置顶;原生 Wayland 下没有该能力,直接跳过 */
|
||
export function raiseWindow(win: BrowserWindow | null, onTop: boolean): void {
|
||
if (!win || win.isDestroyed()) return
|
||
if (!capabilities.topmost) return
|
||
win.setAlwaysOnTop(onTop, 'screen-saver')
|
||
}
|
||
|
||
/** 在所有虚拟桌面 / 全屏窗口上方可见 */
|
||
export function showOnAllWorkspaces(win: BrowserWindow | null): void {
|
||
if (!win || win.isDestroyed()) return
|
||
if (!capabilities.topmost) return
|
||
win.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true })
|
||
}
|
||
|
||
/** 读取全局鼠标位置;原生 Wayland 下返回 null */
|
||
export function globalCursor(): { x: number; y: number } | null {
|
||
if (!capabilities.cursor) return null
|
||
try {
|
||
return screen.getCursorScreenPoint()
|
||
} catch {
|
||
return null
|
||
}
|
||
}
|
||
|
||
/** 主窗口右下角默认位置(按工作区计算,多显示器也适用) */
|
||
export function bottomRightPosition(width: number, height: number, margin = 24): {
|
||
x: number
|
||
y: number
|
||
} {
|
||
const { workArea } = screen.getPrimaryDisplay()
|
||
return {
|
||
x: Math.round(workArea.x + workArea.width - width - margin),
|
||
y: Math.round(workArea.y + workArea.height - height)
|
||
}
|
||
}
|
||
|
||
/** 当前平台的一句话描述,写进日志方便排查 */
|
||
export function platformSummary(): string {
|
||
if (!IS_LINUX) return `${process.platform} (${process.arch})`
|
||
const backend = OZONE_MODE === 'wayland' ? 'wayland(原生)' : 'x11(XWayland)'
|
||
return `linux (${process.arch}) session=${sessionType || 'unknown'} backend=${backend}`
|
||
}
|