Skip to content

Commit

Permalink
update & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
radj307 committed Nov 4, 2023
1 parent bb92e7d commit 417701a
Show file tree
Hide file tree
Showing 22 changed files with 344 additions and 215 deletions.
10 changes: 7 additions & 3 deletions VolumeControl.Core/Input/Actions/HotkeyActionDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public HotkeyActionDefinition(Type objectType, MethodInfo methodInfo, string nam
/// <summary>
/// Gets the method info for the action method that this action represents.
/// </summary>
/// <remarks>
/// <b>Do not use this to invoke the method!</b><br/>
/// Use <see cref="HotkeyActionInstance.Invoke(object, HotkeyPressedEventArgs)"/> instead!
/// </remarks>
public MethodInfo ActionMethodInfo { get; }
#endregion Reflection

Expand Down Expand Up @@ -166,14 +170,14 @@ public HotkeyActionInstance CreateInstance(IActionSettingInstance[] actionSettin
/// </remarks>
/// <param name="parameters">Parameters to invoke the method with. These must match the parameters accepted by the actual method.</param>
/// <returns>The return value from <see cref="ActionMethodInfo"/>.</returns>
public object? Invoke_Unsafe(params object?[] parameters)
private object? Invoke_Unsafe(object?[] parameters)
=> ActionMethodInfo.Invoke(ActionGroupInstance, parameters);
/// <summary>
/// Directly invokes the method specified by <see cref="ActionMethodInfo"/> with the parameters expected by <see cref="HotkeyPressedEventHandler"/>.
/// </summary>
/// <inheritdoc cref="Invoke_Unsafe(object?[])"/>
public object? Invoke_Unsafe(object? sender, IActionSettingInstance[] actionSettings)
=> Invoke_Unsafe(sender, new HotkeyPressedEventArgs(actionSettings));
internal object? Invoke_Unsafe(object sender, HotkeyPressedEventArgs e)
=> Invoke_Unsafe(new[] { sender, e });
#endregion Invoke_Unsafe

#region GetActionSettingDefinition
Expand Down
4 changes: 2 additions & 2 deletions VolumeControl.Core/Input/Actions/HotkeyActionInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal HotkeyActionInstance(HotkeyActionDefinition definition, IActionSettingI
/// </summary>
/// <param name="sender">The object instance to use as the sender of the event.</param>
/// <param name="e">The event arguments to use for the event.</param>
public void Invoke(object? sender, HotkeyPressedEventArgs e)
public void Invoke(object sender, HotkeyPressedEventArgs e)
{
try
{
Expand All @@ -64,7 +64,7 @@ public void Invoke(object? sender, HotkeyPressedEventArgs e)
/// Invokes the method specified by the HotkeyActionDefinition with a new <see cref="HotkeyPressedEventArgs"/> instance.
/// </summary>
/// <param name="sender">The object instance to use as the sender of the event.</param>
public void Invoke(object? sender) => Invoke(sender, new HotkeyPressedEventArgs(ActionSettings));
public void Invoke(object sender) => Invoke(sender, new HotkeyPressedEventArgs(ActionSettings));
#endregion Methods
}
}
2 changes: 1 addition & 1 deletion VolumeControl.CoreAudio/AudioDeviceSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public AudioDeviceSelector(AudioDeviceManager audioDeviceManager)
#endregion Fields

#region Properties
private static Config Settings => (Config.Default as Config)!;
private static Config Settings => Config.Default!;
AudioDeviceManager AudioDeviceManager { get; }
/// <summary>
/// Gets or the sets the selected item.
Expand Down
2 changes: 1 addition & 1 deletion VolumeControl.CoreAudio/AudioSessionSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public AudioSessionSelector(AudioSessionManager audioSessionManager)
#endregion Fields

#region Properties
private static Config Settings => (Config.Default as Config)!;
private static Config Settings => Config.Default!;
AudioSessionManager AudioSessionManager { get; }
/// <summary>
/// Gets or the sets the selected item.
Expand Down
1 change: 0 additions & 1 deletion VolumeControl.HotkeyActions/SystemActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Text;
using System.Windows;
using VolumeControl.Core.Attributes;
using VolumeControl.Core.Helpers;
using VolumeControl.Core.Input;
using VolumeControl.Log;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@
<ProjectReference Include="..\VolumeControl.CoreAudio\VolumeControl.CoreAudio.csproj" />
<ProjectReference Include="..\VolumeControl.Core\VolumeControl.Core.csproj" />
<ProjectReference Include="..\VolumeControl.SDK\VolumeControl.SDK.csproj" />
<ProjectReference Include="..\VolumeControl.TypeExtensions\VolumeControl.TypeExtensions.csproj" />
</ItemGroup>
</Project>
1 change: 0 additions & 1 deletion VolumeControl.Log/Helpers/ExceptionMessageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace VolumeControl.Log.Helpers
Expand Down
26 changes: 4 additions & 22 deletions VolumeControl.SDK/Delegates/HotkeyActionDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
using System.ComponentModel;
using VolumeControl.Core.Input;

namespace VolumeControl.SDK.Delegates
{
/// <summary>
/// Represents a method that can be used by Volume Control to generate a hotkey action.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void HotkeyActionDelegate(object? sender, HandledEventArgs e);
/// <inheritdoc cref="HotkeyActionDelegate"/>
public delegate void HotkeyActionDelegate<T1>(object? sender, HandledEventArgs e, T1 opt1);
/// <inheritdoc cref="HotkeyActionDelegate"/>
public delegate void HotkeyActionDelegate<T1, T2>(object? sender, HandledEventArgs e, T1 opt1, T2 opt2);
/// <inheritdoc cref="HotkeyActionDelegate"/>
public delegate void HotkeyActionDelegate<T1, T2, T3>(object? sender, HandledEventArgs e, T1 opt1, T2 opt2, T3 opt3);
/// <inheritdoc cref="HotkeyActionDelegate"/>
public delegate void HotkeyActionDelegate<T1, T2, T3, T4>(object? sender, HandledEventArgs e, T1 opt1, T2 opt2, T3 opt3, T4 opt4);
/// <inheritdoc cref="HotkeyActionDelegate"/>
public delegate void HotkeyActionDelegate<T1, T2, T3, T4, T5>(object? sender, HandledEventArgs e, T1 opt1, T2 opt2, T3 opt3, T4 opt4, T5 opt5);
/// <inheritdoc cref="HotkeyActionDelegate"/>
public delegate void HotkeyActionDelegate<T1, T2, T3, T4, T5, T6>(object? sender, HandledEventArgs e, T1 opt1, T2 opt2, T3 opt3, T4 opt4, T5 opt5, T6 opt6);
/// <inheritdoc cref="HotkeyActionDelegate"/>
public delegate void HotkeyActionDelegate<T1, T2, T3, T4, T5, T6, T7>(object? sender, HandledEventArgs e, T1 opt1, T2 opt2, T3 opt3, T4 opt4, T5 opt5, T6 opt6, T7 opt7);
/// <inheritdoc cref="HotkeyActionDelegate"/>
public delegate void HotkeyActionDelegate<T1, T2, T3, T4, T5, T6, T7, T8>(object? sender, HandledEventArgs e, T1 opt1, T2 opt2, T3 opt3, T4 opt4, T5 opt5, T6 opt6, T7 opt7, T8 opt8);
/// <inheritdoc cref="HotkeyActionDelegate"/>
public delegate void HotkeyActionDelegate<T1, T2, T3, T4, T5, T6, T7, T8, T9>(object? sender, HandledEventArgs e, T1 opt1, T2 opt2, T3 opt3, T4 opt4, T5 opt5, T6 opt6, T7 opt7, T8 opt8, T9 opt9);
/// <param name="sender">The hotkey that was pressed.</param>
/// <param name="e">The event arguments for this action, including the current values of any action settings.</param>
public delegate void HotkeyActionDelegate(object sender, HotkeyPressedEventArgs e);
}
2 changes: 1 addition & 1 deletion VolumeControl.TypeExtensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static T GetSingleValue<T>(this IEnumerable<T> enumerable) where T : stru
public static bool IsSingleValue<T>(this T e) where T : struct, Enum
{
var e_v = Convert.ToInt64(e);

return e_v == 0 || (e_v & (e_v - 1)) == 0;
}
}
Expand Down
11 changes: 10 additions & 1 deletion VolumeControl.TypeExtensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ public static class StringExtensions
/// <summary>
/// Parses a string containing a version number in semantic versioning 2 format.
/// </summary>
public static SemVersion? GetSemVer(this string? s) => s is null ? null : SemVersion.TryParse(s.Trim(), SemVersionStyles.OptionalPatch, out SemVersion result) ? result : null;
public static SemVersion? GetSemVer(this string? s)
{
if (string.IsNullOrWhiteSpace(s)) return null;

if (SemVersion.TryParse(s.Trim(), SemVersionStyles.OptionalPatch, out SemVersion result))
{
return result;
}
return null;
}
/// <summary>
/// Removes all chars that <paramref name="pred"/> returns true for.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion VolumeControl/ActionSettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ public ActionSettingsWindow(Window owner, HotkeyVM hotkey)

// init window properties
Owner = owner;
Title = hotkey.Hotkey.Name;
var hotkeyName = hotkey.Hotkey.Name;
var hasName = !string.IsNullOrWhiteSpace(hotkeyName);
var actionIdentifier = hotkey.ActionDefinition!.Identifier;
if (hasName)
{
Title = hotkeyName + $" ({actionIdentifier})";
}
else Title = actionIdentifier;
}
#endregion Initializers

Expand Down
96 changes: 28 additions & 68 deletions VolumeControl/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
Expand All @@ -25,38 +23,43 @@ public App()
string version = $"v{assembly?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion}";

// Add a log handler to the dispatcher's unhandled exception event
DispatcherUnhandledException += (s, e) =>
{
FLog.Error($"An unhandled exception occurred!", $"Sender: '{s}' ({s.GetType()})", e.Exception);
e.Handled = true;
};
DispatcherUnhandledException += this.App_DispatcherUnhandledException;

// setup the tray icon
TrayIcon = new(() => this.MainWindow.Visibility == Visibility.Visible)
{
Tooltip = $"Volume Control {version}"
};
TrayIcon.DoubleClick += this.HandleTrayIconClick;
TrayIcon.ShowClicked += this.HandleTrayIconClick;
TrayIcon.DoubleClick += (s, e) =>
{
if (this.MainWindow.IsVisible)
this.HideMainWindow();
else
{
this.ShowMainWindow();
MainWindow.Activate();
}
};
TrayIcon.ShowClicked += (s, e) => this.ShowMainWindow();
TrayIcon.HideClicked += (s, e) => this.HideMainWindow();
TrayIcon.BringToFrontClicked += (s, e) => this.ActivateMainWindow();

TrayIcon.OpenConfigClicked += (s, e) =>
{
Process.Start(new ProcessStartInfo(Config.Default.Location) { UseShellExecute = true });
ShellHelper.OpenWithDefault(Config.Default.Location);
};
TrayIcon.OpenLogClicked += (s, e) =>
{
Process.Start(new ProcessStartInfo(Config.Default.LogPath) { UseShellExecute = true });
ShellHelper.OpenWithDefault(Config.Default.LogPath);
};

TrayIcon.OpenLocationClicked += (s, e) =>
{
OpenFolderAndSelectItem(Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), Path.ChangeExtension(AppDomain.CurrentDomain.FriendlyName, ".exe"))));
ShellHelper.OpenFolderAndSelectItem(Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), Path.ChangeExtension(AppDomain.CurrentDomain.FriendlyName, ".exe"))));
};
TrayIcon.OpenAppDataClicked += (s, e) =>
{
Process.Start("explorer", PathFinder.ApplicationAppDataPath);
ShellHelper.OpenWith("explorer", PathFinder.ApplicationAppDataPath);
};
TrayIcon.CloseClicked += (s, e) => this.Shutdown();
TrayIcon.Visible = true;
Expand All @@ -79,61 +82,6 @@ private void ActivateMainWindow()
this.MainWindow.Show();
_ = this.MainWindow.Activate();
}
private void HandleTrayIconClick(object? sender, EventArgs e)
{
if (this.MainWindow.IsVisible)
this.HideMainWindow();
else
{
this.ShowMainWindow();
MainWindow.Activate();
}
}
#region OpenFolderAndSelectItem
[DllImport("shell32.dll", SetLastError = true)]
private static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, uint cidl, [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl, uint dwFlags);
[DllImport("shell32.dll", SetLastError = true)]
private static extern void SHParseDisplayName([MarshalAs(UnmanagedType.LPWStr)] string name, IntPtr bindingContext, [Out] out IntPtr pidl, uint sfgaoIn, [Out] out uint psfgaoOut);
private static void OpenFolderAndSelectItem(string filePath)
{
if (Path.GetDirectoryName(filePath) is not string directoryPath)
{
FLog.Error($"Cannot get directory from path '{filePath}'!");
return;
}

SHParseDisplayName(directoryPath, IntPtr.Zero, out IntPtr nativeFolder, 0, out uint psfgaoOut);

if (nativeFolder == IntPtr.Zero)
{
FLog.Error($"Cannot locate directory '{directoryPath}'!");
return;
}

SHParseDisplayName(filePath, IntPtr.Zero, out IntPtr nativeFile, 0, out psfgaoOut);

IntPtr[] fileArray;
if (nativeFile == IntPtr.Zero)
{
// Open the folder without the file selected if we can't find the file
fileArray = Array.Empty<IntPtr>();
}
else
{
fileArray = new IntPtr[] { nativeFile };
}

FLog.Debug($"Opening and selecting '{filePath}' in the file explorer.");

_ = SHOpenFolderAndSelectItems(nativeFolder, (uint)fileArray.Length, fileArray, 0);

Marshal.FreeCoTaskMem(nativeFolder);
if (nativeFile != IntPtr.Zero)
{
Marshal.FreeCoTaskMem(nativeFile);
}
}
#endregion OpenFolderAndSelectItem
#endregion Methods

