Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Windows.System;

namespace Microsoft.CmdPal.UI.Controls;

Expand All @@ -22,6 +24,7 @@ public sealed partial class ContentFormControl : UserControl
// tree. If this gets GC'ed, then it'll revoke our Action handler, and the
// form will do seemingly nothing.
private RenderedAdaptiveCard? _renderedCard;
private AdaptiveCard? _adaptiveCard;

public ContentFormViewModel? ViewModel { get => _viewModel; set => AttachViewModel(value); }

Expand Down Expand Up @@ -95,9 +98,11 @@ private void ViewModel_PropertyChanged(object? sender, System.ComponentModel.Pro
private void DisplayCard(AdaptiveCardParseResult result)
{
_renderedCard = _renderer.RenderAdaptiveCard(result.AdaptiveCard);
_adaptiveCard = result.AdaptiveCard;
ContentGrid.Children.Clear();
if (_renderedCard.FrameworkElement is not null)
{
_renderedCard.FrameworkElement.KeyDown += OnFormKeyDown;
ContentGrid.Children.Add(_renderedCard.FrameworkElement);

// Use the Loaded event to ensure we focus after the card is in the visual tree
Expand Down Expand Up @@ -276,6 +281,35 @@ private static void FixToggleAccessibilityNames(DependencyObject root)
return null;
}

private void OnFormKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key != VirtualKey.Enter || _renderedCard == null || _adaptiveCard == null)
{
return;
}

// Only submit when Enter is pressed inside a single-line TextBox
if (e.OriginalSource is TextBox textBox && !textBox.AcceptsReturn)
{
// Find the first Submit or Execute action on the card
IAdaptiveActionElement? submitAction = null;
foreach (var action in _adaptiveCard.Actions)
{
if (action is AdaptiveSubmitAction or AdaptiveExecuteAction)
{
submitAction = action;
break;
}
}

if (submitAction != null)
{
e.Handled = true;
ViewModel?.HandleSubmit(submitAction, _renderedCard.UserInputs.AsJson());
}
}
}

private void Rendered_Action(RenderedAdaptiveCard sender, AdaptiveActionEventArgs args) =>
ViewModel?.HandleSubmit(args.Action, args.Inputs.AsJson());
}
Loading