Skip to content

Commit

Permalink
General improvements (#85)
Browse files Browse the repository at this point in the history
* Align usage of logging to not use interpolation
* Align all casing of hotkey in code
* General readability and maintainability changes
  • Loading branch information
oriash93 authored Aug 25, 2024
1 parent a8240ec commit 86bee27
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 58 deletions.
6 changes: 6 additions & 0 deletions AutoClicker/AutoClicker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
<PropertyGroup>
<ApplicationIcon>Resources\Icons\icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup>
<StartupObject>AutoClicker.App</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="CommonServiceLocator, Version=2.0.6.0, Culture=neutral, PublicKeyToken=489b6accfaf20ef0, processorArchitecture=MSIL">
<HintPath>packages\CommonServiceLocator.2.0.6\lib\net48\CommonServiceLocator.dll</HintPath>
Expand All @@ -76,6 +79,9 @@
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Collections.Immutable, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>packages\System.Collections.Immutable.8.0.0\lib\net462\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.Deployment" />
<Reference Include="System.Diagnostics.DiagnosticSource, Version=8.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>packages\System.Diagnostics.DiagnosticSource.8.0.1\lib\net462\System.Diagnostics.DiagnosticSource.dll</HintPath>
Expand Down
5 changes: 1 addition & 4 deletions AutoClicker/Commands/MainWindowCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public static class MainWindowCommands
AssemblyUtils.CreateCommand(typeof(MainWindowCommands), nameof(About), new KeyGesture(Key.F1));

public static readonly RoutedUICommand CaptureMouseScreenCoordinates =
AssemblyUtils.CreateCommand(
typeof(MainWindowCommands),
nameof(CaptureMouseScreenCoordinates)
);
AssemblyUtils.CreateCommand(typeof(MainWindowCommands), nameof(CaptureMouseScreenCoordinates));
}
}
3 changes: 2 additions & 1 deletion AutoClicker/Utils/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

namespace AutoClicker.Utils
Expand Down Expand Up @@ -36,7 +37,7 @@ public static class Constants
public const int MOD_ALT = 0x0001;
public const int MOD_CONTROL = 0x0002;
public const int MOD_SHIFT = 0x0004;
public static readonly List<int> MODIFIERS = new List<int> { MOD_NONE, MOD_ALT, MOD_CONTROL, MOD_SHIFT };
public static readonly ImmutableList<int> MODIFIERS = ImmutableList.Create(MOD_NONE, MOD_ALT, MOD_CONTROL, MOD_SHIFT);

public const int START_HOTKEY_ID = 9000;
public const int STOP_HOTKEY_ID = 9004;
Expand Down
2 changes: 1 addition & 1 deletion AutoClicker/Utils/JsonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static T ReadJson<T>(string filePath)
Log.Debug("Reading file = {FilePath}", filePath);
string jsonString = File.ReadAllText(filePath);
T result = JsonSerializer.Deserialize<T>(jsonString);
Log.Debug("Read from file successfully", filePath);
Log.Debug("Read from file {FilePath} successfully", filePath);
return result;
}
else
Expand Down
7 changes: 3 additions & 4 deletions AutoClicker/Utils/KeyMappingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ static KeyMappingUtils()

private static void LoadMapping()
{
if (KeyMapping == null)
{
ReadMapping();
}
if (KeyMapping != null)
return;
ReadMapping();
}

public static KeyMapping GetKeyMappingByCode(int virtualKeyCode)
Expand Down
16 changes: 8 additions & 8 deletions AutoClicker/Utils/SettingsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ static SettingsUtils()
LoadSettingsFromFile();
}

public static void SetStartHotKey(KeyMapping key)
public static void SetStartHotkey(KeyMapping key)
{
CurrentSettings.HotkeySettings.StartHotkey = key;
NotifyChanges(CurrentSettings.HotkeySettings.StartHotkey, Operation.Start);
}

public static void SetStopHotKey(KeyMapping key)
public static void SetStopHotkey(KeyMapping key)
{
CurrentSettings.HotkeySettings.StopHotkey = key;
NotifyChanges(CurrentSettings.HotkeySettings.StopHotkey, Operation.Stop);
}

