adapte macos

This commit is contained in:
2026-08-08 22:45:23 +08:00
parent 0d7438e368
commit 1fa69aa055
46 changed files with 2301 additions and 933 deletions

View File

@@ -0,0 +1,53 @@
import { globalShortcut, type BrowserWindow } from 'electron'
import { sendPlayerControl } from './playerControl'
/**
* 媒体快捷键:
* - 播放/暂停:全局 Super+PmacOS ⌘P / Windows Win+P托盘后台也可用
* - 快退/快进:仅窗口聚焦时 Super+← / Super+→,避免全局抢占方向键
*
* Electron 的 Super = Windows 徽标键 / macOS Command。
* 注意Windows 系统常占用 Win+P投影注册失败时会打日志。
*/
const GLOBAL_PLAY_PAUSE = 'Super+P'
export function registerMediaShortcuts(): void {
try {
const ok = globalShortcut.register(GLOBAL_PLAY_PAUSE, () =>
sendPlayerControl('playpause')
)
if (!ok) {
console.warn(`[shortcuts] 注册失败(可能被系统占用): ${GLOBAL_PLAY_PAUSE}`)
}
} catch (e) {
console.warn(`[shortcuts] 注册异常: ${GLOBAL_PLAY_PAUSE}`, e)
}
}
export function unregisterMediaShortcuts(): void {
try {
globalShortcut.unregister(GLOBAL_PLAY_PAUSE)
} catch {
/* ignore */
}
}
/** 窗口聚焦时的快退 / 快进(不注册全局,避免影响其它应用) */
export function bindSeekShortcuts(window: BrowserWindow): void {
window.webContents.on('before-input-event', (event, input) => {
if (input.type !== 'keyDown') return
// meta = macOS Command / Windows 徽标键
if (!input.meta || input.control || input.alt) return
if (input.code === 'ArrowLeft') {
event.preventDefault()
sendPlayerControl('seek-backward')
return
}
if (input.code === 'ArrowRight') {
event.preventDefault()
sendPlayerControl('seek-forward')
}
})
}