Skip to content

Commit

Permalink
Merge pull request #42 from oriash93/fix/reset_settings
Browse files Browse the repository at this point in the history
refactor key mapping and settings
  • Loading branch information
oriash93 authored Mar 22, 2021
2 parents 4bb89f7 + f909c42 commit 5b6e662
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 64 deletions.
8 changes: 6 additions & 2 deletions AutoClicker/Utils/ApplicationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 3 additions & 5 deletions AutoClicker/Utils/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Windows.Input;

namespace AutoClicker.Utils
namespace AutoClicker.Utils
{
public static class Constants
{
Expand Down Expand Up @@ -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;
}
}
7 changes: 0 additions & 7 deletions AutoClicker/Utils/KeyMapping.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Text.Json.Serialization;
using System.Windows.Input;

namespace AutoClicker.Utils
{
Expand All @@ -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; }

Expand Down
32 changes: 17 additions & 15 deletions AutoClicker/Utils/KeyMappingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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> keyMapping;

public static List<KeyMapping> KeyMapping
public static List<KeyMapping> 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<List<KeyMapping>>(jsonString);
KeyMapping = JsonSerializer.Deserialize<List<KeyMapping>>(jsonString);
}
else
{
Expand Down
22 changes: 4 additions & 18 deletions AutoClicker/Utils/SettingsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions AutoClicker/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
}

Expand Down
8 changes: 4 additions & 4 deletions AutoClicker/Views/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@

<Label Grid.Row="0" Grid.Column="0" Margin="5, 5, 5, 5"
Content="Start Hotkey: "/>
<ComboBox Name="comboBox1"
<ComboBox Name="startKeyMapping"
Grid.Row="0" Grid.Column="1" Margin="5, 5, 5, 5"
ItemsSource="{Binding Path=KeyMapping}"
SelectedItem="{Binding SelectedStartKey}"
DisplayMemberPath="DisplayName"
SelectedIndex="0" SelectedItem="{Binding SelectedStartKey}"
HorizontalAlignment="Stretch"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>

<Label Grid.Row="1" Grid.Column="0" Margin="5, 5, 5, 5"
Content="Stop Hotkey: "/>
<ComboBox Name="comboBox2"
<ComboBox Name="stopKeyMapping"
Grid.Row="1" Grid.Column="1" Margin="5, 5, 5, 5"
ItemsSource="{Binding Path=KeyMapping}"
SelectedItem="{Binding SelectedStopKey}"
DisplayMemberPath="DisplayName"
SelectedIndex="0" SelectedItem="{Binding SelectedStopKey}"
HorizontalAlignment="Stretch"
VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>

Expand Down
14 changes: 7 additions & 7 deletions AutoClicker/Views/SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public SettingsWindow()
KeyMapping = KeyMappingUtils.KeyMapping;

Title = Constants.SETTINGS_WINDOW_TITLE;
SelectedStartKey = SettingsUtils.GetStartHotKey();
SelectedStopKey = SettingsUtils.GetStopHotKey();
SelectedStartKey = SettingsUtils.CurrentSettings.StartHotkey;
SelectedStopKey = SettingsUtils.CurrentSettings.StopHotkey;

InitializeComponent();
}
Expand All @@ -51,11 +51,11 @@ public SettingsWindow()

private void SaveCommand_Execute(object sender, ExecutedRoutedEventArgs e)
{
if (SelectedStartKey != SettingsUtils.GetStartHotKey())
if (SelectedStartKey != SettingsUtils.CurrentSettings.StartHotkey)
{
SettingsUtils.SetStartHotKey(SelectedStartKey);
}
if (SelectedStopKey != SettingsUtils.GetStopHotKey())
if (SelectedStopKey != SettingsUtils.CurrentSettings.StopHotkey)
{
SettingsUtils.SetStopHotKey(SelectedStopKey);
}
Expand All @@ -67,9 +67,9 @@ private void SaveCommand_Execute(object sender, ExecutedRoutedEventArgs e)

private void ResetCommand_Execute(object sender, ExecutedRoutedEventArgs e)
{
SettingsUtils.LoadSettings();
SelectedStartKey = SettingsUtils.GetStartHotKey();
SelectedStopKey = SettingsUtils.GetStopHotKey();
SettingsUtils.Reset();
SelectedStartKey = SettingsUtils.CurrentSettings.StartHotkey;
SelectedStopKey = SettingsUtils.CurrentSettings.StopHotkey;
}

#endregion Reset Command
Expand Down

0 comments on commit 5b6e662

Please sign in to comment.