This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 764
/
ShortcutManagementWindow.xaml.cs
219 lines (192 loc) · 8.61 KB
/
ShortcutManagementWindow.xaml.cs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ICSharpCode.Core;
using ICSharpCode.Core.Presentation;
using ICSharpCode.ShortcutsManagement.Data;
namespace ICSharpCode.ShortcutsManagement.Dialogs
{
/// <summary>
/// This window allows user to modify shortcuts registered in application
/// </summary>
public partial class ShortcutManagementWindow : Window
{
/// <summary>
/// Modified shortcut copy
/// </summary>
public Shortcut Shortcut
{
get; private set;
}
/// <summary>
/// Deep copy of addins list (including copies of categories and shortcuts)
/// </summary>
public ICollection<IShortcutTreeEntry> RootEntries
{
get; private set;
}
/// <summary>
/// List of modified shortcuts.
///
/// This list is used to optimize performance. Shortcuts not in this list are not saved.
/// Allways add modified shortcut to this list.
/// </summary>
public List<Shortcut> ModifiedShortcuts
{
get; private set;
}
/// <summary>
/// Initializes new <see cref="ShortcutManagementWindow" /> class
/// </summary>
/// <param name="shortcut">Shortcut</param>
/// <param name="rootEntries">List of all other add-ins containing shortcuts and categories. This list is used to find dupliate shortcuts</param>
public ShortcutManagementWindow(Shortcut shortcut, ICollection<IShortcutTreeEntry> rootEntries)
{
ModifiedShortcuts = new List<Shortcut>();
Shortcut = shortcut;
RootEntries = rootEntries;
shortcut.Gestures.CollectionChanged += Gestures_CollectionChanged;
ModifiedShortcuts.Add(shortcut);
DataContext = shortcut;
InitializeComponent();
// Display similar shortcuts (Shortcuts with the same input gestures assigned to them)
shortcutsManagementOptionsPanel.DataContext = rootEntries;
shortcutsManagementOptionsPanel.Loaded += delegate { FilterSimilarShortcuts(); };
}
/// <summary>
/// Filter shortcuts using same gestures as modified shortcut
/// </summary>
private void FilterSimilarShortcuts()
{
var templates = new InputGestureCollection(Shortcut.Gestures);
// Find shortcuts with same gesture and hide them.
// Also hide modified shortcut from this list
var finder = new ShortcutsFinder(RootEntries);
finder.FilterGesture(templates, GestureCompareMode.Conflicting);
finder.HideShortcut(Shortcut);
shortcutsManagementOptionsPanel.ExpandAll();
shortcutsManagementOptionsPanel.SelectFirstVisibleShortcut(false);
}
/// <summary>
/// Executed when adding or removing gestures used to call modified shortcut
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
void Gestures_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
FilterSimilarShortcuts();
}
/// <summary>
/// Executed when "Remove" button next to gesture is pressed
/// </summary>
/// <param name="sender">Object sender</param>
/// <param name="e">Event arguments</param>
private void removeGestureButton_Click(object sender, RoutedEventArgs e)
{
var tag = ((Button) sender).Tag as InputGesture;
Shortcut.Gestures.Remove(tag);
}
/// <summary>
/// Executed when "Add Gesture" button is clicked
/// </summary>
/// <param name="sender">Object sender</param>
/// <param name="e">Event arguments</param>
private void addGestureButton_Click(object sender, RoutedEventArgs e)
{
// Check if any chords are entered
if (gestureTextBox.Gesture == null) {
DisplayNotification(StringParser.Parse("${res:ShortcutsManagement.ModificationWindow.AdditionFailedNoChords}"), NotificationType.Failed);
return;
}
// Check whether first chord is unfinished
var partialKeyGesture = gestureTextBox.Gesture as PartialKeyGesture;
if (partialKeyGesture != null && !partialKeyGesture.IsFull) {
DisplayNotification(StringParser.Parse("${res:ShortcutsManagement.ModificationWindow.AdditionFailedFirstChordIsIncomplete}"), NotificationType.Failed);
return;
}
// Check whether last chord is finished
var multiKeyGesture = gestureTextBox.Gesture as MultiKeyGesture;
if (multiKeyGesture != null && multiKeyGesture.Chords.Last().Key == Key.None) {
DisplayNotification(StringParser.Parse("${res:ShortcutsManagement.ModificationWindow.AdditionFailedLastChordIsIncompete}"), NotificationType.Failed);
return;
}
// Check whether gesture exist in shortcut gestures collection
foreach (var existingGesture in Shortcut.Gestures) {
if (gestureTextBox.Gesture.IsTemplateFor(existingGesture, GestureCompareMode.ExactlyMatches)) {
DisplayNotification(StringParser.Parse("${res:ShortcutsManagement.ModificationWindow.AddingExistingGesture}"), NotificationType.Failed);
return;
}
}
// Add gesture
if (partialKeyGesture != null) {
try {
var keyGesture = new KeyGesture(partialKeyGesture.Key, partialKeyGesture.Modifiers);
Shortcut.Gestures.Add(keyGesture);
} catch (NotSupportedException) {
Shortcut.Gestures.Add(partialKeyGesture);
}
} else {
Shortcut.Gestures.Add(gestureTextBox.Gesture);
}
DisplayNotification(StringParser.Parse("${res:ShortcutsManagement.ModificationWindow.AdditionIsSuccessfull}"), NotificationType.Added);
}
/// <summary>
/// Executed when "Remove" button next to similar shortcut is pressed
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event argument</param>
private void shortcutsManagementOptionsPanel_RemoveShortcutClick(object sender, RoutedRemoveShortcutEventArgs e)
{
e.Handled = true;
// Remove gestures registered in modified shortcut from deleted shortcut
var removedShortcutGestures = e.RemovedShortcut.Gestures;
for (int i = removedShortcutGestures.Count - 1; i >= 0; i--) {
foreach (var modifiedInputGesture in Shortcut.Gestures) {
if (removedShortcutGestures[i].IsTemplateFor(modifiedInputGesture, GestureCompareMode.StartsWith)) {
removedShortcutGestures.RemoveAt(i);
FilterSimilarShortcuts();
if (!ModifiedShortcuts.Contains(e.RemovedShortcut))
{
ModifiedShortcuts.Add(e.RemovedShortcut);
}
break;
}
}
}
}
/// <summary>
/// Save changes to shortcuts
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
private void saveButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
/// <summary>
/// Display message describing shortcut addition result
/// </summary>
/// <param name="notificationText">Displayed message text</param>
/// <param name="type">Message type</param>
public void DisplayNotification(string notificationText, NotificationType type)
{
gestureTextBox.NotificationText = notificationText;
gestureTextBox.NotificationType = type;
}
/// <summary>
/// Execute this method when Reset button is clicked.
///
/// Modifications are not saved
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
private void resetButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}