Skip to content

Commit

Permalink
feat: add hotkeys window
Browse files Browse the repository at this point in the history
closes #375
  • Loading branch information
DorielRivalet committed Oct 18, 2024
1 parent eb66abe commit d25af92
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 24 deletions.
9 changes: 9 additions & 0 deletions MHFZ_Overlay/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,15 @@
<setting name="EnableAchievementsTracking" serializeAs="String">
<value>True</value>
</setting>
<setting name="OpenSettingsHotkey" serializeAs="String">
<value>Shift + F1</value>
</setting>
<setting name="RestartProgramHotkey" serializeAs="String">
<value>Shift + F5</value>
</setting>
<setting name="CloseProgramHotkey" serializeAs="String">
<value>Shift + F6</value>
</setting>
</MHFZ_Overlay.Settings>
</userSettings>
</configuration>
36 changes: 18 additions & 18 deletions MHFZ_Overlay/MHFZ_Overlay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2001,44 +2001,44 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2" />
<PackageReference Include="CompiledBindings.WPF" Version="1.0.18" />
<PackageReference Include="CsvHelper" Version="32.0.3" />
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="DiscordRichPresence" Version="1.2.1.24" />
<PackageReference Include="DotNetProjects.Extended.Wpf.Toolkit" Version="5.0.115" />
<PackageReference Include="EZlion" Version="8.0.0" />
<PackageReference Include="FontAwesome.Sharp" Version="6.3.0" />
<PackageReference Include="FontAwesome.Sharp" Version="6.6.0" />
<PackageReference Include="LiveChartsCore.SkiaSharpView.WPF" Version="2.0.0-beta.920" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />
<PackageReference Include="Memory.dll.x86" Version="1.2.27" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="8.0.0" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2535.41" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="8.0.2" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2792.45" />
<PackageReference Include="MouseKeyHook" Version="5.7.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="NLog" Version="5.3.2" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.11" />
<PackageReference Include="NuGet.CommandLine" Version="6.10.0">
<PackageReference Include="NLog" Version="5.3.4" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.14" />
<PackageReference Include="NuGet.CommandLine" Version="6.11.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Octokit" Version="12.0.0" />
<PackageReference Include="RESTCountries.NET" Version="3.1.0" />
<PackageReference Include="Octokit" Version="13.0.1" />
<PackageReference Include="RESTCountries.NET" Version="3.2.0" />
<PackageReference Include="ShowMeTheXAML.MSBuild" Version="2.0.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.118" />
<PackageReference Include="System.Drawing.Common" Version="8.0.6" />
<PackageReference Include="Velopack" Version="0.0.359" />
<PackageReference Include="VirtualizingWrapPanel" Version="2.0.6" />
<PackageReference Include="WPF-UI" Version="3.0.4" />
<PackageReference Include="WPF-UI.Tray" Version="3.0.4" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
<PackageReference Include="System.Drawing.Common" Version="8.0.10" />
<PackageReference Include="Velopack" Version="0.0.626" />
<PackageReference Include="VirtualizingWrapPanel" Version="2.0.11" />
<PackageReference Include="WPF-UI" Version="3.0.5" />
<PackageReference Include="WPF-UI.Tray" Version="3.0.5" />
<PackageReference Include="WpfAnimatedGif" Version="2.0.2" />
<PackageReference Include="XamlAnimatedGif" Version="2.3.0" />
<PackageReference Include="XInputium" Version="1.2.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit" Version="2.9.2" />
</ItemGroup>

<ItemGroup>
Expand Down
94 changes: 94 additions & 0 deletions MHFZ_Overlay/Services/Hotkey/HotkeySettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
namespace MHFZ_Overlay.Services.Hotkey;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// HotkeySettings.cs
public class HotkeySettings
{
public string OpenSettings { get; set; } = "Shift + F1";
public string RestartProgram { get; set; } = "Shift + F5";
public string CloseProgram { get; set; } = "Shift + F6";
}

