@@ -25,6 +25,8 @@ public sealed partial class PreviewStack : UserControl
2525
2626 public int SmallerSourceSide { get ; private set ; }
2727
28+ public IconSortOrder SortOrder { get ; set ; } = IconSortOrder . LargestFirst ;
29+
2830 public PreviewStack ( string path , List < IconSize > sizes , bool showTitle = false )
2931 {
3032 StorageFolder sf = ApplicationData . Current . LocalCacheFolder ;
@@ -70,11 +72,19 @@ public void ClearChildren()
7072 PreviewStackPanel . Children . Clear ( ) ;
7173 }
7274
73- public async Task SaveIconAsync ( string outputPath = "" )
75+ public async Task SaveIconAsync ( string outputPath = "" , IconSortOrder sortOrder = IconSortOrder . LargestFirst )
7476 {
7577 MagickImageCollection collection = [ ] ;
7678
77- foreach ( ( _ , string path ) in imagePaths )
79+ // Sort the imagePaths based on the sort order
80+ List < ( string sideLength , string path ) > sortedPaths = sortOrder switch
81+ {
82+ IconSortOrder . LargestFirst => [ .. imagePaths . OrderByDescending ( p => int . TryParse ( p . Item1 , out int size ) ? size : 0 ) ] ,
83+ IconSortOrder . SmallestFirst => [ .. imagePaths . OrderBy ( p => int . TryParse ( p . Item1 , out int size ) ? size : 0 ) ] ,
84+ _ => [ .. imagePaths . OrderByDescending ( p => int . TryParse ( p . Item1 , out int size ) ? size : 0 ) ]
85+ } ;
86+
87+ foreach ( ( _ , string path ) in sortedPaths )
7888 collection . Add ( path ) ;
7989
8090 if ( string . IsNullOrWhiteSpace ( outputPath ) )
@@ -95,15 +105,15 @@ await Task.Run(async () =>
95105 } ) ;
96106 }
97107
98- public async Task SaveAllImagesAsync ( string outputPath = "" )
108+ public async Task SaveAllImagesAsync ( string outputPath = "" , IconSortOrder sortOrder = IconSortOrder . LargestFirst )
99109 {
100110 if ( string . IsNullOrWhiteSpace ( outputPath ) )
101111 {
102112 outputPath = Path . Combine ( Path . GetDirectoryName ( imagePath ) ?? string . Empty ,
103113 $ "{ Path . GetFileNameWithoutExtension ( imagePath ) } .ico") ;
104114 }
105115
106- await SaveIconAsync ( outputPath ) ;
116+ await SaveIconAsync ( outputPath , sortOrder ) ;
107117
108118 string outputFolderPath = Path . GetDirectoryName ( outputPath ) ?? string . Empty ;
109119
@@ -338,13 +348,22 @@ private void ClearOutputImages()
338348
339349 public async Task UpdatePreviewsAsync ( )
340350 {
351+ PreviewStackPanel . Children . Clear ( ) ;
352+
341353 string originalName = Path . GetFileNameWithoutExtension ( imagePath ) ;
342- foreach ( ( string sideLength , string path ) pair in imagePaths )
354+
355+ // Sort imagePaths based on the sort order
356+ List < ( string sideLength , string path ) > sortedPaths = SortOrder switch
343357 {
344- if ( pair . path is not string imagePath )
345- continue ;
358+ IconSortOrder . LargestFirst => [ .. imagePaths . OrderByDescending ( p => int . TryParse ( p . Item1 , out int size ) ? size : 0 ) ] ,
359+ IconSortOrder . SmallestFirst => [ .. imagePaths . OrderBy ( p => int . TryParse ( p . Item1 , out int size ) ? size : 0 ) ] ,
360+ _ => [ .. imagePaths . OrderByDescending ( p => int . TryParse ( p . Item1 , out int size ) ? size : 0 ) ]
361+ } ;
346362
347- if ( ! int . TryParse ( pair . sideLength , out int sideLength ) )
363+ foreach ( ( string sideLength , string path ) pair in sortedPaths )
364+ {
365+ if ( pair . path is not string imagePath
366+ || ! int . TryParse ( pair . sideLength , out int sideLength ) )
348367 continue ;
349368
350369 StorageFile imageSF = await StorageFile . GetFileFromPathAsync ( imagePath ) ;
@@ -357,6 +376,11 @@ public async Task UpdatePreviewsAsync()
357376 await Task . CompletedTask ;
358377 }
359378
379+ public async Task RefreshPreviewsWithSortOrder ( )
380+ {
381+ await UpdatePreviewsAsync ( ) ;
382+ }
383+
360384 private bool CheckIfRefreshIsNeeded ( )
361385 {
362386 if ( imagePaths . Count < 1 )
@@ -384,12 +408,12 @@ public void UpdateSizeAndZoom()
384408
385409 foreach ( UIElement ? child in previewBoxes )
386410 {
387- if ( child is PreviewImage img )
388- {
389- if ( ! double . IsNaN ( ActualWidth ) && ActualWidth > 40 )
390- img . ZoomedWidthSpace = ( int ) ActualWidth - 40 ;
391- img . ZoomPreview = IsZoomingPreview ;
392- }
411+ if ( child is not PreviewImage img )
412+ continue ;
413+
414+ if ( ! double . IsNaN ( ActualWidth ) && ActualWidth > 40 )
415+ img . ZoomedWidthSpace = ( int ) ActualWidth - 40 ;
416+ img . ZoomPreview = IsZoomingPreview ;
393417 }
394418 }
395419}
0 commit comments