Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions src/Wisp.App/OverlayWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -448,17 +455,23 @@ 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
{
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,
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
32 changes: 23 additions & 9 deletions tests/Wisp.App.Tests/OverlayGForcePlacementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
{
Expand Down Expand Up @@ -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<FrameworkElement>(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);
Expand All @@ -188,7 +203,6 @@ void AssertTogglesStayPut()
Assert.Equal(bounds, WindowPixelBounds(window));
Flush(window);
Assert.Equal(bounds, WindowPixelBounds(window));
var meter = Assert.IsAssignableFrom<FrameworkElement>(window.FindName("AttachedNativeGForce"));
Assert.Equal(enabled ? Visibility.Visible : Visibility.Collapsed, meter.Visibility);
}
}
Expand Down