Skip to content

Commit 66cb660

Browse files
Refactor Webcam initialization and navigation logic
Co-authored-by: toniolo.luca <toniolo.luca@outlook.com>
1 parent 2666b15 commit 66cb660

2 files changed

Lines changed: 32 additions & 28 deletions

File tree

src/Captura/Pages/SettingsPage.xaml.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ namespace Captura
55
{
66
public partial class SettingsPage
77
{
8+
bool _webcamPageNavigated;
9+
810
public SettingsPage()
911
{
1012
InitializeComponent();
1113

1214
Loaded += (s, e) =>
1315
{
14-
// Navigate to WebcamPlacementPreviewPage singleton instance for the WebCam tab
15-
if (WebcamTabFrame != null)
16+
// Navigate to WebcamPlacementPreviewPage singleton instance for the WebCam tab (only once)
17+
if (WebcamTabFrame != null && !_webcamPageNavigated)
1618
{
19+
_webcamPageNavigated = true;
1720
WebcamTabFrame.Navigate(ServiceProvider.Get<WebcamPlacementPreviewPage>());
1821
}
1922
};

src/Captura/Pages/WebcamPlacementPreviewPage.xaml.cs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)