1+ /* The MIT License
2+ *
3+ * Copyright (C) 2013 Mike Ward (http://mike-ward.net)
4+ *
5+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
6+ * this software and associated documentation files (the "Software"), to deal in
7+ * the Software without restriction, including without limitation the rights to
8+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9+ * of the Software, and to permit persons to whom the Software is furnished to do
10+ * so, subject to the following conditions:
11+ *
12+ * The above copyright notice and this permission notice shall be included in all
13+ * copies or substantial portions of the Software.
14+ *
15+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+ * SOFTWARE.
22+ */
23+
24+ using System . Windows ;
25+ using System . Windows . Controls . Primitives ;
26+ using System . Windows . Input ;
27+
28+ namespace GitDiffMargin . View
29+ {
30+ public class PopupAllowKeyboardInput
31+ {
32+ public static readonly DependencyProperty IsEnabledProperty =
33+ DependencyProperty . RegisterAttached (
34+ "IsEnabled" ,
35+ typeof ( bool ) ,
36+ typeof ( PopupAllowKeyboardInput ) ,
37+ new PropertyMetadata ( default ( bool ) , IsEnabledChanged ) ) ;
38+
39+ public static bool GetIsEnabled ( DependencyObject d )
40+ {
41+ return ( bool ) d . GetValue ( IsEnabledProperty ) ;
42+ }
43+
44+ public static void SetIsEnabled ( DependencyObject d , bool value )
45+ {
46+ d . SetValue ( IsEnabledProperty , value ) ;
47+ }
48+
49+ private static void IsEnabledChanged ( DependencyObject sender , DependencyPropertyChangedEventArgs ea )
50+ {
51+ EnableKeyboardInput ( ( Popup ) sender , ( bool ) ea . NewValue ) ;
52+ }
53+
54+ private static void EnableKeyboardInput ( Popup popup , bool enabled )
55+ {
56+ if ( ! enabled ) return ;
57+
58+ IInputElement element = null ;
59+ popup . Loaded += ( sender , args ) =>
60+ {
61+ popup . Child . Focusable = true ;
62+ popup . Child . IsVisibleChanged += ( o , ea ) =>
63+ {
64+ if ( popup . Child . IsVisible )
65+ {
66+ element = Keyboard . FocusedElement ;
67+ Keyboard . Focus ( popup . Child ) ;
68+ }
69+ } ;
70+ } ;
71+
72+ popup . Closed += ( sender , args ) => Keyboard . Focus ( element ) ;
73+ }
74+ }
75+ }
0 commit comments