Skip to content

Commit 1defb6a

Browse files
Refactor HotkeyModel
1 parent ac190bd commit 1defb6a

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

Flow.Launcher.Infrastructure/Hotkey/HotkeyModel.cs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,21 @@ public class HotkeyModel
1111
public bool Shift { get; set; }
1212
public bool Win { get; set; }
1313
public bool Ctrl { get; set; }
14-
public Key CharKey { get; set; }
1514

15+
private Key charKey = Key.None;
16+
public Key CharKey
17+
{
18+
get => charKey;
19+
set
20+
{
21+
if (ValidateHotkey(value))
22+
{
23+
charKey = value;
24+
}
25+
}
26+
}
1627

17-
Dictionary<Key, string> specialSymbolDictionary = new Dictionary<Key, string>
28+
private static readonly Dictionary<Key, string> specialSymbolDictionary = new Dictionary<Key, string>
1829
{
1930
{Key.Space, "Space"},
2031
{Key.Oem3, "~"}
@@ -86,7 +97,7 @@ private void Parse(string hotkeyString)
8697
Ctrl = true;
8798
keys.Remove("Ctrl");
8899
}
89-
if (keys.Count > 0)
100+
if (keys.Count == 1)
90101
{
91102
string charKey = keys[0];
92103
KeyValuePair<Key, string>? specialSymbolPair = specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey);
@@ -110,36 +121,61 @@ private void Parse(string hotkeyString)
110121

111122
public override string ToString()
112123
{
113-
string text = string.Empty;
124+
List<string> keys = new List<string>();
114125
if (Ctrl)
115126
{
116-
text += "Ctrl + ";
127+
keys.Add("Ctrl");
117128
}
118129
if (Alt)
119130
{
120-
text += "Alt + ";
131+
keys.Add("Alt");
121132
}
122133
if (Shift)
123134
{
124-
text += "Shift + ";
135+
keys.Add("Shift");
125136
}
126137
if (Win)
127138
{
128-
text += "Win + ";
139+
keys.Add("Win");
129140
}
130141

131142
if (CharKey != Key.None)
132143
{
133-
text += specialSymbolDictionary.ContainsKey(CharKey)
144+
keys.Add(specialSymbolDictionary.ContainsKey(CharKey)
134145
? specialSymbolDictionary[CharKey]
135-
: CharKey.ToString();
146+
: CharKey.ToString());
147+
}
148+
return string.Join(" + ", keys);
149+
}
150+
151+
private static bool ValidateHotkey(Key key)
152+
{
153+
HashSet<Key> invalidKeys = new()
154+
{
155+
Key.LeftAlt, Key.RightAlt,
156+
Key.LeftCtrl, Key.RightCtrl,
157+
Key.LeftShift, Key.RightShift,
158+
Key.LWin, Key.RWin,
159+
};
160+
161+
return !invalidKeys.Contains(key);
162+
}
163+
164+
public override bool Equals(object obj)
165+
{
166+
if (obj is HotkeyModel other)
167+
{
168+
return ModifierKeys == other.ModifierKeys && CharKey == other.charKey;
136169
}
137-
else if (!string.IsNullOrEmpty(text))
170+
else
138171
{
139-
text = text.Remove(text.Length - 3);
172+
return false;
140173
}
174+
}
141175

142-
return text;
176+
public override int GetHashCode()
177+
{
178+
return this.ToString().GetHashCode();
143179
}
144180
}
145181
}

0 commit comments

Comments
 (0)