-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<!-- This file is part of the SoulSplitter distribution (https://github.com/FrankvdStam/SoulSplitter). | ||
Copyright (c) 2022 Frank van der Stam. | ||
https://github.com/FrankvdStam/SoulSplitter/blob/main/LICENSE | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, version 3. | ||
This program is distributed in the hope that it will be useful, but | ||
WITHOUT ANY WARRANTY without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see<http://www.gnu.org/licenses/> . --> | ||
|
||
|
||
<UserControl x:Class="SoulSplitter.UI.Generic.HotkeyPicker" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:local="clr-namespace:SoulSplitter.UI.Generic" | ||
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" | ||
Check warning on line 24 in src/SoulSplitter/UI/Generic/HotkeyPicker.xaml GitHub Actions / Qodana for .NETRedundant namespace alias
|
||
xmlns:validation="clr-namespace:SoulSplitter.UI.Validation" | ||
Check warning on line 25 in src/SoulSplitter/UI/Generic/HotkeyPicker.xaml GitHub Actions / Qodana for .NETRedundant namespace alias
|
||
mc:Ignorable="d" | ||
d:DataContext="{d:DesignInstance local:BaseViewModel, IsDesignTimeCreatable=True}"> | ||
|
||
<UserControl.Resources> | ||
<ResourceDictionary> | ||
<ResourceDictionary.MergedDictionaries> | ||
|
||
<!-- Material design --> | ||
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" /> | ||
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" /> | ||
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/materialdesigncolor.indigo.xaml" /> | ||
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.indigo.xaml" /> | ||
|
||
</ResourceDictionary.MergedDictionaries> | ||
</ResourceDictionary> | ||
</UserControl.Resources> | ||
|
||
<TextBox | ||
IsReadOnly="True" | ||
PreviewMouseDown="OnPreviewMouseDown" | ||
PreviewKeyDown="OnPreviewKeyDown" | ||
PreviewLostKeyboardFocus="OnPreviewLostKeyboardFocus"/> | ||
</UserControl> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// This file is part of the SoulSplitter distribution (https://github.com/FrankvdStam/SoulSplitter). | ||
// Copyright (c) 2022 Frank van der Stam. | ||
// https://github.com/FrankvdStam/SoulSplitter/blob/main/LICENSE | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, version 3. | ||
// | ||
// This program is distributed in the hope that it will be useful, but | ||
// WITHOUT ANY WARRANTY without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
// General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
using System.Collections.Generic; | ||
using System.Globalization; | ||
Check warning on line 18 in src/SoulSplitter/UI/Generic/HotkeyPicker.xaml.cs GitHub Actions / Qodana for .NETRedundant using directive
|
||
using System.Windows.Forms; | ||
Check warning on line 19 in src/SoulSplitter/UI/Generic/HotkeyPicker.xaml.cs GitHub Actions / Qodana for .NETRedundant using directive
|
||
using System.Windows.Input; | ||
using SoulSplitter.UI.EldenRing; | ||
Check warning on line 21 in src/SoulSplitter/UI/Generic/HotkeyPicker.xaml.cs GitHub Actions / Qodana for .NETRedundant using directive
|
||
using KeyEventArgs = System.Windows.Input.KeyEventArgs; | ||
using TextBox = System.Windows.Controls.TextBox; | ||
using UserControl = System.Windows.Controls.UserControl; | ||
|
||
namespace SoulSplitter.UI.Generic | ||
{ | ||
/// <summary> | ||
/// Interaction logic for HotkeyPicker.xaml | ||
/// </summary> | ||
public partial class HotkeyPicker : UserControl | ||
Check warning on line 31 in src/SoulSplitter/UI/Generic/HotkeyPicker.xaml.cs GitHub Actions / Qodana for .NETRedundant class or interface specification in base types list
|
||
{ | ||
public HotkeyPicker() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
public class HotkeyCompletedParameter | ||
{ | ||
public object Sender; | ||
public ModifierKeys ModifierKeys; | ||
public Key Key; | ||
} | ||
|
||
public RelayCommand HotkeyCompletedCommand { get; set; } | ||
|
||
private bool _isActive = false; | ||
Check warning on line 47 in src/SoulSplitter/UI/Generic/HotkeyPicker.xaml.cs GitHub Actions / Qodana for .NETRedundant member initializer
|
||
private ModifierKeys _modifierKeys = ModifierKeys.None; | ||
private Key _key = Key.None; | ||
private void OnPreviewKeyDown(object sender, KeyEventArgs e) | ||
{ | ||
if (_isActive) | ||
{ | ||
if (e.IsDown && sender is TextBox textBox) | ||
{ | ||
//Get modifier key and remember state | ||
if (_modifierKeysLookup.TryGetValue(e.Key, out ModifierKeys modifierKeys)) | ||
{ | ||
if (!_modifierKeys.HasFlag(modifierKeys)) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(textBox.Text)) | ||
{ | ||
textBox.Text += " + "; | ||
} | ||
|
||
textBox.Text += modifierKeys; | ||
_modifierKeys |= modifierKeys; | ||
} | ||
} | ||
else //get regular key, end listening for keypresses, invoke command | ||
{ | ||
if (!string.IsNullOrWhiteSpace(textBox.Text)) | ||
{ | ||
textBox.Text += " + "; | ||
} | ||
|
||
textBox.Text += e.Key; | ||
_key = e.Key; | ||
|
||
HotkeyCompletedCommand?.Execute(new HotkeyCompletedParameter | ||
{ | ||
Sender = this, | ||
Key = _key, | ||
ModifierKeys = _modifierKeys | ||
}); | ||
_isActive = false; | ||
} | ||
|
||
var converter = new ModifierKeysConverter(); | ||
Check warning on line 89 in src/SoulSplitter/UI/Generic/HotkeyPicker.xaml.cs GitHub Actions / Qodana for .NETUnused local variable
|
||
e.Handled = true; | ||
} | ||
} | ||
} | ||
|
||
private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) | ||
{ | ||
if (!_isActive && sender is TextBox textBox) | ||
{ | ||
_isActive = true; | ||
_key = Key.None; | ||
_modifierKeys = ModifierKeys.None; | ||
textBox.Text = ""; | ||
} | ||
} | ||
|
||
private static Dictionary<Key, ModifierKeys> _modifierKeysLookup = new Dictionary<Key, ModifierKeys>() | ||
{ | ||
{ Key.LeftAlt, ModifierKeys.Alt }, | ||
{ Key.RightAlt, ModifierKeys.Alt }, | ||
{ Key.System, ModifierKeys.Alt }, | ||
{ Key.LeftCtrl, ModifierKeys.Control }, | ||
{ Key.RightCtrl, ModifierKeys.Control }, | ||
{ Key.LeftShift, ModifierKeys.Shift }, | ||
{ Key.RightShift, ModifierKeys.Shift }, | ||
{ Key.LWin, ModifierKeys.Windows }, | ||
{ Key.RWin, ModifierKeys.Windows }, | ||
}; | ||
|
||
private void OnPreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) | ||
{ | ||
if (_isActive && sender is TextBox textBox) | ||
{ | ||
_isActive = false; | ||
_key = Key.None; | ||
_modifierKeys = ModifierKeys.None; | ||
textBox.Text = ""; | ||
} | ||
} | ||
} | ||
} |