diff --git a/src/BmsConfig/MainWindow.xaml b/src/BmsConfig/MainWindow.xaml index d53e372..0f674e7 100644 --- a/src/BmsConfig/MainWindow.xaml +++ b/src/BmsConfig/MainWindow.xaml @@ -3,7 +3,6 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Falcon BMS Config Editor" Width="1600" Height="1000" MinWidth="1000" MinHeight="600" - WindowStartupLocation="CenterScreen" Icon="Config.ico" FontFamily="{StaticResource UiFont}" FontSize="14" Background="{DynamicResource WindowBackground}" diff --git a/src/BmsConfig/MainWindow.xaml.cs b/src/BmsConfig/MainWindow.xaml.cs index b0746a9..ec019ec 100644 --- a/src/BmsConfig/MainWindow.xaml.cs +++ b/src/BmsConfig/MainWindow.xaml.cs @@ -56,6 +56,8 @@ public MainWindow() CategoryCombo.SelectedIndex = 0; _suppressEvents = false; + WindowFitting.FitToWorkArea(this); // also centers the window + SourceInitialized += (s, e) => ThemeManager.ApplyTitleBar(this); Loaded += OnLoaded; PreviewKeyDown += OnWindowPreviewKeyDown; diff --git a/src/BmsConfig/WindowFitting.cs b/src/BmsConfig/WindowFitting.cs new file mode 100644 index 0000000..82fb3fd --- /dev/null +++ b/src/BmsConfig/WindowFitting.cs @@ -0,0 +1,40 @@ +using System; +using System.Windows; + +namespace BmsConfig +{ + /// + /// Keeps the window's startup size within the screen's work area. WPF sizes are in 1/96" units, so the + /// nominal 1600x1000 window is scaled up by the display's DPI setting -- eg. on 2560x1440 at 150% the work + /// area is only ~1707x920 units, and the titlebar would be off-screen. + /// + /// + /// Deliberately pure WPF (no p/invoke), for portability. SystemParameters.WorkArea describes the primary + /// monitor, which is where new windows open by default. + /// + static class WindowFitting + { + /// Fraction of the work area the window may take up, at most, when it opens. + const double MaxFraction = 0.8; + + /// + /// Shrinks the window (and its minimum size, if need be) to fit the work area, and centers it there. + /// Call before the window is shown, eg. from its constructor. + /// + public static void FitToWorkArea(Window window) + { + Rect work = SystemParameters.WorkArea; + if (work.IsEmpty || work.Width <= 0 || work.Height <= 0) + return; + + window.MinWidth = Math.Min(window.MinWidth, work.Width); + window.MinHeight = Math.Min(window.MinHeight, work.Height); + window.Width = Math.Max(window.MinWidth, Math.Min(window.Width, work.Width * MaxFraction)); + window.Height = Math.Max(window.MinHeight, Math.Min(window.Height, work.Height * MaxFraction)); + + window.WindowStartupLocation = WindowStartupLocation.Manual; + window.Left = work.Left + (work.Width - window.Width) / 2; + window.Top = work.Top + (work.Height - window.Height) / 2; + } + } +}