From cf40cded1708bb303be4a404b7aceddd196e1d46 Mon Sep 17 00:00:00 2001
From: Moli <13026959681@163.com>
Date: 星期三, 29 七月 2026 20:58:14 +0800
Subject: [PATCH] v1.0

---
 src/Moli.Desktop/Windows/RuntimeSurfaceController.cs |   77 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 76 insertions(+), 1 deletions(-)

diff --git a/src/Moli.Desktop/Windows/RuntimeSurfaceController.cs b/src/Moli.Desktop/Windows/RuntimeSurfaceController.cs
index ca24bb8..00fd78c 100644
--- a/src/Moli.Desktop/Windows/RuntimeSurfaceController.cs
+++ b/src/Moli.Desktop/Windows/RuntimeSurfaceController.cs
@@ -1,4 +1,5 @@
 using System.Windows;
+using System.Text.Json;
 using Microsoft.Extensions.DependencyInjection;
 using Moli.Desktop.Configuration;
 using Moli.Desktop.Diagnostics;
@@ -21,11 +22,13 @@
     private readonly DesktopPaths _paths;
     private readonly IDiagnosticsService _diagnostics;
     private readonly IServiceProvider _services;
+    private readonly IRuntimeEventBus _events;
     private readonly Dictionary<WidgetInstanceId, WidgetWindow> _widgets = [];
     private WallpaperWindow? _wallpaper;
     private WallpaperRuntimeState _wallpaperState = new();
     private bool _editMode;
     private bool _safeInputMode;
+    private IDisposable? _eventSubscription;
 
     public RuntimeSurfaceController(
         IPluginManager plugins,
@@ -33,7 +36,8 @@
         IWebViewHostFactory webViews,
         DesktopPaths paths,
         IDiagnosticsService diagnostics,
-        IServiceProvider services)
+        IServiceProvider services,
+        IRuntimeEventBus events)
     {
         _plugins = plugins;
         _interop = interop;
@@ -41,6 +45,8 @@
         _paths = paths;
         _diagnostics = diagnostics;
         _services = services;
+        _events = events;
+        _eventSubscription = _events.Subscribe(HandleRuntimeEventAsync);
     }
 
     public Task ApplyEditModeAsync(bool enabled, CancellationToken cancellationToken = default)
@@ -127,6 +133,7 @@
             _wallpaper.Show();
             await _wallpaper.LoadAsync(wallpaper, cancellationToken).ConfigureAwait(true);
             _wallpaper.ApplyState(wallpaper, _safeInputMode);
+            ReapplyDesktopWidgetLayers();
         });
 
     public Task DisableWallpaperAsync(CancellationToken cancellationToken = default)
@@ -149,6 +156,8 @@
             _widgets.Clear();
             _wallpaper?.Close();
             _wallpaper = null;
+            _eventSubscription?.Dispose();
+            _eventSubscription = null;
             return Task.CompletedTask;
         });
 
@@ -161,6 +170,72 @@
         await runtime.UpdateWidgetLayoutAsync(instanceId, patch, cancellationToken).ConfigureAwait(true);
     }
 
+    private void ReapplyDesktopWidgetLayers()
+    {
+        foreach (var widget in _widgets.Values.Where(widget => widget.CurrentLayout.DisplayLayer == DisplayLayer.Desktop))
+        {
+            widget.ApplyLayout(widget.CurrentLayout, _editMode, _safeInputMode);
+        }
+    }
+
+    private Task HandleRuntimeEventAsync(RuntimeEvent runtimeEvent)
+    {
+        if (!string.Equals(runtimeEvent.Name, "plugin.resizeRequested", StringComparison.OrdinalIgnoreCase) ||
+            runtimeEvent.Payload is null)
+        {
+            return Task.CompletedTask;
+        }
+
+        return OnUiThreadAsync(() =>
+        {
+            try
+            {
+                var json = JsonSerializer.Serialize(runtimeEvent.Payload, new JsonSerializerOptions(JsonSerializerDefaults.Web));
+                using var document = JsonDocument.Parse(json);
+                var root = document.RootElement;
+                if (!root.TryGetProperty("hostId", out var hostIdElement))
+                {
+                    return Task.CompletedTask;
+                }
+
+                var hostId = hostIdElement.GetString();
+                var widget = _widgets.Values.FirstOrDefault(item => string.Equals(item.HostId, hostId, StringComparison.OrdinalIgnoreCase));
+                if (widget is null ||
+                    !root.TryGetProperty("params", out var parameters) ||
+                    parameters.ValueKind != JsonValueKind.Object)
+                {
+                    return Task.CompletedTask;
+                }
+
+                var width = TryReadPositiveDouble(parameters, "width");
+                var height = TryReadPositiveDouble(parameters, "height");
+                widget.ApplyRequestedSize(width, height);
+            }
+            catch (JsonException ex)
+            {
+                _diagnostics.Record(
+                    DiagnosticSeverity.Warning,
+                    "PluginResizeRequestInvalid",
+                    "Plugin resize request payload could not be parsed.",
+                    new Dictionary<string, string> { ["exception"] = ex.GetType().Name });
+            }
+
+            return Task.CompletedTask;
+        });
+    }
+
+    private static double? TryReadPositiveDouble(JsonElement parameters, string propertyName)
+    {
+        if (!parameters.TryGetProperty(propertyName, out var value))
+        {
+            return null;
+        }
+
+        return value.ValueKind == JsonValueKind.Number && value.TryGetDouble(out var number) && number > 0
+            ? number
+            : null;
+    }
+
     private static async Task OnUiThreadAsync(Func<Task> action)
     {
         var dispatcher = Application.Current?.Dispatcher;

--
Gitblit v1.9.1