Skip to content

Create polished demo videos without editing skills. Mac/Windows/Linux

test(windows): cover WGC display selection

webadderall3074ba7445b9
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index 6969c5d2..5e0527ae 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -12,6 +12,7 @@ import { app, BrowserWindow, desktopCapturer, dialog, ipcMain, shell, systemPref
import { hideCursor, showCursor } from '../cursorHider'
import { RECORDINGS_DIR } from '../main'
import { closeCountdownWindow, createCountdownWindow, getCountdownWindow } from '../windows'
+import { resolveWindowsCaptureDisplay } from './windowsCaptureSelection'
const execFileAsync = promisify(execFile)
const nodeRequire = createRequire(import.meta.url)
@@ -1678,15 +1679,11 @@ function waitForFfmpegCaptureStop(process: ChildProcessWithoutNullStreams, outpu
}
function getDisplayBoundsForSource(source: SelectedSource) {
- const sourceDisplayId = Number(source?.display_id)
- if (Number.isFinite(sourceDisplayId)) {
- const matched = getScreen().getAllDisplays().find((display) => display.id === sourceDisplayId)
- if (matched) {
- return matched.bounds
- }
- }
-
- return getScreen().getPrimaryDisplay().bounds
+ return resolveWindowsCaptureDisplay(
+ source,
+ getScreen().getAllDisplays(),
+ getScreen().getPrimaryDisplay(),
+ ).bounds
}
function parseXwininfoBounds(stdout: string): WindowBounds | null {
@@ -3182,23 +3179,19 @@ body{background:transparent;overflow:hidden;width:100vw;height:100vh}
if (windowId && source?.id?.startsWith('window:')) {
config.windowHandle = windowId
} else {
- const screenId = Number(source?.display_id)
- config.displayId = Number.isFinite(screenId) && screenId > 0
- ? screenId
- : Number(getScreen().getPrimaryDisplay().id)
+ const resolvedDisplay = resolveWindowsCaptureDisplay(
+ source,
+ getScreen().getAllDisplays(),
+ getScreen().getPrimaryDisplay(),
+ )
+ config.displayId = resolvedDisplay.displayId
// Monitor handle IDs can drift across Electron/Windows capture boundaries,
// so also provide display bounds for a coordinate-based native fallback.
- const allDisplays = getScreen().getAllDisplays()
- const display = allDisplays.find((candidate) => String(candidate.id) === String(config.displayId))
- ?? getScreen().getPrimaryDisplay()
-
- if (display) {
- config.displayX = Math.round(display.bounds.x)
- config.displayY = Math.round(display.bounds.y)
- config.displayW = Math.round(display.bounds.width)
- config.displayH = Math.round(display.bounds.height)
- }
+ config.displayX = Math.round(resolvedDisplay.bounds.x)
+ config.displayY = Math.round(resolvedDisplay.bounds.y)
+ config.displayW = Math.round(resolvedDisplay.bounds.width)
+ config.displayH = Math.round(resolvedDisplay.bounds.height)
}
recordNativeCaptureDiagnostics({
diff --git a/electron/ipc/windowsCaptureSelection.test.ts b/electron/ipc/windowsCaptureSelection.test.ts
new file mode 100644
index 00000000..870ebbd2
--- /dev/null
+++ b/electron/ipc/windowsCaptureSelection.test.ts
@@ -0,0 +1,60 @@
+import { describe, expect, it } from 'vitest'
+
+import { resolveWindowsCaptureDisplay } from './windowsCaptureSelection'
+
+describe('resolveWindowsCaptureDisplay', () => {
+ const primaryDisplay = {
+ id: 101,
+ bounds: {
+ x: 0,
+ y: 0,
+ width: 1920,
+ height: 1080,
+ },
+ }
+
+ const secondaryDisplay = {
+ id: 202,
+ bounds: {
+ x: 1920,
+ y: -40,
+ width: 2560,
+ height: 1440,
+ },
+ }
+
+ it('uses the requested secondary display bounds for WGC fallback metadata', () => {
+ const resolved = resolveWindowsCaptureDisplay(
+ { display_id: String(secondaryDisplay.id) },
+ [primaryDisplay, secondaryDisplay],
+ primaryDisplay,
+ )
+
+ expect(resolved).toEqual({
+ displayId: secondaryDisplay.id,
+ bounds: secondaryDisplay.bounds,
+ })
+ })
+
+ it('falls back to the primary display when the source has no display id', () => {
+ const resolved = resolveWindowsCaptureDisplay(undefined, [primaryDisplay, secondaryDisplay], primaryDisplay)
+
+ expect(resolved).toEqual({
+ displayId: primaryDisplay.id,
+ bounds: primaryDisplay.bounds,
+ })
+ })
+
+ it('keeps the requested display id even if Electron cannot rematch it, while using primary bounds', () => {
+ const resolved = resolveWindowsCaptureDisplay(
+ { display_id: '303' },
+ [primaryDisplay, secondaryDisplay],
+ primaryDisplay,
+ )
+
+ expect(resolved).toEqual({
+ displayId: 303,
+ bounds: primaryDisplay.bounds,
+ })
+ })
+})
\ No newline at end of file
diff --git a/electron/ipc/windowsCaptureSelection.ts b/electron/ipc/windowsCaptureSelection.ts
new file mode 100644
index 00000000..ee7ad1c7
--- /dev/null
+++ b/electron/ipc/windowsCaptureSelection.ts
@@ -0,0 +1,40 @@
+export type WindowsCaptureSourceLike = {
+ display_id?: string
+}
+
+export type WindowsCaptureDisplayBounds = {
+ x: number
+ y: number
+ width: number
+ height: number
+}
+
+export type WindowsCaptureDisplayLike = {
+ id: number
+ bounds: WindowsCaptureDisplayBounds
+}
+
+export type ResolvedWindowsCaptureDisplay = {
+ displayId: number
+ bounds: WindowsCaptureDisplayBounds
+}
+
+export function resolveWindowsCaptureDisplay(
+ source: WindowsCaptureSourceLike | null | undefined,
+ allDisplays: WindowsCaptureDisplayLike[],
+ primaryDisplay: WindowsCaptureDisplayLike,
+): ResolvedWindowsCaptureDisplay {
+ const requestedDisplayId = Number(source?.display_id)
+ const primaryDisplayId = Number(primaryDisplay.id)
+ const displayId = Number.isFinite(requestedDisplayId) && requestedDisplayId > 0
+ ? requestedDisplayId
+ : primaryDisplayId
+
+ const matchedDisplay = allDisplays.find((display) => String(display.id) === String(displayId))
+ ?? primaryDisplay
+
+ return {
+ displayId,
+ bounds: matchedDisplay.bounds,
+ }
+}
\ No newline at end of file
diff --git a/vitest.config.ts b/vitest.config.ts
index 273845ca..674c89e3 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -5,7 +5,10 @@ export default defineConfig({
test: {
globals: true,
environment: 'node',
- include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
+ include: [
+ 'src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}',
+ 'electron/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}',
+ ],
},
resolve: {
alias: {