@@ -69,14 +69,27 @@ async Task UpdateBackground()
6969 }
7070
7171 IReadOnlyReactiveProperty < IWebcamCapture > _webcamCapture ;
72+ bool _setupComplete ;
7273
7374 void SetupPreview ( )
7475 {
76+ if ( _setupComplete )
77+ return ;
78+
79+ _setupComplete = true ;
80+
7581 IsVisibleChanged += ( S , E ) =>
7682 {
7783 if ( IsVisible && _webcamCapture == null )
7884 {
79- InitializeWebcam ( ) ;
85+ _webcamCapture = _webcamModel . InitCapture ( ) ;
86+
87+ if ( _webcamCapture . Value is { } capture )
88+ {
89+ _reactor . WebcamSize . OnNext ( new WSize ( capture . Width , capture . Height ) ) ;
90+
91+ UpdateWebcamPreview ( ) ;
92+ }
8093 }
8194 else if ( ! IsVisible && _webcamCapture != null )
8295 {
@@ -97,16 +110,7 @@ void OnRegionChange()
97110
98111 _webcamModel
99112 . ObserveProperty ( M => M . SelectedCam )
100- . Subscribe ( M =>
101- {
102- // Release previous capture and reinitialize when camera changes
103- if ( _webcamCapture != null && IsVisible )
104- {
105- _webcamModel . ReleaseCapture ( ) ;
106- _webcamCapture = null ;
107- InitializeWebcam ( ) ;
108- }
109- } ) ;
113+ . Subscribe ( M => UpdateWebcamPreview ( ) ) ;
110114
111115 _reactor . Location
112116 . CombineLatest ( _reactor . Size , ( M , N ) =>
@@ -116,24 +120,21 @@ void OnRegionChange()
116120 } )
117121 . Subscribe ( ) ;
118122
119- // If page is already visible, initialize webcam immediately
120- if ( IsVisible )
123+ // If page is already visible when setup is first called, initialize webcam
124+ // (IsVisibleChanged won't fire if page is already visible)
125+ if ( IsVisible && _webcamCapture == null )
121126 {
122- InitializeWebcam ( ) ;
123- }
124- }
125-
126- void InitializeWebcam ( )
127- {
128- if ( _webcamCapture != null )
129- return ;
127+ _webcamCapture = _webcamModel . InitCapture ( ) ;
130128
131- _webcamCapture = _webcamModel . InitCapture ( ) ;
132-
133- if ( _webcamCapture . Value is { } capture )
129+ if ( _webcamCapture . Value is { } capture )
130+ {
131+ _reactor . WebcamSize . OnNext ( new WSize ( capture . Width , capture . Height ) ) ;
132+ // UpdateWebcamPreview will be called by the Location/Size subscription
133+ }
134+ }
135+ else
134136 {
135- _reactor . WebcamSize . OnNext ( new WSize ( capture . Width , capture . Height ) ) ;
136-
137+ // Just update preview position if webcam already exists
137138 UpdateWebcamPreview ( ) ;
138139 }
139140 }
0 commit comments