From 9b5711d3124622b1f1b18500d442c76492da877a Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期一, 17 八月 2026 16:52:33 +0800
Subject: [PATCH] 优化llm插件及bug修复

---
 src/Moli.Desktop/Layout/LayoutManager.cs |   97 ++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 88 insertions(+), 9 deletions(-)

diff --git a/src/Moli.Desktop/Layout/LayoutManager.cs b/src/Moli.Desktop/Layout/LayoutManager.cs
index b27014b..d1c42a1 100644
--- a/src/Moli.Desktop/Layout/LayoutManager.cs
+++ b/src/Moli.Desktop/Layout/LayoutManager.cs
@@ -82,7 +82,8 @@
             IsLocked = plugin.Manifest.DefaultLayout.Locked,
             ClickThrough = plugin.Manifest.DefaultLayout.ClickThrough,
             Opacity = Clamp(plugin.Manifest.DefaultLayout.Opacity, MinOpacity, MaxOpacity),
-            Scale = Clamp(plugin.Manifest.DefaultLayout.Scale, MinScale, MaxScale)
+            Scale = Clamp(plugin.Manifest.DefaultLayout.Scale, MinScale, MaxScale),
+            DisplayLayer = plugin.Manifest.DefaultLayout.DisplayLayer
         };
 
         lock (_gate)
@@ -119,8 +120,9 @@
             IsLocked = patch.IsLocked ?? layout.IsLocked,
             Opacity = patch.Opacity ?? layout.Opacity,
             ClickThrough = patch.ClickThrough ?? layout.ClickThrough,
-            Scale = patch.Scale ?? layout.Scale
-        });
+            Scale = patch.Scale ?? layout.Scale,
+            DisplayLayer = patch.DisplayLayer ?? layout.DisplayLayer
+        }, allowLocked: IsUnlockPatch(patch));
     }
 
     public IReadOnlyList<WidgetLayout> ReconcileMonitors(IReadOnlyList<MonitorInfo> monitors)
@@ -140,12 +142,12 @@
                 var monitor = byId.GetValueOrDefault(layout.MonitorId);
                 if (monitor is null)
                 {
-                    var centered = CenterInMonitor(layout, primary);
-                    _document.Instances[i] = centered;
+                    var migrated = MigrateToPrimary(layout, primary);
+                    _document.Instances[i] = migrated;
                     _diagnostics.Record(
                         DiagnosticSeverity.Info,
                         "LayoutReconciled",
-                        "Widget layout moved to primary monitor after monitor topology changed.",
+                        "Widget layout migrated to primary monitor after monitor topology changed.",
                         new Dictionary<string, string>
                         {
                             ["instanceId"] = layout.InstanceId.Value,
@@ -155,18 +157,29 @@
                     continue;
                 }
 
-                var constrained = Constrain(layout, monitor);
+                var scaled = ScaleToWorkArea(layout, monitor);
+                var constrained = Constrain(scaled, monitor);
                 if (constrained != layout)
                 {
                     _document.Instances[i] = constrained;
                 }
             }
 
+            _document.Monitors.Add(new MonitorSnapshot
+            {
+                CapturedAt = DateTimeOffset.UtcNow,
+                Monitors = [.. monitors]
+            });
+            while (_document.Monitors.Count > 5)
+            {
+                _document.Monitors.RemoveAt(0);
+            }
+
             return [.. _document.Instances];
         }
     }
 
-    private OperationResult<WidgetLayout> Mutate(WidgetInstanceId instanceId, Func<WidgetLayout, WidgetLayout> update)
+    private OperationResult<WidgetLayout> Mutate(WidgetInstanceId instanceId, Func<WidgetLayout, WidgetLayout> update, bool allowLocked = false)
     {
         lock (_gate)
         {
@@ -177,7 +190,7 @@
             }
 
             var current = _document.Instances[index];
-            if (current.IsLocked)
+            if (current.IsLocked && !allowLocked)
             {
                 return OperationResult<WidgetLayout>.Failure("WidgetLayoutLocked", $"Widget layout {instanceId} is locked.");
             }
@@ -252,6 +265,72 @@
         };
     }
 
+    private WidgetLayout MigrateToPrimary(WidgetLayout layout, MonitorInfo primary)
+    {
+        var previousPrimary = _document.Monitors
+            .OrderByDescending(snapshot => snapshot.CapturedAt)
+            .SelectMany(snapshot => snapshot.Monitors)
+            .FirstOrDefault(m => m.IsPrimary);
+
+        if (previousPrimary is null || previousPrimary.WorkArea.Width <= 0 || previousPrimary.WorkArea.Height <= 0)
+        {
+            return layout with { MonitorId = primary.Id };
+        }
+
+        var scaleX = primary.WorkArea.Width / previousPrimary.WorkArea.Width;
+        var scaleY = primary.WorkArea.Height / previousPrimary.WorkArea.Height;
+        var relX = (layout.Bounds.X - previousPrimary.WorkArea.X) / previousPrimary.WorkArea.Width;
+        var relY = (layout.Bounds.Y - previousPrimary.WorkArea.Y) / previousPrimary.WorkArea.Height;
+
+        return ApplyScalarConstraints(layout with
+        {
+            MonitorId = primary.Id,
+            Bounds = new RectD(
+                primary.WorkArea.X + relX * primary.WorkArea.Width,
+                primary.WorkArea.Y + relY * primary.WorkArea.Height,
+                layout.Bounds.Width * scaleX,
+                layout.Bounds.Height * scaleY)
+        });
+    }
+
+    private WidgetLayout ScaleToWorkArea(WidgetLayout layout, MonitorInfo current)
+    {
+        var previous = _document.Monitors
+            .OrderByDescending(snapshot => snapshot.CapturedAt)
+            .SelectMany(snapshot => snapshot.Monitors)
+            .FirstOrDefault(m => m.Id == current.Id);
+
+        if (previous is null || previous.WorkArea == current.WorkArea ||
+            previous.WorkArea.Width <= 0 || previous.WorkArea.Height <= 0)
+        {
+            return layout;
+        }
+
+        var scaleX = current.WorkArea.Width / previous.WorkArea.Width;
+        var scaleY = current.WorkArea.Height / previous.WorkArea.Height;
+        var relX = (layout.Bounds.X - previous.WorkArea.X) / previous.WorkArea.Width;
+        var relY = (layout.Bounds.Y - previous.WorkArea.Y) / previous.WorkArea.Height;
+
+        return layout with
+        {
+            Bounds = new RectD(
+                current.WorkArea.X + relX * current.WorkArea.Width,
+                current.WorkArea.Y + relY * current.WorkArea.Height,
+                layout.Bounds.Width * scaleX,
+                layout.Bounds.Height * scaleY)
+        };
+    }
+
     private static double Clamp(double value, double min, double max)
         => Math.Min(Math.Max(value, min), max);
+
+    private static bool IsUnlockPatch(WidgetLayoutPatch patch)
+        => patch.IsLocked == false &&
+           patch.Bounds is null &&
+           patch.MonitorId is null &&
+           patch.IsVisible is null &&
+           patch.Opacity is null &&
+           patch.ClickThrough is null &&
+           patch.Scale is null &&
+           patch.DisplayLayer is null;
 }

--
Gitblit v1.9.1