22using System . Windows . Controls ;
33using System . ComponentModel ;
44using System . Windows . Data ;
5+ using System ;
6+ using System . Collections ;
7+ using System . Collections . Generic ;
58
69namespace Flow . Launcher . Plugin . WebSearch
710{
@@ -19,6 +22,40 @@ public SettingsControl(PluginInitContext context, SettingsViewModel viewModel)
1922 _context = context ;
2023 _settings = viewModel . Settings ;
2124 DataContext = viewModel ;
25+ this . Loaded += SettingsControl_Loaded ;
26+ }
27+
28+ private void SettingsControl_Loaded ( object sender , RoutedEventArgs e )
29+ {
30+ // After the ListView is loaded, sort by Tag in ascending order
31+ if ( SearchSourcesListView . ItemsSource != null )
32+ {
33+ // Apply initial sorting by Tag column
34+ Sort ( "Tag" , ListSortDirection . Ascending ) ;
35+
36+ // Display an arrow on the sorted column (optional)
37+ var tagColumn = GetColumnByHeader ( "Tag" ) ;
38+ if ( tagColumn != null )
39+ {
40+ tagColumn . HeaderTemplate = Resources [ "HeaderTemplateArrowUp" ] as DataTemplate ;
41+ _lastHeaderClicked = tagColumn . Header as GridViewColumnHeader ;
42+ _lastDirection = ListSortDirection . Ascending ;
43+ }
44+ }
45+ }
46+
47+ // Find column by header name
48+ private GridViewColumn GetColumnByHeader ( string header )
49+ {
50+ if ( SearchSourcesListView . View is GridView gridView )
51+ {
52+ foreach ( var column in gridView . Columns )
53+ {
54+ if ( column . Header != null && column . Header . ToString ( ) == header )
55+ return column ;
56+ }
57+ }
58+ return null ;
2259 }
2360
2461 private void OnAddSearchSearchClick ( object sender , RoutedEventArgs e )
@@ -122,8 +159,23 @@ private void Sort(string sortBy, ListSortDirection direction)
122159 {
123160 ICollectionView dataView = CollectionViewSource . GetDefaultView ( SearchSourcesListView . ItemsSource ) ;
124161 dataView . SortDescriptions . Clear ( ) ;
125- SortDescription sd = new ( sortBy , direction ) ;
126- dataView . SortDescriptions . Add ( sd ) ;
162+
163+ // Special handling for Tag sorting
164+ if ( sortBy == "Tag" )
165+ {
166+ // Apply custom sorting (using TagComparer)
167+ if ( dataView is ListCollectionView listView )
168+ {
169+ listView . CustomSort = new TagComparer ( direction ) ;
170+ }
171+ }
172+ else
173+ {
174+ // Normal sorting
175+ SortDescription sd = new SortDescription ( sortBy , direction ) ;
176+ dataView . SortDescriptions . Add ( sd ) ;
177+ }
178+
127179 dataView . Refresh ( ) ;
128180 }
129181
@@ -139,5 +191,48 @@ private void MouseDoubleClickItem(object sender, System.Windows.Input.MouseButto
139191 webSearch . ShowDialog ( ) ;
140192 }
141193 }
194+
195+
196+ public class TagComparer : IComparer
197+ {
198+ private readonly ListSortDirection _direction ;
199+
200+ public TagComparer ( ListSortDirection direction )
201+ {
202+ _direction = direction ;
203+ }
204+
205+ public int Compare ( object x , object y )
206+ {
207+ if ( x is SearchSource sourceX && y is SearchSource sourceY )
208+ {
209+ string tagX = sourceX . Tag ;
210+ string tagY = sourceY . Tag ;
211+
212+ bool isEmptyX = string . IsNullOrWhiteSpace ( tagX ) ;
213+ bool isEmptyY = string . IsNullOrWhiteSpace ( tagY ) ;
214+
215+ // If both are empty tags, they are equal
216+ if ( isEmptyX && isEmptyY )
217+ return 0 ;
218+
219+ // If only x is an empty tag, it always goes to the back
220+ if ( isEmptyX )
221+ return 1 ;
222+
223+ // If only y is an empty tag, it always goes to the front
224+ if ( isEmptyY )
225+ return - 1 ;
226+
227+ // If both have tags, compare as normal strings
228+ int result = string . Compare ( tagX , tagY , StringComparison . OrdinalIgnoreCase ) ;
229+
230+ // Reverse the result according to the sorting direction
231+ return _direction == ListSortDirection . Ascending ? result : - result ;
232+ }
233+
234+ return 0 ;
235+ }
236+ }
142237 }
143238}
0 commit comments