public static void SetToggleHotKey(KeyMapping key)
public static void SetToggleHotkey(KeyMapping key)
{
CurrentSettings.HotkeySettings.ToggleHotkey = key;
NotifyChanges(CurrentSettings.HotkeySettings.ToggleHotkey, Operation.Toggle);
Expand All @@ -57,9 +57,9 @@ public static void SetIncludeModifiers(bool includeModifiers)
public static void Reset()
{
Log.Information("Reset hotkey settings to default");
SetStartHotKey(HotkeySettings.defaultStartKeyMapping);
SetStopHotKey(HotkeySettings.defaultStopKeyMapping);
SetToggleHotKey(HotkeySettings.defaultToggleKeyMapping);
SetStartHotkey(HotkeySettings.defaultStartKeyMapping);
SetStopHotkey(HotkeySettings.defaultStopKeyMapping);
SetToggleHotkey(HotkeySettings.defaultToggleKeyMapping);
SetIncludeModifiers(HotkeySettings.defaultIncludeModifiers);
}

Expand All @@ -71,12 +71,12 @@ private static void NotifyChanges(KeyMapping hotkey, Operation operation)
Operation = operation,
IncludeModifiers = CurrentSettings.HotkeySettings.IncludeModifiers
};
HotKeyChangedEvent.Invoke(null, args);
HotkeyChangedEvent.Invoke(null, args);

SaveSettingsToFile();
}

public static event EventHandler<HotkeyChangedEventArgs> HotKeyChangedEvent;
public static event EventHandler<HotkeyChangedEventArgs> HotkeyChangedEvent;

private static void SaveSettingsToFile()
{
Expand Down
6 changes: 3 additions & 3 deletions AutoClicker/Utils/User32ApiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public static class User32ApiUtils
[DllImport("user32.dll", EntryPoint = "mouse_event")]
internal static extern void ExecuteMouseEvent(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

[DllImport("user32.dll")]
internal static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[DllImport("user32.dll", EntryPoint = "RegisterHotKey")]
internal static extern bool RegisterHotkey(IntPtr hWnd, int id, int fsModifiers, int vk);

[DllImport("user32.dll", EntryPoint = "UnregisterHotKey")]
internal static extern bool DeregisterHotKey(IntPtr hWnd, int id);
internal static extern bool DeregisterHotkey(IntPtr hWnd, int id);
}
}
20 changes: 8 additions & 12 deletions AutoClicker/Views/CaptureMouseScreenCoordinatesWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CaptureMouseScreenCoordinatesWindow()
DataContext = this;
InitializeComponent();

Log.Information("Opening window to capture mouse coordinates.");
Log.Information("Opening capture mouse coordinates window");

Title = Constants.CAPTURE_MOUSE_COORDINATES_WINDOW_TITLE;
Width = 0;
Expand All @@ -31,10 +31,9 @@ public CaptureMouseScreenCoordinatesWindow()
WindowStyle = WindowStyle.None;
WindowStartupLocation = WindowStartupLocation.Manual;
ResizeMode = ResizeMode.NoResize;
//Topmost = true;

var screens = Screen.AllScreens;
Log.Debug($"Total screens detected: {screens.Length}");
Screen[] screens = Screen.AllScreens;
Log.Debug("Total number of screens detected={Screens}", screens.Length);

// Need to do some special screen dimension calculation here to accomodate multiple monitors.
// This works with horizontal, vertical and a combination of horizontal & vertical.
Expand All @@ -43,7 +42,7 @@ public CaptureMouseScreenCoordinatesWindow()

foreach (Screen screen in screens)
{
Log.Information(screen.ToString());
Log.Debug(screen.ToString());

// Find the lowest X & Y screen values, it's possible for screens to have negative
// values depending on how the multi monitor setup is configured
Expand All @@ -61,9 +60,7 @@ public CaptureMouseScreenCoordinatesWindow()
Height += screen.Bounds.Height;
}

Log.Information($"Set window size. Width: {Width}, Height: {Height}");
Log.Information($"Set window position. Left: {Left}, Top: {Top}");
Log.Information("Opened window to capture mouse coordinates.");
Log.Debug("Set window size: width={Width:F0}, height={Height:F0}. Window position: left={Left}, top={Top}", Width, Height, Left, Top);
}

#endregion
Expand All @@ -84,7 +81,7 @@ protected override void OnMouseDown(MouseButtonEventArgs e)

Point position = WPFCursor.Position;
OnCoordinatesCaptured?.Invoke(this, position);
Log.Information($"Captured mouse position: {position.X}, {position.Y}");
Log.Information("Captured mouse position: {PositionX}, {PositionY}", position.X, position.Y);
Close();
}

