Browse Source
Fix focus loss issue with AutocompleteBox (#12883 )
* fix focus loss issue with autocomplete box
* close dropdown when focus actually moves within the autocompletebox's focus scope
* make GetFocusScope internal for now
* remove GetFocusScope from focus manager
---------
Co-authored-by: Max Katz <maxkatz6@outlook.com>
pull/12979/head
Emmanuel Hansen
3 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
37 additions and
1 deletions
src/Avalonia.Controls/AutoCompleteBox/AutoCompleteBox.cs
src/Avalonia.Controls/ListBoxItem.cs
@ -826,12 +826,41 @@ namespace Avalonia.Controls
}
else
{
SetCurrentValue ( IsDropDownOpenProperty , false ) ;
// Check if we still have focus in the parent's focus scope
if ( GetFocusScope ( ) is { } scope & &
( FocusManager . GetFocusManager ( this ) ? . GetFocusedElement ( scope ) is not { } focused | |
( focused ! = this & &
( focused is Visual v & & ! this . IsVisualAncestorOf ( v ) ) ) ) )
{
SetCurrentValue ( IsDropDownOpenProperty , false ) ;
}
_ userCalledPopulate = false ;
ClearTextBoxSelection ( ) ;
}
_ isFocused = hasFocus ;
IFocusScope ? GetFocusScope ( )
{
IInputElement ? c = this ;
while ( c ! = null )
{
if ( c is IFocusScope scope & &
c is Visual v & &
v . VisualRoot is Visual root & &
root . IsVisible )
{
return scope ;
}
c = ( c as Visual ) ? . GetVisualParent < IInputElement > ( ) ? ?
( ( c as IHostedVisualTreeRoot ) ? . Host as IInputElement ) ;
}
return null ;
}
}
/// <summary>
@ -100,7 +100,14 @@ namespace Avalonia.Controls
ItemsControl . ItemsControlFromItemContaner ( this ) is ListBox owner )
{
if ( owner . UpdateSelectionFromPointerEvent ( this , e ) )
{
// As we only update selection from touch/pen on pointer release, we need to raise
// the pointer event on the owner to trigger a commit.
if ( e . Pointer . Type ! = PointerType . Mouse )
owner . RaiseEvent ( e ) ;
e . Handled = true ;
}
}
}