protected void AddItemToIndex(SearcherItem item, ref int lastId, Action <SearcherItem> action)
        {
            m_ItemList.Insert(lastId, item);

            // We can only set the id here as we only know the final index of the item here.
            item.OverwriteId(lastId);
            item.GeneratePath();

            action?.Invoke(item);

            lastId++;

            // This is used for sorting results between databases.
            item.OverwriteDatabase(this);

            if (!item.HasChildren)
            {
                return;
            }

            var childrenIds = new List <int>();

            foreach (SearcherItem child in item.Children)
            {
                AddItemToIndex(child, ref lastId, action);
                childrenIds.Add(child.Id);
            }

            item.OverwriteChildrenIds(childrenIds);
        }
Example #2
0
 void SelectionCallback(SearcherItem item)
 {
     if (s_ItemSelectedDelegate == null || s_ItemSelectedDelegate(item))
     {
         Close();
     }
 }
Example #3
0
        void Expand(SearcherItem item)
        {
            m_ExpandedResults.Add(item);

            RegenerateVisibleResults();
            HideUnexpandedItems();

            m_ListView.Refresh();
        }
Example #4
0
 void OnListViewSelect(SearcherItem item)
 {
     if (!m_Searcher.Adapter.MultiSelectEnabled)
     {
         m_SelectionCallback(item);
     }
     else
     {
         ToggleItemForMultiSelect(item, !m_MultiSelectSelection.Contains(item));
     }
 }
Example #5
0
        public virtual VisualElement Bind(VisualElement element, SearcherItem item, ItemExpanderState expanderState, string query)
        {
            var indent = element.Q <VisualElement>("itemIndent");

            indent.style.width = item.Depth * k_IndentDepthFactor;

            var expander = element.Q <VisualElement>("itemChildExpander");

            var icon = expander.Query("expanderIcon").First();

            icon.ClearClassList();

            switch (expanderState)
            {
            case ItemExpanderState.Expanded:
                icon.AddToClassList("Expanded");
                break;

            case ItemExpanderState.Collapsed:
                icon.AddToClassList("Collapsed");
                break;
            }

            var nameLabelsContainer = element.Q <VisualElement>("labelsContainer");

            nameLabelsContainer.Clear();

            var iconElement = element.Q <VisualElement>("itemIconVisualElement");

            iconElement.style.backgroundImage = item.Icon;
            if (item.Icon == null && item.CollapseEmptyIcon)
            {
                iconElement.style.display = DisplayStyle.None;
            }
            else
            {
                iconElement.style.display = DisplayStyle.Flex;
            }

            if (string.IsNullOrWhiteSpace(query))
            {
                nameLabelsContainer.Add(new Label(item.Name));
            }
            else
            {
                SearcherHighlighter.HighlightTextBasedOnQuery(nameLabelsContainer, item.Name, query);
            }

            element.userData = item;
            element.name     = k_EntryName;

            return(expander);
        }
Example #6
0
        static void GetItemsToHide(SearcherItem parent, ref HashSet <SearcherItem> itemsToHide)
        {
            if (!parent.HasChildren)
            {
                itemsToHide.Add(parent);
                return;
            }

            foreach (var child in parent.Children)
            {
                itemsToHide.Add(child);
                GetItemsToHide(child, ref itemsToHide);
            }
        }
Example #7
0
        bool HasChildResult(SearcherItem item)
        {
            if (m_Results.Contains(item))
            {
                return(true);
            }

            foreach (var child in item.Children)
            {
                if (HasChildResult(child))
                {
                    return(true);
                }
            }

            return(false);
        }
Example #8
0
        VisualElement MakeItem()
        {
            VisualElement item = m_Searcher.Adapter.MakeItem();

            if (m_Searcher.Adapter.MultiSelectEnabled)
            {
                var selectionToggle = item.Q <Toggle>("itemToggle");
                if (selectionToggle != null)
                {
                    selectionToggle.RegisterValueChangedCallback(changeEvent =>
                    {
                        SearcherItem searcherItem = item.userData as SearcherItem;
                        ToggleItemForMultiSelect(searcherItem, changeEvent.newValue);
                    });
                }
            }
            return(item);
        }
Example #9
0
        void AddResultChildren(SearcherItem item, ISet <SearcherItem> idSet)
        {
            if (!item.HasChildren)
            {
                return;
            }

            foreach (var child in item.Children)
            {
                if (!idSet.Contains(child))
                {
                    idSet.Add(child);
                    m_VisibleResults.Add(child);
                }

                AddResultChildren(child, idSet);
            }
        }
Example #10
0
        public static List <SearcherItem> CreateFromFlatList(List <SearcherItem> items)
        {
            List <SearcherItem> searchList = new List <SearcherItem>();

            for (int i = 0; i < items.Count; ++i)
            {
                SearcherItem item       = items[i];
                string[]     pathParts  = item.Name.Split('/');
                SearcherItem searchNode = FindNodeByName(searchList, pathParts[0]);
                if (searchNode == null)
                {
                    searchNode = new SearcherItem(pathParts[0]);
                    searchList.Add(searchNode);
                }
                AddItem(searchNode, item, pathParts);
            }
            return(searchList);
        }
