forked from Imulion/MHFZ_Overlay
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
354 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.