From 574a6a769115034f37bdecf3284545d74843816a Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期三, 12 八月 2026 15:21:41 +0800
Subject: [PATCH] 添加显示器切换兼容处理

---
 src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs |   55 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs b/src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs
index 8f9d094..76e3087 100644
--- a/src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs
+++ b/src/Moli.Desktop/Runtime/DesktopRuntimeCoordinator.cs
@@ -15,6 +15,7 @@
     private readonly IDiagnosticsService _diagnostics;
     private readonly IRuntimeEventBus _events;
     private readonly IRuntimeSurfaceController _surfaces;
+    private readonly DisplayChangeMonitor _displayMonitor;
     private readonly SemaphoreSlim _lock = new(1, 1);
     private AppSettingsDocument _settings = new();
     private IReadOnlyList<PluginDescriptor> _pluginSnapshot = [];
@@ -30,7 +31,8 @@
         IWin32InteropService interop,
         IDiagnosticsService diagnostics,
         IRuntimeEventBus events,
-        IRuntimeSurfaceController surfaces)
+        IRuntimeSurfaceController surfaces,
+        DisplayChangeMonitor displayMonitor)
     {
         _configuration = configuration;
         _plugins = plugins;
@@ -39,6 +41,7 @@
         _diagnostics = diagnostics;
         _events = events;
         _surfaces = surfaces;
+        _displayMonitor = displayMonitor;
     }
 
     public async Task<OperationResult<AppRuntimeState>> StartAsync(CancellationToken cancellationToken = default)
@@ -49,12 +52,22 @@
             _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();
             await _layout.SaveAsync(cancellationToken).ConfigureAwait(false);
 
             _pluginSnapshot = await _plugins.ScanAsync(cancellationToken).ConfigureAwait(false);
+            foreach (var layout in _layout.GetAll().Where(layout => layout.IsVisible))
+            {
+                await _surfaces.ApplyWidgetLayoutAsync(ToSurfaceLayout(layout), cancellationToken).ConfigureAwait(false);
+            }
+
+            _displayMonitor.DisplayChanged += () => OnDisplayTopologyChangedAsync();
+            _displayMonitor.Start();
+
             _diagnostics.Record(DiagnosticSeverity.Info, DiagnosticCodes.ApplicationStarted, "Desktop runtime started.");
             var state = BuildState();
             await PublishStatusAsync(state, cancellationToken).ConfigureAwait(false);
@@ -109,6 +122,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);
@@ -133,6 +147,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);
@@ -212,12 +227,33 @@
                 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);
             await PublishStatusAsync(BuildState(), cancellationToken).ConfigureAwait(false);
             return OperationResult<WidgetRuntimeState>.Success(runtimeState);
+        }
+        finally
+        {
+            _lock.Release();
+        }
+    }
+
+    public async Task OnDisplayTopologyChangedAsync(CancellationToken cancellationToken = default)
+    {
+        await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);
+        try
+        {
+            ReconcileCurrentMonitors();
+            await _layout.SaveAsync(cancellationToken).ConfigureAwait(false);
+            await _surfaces.SyncAllLayoutsAsync(cancellationToken).ConfigureAwait(false);
+            await _surfaces.SyncWallpaperMonitorsAsync(cancellationToken).ConfigureAwait(false);
+            await _surfaces.ReattachDesktopLayersAsync(cancellationToken).ConfigureAwait(false);
+
+            var state = BuildState();
+            await _events.PublishAsync(new RuntimeEvent { Name = "display.topologyChanged", Payload = state }, cancellationToken).ConfigureAwait(false);
+            await PublishStatusAsync(state, cancellationToken).ConfigureAwait(false);
         }
         finally
         {
@@ -267,6 +303,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
@@ -309,9 +354,13 @@
         IsVisible = layout.IsVisible,
         IsLocked = _isGlobalLocked || layout.IsLocked,
         Opacity = layout.Opacity,
-        ClickThrough = _isEditMode || _isSafeInputMode ? false : layout.ClickThrough
+        ClickThrough = _isEditMode || _isSafeInputMode ? false : layout.ClickThrough,
+        DisplayLayer = layout.DisplayLayer
     };
 
+    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;
 }

--
Gitblit v1.9.1