export type Rect = { x: number; y: number; width: number; height: number; }; export type DisplayInfo = { id: number; label: string; primary: boolean; internal: boolean; scaleFactor: number; rotation: number; bounds: Rect; workArea: Rect; simulated: boolean; }; export type Placement = { displayId: number; bounds: Rect; reason: string; }; export function intersectionArea(a: Rect, b: Rect): number { const left = Math.max(a.x, b.x); const top = Math.max(a.y, b.y); const right = Math.min(a.x + a.width, b.x + b.width); const bottom = Math.min(a.y + a.height, b.y + b.height); if (right <= left || bottom <= top) return 0; return (right - left) * (bottom - top); } function contains(rect: Rect, point: { x: number; y: number }): boolean { return ( point.x >= rect.x && point.x < rect.x + rect.width && point.y >= rect.y && point.y < rect.y + rect.height ); } function distanceSquared( a: { x: number; y: number }, b: { x: number; y: number }, ) { const dx = a.x - b.x; const dy = a.y - b.y; return dx * dx + dy * dy; } function center(rect: Rect): { x: number; y: number } { return { x: rect.x + rect.width / 2, y: rect.y + rect.height / 2 }; } export function pickDisplay( displays: readonly DisplayInfo[], rect: Rect, ): DisplayInfo | null { if (displays.length === 0) return null; let best: DisplayInfo | null = null; let bestScore = -1; for (const display of displays) { const score = intersectionArea(rect, display.bounds); if (score > bestScore) { best = display; bestScore = score; } } if (best && bestScore > 0) return best; const target = center(rect); let nearest: DisplayInfo | null = null; let nearestDistance = Infinity; for (const display of displays) { const distance = distanceSquared(target, center(display.bounds)); if (distance < nearestDistance) { nearest = display; nearestDistance = distance; } } return nearest; } export function pickDisplayAtPoint( displays: readonly DisplayInfo[], point: { x: number; y: number }, ): DisplayInfo | null { const exact = displays.find((display) => contains(display.bounds, point)); if (exact) return exact; return pickDisplay(displays, { x: point.x, y: point.y, width: 1, height: 1, }); } export function clampToWorkArea( rect: Rect, workArea: Rect, min: { width: number; height: number } = { width: 480, height: 360 }, ): Rect { const width = Math.min(Math.max(rect.width, min.width), workArea.width); const height = Math.min(Math.max(rect.height, min.height), workArea.height); const maxX = workArea.x + workArea.width - width; const maxY = workArea.y + workArea.height - height; return { x: Math.min(Math.max(rect.x, workArea.x), maxX), y: Math.min(Math.max(rect.y, workArea.y), maxY), width, height, }; } export function centerOn( workArea: Rect, size: { width: number; height: number }, min: { width: number; height: number } = { width: 480, height: 360 }, ): Rect { const fitted = clampToWorkArea( { x: workArea.x, y: workArea.y, width: size.width, height: size.height, }, workArea, min, ); return { ...fitted, x: workArea.x + Math.round((workArea.width - fitted.width) / 2), y: workArea.y + Math.round((workArea.height - fitted.height) / 2), }; } export function placeOnDisplay( display: DisplayInfo, size: { width: number; height: number }, margin = 0, ): Rect { const area = { x: display.workArea.x + margin, y: display.workArea.y + margin, width: Math.max(1, display.workArea.width - margin * 2), height: Math.max(1, display.workArea.height - margin * 2), }; return centerOn(area, size); } export function unionRect(rects: readonly Rect[]): Rect { if (rects.length === 0) return { x: 0, y: 0, width: 1, height: 1 }; const left = Math.min(...rects.map((rect) => rect.x)); const top = Math.min(...rects.map((rect) => rect.y)); const right = Math.max(...rects.map((rect) => rect.x + rect.width)); const bottom = Math.max(...rects.map((rect) => rect.y + rect.height)); return { x: left, y: top, width: Math.max(1, right - left), height: Math.max(1, bottom - top), }; } export function mapRect( rect: Rect, union: Rect, width: number, height: number, padding = 10, ): Rect { const scale = Math.min( (width - padding * 2) / Math.max(1, union.width), (height - padding * 2) / Math.max(1, union.height), ); return { x: padding + (rect.x - union.x) * scale, y: padding + (rect.y - union.y) * scale, width: Math.max(2, rect.width * scale), height: Math.max(2, rect.height * scale), }; } export function physicalSize( rect: Rect, scaleFactor: number, ): { width: number; height: number } { return { width: Math.round(rect.width * scaleFactor), height: Math.round(rect.height * scaleFactor), }; } export function simulatedDisplays(): DisplayInfo[] { return [ { id: 101, label: "左侧外接屏(模拟)", primary: false, internal: false, scaleFactor: 1, rotation: 0, bounds: { x: -1920, y: -180, width: 1920, height: 1080 }, workArea: { x: -1920, y: -150, width: 1920, height: 1000 }, simulated: true, }, { id: 102, label: "主屏(模拟,2x)", primary: true, internal: true, scaleFactor: 2, rotation: 0, bounds: { x: 0, y: 0, width: 1920, height: 1080 }, workArea: { x: 0, y: 30, width: 1920, height: 1000 }, simulated: true, }, { id: 103, label: "右侧竖屏(模拟,1.5x)", primary: false, internal: false, scaleFactor: 1.5, rotation: 90, bounds: { x: 1920, y: -180, width: 1080, height: 1920 }, workArea: { x: 1920, y: -150, width: 1080, height: 1840 }, simulated: true, }, ]; }