-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Accelerator.cs
108 lines (94 loc) · 2.98 KB
/
Accelerator.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/Accelerator.xml" path="Type[@FullName='Microsoft.Maui.Controls.Accelerator']/Docs" />
[System.ComponentModel.TypeConverter(typeof(AcceleratorTypeConverter))]
public class Accelerator
{
const char Separator = '+';
string _text;
internal Accelerator(string text)
{
if (string.IsNullOrEmpty(text))
throw new ArgumentNullException(nameof(text));
_text = text;
}
/// <include file="../../docs/Microsoft.Maui.Controls/Accelerator.xml" path="//Member[@MemberName='Modifiers']/Docs" />
[EditorBrowsable(EditorBrowsableState.Never)]
public IEnumerable<string> Modifiers { get; set; }
/// <include file="../../docs/Microsoft.Maui.Controls/Accelerator.xml" path="//Member[@MemberName='Keys']/Docs" />
[EditorBrowsable(EditorBrowsableState.Never)]
public IEnumerable<string> Keys { get; set; }
/// <include file="../../docs/Microsoft.Maui.Controls/Accelerator.xml" path="//Member[@MemberName='FromString']/Docs" />
public static Accelerator FromString(string text)
{
var accelarat = new Accelerator(text);
var acceleratorParts = text.Split(Separator);
if (acceleratorParts.Length > 1)
{
var modifiers = new List<string>();
for (int i = 0; i < acceleratorParts.Length; i++)
{
var modifierMask = acceleratorParts[i];
var modiferMaskLower = modifierMask.ToLower();
switch (modiferMaskLower)
{
case "ctrl":
case "cmd":
case "alt":
case "shift":
case "fn":
case "win":
modifiers.Add(modiferMaskLower);
#if NETSTANDARD2_0
text = text.Replace(modifierMask, "");
#else
text = text.Replace(modifierMask, "", StringComparison.Ordinal);
#endif
break;
}
}
accelarat.Modifiers = modifiers;
}
if (text != Separator.ToString())
{
var keys = text.Split(new char[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
accelarat.Keys = keys;
}
else
{
accelarat.Keys = new[] { text };
}
return accelarat;
}
/// <include file="../../docs/Microsoft.Maui.Controls/Accelerator.xml" path="//Member[@MemberName='ToString']/Docs" />
public override string ToString()
{
return _text;
}
/// <include file="../../docs/Microsoft.Maui.Controls/Accelerator.xml" path="//Member[@MemberName='Equals']/Docs" />
public override bool Equals(object obj)
{
return obj is Accelerator && Equals((Accelerator)obj);
}
bool Equals(Accelerator other)
{
return other.ToString() == ToString();
}
/// <include file="../../docs/Microsoft.Maui.Controls/Accelerator.xml" path="//Member[@MemberName='GetHashCode']/Docs" />
public override int GetHashCode()
{
#if NETSTANDARD2_0
return _text.GetHashCode();
#else
return _text.GetHashCode(StringComparison.Ordinal);
#endif
}
public static implicit operator Accelerator(string accelerator)
{
return FromString(accelerator);
}
}
}