public class HotkeyManager : IDisposable
{
private readonly Dictionary<string, Action> _hotkeyActions = new();
private HotkeySettings _settings;
private readonly GlobalHotKey _globalHotKey;

public HotkeyManager()
{
_globalHotKey = new GlobalHotKey();
LoadSettings();
}


public void LoadSettings()
{
var s = (Settings)System.Windows.Application.Current.TryFindResource("Settings");

// Load from user settings or create default
_settings = new HotkeySettings
{
OpenSettings = s.OpenSettingsHotkey ?? "Shift + F1",
RestartProgram = s.RestartProgramHotkey ?? "Shift + F5",
CloseProgram = s.CloseProgramHotkey ?? "Shift + F6"
};
}

public void SaveSettings()
{
var s = (Settings)System.Windows.Application.Current.TryFindResource("Settings");

s.OpenSettingsHotkey = _settings.OpenSettings;
s.RestartProgramHotkey = _settings.RestartProgram;
s.CloseProgramHotkey = _settings.CloseProgram;
s.Save();
}

public void RegisterHotkeys(Action openSettings, Action restart, Action close)
{
// Store the actions
_hotkeyActions["OpenSettings"] = openSettings;
_hotkeyActions["RestartProgram"] = restart;
_hotkeyActions["CloseProgram"] = close;

// Register hotkeys using existing GlobalHotKey class
GlobalHotKey.RegisterHotKey(_settings.OpenSettings, openSettings);
GlobalHotKey.RegisterHotKey(_settings.RestartProgram, restart);
GlobalHotKey.RegisterHotKey(_settings.CloseProgram, close);
}

public void UpdateHotkey(string action, string newHotkey)
{
// Update the settings
switch (action)
{
case "OpenSettings":
_settings.OpenSettings = newHotkey;
break;
case "RestartProgram":
_settings.RestartProgram = newHotkey;
break;
case "CloseProgram":
_settings.CloseProgram = newHotkey;
break;
}

SaveSettings();

// Re-register the hotkey if we have an action for it
if (_hotkeyActions.TryGetValue(action, out var hotkeyAction))
{
GlobalHotKey.RegisterHotKey(newHotkey, hotkeyAction);
}
}

public void Dispose()
{
_globalHotKey?.Dispose();
}
}
36 changes: 36 additions & 0 deletions MHFZ_Overlay/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions MHFZ_Overlay/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -1037,5 +1037,14 @@
<Setting Name="EnableAchievementsTracking" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="OpenSettingsHotkey" Type="System.String" Scope="User">
<Value Profile="(Default)">Shift + F1</Value>
</Setting>
<Setting Name="RestartProgramHotkey" Type="System.String" Scope="User">
<Value Profile="(Default)">Shift + F5</Value>
</Setting>
<Setting Name="CloseProgramHotkey" Type="System.String" Scope="User">
<Value Profile="(Default)">Shift + F6</Value>
</Setting>
</Settings>
</SettingsFile>
53 changes: 53 additions & 0 deletions MHFZ_Overlay/Views/Windows/HotkeySettingsWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<ui:FluentWindow x:Class="MHFZ_Overlay.Views.Windows.HotkeySettingsWindow"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
BorderBrush="#b4befe"
ExtendsContentIntoTitleBar="True"
WindowBackdropType="Mica"
WindowCornerPreference="Round"
WindowStartupLocation="CenterScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" FontFamily="{StaticResource Segoe UI Variable}" Height="300" Width="400" Background="{StaticResource Base}" Foreground="{StaticResource Text}">
<Grid Margin="10">
<StackPanel>
<GroupBox Header="Configure Hotkeys" BorderBrush="{StaticResource PaletteBlueBrush}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<TextBox x:Name="testTextBox"/>

<Label Grid.Row="0" Grid.Column="0" Content="Open Settings:"/>
<TextBox x:Name="OpenSettingsHotkey" Grid.Row="0" Grid.Column="1"
PreviewKeyDown="Hotkey_PreviewKeyDown" Tag="OpenSettings"/>

<Label Grid.Row="1" Grid.Column="0" Content="Restart Program:"/>
<TextBox x:Name="RestartProgramHotkey" Grid.Row="1" Grid.Column="1"
PreviewKeyDown="Hotkey_PreviewKeyDown" Tag="RestartProgram"/>

<Label Grid.Row="2" Grid.Column="0" Content="Close Program:"/>
<TextBox x:Name="CloseProgramHotkey" Grid.Row="2" Grid.Column="1"
PreviewKeyDown="Hotkey_PreviewKeyDown" Tag="CloseProgram"/>
</Grid>
</GroupBox>

<TextBlock Margin="0,10,0,0" TextWrapping="Wrap" Foreground="Gray">
Press hotkey combination to set. Press Escape to clear. Each hotkey must include at least one modifier (Ctrl, Alt, Shift, or Win).
</TextBlock>

