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 ( ) ;
@@ -112,12 +113,12 @@ public override async void UpdateSearchText(string oldSearch, string newSearch)
112113 var primaryItems = BuildPrimaryItems ( newSearch ) ;
113114 var snapshotSuggestions = Volatile . Read ( ref _suggestionItems ) ;
114115
115- RenderItems ( [ .. primaryItems , .. historyItems , .. snapshotSuggestions ] , currentEpoch ) ;
116+ RenderItems ( ItemIntegrate ( primaryItems , historyItems , snapshotSuggestions ) , currentEpoch ) ;
116117
117118 if ( ! shouldFetchSuggestions )
118119 return ;
119120
120- IListItem [ ] suggestionItems ;
121+ ListItem [ ] suggestionItems ;
121122 try
122123 {
123124 suggestionItems = await FetchSuggestionItemsAsync ( newSearch , currentCancellationSource ! . Token ) . ConfigureAwait ( false ) ;
@@ -144,7 +145,69 @@ public override async void UpdateSearchText(string oldSearch, string newSearch)
144145
145146 UpdateSuggestionItems ( suggestionItems , currentEpoch ) ;
146147
147- RenderItems ( [ .. primaryItems , .. historyItems , .. suggestionItems ] , currentEpoch ) ;
148+ RenderItems ( ItemIntegrate ( primaryItems , historyItems , suggestionItems ) , currentEpoch ) ;
149+ }
150+
151+ private static IListItem [ ] ItemIntegrate ( ListItem [ ] primaryItems , ListItem [ ] historyItems , ListItem [ ] suggestionItems )
152+ {
153+ var primaryItemByTitle = primaryItems . ToDictionary ( item => item . Title , item => item ) ;
154+ var historyItemByTitle = historyItems . ToDictionary ( item => item . Title , item => item ) ;
155+ var suggestionItemByTitle = suggestionItems . ToDictionary ( item => item . Title , item => item ) ;
156+
157+ HashSet < string > removeFromHistory = [ ] ;
158+ HashSet < string > removeFromSuggestions = [ ] ;
159+
160+ foreach ( var ( title , primaryItem ) in primaryItemByTitle )
161+ {
162+ if ( historyItemByTitle . TryGetValue ( title , out var historyItem ) )
163+ {
164+ primaryItem . Icon = Icons . History ;
165+ primaryItem . MoreCommands = historyItem . MoreCommands ;
166+
167+ if ( suggestionItemByTitle . TryGetValue ( title , out var suggestionItem ) )
168+ {
169+ historyItem . Subtitle = suggestionItem . Subtitle ;
170+ removeFromSuggestions . Add ( title ) ;
171+ }
172+ else
173+ {
174+ removeFromHistory . Add ( title ) ;
175+ }
176+ }
177+ }
178+
179+ foreach ( var ( title , historyItem ) in historyItemByTitle )
180+ {
181+ if ( removeFromHistory . Contains ( title ) )
182+ continue ;
183+
184+ if ( removeFromSuggestions . Contains ( title ) )
185+ continue ;
186+
187+ if ( suggestionItemByTitle . TryGetValue ( title , out var suggestionItem ) )
188+ {
189+ historyItem . Subtitle = suggestionItem . Subtitle ;
190+ removeFromSuggestions . Add ( title ) ;
191+ }
192+ }
193+
194+ List < IListItem > items = new ( primaryItems . Length + historyItems . Length + suggestionItems . Length ) ;
195+
196+ items . AddRange ( primaryItems ) ;
197+
198+ foreach ( var historyItem in historyItems )
199+ {
200+ if ( ! removeFromHistory . Contains ( historyItem . Title ) )
201+ items . Add ( historyItem ) ;
202+ }
203+
204+ foreach ( var suggestoinItem in suggestionItems )
205+ {
206+ if ( ! removeFromSuggestions . Contains ( suggestoinItem . Title ) )
207+ items . Add ( suggestoinItem ) ;
208+ }
209+
210+ return [ .. items . Take ( MaxDisplayCount ) ] ;
148211 }
149212
150213 private void RenderItems ( IListItem [ ] items , int currentUpdateSearchTextEpoch )
@@ -163,7 +226,7 @@ private void RenderItems(IListItem[] items, int currentUpdateSearchTextEpoch)
163226 RaiseItemsChanged ( items . Length ) ;
164227 }
165228
166- private void UpdateSuggestionItems ( IListItem [ ] suggestionItems , int currentUpdateSearchTextEpoch )
229+ private void UpdateSuggestionItems ( ListItem [ ] suggestionItems , int currentUpdateSearchTextEpoch )
167230 {
168231 if ( currentUpdateSearchTextEpoch != Volatile . Read ( ref _lastUpdateSearchTextEpoch ) )
169232 return ;
0 commit comments