/// <summary> /// Draws the search toolbar. /// </summary> public void DrawSearchToolbar(GUIStyle toolbarStyle = null) { var config = this.Config; var searchFieldRect = GUILayoutUtility.GetRect(0, config.SearchToolbarHeight, GUILayoutOptions.ExpandWidth(true)); if (Event.current.type == EventType.Repaint) { (toolbarStyle ?? SirenixGUIStyles.ToolbarBackground).Draw(searchFieldRect, GUIContent.none, 0); } searchFieldRect = searchFieldRect.HorizontalPadding(5).AlignMiddle(16); searchFieldRect.xMin += 3; searchFieldRect.y += 1; EditorGUI.BeginChangeCheck(); config.SearchTerm = this.DrawSearchField(searchFieldRect, config.SearchTerm, config.AutoFocusSearchBar); var changed = EditorGUI.EndChangeCheck(); if ((changed || this.updateSearchResults) && this.hasRepaintedCurrentSearchResult) { this.updateSearchResults = false; // We want fast visual search feedback. If the user is typing faster than the window can repaint, // then no results will be visible while he's typing. this.hasRepaintedCurrentSearchResult fixes that. this.hasRepaintedCurrentSearchResult = false; bool doSearch = !string.IsNullOrEmpty(config.SearchTerm); if (doSearch) { if (!this.DrawInSearchMode) { config.ScrollPos = new Vector2(); } this.DrawInSearchMode = true; if (config.SearchFunction != null) { // Custom search this.FlatMenuTree.Clear(); foreach (var item in this.EnumerateTree()) { if (config.SearchFunction(item)) { this.FlatMenuTree.Add(item); } } } else { // Fuzzy search with sorting. this.FlatMenuTree.Clear(); this.FlatMenuTree.AddRange( this.EnumerateTree() .Where(x => x.Value != null) .Select(x => { int score; bool include = FuzzySearch.Contains(this.Config.SearchTerm, x.SearchString, out score); return(new { score = score, item = x, include = include }); }) .Where(x => x.include) .OrderByDescending(x => x.score) .Select(x => x.item)); } this.root.UpdateFlatMenuItemNavigation(); } else { this.DrawInSearchMode = false; // Ensure all selected elements are visible, and scroll to the last one. this.FlatMenuTree.Clear(); var last = this.selection.LastOrDefault(); this.UpdateMenuTree(); this.Selection.SelectMany(x => x.GetParentMenuItemsRecursive(false)).ForEach(x => x.Toggled = true); if (last != null) { this.ScrollToMenuItem(last); } this.root.UpdateFlatMenuItemNavigation(); } } if (Event.current.type == EventType.Repaint) { this.hasRepaintedCurrentSearchResult = true; } }