Snoop.SnoopUI.FindRoot C# (CSharp) Method

FindRoot() private method

private FindRoot ( ) : object
return object
        private object FindRoot()
        {
            object root = null;

            if (SnoopModes.MultipleDispatcherMode)
            {
                foreach (PresentationSource presentationSource in PresentationSource.CurrentSources)
                {
                    if
                    (
                        presentationSource.RootVisual != null &&
                        presentationSource.RootVisual is UIElement &&
                        ((UIElement)presentationSource.RootVisual).Dispatcher.CheckAccess()
                    )
                    {
                        root = presentationSource.RootVisual;
                        break;
                    }
                }
            }
            else if (Application.Current != null)
            {
                root = Application.Current;
            }
            else
            {
                // if we don't have a current application,
                // then we must be in an interop scenario (win32 -> wpf or windows forms -> wpf).

                // in this case, let's iterate over PresentationSource.CurrentSources,
                // and use the first non-null, visible RootVisual we find as root to inspect.
                foreach (PresentationSource presentationSource in PresentationSource.CurrentSources)
                {
                    if
                    (
                        presentationSource.RootVisual != null &&
                        presentationSource.RootVisual is UIElement &&
                        ((UIElement)presentationSource.RootVisual).Visibility == Visibility.Visible
                    )
                    {
                        root = presentationSource.RootVisual;
                        break;
                    }
                }

                if (System.Windows.Forms.Application.OpenForms.Count > 0)
                {
                    // this is windows forms -> wpf interop

                    // call ElementHost.EnableModelessKeyboardInterop to allow the Snoop UI window
                    // to receive keyboard messages. if you don't call this method,
                    // you will be unable to edit properties in the property grid for windows forms interop.
                    ElementHost.EnableModelessKeyboardInterop(this);
                }
            }

            return root;
        }