Flow.Launcher/Flow.Launcher.Infrastructure/Hotkey/HotkeyModel.cs

186 lines
5 KiB
C#
Raw Normal View History

2014-02-22 07:52:20 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
2023-01-10 07:01:30 +00:00
using System.Windows.Navigation;
2014-02-22 07:52:20 +00:00
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Infrastructure.Hotkey
2014-02-22 07:52:20 +00:00
{
public class HotkeyModel
{
public bool Alt { get; set; }
public bool Shift { get; set; }
public bool Win { get; set; }
public bool Ctrl { get; set; }
2023-01-11 08:11:34 +00:00
public Key CharKey { get; set; } = Key.None;
2023-01-09 10:02:50 +00:00
private static readonly Dictionary<Key, string> specialSymbolDictionary = new Dictionary<Key, string>
{
{Key.Space, "Space"},
{Key.Oem3, "~"}
};
2014-02-22 07:52:20 +00:00
public ModifierKeys ModifierKeys
{
get
{
ModifierKeys modifierKeys = ModifierKeys.None;
if (Alt)
{
2023-01-11 12:08:13 +00:00
modifierKeys |= ModifierKeys.Alt;
2014-02-22 07:52:20 +00:00
}
if (Shift)
{
2023-01-11 12:08:13 +00:00
modifierKeys |= ModifierKeys.Shift;
2014-02-22 07:52:20 +00:00
}
if (Win)
{
2023-01-11 12:08:13 +00:00
modifierKeys |= ModifierKeys.Windows;
2014-02-22 07:52:20 +00:00
}
if (Ctrl)
{
2023-01-11 12:08:13 +00:00
modifierKeys |= ModifierKeys.Control;
2014-02-22 07:52:20 +00:00
}
return modifierKeys;
}
}
public HotkeyModel(string hotkeyString)
{
Parse(hotkeyString);
2014-02-22 07:52:20 +00:00
}
public HotkeyModel(bool alt, bool shift, bool win, bool ctrl, Key key)
{
Alt = alt;
Shift = shift;
Win = win;
Ctrl = ctrl;
CharKey = key;
}
private void Parse(string hotkeyString)
2014-02-22 07:52:20 +00:00
{
if (string.IsNullOrEmpty(hotkeyString))
2014-02-22 07:52:20 +00:00
{
return;
}
List<string> keys = hotkeyString.Replace(" ", "").Split('+').ToList();
if (keys.Contains("Alt"))
{
Alt = true;
keys.Remove("Alt");
}
if (keys.Contains("Shift"))
{
Shift = true;
keys.Remove("Shift");
}
if (keys.Contains("Win"))
{
Win = true;
keys.Remove("Win");
}
if (keys.Contains("Ctrl"))
{
Ctrl = true;
keys.Remove("Ctrl");
}
2023-01-09 10:02:50 +00:00
if (keys.Count == 1)
{
string charKey = keys[0];
KeyValuePair<Key, string>? specialSymbolPair = specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey);
if (specialSymbolPair.Value.Value != null)
2014-02-22 07:52:20 +00:00
{
CharKey = specialSymbolPair.Value.Key;
2014-02-22 07:52:20 +00:00
}
else
2014-02-22 07:52:20 +00:00
{
try
{
CharKey = (Key) Enum.Parse(typeof (Key), charKey);
2014-02-22 07:52:20 +00:00
}
catch (ArgumentException)
2014-02-22 07:52:20 +00:00
{
}
}
}
}
public override string ToString()
{
2023-01-09 10:02:50 +00:00
List<string> keys = new List<string>();
2014-02-22 07:52:20 +00:00
if (Ctrl)
{
2023-01-09 10:02:50 +00:00
keys.Add("Ctrl");
2014-02-22 07:52:20 +00:00
}
if (Alt)
{
2023-01-09 10:02:50 +00:00
keys.Add("Alt");
2014-02-22 07:52:20 +00:00
}
if (Shift)
{
2023-01-09 10:02:50 +00:00
keys.Add("Shift");
2014-02-22 07:52:20 +00:00
}
if (Win)
{
2023-01-09 10:02:50 +00:00
keys.Add("Win");
}
if (CharKey != Key.None)
{
2023-01-09 10:02:50 +00:00
keys.Add(specialSymbolDictionary.ContainsKey(CharKey)
2016-01-06 21:34:42 +00:00
? specialSymbolDictionary[CharKey]
2023-01-09 10:02:50 +00:00
: CharKey.ToString());
}
return string.Join(" + ", keys);
}
2023-01-11 08:11:34 +00:00
public bool Validate()
2023-01-09 10:02:50 +00:00
{
2023-01-11 08:11:34 +00:00
switch (CharKey)
{
case Key.LeftAlt:
case Key.RightAlt:
case Key.LeftCtrl:
case Key.RightCtrl:
case Key.LeftShift:
case Key.RightShift:
case Key.LWin:
case Key.RWin:
return false;
default:
if (ModifierKeys == ModifierKeys.None)
{
2023-01-11 16:27:19 +00:00
return !((CharKey >= Key.A && CharKey <= Key.Z) ||
(CharKey >= Key.D0 && CharKey <= Key.D9) ||
(CharKey >= Key.NumPad0 && CharKey <= Key.NumPad9));
2023-01-11 08:11:34 +00:00
}
else
{
return CharKey != Key.None;
}
2023-01-10 07:01:30 +00:00
}
2023-01-09 10:02:50 +00:00
}
public override bool Equals(object obj)
{
if (obj is HotkeyModel other)
{
2023-01-11 08:11:34 +00:00
return ModifierKeys == other.ModifierKeys && CharKey == other.CharKey;
2014-02-22 07:52:20 +00:00
}
2023-01-09 10:02:50 +00:00
else
2014-02-22 07:52:20 +00:00
{
2023-01-09 10:02:50 +00:00
return false;
2014-02-22 07:52:20 +00:00
}
2023-01-09 10:02:50 +00:00
}
2014-02-22 07:52:20 +00:00
2023-01-09 10:02:50 +00:00
public override int GetHashCode()
{
2023-01-11 08:14:50 +00:00
return HashCode.Combine(ModifierKeys, CharKey);
2014-02-22 07:52:20 +00:00
}
}
}