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
1 change: 0 additions & 1 deletion src/BmsConfig/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 2 additions & 0 deletions src/BmsConfig/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 40 additions & 0 deletions src/BmsConfig/WindowFitting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Windows;

namespace BmsConfig
{
/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// Deliberately pure WPF (no p/invoke), for portability. SystemParameters.WorkArea describes the primary
/// monitor, which is where new windows open by default.
/// </remarks>
static class WindowFitting
{
/// <summary>Fraction of the work area the window may take up, at most, when it opens.</summary>
const double MaxFraction = 0.8;

/// <summary>
/// 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.
/// </summary>
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;
}
}
}
Loading