I had a good time implementing your first request tonight.
Added ModifiedAcceptsReturnBehavior.cs:
using System.Linq;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
namespace Quantum.Client.Windows
{
public static class ModifiedAcceptsReturnBehavior
{
public static readonly DependencyProperty IsEnabledProperty =
DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(ModifiedAcceptsReturnBehavior), new PropertyMetadata(false, OnIsEnabledChanged));
public static bool GetIsEnabled(DependencyObject obj) => (bool)obj.GetValue(IsEnabledProperty);
static void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (e.Property == IsEnabledProperty && sender is TextBox textBox && e.NewValue is bool newValue)
{
if (newValue)
textBox.PreviewKeyDown += PreviewKeyDown;
else
textBox.PreviewKeyDown -= PreviewKeyDown;
}
}
private static void PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if ((App.Configuration?.EnterConfirmsDialogsInMultilineTextboxes ?? true) &&
sender is TextBox textBox &&
textBox.AcceptsReturn &&
e.Key == System.Windows.Input.Key.Enter &&
!System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftCtrl) &&
!System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.RightCtrl) &&
!System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftAlt) &&
!System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.RightAlt) &&
!System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftShift) &&
!System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.RightShift) &&
Window.GetWindow(textBox).FindVisualChildren<Button>().FirstOrDefault(b => b.IsDefault) is Button defaultButton &&
new ButtonAutomationPeer(defaultButton).GetPattern(PatternInterface.Invoke) is IInvokeProvider defaultButtonInvokeProvider)
{
defaultButtonInvokeProvider.Invoke();
e.Handled = true;
}
}
public static void SetIsEnabled(DependencyObject obj, bool value) => obj.SetValue(IsEnabledProperty, value);
}
}
Invoked the new guy in the default text box style:
<Style x:Key="AppTextBoxStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextBoxStyle}">
<!-- other stuff I didn't paste in here -->
<Setter Property="local:ModifiedAcceptsReturnBehavior.IsEnabled" Value="True" />
</Style>
That’s right, bruh. Took less than fifteen minutes to implement. Still got it.
If you want to see it for yourself, check out the topic for the upcoming preview release for instructions.
Ahh, let’s see how hard that would be…