/// <summary> /// Searches the specified key word. /// </summary> /// <param name="keyWord">The key word.</param> /// <param name="token"></param> /// <param name="maxCount">The max count.</param> /// <param name="searchBoxInfo"></param> /// <returns></returns> public List <SearchResult> Search(string keyWord, SearchBoxInfo searchBoxInfo, CancellationToken token, int maxCount) { EverythingNativeApi.Everything_SetSearchW(keyWord); if (_maxCount != maxCount) { EverythingNativeApi.Everything_SetMax(maxCount); _maxCount = maxCount; } token.ThrowIfCancellationRequested(); if (!EverythingNativeApi.Everything_QueryW(true)) { GetLastError(); } token.ThrowIfCancellationRequested(); var buffer = new StringBuilder(BufferSize); var everythingGetNumResults = EverythingNativeApi.Everything_GetNumResults(); var resultList = new List <SearchResult>(everythingGetNumResults); for (int idx = 0; idx < everythingGetNumResults; ++idx) { EverythingNativeApi.Everything_GetResultFullPathNameW(idx, buffer, BufferSize); var fullPath = buffer.ToString(); var result = new SearchResult { FullPath = fullPath, ShowPath = _textWidthManager.GetSubStringForWidth(fullPath, searchBoxInfo), Type = EverythingNativeApi.Everything_IsFolderResult(idx) ? ResultType.Folder : ResultType.File }; result.ImageSource = IconManager.GetImageSource(result.FullPath, result.Type); resultList.Add(result); token.ThrowIfCancellationRequested(); } return(resultList.OrderBy(item => item.Type).ToList()); }
public string GetSubStringForWidth(string text, SearchBoxInfo searchBoxInfo) { if (searchBoxInfo.Width <= 0) { return(string.Empty); } if (searchBoxInfo.Width <= 55 || text.Length <= 6) { return(text); } double realWidth = searchBoxInfo.Width - 55; var typeface = new Typeface(searchBoxInfo.FontName); int foundCharIndex = BinarySearch( text.Length, realWidth, (idxValue1, value2) => { var formattedText = new FormattedText( //text.Substring(0, idxValue1 + 1), text.Substring(0, 3) + "..." + text.Substring(text.Length - idxValue1 + 6, idxValue1 - 6), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, searchBoxInfo.FontSize, Brushes.Black, null, TextFormattingMode.Ideal); return(formattedText.WidthIncludingTrailingWhitespace.CompareTo(value2)); }); int numChars = foundCharIndex < 0 ? ~foundCharIndex : foundCharIndex + 1; if (text.Length > numChars) { return(text.Substring(0, 3) + "..." + text.Substring(text.Length - numChars + 6, numChars - 6)); } return(text); }
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e) { if (!e.WidthChanged) { return; } if (ResultListBox.ItemsSource == null) { return; } var textWidthManager = new TextWidthManager(); foreach (SearchResult item in ResultListBox.ItemsSource) { var searchBoxInfo = new SearchBoxInfo { Width = e.NewSize.Width, FontSize = ResultListBox.FontSize, FontName = ResultListBox.FontFamily.ToString() }; item.ShowPath = textWidthManager.GetSubStringForWidth(item.FullPath, searchBoxInfo); } }