Refactor(Cascader): Add basic key handle support

This commit is contained in:
chinboy
2026-02-04 16:51:08 +08:00
parent 06c93e5e17
commit db79ceed39
3 changed files with 75 additions and 1 deletions
@@ -3,6 +3,7 @@ using System.Reactive.Disposables.Fluent;
using AtomUI.Controls;
using AtomUI.Desktop.Controls.DataLoad;
using AtomUI.Desktop.Controls.Primitives;
using AtomUI.Input;
using AtomUI.Theme;
using AtomUI.Utils;
using Avalonia;
@@ -273,6 +274,57 @@ public class Cascader : AbstractSelect, IControlSharedTokenResourcesHost
}
protected override void OnKeyDown(KeyEventArgs e)
{
_ = e ?? throw new ArgumentNullException(nameof(e));
base.OnKeyDown(e);
if (e.Handled || !IsEnabled)
{
return;
}
if (IsDropDownOpen)
{
if (_cascaderView != null)
{
_cascaderView.HandleKeyDown(e);
if (e.Handled)
{
return;
}
}
if (e.Key == Key.Escape)
{
SetCurrentValue(IsDropDownOpenProperty, false);
e.Handled = true;
}
}
else
{
// The drop down is not open, the Down key will toggle it open.
// Ignore key buttons, if they are used for XY focus.
if (e.Key == Key.Down
&& !this.IsAllowedXYNavigationMode(e.KeyDeviceType))
{
SetCurrentValue(IsDropDownOpenProperty, true);
e.Handled = true;
}
}
// Standard drop down navigation
switch (e.Key)
{
case Key.F4:
SetCurrentValue(IsDropDownOpenProperty, !IsDropDownOpen);
e.Handled = true;
break;
default:
break;
}
}
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
@@ -644,4 +644,26 @@ public partial class CascaderView : TemplatedControl, IMotionAwareControl, ICont
return false;
}
public void HandleKeyDown(KeyEventArgs e)
{
// TODO
switch (e.Key)
{
case Key.Enter:
e.Handled = false;
break;
case Key.Up:
e.Handled = false;
break;
case Key.Down:
e.Handled = false;
break;
default:
break;
}
}
}
@@ -643,7 +643,7 @@ public class AbstractSelect : TemplatedControl, IMotionAwareControl, ISizeTypeAw
private void HandleWindowDeactivated(object? sender, EventArgs e)
{
//SetCurrentValue(IsDropDownOpenProperty, false);
SetCurrentValue(IsDropDownOpenProperty, false);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)