基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-17 9b5711d3124622b1f1b18500d442c76492da877a
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
143
144
145
146
147
148
149
150
151
152
153
154
using System.Text.Json.Serialization;
using Moli.Desktop.Diagnostics;
using Moli.Desktop.Layout;
using Moli.Desktop.Plugins;
using Moli.Desktop.Runtime;
 
namespace Moli.Desktop.Api;
 
public sealed record LocalApiStatusSnapshot
{
    public bool IsListening { get; init; }
    public string Host { get; init; } = "127.0.0.1";
    public int Port { get; init; } = 8765;
    public string? ErrorCode { get; init; }
    public string? Message { get; init; }
}
 
public interface ILocalApiStatus
{
    LocalApiStatusSnapshot GetStatus();
}
 
public sealed record ApiErrorResponse(string ErrorCode, string Message);
 
public sealed record ModeRequest(bool? Enabled);
 
public sealed record RectDto(double X, double Y, double Width, double Height)
{
    public RectD ToRect() => new(X, Y, Width, Height);
    public static RectDto From(RectD rect) => new(rect.X, rect.Y, rect.Width, rect.Height);
}
 
public sealed record WidgetLayoutPatchRequest
{
    public RectDto? Bounds { get; init; }
    public string? MonitorId { get; init; }
    public bool? IsVisible { get; init; }
    public bool? IsLocked { get; init; }
    public double? Opacity { get; init; }
    public bool? ClickThrough { get; init; }
    public double? Scale { get; init; }
    public DisplayLayer? DisplayLayer { get; init; }
 
    public WidgetLayoutPatch ToPatch() => new()
    {
        Bounds = Bounds?.ToRect(),
        MonitorId = string.IsNullOrWhiteSpace(MonitorId) ? null : new MonitorId(MonitorId),
        IsVisible = IsVisible,
        IsLocked = IsLocked,
        Opacity = Opacity,
        ClickThrough = ClickThrough,
        Scale = Scale,
        DisplayLayer = DisplayLayer
    };
}
 
public sealed record ApiStatusResponse
{
    public bool IsEditMode { get; init; }
    public bool IsGlobalLocked { get; init; }
    public bool IsSafeInputMode { get; init; }
    public LocalApiStatusSnapshot Api { get; init; } = new();
    public WallpaperResponse Wallpaper { get; init; } = new();
    public IReadOnlyList<PluginResponse> Plugins { get; init; } = [];
    public IReadOnlyList<WidgetResponse> Widgets { get; init; } = [];
    public IReadOnlyList<DiagnosticEntry> Diagnostics { get; init; } = [];
 
    public static ApiStatusResponse From(AppRuntimeState state, LocalApiStatusSnapshot api) => new()
    {
        IsEditMode = state.IsEditMode,
        IsGlobalLocked = state.IsGlobalLocked,
        IsSafeInputMode = state.IsSafeInputMode,
        Api = api,
        Wallpaper = WallpaperResponse.From(state.Wallpaper),
        Plugins = [.. state.Plugins.Select(PluginResponse.From)],
        Widgets = [.. state.Widgets.Select(WidgetResponse.From)],
        Diagnostics = state.Diagnostics
    };
}
 
public sealed record WallpaperResponse
{
    public bool IsEnabled { get; init; }
    public bool IsPaused { get; init; }
    public bool IsMuted { get; init; }
    public double Opacity { get; init; }
    public bool ClickThrough { get; init; }
    public string? PluginId { get; init; }
 
    public static WallpaperResponse From(WallpaperRuntimeState state) => new()
    {
        IsEnabled = state.IsEnabled,
        IsPaused = state.IsPaused,
        IsMuted = state.IsMuted,
        Opacity = state.Opacity,
        ClickThrough = state.ClickThrough,
        PluginId = state.PluginId?.Value
    };
}
 
public sealed record PluginResponse
{
    public required string Id { get; init; }
    public required string Name { get; init; }
    [JsonConverter(typeof(JsonStringEnumConverter<PluginType>))]
    public PluginType Type { get; init; }
    [JsonConverter(typeof(JsonStringEnumConverter<PluginState>))]
    public PluginState State { get; init; }
    public string? ErrorCode { get; init; }
    public string Entry { get; init; } = "";
 
    public static PluginResponse From(PluginRuntimeState state) => new()
    {
        Id = state.Id.Value,
        Name = state.Name,
        Type = state.Type,
        State = state.State,
        ErrorCode = state.ErrorCode,
        Entry = state.Entry ?? ""
    };
}
 
public sealed record WidgetResponse
{
    public required string InstanceId { get; init; }
    public required string PluginId { get; init; }
    public required RectDto Bounds { get; init; }
    public required string MonitorId { get; init; }
    public bool IsVisible { get; init; }
    public bool IsLocked { get; init; }
    public double Opacity { get; init; }
    public bool ClickThrough { get; init; }
    [JsonConverter(typeof(JsonStringEnumConverter<DisplayLayer>))]
    public DisplayLayer DisplayLayer { get; init; }
 
    public static WidgetResponse From(WidgetRuntimeState state) => new()
    {
        InstanceId = state.InstanceId.Value,
        PluginId = state.PluginId.Value,
        Bounds = RectDto.From(state.Bounds),
        MonitorId = state.MonitorId.Value,
        IsVisible = state.IsVisible,
        IsLocked = state.IsLocked,
        Opacity = state.Opacity,
        ClickThrough = state.ClickThrough,
        DisplayLayer = state.DisplayLayer
    };
}
 
public sealed record RuntimeEventResponse(string Name, DateTimeOffset Timestamp, object? Payload)
{
    public static RuntimeEventResponse From(RuntimeEvent runtimeEvent)
        => new(runtimeEvent.Name, runtimeEvent.Timestamp, runtimeEvent.Payload);
}