Skip to content

Commit 2d1d954

Browse files
Refactor webcam initialization and camera change handling
Co-authored-by: toniolo.luca <toniolo.luca@outlook.com>
1 parent e1a54ad commit 2d1d954

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

src/Captura/Pages/WebcamPlacementPreviewPage.xaml.cs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,7 @@ void SetupPreview()
7676
{
7777
if (IsVisible && _webcamCapture == null)
7878
{
79-
_webcamCapture = _webcamModel.InitCapture();
80-
81-
if (_webcamCapture.Value is { } capture)
82-
{
83-
_reactor.WebcamSize.OnNext(new WSize(capture.Width, capture.Height));
84-
85-
UpdateWebcamPreview();
86-
}
79+
InitializeWebcam();
8780
}
8881
else if (!IsVisible && _webcamCapture != null)
8982
{
@@ -104,7 +97,16 @@ void OnRegionChange()
10497

10598
_webcamModel
10699
.ObserveProperty(M => M.SelectedCam)
107-
.Subscribe(M => UpdateWebcamPreview());
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+
});
108110

109111
_reactor.Location
110112
.CombineLatest(_reactor.Size, (M, N) =>
@@ -114,7 +116,26 @@ void OnRegionChange()
114116
})
115117
.Subscribe();
116118

117-
UpdateWebcamPreview();
119+
// If page is already visible, initialize webcam immediately
120+
if (IsVisible)
121+
{
122+
InitializeWebcam();
123+
}
124+
}
125+
126+
void InitializeWebcam()
127+
{
128+
if (_webcamCapture != null)
129+
return;
130+
131+
_webcamCapture = _webcamModel.InitCapture();
132+
133+
if (_webcamCapture.Value is { } capture)
134+
{
135+
_reactor.WebcamSize.OnNext(new WSize(capture.Width, capture.Height));
136+
137+
UpdateWebcamPreview();
138+
}
118139
}
119140

120141
Rectangle GetPreviewWindowRect()

0 commit comments

Comments
 (0)