Skip to content

Commit 4c20201

Browse files
Code Quality: Ensure minimum size for UI elements based on DPI scaling (#17462)
Co-authored-by: seer-by-sentry[bot] <157164994+seer-by-sentry[bot]@users.noreply.github.com>
1 parent 0e231d6 commit 4c20201

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

src/Files.App/Data/Items/WidgetFolderCardItem.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public async Task LoadCardThumbnailAsync()
4343
if (string.IsNullOrEmpty(Path))
4444
return;
4545

46-
Item.TryGetThumbnail((int)(Constants.ShellIconSizes.Large * App.AppModel.AppWindowDPI), SIIGBF.SIIGBF_ICONONLY, out var rawThumbnailData);
46+
var thumbnailSize = (int)(Constants.ShellIconSizes.Large * App.AppModel.AppWindowDPI);
47+
// Ensure thumbnail size is at least 1 to prevent layout errors
48+
thumbnailSize = Math.Max(1, thumbnailSize);
49+
Item.TryGetThumbnail(thumbnailSize, SIIGBF.SIIGBF_ICONONLY, out var rawThumbnailData);
4750
if (rawThumbnailData is null)
4851
return;
4952

src/Files.App/Data/Models/AppModel.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,18 @@ public string PCloudDrivePath
129129
/// <summary>
130130
/// Gets or sets a value indicating the AppWindow DPI.
131131
/// </summary>
132-
private float _AppWindowDPI = PInvoke.GetDpiForWindow((HWND)MainWindow.Instance.WindowHandle) / 96f;
132+
private float? _AppWindowDPI = null;
133133
public float AppWindowDPI
134134
{
135-
get => _AppWindowDPI;
135+
get
136+
{
137+
if (_AppWindowDPI is null || _AppWindowDPI == 0f)
138+
{
139+
var dpi = PInvoke.GetDpiForWindow((HWND)MainWindow.Instance.WindowHandle);
140+
_AppWindowDPI = dpi > 0 ? dpi / 96f : 1.0f; // Fallback to 1.0f if invalid DPI
141+
}
142+
return _AppWindowDPI.Value;
143+
}
136144
set => SetProperty(ref _AppWindowDPI, value);
137145
}
138146
}

src/Files.App/Utils/Storage/Helpers/FilePropertiesHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public static void OpenPropertiesWindow(object item, IShellPage associatedInstan
100100
propertiesWindow.Closed += PropertiesWindow_Closed;
101101
}
102102

103-
var width = Convert.ToInt32(800 * App.AppModel.AppWindowDPI);
104-
var height = Convert.ToInt32(500 * App.AppModel.AppWindowDPI);
103+
var width = Math.Max(1, Convert.ToInt32(800 * App.AppModel.AppWindowDPI));
104+
var height = Math.Max(1, Convert.ToInt32(500 * App.AppModel.AppWindowDPI));
105105

106106
propertiesWindow.AppWindow.Resize(new(width, height));
107107
propertiesWindow.IsMinimizable = false;

src/Files.App/Utils/Storage/Helpers/FileThumbnailHelper.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public static class FileThumbnailHelper
1313
public static async Task<byte[]?> GetIconAsync(string path, uint requestedSize, bool isFolder, IconOptions iconOptions)
1414
{
1515
var size = iconOptions.HasFlag(IconOptions.UseCurrentScale) ? requestedSize * App.AppModel.AppWindowDPI : requestedSize;
16+
// Ensure size is at least 1 to prevent layout errors
17+
size = Math.Max(1, size);
1618

1719
return await Win32Helper.StartSTATask(() => Win32Helper.GetIcon(path, (int)size, isFolder, iconOptions));
1820
}

0 commit comments

Comments
 (0)