Files
planet/client/src/main/recording-indicator.ts
T
linkong 04cebdc504
ci / backend (push) Waiting to run
ci / frontend (push) Waiting to run
release: bump version to 0.82.0
2026-12-01 18:43:57 +08:00

217 lines
6.5 KiB
TypeScript

import { BrowserWindow, screen, type Display } from 'electron'
import { getClientLocale } from './i18n'
import { buildRecordingIndicatorHtml, computeIndicatorBounds, formatRecordingDuration } from './recording-core'
export interface RecordingIndicatorController {
show: (elapsedMs?: number) => void
updateElapsed: (elapsedMs: number) => void
hide: () => void
destroy: () => void
}
export function createRecordingIndicatorController(options: {
getMainWindow: () => BrowserWindow | null
preloadPath: string
}): RecordingIndicatorController {
let indicatorWin: BrowserWindow | null = null
let boundMainWin: BrowserWindow | null = null
let blurHideTimer: ReturnType<typeof setTimeout> | null = null
let visible = false
let lastElapsedMs = 0
let destroyed = false
let readyToShow = false
const clearBlurHideTimer = () => {
if (blurHideTimer) {
clearTimeout(blurHideTimer)
blurHideTimer = null
}
}
const syncBoundsAndLevel = () => {
const mainWin = options.getMainWindow()
if (
destroyed ||
!visible ||
!indicatorWin ||
indicatorWin.isDestroyed() ||
!mainWin ||
mainWin.isDestroyed()
) {
return
}
if (mainWin.isMinimized() || !mainWin.isVisible() || !mainWin.isFocused()) {
if (indicatorWin.isVisible()) {
indicatorWin.hide()
}
return
}
const contentBounds = mainWin.getContentBounds()
const nextBounds = computeIndicatorBounds({ contentBounds, elapsedMs: lastElapsedMs })
indicatorWin.setBounds(nextBounds)
indicatorWin.setAlwaysOnTop(true, mainWin.isFullScreen() ? 'screen-saver' : 'pop-up-menu')
if (readyToShow && !indicatorWin.isVisible()) {
indicatorWin.showInactive()
}
}
const handleMainFocus = () => {
clearBlurHideTimer()
syncBoundsAndLevel()
}
const handleMainBlur = () => {
clearBlurHideTimer()
blurHideTimer = setTimeout(() => {
blurHideTimer = null
const mainWin = options.getMainWindow()
if (!mainWin || mainWin.isDestroyed() || !mainWin.isFocused()) {
if (indicatorWin && !indicatorWin.isDestroyed() && indicatorWin.isVisible()) {
indicatorWin.hide()
}
}
}, 60)
}
const pushTick = (elapsedMs: number) => {
if (!indicatorWin || indicatorWin.isDestroyed() || indicatorWin.webContents.isDestroyed()) return
indicatorWin.webContents.send('planet:recording-indicator-tick', formatRecordingDuration(elapsedMs))
}
const unbindMainWindowListeners = () => {
clearBlurHideTimer()
if (!boundMainWin || boundMainWin.isDestroyed()) {
boundMainWin = null
return
}
boundMainWin.removeListener('move', syncBoundsAndLevel)
boundMainWin.removeListener('resize', syncBoundsAndLevel)
boundMainWin.removeListener('maximize', syncBoundsAndLevel)
boundMainWin.removeListener('unmaximize', syncBoundsAndLevel)
boundMainWin.removeListener('minimize', syncBoundsAndLevel)
boundMainWin.removeListener('restore', syncBoundsAndLevel)
boundMainWin.removeListener('enter-full-screen', syncBoundsAndLevel)
boundMainWin.removeListener('leave-full-screen', syncBoundsAndLevel)
boundMainWin.removeListener('show', syncBoundsAndLevel)
boundMainWin.removeListener('hide', syncBoundsAndLevel)
boundMainWin.removeListener('focus', handleMainFocus)
boundMainWin.removeListener('blur', handleMainBlur)
boundMainWin.removeListener('closed', hide)
boundMainWin = null
}
const bindMainWindowListeners = (mainWin: BrowserWindow) => {
if (boundMainWin === mainWin) return
unbindMainWindowListeners()
boundMainWin = mainWin
mainWin.on('move', syncBoundsAndLevel)
mainWin.on('resize', syncBoundsAndLevel)
mainWin.on('maximize', syncBoundsAndLevel)
mainWin.on('unmaximize', syncBoundsAndLevel)
mainWin.on('minimize', syncBoundsAndLevel)
mainWin.on('restore', syncBoundsAndLevel)
mainWin.on('enter-full-screen', syncBoundsAndLevel)
mainWin.on('leave-full-screen', syncBoundsAndLevel)
mainWin.on('show', syncBoundsAndLevel)
mainWin.on('hide', syncBoundsAndLevel)
mainWin.on('focus', handleMainFocus)
mainWin.on('blur', handleMainBlur)
mainWin.on('closed', hide)
}
const ensureIndicatorWindow = (mainWin: BrowserWindow): BrowserWindow => {
if (indicatorWin && !indicatorWin.isDestroyed()) {
return indicatorWin
}
readyToShow = false
const initialBounds = computeIndicatorBounds({
contentBounds: mainWin.getContentBounds(),
elapsedMs: lastElapsedMs,
})
indicatorWin = new BrowserWindow({
...initialBounds,
transparent: true,
backgroundColor: '#00000000',
frame: false,
thickFrame: false,
roundedCorners: false,
resizable: false,
movable: false,
minimizable: false,
maximizable: false,
closable: false,
focusable: false,
skipTaskbar: true,
hasShadow: false,
show: false,
webPreferences: {
preload: options.preloadPath,
contextIsolation: true,
nodeIntegration: false,
sandbox: true,
},
})
indicatorWin.setMenuBarVisibility(false)
indicatorWin.webContents.once('did-finish-load', () => {
if (!indicatorWin || indicatorWin.isDestroyed()) return
void indicatorWin.webContents.setVisualZoomLevelLimits(1, 1)
readyToShow = true
pushTick(lastElapsedMs)
if (visible) {
syncBoundsAndLevel()
}
})
void indicatorWin.loadURL(
`data:text/html;charset=utf-8,${encodeURIComponent(buildRecordingIndicatorHtml(getClientLocale()))}`,
)
return indicatorWin
}
const show = (elapsedMs = 0) => {
if (destroyed) return
const mainWin = options.getMainWindow()
if (!mainWin || mainWin.isDestroyed()) return
clearBlurHideTimer()
visible = true
lastElapsedMs = Math.max(0, elapsedMs)
bindMainWindowListeners(mainWin)
ensureIndicatorWindow(mainWin)
pushTick(lastElapsedMs)
syncBoundsAndLevel()
}
const updateElapsed = (elapsedMs: number) => {
lastElapsedMs = Math.max(0, elapsedMs)
if (!visible) return
pushTick(lastElapsedMs)
syncBoundsAndLevel()
}
function hide() {
clearBlurHideTimer()
visible = false
if (indicatorWin && !indicatorWin.isDestroyed() && indicatorWin.isVisible()) {
indicatorWin.hide()
}
}
const destroy = () => {
destroyed = true
visible = false
unbindMainWindowListeners()
if (indicatorWin && !indicatorWin.isDestroyed()) {
indicatorWin.destroy()
}
indicatorWin = null
}
return {
show,
updateElapsed,
hide,
destroy,
}
}