Skip to content

Commit e22d39e

Browse files
Colin MackayColin Mackay
authored andcommitted
Add filter
1 parent 6f7deb9 commit e22d39e

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

src/Stravaig.Icons.Example/Stravaig.Icons.Example.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
2525
</PackageReference>
2626
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2"/>
27+
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
2728
</ItemGroup>
2829

2930
<ItemGroup>

src/Stravaig.Icons.Example/ViewModels/PhosphorIconDemoViewModel.cs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using Avalonia.Controls;
22
using Avalonia.Media;
33
using CommunityToolkit.Mvvm.ComponentModel;
4+
using Humanizer;
45
using Stravaig.Avalonia.Controls.Icons;
56
using System;
67
using System.Collections.Generic;
78
using System.Diagnostics;
89
using System.Linq;
10+
using System.Text.RegularExpressions;
911

1012
namespace Stravaig.Icons.Example.ViewModels;
1113

@@ -29,6 +31,12 @@ public partial class PhosphorIconDemoViewModel : ViewModelBase
2931
[ObservableProperty]
3032
private string _rgbColour;
3133

34+
[ObservableProperty]
35+
private string? _filter;
36+
37+
[ObservableProperty]
38+
private int _filterCount;
39+
3240
public PhosphorIconDemoViewModel()
3341
{
3442
// In design mode, expand the tree by default so that is open in the designer.
@@ -38,9 +46,10 @@ public PhosphorIconDemoViewModel()
3846
_iconSize = 24;
3947
_colour = new HsvColor(Color.FromRgb(0xFF, 0x00, 0x00));
4048
_rgbColour = _colour.ToRgb().ToString();
49+
_filterCount = Icons.Count;
4150
}
4251

43-
public List<KeyValuePair<PhosphorIconName, string>> Icons { get; } = Enum.GetValues<PhosphorIconName>().Select(n => new KeyValuePair<PhosphorIconName, string>(n, n.ToString())).ToList();
52+
public List<IconViewModel> Icons { get; } = Enum.GetValues<PhosphorIconName>().Select(n => new IconViewModel(n)).ToList();
4453

4554
public List<KeyValuePair<PhosphorIconType, string>> IconTypes { get; } = Enum.GetValues<PhosphorIconType>().Select(t => new KeyValuePair<PhosphorIconType, string>(t, t.ToString())).ToList();
4655

@@ -49,11 +58,68 @@ partial void OnColourChanged(HsvColor value)
4958
RgbColour = value.ToRgb().ToString();
5059
}
5160

61+
partial void OnFilterChanged(string? value)
62+
{
63+
if (string.IsNullOrWhiteSpace(value))
64+
{
65+
// No filter, everything visible.
66+
foreach (var icon in Icons.Where(i => !i.IsVisible))
67+
icon.IsVisible = true;
68+
69+
FilterCount = Icons.Count;
70+
return;
71+
}
72+
73+
value = new string(value.Where(c => char.IsLetterOrDigit(c) || c == '-').ToArray());
74+
75+
var regExValue = $"^.*{value.Kebaberize().Replace("-", ".*")}.*$";
76+
77+
var regEx = new Regex(regExValue, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
78+
var newCount = FilterCount;
79+
foreach (var icon in Icons)
80+
{
81+
if (regEx.IsMatch(icon.Value))
82+
{
83+
if (!icon.IsVisible)
84+
{
85+
icon.IsVisible = true;
86+
newCount++;
87+
}
88+
}
89+
else
90+
{
91+
if (icon.IsVisible)
92+
{
93+
icon.IsVisible = false;
94+
newCount--;
95+
}
96+
}
97+
}
98+
99+
FilterCount = newCount;
100+
}
101+
52102
partial void OnSelectedIconTypeIndexChanged(int? value)
53103
{
54104
if (value.HasValue)
55105
SelectedIconType = IconTypes[value.Value].Key;
56106
Trace.WriteLine($"Selected Icon Type Index Changed to {value}. Type is now {SelectedIconType}.");
57107
}
58108

109+
public partial class IconViewModel : ViewModelBase
110+
{
111+
[ObservableProperty]
112+
private bool _isVisible;
113+
114+
public IconViewModel(PhosphorIconName key)
115+
{
116+
Key = key;
117+
Value = key.ToString().Kebaberize();
118+
IsVisible = true;
119+
}
120+
121+
public PhosphorIconName Key { get; }
122+
123+
public string Value { get; }
124+
}
59125
}

src/Stravaig.Icons.Example/Views/PhosphorIconDemoView.axaml

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
<TextBlock Classes="H1" Text="Phosphor Icons"/>
2525
</Expander.Header>
2626
<StackPanel Orientation="Vertical">
27-
<StackPanel Orientation="Horizontal">
27+
<StackPanel
28+
Margin="5"
29+
Orientation="Horizontal">
2830
<TextBlock
2931
Margin="5"
3032
VerticalAlignment="Center"
@@ -50,7 +52,9 @@
5052
</ComboBox>
5153
</StackPanel>
5254

53-
<StackPanel Orientation="Horizontal">
55+
<StackPanel
56+
Margin="5"
57+
Orientation="Horizontal">
5458
<TextBlock
5559
Margin="5"
5660
Classes="H2"
@@ -80,9 +84,29 @@
8084
Text="{Binding RgbColour}"/>
8185
</StackPanel>
8286

83-
<StackPanel Orientation="Horizontal">
84-
<TextBlock Classes="H2" Margin="5" Text="Filter:"/>
85-
87+
<StackPanel
88+
Margin="5"
89+
Orientation="Horizontal">
90+
<TextBlock
91+
VerticalAlignment="Center"
92+
Classes="H2"
93+
Margin="5"
94+
Text="Filter:"/>
95+
<TextBox
96+
VerticalAlignment="Center"
97+
MinWidth="256"
98+
Text="{Binding Filter}"/>
99+
<TextBlock
100+
Margin="5"
101+
VerticalAlignment="Center">
102+
<TextBlock.Text>
103+
<MultiBinding
104+
StringFormat="Showing {0} of {1} icons.">
105+
<Binding Path="FilterCount"/>
106+
<Binding Path="Icons.Count"/>
107+
</MultiBinding>
108+
</TextBlock.Text>
109+
</TextBlock>
86110
</StackPanel>
87111

88112
<ItemsControl Name="IconsItemsControl" ItemsSource="{Binding Icons}">
@@ -94,6 +118,7 @@
94118
<ItemsControl.ItemTemplate>
95119
<DataTemplate>
96120
<Border
121+
IsVisible="{Binding IsVisible}"
97122
Margin="5"
98123
CornerRadius="5"
99124
BorderBrush="Gray"

0 commit comments

Comments
 (0)