-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathKeyboardShortcut.cs
More file actions
247 lines (240 loc) · 7.33 KB
/
KeyboardShortcut.cs
File metadata and controls
247 lines (240 loc) · 7.33 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using BytecodeApi.Data;
using BytecodeApi.Extensions;
using System.Windows.Input;
namespace BytecodeApi.Wpf.Data;
/// <summary>
/// Represents a keyboard shortcut composed of a key and a set of modifiers (Ctrl, Shift, Alt, Win).
/// </summary>
public sealed class KeyboardShortcut : ObservableObject, IEquatable<KeyboardShortcut>
{
/// <summary>
/// Gets or sets the modifiers for this <see cref="KeyboardShortcut" />.
/// </summary>
public ModifierKeys Modifiers
{
get;
set
{
Set(ref field, value);
RaisePropertyChanged(nameof(IsCtrl));
RaisePropertyChanged(nameof(IsShift));
RaisePropertyChanged(nameof(IsAlt));
RaisePropertyChanged(nameof(IsWin));
RaisePropertyChanged(nameof(DisplayName));
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="KeyboardShortcut" /> uses the Control modifier.
/// </summary>
public bool IsCtrl
{
get => Modifiers.HasFlag(ModifierKeys.Control);
set
{
if (value)
{
Modifiers |= ModifierKeys.Control;
}
else
{
Modifiers &= ~ModifierKeys.Control;
}
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="KeyboardShortcut" /> uses the Shift modifier.
/// </summary>
public bool IsShift
{
get => Modifiers.HasFlag(ModifierKeys.Shift);
set
{
if (value)
{
Modifiers |= ModifierKeys.Shift;
}
else
{
Modifiers &= ~ModifierKeys.Shift;
}
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="KeyboardShortcut" /> uses the Alt modifier.
/// </summary>
public bool IsAlt
{
get => Modifiers.HasFlag(ModifierKeys.Alt);
set
{
if (value)
{
Modifiers |= ModifierKeys.Alt;
}
else
{
Modifiers &= ~ModifierKeys.Alt;
}
}
}
/// <summary>
/// Gets or sets a value indicating whether this <see cref="KeyboardShortcut" /> uses the Windows modifier.
/// </summary>
public bool IsWin
{
get => Modifiers.HasFlag(ModifierKeys.Windows);
set
{
if (value)
{
Modifiers |= ModifierKeys.Windows;
}
else
{
Modifiers &= ~ModifierKeys.Windows;
}
}
}
/// <summary>
/// Gets or sets the key for this <see cref="KeyboardShortcut" />.
/// </summary>
public Key Key
{
get;
set
{
Set(ref field, value);
RaisePropertyChanged(nameof(KeyName));
RaisePropertyChanged(nameof(DisplayName));
}
}
/// <summary>
/// Gets the equivalent display name for the <see cref="Key" /> property.
/// </summary>
public string KeyName => new KeyConverter().ConvertTo(Key, typeof(string)) as string ?? "";
/// <summary>
/// Gets the display name for this instance.
/// <para>Example: Ctrl+Shift+A</para>
/// </summary>
public string DisplayName
{
get
{
List<string> modifiers = [];
if (IsCtrl) modifiers.Add("Ctrl");
if (IsShift) modifiers.Add("Shift");
if (IsAlt) modifiers.Add("Alt");
if (IsWin) modifiers.Add("Win");
modifiers.Add(KeyName);
return modifiers.AsString("+");
}
}
/// <summary>
/// Initializes a new instance of the <see cref="KeyboardShortcut" /> class.
/// </summary>
public KeyboardShortcut()
{
Modifiers = ModifierKeys.None;
Key = Key.None;
}
/// <summary>
/// Initializes a new instance of the <see cref="KeyboardShortcut" /> class with the specified modifiers and the specified key.
/// </summary>
/// <param name="modifiers">The modifiers for this <see cref="KeyboardShortcut" />.</param>
/// <param name="key">The key for this <see cref="KeyboardShortcut" />.</param>
public KeyboardShortcut(ModifierKeys modifiers, Key key) : this()
{
Modifiers = modifiers;
Key = key;
}
/// <summary>
/// Initializes a new instance of the <see cref="KeyboardShortcut" /> class with the specified modifiers and the specified key.
/// </summary>
/// <param name="isCtrl">A value indicating whether this <see cref="KeyboardShortcut" /> uses the Control modifier.</param>
/// <param name="isShift">A value indicating whether this <see cref="KeyboardShortcut" /> uses the Shift modifier.</param>
/// <param name="isAlt">A value indicating whether this <see cref="KeyboardShortcut" /> uses the Alt modifier.</param>
/// <param name="isWin">A value indicating whether this <see cref="KeyboardShortcut" /> uses the Windows modifier.</param>
/// <param name="key">The key for this <see cref="KeyboardShortcut" />.</param>
public KeyboardShortcut(bool isCtrl, bool isShift, bool isAlt, bool isWin, Key key) : this()
{
IsCtrl = isCtrl;
IsShift = isShift;
IsAlt = isAlt;
IsWin = isWin;
Key = key;
}
/// <summary>
/// Returns the display name of this <see cref="KeyboardShortcut" />.
/// </summary>
/// <returns>
/// The display name of this <see cref="KeyboardShortcut" />.
/// </returns>
public override string ToString()
{
return DisplayName;
}
/// <summary>
/// Determines whether the specified <see cref="object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="object" /> to compare with this instance.</param>
/// <returns>
/// <see langword="true" />, if the specified <see cref="object" /> is equal to this instance;
/// otherwise, <see langword="false" />.
/// </returns>
public override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is KeyboardShortcut keyboardShortcut && Equals(keyboardShortcut);
}
/// <summary>
/// Determines whether this instance is equal to another <see cref="KeyboardShortcut" />.
/// </summary>
/// <param name="other">The <see cref="KeyboardShortcut" /> to compare to this instance.</param>
/// <returns>
/// <see langword="true" />, if this instance is equal to the <paramref name="other" /> parameter;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Equals([NotNullWhen(true)] KeyboardShortcut? other)
{
return
other != null &&
CSharp.TypeEquals(this, other) &&
Modifiers == other.Modifiers &&
Key == other.Key;
}
/// <summary>
/// Returns a hash code for this <see cref="KeyboardShortcut" />.
/// </summary>
/// <returns>
/// The hash code for this <see cref="KeyboardShortcut" /> instance.
/// </returns>
public override int GetHashCode()
{
return HashCode.Combine((int)Modifiers << 10, Key);
}
/// <summary>
/// Compares two <see cref="KeyboardShortcut" /> instances for equality.
/// </summary>
/// <param name="a">The first <see cref="KeyboardShortcut" /> to compare.</param>
/// <param name="b">The second <see cref="KeyboardShortcut" /> to compare.</param>
/// <returns>
/// <see langword="true" />, if both <see cref="KeyboardShortcut" /> are equal;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator ==(KeyboardShortcut? a, KeyboardShortcut? b)
{
return Equals(a, b);
}
/// <summary>
/// Compares two <see cref="KeyboardShortcut" /> instances for inequality.
/// </summary>
/// <param name="a">The first <see cref="KeyboardShortcut" /> to compare.</param>
/// <param name="b">The second <see cref="KeyboardShortcut" /> to compare.</param>
/// <returns>
/// <see langword="true" />, if both <see cref="KeyboardShortcut" /> are not equal;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool operator !=(KeyboardShortcut? a, KeyboardShortcut? b)
{
return !(a == b);
}
}