| | |
| | | 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, |
| | |
| | | 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 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); |
| | | |