<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0">
<Button Content="Save" Width="75" Click="SaveButton_Click" Margin="0,0,10,0"/>
<Button Content="Cancel" Width="75" Click="CancelButton_Click"/>
</StackPanel>
</StackPanel>
</Grid>
</ui:FluentWindow>
98 changes: 98 additions & 0 deletions MHFZ_Overlay/Views/Windows/HotkeySettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace MHFZ_Overlay.Views.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MHFZ_Overlay.Services.Hotkey;
using Wpf.Ui.Controls;

// HotkeySettingsWindow.xaml.cs
public partial class HotkeySettingsWindow : FluentWindow
{
private readonly HotkeyManager _hotkeyManager;
private readonly Dictionary<System.Windows.Controls.TextBox, string> _originalHotkeys = new();

public HotkeySettingsWindow(HotkeyManager hotkeyManager)
{
this.InitializeComponent();
_hotkeyManager = hotkeyManager;
LoadCurrentHotkeys();
}

private void LoadCurrentHotkeys()
{
var s = (Settings)System.Windows.Application.Current.TryFindResource("Settings");

this.OpenSettingsHotkey.Text = s.OpenSettingsHotkey;
this.RestartProgramHotkey.Text = s.RestartProgramHotkey;
this.CloseProgramHotkey.Text = s.CloseProgramHotkey;

// Store original values for cancellation
_originalHotkeys[this.OpenSettingsHotkey] = s.OpenSettingsHotkey;
_originalHotkeys[this.RestartProgramHotkey] = s.RestartProgramHotkey;
_originalHotkeys[this.CloseProgramHotkey] = s.CloseProgramHotkey;
}

private void Hotkey_PreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
var textBox = (System.Windows.Controls.TextBox)sender;

var key = e.Key == Key.System ? e.SystemKey : e.Key;
if (key == Key.Escape)
{
textBox.Text = _originalHotkeys[textBox];
return;
}

var modifiers = new List<string>();
if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
modifiers.Add("Ctrl");
if (Keyboard.Modifiers.HasFlag(ModifierKeys.Shift))
modifiers.Add("Shift");
if (Keyboard.Modifiers.HasFlag(ModifierKeys.Alt))
modifiers.Add("Alt");
if (Keyboard.Modifiers.HasFlag(ModifierKeys.Windows))
modifiers.Add("Win");

if (key != Key.LeftCtrl && key != Key.RightCtrl &&
key != Key.LeftAlt && key != Key.RightAlt &&
key != Key.LeftShift && key != Key.RightShift &&
key != Key.LWin && key != Key.RWin)
{
if (modifiers.Count == 0)
{
System.Windows.MessageBox.Show("Hotkey must include at least one modifier (Ctrl, Alt, Shift, or Win)",
"Invalid Hotkey", System.Windows.MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}

var hotkeyString = string.Join(" + ", modifiers.Concat(new[] { key.ToString() }));
textBox.Text = hotkeyString;
}
}

private void SaveButton_Click(object sender, RoutedEventArgs e)
{
_hotkeyManager.UpdateHotkey("OpenSettings", this.OpenSettingsHotkey.Text);
_hotkeyManager.UpdateHotkey("RestartProgram", this.RestartProgramHotkey.Text);
_hotkeyManager.UpdateHotkey("CloseProgram", this.CloseProgramHotkey.Text);
DialogResult = true;
Close();
}

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
}
5 changes: 5 additions & 0 deletions MHFZ_Overlay/Views/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
x:Name="MainWindowNotifyIcon">
<tray:NotifyIcon.Menu>
<ContextMenu>
<ui:MenuItem ToolTip="Open the hotkeys window" Foreground="{StaticResource Text}" Header="Hotkeys" Tag="hotkeys" Click="OptionHotkeys_Click">
<ui:MenuItem.Icon>
<ui:SymbolIcon Symbol="Keyboard24" Foreground="{StaticResource Text}"/>
</ui:MenuItem.Icon>
</ui:MenuItem>
<ui:MenuItem ToolTip="Open the configuration window" Foreground="{StaticResource Green}" Header="Settings" Tag="settings" Click="OptionSettings_Click">
<ui:MenuItem.Icon>
<ui:SymbolIcon Symbol="Settings24" Foreground="{StaticResource Green}"/>
Expand Down
Loading

0 comments on commit d25af92

Please sign in to comment.