From 787b2effcd7d56b1c90f42db05c172e28019295f Mon Sep 17 00:00:00 2001
From: Moli <13026959681@163.com>
Date: 星期六, 02 五月 2026 16:46:17 +0800
Subject: [PATCH] T09补充

---
 src/Moli.Desktop.Tests/Runtime/DesktopRuntimeCoordinatorTests.cs |   37 +++++++++++++++++-
 src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs            |   20 +++++++++-
 src/Moli.Desktop/Runtime/IRuntimeSurfaceController.cs            |   15 -------
 3 files changed, 52 insertions(+), 20 deletions(-)

diff --git a/src/Moli.Desktop.Tests/Runtime/DesktopRuntimeCoordinatorTests.cs b/src/Moli.Desktop.Tests/Runtime/DesktopRuntimeCoordinatorTests.cs
index b350e77..7edad8b 100644
--- a/src/Moli.Desktop.Tests/Runtime/DesktopRuntimeCoordinatorTests.cs
+++ b/src/Moli.Desktop.Tests/Runtime/DesktopRuntimeCoordinatorTests.cs
@@ -76,8 +76,26 @@
 
         Assert.False(result.Succeeded);
         Assert.Equal("WidgetLayoutLocked", result.ErrorCode);
-        Assert.Equal(1, fixture.Surface.AppliedLayoutCount);
+        Assert.Equal(2, fixture.Surface.AppliedLayoutCount);
+        Assert.True(fixture.Surface.LastAppliedLayout?.IsLocked);
         Assert.Equal(new RectD(100, 100, 320, 160), fixture.Surface.LastAppliedLayout?.Bounds);
+    }
+
+    [Fact]
+    public async Task StartAsync_RestoresRuntimeFlagsBeforeCreatingWidgetSurfaces()
+    {
+        var fixture = await RuntimeFixture.CreateAsync(new RuntimeSettings
+        {
+            EditMode = true,
+            SafeInputMode = true
+        });
+
+        var result = await fixture.Runtime.StartAsync();
+
+        Assert.True(result.Succeeded);
+        Assert.True(fixture.Surface.EditModeEnabled);
+        Assert.True(fixture.Surface.SafeInputModeEnabled);
+        Assert.Equal(1, fixture.Surface.AppliedLayoutCount);
     }
 
     private sealed class RuntimeFixture
@@ -102,7 +120,7 @@
         public FakeRuntimeSurfaceController Surface { get; }
         public WidgetInstanceId WidgetId { get; }
 
