From 97df0fb5ed49e37d1c8e0e62057bb72ba5c9eb6a Mon Sep 17 00:00:00 2001
From: chunhualin1 <chunhualin1@lenovo.com>
Date: 星期三, 12 八月 2026 17:24:49 +0800
Subject: [PATCH] 优化透明样式
---
src/Moli.Desktop/Layout/LayoutManager.cs | 95 +++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 86 insertions(+), 9 deletions(-)
diff --git a/src/Moli.Desktop/Layout/LayoutManager.cs b/src/Moli.Desktop/Layout/LayoutManager.cs
index 5485c25..d1c42a1 100644
--- a/src/Moli.Desktop/Layout/LayoutManager.cs
+++ b/src/Moli.Desktop/Layout/LayoutManager.cs
@@ -83,7 +83,7 @@
ClickThrough = plugin.Manifest.DefaultLayout.ClickThrough,
Opacity = Clamp(plugin.Manifest.DefaultLayout.Opacity, MinOpacity, MaxOpacity),
Scale = Clamp(plugin.Manifest.DefaultLayout.Scale, MinScale, MaxScale),
- DisplayPriority = plugin.Manifest.DefaultLayout.DisplayPriority
+ DisplayLayer = plugin.Manifest.DefaultLayout.DisplayLayer
};
lock (_gate)
@@ -121,8 +121,8 @@
Opacity = patch.Opacity ?? layout.Opacity,
ClickThrough = patch.ClickThrough ?? layout.ClickThrough,
Scale = patch.Scale ?? layout.Scale,
- DisplayPriority = patch.DisplayPriority ?? layout.DisplayPriority
- });
+ DisplayLayer = patch.DisplayLayer ?? layout.DisplayLayer
+ }, allowLocked: IsUnlockPatch(patch));
}
public IReadOnlyList<WidgetLayout> ReconcileMonitors(IReadOnlyList<MonitorInfo> monitors)
@@ -142,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,
@@ -157,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)
{
@@ -179,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.");
}
@@ -254,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