feat(desktop): add computeWebviewBounds helper

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-05-29 11:36:39 +08:00
parent 78565a7478
commit 53724a6652
2 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,14 @@
import { describe, expect, it } from 'vitest'
import { computeWebviewBounds } from './computeWebviewBounds'
describe('computeWebviewBounds', () => {
it('maps a DOMRect to logical bounds (rounded)', () => {
const rect = { left: 100.4, top: 50.6, width: 800.2, height: 600.9 } as DOMRect
expect(computeWebviewBounds(rect)).toEqual({ x: 100, y: 51, width: 800, height: 601 })
})
it('clamps negative/zero sizes to 0', () => {
const rect = { left: -5, top: -5, width: -10, height: 0 } as DOMRect
expect(computeWebviewBounds(rect)).toEqual({ x: -5, y: -5, width: 0, height: 0 })
})
})

View File

@ -0,0 +1,10 @@
export type WebviewBounds = { x: number; y: number; width: number; height: number }
export function computeWebviewBounds(rect: Pick<DOMRect, 'left' | 'top' | 'width' | 'height'>): WebviewBounds {
return {
x: Math.round(rect.left),
y: Math.round(rect.top),
width: Math.max(0, Math.round(rect.width)),
height: Math.max(0, Math.round(rect.height)),
}
}