11using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Threading ;
45using System . Threading . Tasks ;
@@ -19,11 +20,11 @@ internal sealed partial class SearchWebPage : DynamicListPage
1920
2021 private readonly WebSearchShortcutDataEntry _shortcut ;
2122
22- private readonly IListItem _openHomepageListItem ;
23- private readonly IContextItem _openHomepageContextItem ;
23+ private readonly ListItem _openHomepageListItem ;
24+ private readonly CommandContextItem _openHomepageContextItem ;
2425
26+ private ListItem [ ] _suggestionItems = [ ] ;
2527 private IListItem [ ] _items = [ ] ;
26- private IListItem [ ] _suggestionItems = [ ] ;
2728
2829 private int _lastUpdateSearchTextEpoch ;
2930 private readonly Lock _swapSuggestionsCancellationSourceLock = new ( ) ;
@@ -113,12 +114,12 @@ public override async void UpdateSearchText(string oldSearch, string newSearch)
113114 var primaryItems = BuildPrimaryItems ( newSearch ) ;
114115 var snapshotSuggestions = Volatile . Read ( ref _suggestionItems ) ;
115116
116- RenderItems ( [ .. primaryItems , .. historyItems , .. snapshotSuggestions ] , currentEpoch ) ;
117+ RenderItems ( ItemIntegrate ( primaryItems , historyItems , snapshotSuggestions ) , currentEpoch ) ;
117118
118119 if ( ! shouldFetchSuggestions )
119120 return ;
120121
121- IListItem [ ] suggestionItems ;
122+ ListItem [ ] suggestionItems ;
122123 try
123124 {
124125 suggestionItems = await FetchSuggestionItemsAsync ( newSearch , currentCancellationSource ! . Token ) . ConfigureAwait ( false ) ;
@@ -145,7 +146,69 @@ public override async void UpdateSearchText(string oldSearch, string newSearch)
145146
146147 UpdateSuggestionItems ( suggestionItems , currentEpoch ) ;
147148
148- RenderItems ( [ .. primaryItems , .. historyItems , .. suggestionItems ] , currentEpoch ) ;
149+ RenderItems ( ItemIntegrate ( primaryItems , historyItems , suggestionItems ) , currentEpoch ) ;
150+ }
151+
152+ private static IListItem [ ] ItemIntegrate ( ListItem [ ] primaryItems , ListItem [ ] historyItems , ListItem [ ] suggestionItems )
153+ {
154+ var primaryItemByTitle = primaryItems . ToDictionary ( item => item . Title , item => item ) ;
155+ var historyItemByTitle = historyItems . ToDictionary ( item => item . Title , item => item ) ;
156+ var suggestionItemByTitle = suggestionItems . ToDictionary ( item => item . Title , item => item ) ;
157+
158+ HashSet < string > removeFromHistory = [ ] ;
159+ HashSet < string > removeFromSuggestions = [ ] ;
160+
161+ foreach ( var ( title , primaryItem ) in primaryItemByTitle )
162+ {
163+ if ( historyItemByTitle . TryGetValue ( title , out var historyItem ) )
164+ {
165+ primaryItem . Icon = Icons . History ;
166+ primaryItem . MoreCommands = historyItem . MoreCommands ;
167+
168+ if ( suggestionItemByTitle . TryGetValue ( title , out var suggestionItem ) )
169+ {
170+ historyItem . Subtitle = suggestionItem . Subtitle ;
171+ removeFromSuggestions . Add ( title ) ;
172+ }
173+ else
174+ {
175+ removeFromHistory . Add ( title ) ;
176+ }
177+ }
178+ }
179+
180+ foreach ( var ( title , historyItem ) in historyItemByTitle )
181+ {
182+ if ( removeFromHistory . Contains ( title ) )
183+ continue ;
184+
185+ if ( removeFromSuggestions . Contains ( title ) )
186+ continue ;
187+
188+ if ( suggestionItemByTitle . TryGetValue ( title , out var suggestionItem ) )
189+ {
190+ historyItem . Subtitle = suggestionItem . Subtitle ;
191+ removeFromSuggestions . Add ( title ) ;
192+ }
193+ }
194+
195+ List < IListItem > items = new ( primaryItems . Length + historyItems . Length + suggestionItems . Length ) ;
196+
197+ items . AddRange ( primaryItems ) ;
198+
199+ foreach ( var historyItem in historyItems )
200+ {
201+ if ( ! removeFromHistory . Contains ( historyItem . Title ) )
202+ items . Add ( historyItem ) ;
203+ }
204+
205+ foreach ( var suggestoinItem in suggestionItems )
206+ {
207+ if ( ! removeFromSuggestions . Contains ( suggestoinItem . Title ) )
208+ items . Add ( suggestoinItem ) ;
209+ }
210+
211+ return [ .. items . Take ( MaxDisplayCount ) ] ;
149212 }
150213
151214 private void RenderItems ( IListItem [ ] items , int currentUpdateSearchTextEpoch )
@@ -164,7 +227,7 @@ private void RenderItems(IListItem[] items, int currentUpdateSearchTextEpoch)
164227 RaiseItemsChanged ( items . Length ) ;
165228 }
166229
167- private void UpdateSuggestionItems ( IListItem [ ] suggestionItems , int currentUpdateSearchTextEpoch )
230+ private void UpdateSuggestionItems ( ListItem [ ] suggestionItems , int currentUpdateSearchTextEpoch )
168231 {
169232 if ( currentUpdateSearchTextEpoch != Volatile . Read ( ref _lastUpdateSearchTextEpoch ) )
170233 return ;
0 commit comments