From 2c49fed775a06f8184034619c179044f0c5d529e Mon Sep 17 00:00:00 2001 From: BoiHanny Date: Sat, 22 Jul 2023 21:49:18 +0200 Subject: [PATCH] When you edit a status, it will now automatically focus into the status message. You can use Enter to accept the change or Esc to discard it. --- .../XamlCommunication/TextBoxBehaviour.cs | 61 +++++++++++++++++++ vrcosc-magicchatbox/MainWindow.xaml | 4 ++ vrcosc-magicchatbox/MainWindow.xaml.cs | 20 ++++-- 3 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 vrcosc-magicchatbox/Classes/XamlCommunication/TextBoxBehaviour.cs diff --git a/vrcosc-magicchatbox/Classes/XamlCommunication/TextBoxBehaviour.cs b/vrcosc-magicchatbox/Classes/XamlCommunication/TextBoxBehaviour.cs new file mode 100644 index 00000000..329e839d --- /dev/null +++ b/vrcosc-magicchatbox/Classes/XamlCommunication/TextBoxBehaviour.cs @@ -0,0 +1,61 @@ +using System.Windows.Input; +using System.Windows; +using vrcosc_magicchatbox.ViewModels; +using System.Windows.Controls; + +namespace vrcosc_magicchatbox.Classes +{ + public static class TextBoxBehaviour + { + public static bool GetHandleEnterKey(DependencyObject obj) + { + return (bool)obj.GetValue(HandleEnterKeyProperty); + } + + public static void SetHandleEnterKey(DependencyObject obj, bool value) + { + obj.SetValue(HandleEnterKeyProperty, value); + } + + public static readonly DependencyProperty HandleEnterKeyProperty = + DependencyProperty.RegisterAttached("HandleEnterKey", typeof(bool), typeof(TextBoxBehaviour), new PropertyMetadata(false, HandleEnterKeyPropertyChanged)); + + private static void HandleEnterKeyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is TextBox textBox) + { + textBox.KeyDown -= TextBox_KeyDown; + + if ((bool)e.NewValue) + { + textBox.KeyDown += TextBox_KeyDown; + } + } + } + + private static void TextBox_KeyDown(object sender, KeyEventArgs e) + { + if (e.Key == Key.Escape || e.Key == Key.Enter) + { + var textBox = sender as TextBox; + var item = textBox.DataContext as StatusItem; + if (item == null) return; + + if (e.Key == Key.Escape) + { + // Run your logic to cancel the edit here + item.editMsg = ""; + item.IsEditing = false; + } + else if (e.Key == Key.Enter) + { + // If enter is pressed, IsEditing is set to false + item.editMsg = textBox.Text; + item.IsEditing = false; + } + } + } + + } +} + diff --git a/vrcosc-magicchatbox/MainWindow.xaml b/vrcosc-magicchatbox/MainWindow.xaml index 91124151..580fa11f 100644 --- a/vrcosc-magicchatbox/MainWindow.xaml +++ b/vrcosc-magicchatbox/MainWindow.xaml @@ -737,10 +737,12 @@ Text="{Binding msg}" Visibility="{Binding IsEditing, Converter={StaticResource InverseBoolToVisibilityConverter}, UpdateSourceTrigger=PropertyChanged}" /> + +