Expand All @@ -102,14 +99,13 @@ protected override void OnKeyDown(KeyEventArgs e)
protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
Log.Information($"Rendered window size: Width: {RenderSize.Width}, Height: {RenderSize.Height}");
Log.Information($"Rendered window position: Left:{Left}, Height: {Height}");
Log.Debug("Rendered window size: width={Width:F0}, height={Height:F0}. Window position: left={Left}, top={Top}", RenderSize.Width, RenderSize.Height, Left, Top);
}

protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
Log.Information("Closing window to capture mouse coordinates.");
Log.Information("Closing capture mouse coordinates window");
}

#endregion
Expand Down
35 changes: 16 additions & 19 deletions AutoClicker/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ protected override void OnSourceInitialized(EventArgs e)
_source = HwndSource.FromHwnd(_mainWindowHandle);
_source.AddHook(StartStopHooks);

SettingsUtils.HotKeyChangedEvent += SettingsUtils_HotKeyChangedEvent;
SettingsUtils_HotKeyChangedEvent(this, new HotkeyChangedEventArgs()
SettingsUtils.HotkeyChangedEvent += SettingsUtils_HotkeyChangedEvent;
SettingsUtils_HotkeyChangedEvent(this, new HotkeyChangedEventArgs()
{
Hotkey = SettingsUtils.CurrentSettings.HotkeySettings.StartHotkey,
Operation = Operation.Start
});
SettingsUtils_HotKeyChangedEvent(this, new HotkeyChangedEventArgs()
SettingsUtils_HotkeyChangedEvent(this, new HotkeyChangedEventArgs()
{
Hotkey = SettingsUtils.CurrentSettings.HotkeySettings.StopHotkey,
Operation = Operation.Stop
});
SettingsUtils_HotKeyChangedEvent(this, new HotkeyChangedEventArgs()
SettingsUtils_HotkeyChangedEvent(this, new HotkeyChangedEventArgs()
{
Hotkey = SettingsUtils.CurrentSettings.HotkeySettings.ToggleHotkey,
Operation = Operation.Toggle
Expand All @@ -96,11 +96,11 @@ protected override void OnClosed(EventArgs e)
{
_source.RemoveHook(StartStopHooks);

SettingsUtils.HotKeyChangedEvent -= SettingsUtils_HotKeyChangedEvent;
SettingsUtils.HotkeyChangedEvent -= SettingsUtils_HotkeyChangedEvent;

foreach (int hotkeyId in Constants.ALL_HOTKEY_IDS)
{
DeregisterHotKey(hotkeyId);
DeregisterHotkey(hotkeyId);
}

systemTrayIcon.Click -= SystemTrayIcon_Click;
Expand Down Expand Up @@ -302,7 +302,7 @@ private void ReRegisterHotkey(IEnumerable<int> hotkeyIds, KeyMapping hotkey, boo
{
foreach (int hotkeyId in hotkeyIds)
{
DeregisterHotKey(hotkeyId);
DeregisterHotkey(hotkeyId);
}
RegisterHotkey(hotkeyIds, hotkey, includeModifiers);
}
Expand All @@ -315,19 +315,19 @@ private void RegisterHotkey(IEnumerable<int> hotkeyIds, KeyMapping hotkey, bool
{
foreach ((int, int) item in hotkeyIdsToModifiers)
{
User32ApiUtils.RegisterHotKey(_mainWindowHandle, item.Item1, item.Item2, hotkey.VirtualKeyCode);
User32ApiUtils.RegisterHotkey(_mainWindowHandle, item.Item1, item.Item2, hotkey.VirtualKeyCode);
}
}
else
{
User32ApiUtils.RegisterHotKey(_mainWindowHandle, hotkeyIdsToModifiers.ElementAt(0).Item1, hotkeyIdsToModifiers.ElementAt(0).Item2, hotkey.VirtualKeyCode);
User32ApiUtils.RegisterHotkey(_mainWindowHandle, hotkeyIdsToModifiers.ElementAt(0).Item1, hotkeyIdsToModifiers.ElementAt(0).Item2, hotkey.VirtualKeyCode);
}
}

private void DeregisterHotKey(int hotkeyId)
private void DeregisterHotkey(int hotkeyId)
{
Log.Information("DeregisterHotKey with hotkeyId={HotkeyId}", hotkeyId);
if (User32ApiUtils.DeregisterHotKey(_mainWindowHandle, hotkeyId))
Log.Information("DeregisterHotkey with hotkeyId={HotkeyId}", hotkeyId);
if (User32ApiUtils.DeregisterHotkey(_mainWindowHandle, hotkeyId))
return;
Log.Debug("No hotkey registered on {HotkeyId}", hotkeyId);
}
Expand Down Expand Up @@ -377,7 +377,7 @@ private void PerformMouseClick(int mouseDownAction, int mouseUpAction, int xPos,
bool setCursorPos = User32ApiUtils.SetCursorPosition(xPos, yPos);
if (!setCursorPos)
{
Log.Error($"Could not set the mouse cursor.");
Log.Error("Failed to set the mouse cursor!");
}

User32ApiUtils.ExecuteMouseEvent(mouseDownAction | mouseUpAction, xPos, yPos, 0, 0);
Expand Down Expand Up @@ -407,9 +407,9 @@ private IntPtr StartStopHooks(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam
return IntPtr.Zero;
}

private void SettingsUtils_HotKeyChangedEvent(object sender, HotkeyChangedEventArgs e)
private void SettingsUtils_HotkeyChangedEvent(object sender, HotkeyChangedEventArgs e)
{
Log.Information("HotKeyChangedEvent with operation={Operation}, hotkey={Hotkey}, includeModifiers={IncludeModifiers}", e.Operation, e.Hotkey.DisplayName, e.IncludeModifiers);
Log.Information("HotkeyChangedEvent with operation={Operation}, hotkey={Hotkey}, includeModifiers={IncludeModifiers}", e.Operation, e.Hotkey.DisplayName, e.IncludeModifiers);
switch (e.Operation)
{
case Operation.Start:
Expand Down Expand Up @@ -477,10 +477,7 @@ private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
Exit();
}

private void RadioButtonSelectedLocationMode_CurrentLocationOnChecked(
object sender,
RoutedEventArgs e
)
private void RadioButtonSelectedLocationMode_CurrentLocationOnChecked(object sender, RoutedEventArgs e)
{
TextBoxPickedXValue.Text = string.Empty;
TextBoxPickedYValue.Text = string.Empty;
Expand Down
11 changes: 5 additions & 6 deletions AutoClicker/Views/SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using AutoClicker.Models;
Expand Down Expand Up @@ -77,15 +76,15 @@ private void SaveCommand_Execute(object sender, ExecutedRoutedEventArgs e)
{
if (SelectedStartKey != SettingsUtils.CurrentSettings.HotkeySettings.StartHotkey)
{
SettingsUtils.SetStartHotKey(SelectedStartKey);
SettingsUtils.SetStartHotkey(SelectedStartKey);
}
if (SelectedStopKey != SettingsUtils.CurrentSettings.HotkeySettings.StopHotkey)
{
SettingsUtils.SetStopHotKey(SelectedStopKey);
SettingsUtils.SetStopHotkey(SelectedStopKey);
}
if (SelectedToggleKey != SettingsUtils.CurrentSettings.HotkeySettings.ToggleHotkey)
{
SettingsUtils.SetToggleHotKey(SelectedToggleKey);
SettingsUtils.SetToggleHotkey(SelectedToggleKey);
}
if (IncludeModifiers != SettingsUtils.CurrentSettings.HotkeySettings.IncludeModifiers)
{
Expand Down Expand Up @@ -133,7 +132,7 @@ private KeyMapping GenericKeyDownHandler(KeyEventArgs e)
KeyMapping newKeyMapping = GetNewKeyMapping(e.Key);
if (newKeyMapping == null)
{
Log.Error("No Matching key for {Key}", e.Key);
Log.Error("No Matching key for {Key}!", e.Key);
return null;
}

Expand All @@ -145,7 +144,7 @@ private KeyMapping GetNewKeyMapping(Key key)
{
int virtualKeyCode = KeyInterop.VirtualKeyFromKey(key);
Log.Debug("GetNewKeyMapping with virtualKeyCode={VirtualKeyCode}", virtualKeyCode);
return KeyMapping.FirstOrDefault(keyMapping => keyMapping.VirtualKeyCode == virtualKeyCode);
return KeyMapping.Find(keyMapping => keyMapping.VirtualKeyCode == virtualKeyCode);
}

#endregion Helper Methods
Expand Down
1 change: 1 addition & 0 deletions AutoClicker/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<package id="Serilog.Sinks.Console" version="6.0.0" targetFramework="net48" />
<package id="Serilog.Sinks.File" version="6.0.0" targetFramework="net48" />
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
<package id="System.Collections.Immutable" version="8.0.0" targetFramework="net48" />
<package id="System.Diagnostics.DiagnosticSource" version="8.0.1" targetFramework="net48" />
<package id="System.Memory" version="4.5.5" targetFramework="net48" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
Expand Down

0 comments on commit 86bee27

Please sign in to comment.