Snoop.SnoopUI.FindItem C# (CSharp) Method

FindItem() private method

Find the VisualTreeItem for the specified visual. If the item is not found and is not part of the Snoop UI, the tree will be adjusted to include the window the item is in.
private FindItem ( object target ) : Snoop.VisualTreeItem
target object
return Snoop.VisualTreeItem
        private VisualTreeItem FindItem(object target)
        {
            VisualTreeItem node = this.rootVisualTreeItem.FindNode(target);
            Visual rootVisual = this.rootVisualTreeItem.MainVisual;
            if (node == null)
            {
                Visual visual = target as Visual;
                if (visual != null && rootVisual != null)
                {
                    // If target is a part of the SnoopUI, let's get out of here.
                    if (visual.IsDescendantOf(this))
                    {
                        return null;
                    }

                    // If not in the root tree, make the root be the tree the visual is in.
                    if (!visual.IsDescendantOf(rootVisual))
                    {
                        var presentationSource = PresentationSource.FromVisual(visual);
                        if (presentationSource == null)
                        {
                            return null; // Something went wrong. At least we will not crash with null ref here.
                        }

                        this.Root = new VisualItem(presentationSource.RootVisual, null);
                    }
                }

                this.rootVisualTreeItem.Reload();

                node = this.rootVisualTreeItem.FindNode(target);

                this.SetFilter(this.filter);
            }
            return node;
        }