Skip to content

Commit 41f51fc

Browse files
0x5bfayaira2
authored andcommitted
Init
1 parent afcd8b4 commit 41f51fc

File tree

23 files changed

+1227
-109
lines changed

23 files changed

+1227
-109
lines changed

src/Files.App/Actions/Content/Archives/Decompress/DecompressArchiveToChildFolderAction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected override void Context_PropertyChanged(object? sender, PropertyChangedE
4040
private string ComputeLabel()
4141
{
4242
if (context.SelectedItems == null || context.SelectedItems.Count == 0)
43-
return string.Empty;
43+
return string.Format("BaseLayoutItemContextFlyoutExtractToChildFolder".GetLocalizedResource(), string.Empty);
4444

4545
return context.SelectedItems.Count > 1
4646
? string.Format("BaseLayoutItemContextFlyoutExtractToChildFolder".GetLocalizedResource(), "*")

src/Files.App/Actions/Navigation/NavigateBackAction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public HotKey SecondHotKey
2222
public HotKey ThirdHotKey
2323
=> new(Keys.Mouse4);
2424

25-
public HotKey MediaHotKey
26-
=> new(Keys.GoBack, false);
25+
//public HotKey MediaHotKey
26+
// => new(Keys.GoBack, false);
2727

2828
public RichGlyph Glyph
2929
=> new("\uE72B");

src/Files.App/Actions/Navigation/NavigateForwardAction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public HotKey HotKey
1919
public HotKey SecondHotKey
2020
=> new(Keys.Mouse5);
2121

22-
public HotKey MediaHotKey
23-
=> new(Keys.GoForward, false);
22+
//public HotKey MediaHotKey
23+
// => new(Keys.GoForward, false);
2424

2525
public RichGlyph Glyph
2626
=> new("\uE72A");

src/Files.App/Data/Commands/HotKey/HotKey.cs

Lines changed: 81 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88

99
namespace Files.App.Data.Commands
1010
{
11+
/// <summary>
12+
/// Represents hot key.
13+
/// </summary>
1114
[DebuggerDisplay("{Code}")]
1215
public readonly struct HotKey : IEquatable<HotKey>
1316
{
14-
public static readonly FrozenDictionary<KeyModifiers, string> modifiers = new Dictionary<KeyModifiers, string>()
17+
public static FrozenDictionary<KeyModifiers, string> LocalizedModifiers { get; } = new Dictionary<KeyModifiers, string>()
1518
{
1619
[KeyModifiers.Menu] = GetKeyString("Menu"),
1720
[KeyModifiers.Ctrl] = GetKeyString("Control"),
1821
[KeyModifiers.Shift] = GetKeyString("Shift"),
1922
[KeyModifiers.Win] = GetKeyString("Windows"),
2023
}.ToFrozenDictionary();
2124

22-
public static readonly FrozenDictionary<Keys, string> keys = new Dictionary<Keys, string>()
25+
public static FrozenDictionary<Keys, string> LocalizedKeys { get; } = new Dictionary<Keys, string>()
2326
{
2427
[Keys.Enter] = GetKeyString("Enter"),
2528
[Keys.Space] = GetKeyString("Space"),
@@ -153,29 +156,52 @@ namespace Files.App.Data.Commands
153156
[Keys.VolumeUp] = GetKeyString("MediaVolumeUp"),
154157
}.ToFrozenDictionary();
155158

159+
/// <summary>
160+
/// Gets the none value.
161+
/// </summary>
156162
public static HotKey None { get; } = new(Keys.None, KeyModifiers.None);
157163

164+
/// <summary>
165+
/// Gets the value that indicates whether the hotkey is none.
166+
/// </summary>
158167
public bool IsNone => Key is Keys.None && Modifier is KeyModifiers.None;
159168

169+
/// <summary>
170+
/// Gets the value that indicates whether the key should be visible.
171+
/// </summary>
172+
/// <remarks>
173+
/// This is always true for now.
174+
/// </remarks>
160175
public bool IsVisible { get; init; }
161176

177+
/// <summary>
178+
/// Gets the key.
179+
/// </summary>
162180
public Keys Key { get; }
181+
182+
/// <summary>
183+
/// Gets the modifier.
184+
/// </summary>
163185
public KeyModifiers Modifier { get; }
164186

187+
/// <summary>
188+
/// Gets the raw humanized code of the hotkey.
189+
/// </summary>
190+
/// <remarks>
191+
/// For example, this is "Ctrl+A" and "Ctrl+Menu+C"
192+
/// </remarks>
165193
public string Code
166194
{
167195
get
168196
{
169197
return (Key, Modifier) switch
170198
{
171199
(Keys.None, KeyModifiers.None) => string.Empty,
172-
(Keys.None, _) => $"{GetVisibleCode(IsVisible)}{GetModifierCode(Modifier)}",
173-
(_, KeyModifiers.None) => $"{GetVisibleCode(IsVisible)}{Key}",
174-
_ => $"{GetVisibleCode(IsVisible)}{GetModifierCode(Modifier)}+{Key}",
200+
(Keys.None, _) => $"{GetModifierCode(Modifier)}",
201+
(_, KeyModifiers.None) => $"{Key}",
202+
_ => $"{GetModifierCode(Modifier)}+{Key}",
175203
};
176204

177-
static string GetVisibleCode(bool isVisible) => isVisible ? string.Empty : "!";
178-
179205
static string GetModifierCode(KeyModifiers modifiers)
180206
{
181207
StringBuilder builder = new();
@@ -193,6 +219,12 @@ static string GetModifierCode(KeyModifiers modifiers)
193219
}
194220
}
195221

222+
/// <summary>
223+
/// Gets the humanized and localized label of the hotkey to shown in the UI.
224+
/// </summary>
225+
/// <remarks>
226+
/// For example, this is "Ctrl+A" and "Ctrl+Alt+C"
227+
/// </remarks>
196228
public string Label
197229
{
198230
get
@@ -201,29 +233,34 @@ public string Label
201233
{
202234
(Keys.None, KeyModifiers.None) => string.Empty,
203235
(Keys.None, _) => GetModifierCode(Modifier),
204-
(_, KeyModifiers.None) => keys[Key],
205-
_ => $"{GetModifierCode(Modifier)}+{keys[Key]}",
236+
(_, KeyModifiers.None) => LocalizedKeys[Key],
237+
_ => $"{GetModifierCode(Modifier)}+{LocalizedKeys[Key]}",
206238
};
207239

208240
static string GetModifierCode(KeyModifiers modifier)
209241
{
210242
StringBuilder builder = new();
211243
if (modifier.HasFlag(KeyModifiers.Menu))
212-
builder.Append($"+{modifiers[KeyModifiers.Menu]}");
244+
builder.Append($"+{LocalizedModifiers[KeyModifiers.Menu]}");
213245
if (modifier.HasFlag(KeyModifiers.Ctrl))
214-
builder.Append($"+{modifiers[KeyModifiers.Ctrl]}");
246+
builder.Append($"+{LocalizedModifiers[KeyModifiers.Ctrl]}");
215247
if (modifier.HasFlag(KeyModifiers.Shift))
216-
builder.Append($"+{modifiers[KeyModifiers.Shift]}");
248+
builder.Append($"+{LocalizedModifiers[KeyModifiers.Shift]}");
217249
if (modifier.HasFlag(KeyModifiers.Win))
218-
builder.Append($"+{modifiers[KeyModifiers.Win]}");
250+
builder.Append($"+{LocalizedModifiers[KeyModifiers.Win]}");
219251
builder.Remove(0, 1);
220252
return builder.ToString();
221253
}
222254
}
223255
}
224256

225-
public HotKey(Keys key, bool isVisible = true) : this(key, KeyModifiers.None, isVisible) {}
226-
public HotKey(Keys key, KeyModifiers modifier, bool isVisible = true)
257+
/// <summary>
258+
/// Initializes an instance of <see cref="HotKey"/>.
259+
/// </summary>
260+
/// <param name="key">A key</param>
261+
/// <param name="modifier">A modifier</param>
262+
/// <param name="isVisible">A value that indicates the hotkey should be available.</param>
263+
public HotKey(Keys key, KeyModifiers modifier = KeyModifiers.None, bool isVisible = true)
227264
{
228265
if (!Enum.IsDefined(key) || !Enum.IsDefined(modifier))
229266
return;
@@ -233,67 +270,65 @@ public HotKey(Keys key, KeyModifiers modifier, bool isVisible = true)
233270
Modifier = modifier;
234271
}
235272

236-
public void Deconstruct(out Keys key, out KeyModifiers modifier)
237-
=> (key, modifier) = (Key, Modifier);
238-
public void Deconstruct(out Keys key, out KeyModifiers modifier, out bool isVisible)
239-
=> (key, modifier, isVisible) = (Key, Modifier, IsVisible);
240-
273+
/// <summary>
274+
/// Parses humanized hotkey code with separators.
275+
/// </summary>
276+
/// <param name="code">Humanized code to parse.</param>
277+
/// <returns>Humanized code with a format <see cref="HotKey"/>.</returns>
241278
public static HotKey Parse(string code)
242279
{
243280
var key = Keys.None;
244281
var modifier = KeyModifiers.None;
245282
bool isVisible = true;
246283

247284
code = code.Trim();
248-
if (code.StartsWith('!'))
249-
{
250-
isVisible = false;
251-
code = code.Remove(0, 1);
252-
}
253-
254285
var parts = code.Split('+').Select(part => part.Trim());
286+
255287
foreach (var part in parts)
256288
{
257-
if (Enum.TryParse(part, true, out Keys partKey))
258-
key = partKey;
259-
if (Enum.TryParse(part, true, out KeyModifiers partModifier))
260-
modifier |= partModifier;
289+
key |= LocalizedKeys.FirstOrDefault(x => x.Value == part).Key;
290+
modifier |= LocalizedModifiers.FirstOrDefault(x => x.Value == part).Key;
261291
}
292+
262293
return new(key, modifier, isVisible);
263294
}
264295

265-
public HotKeyCollection AsCollection() => new(this);
296+
/// <summary>
297+
/// Converts this <see cref="HotKey"/> instance into a <see cref="HotKeyCollection"/> instance.
298+
/// </summary>
299+
/// <returns></returns>
300+
public HotKeyCollection AsCollection()
301+
{
302+
return new(this);
303+
}
266304

267-
public static implicit operator string(HotKey hotKey) => hotKey.Label;
305+
// Operator overloads
268306

307+
public static implicit operator string(HotKey hotKey) => hotKey.Label;
269308
public static bool operator ==(HotKey a, HotKey b) => a.Equals(b);
270309
public static bool operator !=(HotKey a, HotKey b) => !a.Equals(b);
271310

272-
public override string ToString() => Label;
311+
// Default methods
273312

313+
public override string ToString() => Label;
274314
public override int GetHashCode() => (Key, Modifier, IsVisible).GetHashCode();
275315
public override bool Equals(object? other) => other is HotKey hotKey && Equals(hotKey);
276316
public bool Equals(HotKey other) => (other.Key, other.Modifier, other.IsVisible).Equals((Key, Modifier, IsVisible));
277317

278-
private static string GetKeyString(string key) => $"Key/{key}".GetLocalizedResource();
318+
// Private methods
319+
320+
private static string GetKeyString(string key)
321+
{
322+
return $"Key/{key}".GetLocalizedResource();
323+
}
279324

280325
private static string GetKeyCharacter(Forms.Keys key)
281326
{
282327
var buffer = new StringBuilder(256);
283328
var state = new byte[256];
284-
_ = ToUnicode((uint)key, 0, state, buffer, 256, 0);
329+
_ = Win32PInvoke.ToUnicode((uint)key, 0, state, buffer, 256, 0);
330+
285331
return buffer.ToString();
286332
}
287-
288-
[DllImport("user32.dll")]
289-
private static extern int ToUnicode
290-
(
291-
uint virtualKeyCode,
292-
uint scanCode,
293-
byte[] keyboardState,
294-
[Out, MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)] StringBuilder receivingBuffer,
295-
int bufferSize,
296-
uint flags
297-
);
298333
}
299334
}

0 commit comments

Comments
 (0)