Web source: off-screen WebView2 capture + layout persistence + transparent bg
Bundle exception (2026-08-27): this lands prior-session work plus today's transparency fix as one commit per creator instruction. WebView2Manager (new): off-screen WebView2 control parked at -5000/-5000 in the window's visual tree, captures via CapturePreviewAsync every 100 ms into a BGRA8 VideoFrame. Source Width/Height sync to the control via PropertyChanged. JS transparent-background injection runs on every NavigationStarting/NavigationCompleted so the page's own CSS does not paint white over the capture. Transparency fix: DefaultBackgroundColor = System.Drawing.Color.Transparent set on the WPF control before EnsureCoreWebView2Async. The WPF SDK doc says the value is forwarded to the controller on init; the IDL (ICoreWebView2- Controller2) confirms alpha=0 makes the capture alpha-preserving. The prior-session 'CoreWebView2.BackgroundColor' path was the wrong property name and has been replaced. LayoutStore: WebUri column added (idempotent ALTER TABLE) and round-tripped on save/load (schema v11). MainViewModel: WebView2Manager constructed on Loaded; loaded web sources re-registered; Source.WebUri PropertyChanged forwards to manager. MainWindow: off-screen host Canvas added to visual tree. Tests: 3 scale-sync + WebView2_DefaultBackgroundColor_Is_Transparent + LayoutStorePersistenceTests.WebSource_WebUri_Persists. 234/236 pass (2 pre-existing failures unchanged). Scale bug (DPI/physical-pixel mismatch) still open.
This commit is contained in:
@@ -131,6 +131,7 @@ public class MainViewModel : ViewModelBase
|
||||
private readonly ScreenCaptureManager _screenCaptureManager;
|
||||
private readonly ScreenCaptureSourceFactory _screenCaptureFactory;
|
||||
private readonly ChatBoxRenderer _chatBoxRenderer;
|
||||
private WebView2Manager? _webView2Manager;
|
||||
|
||||
// Mock chat messages for the ChatBox preview — branded placeholders so the
|
||||
// creator sees where the overlay lands before going live.
|
||||
@@ -1892,6 +1893,7 @@ public class MainViewModel : ViewModelBase
|
||||
UpdateBackgroundImage();
|
||||
ReacquireWebcam();
|
||||
ReacquireScreenCaptures();
|
||||
RegisterLoadedWebSources();
|
||||
_socials = _layoutStore.Socials;
|
||||
RenderSocialBarFrame();
|
||||
HealFediverseSoftwareInBackground();
|
||||
@@ -2109,6 +2111,22 @@ public class MainViewModel : ViewModelBase
|
||||
_fullScreenDetector.StartWatching();
|
||||
}
|
||||
|
||||
private void RegisterLoadedWebSources()
|
||||
{
|
||||
if (_webView2Manager == null) return;
|
||||
|
||||
foreach (var source in Scenes.SelectMany(s => s.Elements).OfType<Source>()
|
||||
.Where(s => s.Type == SourceType.WebSource))
|
||||
{
|
||||
_webView2Manager.Register(source);
|
||||
source.PropertyChanged += (_, e) =>
|
||||
{
|
||||
if (e.PropertyName == nameof(Source.WebUri))
|
||||
_webView2Manager.OnWebUriChanged(source);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// ScreenCaptureManager creates the shared WriteableBitmap on the UI thread;
|
||||
// every background pointing at that key picks it up.
|
||||
private void OnScreenPreviewBitmapChanged(string key, WriteableBitmap bitmap)
|
||||
@@ -2272,6 +2290,19 @@ public class MainViewModel : ViewModelBase
|
||||
_ = RedesignateBackgroundAsync($"{MonitorKeyPrefix}{display.Index}");
|
||||
}
|
||||
|
||||
public void InitWebView2(Panel hostPanel)
|
||||
{
|
||||
_webView2Manager = new WebView2Manager(hostPanel, System.Windows.Application.Current.Dispatcher);
|
||||
_webView2Manager.PreviewBitmapChanged += OnWebView2PreviewBitmapChanged;
|
||||
}
|
||||
|
||||
private void OnWebView2PreviewBitmapChanged(string sourceId, WriteableBitmap bitmap)
|
||||
{
|
||||
foreach (var scene in Scenes)
|
||||
foreach (var el in scene.Elements.OfType<Source>().Where(s => s.Id == sourceId))
|
||||
el.VideoImageSource = bitmap;
|
||||
}
|
||||
|
||||
public void Shutdown()
|
||||
{
|
||||
_saveDebounce?.Stop();
|
||||
@@ -2280,6 +2311,7 @@ public class MainViewModel : ViewModelBase
|
||||
_framePump.Dispose();
|
||||
_cameraManager.Dispose();
|
||||
_screenCaptureManager.Dispose();
|
||||
_webView2Manager?.Dispose();
|
||||
_layoutStore.Dispose();
|
||||
_musicPlayer.Dispose();
|
||||
_youtubeChat.Dispose();
|
||||
@@ -2488,7 +2520,7 @@ public class MainViewModel : ViewModelBase
|
||||
SourceType.Image => "Image",
|
||||
SourceType.TextOverlay => "Text",
|
||||
SourceType.Countdown => "Countdown",
|
||||
SourceType.WebSource => "Web",
|
||||
SourceType.WebSource => "Web Resource",
|
||||
SourceType.ChatBox => "YouTube Chat",
|
||||
_ => "Layer",
|
||||
};
|
||||
@@ -2501,17 +2533,31 @@ public class MainViewModel : ViewModelBase
|
||||
if (sourceType == SourceType.ChatBox && !CanAddYouTubeChat)
|
||||
return;
|
||||
|
||||
// Web resources use "Web Resource-N" naming (0-indexed per scene)
|
||||
var webCount = scene.Elements.OfType<Source>().Count(s => s.Type == SourceType.WebSource);
|
||||
var source = new Source
|
||||
{
|
||||
Name = NextSourceName(scene, baseName),
|
||||
Name = sourceType == SourceType.WebSource
|
||||
? $"{baseName}-{webCount}"
|
||||
: NextSourceName(scene, baseName),
|
||||
Type = sourceType,
|
||||
X = sourceType == SourceType.ChatBox ? 1400 : 0,
|
||||
Y = sourceType == SourceType.ChatBox ? 20 : 0,
|
||||
Width = sourceType == SourceType.ChatBox ? 500 : 0,
|
||||
Height = sourceType == SourceType.ChatBox ? 648 : 0,
|
||||
Width = sourceType == SourceType.WebSource ? 640 : sourceType == SourceType.ChatBox ? 500 : 0,
|
||||
Height = sourceType == SourceType.WebSource ? 480 : sourceType == SourceType.ChatBox ? 648 : 0,
|
||||
};
|
||||
scene.Elements.Add(source);
|
||||
|
||||
if (sourceType == SourceType.WebSource)
|
||||
{
|
||||
_webView2Manager?.Register(source);
|
||||
source.PropertyChanged += (_, e) =>
|
||||
{
|
||||
if (e.PropertyName == nameof(Source.WebUri))
|
||||
_webView2Manager?.OnWebUriChanged(source);
|
||||
};
|
||||
}
|
||||
|
||||
if (sourceType == SourceType.ChatBox)
|
||||
{
|
||||
source.PropertyChanged += (_, e) =>
|
||||
@@ -2797,6 +2843,8 @@ public class MainViewModel : ViewModelBase
|
||||
SelectedElement = null;
|
||||
_ = _cameraManager.ReleaseAsync(_webcam.DeviceId);
|
||||
}
|
||||
if (element is Source { Type: SourceType.WebSource } webSource)
|
||||
_webView2Manager?.Unregister(webSource.Id);
|
||||
scene.Elements.Remove(element);
|
||||
|
||||
if (element is WebcamSceneConfig && _webcam != null
|
||||
@@ -3200,6 +3248,7 @@ public class MainViewModel : ViewModelBase
|
||||
WebcamSceneConfig webcam => _cameraManager.GetLatestFrame(webcam.WebcamId),
|
||||
Source { IsLiveCapture: true, CaptureKey: not null } live => _screenCaptureManager.GetLatestFrame(live.CaptureKey),
|
||||
Source { Type: SourceType.ChatBox } chatBox => RenderChatBox(chatBox),
|
||||
Source { Type: SourceType.WebSource } web => _webView2Manager?.GetLatestFrame(web.Id),
|
||||
Source { AssetId: not null } image => StaticPixelCache.Get(image.AssetId),
|
||||
_ => null,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user