Skip to content

Commit 9c4660d

Browse files
authored
Code Quality: Use single image control for placeholder & thumbnail (#12970)
1 parent 90fb068 commit 9c4660d

File tree

7 files changed

+17
-107
lines changed

7 files changed

+17
-107
lines changed

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

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,11 @@ public bool LoadFileIcon
7373
set => SetProperty(ref loadFileIcon, value);
7474
}
7575

76-
private bool loadDefaultIcon = false;
77-
public bool LoadDefaultIcon
78-
{
79-
get => loadDefaultIcon;
80-
[Obsolete("The set accessor is used internally and should not be used outside ListedItem and derived classes.")]
81-
set => SetProperty(ref loadDefaultIcon, value);
82-
}
83-
8476
private bool loadWebShortcutGlyph;
8577
public bool LoadWebShortcutGlyph
8678
{
8779
get => loadWebShortcutGlyph;
88-
set
89-
{
90-
if (SetProperty(ref loadWebShortcutGlyph, value))
91-
{
92-
LoadDefaultIcon = !value;
93-
}
94-
}
80+
set => SetProperty(ref loadWebShortcutGlyph, value);
9581
}
9682

9783
private bool loadCustomIcon;
@@ -187,43 +173,14 @@ public BitmapImage FileImage
187173
get => fileImage;
188174
set
189175
{
190-
if (fileImage is BitmapImage imgOld)
191-
{
192-
imgOld.ImageOpened -= Img_ImageOpened;
193-
}
194176
if (SetProperty(ref fileImage, value))
195177
{
196-
if (value is BitmapImage img)
197-
{
198-
if (img.PixelWidth > 0)
199-
{
200-
Img_ImageOpened(img, null);
201-
}
202-
else
203-
{
204-
img.ImageOpened += Img_ImageOpened;
205-
}
206-
}
207-
}
208-
}
209-
}
210-
211-
private void Img_ImageOpened(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
212-
{
213-
if (sender is BitmapImage image)
214-
{
215-
image.ImageOpened -= Img_ImageOpened;
216-
217-
if (image.PixelWidth > 0)
218-
{
219-
SafetyExtensions.IgnoreExceptions(() =>
178+
if (value is BitmapImage)
220179
{
221180
LoadFileIcon = true;
222-
PlaceholderDefaultIcon = null;
223181
NeedsPlaceholderGlyph = false;
224-
LoadDefaultIcon = false;
225182
LoadWebShortcutGlyph = false;
226-
}, App.Logger); // 2009482836u
183+
}
227184
}
228185
}
229186
}
@@ -243,13 +200,6 @@ public BitmapImage IconOverlay
243200
}
244201
}
245202

246-
private BitmapImage placeholderDefaultIcon;
247-
public BitmapImage PlaceholderDefaultIcon
248-
{
249-
get => placeholderDefaultIcon;
250-
set => SetProperty(ref placeholderDefaultIcon, value);
251-
}
252-
253203
private BitmapImage shieldIcon;
254204
public BitmapImage ShieldIcon
255205
{
@@ -454,13 +404,6 @@ public void UpdateContainsFilesFolders()
454404
ContainsFilesOrFolders = FolderHelpers.CheckForFilesFolders(ItemPath);
455405
}
456406