Example #11
0
        void AddResultChildren(SearcherItem item, ISet <SearcherItem> idSet)
        {
            if (!item.HasChildren)
            {
                return;
            }
            if (m_Searcher.Adapter.AddAllChildResults)
            {
                //add all children results for current search term
                // eg "Book" will show both "Cook Book" and "Cooking" as children
                foreach (var child in item.Children)
                {
                    if (!idSet.Contains(child))
                    {
                        idSet.Add(child);
                        m_VisibleResults.Add(child);
                    }

                    AddResultChildren(child, idSet);
                }
            }
            else
            {
                foreach (var child in item.Children)
                {
                    //only add child results if the child matches the search term
                    // eg "Book" will show "Cook Book" but not "Cooking" as a child
                    if (!m_Results.Contains(child))
                    {
                        continue;
                    }

                    if (!idSet.Contains(child))
                    {
                        idSet.Add(child);
                        m_VisibleResults.Add(child);
                    }

                    AddResultChildren(child, idSet);
                }
            }
        }
Example #12
0
        public void AddChild(SearcherItem child)
        {
            if (child == null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            if (Database != null)
            {
                throw new InvalidOperationException(
                          "Cannot add more children to an item that was already used in a database.");
            }

            if (Children == null)
            {
                Children = new List <SearcherItem>();
            }

            Children.Add(child);
            child.OverwriteParent(this);
        }
Example #13
0
        private static void AddItem(SearcherItem root, SearcherItem item, string[] pathParts)
        {
            string       itemFullPath      = item.Name;
            string       itemName          = pathParts[pathParts.Length - 1];
            string       currentPath       = string.Empty;
            SearcherItem currentSearchNode = root;

            for (int i = 1; i < pathParts.Length; ++i)
            {
                SearcherItem node = FindNodeByName(currentSearchNode.Children, pathParts[i]);
                if (node == null)
                {
                    node = new SearcherItem(pathParts[i]);
                    currentSearchNode.AddChild(node);
                }
                currentSearchNode = node;
            }
            // Set the user data to the final node, which is guaranteed to correspond to the item.
            currentSearchNode.UserData = item.UserData;
            currentSearchNode.Icon     = item.Icon;
        }
Example #14
0
        void ToggleItemForMultiSelect(SearcherItem item, bool selected)
        {
            if (selected)
            {
                m_MultiSelectSelection.Add(item);
            }
            else
            {
                m_MultiSelectSelection.Remove(item);
            }

            Toggle toggle;

            if (m_SearchItemToVisualToggle.TryGetValue(item, out toggle))
            {
                toggle.SetValueWithoutNotify(selected);
            }

            foreach (var child in item.Children)
            {
                ToggleItemForMultiSelect(child, selected);
            }
        }
Example #15
0
        void Collapse(SearcherItem item)
        {
            // if it's already collapsed or not collapsed
            if (!m_ExpandedResults.Remove(item))
            {
                // this case applies for a left arrow key press
                if (item.Parent != null)
                {
                    SetSelectedElementInResultsList(m_VisibleResults.IndexOf(item.Parent));
                }

                // even if it's a root item and has no parents, do nothing more
                return;
            }

            RegenerateVisibleResults();
            HideUnexpandedItems();

            // TODO: understand what happened
            m_ListView.Refresh();

            // RefreshListViewOn();
        }
        public override void OnInspectorGUI()
        {
            var root     = new SearcherItem("Root");
            var children = new List <SearcherItem>();

            for (var i = 0; i < 10; ++i)
            {
                children.Add(new SearcherItem("B-" + i));
            }

            var child = new SearcherItem("Child", "", children);

            root.AddChild(child);

            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.PrefixLabel("Mouse Position");
                if (EditorGUILayout.DropdownButton(new GUIContent("Button"), FocusType.Passive, GUI.skin.button))
                {
                    var editorWindow       = EditorWindow.focusedWindow;
                    var localMousePosition = Event.current.mousePosition;
                    var worldMousePosition = editorWindow.rootVisualElement.LocalToWorld(localMousePosition);

                    SearcherWindow.Show(
                        editorWindow,
                        new List <SearcherItem> {
                        root
                    },
                        "Mouse Position",
                        item => {
                        Debug.Log("Searcher item selected: " + (item?.Name ?? "<none>"));
                        return(true);
                    },
                        worldMousePosition);
                }
                EditorGUILayout.EndHorizontal();
            }

            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.PrefixLabel("Button Center");

                var selected = EditorGUILayout.DropdownButton(new GUIContent("Button"), FocusType.Passive, GUI.skin.button);
                if (Event.current.type == EventType.Repaint)
                {
                    m_ButtonRect = GUILayoutUtility.GetLastRect();
                }

                if (selected)
                {
                    var editorWindow      = EditorWindow.focusedWindow;
                    var localButtonCenter = m_ButtonRect.center;
                    var worldButtonCenter = editorWindow.rootVisualElement.LocalToWorld(localButtonCenter);

                    SearcherWindow.Show(
                        editorWindow,
                        new List <SearcherItem> {
                        root
                    },
                        "Button Center",
                        item => {
                        Debug.Log("Searcher item selected: " + (item?.Name ?? "<none>"));
                        return(true);
                    },
                        worldButtonCenter);
                }

                EditorGUILayout.EndHorizontal();
            }
        }
Example #17
0
        protected virtual bool Match(string query, IReadOnlyList <string> tokenizedQuery, SearcherItem item, out float score)
        {
            var filter = MatchFilter?.Invoke(query, item) ?? true;

            return(Match(tokenizedQuery, item.Path, out score) && filter);
        }
Example #18
0
        bool Match(string query, SearcherItem item, out float score)
        {
            var filter = MatchFilter?.Invoke(query, item) ?? true;

            return(Match(query, item.Path, out score) && filter);
        }
Example #19
0
 void OverwriteParent(SearcherItem newParent)
 {
     Parent = newParent;
 }
 private bool Match(string query, SearcherItem item)
 {
     return(MatchFilter?.Invoke(query, item) ?? true);
 }