[PTRun]Reverted start position to slightly off-center(#34083)

## Summary of the Pull Request
Reverted start-position to slightly off-center while still addressing
multi-monitor/multi-DPI scenarios.
This commit is contained in:
Ani 2024-07-31 17:14:45 +02:00 committed by GitHub
parent 3a080f5efd
commit 77c90b8d98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 11 deletions

View File

@ -10,6 +10,7 @@ using System.Windows;
using System.Windows.Forms; using System.Windows.Forms;
using System.Windows.Interop; using System.Windows.Interop;
using System.Windows.Media; using System.Windows.Media;
using Point = System.Windows.Point;
namespace PowerLauncher.Helper namespace PowerLauncher.Helper
{ {
@ -187,15 +188,18 @@ namespace PowerLauncher.Helper
_ = NativeMethods.SetWindowLong(hwnd, GWL_EX_STYLE, NativeMethods.GetWindowLong(hwnd, GWL_EX_STYLE) | WS_EX_TOOLWINDOW); _ = NativeMethods.SetWindowLong(hwnd, GWL_EX_STYLE, NativeMethods.GetWindowLong(hwnd, GWL_EX_STYLE) | WS_EX_TOOLWINDOW);
} }
public static void MoveToScreenCenter(Window window, Screen screen) /// <summary>
/// Transforms pixels to Device Independent Pixels used by WPF
/// </summary>
/// <param name="visual">current window, required to get presentation source</param>
/// <param name="unitX">horizontal position in pixels</param>
/// <param name="unitY">vertical position in pixels</param>
/// <returns>point containing device independent pixels</returns>
public static Point TransformPixelsToDIP(Visual visual, double unitX, double unitY)
{ {
var workingArea = screen.WorkingArea; var matrix = GetCompositionTarget(visual).TransformFromDevice;
var matrix = GetCompositionTarget(window).TransformFromDevice;
var dpiX = matrix.M11;
var dpiY = matrix.M22;
window.Left = (dpiX * workingArea.Left) + (((dpiX * workingArea.Width) - window.Width) / 2); return new Point((int)(matrix.M11 * unitX), (int)(matrix.M22 * unitY));
window.Top = (dpiY * workingArea.Top) + (((dpiY * workingArea.Height) - window.Height) / 2);
} }
private static CompositionTarget GetCompositionTarget(Visual visual) private static CompositionTarget GetCompositionTarget(Visual visual)

View File

@ -449,14 +449,22 @@ namespace PowerLauncher
// In terms of the hack itself, removing any of these three steps seems to fail in certain scenarios only, // In terms of the hack itself, removing any of these three steps seems to fail in certain scenarios only,
// so be careful with testing! // so be careful with testing!
var desiredScreen = GetScreen(); var desiredScreen = GetScreen();
var workingArea = desiredScreen.WorkingArea;
Point ToDIP(double unitX, double unitY) => WindowsInteropHelper.TransformPixelsToDIP(this, unitX, unitY);
// Move to top-left of desired screen. // Move to top-left of desired screen.
Top = desiredScreen.WorkingArea.Top; Top = workingArea.Top;
Left = desiredScreen.WorkingArea.Left; Left = workingArea.Left;
// Centralize twice. // Centralize twice.
WindowsInteropHelper.MoveToScreenCenter(this, desiredScreen); void MoveToScreenTopCenter()
WindowsInteropHelper.MoveToScreenCenter(this, desiredScreen); {
Left = ((ToDIP(workingArea.Width, 0).X - ActualWidth) / 2) + ToDIP(workingArea.X, 0).X;
Top = ((ToDIP(0, workingArea.Height).Y - SearchBox.ActualHeight) / 4) + ToDIP(0, workingArea.Y).Y;
}
MoveToScreenTopCenter();
MoveToScreenTopCenter();
} }
private void OnLocationChanged(object sender, EventArgs e) private void OnLocationChanged(object sender, EventArgs e)