mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-04 18:26:39 +02:00
Use effective pixels for unsupported previewer (#26345)
This commit is contained in:
@@ -8,7 +8,7 @@ namespace Peek.Common.Constants
|
||||
{
|
||||
public const double MaxWindowToMonitorRatio = 0.80;
|
||||
public const double MinWindowHeight = 500;
|
||||
public const double MinWindowWidth = 680;
|
||||
public const double MinWindowWidth = 500;
|
||||
public const double WindowWidthContentPadding = 7;
|
||||
public const double WindowHeightContentPadding = 16;
|
||||
}
|
||||
|
||||
@@ -167,8 +167,8 @@ namespace Peek.FilePreviewer
|
||||
{
|
||||
if (Previewer != null)
|
||||
{
|
||||
var size = await Previewer.GetPreviewSizeAsync(cancellationToken);
|
||||
PreviewSizeChanged?.Invoke(this, new PreviewSizeChangedArgs(size));
|
||||
var previewSize = await Previewer.GetPreviewSizeAsync(cancellationToken);
|
||||
PreviewSizeChanged?.Invoke(this, new PreviewSizeChangedArgs(previewSize));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
15
src/modules/peek/Peek.FilePreviewer/Models/PreviewSize.cs
Normal file
15
src/modules/peek/Peek.FilePreviewer/Models/PreviewSize.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
// Copyright (c) Microsoft Corporation
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace Peek.FilePreviewer.Models
|
||||
{
|
||||
public record PreviewSize
|
||||
{
|
||||
public Size? MonitorSize { get; init; }
|
||||
|
||||
public bool UseEffectivePixels { get; init; }
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,11 @@ namespace Peek.FilePreviewer.Models
|
||||
{
|
||||
public class PreviewSizeChangedArgs
|
||||
{
|
||||
public PreviewSizeChangedArgs(Size? windowSizeRequested)
|
||||
public PreviewSizeChangedArgs(PreviewSize previewSize)
|
||||
{
|
||||
WindowSizeRequested = windowSizeRequested;
|
||||
PreviewSize = previewSize;
|
||||
}
|
||||
|
||||
public Size? WindowSizeRequested { get; init; }
|
||||
public PreviewSize PreviewSize { get; init; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ using System;
|
||||
using System.ComponentModel;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Peek.FilePreviewer.Models;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers
|
||||
@@ -16,7 +17,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
public static bool IsFileTypeSupported(string fileExt) => throw new NotImplementedException();
|
||||
|
||||
public Task<Size?> GetPreviewSizeAsync(CancellationToken cancellationToken);
|
||||
public Task<PreviewSize> GetPreviewSizeAsync(CancellationToken cancellationToken);
|
||||
|
||||
Task LoadPreviewAsync(CancellationToken cancellationToken);
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ using Peek.Common.Extensions;
|
||||
using Peek.Common.Helpers;
|
||||
using Peek.Common.Models;
|
||||
using Peek.FilePreviewer.Exceptions;
|
||||
using Peek.FilePreviewer.Models;
|
||||
using Peek.FilePreviewer.Previewers.Helpers;
|
||||
using Peek.FilePreviewer.Previewers.Interfaces;
|
||||
using Windows.Foundation;
|
||||
@@ -68,7 +69,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public async Task<Size?> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
public async Task<PreviewSize> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
if (IsSvg(Item))
|
||||
@@ -88,7 +89,7 @@ namespace Peek.FilePreviewer.Previewers
|
||||
}
|
||||
}
|
||||
|
||||
return ImageSize;
|
||||
return new PreviewSize { MonitorSize = ImageSize };
|
||||
}
|
||||
|
||||
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
||||
|
||||
@@ -13,6 +13,7 @@ using Microsoft.UI.Dispatching;
|
||||
using Peek.Common.Extensions;
|
||||
using Peek.Common.Helpers;
|
||||
using Peek.Common.Models;
|
||||
using Peek.FilePreviewer.Models;
|
||||
using Peek.FilePreviewer.Previewers.Interfaces;
|
||||
using Windows.Foundation;
|
||||
using Windows.Media.Core;
|
||||
@@ -74,10 +75,11 @@ namespace Peek.FilePreviewer.Previewers
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Size?> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
public async Task<PreviewSize> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return await Task.Run(Item.GetVideoSize);
|
||||
var videoSize = await Task.Run(Item.GetVideoSize);
|
||||
return new PreviewSize { MonitorSize = videoSize };
|
||||
}
|
||||
|
||||
public async Task CopyAsync()
|
||||
|
||||
@@ -51,10 +51,11 @@ namespace Peek.FilePreviewer.Previewers
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public Task<Size?> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
public Task<PreviewSize> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Size? size = new Size(680, 500);
|
||||
return Task.FromResult(size);
|
||||
var previewSize = new PreviewSize { MonitorSize = size, UseEffectivePixels = true };
|
||||
return Task.FromResult(previewSize);
|
||||
}
|
||||
|
||||
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
||||
|
||||
@@ -12,6 +12,7 @@ using Peek.Common.Constants;
|
||||
using Peek.Common.Extensions;
|
||||
using Peek.Common.Helpers;
|
||||
using Peek.Common.Models;
|
||||
using Peek.FilePreviewer.Models;
|
||||
using Windows.Foundation;
|
||||
|
||||
namespace Peek.FilePreviewer.Previewers
|
||||
@@ -76,10 +77,9 @@ namespace Peek.FilePreviewer.Previewers
|
||||
|
||||
private Task<bool>? DisplayInfoTask { get; set; }
|
||||
|
||||
public Task<Size?> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
public Task<PreviewSize> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
Size? size = null;
|
||||
return Task.FromResult(size);
|
||||
return Task.FromResult(new PreviewSize { MonitorSize = null });
|
||||
}
|
||||
|
||||
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
||||
|
||||
@@ -58,5 +58,12 @@ namespace Peek.UI.Extensions
|
||||
PInvoke.GetMonitorInfo(monitor, ref info);
|
||||
return new Size(info.rcMonitor.Size.Width, info.rcMonitor.Size.Height);
|
||||
}
|
||||
|
||||
internal static double GetMonitorScale(this HWND hwnd)
|
||||
{
|
||||
var dpi = PInvoke.GetDpiForWindow(hwnd);
|
||||
var scalingFactor = dpi / 96d;
|
||||
return scalingFactor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,7 @@ namespace Peek.UI.Extensions
|
||||
public static double GetMonitorScale(this Window window)
|
||||
{
|
||||
var hwnd = new HWND(window.GetWindowHandle());
|
||||
var dpi = PInvoke.GetDpiForWindow(new HWND(hwnd));
|
||||
double scalingFactor = dpi / 96d;
|
||||
|
||||
return scalingFactor;
|
||||
return hwnd.GetMonitorScale();
|
||||
}
|
||||
|
||||
public static void BringToForeground(this Window window)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using interop;
|
||||
using Microsoft.PowerToys.Telemetry;
|
||||
using Microsoft.UI.Windowing;
|
||||
@@ -104,42 +105,48 @@ namespace Peek.UI
|
||||
private void FilePreviewer_PreviewSizeChanged(object sender, PreviewSizeChangedArgs e)
|
||||
{
|
||||
var foregroundWindowHandle = Windows.Win32.PInvoke.GetForegroundWindow();
|
||||
|
||||
var monitorSize = foregroundWindowHandle.GetMonitorSize();
|
||||
var monitorScale = foregroundWindowHandle.GetMonitorScale();
|
||||
|
||||
// If no size is requested, try to fit to the monitor size.
|
||||
Size requestedSize = e.WindowSizeRequested ?? monitorSize;
|
||||
Size requestedSize = e.PreviewSize.MonitorSize ?? monitorSize;
|
||||
var contentScale = e.PreviewSize.UseEffectivePixels ? 1 : monitorScale;
|
||||
Size scaledRequestedSize = new Size(requestedSize.Width / contentScale, requestedSize.Height / contentScale);
|
||||
|
||||
double titleBarHeight = TitleBarControl.ActualHeight;
|
||||
|
||||
double maxContentWidth = monitorSize.Width * WindowConstants.MaxWindowToMonitorRatio;
|
||||
double maxContentHeight = (monitorSize.Height - titleBarHeight) * WindowConstants.MaxWindowToMonitorRatio;
|
||||
Size maxContentSize = new(maxContentWidth, maxContentHeight);
|
||||
|
||||
double minContentWidth = WindowConstants.MinWindowWidth;
|
||||
double minContentHeight = WindowConstants.MinWindowHeight - titleBarHeight;
|
||||
Size minContentSize = new(minContentWidth, minContentHeight);
|
||||
|
||||
Size adjustedContentSize = requestedSize.Fit(maxContentSize, minContentSize);
|
||||
|
||||
// TODO: Only re-center if window has not been resized by user (or use design-defined logic).
|
||||
// TODO: Investigate why portrait images do not perfectly fit edge-to-edge
|
||||
double monitorScale = this.GetMonitorScale();
|
||||
double scaledWindowWidth = adjustedContentSize.Width / monitorScale;
|
||||
double scaledWindowHeight = adjustedContentSize.Height / monitorScale;
|
||||
Size monitorMinContentSize = GetMonitorMinContentSize(monitorScale);
|
||||
Size monitorMaxContentSize = GetMonitorMaxContentSize(monitorSize, monitorScale);
|
||||
Size adjustedContentSize = scaledRequestedSize.Fit(monitorMaxContentSize, monitorMinContentSize);
|
||||
|
||||
double desiredScaledHeight = scaledWindowHeight + titleBarHeight + WindowConstants.WindowWidthContentPadding;
|
||||
double desiredScaledWidth = scaledWindowWidth + WindowConstants.WindowHeightContentPadding;
|
||||
var titleBarHeight = TitleBarControl.ActualHeight;
|
||||
var desiredWindowHeight = adjustedContentSize.Height + titleBarHeight + WindowConstants.WindowWidthContentPadding;
|
||||
var desiredWindowWidth = adjustedContentSize.Width + WindowConstants.WindowHeightContentPadding;
|
||||
|
||||
if (!TitleBarControl.Pinned)
|
||||
{
|
||||
this.CenterOnMonitor(foregroundWindowHandle, desiredScaledWidth, desiredScaledHeight); // re-center if not pinned
|
||||
this.CenterOnMonitor(foregroundWindowHandle, desiredWindowWidth, desiredWindowHeight);
|
||||
}
|
||||
|
||||
this.Show();
|
||||
this.BringToForeground();
|
||||
}
|
||||
|
||||
private Size GetMonitorMaxContentSize(Size monitorSize, double scaling)
|
||||
{
|
||||
var titleBarHeight = TitleBarControl.ActualHeight;
|
||||
var maxContentWidth = monitorSize.Width * WindowConstants.MaxWindowToMonitorRatio;
|
||||
var maxContentHeight = (monitorSize.Height - titleBarHeight) * WindowConstants.MaxWindowToMonitorRatio;
|
||||
return new Size(maxContentWidth / scaling, maxContentHeight / scaling);
|
||||
}
|
||||
|
||||
private Size GetMonitorMinContentSize(double scaling)
|
||||
{
|
||||
var titleBarHeight = TitleBarControl.ActualHeight;
|
||||
var minContentWidth = WindowConstants.MinWindowWidth;
|
||||
var minContentHeight = WindowConstants.MinWindowHeight - titleBarHeight;
|
||||
return new Size(minContentWidth / scaling, minContentHeight / scaling);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle AppWindow closing to prevent app termination on close.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user