Browse Source

Fix CalendarDatePicker.Focus() not focusing the text box (#22170)

* Fix CalendarDatePicker.Focus() not focusing the text box

OnGotFocus only forwarded focus to PART_TextBox when the navigation method
was Tab, so a programmatic Focus() call left focus on the picker itself.
Gate on the focus source instead, and keep select-all on Tab only.

* Remove comment from CalendarDatePicker.OnGotFocus
pull/22225/head
Evan 3 months ago
committed by GitHub
parent
commit
eb5cce2492
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 17
      src/Avalonia.Controls/CalendarDatePicker/CalendarDatePicker.cs
  2. 41
      tests/Avalonia.Controls.UnitTests/CalendarDatePickerTests.cs

17
src/Avalonia.Controls/CalendarDatePicker/CalendarDatePicker.cs

@ -396,14 +396,19 @@ namespace Avalonia.Controls
protected override void OnGotFocus(FocusChangedEventArgs e)
{
base.OnGotFocus(e);
if(IsEnabled && _textBox != null && e.NavigationMethod == NavigationMethod.Tab)
if (IsEnabled && _textBox != null && ReferenceEquals(e.Source, this))
{
_textBox.Focus();
var text = _textBox.Text;
if(!string.IsNullOrEmpty(text))
_textBox.Focus(e.NavigationMethod);
if (e.NavigationMethod == NavigationMethod.Tab)
{
_textBox.SelectionStart = 0;
_textBox.SelectionEnd = text.Length;
var text = _textBox.Text;
if (!string.IsNullOrEmpty(text))
{
_textBox.SelectionStart = 0;
_textBox.SelectionEnd = text.Length;
}
}
}
}

41
tests/Avalonia.Controls.UnitTests/CalendarDatePickerTests.cs

@ -3,6 +3,8 @@ using System.Linq;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Data.Converters;
using Avalonia.Harfbuzz;
using Avalonia.Headless;
using Avalonia.Input;
using Avalonia.Platform;
using Avalonia.UnitTests;
@ -195,6 +197,45 @@ namespace Avalonia.Controls.UnitTests
}
}
[Fact]
public void Tab_Focus_Should_Move_Focus_To_TextBox()
{
using (UnitTestApplication.Start(FocusServices))
{
var datePicker = new CalendarDatePicker { Template = CreateTemplate() };
var root = new TestRoot(datePicker);
root.LayoutManager.ExecuteInitialLayoutPass();
datePicker.Focus(NavigationMethod.Tab);
Assert.Same(GetTextBox(datePicker), root.FocusManager.GetFocusedElement());
}
}
[Fact]
public void Programmatic_Focus_Should_Move_Focus_To_TextBox()
{
using (UnitTestApplication.Start(FocusServices))
{
var datePicker = new CalendarDatePicker { Template = CreateTemplate() };
var root = new TestRoot(datePicker);
root.LayoutManager.ExecuteInitialLayoutPass();
datePicker.Focus();
Assert.Same(GetTextBox(datePicker), root.FocusManager.GetFocusedElement());
}
}
private static TestServices FocusServices => TestServices.MockThreadingInterface.With(
fontManagerImpl: new HeadlessFontManagerStub(),
standardCursorFactory: Mock.Of<ICursorFactory>(),
textShaperImpl: new HarfBuzzTextShaper(),
renderInterface: new HeadlessPlatformRenderInterface(),
keyboardDevice: () => new KeyboardDevice(),
keyboardNavigation: () => new KeyboardNavigationHandler(),
inputManager: new InputManager());
private static TestServices Services => TestServices.MockThreadingInterface.With(
standardCursorFactory: Mock.Of<ICursorFactory>());

Loading…
Cancel
Save