From bebff8321f881b6891f416175d0edb4691b39878 Mon Sep 17 00:00:00 2001 From: Lasith Manujitha Date: Fri, 19 Jul 2024 02:36:46 +0530 Subject: [PATCH] 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. --- Flow.Launcher/SettingWindow.xaml.cs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index de4fd1f9129..4cc125fa4af 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -110,19 +110,37 @@ private void Window_StateChanged(object sender, EventArgs e) 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);