private string GetItemGroupName(SelectionWindowTreeViewItem item)
        {
            var tokens = item.TreeItemIdentifier.name.Split('.');

            if (tokens.Length <= 1)
            {
                return("");
            }

            return(tokens[0]);
        }
        protected override TreeViewItem BuildRoot()
        {
            var idForHiddenRoot    = -1;
            var depthForHiddenRoot = -1;
            var root = new TreeViewItem(idForHiddenRoot, depthForHiddenRoot, "root");

            var depth = 0;

            var top = new SelectionWindowTreeViewItem(-1, depth, m_AllIdentifier.name, m_AllIdentifier);

            root.AddChild(top);

            var expandList = new List <int> {
                -1
            };
            var lastName = "";
            var node     = root;

            for (var index = 0; index < m_Names.Length; ++index)
            {
                var nameWithIndex = m_Names[index];
                if (nameWithIndex == m_AllIdentifier.nameWithIndex)
                {
                    continue;
                }

                var identifier = new TreeItemIdentifier(nameWithIndex);
                var item       = new SelectionWindowTreeViewItem(index, depth, m_Names[index], identifier);

                if (identifier.name != lastName)
                {
                    // New items at root
                    node  = top;
                    depth = 0;
                }

                node.AddChild(item);

                if (identifier.name != lastName)
                {
                    // Extra instances hang of the parent
                    lastName = identifier.name;
                    node     = item;
                    depth    = 1;
                }
            }

            SetExpanded(expandList);

            SetupDepthsFromParentsAndChildren(root);

            return(root);
        }
        private void CellGUI(Rect cellRect, SelectionWindowTreeViewItem item, MyColumns column, ref RowGUIArgs args)
        {
            // Center cell rect vertically (makes it easier to place controls, icons etc in the cells)
            CenterRectUsingSingleLineHeight(ref cellRect);

            switch (column)
            {
            case MyColumns.ItemName:
            {
                args.rowRect = cellRect;
                // base.RowGUI(args);    // Required to show tree indenting

                // Draw manually to keep indenting by add a tooltip
                var rect = cellRect;
                if (Event.current.rawType == EventType.Repaint)
                {
                    int selectedChildren;
                    var childCount = GetChildCount(item.TreeItemIdentifier, out selectedChildren);

                    string text;
                    string tooltip;
                    var    fullName  = item.TreeItemIdentifier.name;
                    var    groupName = GetItemGroupName(item);

                    if (childCount <= 1)
                    {
                        text    = item.displayName;
                        tooltip = groupName == "" ? text : string.Format("{0}\n{1}", text, groupName);
                    }
                    else if (selectedChildren != childCount)
                    {
                        text    = string.Format("{0} ({1} of {2})", fullName, selectedChildren, childCount);
                        tooltip = groupName == "" ? text : string.Format("{0}\n{1}", text, groupName);
                    }
                    else
                    {
                        text    = string.Format("{0} (All)", fullName);
                        tooltip = groupName == "" ? text : string.Format("{0}\n{1}", text, groupName);
                    }

                    var content = new GUIContent(text, tooltip);

                    if (m_ActiveLineStyle == null)
                    {
                        m_ActiveLineStyle = new GUIStyle(DefaultStyles.label);
                        m_ActiveLineStyle.normal.textColor = DefaultStyles.boldLabel.onActive.textColor;
                    }

                    // The rect is assumed indented and sized after the content when pinging
                    var indent = GetContentIndent(item) + extraSpaceBeforeIconAndLabel;
                    rect.xMin += indent;

                    var iconRectWidth            = 16;
                    var kSpaceBetweenIconAndText = 2;

                    // Draw icon
                    var iconRect = rect;
                    iconRect.width = iconRectWidth;
                    // iconRect.x += 7f;

                    Texture icon = args.item.icon;
                    if (icon != null)
                    {
                        GUI.DrawTexture(iconRect, icon, ScaleMode.ScaleToFit);
                    }

                    rect.xMin += icon == null ? 0 : iconRectWidth + kSpaceBetweenIconAndText;

                    //bool mouseOver = rect.Contains(Event.current.mousePosition);
                    //DefaultStyles.label.Draw(rect, content, mouseOver, false, args.selected, args.focused);

                    // Must use this call to draw tooltip
                    EditorGUI.LabelField(rect, content, args.selected ? m_ActiveLineStyle : DefaultStyles.label);
                }
            }
            break;

            case MyColumns.GroupName:
            {
                var groupName = GetItemGroupName(item);
                var content   = new GUIContent(groupName, groupName);
                EditorGUI.LabelField(cellRect, content);
            }
            break;

            case MyColumns.State:
                var oldState = TreeItemSelected(item.TreeItemIdentifier);
                var newState = EditorGUI.Toggle(cellRect, oldState);
                if (newState != oldState)
                {
                    if (item.TreeItemIdentifier.nameWithIndex == m_AllIdentifier.nameWithIndex)
                    {
                        // Record active groups
                        m_Selection.groups.Clear();
                        if (newState)
                        {
                            if (!m_Selection.groups.Contains(item.TreeItemIdentifier.nameWithIndex))
                            {
                                m_Selection.groups.Add(item.TreeItemIdentifier.nameWithIndex);
                            }
                        }

                        // Update selection
                        m_Selection.selection.Clear();
                        if (newState)
                        {
                            foreach (var nameWithIndex in m_Names)
                            {
                                if (nameWithIndex != m_AllIdentifier.nameWithIndex)
                                {
                                    var identifier = new TreeItemIdentifier(nameWithIndex);
                                    if (identifier.index != TreeItemIdentifier.kAll)
                                    {
                                        m_Selection.selection.Add(nameWithIndex);
                                    }
                                }
                            }
                        }
                    }
                    else if (item.TreeItemIdentifier.index == TreeItemIdentifier.kAll)
                    {
                        // Record active groups
                        if (newState)
                        {
                            if (!m_Selection.groups.Contains(item.TreeItemIdentifier.nameWithIndex))
                            {
                                m_Selection.groups.Add(item.TreeItemIdentifier.nameWithIndex);
                            }
                        }
                        else
                        {
                            m_Selection.groups.Remove(item.TreeItemIdentifier.nameWithIndex);
                            // When turning off a sub group, turn of the 'all' group too
                            m_Selection.groups.Remove(m_AllIdentifier.nameWithIndex);
                        }

                        // Update selection
                        if (newState)
                        {
                            foreach (var nameWithIndex in m_Names)
                            {
                                var identifier = new TreeItemIdentifier(nameWithIndex);
                                if (identifier.name == item.TreeItemIdentifier.name &&
                                    identifier.index != TreeItemIdentifier.kAll)
                                {
                                    if (!m_Selection.selection.Contains(nameWithIndex))
                                    {
                                        m_Selection.selection.Add(nameWithIndex);
                                    }
                                }
                            }
                        }
                        else
                        {
                            var removeSelection = new List <string>();
                            foreach (var nameWithIndex in m_Selection.selection)
                            {
                                var identifier = new TreeItemIdentifier(nameWithIndex);
                                if (identifier.name == item.TreeItemIdentifier.name &&
                                    identifier.index != TreeItemIdentifier.kAll)
                                {
                                    removeSelection.Add(nameWithIndex);
                                }
                            }

                            foreach (var nameWithIndex in removeSelection)
                            {
                                m_Selection.selection.Remove(nameWithIndex);
                            }
                        }
                    }
                    else
                    {
                        if (newState)
                        {
                            m_Selection.selection.Add(item.TreeItemIdentifier.nameWithIndex);
                        }
                        else
                        {
                            m_Selection.selection.Remove(item.TreeItemIdentifier.nameWithIndex);

                            // Turn off any group its in too
                            var groupIdentifier = new TreeItemIdentifier(item.TreeItemIdentifier);
                            groupIdentifier.SetAll();
                            m_Selection.groups.Remove(groupIdentifier.nameWithIndex);

                            // Turn of the 'all' group too
                            m_Selection.groups.Remove(m_AllIdentifier.nameWithIndex);
                        }
                    }
                }

                break;
            }
        }