From c3a5f58e60f85400edfb5f5379145390316394c5 Mon Sep 17 00:00:00 2001
From: Moli <13026959681@163.com>
Date: 星期日, 03 五月 2026 01:51:00 +0800
Subject: [PATCH] 暂存

---
 src/Moli.Desktop/WebView/WebViewHost.cs |   66 +++++++++++++++++++++++++++++++++
 1 files changed, 66 insertions(+), 0 deletions(-)

diff --git a/src/Moli.Desktop/WebView/WebViewHost.cs b/src/Moli.Desktop/WebView/WebViewHost.cs
index 357eb59..59b2a10 100644
--- a/src/Moli.Desktop/WebView/WebViewHost.cs
+++ b/src/Moli.Desktop/WebView/WebViewHost.cs
@@ -1,6 +1,8 @@
 using System.IO;
 using System.Text.Encodings.Web;
 using System.Text.Json;
+using System.Windows;
+using System.Drawing;
 using Microsoft.Web.WebView2.Core;
 using Microsoft.Web.WebView2.Wpf;
 using Moli.Desktop.Diagnostics;
@@ -22,6 +24,7 @@
     };
     private PluginBridgeContext? _bridgeContext;
     private WebViewHostOptions? _options;
+    private IDisposable? _audioSubscription;
     private bool _initialized;
 
     public WebViewHost(
@@ -38,6 +41,7 @@
     }
 
     public WebView2 Control { get; }
+    public FrameworkElement View => Control;
     public string HostId { get; } = $"webview-{Guid.NewGuid():N}";
     public PluginId PluginId => _plugin.Id;
     public PluginType PluginType => _plugin.Type;
@@ -49,10 +53,20 @@
         Directory.CreateDirectory(options.UserDataFolder);
         var environment = await CoreWebView2Environment.CreateAsync(null, options.UserDataFolder).ConfigureAwait(true);
         await Control.EnsureCoreWebView2Async(environment).ConfigureAwait(true);
+        if (options.IsTransparent)
+        {
+            Control.DefaultBackgroundColor = Color.Transparent;
+        }
+
         Control.CoreWebView2.Settings.AreDevToolsEnabled = options.AreDevToolsEnabled;
         Control.CoreWebView2.IsMuted = options.IsMuted;
         Control.CoreWebView2.WebMessageReceived += OnWebMessageReceived;
+        Control.CoreWebView2.ScreenCaptureStarting += OnScreenCaptureStarting;
         Control.NavigationCompleted += OnNavigationCompleted;
+        if (HasPermission("audio-capture"))
+        {
+            _audioSubscription = _events.Subscribe(DispatchAudioSpectrumAsync);
+        }
 
         if (!string.IsNullOrWhiteSpace(options.InitialScript))
         {
@@ -111,14 +125,36 @@
 
     public async ValueTask DisposeAsync()
     {
+        _audioSubscription?.Dispose();
+        _audioSubscription = null;
+
         if (Control.CoreWebView2 is not null)
         {
             Control.CoreWebView2.WebMessageReceived -= OnWebMessageReceived;
+            Control.CoreWebView2.ScreenCaptureStarting -= OnScreenCaptureStarting;
         }
 
         Control.NavigationCompleted -= OnNavigationCompleted;
         Control.Dispose();
         await Task.CompletedTask;
+    }
+
+    private Task DispatchAudioSpectrumAsync(RuntimeEvent runtimeEvent)
+    {
+        if (runtimeEvent.Name != "audio.spectrum" || runtimeEvent.Payload is null)
+        {
+            return Task.CompletedTask;
+        }
+
+        var detail = JsonSerializer.Serialize(runtimeEvent.Payload, _jsonOptions);
+        var script = $"window.dispatchEvent(new CustomEvent(\"moli:audio-spectrum\", {{ detail: {detail} }}));";
+        var dispatcher = Control.Dispatcher;
+        if (dispatcher.CheckAccess())
+        {
+            return ExecuteScriptSafelyAsync(script);
+        }
+
+        return dispatcher.InvokeAsync(() => ExecuteScriptSafelyAsync(script)).Task.Unwrap();
     }
 
     private async void OnWebMessageReceived(object? sender, CoreWebView2WebMessageReceivedEventArgs e)
@@ -163,6 +199,36 @@
         }).ConfigureAwait(false);
     }
 
+    private void OnScreenCaptureStarting(object? sender, CoreWebView2ScreenCaptureStartingEventArgs e)
+    {
+        e.Cancel = true;
+        e.Handled = true;
+        _diagnostics.Record(
+            DiagnosticSeverity.Info,
+            "WebViewScreenCaptureBlocked",
+            "WebView screen capture request was blocked.",
+            new Dictionary<string, string> { ["hostId"] = HostId, ["pluginId"] = PluginId.Value });
+    }
+
+    private async Task ExecuteScriptSafelyAsync(string script)
+    {
+        if (!_initialized || Control.CoreWebView2 is null)
+        {
+            return;
+        }
+
+        try
+        {
+            await Control.ExecuteScriptAsync(script).ConfigureAwait(true);
+        }
+        catch (InvalidOperationException)
+        {
+        }
+    }
+
+    private bool HasPermission(string permission)
+        => _plugin.Manifest.Permissions.Any(item => string.Equals(item, permission, StringComparison.OrdinalIgnoreCase));
+
     private void EnsureInitialized()
     {
         if (!_initialized || Control.CoreWebView2 is null || _options is null)

--
Gitblit v1.9.1