Files
cursor-lang/CursorLang/Services/MainWindowPlacement.cs
T
2026-08-09 20:02:23 +05:00

162 lines
5.4 KiB
C#

using System.Windows;
using System.Windows.Interop;
using CursorLang.Interop;
namespace CursorLang.Services;
/// <summary>
/// Decides where the settings window shows up: for the first time in a session — in
/// the centre of the monitor the user is working on, and after that — where they
/// left that window.
/// </summary>
/// <remarks>
/// The position lives in memory only and is not kept between launches: the set of
/// monitors may be different by the next launch, while "in the centre of the active
/// one" is always right.
/// </remarks>
public sealed class MainWindowPlacement
{
private PopupWindowNative.Point? _position;
// The window reports a move when we move it ourselves as well;
// what has to be remembered is only what the user chose
private bool _isPlacing;
/// <summary>
/// Takes over the placement of the window: puts it in place by the first show
/// and follows where the user moves it.
/// </summary>
public void Attach(Window window)
{
window.SourceInitialized += OnSourceInitialized;
window.LocationChanged += OnLocationChanged;
}
/// <summary>
/// Returns the window to the remembered place, and when it has not been shown
/// yet during this session — puts it in the centre of the active monitor.
/// </summary>
public void Apply(Window window)
{
// A minimized window has no meaningful bounds: it is restored in its former
// place, and it can be positioned only after that
if (window.WindowState != WindowState.Normal)
{
return;
}
IntPtr handle = new WindowInteropHelper(window).Handle;
if (handle == IntPtr.Zero || WindowPlacementNative.TryGetBounds(handle) is not { } bounds)
{
return;
}
PopupWindowNative.Point? wanted = _position ?? CenterOnActiveMonitor(bounds);
if (wanted is null || KeepOnScreen(wanted.Value, bounds) is not { } target)
{
return;
}
_isPlacing = true;
try
{
PopupWindowNative.MoveTo(handle, target.X, target.Y);
}
finally
{
_isPlacing = false;
}
_position = target;
}
// The window height adapts to its content and is unknown until the first layout
// pass — an empty window frame would end up in the centre. So we ask for the
// layout to be computed right away: by that moment the window is not shown yet,
// so it will not flash in its former place
private void OnSourceInitialized(object? sender, EventArgs e)
{
if (sender is not Window window)
{
return;
}
window.SourceInitialized -= OnSourceInitialized;
window.UpdateLayout();
Apply(window);
}
private void OnLocationChanged(object? sender, EventArgs e)
{
if (_isPlacing || sender is not Window { WindowState: WindowState.Normal } window)
{
return;
}
IntPtr handle = new WindowInteropHelper(window).Handle;
if (handle != IntPtr.Zero && WindowPlacementNative.TryGetBounds(handle) is { } bounds)
{
_position = new PopupWindowNative.Point { X = bounds.Left, Y = bounds.Top };
}
}
private static PopupWindowNative.Point? CenterOnActiveMonitor(PopupWindowNative.Rect bounds)
{
(PopupWindowNative.Rect work, _) = PopupWindowNative.GetActiveMonitorWorkArea();
if (IsEmpty(work))
{
return null;
}
return new PopupWindowNative.Point
{
X = work.Left + (((work.Right - work.Left) - Width(bounds)) / 2),
Y = work.Top + (((work.Bottom - work.Top) - Height(bounds)) / 2),
};
}
/// <summary>
/// Pulls the window into the work area of the nearest monitor.
/// </summary>
/// <remarks>
/// Needed in two cases. The monitor the user put the window on may be disconnected
/// during the session — returning the window to its place would then leave the
/// user without a window, so the remembered position is a wish here rather than an
/// order. And the window height equals the height of its content and may exceed
/// the work area on a short monitor — then the title bar of a window placed in the
/// centre would go past the top edge.
/// </remarks>
private static PopupWindowNative.Point? KeepOnScreen(
PopupWindowNative.Point position, PopupWindowNative.Rect bounds)
{
int width = Width(bounds);
int height = Height(bounds);
var wanted = new PopupWindowNative.Rect
{
Left = position.X,
Top = position.Y,
Right = position.X + width,
Bottom = position.Y + height,
};
if (WindowPlacementNative.TryGetWorkAreaNear(wanted) is not { } work || IsEmpty(work))
{
return null;
}
return new PopupWindowNative.Point
{
X = Math.Clamp(position.X, work.Left, Math.Max(work.Left, work.Right - width)),
Y = Math.Clamp(position.Y, work.Top, Math.Max(work.Top, work.Bottom - height)),
};
}
private static int Width(PopupWindowNative.Rect rect) => rect.Right - rect.Left;
private static int Height(PopupWindowNative.Rect rect) => rect.Bottom - rect.Top;
private static bool IsEmpty(PopupWindowNative.Rect rect) =>
rect.Right <= rect.Left || rect.Bottom <= rect.Top;
}