11using Avalonia . Controls ;
22using Avalonia . Media ;
33using CommunityToolkit . Mvvm . ComponentModel ;
4+ using Humanizer ;
45using Stravaig . Avalonia . Controls . Icons ;
56using System ;
67using System . Collections . Generic ;
78using System . Diagnostics ;
89using System . Linq ;
10+ using System . Text . RegularExpressions ;
911
1012namespace 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}
0 commit comments