From 7df3d780737dc21e670fdfba6d6a30dd81441e23 Mon Sep 17 00:00:00 2001 From: Views2k <292889387+Views2k@users.noreply.github.com> Date: Wed, 2 Dec 2026 21:58:44 -0600 Subject: [PATCH] Keep visible native HUD gauges against the chosen monitor edge --- src/Wisp.App/OverlayWindow.xaml.cs | 46 +++++++++++++------ .../OverlayGForcePlacementTests.cs | 32 +++++++++---- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/src/Wisp.App/OverlayWindow.xaml.cs b/src/Wisp.App/OverlayWindow.xaml.cs index aecb7e8e..4be62631 100644 --- a/src/Wisp.App/OverlayWindow.xaml.cs +++ b/src/Wisp.App/OverlayWindow.xaml.cs @@ -46,6 +46,7 @@ protected override void OnClosed(EventArgs e) private const double NativeElectricAnalogHeight = 345; private const double AttachedGForceTopPadding = 72; private double _nativeTopPadding = AttachedGForceTopPadding; + private Rect _visiblePlacementBounds; private double _gForceScale = 1; private readonly AppController _controller; @@ -403,18 +404,24 @@ public void ApplyLayout( HudLayoutMode.SeparateBoxes => (BoxedWidth, BoxedHeight), HudLayoutMode.Native when _isElectricPowertrain && nativeGaugeMode == NativeGaugeMode.Analogue => (NativeElectricAnalogWidth, NativeElectricAnalogHeight), - HudLayoutMode.Native when _isElectricPowertrain => - (NativeElectricDigitalWidth, - digitalTireTemperatureVisible ? NativeDigitalTireHeight : NativeElectricDigitalHeight), + HudLayoutMode.Native when _isElectricPowertrain => (NativeElectricDigitalWidth, NativeElectricDigitalHeight), HudLayoutMode.Native when nativeGaugeMode == NativeGaugeMode.Analogue => - (analogBoostVisible || analogTireTemperatureVisible ? NativeAnalogBoostWidth : NativeAnalogWidth, - NativeAnalogHeight), - HudLayoutMode.Native => (NativeDigitalWidth, - digitalTireTemperatureVisible - ? digitalBoostVisible ? NativeDigitalBoostTireHeight : NativeDigitalTireHeight - : digitalBoostVisible ? NativeDigitalBoostHeight : NativeDigitalHeight), + (NativeAnalogWidth, NativeAnalogHeight), + HudLayoutMode.Native => (NativeDigitalWidth, NativeDigitalHeight), _ => (MinimalWidth, MinimalHeight) }; + var visibleBounds = new Rect(0, nativeTop, baseWidth, baseHeight); + if (attachedGForceVisible) visibleBounds.Union(gForceBounds); + if (!_isElectricPowertrain && (analogBoostVisible || analogTireTemperatureVisible)) + baseWidth = NativeAnalogBoostWidth; + if (digitalBoostVisible) + { + visibleBounds.Union(new Rect(AttachedDigitalBoost.Margin.Left, AttachedDigitalBoost.Margin.Top, + AttachedDigitalBoost.Width, AttachedDigitalBoost.Height)); + baseHeight = NativeDigitalBoostHeight; + } + if (digitalTireTemperatureVisible) + baseHeight = digitalBoostVisible ? NativeDigitalBoostTireHeight : NativeDigitalTireHeight; baseHeight += nativeTop; if (layoutMode == HudLayoutMode.Native) baseWidth = Math.Max(baseWidth, gForceBounds.Right); @@ -448,6 +455,10 @@ public void ApplyLayout( ApplyAnalogGaugeBounds(AttachedAnalogTireTemperature, gauges.TireBounds); ApplyPowerTorqueBounds(AttachedPowerGauge, gauges.PowerBounds); ApplyPowerTorqueBounds(AttachedTorqueGauge, gauges.TorqueBounds); + visibleBounds.Union(gauges.BoostBounds); + visibleBounds.Union(gauges.TireBounds); + visibleBounds.Union(gauges.PowerBounds); + visibleBounds.Union(gauges.TorqueBounds); size = gauges.Size; } else @@ -455,10 +466,12 @@ public void ApplyLayout( if (digitalTireTemperatureVisible) { AttachedDigitalTireTemperature.LayoutTransform = new ScaleTransform(_tireScale, _tireScale); - baseWidth = Math.Max(baseWidth, - AttachedDigitalTireTemperature.Margin.Left + AttachedDigitalTireTemperature.Width * _tireScale + 4); - baseHeight = Math.Max(baseHeight, - AttachedDigitalTireTemperature.Margin.Top + AttachedDigitalTireTemperature.Height * _tireScale + 4); + var tireBounds = new Rect(AttachedDigitalTireTemperature.Margin.Left, + AttachedDigitalTireTemperature.Margin.Top, AttachedDigitalTireTemperature.Width * _tireScale, + AttachedDigitalTireTemperature.Height * _tireScale); + visibleBounds.Union(tireBounds); + baseWidth = Math.Max(baseWidth, tireBounds.Right + 4); + baseHeight = Math.Max(baseHeight, tireBounds.Bottom + 4); } var gauges = PowerTorqueGaugeLayout.Calculate( new Size(baseWidth, baseHeight), nativeTop + 4, @@ -471,6 +484,8 @@ public void ApplyLayout( RootPanel.Height = size.Height; Width = size.Width * widthScale; Height = size.Height * heightScale; + visibleBounds.Scale(widthScale, heightScale); + _visiblePlacementBounds = visibleBounds; EditChrome.Visibility = _editMode && layoutMode != HudLayoutMode.Minimal ? Visibility.Visible : Visibility.Collapsed; @@ -575,8 +590,11 @@ public void RestorePosition(double left, double top) _ = new WindowInteropHelper(this).EnsureHandle(); Left = left; Top = top; + // Transparent space reserved for a hidden gauge may extend past the monitor. + var monitor = CurrentMonitorBounds(); + monitor.Offset(-_visiblePlacementBounds.X, -_visiblePlacementBounds.Y); var position = OverlayPlacementGeometry.ClampOnScreen( - CurrentMonitorBounds(), new Size(Width, Height), new Point(left, top)); + monitor, _visiblePlacementBounds.Size, new Point(left, top)); Left = position.X; Top = position.Y; } diff --git a/tests/Wisp.App.Tests/OverlayGForcePlacementTests.cs b/tests/Wisp.App.Tests/OverlayGForcePlacementTests.cs index 9f500f5d..57ff3525 100644 --- a/tests/Wisp.App.Tests/OverlayGForcePlacementTests.cs +++ b/tests/Wisp.App.Tests/OverlayGForcePlacementTests.cs @@ -117,17 +117,24 @@ private static void AssertPlacement(NativeGaugeMode mode, bool electric) VisualTreeHelper.GetDpi(window).DpiScaleY); AssertLogicalPoint(window, expected, logicalBounds.TopLeft); - // Restoring a legacy top-edge position clamps the full fixed surface once; - // subsequent toggles must not move it or hide any part of the enabled meter. + // Saved coordinates exclude the reserved strip above the dial when the meter is hidden. window.RestorePlacementPosition(area.Left, area.Top); Flush(window); - AssertLogicalPoint(window, area.TopLeft, new Point(window.Left, window.Top)); - AssertTogglesStayPut(); + AssertLogicalPoint(window, area.TopLeft, window.GetPlacementBounds().TopLeft); + var atEdge = WindowPixelBounds(window); + controller.SaveOverlayPlacement(); + window.RestorePosition(area.Left + 160, area.Top + 160); + controller.RestoreOverlayPlacement(); + Flush(window); + Assert.Equal(atEdge, WindowPixelBounds(window)); + AssertMeterFitsWhenEnabled(); + + // The ICE analogue dial is narrower than the transparent space reserved for G-force. window.RestorePosition(area.Right, area.Bottom); Flush(window); - Assert.InRange(window.Left + window.Width, area.Left, area.Right + 1); - Assert.InRange(window.Top + window.Height, area.Top, area.Bottom + 1); - AssertTogglesStayPut(); + var dialWidth = mode == NativeGaugeMode.Digital ? 327.5 : electric ? 345 : 293; + AssertLogicalPoint(window, area.BottomRight, new Point(window.Left + dialWidth * scale, window.Top + window.Height)); + AssertMeterFitsWhenEnabled(); foreach (var enabled in new[] { false, true }) { @@ -176,8 +183,16 @@ private static void AssertPlacement(NativeGaugeMode mode, bool electric) Assert.NotEqual(0, style & OverlayActivationPolicy.TransparentExtendedStyle); Assert.NotEqual(0, style & OverlayActivationPolicy.NoActivateExtendedStyle); - void AssertTogglesStayPut() + void AssertMeterFitsWhenEnabled() { + SetEnabled(true); + Flush(window); + var meter = Assert.IsAssignableFrom(window.FindName("AttachedNativeGForce")); + var visible = meter.TransformToAncestor(window).TransformBounds(new Rect(meter.RenderSize)); + visible.Offset(window.Left, window.Top); + var tolerance = area; + tolerance.Inflate(1 / VisualTreeHelper.GetDpi(window).DpiScaleX, 1 / VisualTreeHelper.GetDpi(window).DpiScaleY); + Assert.True(tolerance.Contains(visible), $"The attached meter {visible} must fit on {area}."); // WPF may briefly report the requested fractional DIP size before // reflecting HWND rounding. Actual pixels must never move or resize. var bounds = WindowPixelBounds(window); @@ -188,7 +203,6 @@ void AssertTogglesStayPut() Assert.Equal(bounds, WindowPixelBounds(window)); Flush(window); Assert.Equal(bounds, WindowPixelBounds(window)); - var meter = Assert.IsAssignableFrom(window.FindName("AttachedNativeGForce")); Assert.Equal(enabled ? Visibility.Visible : Visibility.Collapsed, meter.Visibility); } }