mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +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 MaxWindowToMonitorRatio = 0.80;
|
||||||
public const double MinWindowHeight = 500;
|
public const double MinWindowHeight = 500;
|
||||||
public const double MinWindowWidth = 680;
|
public const double MinWindowWidth = 500;
|
||||||
public const double WindowWidthContentPadding = 7;
|
public const double WindowWidthContentPadding = 7;
|
||||||
public const double WindowHeightContentPadding = 16;
|
public const double WindowHeightContentPadding = 16;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -167,8 +167,8 @@ namespace Peek.FilePreviewer
|
|||||||
{
|
{
|
||||||
if (Previewer != null)
|
if (Previewer != null)
|
||||||
{
|
{
|
||||||
var size = await Previewer.GetPreviewSizeAsync(cancellationToken);
|
var previewSize = await Previewer.GetPreviewSizeAsync(cancellationToken);
|
||||||
PreviewSizeChanged?.Invoke(this, new PreviewSizeChangedArgs(size));
|
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 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.ComponentModel;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Peek.FilePreviewer.Models;
|
||||||
using Windows.Foundation;
|
using Windows.Foundation;
|
||||||
|
|
||||||
namespace Peek.FilePreviewer.Previewers
|
namespace Peek.FilePreviewer.Previewers
|
||||||
@@ -16,7 +17,7 @@ namespace Peek.FilePreviewer.Previewers
|
|||||||
|
|
||||||
public static bool IsFileTypeSupported(string fileExt) => throw new NotImplementedException();
|
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);
|
Task LoadPreviewAsync(CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ using Peek.Common.Extensions;
|
|||||||
using Peek.Common.Helpers;
|
using Peek.Common.Helpers;
|
||||||
using Peek.Common.Models;
|
using Peek.Common.Models;
|
||||||
using Peek.FilePreviewer.Exceptions;
|
using Peek.FilePreviewer.Exceptions;
|
||||||
|
using Peek.FilePreviewer.Models;
|
||||||
using Peek.FilePreviewer.Previewers.Helpers;
|
using Peek.FilePreviewer.Previewers.Helpers;
|
||||||
using Peek.FilePreviewer.Previewers.Interfaces;
|
using Peek.FilePreviewer.Previewers.Interfaces;
|
||||||
using Windows.Foundation;
|
using Windows.Foundation;
|
||||||
@@ -68,7 +69,7 @@ namespace Peek.FilePreviewer.Previewers
|
|||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Size?> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
public async Task<PreviewSize> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
if (IsSvg(Item))
|
if (IsSvg(Item))
|
||||||
@@ -88,7 +89,7 @@ namespace Peek.FilePreviewer.Previewers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ImageSize;
|
return new PreviewSize { MonitorSize = ImageSize };
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ using Microsoft.UI.Dispatching;
|
|||||||
using Peek.Common.Extensions;
|
using Peek.Common.Extensions;
|
||||||
using Peek.Common.Helpers;
|
using Peek.Common.Helpers;
|
||||||
using Peek.Common.Models;
|
using Peek.Common.Models;
|
||||||
|
using Peek.FilePreviewer.Models;
|
||||||
using Peek.FilePreviewer.Previewers.Interfaces;
|
using Peek.FilePreviewer.Previewers.Interfaces;
|
||||||
using Windows.Foundation;
|
using Windows.Foundation;
|
||||||
using Windows.Media.Core;
|
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();
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
return await Task.Run(Item.GetVideoSize);
|
var videoSize = await Task.Run(Item.GetVideoSize);
|
||||||
|
return new PreviewSize { MonitorSize = videoSize };
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task CopyAsync()
|
public async Task CopyAsync()
|
||||||
|
|||||||
@@ -51,10 +51,11 @@ namespace Peek.FilePreviewer.Previewers
|
|||||||
GC.SuppressFinalize(this);
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<Size?> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
public Task<PreviewSize> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
Size? size = new Size(680, 500);
|
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)
|
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ using Peek.Common.Constants;
|
|||||||
using Peek.Common.Extensions;
|
using Peek.Common.Extensions;
|
||||||
using Peek.Common.Helpers;
|
using Peek.Common.Helpers;
|
||||||
using Peek.Common.Models;
|
using Peek.Common.Models;
|
||||||
|
using Peek.FilePreviewer.Models;
|
||||||
using Windows.Foundation;
|
using Windows.Foundation;
|
||||||
|
|
||||||
namespace Peek.FilePreviewer.Previewers
|
namespace Peek.FilePreviewer.Previewers
|
||||||
@@ -76,10 +77,9 @@ namespace Peek.FilePreviewer.Previewers
|
|||||||
|
|
||||||
private Task<bool>? DisplayInfoTask { get; set; }
|
private Task<bool>? DisplayInfoTask { get; set; }
|
||||||
|
|
||||||
public Task<Size?> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
public Task<PreviewSize> GetPreviewSizeAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
Size? size = null;
|
return Task.FromResult(new PreviewSize { MonitorSize = null });
|
||||||
return Task.FromResult(size);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
public async Task LoadPreviewAsync(CancellationToken cancellationToken)
|
||||||
|
|||||||
@@ -58,5 +58,12 @@ namespace Peek.UI.Extensions
|
|||||||
PInvoke.GetMonitorInfo(monitor, ref info);
|
PInvoke.GetMonitorInfo(monitor, ref info);
|
||||||
return new Size(info.rcMonitor.Size.Width, info.rcMonitor.Size.Height);
|
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)
|
public static double GetMonitorScale(this Window window)
|
||||||
{
|
{
|
||||||
var hwnd = new HWND(window.GetWindowHandle());
|
var hwnd = new HWND(window.GetWindowHandle());
|
||||||
var dpi = PInvoke.GetDpiForWindow(new HWND(hwnd));
|
return hwnd.GetMonitorScale();
|
||||||
double scalingFactor = dpi / 96d;
|
|
||||||
|
|
||||||
return scalingFactor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void BringToForeground(this Window window)
|
public static void BringToForeground(this Window window)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
// The Microsoft Corporation licenses this file to you under the MIT license.
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
||||||
// See the LICENSE file in the project root for more information.
|
// See the LICENSE file in the project root for more information.
|
||||||
|
|
||||||
|
using System;
|
||||||
using interop;
|
using interop;
|
||||||
using Microsoft.PowerToys.Telemetry;
|
using Microsoft.PowerToys.Telemetry;
|
||||||
using Microsoft.UI.Windowing;
|
using Microsoft.UI.Windowing;
|
||||||
@@ -104,42 +105,48 @@ namespace Peek.UI
|
|||||||
private void FilePreviewer_PreviewSizeChanged(object sender, PreviewSizeChangedArgs e)
|
private void FilePreviewer_PreviewSizeChanged(object sender, PreviewSizeChangedArgs e)
|
||||||
{
|
{
|
||||||
var foregroundWindowHandle = Windows.Win32.PInvoke.GetForegroundWindow();
|
var foregroundWindowHandle = Windows.Win32.PInvoke.GetForegroundWindow();
|
||||||
|
|
||||||
var monitorSize = foregroundWindowHandle.GetMonitorSize();
|
var monitorSize = foregroundWindowHandle.GetMonitorSize();
|
||||||
|
var monitorScale = foregroundWindowHandle.GetMonitorScale();
|
||||||
|
|
||||||
// If no size is requested, try to fit to the monitor size.
|
// 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
|
// TODO: Investigate why portrait images do not perfectly fit edge-to-edge
|
||||||
double monitorScale = this.GetMonitorScale();
|
Size monitorMinContentSize = GetMonitorMinContentSize(monitorScale);
|
||||||
double scaledWindowWidth = adjustedContentSize.Width / monitorScale;
|
Size monitorMaxContentSize = GetMonitorMaxContentSize(monitorSize, monitorScale);
|
||||||
double scaledWindowHeight = adjustedContentSize.Height / monitorScale;
|
Size adjustedContentSize = scaledRequestedSize.Fit(monitorMaxContentSize, monitorMinContentSize);
|
||||||
|
|
||||||
double desiredScaledHeight = scaledWindowHeight + titleBarHeight + WindowConstants.WindowWidthContentPadding;
|
var titleBarHeight = TitleBarControl.ActualHeight;
|
||||||
double desiredScaledWidth = scaledWindowWidth + WindowConstants.WindowHeightContentPadding;
|
var desiredWindowHeight = adjustedContentSize.Height + titleBarHeight + WindowConstants.WindowWidthContentPadding;
|
||||||
|
var desiredWindowWidth = adjustedContentSize.Width + WindowConstants.WindowHeightContentPadding;
|
||||||
|
|
||||||
if (!TitleBarControl.Pinned)
|
if (!TitleBarControl.Pinned)
|
||||||
{
|
{
|
||||||
this.CenterOnMonitor(foregroundWindowHandle, desiredScaledWidth, desiredScaledHeight); // re-center if not pinned
|
this.CenterOnMonitor(foregroundWindowHandle, desiredWindowWidth, desiredWindowHeight);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.Show();
|
this.Show();
|
||||||
this.BringToForeground();
|
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>
|
/// <summary>
|
||||||
/// Handle AppWindow closing to prevent app termination on close.
|
/// Handle AppWindow closing to prevent app termination on close.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user