#region EventHandlers
Expand All @@ -147,6 +95,18 @@ private void Application_Exit(object sender, ExitEventArgs e)
}
#endregion Application

#region App
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
FLog.Critical(
$"An unhandled exception occurred in the application dispatcher!",
$"Sender: \"{sender}\" ({sender.GetType()})",
$"Thread: \"{e.Dispatcher.Thread.Name}\"",
e.Exception);
e.Handled = true;
}
#endregion App

#region PART_TextBox
/// <summary>
/// Prevents non-numeric or control keys from being received as input by the textbox.<br/>
Expand Down
5 changes: 2 additions & 3 deletions VolumeControl/Controls/VolumeControlNotifyIcon.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CodingSeb.Localization;
using System;
using System.Collections.Generic;

namespace VolumeControl.Controls
{
Expand All @@ -12,7 +11,7 @@ public VolumeControlNotifyIcon(Query queryMainWindowVisible)

_notifyIcon.Icon = Properties.Resources.iconSilvered;

this.Items.AddRange(new List<System.Windows.Forms.ToolStripItem>()
this.Items.AddRange(new System.Windows.Forms.ToolStripItem[]
{ // TOOLSTRIP:
new System.Windows.Forms.ToolStripButton(GetShowText(), Properties.Resources.foreground, this.HandleShowHideClick),
new System.Windows.Forms.ToolStripButton(GetBringToFrontText(), Properties.Resources.bringtofront, this.HandleBringToFrontClick),
Expand All @@ -25,7 +24,7 @@ public VolumeControlNotifyIcon(Query queryMainWindowVisible)
new System.Windows.Forms.ToolStripButton(GetOpenAppDataText(), Properties.Resources.file_inverted, this.HandleOpenAppDataClick),
new System.Windows.Forms.ToolStripButton(GetOpenLocationText(), Properties.Resources.file, this.HandleOpenLocationClick),
new System.Windows.Forms.ToolStripButton(GetCloseText(), Properties.Resources.X, this.HandleCloseClicked),
}.ToArray());
});

Loc.Instance.CurrentLanguageChanged += this.Handle_CurrentLanguageChanged;
_contextMenuStrip.VisibleChanged += this.Handle_ContextMenuVisibilityChanged;
Expand Down
Loading

0 comments on commit 417701a

Please sign in to comment.