From 37ccf259405e4ac70c2587b1f7b3a5e8c45159ec Mon Sep 17 00:00:00 2001 From: DB p Date: Wed, 5 Jun 2024 04:53:31 +0900 Subject: [PATCH 01/17] Add Top Positioning per monitor --- Flow.Launcher/MainWindow.xaml.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 22799a63d9e..6c0889295f0 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -306,15 +306,15 @@ private void InitializePosition() break; case SearchWindowAligns.CenterTop: Left = HorizonCenter(screen); - Top = 10; + Top = VerticalTop(screen); break; case SearchWindowAligns.LeftTop: Left = HorizonLeft(screen); - Top = 10; + Top = VerticalTop(screen); break; case SearchWindowAligns.RightTop: Left = HorizonRight(screen); - Top = 10; + Top = VerticalTop(screen); break; case SearchWindowAligns.Custom: Left = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X + _settings.CustomWindowLeft, 0).X; @@ -727,6 +727,13 @@ public double HorizonLeft(Screen screen) return left; } + public double VerticalTop(Screen screen) + { + var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y); + var top = dip1.Y +10; + return top; + } + /// /// Register up and down key /// todo: any way to put this in xaml ? From 63ce735169afe7cf871d3d2a8eb6086e49fefa9a Mon Sep 17 00:00:00 2001 From: DB p Date: Wed, 5 Jun 2024 06:42:33 +0900 Subject: [PATCH 02/17] =?UTF-8?q?When=20using=20the=20=E2=80=9Cremember=20?= =?UTF-8?q?last=20position=E2=80=9D=20setting,=20if=20the=20resolution=20o?= =?UTF-8?q?r=20dpi=20has=20changed,=20the=20position=20will=20be=20rescale?= =?UTF-8?q?d=20proportionally?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Flow.Launcher/MainWindow.xaml.cs | 33 +++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 6c0889295f0..87e81815489 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -27,6 +27,7 @@ using System.Windows.Media; using System.Windows.Interop; using System.Runtime.InteropServices; +using System.Drawing; namespace Flow.Launcher { @@ -48,6 +49,10 @@ public partial class MainWindow private MediaPlayer animationSoundWMP; private SoundPlayer animationSoundWPF; + private double _previousScreenWidth; + private double _previousScreenHeight; + private double _previousDpiX; + private double _previousDpiY; #endregion @@ -288,8 +293,31 @@ private void OnLoaded(object sender, RoutedEventArgs _) }; } + private (double X, double Y) GetCurrentDpi(Screen screen) + { + using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) + { + return (g.DpiX, g.DpiY); + } + } private void InitializePosition() { + var screen = SelectedScreen(); + var currentDpi = GetCurrentDpi(screen); + double currentScreenWidth = screen.WorkingArea.Width; + double currentScreenHeight = screen.WorkingArea.Height; + + if (_previousScreenWidth != 0 && _previousScreenHeight != 0 && _previousDpiX != 0 && _previousDpiY != 0) + { + double widthRatio = currentScreenWidth / _previousScreenWidth; + double heightRatio = currentScreenHeight / _previousScreenHeight; + double dpiXRatio = currentDpi.X / _previousDpiX; + double dpiYRatio = currentDpi.Y / _previousDpiY; + + _settings.WindowLeft *= widthRatio * dpiXRatio; + _settings.WindowTop *= heightRatio * dpiYRatio; + } + if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation) { Top = _settings.WindowTop; @@ -297,7 +325,6 @@ private void InitializePosition() } else { - var screen = SelectedScreen(); switch (_settings.SearchWindowAlign) { case SearchWindowAligns.Center: @@ -323,6 +350,10 @@ private void InitializePosition() } } + _previousScreenWidth = currentScreenWidth; + _previousScreenHeight = currentScreenHeight; + _previousDpiX = currentDpi.X; + _previousDpiY = currentDpi.Y; } private void UpdateNotifyIconText() From ae0ec8ecf292ee56417da240966f516028f4c88d Mon Sep 17 00:00:00 2001 From: DB p Date: Wed, 5 Jun 2024 06:51:25 +0900 Subject: [PATCH 03/17] Revert "Fix blurry situation when changed dpi in some case" This reverts commit 2869812a74160517b2cd4ba6a7436b40eaa59195. --- Flow.Launcher/app.manifest | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher/app.manifest b/Flow.Launcher/app.manifest index c45508fcf03..52d1c39327b 100644 --- a/Flow.Launcher/app.manifest +++ b/Flow.Launcher/app.manifest @@ -49,12 +49,13 @@ DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. --> + From 45dd84a3fa39e5784d9101bde4aabd5eff7e7969 Mon Sep 17 00:00:00 2001 From: DB p Date: Wed, 5 Jun 2024 07:03:26 +0900 Subject: [PATCH 04/17] Adjust Code --- Flow.Launcher/MainWindow.xaml.cs | 92 +++++++++++++++++--------------- 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 87e81815489..927067f932f 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -307,53 +307,61 @@ private void InitializePosition() double currentScreenWidth = screen.WorkingArea.Width; double currentScreenHeight = screen.WorkingArea.Height; - if (_previousScreenWidth != 0 && _previousScreenHeight != 0 && _previousDpiX != 0 && _previousDpiY != 0) - { - double widthRatio = currentScreenWidth / _previousScreenWidth; - double heightRatio = currentScreenHeight / _previousScreenHeight; - double dpiXRatio = currentDpi.X / _previousDpiX; - double dpiYRatio = currentDpi.Y / _previousDpiY; + if (!( + _previousScreenWidth == currentScreenWidth && + _previousScreenHeight == currentScreenHeight && + _previousDpiX == currentDpi.X && + _previousDpiY == currentDpi.Y + )) + { + if (_previousScreenWidth != 0 && _previousScreenHeight != 0 && _previousDpiX != 0 && _previousDpiY != 0) + { + double widthRatio = currentScreenWidth / _previousScreenWidth; + double heightRatio = currentScreenHeight / _previousScreenHeight; + double dpiXRatio = currentDpi.X / _previousDpiX; + double dpiYRatio = currentDpi.Y / _previousDpiY; - _settings.WindowLeft *= widthRatio * dpiXRatio; - _settings.WindowTop *= heightRatio * dpiYRatio; - } + _settings.WindowLeft *= widthRatio * dpiXRatio; + _settings.WindowTop *= heightRatio * dpiYRatio; + } - if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation) - { - Top = _settings.WindowTop; - Left = _settings.WindowLeft; - } - else - { - switch (_settings.SearchWindowAlign) + if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation) { - case SearchWindowAligns.Center: - Left = HorizonCenter(screen); - Top = VerticalCenter(screen); - break; - case SearchWindowAligns.CenterTop: - Left = HorizonCenter(screen); - Top = VerticalTop(screen); - break; - case SearchWindowAligns.LeftTop: - Left = HorizonLeft(screen); - Top = VerticalTop(screen); - break; - case SearchWindowAligns.RightTop: - Left = HorizonRight(screen); - Top = VerticalTop(screen); - break; - case SearchWindowAligns.Custom: - Left = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X + _settings.CustomWindowLeft, 0).X; - Top = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y + _settings.CustomWindowTop).Y; - break; + Top = _settings.WindowTop; + Left = _settings.WindowLeft; + } + else + { + switch (_settings.SearchWindowAlign) + { + case SearchWindowAligns.Center: + Left = HorizonCenter(screen); + Top = VerticalCenter(screen); + break; + case SearchWindowAligns.CenterTop: + Left = HorizonCenter(screen); + Top = VerticalTop(screen); + break; + case SearchWindowAligns.LeftTop: + Left = HorizonLeft(screen); + Top = VerticalTop(screen); + break; + case SearchWindowAligns.RightTop: + Left = HorizonRight(screen); + Top = VerticalTop(screen); + break; + case SearchWindowAligns.Custom: + Left = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X + _settings.CustomWindowLeft, 0).X; + Top = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y + _settings.CustomWindowTop).Y; + break; + } } - } - _previousScreenWidth = currentScreenWidth; - _previousScreenHeight = currentScreenHeight; - _previousDpiX = currentDpi.X; - _previousDpiY = currentDpi.Y; + _previousScreenWidth = currentScreenWidth; + _previousScreenHeight = currentScreenHeight; + _previousDpiX = currentDpi.X; + _previousDpiY = currentDpi.Y; + } } private void UpdateNotifyIconText() From e0cdc92766b7a3602385be45911c187754ea9637 Mon Sep 17 00:00:00 2001 From: DB p Date: Thu, 6 Jun 2024 06:02:26 +0900 Subject: [PATCH 05/17] Revert "Adjust Code" This reverts commit 45dd84a3fa39e5784d9101bde4aabd5eff7e7969. --- Flow.Launcher/MainWindow.xaml.cs | 92 +++++++++++++++----------------- 1 file changed, 42 insertions(+), 50 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 927067f932f..87e81815489 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -307,61 +307,53 @@ private void InitializePosition() double currentScreenWidth = screen.WorkingArea.Width; double currentScreenHeight = screen.WorkingArea.Height; - if (!( - _previousScreenWidth == currentScreenWidth && - _previousScreenHeight == currentScreenHeight && - _previousDpiX == currentDpi.X && - _previousDpiY == currentDpi.Y - )) - { - if (_previousScreenWidth != 0 && _previousScreenHeight != 0 && _previousDpiX != 0 && _previousDpiY != 0) - { - double widthRatio = currentScreenWidth / _previousScreenWidth; - double heightRatio = currentScreenHeight / _previousScreenHeight; - double dpiXRatio = currentDpi.X / _previousDpiX; - double dpiYRatio = currentDpi.Y / _previousDpiY; + if (_previousScreenWidth != 0 && _previousScreenHeight != 0 && _previousDpiX != 0 && _previousDpiY != 0) + { + double widthRatio = currentScreenWidth / _previousScreenWidth; + double heightRatio = currentScreenHeight / _previousScreenHeight; + double dpiXRatio = currentDpi.X / _previousDpiX; + double dpiYRatio = currentDpi.Y / _previousDpiY; - _settings.WindowLeft *= widthRatio * dpiXRatio; - _settings.WindowTop *= heightRatio * dpiYRatio; - } + _settings.WindowLeft *= widthRatio * dpiXRatio; + _settings.WindowTop *= heightRatio * dpiYRatio; + } - if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation) - { - Top = _settings.WindowTop; - Left = _settings.WindowLeft; - } - else + if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation) + { + Top = _settings.WindowTop; + Left = _settings.WindowLeft; + } + else + { + switch (_settings.SearchWindowAlign) { - switch (_settings.SearchWindowAlign) - { - case SearchWindowAligns.Center: - Left = HorizonCenter(screen); - Top = VerticalCenter(screen); - break; - case SearchWindowAligns.CenterTop: - Left = HorizonCenter(screen); - Top = VerticalTop(screen); - break; - case SearchWindowAligns.LeftTop: - Left = HorizonLeft(screen); - Top = VerticalTop(screen); - break; - case SearchWindowAligns.RightTop: - Left = HorizonRight(screen); - Top = VerticalTop(screen); - break; - case SearchWindowAligns.Custom: - Left = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X + _settings.CustomWindowLeft, 0).X; - Top = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y + _settings.CustomWindowTop).Y; - break; - } + case SearchWindowAligns.Center: + Left = HorizonCenter(screen); + Top = VerticalCenter(screen); + break; + case SearchWindowAligns.CenterTop: + Left = HorizonCenter(screen); + Top = VerticalTop(screen); + break; + case SearchWindowAligns.LeftTop: + Left = HorizonLeft(screen); + Top = VerticalTop(screen); + break; + case SearchWindowAligns.RightTop: + Left = HorizonRight(screen); + Top = VerticalTop(screen); + break; + case SearchWindowAligns.Custom: + Left = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X + _settings.CustomWindowLeft, 0).X; + Top = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y + _settings.CustomWindowTop).Y; + break; } - - _previousScreenWidth = currentScreenWidth; - _previousScreenHeight = currentScreenHeight; - _previousDpiX = currentDpi.X; - _previousDpiY = currentDpi.Y; } + + _previousScreenWidth = currentScreenWidth; + _previousScreenHeight = currentScreenHeight; + _previousDpiX = currentDpi.X; + _previousDpiY = currentDpi.Y; } private void UpdateNotifyIconText() From 5c6f539c6ffc469b70bdfc5839d2ed2ff0fcfae3 Mon Sep 17 00:00:00 2001 From: DB p Date: Thu, 6 Jun 2024 06:02:36 +0900 Subject: [PATCH 06/17] Reapply "Fix blurry situation when changed dpi in some case" This reverts commit ae0ec8ecf292ee56417da240966f516028f4c88d. --- Flow.Launcher/app.manifest | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Flow.Launcher/app.manifest b/Flow.Launcher/app.manifest index 52d1c39327b..c45508fcf03 100644 --- a/Flow.Launcher/app.manifest +++ b/Flow.Launcher/app.manifest @@ -49,13 +49,12 @@ DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. --> - From 4f16a1123fb2e28bf86b3bea711869a36d42f31f Mon Sep 17 00:00:00 2001 From: DB p Date: Thu, 6 Jun 2024 06:02:52 +0900 Subject: [PATCH 07/17] Revert "Fix blurry situation when changed dpi in some case" This reverts commit 2869812a74160517b2cd4ba6a7436b40eaa59195. --- Flow.Launcher/app.manifest | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher/app.manifest b/Flow.Launcher/app.manifest index c45508fcf03..52d1c39327b 100644 --- a/Flow.Launcher/app.manifest +++ b/Flow.Launcher/app.manifest @@ -49,12 +49,13 @@ DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. --> + From d2472084a2804bd2e3795051d6e1bf28ad08b0ee Mon Sep 17 00:00:00 2001 From: DB p Date: Thu, 6 Jun 2024 06:02:55 +0900 Subject: [PATCH 08/17] =?UTF-8?q?Revert=20"When=20using=20the=20=E2=80=9Cr?= =?UTF-8?q?emember=20last=20position=E2=80=9D=20setting,=20if=20the=20reso?= =?UTF-8?q?lution=20or=20dpi=20has=20changed,=20the=20position=20will=20be?= =?UTF-8?q?=20rescaled=20proportionally"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 63ce735169afe7cf871d3d2a8eb6086e49fefa9a. --- Flow.Launcher/MainWindow.xaml.cs | 33 +------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 87e81815489..6c0889295f0 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -27,7 +27,6 @@ using System.Windows.Media; using System.Windows.Interop; using System.Runtime.InteropServices; -using System.Drawing; namespace Flow.Launcher { @@ -49,10 +48,6 @@ public partial class MainWindow private MediaPlayer animationSoundWMP; private SoundPlayer animationSoundWPF; - private double _previousScreenWidth; - private double _previousScreenHeight; - private double _previousDpiX; - private double _previousDpiY; #endregion @@ -293,31 +288,8 @@ private void OnLoaded(object sender, RoutedEventArgs _) }; } - private (double X, double Y) GetCurrentDpi(Screen screen) - { - using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) - { - return (g.DpiX, g.DpiY); - } - } private void InitializePosition() { - var screen = SelectedScreen(); - var currentDpi = GetCurrentDpi(screen); - double currentScreenWidth = screen.WorkingArea.Width; - double currentScreenHeight = screen.WorkingArea.Height; - - if (_previousScreenWidth != 0 && _previousScreenHeight != 0 && _previousDpiX != 0 && _previousDpiY != 0) - { - double widthRatio = currentScreenWidth / _previousScreenWidth; - double heightRatio = currentScreenHeight / _previousScreenHeight; - double dpiXRatio = currentDpi.X / _previousDpiX; - double dpiYRatio = currentDpi.Y / _previousDpiY; - - _settings.WindowLeft *= widthRatio * dpiXRatio; - _settings.WindowTop *= heightRatio * dpiYRatio; - } - if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation) { Top = _settings.WindowTop; @@ -325,6 +297,7 @@ private void InitializePosition() } else { + var screen = SelectedScreen(); switch (_settings.SearchWindowAlign) { case SearchWindowAligns.Center: @@ -350,10 +323,6 @@ private void InitializePosition() } } - _previousScreenWidth = currentScreenWidth; - _previousScreenHeight = currentScreenHeight; - _previousDpiX = currentDpi.X; - _previousDpiY = currentDpi.Y; } private void UpdateNotifyIconText() From ab40e7a2e4045772a36b6a6b6f712187e24be377 Mon Sep 17 00:00:00 2001 From: DB p Date: Thu, 6 Jun 2024 07:37:13 +0900 Subject: [PATCH 09/17] Adjust logic --- .../UserSettings/Settings.cs | 4 + Flow.Launcher/MainWindow.xaml.cs | 112 +++++++++++++++++- 2 files changed, 115 insertions(+), 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index 4588466652e..c44e0369ad9 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -205,6 +205,10 @@ public SearchPrecisionScore QuerySearchPrecision public double WindowLeft { get; set; } public double WindowTop { get; set; } + public double PreviousScreenWidth { get; set; } + public double PreviousScreenHeight { get; set; } + public double PreviousDpiX { get; set; } + public double PreviousDpiY { get; set; } /// /// Custom left position on selected monitor diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 6c0889295f0..a699eaa64d3 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -27,6 +27,7 @@ using System.Windows.Media; using System.Windows.Interop; using System.Runtime.InteropServices; +using System.Runtime.Intrinsics.Arm; namespace Flow.Launcher { @@ -292,11 +293,39 @@ private void InitializePosition() { if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation) { - Top = _settings.WindowTop; + // Get the previous screen width, height, DPI X, and DPI Y + double previousScreenWidth = _settings.PreviousScreenWidth; + double previousScreenHeight = _settings.PreviousScreenHeight; + double previousDpiX = _settings.PreviousDpiX; + double previousDpiY = _settings.PreviousDpiY; + + // Save current screen width, height, DPI X, and DPI Y + _settings.PreviousScreenWidth = SystemParameters.VirtualScreenWidth; + _settings.PreviousScreenHeight = SystemParameters.VirtualScreenHeight; + _settings.PreviousDpiX = GetDpiX(); + _settings.PreviousDpiY = GetDpiY(); + + // If previous screen width, height, DPI X, or DPI Y are not zero + if (previousScreenWidth != 0 && previousScreenHeight != 0 && + previousDpiX != 0 && previousDpiY != 0) + { + // If previous and current screen properties are different, adjust position + if (previousScreenWidth != SystemParameters.VirtualScreenWidth || + previousScreenHeight != SystemParameters.VirtualScreenHeight || + previousDpiX != GetDpiX() || previousDpiY != GetDpiY()) + { + AdjustPositionForResolutionChange(); + return; + } + } + + // If previous screen width, height, DPI X, and DPI Y are the same, initialize with previous position Left = _settings.WindowLeft; + Top = _settings.WindowTop; } else { + // Keep the existing logic var screen = SelectedScreen(); switch (_settings.SearchWindowAlign) { @@ -322,9 +351,90 @@ private void InitializePosition() break; } } + } + private void AdjustPositionForResolutionChange() + { + double screenWidth = SystemParameters.VirtualScreenWidth; + double screenHeight = SystemParameters.VirtualScreenHeight; + double screenLeft = SystemParameters.VirtualScreenLeft; + double screenTop = SystemParameters.VirtualScreenTop; + double currentDpiX = GetDpiX(); + double currentDpiY = GetDpiY(); + + // Get current screen width, height, left, top, and DPI X, DPI Y + double currentScreenWidth = screenWidth; + double currentScreenHeight = screenHeight; + double currentScreenLeft = screenLeft; + double currentScreenTop = screenTop; + + // Get previous window left, top, and DPI X, DPI Y + double previousLeft = _settings.WindowLeft; + double previousTop = _settings.WindowTop; + double previousDpiX = _settings.PreviousDpiX; + double previousDpiY = _settings.PreviousDpiY; + + // Calculate ratios for width, height, DPI X, and DPI Y + double widthRatio = currentScreenWidth / _settings.PreviousScreenWidth; + double heightRatio = currentScreenHeight / _settings.PreviousScreenHeight; + double dpiXRatio = currentDpiX / previousDpiX; + double dpiYRatio = currentDpiY / previousDpiY; + + // Adjust previous position according to current resolution and DPI + double newLeft = previousLeft * widthRatio * dpiXRatio; + double newTop = previousTop * heightRatio * dpiYRatio; + + // Ensure the adjusted position is within the current screen boundaries + if (newLeft + ActualWidth > currentScreenLeft + currentScreenWidth) + { + newLeft = currentScreenLeft + currentScreenWidth - ActualWidth; + } + else if (newLeft < currentScreenLeft) + { + newLeft = currentScreenLeft; + } + + if (newTop + ActualHeight > currentScreenTop + currentScreenHeight) + { + newTop = currentScreenTop + currentScreenHeight - ActualHeight; + } + else if (newTop < currentScreenTop) + { + newTop = currentScreenTop; + } + + // Set the new position + Left = newLeft; + Top = newTop; + } + + private double GetDpiX() + { + // Get the PresentationSource for this visual + PresentationSource source = PresentationSource.FromVisual(this); + // Check if the PresentationSource and its CompositionTarget are not null + if (source != null && source.CompositionTarget != null) + { + // Get the transform matrix for the CompositionTarget + Matrix m = source.CompositionTarget.TransformToDevice; + // Calculate DPI in X direction + double dpiX = 96 * m.M11; + return dpiX; + } + return 96; //Return a default DPI of 96 if PresentationSource or CompositionTarget is null } + private double GetDpiY() + { + PresentationSource source = PresentationSource.FromVisual(this); + if (source != null && source.CompositionTarget != null) + { + Matrix m = source.CompositionTarget.TransformToDevice; + double dpiY = 96 * m.M22; + return dpiY; + } + return 96; + } private void UpdateNotifyIconText() { var menu = contextMenu; From 5df736267426dd5e2191e1d757895eae99ad1015 Mon Sep 17 00:00:00 2001 From: DB p Date: Thu, 6 Jun 2024 08:10:40 +0900 Subject: [PATCH 10/17] - Add Logic in settingwindow --- Flow.Launcher/SettingWindow.xaml.cs | 34 ++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index de4fd1f9129..99c36322260 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -112,23 +112,45 @@ public void InitializePosition() { if (_settings.SettingWindowTop == null || _settings.SettingWindowLeft == null) { - Top = WindowTop(); - Left = WindowLeft(); + SetWindowPosition(WindowTop(), WindowLeft()); } else { - Top = _settings.SettingWindowTop.Value; - Left = _settings.SettingWindowLeft.Value; + double left = _settings.SettingWindowLeft.Value; + double top = _settings.SettingWindowTop.Value; + AdjustWindowPosition(ref top, ref left); + SetWindowPosition(top, left); } WindowState = _settings.SettingWindowState; } + private void SetWindowPosition(double top, double left) + { + // Ensure window does not exceed screen boundaries + top = Math.Max(top, SystemParameters.VirtualScreenTop); + left = Math.Max(left, SystemParameters.VirtualScreenLeft); + top = Math.Min(top, SystemParameters.VirtualScreenHeight - ActualHeight); + left = Math.Min(left, SystemParameters.VirtualScreenWidth - ActualWidth); + + Top = top; + Left = left; + } + + private void AdjustWindowPosition(ref double top, ref double left) + { + // Adjust window position if it exceeds screen boundaries + top = Math.Max(top, SystemParameters.VirtualScreenTop); + left = Math.Max(left, SystemParameters.VirtualScreenLeft); + top = Math.Min(top, SystemParameters.VirtualScreenHeight - ActualHeight); + left = Math.Min(left, SystemParameters.VirtualScreenWidth - ActualWidth); + } + private double WindowLeft() { var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X, 0); var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.Width, 0); - var left = (dip2.X - this.ActualWidth) / 2 + dip1.X; + var left = (dip2.X - ActualWidth) / 2 + dip1.X; return left; } @@ -137,7 +159,7 @@ private double WindowTop() var screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y); var dip2 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Height); - var top = (dip2.Y - this.ActualHeight) / 2 + dip1.Y - 20; + var top = (dip2.Y - ActualHeight) / 2 + dip1.Y; return top; } From 88c6fe3acbeef0e3cbb53d9d8f1308afb5a67678 Mon Sep 17 00:00:00 2001 From: DB p Date: Thu, 6 Jun 2024 09:32:46 +0900 Subject: [PATCH 11/17] Adjust Code --- Flow.Launcher/MainWindow.xaml.cs | 107 +++++++++---------------------- 1 file changed, 32 insertions(+), 75 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index a699eaa64d3..b9b82909d38 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -293,39 +293,31 @@ private void InitializePosition() { if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation) { - // Get the previous screen width, height, DPI X, and DPI Y double previousScreenWidth = _settings.PreviousScreenWidth; double previousScreenHeight = _settings.PreviousScreenHeight; - double previousDpiX = _settings.PreviousDpiX; - double previousDpiY = _settings.PreviousDpiY; + double previousDpiX, previousDpiY; + GetDpi(out previousDpiX, out previousDpiY); - // Save current screen width, height, DPI X, and DPI Y _settings.PreviousScreenWidth = SystemParameters.VirtualScreenWidth; _settings.PreviousScreenHeight = SystemParameters.VirtualScreenHeight; - _settings.PreviousDpiX = GetDpiX(); - _settings.PreviousDpiY = GetDpiY(); + double currentDpiX, currentDpiY; + GetDpi(out currentDpiX, out currentDpiY); - // If previous screen width, height, DPI X, or DPI Y are not zero if (previousScreenWidth != 0 && previousScreenHeight != 0 && - previousDpiX != 0 && previousDpiY != 0) + previousDpiX != 0 && previousDpiY != 0 && + (previousScreenWidth != SystemParameters.VirtualScreenWidth || + previousScreenHeight != SystemParameters.VirtualScreenHeight || + previousDpiX != currentDpiX || previousDpiY != currentDpiY)) { - // If previous and current screen properties are different, adjust position - if (previousScreenWidth != SystemParameters.VirtualScreenWidth || - previousScreenHeight != SystemParameters.VirtualScreenHeight || - previousDpiX != GetDpiX() || previousDpiY != GetDpiY()) - { - AdjustPositionForResolutionChange(); - return; - } + AdjustPositionForResolutionChange(); + return; } - // If previous screen width, height, DPI X, and DPI Y are the same, initialize with previous position Left = _settings.WindowLeft; Top = _settings.WindowTop; } else { - // Keep the existing logic var screen = SelectedScreen(); switch (_settings.SearchWindowAlign) { @@ -346,8 +338,10 @@ private void InitializePosition() Top = VerticalTop(screen); break; case SearchWindowAligns.Custom: - Left = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X + _settings.CustomWindowLeft, 0).X; - Top = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y + _settings.CustomWindowTop).Y; + var customLeft = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X + _settings.CustomWindowLeft, 0); + var customTop = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y + _settings.CustomWindowTop); + Left = customLeft.X; + Top = customTop.Y; break; } } @@ -357,83 +351,46 @@ private void AdjustPositionForResolutionChange() { double screenWidth = SystemParameters.VirtualScreenWidth; double screenHeight = SystemParameters.VirtualScreenHeight; - double screenLeft = SystemParameters.VirtualScreenLeft; - double screenTop = SystemParameters.VirtualScreenTop; - double currentDpiX = GetDpiX(); - double currentDpiY = GetDpiY(); - - // Get current screen width, height, left, top, and DPI X, DPI Y - double currentScreenWidth = screenWidth; - double currentScreenHeight = screenHeight; - double currentScreenLeft = screenLeft; - double currentScreenTop = screenTop; + double currentDpiX, currentDpiY; + GetDpi(out currentDpiX, out currentDpiY); - // Get previous window left, top, and DPI X, DPI Y double previousLeft = _settings.WindowLeft; double previousTop = _settings.WindowTop; - double previousDpiX = _settings.PreviousDpiX; - double previousDpiY = _settings.PreviousDpiY; + double previousDpiX, previousDpiY; + GetDpi(out previousDpiX, out previousDpiY); - // Calculate ratios for width, height, DPI X, and DPI Y - double widthRatio = currentScreenWidth / _settings.PreviousScreenWidth; - double heightRatio = currentScreenHeight / _settings.PreviousScreenHeight; + double widthRatio = screenWidth / _settings.PreviousScreenWidth; + double heightRatio = screenHeight / _settings.PreviousScreenHeight; double dpiXRatio = currentDpiX / previousDpiX; double dpiYRatio = currentDpiY / previousDpiY; - // Adjust previous position according to current resolution and DPI double newLeft = previousLeft * widthRatio * dpiXRatio; double newTop = previousTop * heightRatio * dpiYRatio; - // Ensure the adjusted position is within the current screen boundaries - if (newLeft + ActualWidth > currentScreenLeft + currentScreenWidth) - { - newLeft = currentScreenLeft + currentScreenWidth - ActualWidth; - } - else if (newLeft < currentScreenLeft) - { - newLeft = currentScreenLeft; - } + double screenLeft = SystemParameters.VirtualScreenLeft; + double screenTop = SystemParameters.VirtualScreenTop; - if (newTop + ActualHeight > currentScreenTop + currentScreenHeight) - { - newTop = currentScreenTop + currentScreenHeight - ActualHeight; - } - else if (newTop < currentScreenTop) - { - newTop = currentScreenTop; - } + double maxX = screenLeft + screenWidth - ActualWidth; + double maxY = screenTop + screenHeight - ActualHeight; - // Set the new position - Left = newLeft; - Top = newTop; + Left = Math.Max(screenLeft, Math.Min(newLeft, maxX)); + Top = Math.Max(screenTop, Math.Min(newTop, maxY)); } - private double GetDpiX() + private void GetDpi(out double dpiX, out double dpiY) { - // Get the PresentationSource for this visual PresentationSource source = PresentationSource.FromVisual(this); - // Check if the PresentationSource and its CompositionTarget are not null if (source != null && source.CompositionTarget != null) { - // Get the transform matrix for the CompositionTarget Matrix m = source.CompositionTarget.TransformToDevice; - // Calculate DPI in X direction - double dpiX = 96 * m.M11; - return dpiX; + dpiX = 96 * m.M11; + dpiY = 96 * m.M22; } - return 96; //Return a default DPI of 96 if PresentationSource or CompositionTarget is null - } - - private double GetDpiY() - { - PresentationSource source = PresentationSource.FromVisual(this); - if (source != null && source.CompositionTarget != null) + else { - Matrix m = source.CompositionTarget.TransformToDevice; - double dpiY = 96 * m.M22; - return dpiY; + dpiX = 96; + dpiY = 96; } - return 96; } private void UpdateNotifyIconText() { From 1bd9e8131d2a50ccd28a3600c15e0049bc34fdc8 Mon Sep 17 00:00:00 2001 From: DB p Date: Thu, 6 Jun 2024 09:35:44 +0900 Subject: [PATCH 12/17] Fix app manifest --- Flow.Launcher/app.manifest | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Flow.Launcher/app.manifest b/Flow.Launcher/app.manifest index 52d1c39327b..b6944273ac6 100644 --- a/Flow.Launcher/app.manifest +++ b/Flow.Launcher/app.manifest @@ -49,13 +49,12 @@ DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. --> - From e863d998ab4698dbdf73bb813f59d543e620e223 Mon Sep 17 00:00:00 2001 From: DB p Date: Thu, 6 Jun 2024 09:36:25 +0900 Subject: [PATCH 13/17] Adjust Typo --- Flow.Launcher/app.manifest | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher/app.manifest b/Flow.Launcher/app.manifest index b6944273ac6..c45508fcf03 100644 --- a/Flow.Launcher/app.manifest +++ b/Flow.Launcher/app.manifest @@ -49,7 +49,7 @@ DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. --> - + true PerMonitorV2, PerMonitor From ce100c46278eb3d94fae35ba0b34a7ab9171f633 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Wed, 19 Jun 2024 11:38:34 -0700 Subject: [PATCH 14/17] refactor code with Point2D.cs --- .../UserSettings/Point2D.cs | 46 +++++ .../UserSettings/Settings.cs | 10 +- Flow.Launcher/MainWindow.xaml.cs | 181 ++++++++---------- Flow.Launcher/ViewModel/MainViewModel.cs | 6 +- 4 files changed, 135 insertions(+), 108 deletions(-) create mode 100644 Flow.Launcher.Infrastructure/UserSettings/Point2D.cs diff --git a/Flow.Launcher.Infrastructure/UserSettings/Point2D.cs b/Flow.Launcher.Infrastructure/UserSettings/Point2D.cs new file mode 100644 index 00000000000..4c47ba6c05a --- /dev/null +++ b/Flow.Launcher.Infrastructure/UserSettings/Point2D.cs @@ -0,0 +1,46 @@ +using System; + +namespace Flow.Launcher.Infrastructure.UserSettings; + +public record struct Point2D(double X, double Y) +{ + public static implicit operator Point2D((double X, double Y) point) + { + return new Point2D(point.X, point.Y); + } + + public static Point2D operator +(Point2D point1, Point2D point2) + { + return new Point2D(point1.X + point2.X, point1.Y + point2.Y); + } + + public static Point2D operator -(Point2D point1, Point2D point2) + { + return new Point2D(point1.X - point2.X, point1.Y - point2.Y); + } + + public static Point2D operator *(Point2D point, double scalar) + { + return new Point2D(point.X * scalar, point.Y * scalar); + } + + public static Point2D operator /(Point2D point, double scalar) + { + return new Point2D(point.X / scalar, point.Y / scalar); + } + + public static Point2D operator /(Point2D point1, Point2D point2) + { + return new Point2D(point1.X / point2.X, point1.Y / point2.Y); + } + + public static Point2D operator *(Point2D point1, Point2D point2) + { + return new Point2D(point1.X * point2.X, point1.Y * point2.Y); + } + + public Point2D Clamp(Point2D min, Point2D max) + { + return new Point2D(Math.Clamp(X, min.X, max.X), Math.Clamp(Y, min.Y, max.Y)); + } +} diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index c44e0369ad9..c216efad215 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -203,12 +203,10 @@ public SearchPrecisionScore QuerySearchPrecision public bool AutoUpdates { get; set; } = false; - public double WindowLeft { get; set; } - public double WindowTop { get; set; } - public double PreviousScreenWidth { get; set; } - public double PreviousScreenHeight { get; set; } - public double PreviousDpiX { get; set; } - public double PreviousDpiY { get; set; } + + public Point2D WindowPosition { get; set; } + public Point2D PreviousScreen { get; set; } + public Point2D PreviousDpi { get; set; } /// /// Custom left position on selected monitor diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index b9b82909d38..3516d7e69f1 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -73,11 +73,7 @@ public MainWindow(Settings settings, MainViewModel mainVM) }; } - DispatcherTimer timer = new DispatcherTimer - { - Interval = new TimeSpan(0, 0, 0, 0, 500), - IsEnabled = false - }; + DispatcherTimer timer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 500), IsEnabled = false }; public MainWindow() { @@ -88,6 +84,7 @@ public MainWindow() private const int WM_EXITSIZEMOVE = 0x0232; private int _initialWidth; private int _initialHeight; + private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_ENTERSIZEMOVE) @@ -96,18 +93,22 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b _initialHeight = (int)Height; handled = true; } + if (msg == WM_EXITSIZEMOVE) { - if ( _initialHeight != (int)Height) + if (_initialHeight != (int)Height) { OnResizeEnd(); } + if (_initialWidth != (int)Width) { FlowMainWindow.SizeToContent = SizeToContent.Height; } + handled = true; } + return IntPtr.Zero; } @@ -132,6 +133,7 @@ private void OnResizeEnd() _settings.MaxResultsToShow = Convert.ToInt32(Math.Truncate(itemCount)); } } + FlowMainWindow.SizeToContent = SizeToContent.Height; _viewModel.MainWindowWidth = Width; } @@ -176,6 +178,7 @@ private async void OnClosing(object sender, CancelEventArgs e) private void OnInitialized(object sender, EventArgs e) { } + private void OnLoaded(object sender, RoutedEventArgs _) { // MouseEventHandler @@ -207,6 +210,7 @@ private void OnLoaded(object sender, RoutedEventArgs _) { SoundPlay(); } + UpdatePosition(); PreviewReset(); Activate(); @@ -218,7 +222,8 @@ private void OnLoaded(object sender, RoutedEventArgs _) _viewModel.LastQuerySelected = true; } - if (_viewModel.ProgressBarVisibility == Visibility.Visible && isProgressBarStoryboardPaused) + if (_viewModel.ProgressBarVisibility == Visibility.Visible && + isProgressBarStoryboardPaused) { _progressBarStoryboard.Begin(ProgressBar, true); isProgressBarStoryboardPaused = false; @@ -259,9 +264,12 @@ private void OnLoaded(object sender, RoutedEventArgs _) MoveQueryTextToEnd(); _viewModel.QueryTextCursorMovedToEnd = false; } + break; case nameof(MainViewModel.GameModeStatus): - _notifyIcon.Icon = _viewModel.GameModeStatus ? Properties.Resources.gamemode : Properties.Resources.app; + _notifyIcon.Icon = _viewModel.GameModeStatus + ? Properties.Resources.gamemode + : Properties.Resources.app; break; } }; @@ -279,11 +287,8 @@ private void OnLoaded(object sender, RoutedEventArgs _) case nameof(Settings.Hotkey): UpdateNotifyIconText(); break; - case nameof(Settings.WindowLeft): - Left = _settings.WindowLeft; - break; - case nameof(Settings.WindowTop): - Top = _settings.WindowTop; + case nameof(Settings.WindowPosition): + (Left, Top) = _settings.WindowPosition; break; } }; @@ -293,28 +298,24 @@ private void InitializePosition() { if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation) { - double previousScreenWidth = _settings.PreviousScreenWidth; - double previousScreenHeight = _settings.PreviousScreenHeight; - double previousDpiX, previousDpiY; - GetDpi(out previousDpiX, out previousDpiY); - - _settings.PreviousScreenWidth = SystemParameters.VirtualScreenWidth; - _settings.PreviousScreenHeight = SystemParameters.VirtualScreenHeight; - double currentDpiX, currentDpiY; - GetDpi(out currentDpiX, out currentDpiY); - - if (previousScreenWidth != 0 && previousScreenHeight != 0 && - previousDpiX != 0 && previousDpiY != 0 && - (previousScreenWidth != SystemParameters.VirtualScreenWidth || - previousScreenHeight != SystemParameters.VirtualScreenHeight || - previousDpiX != currentDpiX || previousDpiY != currentDpiY)) + var previousScreen = _settings.PreviousScreen; + + var previousDpi = _settings.PreviousDpi; + + _settings.PreviousScreen = (SystemParameters.VirtualScreenWidth, SystemParameters.VirtualScreenHeight); + + var currentDpi = GetDpi(); + + if (previousScreen != default || + previousDpi != default || + (previousScreen != (SystemParameters.VirtualScreenWidth, SystemParameters.VirtualScreenHeight) || + previousDpi != currentDpi)) { AdjustPositionForResolutionChange(); return; } - Left = _settings.WindowLeft; - Top = _settings.WindowTop; + (Left, Top) = _settings.WindowPosition; } else { @@ -338,8 +339,10 @@ private void InitializePosition() Top = VerticalTop(screen); break; case SearchWindowAligns.Custom: - var customLeft = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X + _settings.CustomWindowLeft, 0); - var customTop = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y + _settings.CustomWindowTop); + var customLeft = WindowsInteropHelper.TransformPixelsToDIP(this, + screen.WorkingArea.X + _settings.CustomWindowLeft, 0); + var customTop = WindowsInteropHelper.TransformPixelsToDIP(this, 0, + screen.WorkingArea.Y + _settings.CustomWindowTop); Left = customLeft.X; Top = customTop.Y; break; @@ -349,58 +352,48 @@ private void InitializePosition() private void AdjustPositionForResolutionChange() { - double screenWidth = SystemParameters.VirtualScreenWidth; - double screenHeight = SystemParameters.VirtualScreenHeight; - double currentDpiX, currentDpiY; - GetDpi(out currentDpiX, out currentDpiY); + Point2D screenBound = (SystemParameters.VirtualScreenWidth, SystemParameters.VirtualScreenHeight); + + var currentDpi = GetDpi(); - double previousLeft = _settings.WindowLeft; - double previousTop = _settings.WindowTop; - double previousDpiX, previousDpiY; - GetDpi(out previousDpiX, out previousDpiY); + var previousPosition = _settings.WindowPosition; - double widthRatio = screenWidth / _settings.PreviousScreenWidth; - double heightRatio = screenHeight / _settings.PreviousScreenHeight; - double dpiXRatio = currentDpiX / previousDpiX; - double dpiYRatio = currentDpiY / previousDpiY; + var ratio = screenBound / _settings.PreviousScreen; - double newLeft = previousLeft * widthRatio * dpiXRatio; - double newTop = previousTop * heightRatio * dpiYRatio; + var dpiXRatio = currentDpi / _settings.PreviousDpi; - double screenLeft = SystemParameters.VirtualScreenLeft; - double screenTop = SystemParameters.VirtualScreenTop; + var newPosition = previousPosition * ratio * dpiXRatio; - double maxX = screenLeft + screenWidth - ActualWidth; - double maxY = screenTop + screenHeight - ActualHeight; + Point2D screenPosition = (SystemParameters.VirtualScreenLeft, SystemParameters.VirtualScreenTop); - Left = Math.Max(screenLeft, Math.Min(newLeft, maxX)); - Top = Math.Max(screenTop, Math.Min(newTop, maxY)); + var maxPosition = screenPosition + screenBound - (ActualWidth, ActualHeight); + + (Left, Top) = newPosition.Clamp(screenPosition, maxPosition); } - private void GetDpi(out double dpiX, out double dpiY) + private Point2D GetDpi() { PresentationSource source = PresentationSource.FromVisual(this); - if (source != null && source.CompositionTarget != null) + Point2D point = (96, 96); + if (source is { CompositionTarget: not null }) { Matrix m = source.CompositionTarget.TransformToDevice; - dpiX = 96 * m.M11; - dpiY = 96 * m.M22; - } - else - { - dpiX = 96; - dpiY = 96; + point.X = 96 * m.M11; + point.Y = 96 * m.M22; } + + return point; } + private void UpdateNotifyIconText() { var menu = contextMenu; - ((MenuItem)menu.Items[0]).Header = InternationalizationManager.Instance.GetTranslation("iconTrayOpen") + " (" + _settings.Hotkey + ")"; + ((MenuItem)menu.Items[0]).Header = InternationalizationManager.Instance.GetTranslation("iconTrayOpen") + + " (" + _settings.Hotkey + ")"; ((MenuItem)menu.Items[1]).Header = InternationalizationManager.Instance.GetTranslation("GameMode"); ((MenuItem)menu.Items[2]).Header = InternationalizationManager.Instance.GetTranslation("PositionReset"); ((MenuItem)menu.Items[3]).Header = InternationalizationManager.Instance.GetTranslation("iconTraySettings"); ((MenuItem)menu.Items[4]).Header = InternationalizationManager.Instance.GetTranslation("iconTrayExit"); - } private void InitializeNotifyIcon() @@ -412,50 +405,34 @@ private void InitializeNotifyIcon() Visible = !_settings.HideNotifyIcon }; - var openIcon = new FontIcon - { - Glyph = "\ue71e" - }; + var openIcon = new FontIcon { Glyph = "\ue71e" }; var open = new MenuItem { - Header = InternationalizationManager.Instance.GetTranslation("iconTrayOpen") + " (" + _settings.Hotkey + ")", + Header = InternationalizationManager.Instance.GetTranslation("iconTrayOpen") + " (" + + _settings.Hotkey + ")", Icon = openIcon }; - var gamemodeIcon = new FontIcon - { - Glyph = "\ue7fc" - }; + var gamemodeIcon = new FontIcon { Glyph = "\ue7fc" }; var gamemode = new MenuItem { - Header = InternationalizationManager.Instance.GetTranslation("GameMode"), - Icon = gamemodeIcon - }; - var positionresetIcon = new FontIcon - { - Glyph = "\ue73f" + Header = InternationalizationManager.Instance.GetTranslation("GameMode"), Icon = gamemodeIcon }; + var positionresetIcon = new FontIcon { Glyph = "\ue73f" }; var positionreset = new MenuItem { Header = InternationalizationManager.Instance.GetTranslation("PositionReset"), Icon = positionresetIcon }; - var settingsIcon = new FontIcon - { - Glyph = "\ue713" - }; + var settingsIcon = new FontIcon { Glyph = "\ue713" }; var settings = new MenuItem { Header = InternationalizationManager.Instance.GetTranslation("iconTraySettings"), Icon = settingsIcon }; - var exitIcon = new FontIcon - { - Glyph = "\ue7e8" - }; + var exitIcon = new FontIcon { Glyph = "\ue7e8" }; var exit = new MenuItem { - Header = InternationalizationManager.Instance.GetTranslation("iconTrayExit"), - Icon = exitIcon + Header = InternationalizationManager.Instance.GetTranslation("iconTrayExit"), Icon = exitIcon }; open.Click += (o, e) => _viewModel.ToggleFlowLauncher(); @@ -488,6 +465,7 @@ private void InitializeNotifyIcon() { _ = SetForegroundWindow(hwndSource.Handle); } + contextMenu.Focus(); break; } @@ -521,8 +499,10 @@ private async void PositionReset() private void InitProgressbarAnimation() { - var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100, new Duration(new TimeSpan(0, 0, 0, 0, 1600))); - var da1 = new DoubleAnimation(ProgressBar.X1, ActualWidth + 0, new Duration(new TimeSpan(0, 0, 0, 0, 1600))); + var da = new DoubleAnimation(ProgressBar.X2, ActualWidth + 100, + new Duration(new TimeSpan(0, 0, 0, 0, 1600))); + var da1 = new DoubleAnimation(ProgressBar.X1, ActualWidth + 0, + new Duration(new TimeSpan(0, 0, 0, 0, 1600))); Storyboard.SetTargetProperty(da, new PropertyPath("(Line.X2)")); Storyboard.SetTargetProperty(da1, new PropertyPath("(Line.X1)")); _progressBarStoryboard.Children.Add(da); @@ -624,14 +604,14 @@ public void WindowAnimator() iconsb.Children.Add(IconOpacity); windowsb.Completed += (_, _) => _animating = false; - _settings.WindowLeft = Left; - _settings.WindowTop = Top; + _settings.WindowPosition = (Left, Top); isArrowKeyPressed = false; if (QueryTextBox.Text.Length == 0) { clocksb.Begin(ClockPanel); } + iconsb.Begin(SearchIcon); windowsb.Begin(FlowMainWindow); } @@ -685,8 +665,7 @@ private async void OnContextMenusForSettingsClick(object sender, RoutedEventArgs private async void OnDeactivated(object sender, EventArgs e) { - _settings.WindowLeft = Left; - _settings.WindowTop = Top; + _settings.WindowPosition = (Left, Top); //This condition stops extra hide call when animator is on, // which causes the toggling to occasional hide instead of show. if (_viewModel.MainWindowVisibilityStatus) @@ -717,8 +696,7 @@ private void OnLocationChanged(object sender, EventArgs e) return; if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation) { - _settings.WindowLeft = Left; - _settings.WindowTop = Top; + _settings.WindowPosition = (Left, Top); } } @@ -738,7 +716,7 @@ public void HideStartup() public Screen SelectedScreen() { Screen screen = null; - switch(_settings.SearchWindowScreen) + switch (_settings.SearchWindowScreen) { case SearchWindowScreens.Cursor: screen = Screen.FromPoint(System.Windows.Forms.Cursor.Position); @@ -760,6 +738,7 @@ public Screen SelectedScreen() screen = Screen.AllScreens[0]; break; } + return screen ?? Screen.AllScreens[0]; } @@ -797,7 +776,7 @@ public double HorizonLeft(Screen screen) public double VerticalTop(Screen screen) { var dip1 = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y); - var top = dip1.Y +10; + var top = dip1.Y + 10; return top; } @@ -836,6 +815,7 @@ private void OnKeyDown(object sender, KeyEventArgs e) _viewModel.LoadContextMenuCommand.Execute(null); e.Handled = true; } + break; case Key.Left: if (!_viewModel.SelectedIsFromQueryResults() && QueryTextBox.CaretIndex == 0) @@ -843,6 +823,7 @@ private void OnKeyDown(object sender, KeyEventArgs e) _viewModel.EscCommand.Execute(null); e.Handled = true; } + break; case Key.Back: if (specialKeyState.CtrlPressed) @@ -861,12 +842,13 @@ private void OnKeyDown(object sender, KeyEventArgs e) } } } + break; default: break; - } } + private void OnKeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.Up || e.Key == Key.Down) @@ -882,6 +864,7 @@ private void MainPreviewMouseMove(object sender, System.Windows.Input.MouseEvent e.Handled = true; // Ignore Mouse Hover when press Arrowkeys } } + public void PreviewReset() { _viewModel.ResetPreview(); diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index a793ceaa570..406d2af58a1 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -543,20 +543,20 @@ public string QueryText private void IncreaseWidth() { Settings.WindowSize += 100; - Settings.WindowLeft -= 50; + Settings.WindowPosition -= (50, 0); OnPropertyChanged(nameof(MainWindowWidth)); } [RelayCommand] private void DecreaseWidth() { - if (MainWindowWidth - 100 < 400 || Settings.WindowSize == 400) + if (MainWindowWidth - 100 < 400 || Math.Abs(Settings.WindowSize - 400) < 0.01) { Settings.WindowSize = 400; } else { - Settings.WindowLeft += 50; + Settings.WindowPosition += (50, 0); Settings.WindowSize -= 100; } From feaf6c45c128fb59d0e9f9bbfe75e4e7cc472d35 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Wed, 19 Jun 2024 11:42:37 -0700 Subject: [PATCH 15/17] fix a variable name --- Flow.Launcher/MainWindow.xaml.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index ae57e7934cd..4fd0145dba5 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -360,15 +360,15 @@ private void AdjustPositionForResolutionChange() var ratio = screenBound / _settings.PreviousScreen; - var dpiXRatio = currentDpi / _settings.PreviousDpi; + var dpiRatio = currentDpi / _settings.PreviousDpi; - var newPosition = previousPosition * ratio * dpiXRatio; + var newPosition = previousPosition * ratio * dpiRatio; - Point2D screenPosition = (SystemParameters.VirtualScreenLeft, SystemParameters.VirtualScreenTop); + Point2D minPosition = (SystemParameters.VirtualScreenLeft, SystemParameters.VirtualScreenTop); - var maxPosition = screenPosition + screenBound - (ActualWidth, ActualHeight); + var maxPosition = minPosition + screenBound - (ActualWidth, ActualHeight); - (Left, Top) = newPosition.Clamp(screenPosition, maxPosition); + (Left, Top) = newPosition.Clamp(minPosition, maxPosition); } private Point2D GetDpi() From 70a24e077459309a62b19481741b29d5509c96e3 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Wed, 19 Jun 2024 11:43:23 -0700 Subject: [PATCH 16/17] remove unused intrinsic --- Flow.Launcher/MainWindow.xaml.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 4fd0145dba5..9e09c3470d1 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -27,7 +27,6 @@ using System.Windows.Media; using System.Windows.Interop; using System.Runtime.InteropServices; -using System.Runtime.Intrinsics.Arm; namespace Flow.Launcher { From 58caa493bcec26ef74e5b93653b94111ca1356fb Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Wed, 19 Jun 2024 11:45:23 -0700 Subject: [PATCH 17/17] fix logic --- Flow.Launcher/MainWindow.xaml.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 9e09c3470d1..4027e7698e6 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -305,8 +305,8 @@ private void InitializePosition() var currentDpi = GetDpi(); - if (previousScreen != default || - previousDpi != default || + if (previousScreen == default || + previousDpi == default || (previousScreen != (SystemParameters.VirtualScreenWidth, SystemParameters.VirtualScreenHeight) || previousDpi != currentDpi)) {