| | |
| | | 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) |
| | |
| | | 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) |
| | |
| | | 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 OperationResult<WidgetLayout> Mutate(WidgetInstanceId instanceId, Func<WidgetLayout, WidgetLayout> update) |
| | | private OperationResult<WidgetLayout> Mutate(WidgetInstanceId instanceId, Func<WidgetLayout, WidgetLayout> update, bool allowLocked = false) |
| | | { |
| | | lock (_gate) |
| | | { |
| | |
| | | } |
| | | |
| | | var current = _document.Instances[index]; |
| | | if (current.IsLocked) |
| | | if (current.IsLocked && !allowLocked) |
| | | { |
| | | return OperationResult<WidgetLayout>.Failure("WidgetLayoutLocked", $"Widget layout {instanceId} is locked."); |
| | | } |
| | |
| | | }; |
| | | } |
| | | |
| | | 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; |
| | | } |