Skip to content

Commit 88c6fe3

Browse files
committed
Adjust Code
1 parent 5df7362 commit 88c6fe3

File tree

1 file changed

+32
-75
lines changed

1 file changed

+32
-75
lines changed

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 32 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -293,39 +293,31 @@ private void InitializePosition()
293293
{
294294
if (_settings.SearchWindowScreen == SearchWindowScreens.RememberLastLaunchLocation)
295295
{
296-
// Get the previous screen width, height, DPI X, and DPI Y
297296
double previousScreenWidth = _settings.PreviousScreenWidth;
298297
double previousScreenHeight = _settings.PreviousScreenHeight;
299-
double previousDpiX = _settings.PreviousDpiX;
300-
double previousDpiY = _settings.PreviousDpiY;
298+
double previousDpiX, previousDpiY;
299+
GetDpi(out previousDpiX, out previousDpiY);
301300

302-
// Save current screen width, height, DPI X, and DPI Y
303301
_settings.PreviousScreenWidth = SystemParameters.VirtualScreenWidth;
304302
_settings.PreviousScreenHeight = SystemParameters.VirtualScreenHeight;
305-
_settings.PreviousDpiX = GetDpiX();
306-
_settings.PreviousDpiY = GetDpiY();
303+
double currentDpiX, currentDpiY;
304+
GetDpi(out currentDpiX, out currentDpiY);
307305

308-
// If previous screen width, height, DPI X, or DPI Y are not zero
309306
if (previousScreenWidth != 0 && previousScreenHeight != 0 &&
310-
previousDpiX != 0 && previousDpiY != 0)
307+
previousDpiX != 0 && previousDpiY != 0 &&
308+
(previousScreenWidth != SystemParameters.VirtualScreenWidth ||
309+
previousScreenHeight != SystemParameters.VirtualScreenHeight ||
310+
previousDpiX != currentDpiX || previousDpiY != currentDpiY))
311311
{
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-
}
312+
AdjustPositionForResolutionChange();
313+
return;
320314
}
321315

322-
// If previous screen width, height, DPI X, and DPI Y are the same, initialize with previous position
323316
Left = _settings.WindowLeft;
324317
Top = _settings.WindowTop;
325318
}
326319
else
327320
{
328-
// Keep the existing logic
329321
var screen = SelectedScreen();
330322
switch (_settings.SearchWindowAlign)
331323
{
@@ -346,8 +338,10 @@ private void InitializePosition()
346338
Top = VerticalTop(screen);
347339
break;
348340
case SearchWindowAligns.Custom:
349-
Left = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X + _settings.CustomWindowLeft, 0).X;
350-
Top = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y + _settings.CustomWindowTop).Y;
341+
var customLeft = WindowsInteropHelper.TransformPixelsToDIP(this, screen.WorkingArea.X + _settings.CustomWindowLeft, 0);
342+
var customTop = WindowsInteropHelper.TransformPixelsToDIP(this, 0, screen.WorkingArea.Y + _settings.CustomWindowTop);
343+
Left = customLeft.X;
344+
Top = customTop.Y;
351345
break;
352346
}
353347
}
@@ -357,83 +351,46 @@ private void AdjustPositionForResolutionChange()
357351
{
358352
double screenWidth = SystemParameters.VirtualScreenWidth;
359353
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;
354+
double currentDpiX, currentDpiY;
355+
GetDpi(out currentDpiX, out currentDpiY);
370356

371-
// Get previous window left, top, and DPI X, DPI Y
372357
double previousLeft = _settings.WindowLeft;
373358
double previousTop = _settings.WindowTop;
374-
double previousDpiX = _settings.PreviousDpiX;
375-
double previousDpiY = _settings.PreviousDpiY;
359+
double previousDpiX, previousDpiY;
360+
GetDpi(out previousDpiX, out previousDpiY);
376361

377-
// Calculate ratios for width, height, DPI X, and DPI Y
378-
double widthRatio = currentScreenWidth / _settings.PreviousScreenWidth;
379-
double heightRatio = currentScreenHeight / _settings.PreviousScreenHeight;
362+
double widthRatio = screenWidth / _settings.PreviousScreenWidth;
363+
double heightRatio = screenHeight / _settings.PreviousScreenHeight;
380364
double dpiXRatio = currentDpiX / previousDpiX;
381365
double dpiYRatio = currentDpiY / previousDpiY;
382366

383-
// Adjust previous position according to current resolution and DPI
384367
double newLeft = previousLeft * widthRatio * dpiXRatio;
385368
double newTop = previousTop * heightRatio * dpiYRatio;
386369

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-
}
370+
double screenLeft = SystemParameters.VirtualScreenLeft;
371+
double screenTop = SystemParameters.VirtualScreenTop;
396372

397-
if (newTop + ActualHeight > currentScreenTop + currentScreenHeight)
398-
{
399-
newTop = currentScreenTop + currentScreenHeight - ActualHeight;
400-
}
401-
else if (newTop < currentScreenTop)
402-
{
403-
newTop = currentScreenTop;
404-
}
373+
double maxX = screenLeft + screenWidth - ActualWidth;
374+
double maxY = screenTop + screenHeight - ActualHeight;
405375

406-
// Set the new position
407-
Left = newLeft;
408-
Top = newTop;
376+
Left = Math.Max(screenLeft, Math.Min(newLeft, maxX));
377+
Top = Math.Max(screenTop, Math.Min(newTop, maxY));
409378
}
410379

411-
private double GetDpiX()
380+
private void GetDpi(out double dpiX, out double dpiY)
412381
{
413-
// Get the PresentationSource for this visual
414382
PresentationSource source = PresentationSource.FromVisual(this);
415-
// Check if the PresentationSource and its CompositionTarget are not null
416383
if (source != null && source.CompositionTarget != null)
417384
{
418-
// Get the transform matrix for the CompositionTarget
419385
Matrix m = source.CompositionTarget.TransformToDevice;
420-
// Calculate DPI in X direction
421-
double dpiX = 96 * m.M11;
422-
return dpiX;
386+
dpiX = 96 * m.M11;
387+
dpiY = 96 * m.M22;
423388
}
424-
return 96; //Return a default DPI of 96 if PresentationSource or CompositionTarget is null
425-
}
426-
427-
private double GetDpiY()
428-
{
429-
PresentationSource source = PresentationSource.FromVisual(this);
430-
if (source != null && source.CompositionTarget != null)
389+
else
431390
{
432-
Matrix m = source.CompositionTarget.TransformToDevice;
433-
double dpiY = 96 * m.M22;
434-
return dpiY;
391+
dpiX = 96;
392+
dpiY = 96;
435393
}
436-
return 96;
437394
}
438395
private void UpdateNotifyIconText()
439396
{

0 commit comments

Comments
 (0)