/// <summary> /// Restores the position of a form from the user settings. Does /// nothing if there is no entry for the form in the settings, or the /// setting would be invisible on the current display configuration. /// </summary> protected virtual void RestorePosition() { if (!_needsPositionRestore) { return; } if (WindowState == FormWindowState.Minimized) { // TODO: do we still need to assert when restored it is shown on the correct monitor? return; } var position = _windowPositionManager.LoadPosition(this); if (position is null) { return; } _needsPositionRestore = false; var workingArea = _getScreensWorkingArea(); if (!workingArea.Any(screen => screen.IntersectsWith(position.Rect))) { if (position.State == FormWindowState.Maximized) { WindowState = FormWindowState.Maximized; } return; } SuspendLayout(); var windowCentred = StartPosition == FormStartPosition.CenterParent; StartPosition = FormStartPosition.Manual; if (FormBorderStyle == FormBorderStyle.Sizable || FormBorderStyle == FormBorderStyle.SizableToolWindow) { Size = DpiUtil.Scale(position.Rect.Size, originalDpi: position.DeviceDpi); } if (Owner is null || !windowCentred) { var location = DpiUtil.Scale(position.Rect.Location, originalDpi: position.DeviceDpi); if (WindowPositionManager.FindWindowScreen(location, workingArea) is Rectangle rect) { location.Y = rect.Y; } DesktopLocation = location; }
public void RestorePosition_should_not_restore_position_if_not_persisted() { using var form = new MockForm(true) { Location = new Point(-100, -100), Size = new Size(500, 500) }; GitExtensionsFormTestAccessor testAccessor = new(form); testAccessor.GetScreensWorkingArea = () => throw new InvalidOperationException(); testAccessor.WindowPositionManager = _windowPositionManager; _windowPositionManager.LoadPosition(form).Returns(x => null); form.InvokeRestorePosition(); form.Location.Should().Be(new Point(-100, -100)); form.Size.Should().Be(new Size(500, 500)); }