diff --git a/Helpers/WindowPlacementHelper.cs b/Helpers/WindowPlacementHelper.cs index 335ed0e..4a39681 100644 --- a/Helpers/WindowPlacementHelper.cs +++ b/Helpers/WindowPlacementHelper.cs @@ -34,11 +34,17 @@ namespace ThreadPilot.Helpers private const double DefaultWindowWidth = 1280; private const double DefaultWindowHeight = 864; private const double MinimumVisibleAreaRatio = 0.25; + + // A window this close to filling its monitor's working area is treated as having no + // usable windowed bounds (e.g. restored from maximised without a smaller size to go back to). + private const double FillsWorkingAreaRatio = 0.98; + private const double FallbackWorkingAreaRatio = 0.9; private const double Epsilon = 0.001; public static WindowPlacementCorrection CorrectWindowBounds( WindowBounds currentBounds, - IReadOnlyCollection workingAreas) + IReadOnlyCollection workingAreas, + bool shrinkWhenFillingWorkingArea = false) { var validWorkingAreas = workingAreas .Where(IsValidWorkingArea) @@ -56,6 +62,20 @@ namespace ThreadPilot.Helpers } var targetArea = SelectTargetWorkingArea(currentBounds, validWorkingAreas); + + // The target monitor is resolved from the original bounds first, so the fallback size + // stays centred on the monitor the window was already on. + if (shrinkWhenFillingWorkingArea && FillsWorkingArea(currentBounds, targetArea)) + { + currentBounds = currentBounds with + { + Left = double.NaN, + Top = double.NaN, + Width = Math.Min(DefaultWindowWidth, targetArea.Width * FallbackWorkingAreaRatio), + Height = Math.Min(DefaultWindowHeight, targetArea.Height * FallbackWorkingAreaRatio), + }; + } + var correctedWidth = Math.Min(ResolveDimension(currentBounds.Width, DefaultWindowWidth), targetArea.Width); var correctedHeight = Math.Min(ResolveDimension(currentBounds.Height, DefaultWindowHeight), targetArea.Height); var sanitizedBounds = new WindowBounds( @@ -88,7 +108,7 @@ namespace ThreadPilot.Helpers return new WindowPlacementCorrection(correctedBounds, !BoundsAreEquivalent(currentBounds, correctedBounds)); } - public static bool TryCorrectWindowPlacement(Window window) + public static bool TryCorrectWindowPlacement(Window window, bool shrinkWhenFillingWorkingArea = false) { ArgumentNullException.ThrowIfNull(window); @@ -106,7 +126,7 @@ namespace ThreadPilot.Helpers ResolveWindowDimension(window.ActualWidth, window.Width, DefaultWindowWidth), ResolveWindowDimension(window.ActualHeight, window.Height, DefaultWindowHeight)); - var correction = CorrectWindowBounds(currentBounds, workingAreas); + var correction = CorrectWindowBounds(currentBounds, workingAreas, shrinkWhenFillingWorkingArea); if (!correction.WasCorrected) { return false; @@ -219,6 +239,14 @@ namespace ThreadPilot.Helpers return (deltaX * deltaX) + (deltaY * deltaY); } + private static bool FillsWorkingArea(WindowBounds bounds, MonitorWorkingArea area) + { + return IsFinite(bounds.Width) + && IsFinite(bounds.Height) + && bounds.Width >= area.Width * FillsWorkingAreaRatio + && bounds.Height >= area.Height * FillsWorkingAreaRatio; + } + private static bool HasSufficientIntersection(WindowBounds bounds, MonitorWorkingArea area) { var intersectionArea = GetIntersectionArea(bounds, area); diff --git a/MainWindow.Behaviors.partial.cs b/MainWindow.Behaviors.partial.cs index b3137bf..c8876a3 100644 --- a/MainWindow.Behaviors.partial.cs +++ b/MainWindow.Behaviors.partial.cs @@ -1400,8 +1400,13 @@ namespace ThreadPilot } } + private WindowState lastObservedWindowState = WindowState.Normal; + protected override void OnStateChanged(EventArgs e) { + var previousWindowState = this.lastObservedWindowState; + this.lastObservedWindowState = this.WindowState; + try { if (this.WindowState == WindowState.Minimized) @@ -1420,7 +1425,15 @@ namespace ThreadPilot else if (this.WindowState == WindowState.Normal || this.WindowState == WindowState.Maximized) { this.ShowInTaskbar = true; - this.EnsureDashboardVisibleOnScreen(); + + // While StateChanged runs, ActualWidth/ActualHeight/Left/Top still describe the + // pre-transition window, so correcting placement here would write the maximised + // size back into Width/Height and destroy the restore bounds. Let it settle first. + var restoredFromMaximized = this.WindowState == WindowState.Normal + && previousWindowState == WindowState.Maximized; + _ = this.Dispatcher.BeginInvoke( + new Action(() => this.EnsureDashboardVisibleOnScreen(restoredFromMaximized)), + System.Windows.Threading.DispatcherPriority.Background); this.ApplyAppRefreshPolicy(this.GetForegroundActivityState()); } @@ -1625,10 +1638,11 @@ namespace ThreadPilot if (this.WindowState == WindowState.Minimized) { + // OnStateChanged schedules the placement correction once the restore has settled; + // doing it here would read the pre-restore metrics. this.WindowState = WindowState.Normal; } - this.EnsureDashboardVisibleOnScreen(); this.ShowInTaskbar = true; if (tabTag != null) @@ -1653,9 +1667,9 @@ namespace ThreadPilot : AppActivityState.ForegroundOtherTab); } - internal bool EnsureDashboardVisibleOnScreen() + internal bool EnsureDashboardVisibleOnScreen(bool shrinkWhenFillingWorkingArea = false) { - return WindowPlacementHelper.TryCorrectWindowPlacement(this); + return WindowPlacementHelper.TryCorrectWindowPlacement(this, shrinkWhenFillingWorkingArea); } private void SelectMainTab(string tag) diff --git a/Tests/ThreadPilot.Core.Tests/WindowPlacementHelperTests.cs b/Tests/ThreadPilot.Core.Tests/WindowPlacementHelperTests.cs index 841b524..76746a3 100644 --- a/Tests/ThreadPilot.Core.Tests/WindowPlacementHelperTests.cs +++ b/Tests/ThreadPilot.Core.Tests/WindowPlacementHelperTests.cs @@ -63,6 +63,85 @@ namespace ThreadPilot.Core.Tests Assert.Equal(864, result.Bounds.Height); } + [Fact] + public void CorrectWindowBounds_WhenFillingWorkingAreaAndShrinkRequested_CentersDefaultSizeOnSameMonitor() + { + var monitors = new[] + { + new MonitorWorkingArea(0, 0, 1920, 1040, true), + new MonitorWorkingArea(1920, 0, 2560, 1400, false), + }; + + // Restored from maximised on the secondary monitor with no smaller windowed size to return to. + var result = WindowPlacementHelper.CorrectWindowBounds( + new WindowBounds(1920, 0, 2560, 1400), + monitors, + shrinkWhenFillingWorkingArea: true); + + Assert.True(result.WasCorrected); + Assert.Equal(1280, result.Bounds.Width); + Assert.Equal(864, result.Bounds.Height); + Assert.Equal(1920 + ((2560 - 1280) / 2), result.Bounds.Left); + Assert.Equal((1400 - 864) / 2, result.Bounds.Top); + } + + [Fact] + public void CorrectWindowBounds_WhenFillingSmallWorkingAreaAndShrinkRequested_StaysClearlySmallerThanWorkingArea() + { + var monitors = new[] + { + new MonitorWorkingArea(0, 0, 1366, 728, true), + }; + + var result = WindowPlacementHelper.CorrectWindowBounds( + new WindowBounds(0, 0, 1366, 728), + monitors, + shrinkWhenFillingWorkingArea: true); + + Assert.True(result.WasCorrected); + Assert.True(result.Bounds.Width < 1366); + Assert.True(result.Bounds.Height < 728); + Assert.Equal(1229, result.Bounds.Width); + Assert.Equal(655, result.Bounds.Height); + } + + [Fact] + public void CorrectWindowBounds_WhenFillingWorkingAreaAndShrinkNotRequested_KeepsSize() + { + var monitors = new[] + { + new MonitorWorkingArea(0, 0, 1920, 1040, true), + }; + + var result = WindowPlacementHelper.CorrectWindowBounds( + new WindowBounds(0, 0, 1920, 1040), + monitors); + + Assert.False(result.WasCorrected); + Assert.Equal(1920, result.Bounds.Width); + Assert.Equal(1040, result.Bounds.Height); + } + + [Fact] + public void CorrectWindowBounds_WhenSmallerThanWorkingAreaAndShrinkRequested_KeepsWindowedBounds() + { + var monitors = new[] + { + new MonitorWorkingArea(0, 0, 1920, 1040, true), + }; + + var result = WindowPlacementHelper.CorrectWindowBounds( + new WindowBounds(300, 200, 1100, 700), + monitors, + shrinkWhenFillingWorkingArea: true); + + Assert.False(result.WasCorrected); + Assert.Equal(300, result.Bounds.Left); + Assert.Equal(200, result.Bounds.Top); + Assert.Equal(1100, result.Bounds.Width); + Assert.Equal(700, result.Bounds.Height); + } + [Fact] public void CorrectWindowBounds_WhenAlreadyVisibleOnSecondaryMonitor_DoesNotMoveWindow() {