11import type { BrowserWindow , WebContents } from "electron" ;
2- import { CAMERA_PREVIEW_SIZES } from "../src/lib/cameraPreview" ;
2+ import {
3+ CAMERA_PREVIEW_DEFAULT_ASPECT_RATIO ,
4+ CAMERA_PREVIEW_DEFAULT_SIZE ,
5+ CAMERA_PREVIEW_SIZES ,
6+ } from "../src/lib/cameraPreview" ;
37
48/** One stable window per HUD: hiding it must not change the native capture exclusion id. */
59export class CameraPreviewController {
610 private window : BrowserWindow | null = null ;
711 private owner : WebContents | null = null ;
812 private visible = false ;
13+ private width : number = CAMERA_PREVIEW_DEFAULT_SIZE ;
14+ private aspectRatio = CAMERA_PREVIEW_DEFAULT_ASPECT_RATIO ;
915 private ready = false ;
1016 private onOwnerDestroyed : ( ( ) => void ) | null = null ;
1117
1218 constructor (
1319 private readonly createWindow : ( ) => BrowserWindow ,
14- private readonly resizeWindow : ( window : BrowserWindow , diameter : number ) => void ,
20+ private readonly resizeWindow : (
21+ window : BrowserWindow ,
22+ width : number ,
23+ aspectRatio : number ,
24+ ) => void ,
1525 ) { }
1626
1727 configure ( owner : WebContents , enabled : boolean , visible : boolean ) : void {
@@ -39,6 +49,7 @@ export class CameraPreviewController {
3949 this . window . once ( "ready-to-show" , ( ) => {
4050 if ( this . window !== previewWindow ) return ;
4151 this . ready = true ;
52+ this . resizeWindow ( previewWindow , this . width , this . aspectRatio ) ;
4253 if ( this . visible ) this . window ?. showInactive ( ) ;
4354 } ) ;
4455 }
@@ -54,17 +65,18 @@ export class CameraPreviewController {
5465 this . owner ?. send ( "camera-preview:hidden" ) ;
5566 }
5667
57- resize ( sender : WebContents , diameter : unknown ) : void {
68+ resize ( sender : WebContents , width : unknown ) : void {
5869 if (
5970 sender !== this . window ?. webContents ||
60- typeof diameter !== "number" ||
61- ! CAMERA_PREVIEW_SIZES . some ( ( size ) => size === diameter )
71+ typeof width !== "number" ||
72+ ! CAMERA_PREVIEW_SIZES . some ( ( size ) => size === width )
6273 )
6374 return ;
64- this . resizeWindow ( this . window , diameter ) ;
75+ this . width = width ;
76+ this . resizeWindow ( this . window , this . width , this . aspectRatio ) ;
6577 }
6678
67- frame ( sender : WebContents , jpeg : unknown ) : void {
79+ frame ( sender : WebContents , jpeg : unknown , aspectRatio ?: unknown ) : void {
6880 if ( sender !== this . owner || ! this . visible || ! this . window ) return ;
6981 // A 320px JPEG is usually under 20KB. Never relay arbitrary URLs or unbounded IPC data.
7082 if (
@@ -73,6 +85,19 @@ export class CameraPreviewController {
7385 ! jpeg . startsWith ( "data:image/jpeg;base64," )
7486 )
7587 return ;
88+ if ( aspectRatio !== undefined ) {
89+ if (
90+ typeof aspectRatio !== "number" ||
91+ ! Number . isFinite ( aspectRatio ) ||
92+ aspectRatio < 1 / 8 ||
93+ aspectRatio > 8
94+ )
95+ return ;
96+ if ( this . aspectRatio !== aspectRatio ) {
97+ this . aspectRatio = aspectRatio ;
98+ this . resizeWindow ( this . window , this . width , aspectRatio ) ;
99+ }
100+ }
76101 this . window . webContents . send ( "camera-preview:frame" , jpeg ) ;
77102 }
78103
@@ -86,5 +111,7 @@ export class CameraPreviewController {
86111 this . window = null ;
87112 this . owner = null ;
88113 this . visible = false ;
114+ this . width = CAMERA_PREVIEW_DEFAULT_SIZE ;
115+ this . aspectRatio = CAMERA_PREVIEW_DEFAULT_ASPECT_RATIO ;
89116 }
90117}
0 commit comments