Skip to content

Commit dd587b6

Browse files
authored
Merge pull request #141 from laurentkempe/feature/close-esc-key
Add possibility to close popup with ESC key
2 parents 14bce91 + 04b2e5e commit dd587b6

4 files changed

Lines changed: 94 additions & 3 deletions

File tree

GitDiffMargin/GitDiffMargin.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@
226226
<Compile Include="View\EditorDiffMarginControl.xaml.cs">
227227
<DependentUpon>EditorDiffMarginControl.xaml</DependentUpon>
228228
</Compile>
229+
<Compile Include="View\PopupKeyboardBehavior.cs" />
229230
<Compile Include="View\ScrollDiffMarginControl.xaml.cs">
230231
<DependentUpon>ScrollDiffMarginControl.xaml</DependentUpon>
231232
</Compile>

GitDiffMargin/View/EditorDiffMarginControl.xaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
xmlns:viewModel="clr-namespace:GitDiffMargin.ViewModel"
7+
xmlns:view="clr-namespace:GitDiffMargin.View"
78
Cursor="Hand"
89
ClipToBounds="True"
910
d:DataContext="{d:DesignInstance viewModel:EditorDiffMarginViewModel}"
@@ -65,7 +66,14 @@
6566
IsOpen="{Binding ShowPopup, Mode=TwoWay}"
6667
Placement="MousePoint"
6768
PlacementTarget="{Binding ElementName=diffControl}"
68-
StaysOpen="False">
69+
StaysOpen="False"
70+
view:PopupAllowKeyboardInput.IsEnabled="True">
71+
<Popup.CommandBindings>
72+
<CommandBinding Command="Close" Executed="Popup_Close" />
73+
</Popup.CommandBindings>
74+
<Popup.InputBindings>
75+
<KeyBinding Key="Escape" Command="Close" />
76+
</Popup.InputBindings>
6977
<Border Background="Transparent">
7078
<StackPanel Orientation="Vertical">
7179
<Border HorizontalAlignment="Left"
@@ -92,8 +100,7 @@
92100
Background="{Binding Background, Mode=OneWay}"
93101
Foreground="{Binding Foreground, Mode=OneWay}"
94102
VerticalScrollBarVisibility="Auto"
95-
HorizontalScrollBarVisibility="Auto"
96-
/>
103+
HorizontalScrollBarVisibility="Auto" />
97104
</Border>
98105
</StackPanel>
99106
</Border>

GitDiffMargin/View/EditorDiffMarginControl.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Windows;
33
using System.Windows.Controls;
4+
using System.Windows.Controls.Primitives;
45
using System.Windows.Input;
56
using System.Windows.Threading;
67
using GitDiffMargin.ViewModel;
@@ -43,6 +44,13 @@ private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
4344
editorDiffViewModel?.ShowDifferenceCommand.Execute(null);
4445
}
4546

47+
private void Popup_Close(object sender, ExecutedRoutedEventArgs e)
48+
{
49+
if (!(sender is Popup popup)) return;
50+
51+
popup.IsOpen = false;
52+
}
53+
4654
private static void DoubleClick(object sender, EventArgs e)
4755
{
4856
ClickWaitTimer.Stop();
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)