262 lines
7.0 KiB
C#
262 lines
7.0 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using MaxSlurper.Models;
|
|
using MaxSlurper.Services;
|
|
using Microsoft.Win32;
|
|
using Wpf.Ui.Controls;
|
|
|
|
namespace MaxSlurper.Controls
|
|
{
|
|
public partial class SettingsWindow : FluentWindow, INotifyPropertyChanged
|
|
{
|
|
private readonly AppSettings _settings;
|
|
private readonly ISettingsService _settingsService;
|
|
private readonly Action<AppSettings> _onSettingsChanged;
|
|
private bool _isCapturingHotkey = false;
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
private string _currentHotkey = "";
|
|
public string CurrentHotkey
|
|
{
|
|
get => _currentHotkey;
|
|
set
|
|
{
|
|
_currentHotkey = value;
|
|
OnPropertyChanged();
|
|
}
|
|
}
|
|
|
|
private const string RegistryRunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
|
|
private const string AppName = "MaxSlurper";
|
|
|
|
public bool IsAutostart
|
|
{
|
|
get
|
|
{
|
|
using var key = Registry.CurrentUser.OpenSubKey(RegistryRunKey, false);
|
|
return key?.GetValue(AppName) != null;
|
|
}
|
|
set
|
|
{
|
|
using var key = Registry.CurrentUser.OpenSubKey(RegistryRunKey, true);
|
|
if (key == null) return;
|
|
|
|
if (value)
|
|
{
|
|
var exePath = Environment.ProcessPath ?? System.Reflection.Assembly.GetEntryAssembly()!.Location;
|
|
key.SetValue(AppName, $"\"{exePath}\" --minimized");
|
|
}
|
|
else
|
|
{
|
|
key.DeleteValue(AppName, false);
|
|
}
|
|
|
|
OnPropertyChanged();
|
|
}
|
|
}
|
|
|
|
public SettingsWindow(AppSettings settings, ISettingsService settingsService, Action<AppSettings> onSettingsChanged)
|
|
{
|
|
InitializeComponent();
|
|
DataContext = this;
|
|
|
|
_settings = settings;
|
|
_settingsService = settingsService;
|
|
_onSettingsChanged = onSettingsChanged;
|
|
|
|
UpdateHotkeyDisplay();
|
|
}
|
|
|
|
private void UpdateHotkeyDisplay()
|
|
{
|
|
CurrentHotkey = _settings.HotkeyDisplayText;
|
|
}
|
|
|
|
private void ChangeHotkey_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (_isCapturingHotkey)
|
|
return;
|
|
|
|
_isCapturingHotkey = true;
|
|
CurrentHotkey = "Tastenkombination eingeben...";
|
|
|
|
var captureWindow = new HotkeyCapture();
|
|
captureWindow.Owner = this;
|
|
|
|
if (captureWindow.ShowDialog() == true)
|
|
{
|
|
_settings.HotkeyModifiers = captureWindow.CapturedModifiers;
|
|
_settings.HotkeyKey = captureWindow.CapturedKey;
|
|
|
|
UpdateHotkeyDisplay();
|
|
SaveSettings();
|
|
}
|
|
else
|
|
{
|
|
UpdateHotkeyDisplay();
|
|
}
|
|
|
|
_isCapturingHotkey = false;
|
|
}
|
|
|
|
private void SaveSettings()
|
|
{
|
|
_settingsService.SaveSettings(_settings);
|
|
_onSettingsChanged(_settings);
|
|
}
|
|
|
|
private void Close_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
Close();
|
|
}
|
|
|
|
protected void OnPropertyChanged([CallerMemberName] string? name = null) =>
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
|
}
|
|
|
|
// Hotkey capture dialog
|
|
public partial class HotkeyCapture : FluentWindow
|
|
{
|
|
public ModifierKeys CapturedModifiers { get; private set; } = ModifierKeys.None;
|
|
public Key CapturedKey { get; private set; } = Key.None;
|
|
|
|
public HotkeyCapture()
|
|
{
|
|
Title = "Tastenkombination aufnehmen";
|
|
Width = 400;
|
|
Height = 200;
|
|
WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
|
ResizeMode = ResizeMode.NoResize;
|
|
ExtendsContentIntoTitleBar = true;
|
|
WindowBackdropType = WindowBackdropType.Mica;
|
|
WindowCornerPreference = WindowCornerPreference.Round;
|
|
|
|
var grid = new Grid();
|
|
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
|
|
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
|
|
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
|
|
|
|
var titleBar = new TitleBar
|
|
{
|
|
Title = "Tastenkombination",
|
|
ShowMaximize = false,
|
|
ShowMinimize = false
|
|
};
|
|
Grid.SetRow(titleBar, 0);
|
|
grid.Children.Add(titleBar);
|
|
|
|
var stackPanel = new StackPanel
|
|
{
|
|
Margin = new Thickness(24),
|
|
VerticalAlignment = VerticalAlignment.Center,
|
|
HorizontalAlignment = System.Windows.HorizontalAlignment.Center
|
|
};
|
|
Grid.SetRow(stackPanel, 1);
|
|
|
|
var infoText = new System.Windows.Controls.TextBlock
|
|
{
|
|
Text = "Drücken Sie die gewünschte Tastenkombination",
|
|
FontSize = 14,
|
|
TextAlignment = TextAlignment.Center,
|
|
Foreground = (System.Windows.Media.Brush)FindResource("TextFillColorSecondaryBrush"),
|
|
Margin = new Thickness(0, 0, 0, 16),
|
|
TextWrapping = TextWrapping.Wrap
|
|
};
|
|
stackPanel.Children.Add(infoText);
|
|
|
|
var keyDisplay = new System.Windows.Controls.TextBlock
|
|
{
|
|
Text = "Warte auf Eingabe...",
|
|
FontSize = 18,
|
|
FontWeight = FontWeights.SemiBold,
|
|
TextAlignment = TextAlignment.Center,
|
|
Foreground = (System.Windows.Media.Brush)FindResource("TextFillColorPrimaryBrush")
|
|
};
|
|
stackPanel.Children.Add(keyDisplay);
|
|
|
|
grid.Children.Add(stackPanel);
|
|
|
|
var buttonPanel = new StackPanel
|
|
{
|
|
Orientation = System.Windows.Controls.Orientation.Horizontal,
|
|
HorizontalAlignment = System.Windows.HorizontalAlignment.Right,
|
|
Margin = new Thickness(24, 16, 24, 24)
|
|
};
|
|
Grid.SetRow(buttonPanel, 2);
|
|
|
|
var cancelButton = new Wpf.Ui.Controls.Button
|
|
{
|
|
Content = "Abbrechen",
|
|
Appearance = ControlAppearance.Secondary,
|
|
Margin = new Thickness(0, 0, 8, 0),
|
|
Padding = new Thickness(16, 8, 16, 8)
|
|
};
|
|
cancelButton.Click += (s, e) => { DialogResult = false; Close(); };
|
|
buttonPanel.Children.Add(cancelButton);
|
|
|
|
var okButton = new Wpf.Ui.Controls.Button
|
|
{
|
|
Content = "OK",
|
|
Appearance = ControlAppearance.Primary,
|
|
Padding = new Thickness(24, 8, 24, 8),
|
|
IsEnabled = false
|
|
};
|
|
okButton.Click += (s, e) => { DialogResult = true; Close(); };
|
|
buttonPanel.Children.Add(okButton);
|
|
|
|
grid.Children.Add(buttonPanel);
|
|
|
|
Content = grid;
|
|
|
|
// Capture keys
|
|
PreviewKeyDown += (s, e) =>
|
|
{
|
|
e.Handled = true;
|
|
|
|
var modifiers = Keyboard.Modifiers;
|
|
var key = e.Key == Key.System ? e.SystemKey : e.Key;
|
|
|
|
// Ignore modifier-only keys
|
|
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)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// Require at least one modifier
|
|
if (modifiers == ModifierKeys.None)
|
|
{
|
|
keyDisplay.Text = "Mindestens eine Zusatztaste (Strg, Alt, Shift, Win) erforderlich!";
|
|
okButton.IsEnabled = false;
|
|
return;
|
|
}
|
|
|
|
CapturedModifiers = modifiers;
|
|
CapturedKey = key;
|
|
|
|
var parts = new System.Collections.Generic.List<string>();
|
|
if (modifiers.HasFlag(ModifierKeys.Control))
|
|
parts.Add("Strg");
|
|
if (modifiers.HasFlag(ModifierKeys.Alt))
|
|
parts.Add("Alt");
|
|
if (modifiers.HasFlag(ModifierKeys.Shift))
|
|
parts.Add("Shift");
|
|
if (modifiers.HasFlag(ModifierKeys.Windows))
|
|
parts.Add("Win");
|
|
parts.Add(key.ToString());
|
|
|
|
keyDisplay.Text = string.Join(" + ", parts);
|
|
okButton.IsEnabled = true;
|
|
};
|
|
}
|
|
}
|
|
}
|