基于WebView2实现的Win11桌面框架,利用插件自定义
Moli
2026-05-03 9806138b910904d2f982ebf9471b295fb6939715
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
using Moli.Desktop.Diagnostics;
using Moli.Desktop.Layout;
using Moli.Desktop.Plugins;
 
namespace Moli.Desktop.Runtime;
 
public sealed record WallpaperRuntimeState
{
    public bool IsEnabled { get; init; }
    public bool IsPaused { get; init; }
    public bool IsMuted { get; init; }
    public double Opacity { get; init; } = 1;
    public bool ClickThrough { get; init; } = true;
    public PluginId? PluginId { get; init; }
}
 
public sealed record AppRuntimeState
{
    public bool IsEditMode { get; init; }
    public bool IsGlobalLocked { get; init; }
    public bool IsSafeInputMode { get; init; }
    public WallpaperRuntimeState Wallpaper { get; init; } = new();
    public IReadOnlyList<PluginRuntimeState> Plugins { get; init; } = [];
    public IReadOnlyList<WidgetRuntimeState> Widgets { get; init; } = [];
    public IReadOnlyList<DiagnosticEntry> Diagnostics { get; init; } = [];
}
 
public sealed record RuntimeEvent
{
    public required string Name { get; init; }
    public object? Payload { get; init; }
    public DateTimeOffset Timestamp { get; init; } = DateTimeOffset.UtcNow;
}
 
public interface IRuntimeEventBus
{
    IDisposable Subscribe(Func<RuntimeEvent, Task> handler);
    Task PublishAsync(RuntimeEvent runtimeEvent, CancellationToken cancellationToken = default);
}
 
public sealed class RuntimeEventBus : IRuntimeEventBus
{
    private readonly object _gate = new();
    private readonly List<Func<RuntimeEvent, Task>> _handlers = [];
 
    public IDisposable Subscribe(Func<RuntimeEvent, Task> handler)
    {
        lock (_gate)
        {
            _handlers.Add(handler);
        }
 
        return new Subscription(() =>
        {
            lock (_gate)
            {
                _handlers.Remove(handler);
            }
        });
    }
 
    public async Task PublishAsync(RuntimeEvent runtimeEvent, CancellationToken cancellationToken = default)
    {
        Func<RuntimeEvent, Task>[] handlers;
        lock (_gate)
        {
            handlers = [.. _handlers];
        }
 
        foreach (var handler in handlers)
        {
            cancellationToken.ThrowIfCancellationRequested();
            await handler(runtimeEvent).ConfigureAwait(false);
        }
    }
 
    private sealed class Subscription(Action dispose) : IDisposable
    {
        public void Dispose() => dispose();
    }
}