Skip to content

Commit a979051

Browse files
Refactor WebcamPreviewPage to avoid duplicate updates
Co-authored-by: toniolo.luca <toniolo.luca@outlook.com>
1 parent 66cb660 commit a979051

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

src/Captura/Pages/WebcamPlacementPreviewPage.xaml.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,33 +108,39 @@ void OnRegionChange()
108108

109109
PreviewGrid.LayoutUpdated += (S, E) => OnRegionChange();
110110

111-
_webcamModel
112-
.ObserveProperty(M => M.SelectedCam)
113-
.Subscribe(M => UpdateWebcamPreview());
114-
115-
_reactor.Location
116-
.CombineLatest(_reactor.Size, (M, N) =>
117-
{
118-
UpdateWebcamPreview();
119-
return 0;
120-
})
121-
.Subscribe();
122-
123111
// If page is already visible when setup is first called, initialize webcam
124112
// (IsVisibleChanged won't fire if page is already visible)
113+
var needsInitialUpdate = false;
125114
if (IsVisible && _webcamCapture == null)
126115
{
127116
_webcamCapture = _webcamModel.InitCapture();
128117

129118
if (_webcamCapture.Value is { } capture)
130119
{
131120
_reactor.WebcamSize.OnNext(new WSize(capture.Width, capture.Height));
132-
// UpdateWebcamPreview will be called by the Location/Size subscription
121+
needsInitialUpdate = true;
133122
}
134123
}
135-
else
124+
125+
// Set up reactive subscriptions after initial webcam setup
126+
// Skip first emission to avoid duplicate update during initialization
127+
_reactor.Location
128+
.CombineLatest(_reactor.Size, (M, N) =>
129+
{
130+
UpdateWebcamPreview();
131+
return 0;
132+
})
133+
.Skip(needsInitialUpdate ? 1 : 0)
134+
.Subscribe();
135+
136+
_webcamModel
137+
.ObserveProperty(M => M.SelectedCam)
138+
.Skip(1) // Skip initial value to avoid duplicate update
139+
.Subscribe(M => UpdateWebcamPreview());
140+
141+
// Only call update once if we initialized the webcam during setup
142+
if (needsInitialUpdate)
136143
{
137-
// Just update preview position if webcam already exists
138144
UpdateWebcamPreview();
139145
}
140146
}

0 commit comments

Comments
 (0)