基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-02 690cf73d6100e93d6ae7ca95dededbad4e40eeed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System.IO;
using System.Text.Json;
using System.Windows;
using System.Windows.Media;
using Moli.Desktop.Configuration;
using Moli.Desktop.Diagnostics;
using Moli.Desktop.Host;
using Moli.Desktop.Interop;
using Moli.Desktop.Plugins;
using Moli.Desktop.Runtime;
using Moli.Desktop.WebView;
using Brushes = System.Windows.Media.Brushes;
 
namespace Moli.Desktop.Windows;
 
/// <summary>Borderless desktop wallpaper surface backed by one WebView host.</summary>
public sealed class WallpaperWindow : Window
{
    private readonly PluginDescriptor _plugin;
    private readonly MonitorInfo _monitor;
    private readonly IWebViewHost _webView;
    private readonly IWin32InteropService _interop;
    private readonly DesktopPaths _paths;
    private readonly IDiagnosticsService _diagnostics;
    private bool _clickThrough = true;
    private double _opacity = 1;
 
    public WallpaperWindow(
        PluginDescriptor plugin,
        MonitorInfo monitor,
        IWebViewHost webView,
        IWin32InteropService interop,
        DesktopPaths paths,
        IDiagnosticsService diagnostics)
    {
        _plugin = plugin;
        _monitor = monitor;
        _webView = webView;
        _interop = interop;
        _paths = paths;
        _diagnostics = diagnostics;
 
        Title = $"Moli Wallpaper - {plugin.Manifest.Name}";
        WindowStyle = WindowStyle.None;
        ResizeMode = ResizeMode.NoResize;
        AllowsTransparency = true;
        Background = Brushes.Transparent;
        ShowInTaskbar = false;
        Topmost = false;
        Left = monitor.Bounds.X;
        Top = monitor.Bounds.Y;
        Width = monitor.Bounds.Width;
        Height = monitor.Bounds.Height;
        Content = webView.View;
 
        Loaded += (_, _) => ApplyWin32Styles();
    }
 
    public async Task LoadAsync(WallpaperRuntimeState state, CancellationToken cancellationToken = default)
    {
        _clickThrough = state.ClickThrough;
        _opacity = state.Opacity;
        await _webView.InitializeAsync(new WebViewHostOptions
        {
            UserDataFolder = Path.Combine(_paths.WebViewDataDirectory, "wallpaper", _plugin.Id.Value),
            IsTransparent = true,
            AreDevToolsEnabled = true,
            IsMuted = state.IsMuted,
            InitialScript = PluginBridgeScript.Build()
        }, cancellationToken).ConfigureAwait(true);
 
        await _webView.InjectBridgeAsync(CreateBridgeContext(), cancellationToken).ConfigureAwait(true);
        await _webView.NavigateAsync(_plugin.EntryUri, cancellationToken).ConfigureAwait(true);
        await _webView.DispatchLifecycleEventAsync("moli:wallpaper-enabled", state, cancellationToken).ConfigureAwait(true);
    }
 
    public void ApplyState(WallpaperRuntimeState state, bool safeInputMode)
    {
        _clickThrough = !safeInputMode && state.ClickThrough;
        _opacity = state.Opacity;
        Opacity = Math.Clamp(state.Opacity, 0.1, 1.0);
        ApplyWin32Styles();
    }
 
    public Task PauseAsync(CancellationToken cancellationToken = default)
        => _webView.DispatchLifecycleEventAsync("moli:pause", null, cancellationToken);
 
    public Task ResumeAsync(CancellationToken cancellationToken = default)
        => _webView.DispatchLifecycleEventAsync("moli:resume", null, cancellationToken);
 
    public Task ReloadAsync(CancellationToken cancellationToken = default) => _webView.ReloadAsync(cancellationToken);
 
    protected override async void OnClosed(EventArgs e)
    {
        await _webView.DisposeAsync();
        base.OnClosed(e);
    }
 
    private PluginBridgeContext CreateBridgeContext() => new()
    {
        HostId = _webView.HostId,
        Manifest = _plugin.Manifest,
        RuntimeInfo = new PluginRuntimeBridgeInfo
        {
            HostId = _webView.HostId,
            PluginId = _plugin.Id.Value,
            PluginType = _plugin.Type,
            IsVisible = true
        },
        Config = _plugin.Manifest.Config.ValueKind == JsonValueKind.Undefined
            ? default
            : _plugin.Manifest.Config.Clone()
    };
 
    private void ApplyWin32Styles()
    {
        var hwnd = _interop.GetWindowHandle(this);
        if (hwnd == IntPtr.Zero)
        {
            return;
        }
 
        try
        {
            _interop.SetToolWindow(hwnd, true);
            _interop.SetNoActivate(hwnd, true);
            _interop.SetLayered(hwnd, true, ToAlpha(_opacity));
            _interop.SetClickThrough(hwnd, _clickThrough);
            _interop.MoveToDesktopLayer(hwnd, DesktopLayerMode.WorkerW, _monitor);
        }
        catch (Exception ex)
        {
            _diagnostics.Record(
                DiagnosticSeverity.Warning,
                DiagnosticCodes.DesktopLayerAttachFailed,
                "Failed to apply wallpaper window styles.",
                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);
}