-        public static async Task<RuntimeFixture> CreateAsync()
+        public static async Task<RuntimeFixture> CreateAsync(RuntimeSettings? runtimeSettings = null)
         {
             var root = Path.Combine(Path.GetTempPath(), "Moli.Desktop.Tests", Guid.NewGuid().ToString("N"));
             var paths = new DesktopPaths(Path.Combine(root, "app"), Path.Combine(root, "local"));
@@ -115,6 +133,14 @@
             var widgetId = new WidgetInstanceId("widget-01");
 
             await CreatePluginAsync(paths);
+            if (runtimeSettings is not null)
+            {
+                await configuration.SaveAppSettingsAsync(new AppSettingsDocument
+                {
+                    Runtime = runtimeSettings
+                });
+            }
+
             await configuration.SaveLayoutAsync(new LayoutDocument
             {
                 Monitors =
@@ -183,6 +209,7 @@
     private sealed class FakeRuntimeSurfaceController : IRuntimeSurfaceController
     {
         public bool EditModeEnabled { get; private set; }
+        public bool SafeInputModeEnabled { get; private set; }
         public WidgetLayout? LastAppliedLayout { get; private set; }
         public int AppliedLayoutCount { get; private set; }
 
@@ -192,7 +219,11 @@
             return Task.CompletedTask;
         }
 
-        public Task ApplySafeInputModeAsync(bool enabled, CancellationToken cancellationToken = default) => Task.CompletedTask;
+        public Task ApplySafeInputModeAsync(bool enabled, CancellationToken cancellationToken = default)
+        {
+            SafeInputModeEnabled = enabled;
+            return Task.CompletedTask;
+        }
 
         public Task ApplyWidgetLayoutAsync(WidgetLayout layout, CancellationToken cancellationToken = default)
         {
diff --git a/src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs b/src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs
index fc537cd..0b9f6e9 100644
--- a/src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs
+++ b/src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs
@@ -49,6 +49,8 @@
             _settings = await _configuration.LoadAppSettingsAsync(cancellationToken).ConfigureAwait(false);
             _isEditMode = _settings.Runtime.EditMode;
             _isSafeInputMode = _settings.Runtime.SafeInputMode;
+            await _surfaces.ApplyEditModeAsync(_isEditMode, cancellationToken).ConfigureAwait(false);
+            await _surfaces.ApplySafeInputModeAsync(_isSafeInputMode, cancellationToken).ConfigureAwait(false);
 
             await _layout.LoadAsync(cancellationToken).ConfigureAwait(false);
             ReconcileCurrentMonitors();
@@ -57,7 +59,7 @@
             _pluginSnapshot = await _plugins.ScanAsync(cancellationToken).ConfigureAwait(false);
             foreach (var layout in _layout.GetAll().Where(layout => layout.IsVisible))
             {
-                await _surfaces.ApplyWidgetLayoutAsync(layout, cancellationToken).ConfigureAwait(false);
+                await _surfaces.ApplyWidgetLayoutAsync(ToSurfaceLayout(layout), cancellationToken).ConfigureAwait(false);
             }
 
             _diagnostics.Record(DiagnosticSeverity.Info, DiagnosticCodes.ApplicationStarted, "Desktop runtime started.");
@@ -114,6 +116,7 @@
         try
         {
             _isGlobalLocked = enabled;
+            await ApplyAllWidgetLayoutsAsync(cancellationToken).ConfigureAwait(false);
             var state = BuildState();
             await _events.PublishAsync(new RuntimeEvent { Name = "app.globalLockedChanged", Payload = state }, cancellationToken).ConfigureAwait(false);
             await PublishStatusAsync(state, cancellationToken).ConfigureAwait(false);
@@ -138,6 +141,7 @@
         try
         {
             _pluginSnapshot = await _plugins.ScanAsync(cancellationToken).ConfigureAwait(false);
+            await ApplyAllWidgetLayoutsAsync(cancellationToken).ConfigureAwait(false);
             var state = BuildState();
             await _events.PublishAsync(new RuntimeEvent { Name = "plugin.reloaded", Payload = state.Plugins }, cancellationToken).ConfigureAwait(false);
             await PublishStatusAsync(state, cancellationToken).ConfigureAwait(false);
@@ -217,7 +221,7 @@
                 return OperationResult<WidgetRuntimeState>.Failure(result.ErrorCode ?? "WidgetLayoutUpdateFailed", result.Message ?? "Widget layout update failed.");
             }
 
-            await _surfaces.ApplyWidgetLayoutAsync(result.Value, cancellationToken).ConfigureAwait(false);
+            await _surfaces.ApplyWidgetLayoutAsync(ToSurfaceLayout(result.Value), cancellationToken).ConfigureAwait(false);
             await _layout.SaveAsync(cancellationToken).ConfigureAwait(false);
             var runtimeState = ToWidgetRuntimeState(result.Value);
             await _events.PublishAsync(new RuntimeEvent { Name = "widget.layoutChanged", Payload = runtimeState }, cancellationToken).ConfigureAwait(false);
@@ -272,6 +276,15 @@
         await _configuration.SaveAppSettingsAsync(_settings, cancellationToken).ConfigureAwait(false);
     }
 
+    private async Task ApplyAllWidgetLayoutsAsync(CancellationToken cancellationToken)
+    {
+        foreach (var layout in _layout.GetAll())
+        {
+            cancellationToken.ThrowIfCancellationRequested();
+            await _surfaces.ApplyWidgetLayoutAsync(ToSurfaceLayout(layout), cancellationToken).ConfigureAwait(false);
+        }
+    }
+
     private AppRuntimeState BuildState()
     {
         return new AppRuntimeState
@@ -317,6 +330,9 @@
         ClickThrough = _isEditMode || _isSafeInputMode ? false : layout.ClickThrough
     };
 
+    private WidgetLayout ToSurfaceLayout(WidgetLayout layout)
+        => _isGlobalLocked ? layout with { IsLocked = true } : layout;
+
     private static bool IsGeometryPatch(WidgetLayoutPatch patch)
         => patch.Bounds is not null || patch.MonitorId is not null || patch.Scale is not null;
 }
diff --git a/src/Moli.Desktop/Runtime/IRuntimeSurfaceController.cs b/src/Moli.Desktop/Runtime/IRuntimeSurfaceController.cs
index 3b0074a..4acbae1 100644
--- a/src/Moli.Desktop/Runtime/IRuntimeSurfaceController.cs
+++ b/src/Moli.Desktop/Runtime/IRuntimeSurfaceController.cs
@@ -12,18 +12,3 @@
     Task DisableWallpaperAsync(CancellationToken cancellationToken = default);
     Task ShutdownAsync(CancellationToken cancellationToken = default);
 }
-
-public sealed class NullRuntimeSurfaceController : IRuntimeSurfaceController
-{
-    public Task ApplyEditModeAsync(bool enabled, CancellationToken cancellationToken = default) => Task.CompletedTask;
-
-    public Task ApplySafeInputModeAsync(bool enabled, CancellationToken cancellationToken = default) => Task.CompletedTask;
-
-    public Task ApplyWidgetLayoutAsync(WidgetLayout layout, CancellationToken cancellationToken = default) => Task.CompletedTask;
-
-    public Task EnableWallpaperAsync(WallpaperRuntimeState wallpaper, CancellationToken cancellationToken = default) => Task.CompletedTask;
-
-    public Task DisableWallpaperAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
-
-    public Task ShutdownAsync(CancellationToken cancellationToken = default) => Task.CompletedTask;
-}

--
Gitblit v1.9.1