public void SelectElement(VisualElement element, string query, SearchHighlight searchHighlight) { ClearSearchResults(); if (!FindElement(m_TreeRootItems, element, out var item)) { return; } m_TreeView.SetSelectionById(item.id); m_TreeView.ScrollToItemById(item.id); if (string.IsNullOrEmpty(query)) { return; } schedule.Execute(() => { var selected = m_TreeView.Q(className: BaseVerticalCollectionView.itemSelectedVariantUssClassName); if (selected == null || searchHighlight == SearchHighlight.None) { return; } var content = selected.Q(itemContentName); var labelContainers = content.Query(classes: labelContainerUssClassName).ToList(); foreach (var labelContainer in labelContainers) { var label = labelContainer.Q <Label>(); if (label.ClassListContains(itemTypeUssClassName) && searchHighlight != SearchHighlight.Type) { continue; } if (label.ClassListContains(itemNameUssClassName) && searchHighlight != SearchHighlight.Name) { continue; } if (label.ClassListContains(itemClassListUssClassName) && searchHighlight != SearchHighlight.Class) { continue; } var text = label.text; var indexOf = text.IndexOf(query, StringComparison.OrdinalIgnoreCase); if (indexOf < 0) { continue; } var highlight = new VisualElement(); m_SearchResultsHightlights.Add(highlight); highlight.AddToClassList(debuggerHighlightUssClassName); var letterSize = 8.4f; highlight.style.width = query.Length * letterSize; highlight.style.left = indexOf * letterSize; labelContainer.Insert(0, highlight); break; } }); }
public void SelectElement(VisualElement element, string query, SearchHighlight searchHighlight) { ClearSearchResults(); var item = FindElement(m_TreeRootItems, element); if (item == null) { return; } #if UNITY_2020_1_OR_NEWER m_TreeView.SetSelection(item.id); m_TreeView.ScrollToItem(item.id); #else m_TreeView.SelectItem(item.id); #endif if (string.IsNullOrEmpty(query)) { return; } var selected = m_TreeView.Query(classes: "unity-list-view__item--selected").First(); if (selected == null || searchHighlight == SearchHighlight.None) { return; } var content = selected.Q("unity-treeview-item-content"); var labelContainers = content.Query(classes: "unity-builder-explorer-tree-item-label-cont").ToList(); foreach (var labelContainer in labelContainers) { var label = labelContainer.Q <Label>(); if (label.ClassListContains("unity-debugger-tree-item-type") && searchHighlight != SearchHighlight.Type) { continue; } if (label.ClassListContains("unity-debugger-tree-item-name") && searchHighlight != SearchHighlight.Name) { continue; } if (label.ClassListContains("unity-debugger-tree-item-classlist") && searchHighlight != SearchHighlight.Class) { continue; } var text = label.text; var indexOf = text.IndexOf(query, StringComparison.OrdinalIgnoreCase); if (indexOf < 0) { continue; } var highlight = new VisualElement(); m_SearchResultsHightlights.Add(highlight); highlight.AddToClassList("unity-debugger-highlight"); int letterSize = 8; highlight.style.width = query.Length * letterSize; highlight.style.left = indexOf * letterSize; labelContainer.Insert(0, highlight); break; } }
public static List <T> ApplySearchFilter <T>(Dictionary <string, string> query, Func <T, string> getName, Action <T, string> setName, List <T> items) { if (query.ContainsKey("s") || query.ContainsKey("sh")) { var sSearches = query.ContainsKey("s") ? query["s"].ToLower().Split('+') : new string[0]; for (int i = 0; i < sSearches.Length; i++) { if (SearchCorrections.ContainsKey(sSearches[i])) { sSearches[i] = SearchCorrections[sSearches[i]]; } } var shSearches = query.ContainsKey("sh") ? query["sh"].ToLower().Split('+') : new string[0]; for (int i = 0; i < shSearches.Length; i++) { if (SearchCorrections.ContainsKey(shSearches[i])) { shSearches[i] = SearchCorrections[shSearches[i]]; } } SortedList <double, T> newItems = new SortedList <double, T>(); Dictionary <double, int> numRanks = new Dictionary <double, int>(); foreach (T item in items) { string name = getName(item).ToLower(); SearchHighlight[] highlighted = new SearchHighlight[name.Length]; double rank = 0; foreach (string search in shSearches) { int start = 0; int i; bool found = false; while ((i = name.IndexOf(search, start)) >= 0) { found = true; start = i + 1; for (int j = 0; j < search.Length; j++) { highlighted[i + j] = SearchHighlight.SH; } } if (found) { rank += 1000.0; } } foreach (string search in sSearches) { int start = 0; int i; bool found = false; while ((i = name.IndexOf(search, start)) >= 0) { found = true; start = i + 1; for (int j = 0; j < search.Length; j++) { if (highlighted[i + j] == SearchHighlight.NA) { highlighted[i + j] = SearchHighlight.S; } } } if (found) { rank += 1000.0; } } rank += (highlighted.Count(x => x != SearchHighlight.NA) * 1000.0 / highlighted.Count()) - highlighted.Count(); if (rank > 0) { if (query.ContainsKey("sh")) { name = getName(item); bool highlighting = false; for (int i = highlighted.Length - 1; i >= 0; i--) { if (highlighting) { if (highlighted[i] != SearchHighlight.SH) { name = name.Insert(i + 1, StartHighlight); highlighting = false; } } else { if (highlighted[i] == SearchHighlight.SH) { name = name.Insert(i + 1, EndHighlight); highlighting = true; } } } if (highlighting) { name = name.Insert(0, StartHighlight); } setName(item, name); } if (numRanks.ContainsKey(rank)) { newItems.Add(rank - numRanks[rank] / 10000.0, item); numRanks[rank]++; } else { newItems.Add(rank, item); numRanks[rank] = 1; } } } return(newItems.Values.Reverse().ToList()); } else { return(items); } }