基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-14 b84db8983d63a9a981083ebce1a139860c6bdf25
src/Moli.Desktop/Windows/WallpaperWindow.cs
@@ -23,7 +23,6 @@
    private readonly DesktopPaths _paths;
    private readonly IDiagnosticsService _diagnostics;
    private bool _clickThrough = true;
    private double _opacity = 1;
    public WallpaperWindow(
        PluginDescriptor plugin,
@@ -59,7 +58,7 @@
    public async Task LoadAsync(WallpaperRuntimeState state, CancellationToken cancellationToken = default)
    {
        _clickThrough = state.ClickThrough;
        _opacity = state.Opacity;
        Opacity = Math.Clamp(state.Opacity, 0.1, 1.0);
        await _webView.InitializeAsync(new WebViewHostOptions
        {
            UserDataFolder = Path.Combine(_paths.WebViewDataDirectory, "wallpaper", _plugin.Id.Value),
@@ -77,7 +76,6 @@
    public void ApplyState(WallpaperRuntimeState state, bool safeInputMode)
    {
        _clickThrough = !safeInputMode && state.ClickThrough;
        _opacity = state.Opacity;
        Opacity = Math.Clamp(state.Opacity, 0.1, 1.0);
        ApplyWin32Styles();
    }
@@ -124,9 +122,9 @@
        {
            _interop.SetToolWindow(hwnd, true);
            _interop.SetNoActivate(hwnd, true);
            _interop.SetLayered(hwnd, true, ToAlpha(_opacity));
            _interop.SetLayered(hwnd, true);
            _interop.SetClickThrough(hwnd, _clickThrough);
            _interop.MoveToDesktopLayer(hwnd, DesktopLayerMode.WorkerW, _monitor);
            _interop.MoveToDesktopLayer(hwnd, DesktopLayerMode.WorkerW, _monitor, _monitor.Bounds);
        }
        catch (Exception ex)
        {
@@ -137,6 +135,4 @@
                new Dictionary<string, string> { ["pluginId"] = _plugin.Id.Value, ["exception"] = ex.GetType().Name });
        }
    }
    private static byte ToAlpha(double opacity) => (byte)Math.Round(Math.Clamp(opacity, 0.1, 1.0) * 255);
}