457-
public void SetDefaultIcon(BitmapImage img)
458-
{
459-
NeedsPlaceholderGlyph = false;
460-
LoadDefaultIcon = true;
461-
PlaceholderDefaultIcon = img;
462-
}
463-
464407
private bool CheckElevationRights()
465408
{
466409
return IsShortcut

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,11 @@ private async Task LoadItemThumbnail(ListedItem item, uint thumbnailSize = 96, I
913913
{
914914
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
915915
{
916-
item.FileImage ??= new BitmapImage();
917-
item.FileImage.DecodePixelType = DecodePixelType.Logical;
918-
item.FileImage.DecodePixelWidth = (int)thumbnailSize;
919-
await item.FileImage.SetSourceAsync(Thumbnail);
916+
var img = new BitmapImage();
917+
img.DecodePixelType = DecodePixelType.Logical;
918+
img.DecodePixelWidth = (int)thumbnailSize;
919+
await img.SetSourceAsync(Thumbnail);
920+
item.FileImage = img;
920921
if (!string.IsNullOrEmpty(item.FileExtension) &&
921922
!item.IsShortcut && !item.IsExecutable &&
922923
!ImagePreviewViewModel.ContainsExtension(item.FileExtension.ToLowerInvariant()))
@@ -983,10 +984,11 @@ await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
983984
{
984985
await dispatcherQueue.EnqueueOrInvokeAsync(async () =>
985986
{
986-
item.FileImage ??= new BitmapImage();
987-
item.FileImage.DecodePixelType = DecodePixelType.Logical;
988-
item.FileImage.DecodePixelWidth = (int)thumbnailSize;
989-
await item.FileImage.SetSourceAsync(Thumbnail);
987+
var img = new BitmapImage();
988+
img.DecodePixelType = DecodePixelType.Logical;
989+
img.DecodePixelWidth = (int)thumbnailSize;
990+
await img.SetSourceAsync(Thumbnail);
991+
item.FileImage = img;
990992
}, Microsoft.UI.Dispatching.DispatcherQueuePriority.Normal);
991993
wasIconLoaded = true;
992994
}

src/Files.App/Utils/StorageEnumerators/UniversalStorageEnumerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ ex is UnauthorizedAccessException
8989
{
9090
if (defaultIconPairs?.ContainsKey(string.Empty) ?? false)
9191
{
92-
folder.SetDefaultIcon(defaultIconPairs[string.Empty]);
92+
folder.FileImage = defaultIconPairs[string.Empty];
9393
}
9494
tempList.Add(folder);
9595
}
@@ -106,7 +106,7 @@ ex is UnauthorizedAccessException
106106
var lowercaseExtension = fileEntry.FileExtension.ToLowerInvariant();
107107
if (defaultIconPairs.ContainsKey(lowercaseExtension))
108108
{
109-
fileEntry.SetDefaultIcon(defaultIconPairs[lowercaseExtension]);
109+
fileEntry.FileImage = defaultIconPairs[lowercaseExtension];
110110
}
111111
}
112112
}

src/Files.App/Utils/StorageEnumerators/Win32StorageEnumerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static async Task<List<ListedItem>> ListEntries(
6363
var lowercaseExtension = file.FileExtension.ToLowerInvariant();
6464
if (defaultIconPairs.ContainsKey(lowercaseExtension))
6565
{
66-
file.SetDefaultIcon(defaultIconPairs[lowercaseExtension]);
66+
file.FileImage = defaultIconPairs[lowercaseExtension];
6767
}
6868
}
6969
}
@@ -86,7 +86,7 @@ public static async Task<List<ListedItem>> ListEntries(
8686
if (defaultIconPairs?.ContainsKey(string.Empty) ?? false)
8787
{
8888
// Set folder icon (found by empty extension string)
89-
folder.SetDefaultIcon(defaultIconPairs[string.Empty]);
89+
folder.FileImage = defaultIconPairs[string.Empty];
9090
}
9191
tempList.Add(folder);
9292
++count;

src/Files.App/Views/LayoutModes/ColumnViewBase.xaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,6 @@
218218
Source="{x:Bind FileImage, Mode=OneWay}"
219219
Stretch="Uniform" />
220220
</ContentPresenter>
221-
<Image
222-
x:Name="FolderGlyphIcon"
223-
Width="20"
224-
Height="20"
225-
HorizontalAlignment="Stretch"
226-
VerticalAlignment="Stretch"
227-
x:Load="{x:Bind LoadDefaultIcon, Mode=OneWay}"
228-
Source="{x:Bind PlaceholderDefaultIcon, Mode=OneWay}" />
229221
<Border
230222
x:Name="TypeUnknownGlyph"
231223
Width="20"

src/Files.App/Views/LayoutModes/DetailsLayoutBrowser.xaml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -915,14 +915,6 @@
915915
Source="{x:Bind FileImage, Mode=OneWay}"
916916
Stretch="Uniform" />
917917
</ContentPresenter>
918-
<Image
919-
x:Name="FolderGlyphIcon"
920-
Width="20"
921-
Height="20"
922-
HorizontalAlignment="Stretch"
923-
VerticalAlignment="Stretch"
924-
x:Load="{x:Bind LoadDefaultIcon, Mode=OneWay}"
925-
Source="{x:Bind PlaceholderDefaultIcon, Mode=OneWay}" />
926918
<Border
927919
x:Name="TypeUnknownGlyph"
928920
Width="20"

src/Files.App/Views/LayoutModes/GridViewBrowser.xaml

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,6 @@
9292
Stretch="Uniform" />
9393
</ContentPresenter>
9494

95-
<Image
96-
x:Name="FolderGlyphIcon"
97-
MaxWidth="{Binding FolderSettings.GridViewSize, ElementName=PageRoot, Mode=OneWay}"
98-
MaxHeight="{Binding FolderSettings.GridViewSize, ElementName=PageRoot, Mode=OneWay}"
99-
Margin="12"
100-
HorizontalAlignment="Stretch"
101-
VerticalAlignment="Stretch"
102-
x:Load="{x:Bind LoadDefaultIcon, Mode=OneWay}"
103-
Source="{x:Bind PlaceholderDefaultIcon, Mode=OneWay}" />
104-
10595
<Border
10696
x:Name="TypeUnknownGlyph"
10797
MaxWidth="{Binding FolderSettings.GridViewSize, ElementName=PageRoot, Mode=OneWay}"
@@ -332,15 +322,6 @@
332322
Stretch="Uniform" />
333323
</ContentPresenter>
334324

335-
<Image
336-
x:Name="FolderGlyphIcon"
337-
Width="60"
338-
Height="60"
339-
HorizontalAlignment="Stretch"
340-
VerticalAlignment="Stretch"
341-
x:Load="{x:Bind LoadDefaultIcon, Mode=OneWay}"
342-
Source="{x:Bind PlaceholderDefaultIcon, Mode=OneWay}" />
343-
344325
<Border
345326
x:Name="TypeUnknownGlyph"
346327
Width="60"

0 commit comments

Comments
 (0)