From d25af927216c2561efe8ff352f26d4234d720eb7 Mon Sep 17 00:00:00 2001 From: Doriel Rivalet <100863878+DorielRivalet@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:37:35 -0300 Subject: [PATCH] feat: add hotkeys window closes #375 --- MHFZ_Overlay/App.config | 9 ++ MHFZ_Overlay/MHFZ_Overlay.csproj | 36 +++---- .../Services/Hotkey/HotkeySettings.cs | 94 ++++++++++++++++++ MHFZ_Overlay/Settings.Designer.cs | 36 +++++++ MHFZ_Overlay/Settings.settings | 9 ++ .../Views/Windows/HotkeySettingsWindow.xaml | 53 ++++++++++ .../Windows/HotkeySettingsWindow.xaml.cs | 98 +++++++++++++++++++ MHFZ_Overlay/Views/Windows/MainWindow.xaml | 5 + MHFZ_Overlay/Views/Windows/MainWindow.xaml.cs | 36 ++++++- README.md | 2 +- 10 files changed, 354 insertions(+), 24 deletions(-) create mode 100644 MHFZ_Overlay/Services/Hotkey/HotkeySettings.cs create mode 100644 MHFZ_Overlay/Views/Windows/HotkeySettingsWindow.xaml create mode 100644 MHFZ_Overlay/Views/Windows/HotkeySettingsWindow.xaml.cs diff --git a/MHFZ_Overlay/App.config b/MHFZ_Overlay/App.config index 51b382b6..76e0a0c0 100644 --- a/MHFZ_Overlay/App.config +++ b/MHFZ_Overlay/App.config @@ -1044,6 +1044,15 @@ True + + Shift + F1 + + + Shift + F5 + + + Shift + F6 + diff --git a/MHFZ_Overlay/MHFZ_Overlay.csproj b/MHFZ_Overlay/MHFZ_Overlay.csproj index 3fb4ff83..f2c69f46 100644 --- a/MHFZ_Overlay/MHFZ_Overlay.csproj +++ b/MHFZ_Overlay/MHFZ_Overlay.csproj @@ -2001,44 +2001,44 @@ - + - + - + - - - + + + - - - + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - + + + + + + - + diff --git a/MHFZ_Overlay/Services/Hotkey/HotkeySettings.cs b/MHFZ_Overlay/Services/Hotkey/HotkeySettings.cs new file mode 100644 index 00000000..8b029615 --- /dev/null +++ b/MHFZ_Overlay/Services/Hotkey/HotkeySettings.cs @@ -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 _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(); + } +} diff --git a/MHFZ_Overlay/Settings.Designer.cs b/MHFZ_Overlay/Settings.Designer.cs index 2657a844..d2dbaa39 100644 --- a/MHFZ_Overlay/Settings.Designer.cs +++ b/MHFZ_Overlay/Settings.Designer.cs @@ -4162,5 +4162,41 @@ public bool EnableAchievementsTracking { this["EnableAchievementsTracking"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("Shift + F1")] + public string OpenSettingsHotkey { + get { + return ((string)(this["OpenSettingsHotkey"])); + } + set { + this["OpenSettingsHotkey"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("Shift + F5")] + public string RestartProgramHotkey { + get { + return ((string)(this["RestartProgramHotkey"])); + } + set { + this["RestartProgramHotkey"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("Shift + F6")] + public string CloseProgramHotkey { + get { + return ((string)(this["CloseProgramHotkey"])); + } + set { + this["CloseProgramHotkey"] = value; + } + } } } diff --git a/MHFZ_Overlay/Settings.settings b/MHFZ_Overlay/Settings.settings index 17bf6b45..3d979bad 100644 --- a/MHFZ_Overlay/Settings.settings +++ b/MHFZ_Overlay/Settings.settings @@ -1037,5 +1037,14 @@ True + + Shift + F1 + + + Shift + F5 + + + Shift + F6 + \ No newline at end of file diff --git a/MHFZ_Overlay/Views/Windows/HotkeySettingsWindow.xaml b/MHFZ_Overlay/Views/Windows/HotkeySettingsWindow.xaml new file mode 100644 index 00000000..e1660d73 --- /dev/null +++ b/MHFZ_Overlay/Views/Windows/HotkeySettingsWindow.xaml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + Press hotkey combination to set. Press Escape to clear. Each hotkey must include at least one modifier (Ctrl, Alt, Shift, or Win). + + + +