Skip to content

Commit

Permalink
feat: Support Enter, Space and Escape on ComboBox
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Mar 1, 2022
1 parent 41d1bdc commit fb52456
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
41 changes: 30 additions & 11 deletions src/Uno.UI/UI/Xaml/Controls/ComboBox/ComboBox.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using Uno.Client;
using System.Collections;
using Uno.UI.Controls;
using Uno.Extensions;
using Windows.UI.Xaml.Automation.Peers;
using Windows.UI.Xaml.Input;
using Uno.Foundation.Logging;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.Foundation;
using Uno.UI;
using System.Linq;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml.Data;


using Uno.UI.DataBinding;
using Uno.UI.Xaml.Controls;
using Windows.UI.Core;
#if __ANDROID__
using Android.Views;
using _View = Android.Views.View;
Expand All @@ -35,6 +24,7 @@
using _View = AppKit.NSView;
#else
using _View = Windows.UI.Xaml.FrameworkElement;
using Windows.System;
#endif

#if HAS_UNO_WINUI
Expand Down Expand Up @@ -490,6 +480,35 @@ protected override void OnPointerReleased(PointerRoutedEventArgs args)
args.Handled = true;
}

protected override void OnKeyDown(KeyRoutedEventArgs args)
{
args.Handled = TryHandleKeyDown(args);

base.OnKeyDown(args);
}

internal bool TryHandleKeyDown(KeyRoutedEventArgs args)
{
if (args.Key == VirtualKey.Enter ||
args.Key == VirtualKey.Space)
{
if (!IsDropDownOpen)
{
IsDropDownOpen = true;
return true;
}
}
else if (args.Key == VirtualKey.Escape)
{
if (IsDropDownOpen)
{
IsDropDownOpen = false;
return true;
}
}
return false;
}

/// <summary>
/// Stretches the opened Popup horizontally, and uses the VerticalAlignment
/// of the first child for positioning.
Expand Down
30 changes: 27 additions & 3 deletions src/Uno.UI/UI/Xaml/Controls/ComboBox/ComboBoxItem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using Windows.System;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;

namespace Windows.UI.Xaml.Controls
{
Expand All @@ -11,5 +10,30 @@ public ComboBoxItem()
{
DefaultStyleKey = typeof(ComboBoxItem);
}

protected override void OnKeyDown(KeyRoutedEventArgs args)
{
if (ItemsControl.ItemsControlFromItemContainer(this) is ComboBox comboBox)
{
if (args.Key == VirtualKey.Enter && comboBox.IsDropDownOpen)
{
var item = comboBox.ItemFromContainer(this);
if (item != null)
{
comboBox.SelectedItem = item;
comboBox.IsDropDownOpen = false;
args.Handled = true;
}
}

if (!args.Handled)
{
// Fallback to combobox keydown handling
args.Handled = comboBox.TryHandleKeyDown(args);
}
}

base.OnKeyDown(args);
}
}
}

0 comments on commit fb52456

Please sign in to comment.