From f909c427562304c5c1c94a4d61b5f7a69827b4f5 Mon Sep 17 00:00:00 2001 From: oriash93 Date: Mon, 22 Mar 2021 12:45:43 +0200 Subject: [PATCH] refactor key mapping and settings --- AutoClicker/Utils/ApplicationSettings.cs | 8 ++++-- AutoClicker/Utils/Constants.cs | 8 +++--- AutoClicker/Utils/KeyMapping.cs | 7 ------ AutoClicker/Utils/KeyMappingUtils.cs | 32 +++++++++++++----------- AutoClicker/Utils/SettingsUtils.cs | 22 +++------------- AutoClicker/Views/MainWindow.xaml.cs | 12 ++++----- AutoClicker/Views/SettingsWindow.xaml | 8 +++--- AutoClicker/Views/SettingsWindow.xaml.cs | 14 +++++------ 8 files changed, 47 insertions(+), 64 deletions(-) diff --git a/AutoClicker/Utils/ApplicationSettings.cs b/AutoClicker/Utils/ApplicationSettings.cs index 27a1a68..1f130cb 100644 --- a/AutoClicker/Utils/ApplicationSettings.cs +++ b/AutoClicker/Utils/ApplicationSettings.cs @@ -2,9 +2,13 @@ { public class ApplicationSettings { - public KeyMapping StartHotkey { get; set; } = new KeyMapping(Constants.DEFAULT_START_HOTKEY); + public static readonly KeyMapping defaultStartKeyMapping = KeyMappingUtils.GetKeyMappingByCode(Constants.DEFAULT_START_HOTKEY); - public KeyMapping StopHotkey { get; set; } = new KeyMapping(Constants.DEFAULT_STOP_HOTKEY); + public static readonly KeyMapping defaultStopKeyMapping = KeyMappingUtils.GetKeyMappingByCode(Constants.DEFAULT_STOP_HOTKEY); + + public KeyMapping StartHotkey { get; set; } = defaultStartKeyMapping; + + public KeyMapping StopHotkey { get; set; } = defaultStopKeyMapping; public bool MinimizeToTray { get; set; } = false; } diff --git a/AutoClicker/Utils/Constants.cs b/AutoClicker/Utils/Constants.cs index 314f9fc..0855406 100644 --- a/AutoClicker/Utils/Constants.cs +++ b/AutoClicker/Utils/Constants.cs @@ -1,6 +1,4 @@ -using System.Windows.Input; - -namespace AutoClicker.Utils +namespace AutoClicker.Utils { public static class Constants { @@ -28,7 +26,7 @@ public static class Constants public const int STOP_HOTKEY_ID = 9001; public const int WM_HOTKEY = 0x0312; - public const Key DEFAULT_START_HOTKEY = Key.F6; - public const Key DEFAULT_STOP_HOTKEY = Key.F7; + public const int DEFAULT_START_HOTKEY = 0x75; + public const int DEFAULT_STOP_HOTKEY = 0x76; } } diff --git a/AutoClicker/Utils/KeyMapping.cs b/AutoClicker/Utils/KeyMapping.cs index cc25a0b..5a73c44 100644 --- a/AutoClicker/Utils/KeyMapping.cs +++ b/AutoClicker/Utils/KeyMapping.cs @@ -1,6 +1,5 @@ using System; using System.Text.Json.Serialization; -using System.Windows.Input; namespace AutoClicker.Utils { @@ -12,12 +11,6 @@ public KeyMapping() { } - public KeyMapping(Key key) - { - DisplayName = key.ToString(); - VirtualKeyCode = KeyInterop.VirtualKeyFromKey(key); - } - [JsonPropertyName("display_name")] public string DisplayName { get; set; } diff --git a/AutoClicker/Utils/KeyMappingUtils.cs b/AutoClicker/Utils/KeyMappingUtils.cs index 62bd299..3fcabab 100644 --- a/AutoClicker/Utils/KeyMappingUtils.cs +++ b/AutoClicker/Utils/KeyMappingUtils.cs @@ -9,33 +9,35 @@ public static class KeyMappingUtils { private static readonly string keysMappingPath = Path.Combine(Environment.CurrentDirectory, Constants.RESOURCES_DIRECTORY, Constants.KEY_MAPPINGS_RESOURCE_PATH); - - private static List keyMapping; - public static List KeyMapping + public static List KeyMapping { get; set; } + + static KeyMappingUtils() { - get - { - if (keyMapping == null) - { - LoadMapping(); - } - return keyMapping; - } - set + LoadMapping(); + } + + private static void LoadMapping() + { + if (KeyMapping == null) { - keyMapping = value; + ReadMapping(); } } - private static void LoadMapping() + public static KeyMapping GetKeyMappingByCode(int virtualKeyCode) + { + return KeyMapping.Find(keyMapping => keyMapping.VirtualKeyCode == virtualKeyCode); + } + + private static void ReadMapping() { try { if (File.Exists(keysMappingPath)) { string jsonString = File.ReadAllText(keysMappingPath); - keyMapping = JsonSerializer.Deserialize>(jsonString); + KeyMapping = JsonSerializer.Deserialize>(jsonString); } else { diff --git a/AutoClicker/Utils/SettingsUtils.cs b/AutoClicker/Utils/SettingsUtils.cs index 063b49c..088efd4 100644 --- a/AutoClicker/Utils/SettingsUtils.cs +++ b/AutoClicker/Utils/SettingsUtils.cs @@ -10,43 +10,29 @@ public static class SettingsUtils private static readonly string settingsFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Constants.SETTINGS_FILE_PATH); - private static ApplicationSettings CurrentSettings { get; set; } + public static ApplicationSettings CurrentSettings { get; set; } static SettingsUtils() { CurrentSettings = new ApplicationSettings(); } - public static KeyMapping GetStartHotKey() - { - return CurrentSettings.StartHotkey; - } - public static void SetStartHotKey(KeyMapping key) { CurrentSettings.StartHotkey = key; NotifyChanges(CurrentSettings.StartHotkey, Operation.Start); } - public static KeyMapping GetStopHotKey() - { - return CurrentSettings.StopHotkey; - } - public static void SetStopHotKey(KeyMapping key) { CurrentSettings.StopHotkey = key; NotifyChanges(CurrentSettings.StopHotkey, Operation.Stop); } - public static void SetMinimizeToTray(bool minimize) - { - CurrentSettings.MinimizeToTray = minimize; - } - - public static bool GetMinimizeToTray() + public static void Reset() { - return CurrentSettings.MinimizeToTray; + SetStartHotKey(ApplicationSettings.defaultStartKeyMapping); + SetStopHotKey(ApplicationSettings.defaultStopKeyMapping); } private static void NotifyChanges(KeyMapping hotkey, Operation operation) diff --git a/AutoClicker/Views/MainWindow.xaml.cs b/AutoClicker/Views/MainWindow.xaml.cs index 3e4779e..f68e312 100644 --- a/AutoClicker/Views/MainWindow.xaml.cs +++ b/AutoClicker/Views/MainWindow.xaml.cs @@ -154,15 +154,15 @@ protected override void OnSourceInitialized(EventArgs e) _source.AddHook(StartStopHooks); SettingsUtils.HotKeyChangedEvent += AppSettings_HotKeyChanged; - RegisterHotkey(Constants.START_HOTKEY_ID, SettingsUtils.GetStartHotKey()); - RegisterHotkey(Constants.STOP_HOTKEY_ID, SettingsUtils.GetStopHotKey()); + RegisterHotkey(Constants.START_HOTKEY_ID, SettingsUtils.CurrentSettings.StartHotkey); + RegisterHotkey(Constants.STOP_HOTKEY_ID, SettingsUtils.CurrentSettings.StopHotkey); InitializeSystemTrayIcon(); } protected override void OnStateChanged(EventArgs e) { - if (WindowState == WindowState.Minimized && SettingsUtils.GetMinimizeToTray()) + if (WindowState == WindowState.Minimized && SettingsUtils.CurrentSettings.MinimizeToTray) { Hide(); } @@ -383,11 +383,11 @@ private IntPtr StartStopHooks(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam if (msg == Constants.WM_HOTKEY && hotkeyId == Constants.START_HOTKEY_ID || hotkeyId == Constants.STOP_HOTKEY_ID) { int virtualKey = ((int)lParam >> 16) & 0xFFFF; - if (virtualKey == SettingsUtils.GetStartHotKey().VirtualKeyCode && CanStart()) + if (virtualKey == SettingsUtils.CurrentSettings.StartHotkey.VirtualKeyCode && CanStart()) { StartCommand_Execute(null, null); } - if (virtualKey == SettingsUtils.GetStopHotKey().VirtualKeyCode && clickTimer.Enabled) + if (virtualKey == SettingsUtils.CurrentSettings.StopHotkey.VirtualKeyCode && clickTimer.Enabled) { StopCommand_Execute(null, null); } @@ -437,7 +437,7 @@ private void OnMenuItemCheckChanged(object sender, RoutedEventArgs e) { if (sender is MenuItem menuItem) { - SettingsUtils.SetMinimizeToTray(menuItem.IsChecked); + SettingsUtils.CurrentSettings.MinimizeToTray = menuItem.IsChecked; } } diff --git a/AutoClicker/Views/SettingsWindow.xaml b/AutoClicker/Views/SettingsWindow.xaml index 2bb5cf6..3ce76d9 100644 --- a/AutoClicker/Views/SettingsWindow.xaml +++ b/AutoClicker/Views/SettingsWindow.xaml @@ -27,21 +27,21 @@