mirror of
https://github.com/AtomUI/AtomUI.git
synced 2026-10-28 23:08:36 +08:00
feat(SearchEdit): Enter handler
1. Add HandleEnterAsSearch property with default value false to maintain old code's behavior. 2. Override OnKeyUp method to process Enter keyUp. It triggers NotifySearchButtonClicked method only if HandleEnterAsSearch is true. Override keyUp rather than keyDown to avoid triggering repeatedly when user press Enter continuously.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Styling;
|
||||
|
||||
@@ -28,6 +29,15 @@ public class SearchEdit : LineEdit
|
||||
|
||||
public static readonly StyledProperty<ControlTheme?> SearchButtonThemeProperty =
|
||||
AvaloniaProperty.Register<SearchEdit, ControlTheme?>(nameof(SearchButtonTheme));
|
||||
|
||||
public static readonly StyledProperty<bool> HandleEnterAsSearchProperty = AvaloniaProperty.Register<SearchEdit, bool>(
|
||||
nameof(HandleEnterAsSearch), defaultValue: false); //为了不破坏老代码行为,默认值为false,即默认不处理Enter
|
||||
|
||||
public bool HandleEnterAsSearch
|
||||
{
|
||||
get => GetValue(HandleEnterAsSearchProperty);
|
||||
set => SetValue(HandleEnterAsSearchProperty, value);
|
||||
}
|
||||
|
||||
public SearchEditButtonStyle SearchButtonStyle
|
||||
{
|
||||
@@ -96,4 +106,25 @@ public class SearchEdit : LineEdit
|
||||
var eventArgs = new RoutedEventArgs(SearchButtonClickEvent, this);
|
||||
RaiseEvent(eventArgs);
|
||||
}
|
||||
|
||||
|
||||
//Enter handler
|
||||
//使用OnKeyUp而不是OnKeyDown,避免用户一直按着Enter一直触发Search
|
||||
protected override void OnKeyUp(KeyEventArgs e)
|
||||
{
|
||||
base.OnKeyUp(e);
|
||||
|
||||
if (!HandleEnterAsSearch)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (e is not { Key: Key.Enter, Handled: false })
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
NotifySearchButtonClicked();
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user