Add function to validate window position

A new function was added to check whether the previously saved Window position is still valid or not. If not it resets the Window position to default.
This commit is contained in:
Lasith Manujitha 2024-07-19 02:36:46 +05:30
parent 08af45fd3c
commit bebff8321f

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);