27
27
using System . Windows . Media ;
28
28
using System . Windows . Interop ;
29
29
using System . Runtime . InteropServices ;
30
+ using System . Runtime . Intrinsics . Arm ;
30
31
31
32
namespace Flow . Launcher
32
33
{
@@ -292,11 +293,39 @@ private void InitializePosition()
292
293
{
293
294
if ( _settings . SearchWindowScreen == SearchWindowScreens . RememberLastLaunchLocation )
294
295
{
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
296
323
Left = _settings . WindowLeft ;
324
+ Top = _settings . WindowTop ;
297
325
}
298
326
else
299
327
{
328
+ // Keep the existing logic
300
329
var screen = SelectedScreen ( ) ;
301
330
switch ( _settings . SearchWindowAlign )
302
331
{
@@ -322,9 +351,90 @@ private void InitializePosition()
322
351
break ;
323
352
}
324
353
}
354
+ }
325
355
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
326
425
}
327
426
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
+ }
328
438
private void UpdateNotifyIconText ( )
329
439
{
330
440
var menu = contextMenu ;
0 commit comments