Merge pull request #2848 from z1nc0r3/window-position-validate

Add function to validate previous Window position
This commit is contained in:
Jeremy Wu 2024-07-23 13:34:21 +10:00 committed by GitHub
commit d0a8f51114
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -110,19 +110,37 @@ public partial class SettingWindow
public void InitializePosition()
{
if (_settings.SettingWindowTop == null || _settings.SettingWindowLeft == null)
var previousTop = _settings.SettingWindowTop;
var previousLeft = _settings.SettingWindowLeft;
if (previousTop == null || previousLeft == null || !IsPositionValid(previousTop.Value, previousLeft.Value))
{
Top = WindowTop();
Left = WindowLeft();
}
else
{
Top = _settings.SettingWindowTop.Value;
Left = _settings.SettingWindowLeft.Value;
Top = previousTop.Value;
Left = previousLeft.Value;
}
WindowState = _settings.SettingWindowState;
}
private bool IsPositionValid(double top, double left)
{
foreach (var screen in Screen.AllScreens)
{
var workingArea = screen.WorkingArea;
if (left >= workingArea.Left && left < workingArea.Right &&
top >= workingArea.Top && top < workingArea.Bottom)
{
return true;
}
}
return false;
}
private double WindowLeft()
{
var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);