基于WebView2实现的Win11桌面框架,利用插件自定义
chunhualin1
2026-08-12 574a6a769115034f37bdecf3284545d74843816a
src/Moli.Desktop/Layout/LayoutManager.cs
@@ -122,7 +122,7 @@
            ClickThrough = patch.ClickThrough ?? layout.ClickThrough,
            Scale = patch.Scale ?? layout.Scale,
            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;
}