Skip to content

Commit ab40e7a

Browse files
committed
Adjust logic
1 parent d247208 commit ab40e7a

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ public SearchPrecisionScore QuerySearchPrecision
205205

206206
public double WindowLeft { get; set; }
207207
public double WindowTop { get; set; }
208+
public double PreviousScreenWidth { get; set; }
209+
public double PreviousScreenHeight { get; set; }
210+
public double PreviousDpiX { get; set; }
211+
public double PreviousDpiY { get; set; }
208212

209213
/// <summary>
210214
/// Custom left position on selected monitor

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 111 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.Runtime.Intrinsics.Arm;
3031

3132
namespace Flow.Launcher
3233
{
@@ -292,11 +293,39 @@ private void InitializePosition()
292293
{
293294
if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation)
294295
{
295-
Top = _settings.WindowTop;
296+
// Get the previous screen width, height, DPI X, and DPI Y
297+
double previousScreenWidth = _settings.PreviousScreenWidth;
298+
double previousScreenHeight = _settings.PreviousScreenHeight;
299+
double previousDpiX = _settings.PreviousDpiX;
300+
double previousDpiY = _settings.PreviousDpiY;
301+
302+
// Save current screen width, height, DPI X, and DPI Y
303+
_settings.PreviousScreenWidth = SystemParameters.VirtualScreenWidth;
304+
_settings.PreviousScreenHeight = SystemParameters.VirtualScreenHeight;
305+
_settings.PreviousDpiX = GetDpiX();
306+
_settings.PreviousDpiY = GetDpiY();
307+
308+
// If previous screen width, height, DPI X, or DPI Y are not zero
309+
if (previousScreenWidth != 0 && previousScreenHeight != 0 &&
310+
previousDpiX != 0 && previousDpiY != 0)
311+
{
312+
// If previous and current screen properties are different, adjust position
313+
if (previousScreenWidth != SystemParameters.VirtualScreenWidth ||
314+
previousScreenHeight != SystemParameters.VirtualScreenHeight ||
315+
previousDpiX != GetDpiX() || previousDpiY != GetDpiY())
316+
{
317+
AdjustPositionForResolutionChange();
318+
return;
319+
}
320+
}
321+
322+
// If previous screen width, height, DPI X, and DPI Y are the same, initialize with previous position
296323
Left = _settings.WindowLeft;
324+
Top = _settings.WindowTop;
297325
}
298326
else
299327
{
328+
// Keep the existing logic
300329
var screen = SelectedScreen();
301330
switch (_settings.SearchWindowAlign)
302331
{
@@ -322,9 +351,90 @@ private void InitializePosition()
322351
break;
323352
}
324353
}
354+
}
325355

356+
private void AdjustPositionForResolutionChange()
357+
{
358+
double screenWidth = SystemParameters.VirtualScreenWidth;
359+
double screenHeight = SystemParameters.VirtualScreenHeight;
360+
double screenLeft = SystemParameters.VirtualScreenLeft;
361+
double screenTop = SystemParameters.VirtualScreenTop;
362+
double currentDpiX = GetDpiX();
363+
double currentDpiY = GetDpiY();
364+
365+
// Get current screen width, height, left, top, and DPI X, DPI Y
366+
double currentScreenWidth = screenWidth;
367+
double currentScreenHeight = screenHeight;
368+
double currentScreenLeft = screenLeft;
369+
double currentScreenTop = screenTop;
370+
371+
// Get previous window left, top, and DPI X, DPI Y
372+
double previousLeft = _settings.WindowLeft;
373+
double previousTop = _settings.WindowTop;
374+
double previousDpiX = _settings.PreviousDpiX;
375+
double previousDpiY = _settings.PreviousDpiY;
376+
377+
// Calculate ratios for width, height, DPI X, and DPI Y
378+
double widthRatio = currentScreenWidth / _settings.PreviousScreenWidth;
379+
double heightRatio = currentScreenHeight / _settings.PreviousScreenHeight;
380+
double dpiXRatio = currentDpiX / previousDpiX;
381+
double dpiYRatio = currentDpiY / previousDpiY;
382+
383+
// Adjust previous position according to current resolution and DPI
384+
double newLeft = previousLeft * widthRatio * dpiXRatio;
385+
double newTop = previousTop * heightRatio * dpiYRatio;
386+
387+
// Ensure the adjusted position is within the current screen boundaries
388+
if (newLeft + ActualWidth > currentScreenLeft + currentScreenWidth)
389+
{
390+
newLeft = currentScreenLeft + currentScreenWidth - ActualWidth;
391+
}
392+
else if (newLeft < currentScreenLeft)
393+
{
394+
newLeft = currentScreenLeft;
395+
}
396+
397+
if (newTop + ActualHeight > currentScreenTop + currentScreenHeight)
398+
{
399+
newTop = currentScreenTop + currentScreenHeight - ActualHeight;
400+
}
401+
else if (newTop < currentScreenTop)
402+
{
403+
newTop = currentScreenTop;
404+
}
405+
406+
// Set the new position
407+
Left = newLeft;
408+
Top = newTop;
409+
}
410+
411+
private double GetDpiX()
412+
{
413+
// Get the PresentationSource for this visual
414+
PresentationSource source = PresentationSource.FromVisual(this);
415+
// Check if the PresentationSource and its CompositionTarget are not null
416+
if (source != null && source.CompositionTarget != null)
417+
{
418+
// Get the transform matrix for the CompositionTarget
419+
Matrix m = source.CompositionTarget.TransformToDevice;
420+
// Calculate DPI in X direction
421+
double dpiX = 96 * m.M11;
422+
return dpiX;
423+
}
424+
return 96; //Return a default DPI of 96 if PresentationSource or CompositionTarget is null
326425
}
327426

427+
private double GetDpiY()
428+
{
429+
PresentationSource source = PresentationSource.FromVisual(this);
430+
if (source != null && source.CompositionTarget != null)
431+
{
432+
Matrix m = source.CompositionTarget.TransformToDevice;
433+
double dpiY = 96 * m.M22;
434+
return dpiY;
435+
}
436+
return 96;
437+
}
328438
private void UpdateNotifyIconText()
329439
{
330440
var menu = contextMenu;

0 commit comments

Comments
 (0)