55using Microsoft . UI . Xaml ;
66using Microsoft . UI . Xaml . Controls ;
77using Microsoft . UI . Xaml . Media . Animation ;
8+ using CommunityToolkit . WinUI . Controls ;
9+ using Microsoft . UI . Xaml . Media ;
10+ using System . Threading . Tasks ;
11+ using System . IO ;
12+ using Windows . Storage ;
13+ using Windows . Foundation ;
814
915namespace Files . App . Dialogs
1016{
@@ -15,12 +21,18 @@ public sealed partial class SettingsDialog : ContentDialog, IDialog<SettingsDial
1521 private FrameworkElement RootAppElement
1622 => ( FrameworkElement ) MainWindow . Instance . Content ;
1723
24+
25+
26+
27+
1828 public SettingsDialog ( )
1929 {
2030 InitializeComponent ( ) ;
2131
2232 MainWindow . Instance . SizeChanged += Current_SizeChanged ;
33+
2334 UpdateDialogLayout ( ) ;
35+ LoadSettingsKeysAsync ( ) ;
2436 }
2537
2638 public new async Task < DialogResult > ShowAsync ( )
@@ -79,6 +91,134 @@ private void MainSettingsNavigationView_SelectionChanged(NavigationView sender,
7991 } ;
8092 }
8193
94+ private Dictionary < string , List < string > > settingsKeysByPage = new ( ) ;
95+ private Dictionary < string , string > keyToPage = new ( ) ;
96+ private Dictionary < string , string > keyToLocalized = new ( ) ;
97+
98+ public async void LoadSettingsKeysAsync ( )
99+ {
100+ try
101+ {
102+ var file = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( Constants . ResourceFilePaths . SettingsStringKeysJsonPath ) ) ;
103+ using var stream = await file . OpenStreamForReadAsync ( ) ;
104+ var dict = await JsonSerializer . DeserializeAsync < Dictionary < string , List < string > > > ( stream ) ;
105+ if ( dict != null )
106+ {
107+ settingsKeysByPage = dict ;
108+ keyToPage . Clear ( ) ;
109+ keyToLocalized . Clear ( ) ;
110+ var resourceLoader = Windows . ApplicationModel . Resources . ResourceLoader . GetForViewIndependentUse ( ) ;
111+ foreach ( var kvp in dict )
112+ {
113+ foreach ( var key in kvp . Value )
114+ {
115+ keyToPage [ key ] = kvp . Key ;
116+ string localized = resourceLoader . GetString ( key ) ;
117+ if ( string . IsNullOrEmpty ( localized ) )
118+ localized = key ;
119+ keyToLocalized [ key ] = localized ;
120+ }
121+ }
122+ }
123+ }
124+ catch { }
125+ }
126+
127+ private void SettingsSearchBox_TextChanged ( AutoSuggestBox sender , AutoSuggestBoxTextChangedEventArgs args )
128+ {
129+ if ( args . Reason != AutoSuggestionBoxTextChangeReason . UserInput )
130+ return ;
131+
132+ var query = sender . Text ? . Trim ( ) . ToLowerInvariant ( ) ?? string . Empty ;
133+
134+ // Debounce: Only update suggestions if query changed
135+ var resourceLoader = Windows . ApplicationModel . Resources . ResourceLoader . GetForViewIndependentUse ( ) ;
136+ string noResults = resourceLoader . GetString ( "NoResultsFound" ) ;
137+ if ( string . IsNullOrEmpty ( query ) )
138+ {
139+ // Show a placeholder for empty search
140+ sender . ItemsSource = new List < SettingSuggestion > {
141+ new SettingSuggestion { Key = null , Localized = noResults }
142+ } ;
143+ return ;
144+ }
145+
146+ var suggestions = keyToLocalized
147+ . Where ( kvp => kvp . Value . ToLowerInvariant ( ) . Contains ( query ) )
148+ . Select ( kvp => new SettingSuggestion { Key = kvp . Key , Localized = kvp . Value } )
149+ . DistinctBy ( s => s . Localized )
150+ . ToList ( ) ;
151+
152+ if ( suggestions . Count == 0 )
153+ {
154+ // Show a placeholder for no results
155+ sender . ItemsSource = new List < SettingSuggestion > {
156+ new SettingSuggestion { Key = null , Localized = noResults }
157+ } ;
158+ }
159+ else
160+ {
161+ sender . ItemsSource = suggestions ;
162+ }
163+ }
164+
165+ private class SettingSuggestion
166+ {
167+ public string Key { get ; set ; }
168+ public string Localized { get ; set ; }
169+ public override string ToString ( ) => Localized ;
170+ }
171+
172+ private void SettingsSearchBox_SuggestionChosen ( AutoSuggestBox sender , AutoSuggestBoxSuggestionChosenEventArgs args )
173+ {
174+ if ( args . SelectedItem is SettingSuggestion suggestion && ! string . IsNullOrEmpty ( suggestion . Key ) )
175+ {
176+ if ( keyToPage . TryGetValue ( suggestion . Key , out var page ) )
177+ {
178+ NavigateToPageByName ( page ) ;
179+ }
180+ }
181+
182+ // Close the suggestion dropdown
183+ sender . IsSuggestionListOpen = false ;
184+ // Clear the query after a suggestion is chosen
185+ sender . Text = string . Empty ;
186+ }
187+
188+ private void SettingsSearchBox_QuerySubmitted ( AutoSuggestBox sender , AutoSuggestBoxQuerySubmittedEventArgs args )
189+ {
190+ var query = args . QueryText ? . Trim ( ) . ToLowerInvariant ( ) ?? string . Empty ;
191+ if ( string . IsNullOrEmpty ( query ) )
192+ {
193+ // Close dropdown and clear text
194+ sender . IsSuggestionListOpen = false ;
195+ sender . Text = string . Empty ;
196+ return ;
197+ }
198+
199+ var match = keyToLocalized . FirstOrDefault ( x => x . Value . ToLowerInvariant ( ) . Contains ( query ) ) ;
200+ if ( ! string . IsNullOrEmpty ( match . Key ) && keyToPage . TryGetValue ( match . Key , out var page ) )
201+ {
202+ NavigateToPageByName ( page ) ;
203+ }
204+
205+ // Close dropdown and clear text
206+ sender . IsSuggestionListOpen = false ;
207+ sender . Text = string . Empty ;
208+ }
209+
210+ private void NavigateToPageByName ( string pageName )
211+ {
212+ foreach ( NavigationViewItem item in MainSettingsNavigationView . MenuItems )
213+ {
214+ if ( ( item . Tag as string ) == pageName )
215+ {
216+ MainSettingsNavigationView . SelectedItem = item ;
217+ break ;
218+ }
219+ }
220+ }
221+
82222 private void ContentDialog_Closing ( ContentDialog sender , ContentDialogClosingEventArgs args )
83223 {
84224 MainWindow . Instance . SizeChanged -= Current_SizeChanged ;
0 commit comments