70 lines
2.2 KiB
C#
70 lines
2.2 KiB
C#
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using ytLive.Services;
|
|
using ytLive.ViewModels;
|
|
|
|
namespace ytLive;
|
|
|
|
public partial class HotkeyConfigDialog : Window
|
|
{
|
|
private readonly HotkeyConfigViewModel _viewModel;
|
|
|
|
public HotkeyConfigDialog(HotkeyConfigViewModel viewModel)
|
|
{
|
|
InitializeComponent();
|
|
DataContext = viewModel;
|
|
_viewModel = viewModel;
|
|
viewModel.SaveRequested += () => DialogResult = true;
|
|
viewModel.CancelRequested += () => DialogResult = false;
|
|
}
|
|
|
|
private void ChordButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (sender is not FrameworkElement { Tag: HotkeyBindingRow row }) return;
|
|
|
|
_viewModel.StartCaptureCommand.Execute(row);
|
|
|
|
// Enter capture mode: listen for the next key combo on this specific button.
|
|
if (row.IsCapturing && sender is Button button)
|
|
{
|
|
button.PreviewKeyDown += ChordButton_PreviewKeyDown;
|
|
button.Focus();
|
|
}
|
|
}
|
|
|
|
private void ChordButton_PreviewKeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (sender is not Button button) return;
|
|
button.PreviewKeyDown -= ChordButton_PreviewKeyDown;
|
|
|
|
e.Handled = true;
|
|
|
|
// Escape cancels capture without changing the binding.
|
|
if (e.Key == Key.Escape)
|
|
{
|
|
_viewModel.CancelCapture();
|
|
return;
|
|
}
|
|
|
|
// Skip bare modifier-only presses — wait for a real key.
|
|
var key = e.Key == Key.System ? e.SystemKey : e.Key;
|
|
if (key is Key.LeftCtrl or Key.RightCtrl or Key.LeftAlt or Key.RightAlt
|
|
or Key.LeftShift or Key.RightShift or Key.LWin or Key.RWin)
|
|
{
|
|
// Re-enter capture — the button is still focused, re-subscribe.
|
|
button.PreviewKeyDown += ChordButton_PreviewKeyDown;
|
|
return;
|
|
}
|
|
|
|
var chord = HotkeyChord.FromWpf(key, Keyboard.Modifiers);
|
|
_viewModel.AssignCapturedChord(chord);
|
|
}
|
|
|
|
private void UnbindButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (sender is FrameworkElement { Tag: HotkeyBindingRow row })
|
|
_viewModel.UnbindCommand.Execute(row);
|
|
}
|
|
}
|