Files
planet/client/src/main/recording-core.ts
T
linkong b3d2d9e9f1
ci / backend (push) Canceled after 0s
ci / frontend (push) Canceled after 0s
ci / delivery (push) Canceled after 0s
release: bump version to 0.81.0
2026-11-30 19:39:29 +08:00

330 lines
11 KiB
TypeScript

export type RecordingState = 'idle' | 'starting' | 'recording' | 'stopping'
export interface RecordingStatusSnapshot {
state: RecordingState
elapsedMs: number
includeAudio: boolean
hideCursor: boolean
filePath: string | null
}
export type RecordingCursorShape = 'default' | 'pointer' | 'grab' | 'grabbing' | 'text'
export interface RecordingCursorPayload {
visible: boolean
x: number
y: number
shape: RecordingCursorShape
}
export interface RectBounds {
x: number
y: number
width: number
height: number
}
const INDICATOR_BASE_WIDTH = 120
const INDICATOR_LONG_WIDTH = 136
const INDICATOR_HEIGHT = 36
/**
* Format elapsed recording duration as MM:SS (or HH:MM:SS once >= 1 hour).
*/
export function formatRecordingDuration(elapsedMs: number): string {
const safeSeconds = Number.isFinite(elapsedMs) && elapsedMs > 0 ? Math.floor(elapsedMs / 1000) : 0
const hours = Math.floor(safeSeconds / 3600)
const minutes = Math.floor((safeSeconds % 3600) / 60)
const seconds = safeSeconds % 60
const pad = (value: number) => String(value).padStart(2, '0')
return hours > 0 ? `${pad(hours)}:${pad(minutes)}:${pad(seconds)}` : `${pad(minutes)}:${pad(seconds)}`
}
/**
* Map a MediaRecorder MIME type to an output video container extension.
*/
export function resolveRecordingExtension(mimeType?: string): 'mp4' | 'webm' {
return typeof mimeType === 'string' && mimeType.toLowerCase().includes('mp4') ? 'mp4' : 'webm'
}
/**
* Build a collision-safe output filename for saved screen recordings.
*/
export function buildRecordingFilename(now: Date, ext: 'mp4' | 'webm', suffixIndex = 0): string {
const pad = (value: number) => String(value).padStart(2, '0')
const stamp = [
now.getFullYear(),
pad(now.getMonth() + 1),
pad(now.getDate()),
'-',
pad(now.getHours()),
pad(now.getMinutes()),
pad(now.getSeconds()),
].join('')
const suffix = suffixIndex > 0 ? `-${suffixIndex + 1}` : ''
return `Planet-Earth-${stamp}${suffix}.${ext}`
}
/**
* Return ordered MediaRecorder MIME candidates, preferring H.264 MP4 before WebM fallbacks.
*/
export function pickSupportedRecordingMimeTypes(includeAudio: boolean): string[] {
if (includeAudio) {
return [
'video/mp4;codecs=avc1.42E01E,mp4a.40.2',
'video/mp4;codecs=avc1.4d401f,mp4a.40.2',
'video/mp4;codecs=avc1.42E01E',
'video/mp4',
'video/webm;codecs=vp9,opus',
'video/webm;codecs=vp8,opus',
'video/webm;codecs=vp9',
'video/webm;codecs=vp8',
'video/webm',
]
}
return [
'video/mp4;codecs=avc1.42E01E',
'video/mp4;codecs=avc1.4d401f',
'video/mp4',
'video/webm;codecs=vp9',
'video/webm;codecs=vp8',
'video/webm',
]
}
/**
* Compute centered top-overlay indicator bounds anchored safely inside mainWindow content area.
*/
export function computeIndicatorBounds(options: {
contentBounds: RectBounds
elapsedMs: number
}): RectBounds {
const { contentBounds, elapsedMs } = options
const desiredWidth = elapsedMs >= 3_600_000 ? INDICATOR_LONG_WIDTH : INDICATOR_BASE_WIDTH
const width = Math.max(60, Math.min(desiredWidth, Math.max(60, contentBounds.width - 16)))
const height = Math.min(INDICATOR_HEIGHT, Math.max(24, contentBounds.height - 8))
const rawX = contentBounds.x + Math.round((contentBounds.width - width) / 2)
const topOffset = Math.min(56, Math.max(10, Math.round(contentBounds.height * 0.08)))
const rawY = contentBounds.y + topOffset
const maxX = contentBounds.x + Math.max(0, contentBounds.width - width)
const maxY = contentBounds.y + Math.max(0, contentBounds.height - height)
return {
x: Math.max(contentBounds.x, Math.min(rawX, maxX)),
y: Math.max(contentBounds.y, Math.min(rawY, maxY)),
width,
height,
}
}
/**
* Render self-contained HTML for the transparent floating recording indicator pill.
*/
export function buildRecordingIndicatorHtml(): string {
return `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<style>
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background: transparent;
overflow: hidden;
user-select: none;
-webkit-user-select: none;
display: flex;
align-items: center;
justify-content: center;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}
.rec-pill {
appearance: none;
height: 30px;
padding: 0 12px 0 10px;
border-radius: 999px;
border: 1px solid rgba(201, 225, 247, 0.22);
background: rgba(8, 16, 28, 0.8);
backdrop-filter: blur(14px) saturate(130%);
-webkit-backdrop-filter: blur(14px) saturate(130%);
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.42), inset 0 1px 0 rgba(255, 255, 255, 0.14);
color: #eef5ff;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
cursor: pointer;
outline: none;
transition: border-color 0.16s ease, background 0.16s ease, box-shadow 0.16s ease, transform 0.14s ease;
}
.rec-pill:hover {
border-color: rgba(248, 113, 113, 0.56);
background: rgba(24, 18, 28, 0.88);
box-shadow: 0 6px 20px rgba(239, 68, 68, 0.22), inset 0 1px 0 rgba(255, 255, 255, 0.18);
}
.rec-pill:active {
transform: scale(0.96);
}
.rec-glyph-wrap {
width: 12px;
height: 12px;
display: inline-flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.rec-glyph {
width: 9px;
height: 9px;
border-radius: 50%;
background: #ef4444;
box-shadow: 0 0 8px rgba(239, 68, 68, 0.72);
animation: recPulse 1.4s ease-in-out infinite;
transition: width 0.15s ease, height 0.15s ease, border-radius 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
}
.rec-pill:hover .rec-glyph {
width: 10px;
height: 10px;
border-radius: 2px;
background: #f87171;
box-shadow: 0 0 10px rgba(248, 113, 113, 0.8);
animation: none;
opacity: 1;
transform: none;
}
.rec-time {
font-size: 12px;
font-weight: 600;
line-height: 1;
letter-spacing: 0.03em;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
@keyframes recPulse {
0%, 100% {
opacity: 1;
transform: scale(1);
}
50% {
opacity: 0.32;
transform: scale(0.8);
}
}
</style>
</head>
<body>
<button id="rec-stop-btn" class="rec-pill" type="button" title="点击停止录屏 (Ctrl+Shift+F9)" aria-label="停止录屏">
<span class="rec-glyph-wrap" aria-hidden="true"><span class="rec-glyph"></span></span>
<span id="rec-timer" class="rec-time">00:00</span>
</button>
<script>
const btn = document.getElementById('rec-stop-btn');
const timerEl = document.getElementById('rec-timer');
if (window.planetRecordingIndicator?.onTick) {
window.planetRecordingIndicator.onTick((formatted) => {
if (typeof formatted === 'string' && timerEl) {
timerEl.textContent = formatted;
}
});
}
btn?.addEventListener('click', () => {
window.planetRecordingIndicator?.requestStop?.();
});
</script>
</body>
</html>`
}
/**
* Render self-contained HTML for the transparent click-through custom cursor overlay window.
*/
export function buildCursorOverlayHtml(): string {
return `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<style>
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background: transparent;
overflow: hidden;
pointer-events: none;
user-select: none;
-webkit-user-select: none;
}
#cursor-root {
position: fixed;
left: 0;
top: 0;
width: 24px;
height: 24px;
opacity: 0;
will-change: transform;
pointer-events: none;
}
.cursor-svg {
display: none;
width: 24px;
height: 24px;
filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.55));
}
#cursor-root[data-shape="default"] .cursor-svg--default,
#cursor-root[data-shape="pointer"] .cursor-svg--pointer,
#cursor-root[data-shape="grab"] .cursor-svg--grab,
#cursor-root[data-shape="grabbing"] .cursor-svg--grabbing,
#cursor-root[data-shape="text"] .cursor-svg--text {
display: block;
}
</style>
</head>
<body>
<div id="cursor-root" data-shape="default" aria-hidden="true">
<svg class="cursor-svg cursor-svg--default" viewBox="0 0 24 24" fill="none">
<path d="M2 1.5L2 19.5L6.8 14.9L10.3 21.8L13.1 20.4L9.7 13.6L16.3 13.6L2 1.5Z" fill="#ffffff" stroke="#0f172a" stroke-width="1.5" stroke-linejoin="round"/>
</svg>
<svg class="cursor-svg cursor-svg--pointer" viewBox="0 0 24 24" fill="none">
<path d="M8.5 1.8C9.6 1.8 10.5 2.7 10.5 3.8V10.1C11 9.6 11.7 9.4 12.4 9.7C13.1 9.9 13.6 10.5 13.7 11.1C14.2 10.7 15 10.5 15.7 10.8C16.4 11.1 16.9 11.8 17 12.6C17.5 12.3 18.2 12.3 18.8 12.6C19.5 12.9 20 13.6 20 14.5V16.8C20 19.7 17.8 22 14.9 22H11.5C9.6 22 7.9 21 6.9 19.4L4.7 15.7C4.2 14.9 4.5 13.8 5.3 13.4C6 13 6.9 13.2 7.4 13.9L6.5 15.3V3.8C6.5 2.7 7.4 1.8 8.5 1.8Z" fill="#ffffff" stroke="#0f172a" stroke-width="1.45" stroke-linejoin="round"/>
</svg>
<svg class="cursor-svg cursor-svg--grab" viewBox="0 0 24 24" fill="none">
<path d="M7 11V5.5C7 4.7 7.7 4 8.5 4C9.3 4 10 4.7 10 5.5V10.5M10 10V4.5C10 3.7 10.7 3 11.5 3C12.3 3 13 3.7 13 4.5V10.5M13 10.5V5.5C13 4.7 13.7 4 14.5 4C15.3 4 16 4.7 16 5.5V11.5M16 11.5V7.5C16 6.7 16.7 6 17.5 6C18.3 6 19 6.7 19 7.5V15C19 18.6 16.3 21 12.8 21H10.5C8.5 21 6.7 20 5.7 18.3L4.2 15.7C3.7 14.9 4 13.9 4.8 13.4C5.5 13 6.4 13.2 6.9 13.9L7 14V11Z" fill="#ffffff" stroke="#0f172a" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
<svg class="cursor-svg cursor-svg--grabbing" viewBox="0 0 24 24" fill="none">
<path d="M6.5 11.5C6.5 9.8 7.8 8.8 9.2 9.2V8.2C9.2 7 10.2 6.2 11.3 6.5C11.9 6.7 12.4 7.1 12.6 7.7C13.2 7 14.3 6.8 15.2 7.3C15.8 7.6 16.2 8.2 16.3 8.9C17.2 8.7 18.2 9.4 18.2 10.4V15C18.2 18.4 15.7 21 12.4 21H10.4C8.4 21 6.7 19.8 5.9 18L4.9 15.6C4.6 14.9 4.9 14 5.7 13.7L6.5 13.4V11.5Z" fill="#ffffff" stroke="#0f172a" stroke-width="1.45" stroke-linejoin="round"/>
</svg>
<svg class="cursor-svg cursor-svg--text" viewBox="0 0 24 24" fill="none">
<path d="M6 4H14M6 20H14M10 4V20" stroke="#0f172a" stroke-width="3.2" stroke-linecap="round"/>
<path d="M6 4H14M6 20H14M10 4V20" stroke="#ffffff" stroke-width="1.6" stroke-linecap="round"/>
</svg>
</div>
<script>
const root = document.getElementById('cursor-root');
const HOTSPOTS = {
default: [0, 0],
pointer: [-8, -2],
grab: [-10, -10],
grabbing: [-10, -10],
text: [-10, -12],
};
if (window.planetRecordingCursor?.onUpdate) {
window.planetRecordingCursor.onUpdate((payload) => {
if (!root || !payload) return;
if (!payload.visible) {
root.style.opacity = '0';
return;
}
const shape = HOTSPOTS[payload.shape] ? payload.shape : 'default';
const [ox, oy] = HOTSPOTS[shape];
root.dataset.shape = shape;
root.style.transform = 'translate3d(' + (Math.round(payload.x) + ox) + 'px, ' + (Math.round(payload.y) + oy) + 'px, 0)';
root.style.opacity = '1';
});
}
</script>
</body>
</html>`
}