Skip to content

Commit cfef9bd

Browse files
authored
Merge pull request #63 from TheJoeFin/cancel-and-sort
Cancel and sort
2 parents 876c910 + 6443128 commit cfef9bd

17 files changed

Lines changed: 608 additions & 372 deletions

Simple Icon File Maker/Simple Icon File Maker/App.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public App()
7272
services.AddTransient<AboutViewModel>();
7373
services.AddTransient<MultiPage>();
7474
services.AddTransient<MultiViewModel>();
75+
services.AddTransient<SizesControlViewModel>();
7576

7677
// Configuration
7778
services.Configure<LocalSettingsOptions>(context.Configuration.GetSection(nameof(LocalSettingsOptions)));

Simple Icon File Maker/Simple Icon File Maker/Contracts/Services/IIconSizesService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ namespace Simple_Icon_File_Maker.Contracts.Services;
55
public interface IIconSizesService
66
{
77
List<IconSize> IconSizes { get; }
8+
IconSortOrder SortOrder { get; }
89
Task Save(IEnumerable<IconSize> iconSizes);
10+
Task SaveSortOrder(IconSortOrder sortOrder);
911

1012
Task InitializeAsync();
1113
}

Simple Icon File Maker/Simple Icon File Maker/Controls/PreviewStack.xaml.cs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<UserControl
3+
x:Class="Simple_Icon_File_Maker.Controls.SizesControl"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:local="using:Simple_Icon_File_Maker.Controls"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
xmlns:models="using:Simple_Icon_File_Maker.Models"
10+
mc:Ignorable="d">
11+
12+
<Grid RowSpacing="0">
13+
<Grid.RowDefinitions>
14+
<RowDefinition Height="48" />
15+
<RowDefinition Height="Auto" />
16+
<RowDefinition Height="*" />
17+
</Grid.RowDefinitions>
18+
19+
<!-- Header with title and action buttons -->
20+
<Grid Grid.Row="0">
21+
<TextBlock
22+
Margin="16,0,0,0"
23+
VerticalAlignment="Center"
24+
Style="{StaticResource BodyStrongTextBlockStyle}"
25+
Text="Icon sizes" />
26+
27+
<StackPanel
28+
Margin="0,0,8,0"
29+
HorizontalAlignment="Right"
30+
VerticalAlignment="Center"
31+
Orientation="Horizontal"
32+
Spacing="8">
33+
34+
<!-- Edit Sizes Button (Pro) -->
35+
<Button
36+
x:Name="EditSizesButton"
37+
Height="32"
38+
Command="{x:Bind ViewModel.EditIconSizesCommand}"
39+
ToolTipService.ToolTip="Edit icon sizes (Pro only)">
40+
<Grid>
41+
<FontIcon FontSize="16" Glyph="&#xE70F;" />
42+
<FontIcon
43+
FontSize="8"
44+
Foreground="Goldenrod"
45+
Glyph="&#xE785;">
46+
<FontIcon.RenderTransform>
47+
<TransformGroup>
48+
<TranslateTransform X="12" Y="6" />
49+
</TransformGroup>
50+
</FontIcon.RenderTransform>
51+
</FontIcon>
52+
</Grid>
53+
</Button>
54+
55+
<!-- Sort Order Button -->
56+
<DropDownButton
57+
x:Name="SortOrderButton"
58+
Height="32"
59+
ToolTipService.ToolTip="Sort order for .ico file">
60+
<FontIcon FontSize="16" Glyph="&#xE8CB;" />
61+
<DropDownButton.Flyout>
62+
<MenuFlyout>
63+
<MenuFlyoutItem Command="{x:Bind ViewModel.ChangeSortOrderCommand}" CommandParameter="{x:Bind models:IconSortOrder.LargestFirst}">
64+
<MenuFlyoutItem.Icon>
65+
<FontIcon Glyph="&#xE70E;" />
66+
</MenuFlyoutItem.Icon>
67+
<MenuFlyoutItem.Text>Largest First</MenuFlyoutItem.Text>
68+
</MenuFlyoutItem>
69+
<MenuFlyoutItem Command="{x:Bind ViewModel.ChangeSortOrderCommand}" CommandParameter="{x:Bind models:IconSortOrder.SmallestFirst}">
70+
<MenuFlyoutItem.Icon>
71+
<FontIcon Glyph="&#xE70D;" />
72+
</MenuFlyoutItem.Icon>
73+
<MenuFlyoutItem.Text>Smallest First</MenuFlyoutItem.Text>
74+
</MenuFlyoutItem>
75+
</MenuFlyout>
76+
</DropDownButton.Flyout>
77+
</DropDownButton>
78+
79+
<!-- Select Sizes Button -->
80+
<DropDownButton
81+
x:Name="SelectSizesButton"
82+
Height="32"
83+
ToolTipService.ToolTip="Select icon sizes">
84+
<FontIcon FontSize="16" Glyph="&#xE762;" />
85+
<DropDownButton.Flyout>
86+
<MenuFlyout>
87+
<MenuFlyoutItem Command="{x:Bind ViewModel.SelectWindowsSizesCommand}">
88+
<MenuFlyoutItem.Icon>
89+
<FontIcon Glyph="&#xE7F8;" />
90+
</MenuFlyoutItem.Icon>
91+
<MenuFlyoutItem.Text>Windows Sizes</MenuFlyoutItem.Text>
92+
</MenuFlyoutItem>
93+
<MenuFlyoutItem Command="{x:Bind ViewModel.SelectWebSizesCommand}">
94+
<MenuFlyoutItem.Icon>
95+
<FontIcon Glyph="&#xE12B;" />
96+
</MenuFlyoutItem.Icon>
97+
<MenuFlyoutItem.Text>Web Sizes</MenuFlyoutItem.Text>
98+
</MenuFlyoutItem>
99+
<MenuFlyoutSeparator />
100+
<MenuFlyoutItem Command="{x:Bind ViewModel.SelectAllSizesCommand}">
101+
<MenuFlyoutItem.Icon>
102+
<FontIcon Glyph="&#xE8B3;" />
103+
</MenuFlyoutItem.Icon>
104+
<MenuFlyoutItem.Text>Select All</MenuFlyoutItem.Text>
105+
</MenuFlyoutItem>
106+
<MenuFlyoutItem Command="{x:Bind ViewModel.ClearSizeSelectionCommand}">
107+
<MenuFlyoutItem.Icon>
108+
<FontIcon Glyph="&#xE8E6;" />
109+
</MenuFlyoutItem.Icon>
110+
<MenuFlyoutItem.Text>Clear All</MenuFlyoutItem.Text>
111+
</MenuFlyoutItem>
112+
</MenuFlyout>
113+
</DropDownButton.Flyout>
114+
</DropDownButton>
115+
</StackPanel>
116+
</Grid>
117+
118+
<!-- Warning InfoBar -->
119+
<InfoBar
120+
x:Name="SizeDisabledWarning"
121+
Grid.Row="1"
122+
IsOpen="{x:Bind ViewModel.SizeDisabledWarningIsOpen, Mode=OneWay}"
123+
Severity="Informational">
124+
<TextBlock TextWrapping="WrapWholeWords">
125+
Sizes larger than the source have been disabled.
126+
</TextBlock>
127+
</InfoBar>
128+
129+
<!-- Icon Sizes ListView -->
130+
<ListView
131+
x:Name="IconSizesListView"
132+
Grid.Row="2"
133+
ItemsSource="{x:Bind ViewModel.IconSizes, Mode=OneWay}"
134+
SelectionMode="None">
135+
<ListView.ItemTemplate>
136+
<DataTemplate x:DataType="models:IconSize">
137+
<CheckBox
138+
Command="{Binding DataContext.SizeCheckboxTappedCommand, ElementName=IconSizesListView}"
139+
Content="{x:Bind Tooltip}"
140+
IsChecked="{x:Bind IsSelected, Mode=TwoWay}"
141+
IsEnabled="{x:Bind IsEnabled, Mode=OneWay}" />
142+
</DataTemplate>
143+
</ListView.ItemTemplate>
144+
</ListView>
145+
</Grid>
146+
</UserControl>
147+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.UI.Xaml.Controls;
2+
using Simple_Icon_File_Maker.ViewModels;
3+
4+
namespace Simple_Icon_File_Maker.Controls;
5+
6+
public sealed partial class SizesControl : UserControl
7+
{
8+
public SizesControlViewModel ViewModel { get; }
9+
10+
public SizesControl()
11+
{
12+
InitializeComponent();
13+
ViewModel = App.GetService<SizesControlViewModel>();
14+
DataContext = ViewModel;
15+
}
16+
17+
public void UpdateEnabledSizes(int smallerImageSide)
18+
{
19+
ViewModel.UpdateEnabledSizes(smallerImageSide);
20+
}
21+
22+
public void ReloadIconSizes()
23+
{
24+
ViewModel.LoadIconSizes();
25+
}
26+
}
27+

0 commit comments

Comments
 (0)