Skip to content

Commit 63ce735

Browse files
committed
When using the “remember last position” setting, if the resolution or dpi has changed, the position will be rescaled proportionally
1 parent 37ccf25 commit 63ce735

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using System.Windows.Media;
2828
using System.Windows.Interop;
2929
using System.Runtime.InteropServices;
30+
using System.Drawing;
3031

3132
namespace Flow.Launcher
3233
{
@@ -48,6 +49,10 @@ public partial class MainWindow
4849

4950
private MediaPlayer animationSoundWMP;
5051
private SoundPlayer animationSoundWPF;
52+
private double _previousScreenWidth;
53+
private double _previousScreenHeight;
54+
private double _previousDpiX;
55+
private double _previousDpiY;
5156

5257
#endregion
5358

@@ -288,16 +293,38 @@ private void OnLoaded(object sender, RoutedEventArgs _)
288293
};
289294
}
290295

296+
private (double X, double Y) GetCurrentDpi(Screen screen)
297+
{
298+
using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
299+
{
300+
return (g.DpiX, g.DpiY);
301+
}
302+
}
291303
private void InitializePosition()
292304
{
305+
var screen = SelectedScreen();
306+
var currentDpi = GetCurrentDpi(screen);
307+
double currentScreenWidth = screen.WorkingArea.Width;
308+
double currentScreenHeight = screen.WorkingArea.Height;
309+
310+
if (_previousScreenWidth != 0 && _previousScreenHeight != 0 && _previousDpiX != 0 && _previousDpiY != 0)
311+
{
312+
double widthRatio = currentScreenWidth / _previousScreenWidth;
313+
double heightRatio = currentScreenHeight / _previousScreenHeight;
314+
double dpiXRatio = currentDpi.X / _previousDpiX;
315+
double dpiYRatio = currentDpi.Y / _previousDpiY;
316+
317+
_settings.WindowLeft *= widthRatio * dpiXRatio;
318+
_settings.WindowTop *= heightRatio * dpiYRatio;
319+
}
320+
293321
if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation)
294322
{
295323
Top = _settings.WindowTop;
296324
Left = _settings.WindowLeft;
297325
}
298326
else
299327
{
300-
var screen = SelectedScreen();
301328
switch (_settings.SearchWindowAlign)
302329
{
303330
case SearchWindowAligns.Center:
@@ -323,6 +350,10 @@ private void InitializePosition()
323350
}
324351
}
325352

353+
_previousScreenWidth = currentScreenWidth;
354+
_previousScreenHeight = currentScreenHeight;
355+
_previousDpiX = currentDpi.X;
356+
_previousDpiY = currentDpi.Y;
326357
}
327358

328359
private void UpdateNotifyIconText()

0 commit comments

Comments
 (0)