private void _DatabaseSearcher_SearchComplete(object sender, SearchResult e) { lock ( m_resultLock ) { if (LatestSearchResult == null || e.ID != LatestSearchResult.ID) { return; } EnableShowMoreButton = false; int numResultsToDisplay = Math.Min(Settings.MaxResultsDisplayed, e.ValidResults.Count); //need to be on the UI thread since we will be creating controls Application.Current.Dispatcher.Invoke((Action) delegate { m_limitedResultCollection.Clear(); for (int i = 0; i < numResultsToDisplay; i++) { Controls.SearchResultControl newResult = new Controls.SearchResultControl(); newResult.SearchResultContent = e.ValidResults[i]; newResult.PreviewMouseLeftButtonUp += SearchResult_MouseLeftButtonUp; //if a file is modified we want to know about it so we can re-sort the list if necessary and write the changes to the DB e.ValidResults[i].OnChangedEvent -= _OnFileChanged; e.ValidResults[i].OnChangedEvent += _OnFileChanged; //load the file's icon e.ValidResults[i].LoadFileIcon(); m_limitedResultCollection.Add(newResult); } if (m_limitedResultCollection.Count < e.ValidResults.Count) { EnableShowMoreButton = true; } if (m_limitedResultCollection.Count > 0) { if (e.ValidResults.Count > m_limitedResultCollection.Count) { LatestSearchResultDetails = $"Showing top {String.Format("{0:n0}", Settings.MaxResultsDisplayed)} of {String.Format("{0:n0}", e.ValidResults.Count)} results"; } else { LatestSearchResultDetails = $"Showing {String.Format("{0:n0}", e.ValidResults.Count)} of {String.Format("{0:n0}", e.ValidResults.Count)} results"; } } else { LatestSearchResultDetails = (e.SearchTerm.Length == 0) ? "" : "Showing 0 of 0 results"; } }); } //remove any invalid results from the database so they don't appear in more searches List <List <DBPredicate> > removalPredicates = new List <List <DBPredicate> >(); for (int i = 0; i < e.InvalidResults.Count; i++) { List <DBPredicate> filePredicates = new List <DBPredicate>(); filePredicates.Add(new DBPredicate("path", DBOperator.EQUALS, e.InvalidResults[i].Path)); removalPredicates.Add(filePredicates); } try { m_database.DeleteTransacted("FileCache", removalPredicates); } catch (Exception crap) { Settings.SessionLog += crap.Message + "\n"; Debug.WriteLine(crap.Message); } }
/// <summary> /// adds more results to the displays results list when the user clicks the 'show more' button /// </summary> private void _ShowMoreResults(object obj) { lock ( m_resultLock ) { if (m_limitedResultCollection.Count == 0 || LatestSearchResult == null || m_limitedResultCollection.Count >= LatestSearchResult.ValidResults.Count || m_limitedResultCollection.Count >= 2000) { return; } //add up to 100 items to the list, but if there are 10 or less results remaining after that, add them as well int maxItemsAfterAdditions = m_limitedResultCollection.Count + Settings.MaxResultsDisplayed; if (maxItemsAfterAdditions + 10 >= LatestSearchResult.ValidResults.Count) { maxItemsAfterAdditions = LatestSearchResult.ValidResults.Count; } //cap loaded search results at 2000 for memory and performance purposes if (maxItemsAfterAdditions > 2000) { maxItemsAfterAdditions = 2000; } //need to be on the UI thread since we will be creating controls Application.Current.Dispatcher.Invoke((Action) delegate { //increase the weight of the existing elements by a tiny amount so that the sort order isn't disturbed //by new elements with the same weight (causing items to shift around for no apparent reason) foreach (var control in m_limitedResultCollection) { control.SearchResultContent.Weight += 0.0000002; } for (int i = m_limitedResultCollection.Count; i < LatestSearchResult.ValidResults.Count && i < maxItemsAfterAdditions; i++) { Controls.SearchResultControl newResult = new Controls.SearchResultControl(); newResult.SearchResultContent = LatestSearchResult.ValidResults[i]; newResult.PreviewMouseLeftButtonUp += SearchResult_MouseLeftButtonUp; //if a file is modified we want to know about it so we can re-sort the list if necessary and write the changes to the DB LatestSearchResult.ValidResults[i].OnChangedEvent -= _OnFileChanged; LatestSearchResult.ValidResults[i].OnChangedEvent += _OnFileChanged; //load the icon for the new file LatestSearchResult.ValidResults[i].LoadFileIcon(); m_limitedResultCollection.Add(newResult); } //since sorting is only performed on the elements loaded into the list, need to sort the new and old elements into place var sortedResults = m_limitedResultCollection.OrderByDescending(x => x.SearchResultContent.Weight).ToList(); for (int i = 0; i < sortedResults.Count; i++) { m_limitedResultCollection.Move(m_limitedResultCollection.IndexOf(sortedResults[i]), i); } if (LatestSearchResult.ValidResults.Count > m_limitedResultCollection.Count) { LatestSearchResultDetails = $"Showing top {String.Format("{0:n0}", m_limitedResultCollection.Count)} of {String.Format("{0:n0}", LatestSearchResult.ValidResults.Count)} results"; EnableShowMoreButton = true; } else { LatestSearchResultDetails = $"Showing {String.Format("{0:n0}", LatestSearchResult.ValidResults.Count)} of {String.Format("{0:n0}", LatestSearchResult.ValidResults.Count)} results"; EnableShowMoreButton = false; } }); } }