File size: 4,824 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace Microsoft.Management.UI.Internal
{
    /// <content>
    /// Partial class implementation for DismissiblePopup control.
    /// </content>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.MSInternal", "CA903:InternalNamespaceShouldNotContainPublicTypes")]
    public partial class DismissiblePopup : Popup
    {
        /// <summary>
        /// Constructs an instance of DismissablePopup.
        /// </summary>
        public DismissiblePopup() : base()
        {
            // nothing
        }

        private delegate void FocusChildDelegate();

        /// <summary>
        /// Responds to the condition in which the value of the IsOpen property changes from false to true.
        /// </summary>
        /// <param name="e">The event arguments.</param>
        protected override void OnOpened(EventArgs e)
        {
            base.OnOpened(e);

            if (this.FocusChildOnOpen)
            {
                this.Dispatcher.BeginInvoke(
                                            System.Windows.Threading.DispatcherPriority.Loaded,
                                            new FocusChildDelegate(this.FocusChild));
            }

            this.SetupAutomationIdBinding();
        }

        /// <summary>
        /// Responds when the value of the IsOpen property changes from to true to false.
        /// </summary>
        /// <param name="e">The event arguments.</param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            if (this.SetFocusOnClose)
            {
                // Find a control to set focus on.
                if (this.SetFocusOnCloseElement != null)
                {
                    // The focus target is set explicitly.
                    this.SetFocus(this.SetFocusOnCloseElement);
                }
                else if (this.PlacementTarget != null)
                {
                    // Use PlacementTarget as a first chance option.
                    this.SetFocus(this.PlacementTarget);
                }
                else
                {
                    // Use parent UIObject when neither FocusOnCloseTarget nor PlacementTarget is set.
                    UIElement parent = this.Parent as UIElement;
                    if (parent != null)
                    {
                        this.SetFocus(parent);
                    }
                }
            }
        }

        private void SetFocus(UIElement element)
        {
            if (element.Focusable)
            {
                element.Focus();
            }
            else
            {
                element.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
            }
        }

        private void SetupAutomationIdBinding()
        {
            var popupRoot = this.FindPopupRoot();

            var binding = new Binding();
            binding.Source = this;
            binding.Path = new PropertyPath(AutomationProperties.AutomationIdProperty);
            popupRoot.SetBinding(AutomationProperties.AutomationIdProperty, binding);
        }

        private FrameworkElement FindPopupRoot()
        {
            DependencyObject element = this.Child;

            while (element.GetType().Name.Equals("PopupRoot", StringComparison.Ordinal) == false)
            {
                element = VisualTreeHelper.GetParent(element);
            }

            Debug.Assert(element != null, "element not null");

            return (FrameworkElement)element;
        }

        /// <summary>
        /// Provides class handling for the KeyDown routed event that occurs when the user presses a key while this control has focus.
        /// </summary>
        /// <param name="e">The event data.</param>
        protected override void OnKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            ////
            // Close the popup if ESC is pressed
            ////
            if (e.Key == System.Windows.Input.Key.Escape && this.CloseOnEscape)
            {
                this.IsOpen = false;
            }
            else
            {
                base.OnKeyDown(e);
            }
        }

        partial void OnDismissPopupExecutedImplementation(ExecutedRoutedEventArgs e)
        {
            this.IsOpen = false;
        }

        private void FocusChild()
        {
            if (this.Child != null)
            {
                this.Child.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
            }
        }
    }
}