Skip to content

Fix DatePickerPresenter focusing wrong selector for day-first locales#21116

Merged
MrJul merged 4 commits into
AvaloniaUI:masterfrom
NathanDrake2406:fix/datepicker-initial-focus
Apr 9, 2026
Merged

Fix DatePickerPresenter focusing wrong selector for day-first locales#21116
MrJul merged 4 commits into
AvaloniaUI:masterfrom
NathanDrake2406:fix/datepicker-initial-focus

Conversation

@NathanDrake2406

Copy link
Copy Markdown
Contributor

What does the pull request do?

Fixes DatePickerPresenter.SetInitialFocus focusing the month selector instead of the day selector when the day column is leftmost (e.g. dd/MM/yyyy locales).

What is the current behavior?

Copy-paste bug: the dayCol < monthCol && dayCol < yearCol branch calls _monthSelector.Focus() instead of _daySelector.Focus(). Users in day-first locales (UK, Europe, Australia, India, etc.) get the wrong selector focused when the picker opens.

What is the updated/expected behavior with this PR?

Replaced the three if/else branches with a data-driven loop that pairs each (visible, host, selector) tuple and focuses the leftmost visible column. This fixes the bug and makes the same class of copy-paste error structurally impossible.

Checklist

…resenter

SetInitialFocus had a copy-paste bug where the day-first branch
focused _monthSelector instead of _daySelector. This caused every
locale that puts day first (UK, Europe, Australia, India — dd/MM/yyyy)
to focus the wrong selector when the picker opened.

Refactored the three near-identical if/else branches into a
data-driven loop that pairs each host panel with its selector,
making this class of copy-paste bug structurally impossible.
Copilot AI review requested due to automatic review settings April 9, 2026 03:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an initial-focus bug in DatePickerPresenter where day-first locales (e.g., en-GB with dd/MM/yyyy) could focus the wrong selector on open, and adds a regression unit test to ensure the leftmost visible column receives focus.

Changes:

  • Reworked DatePickerPresenter.SetInitialFocus to select the leftmost visible selector via a small candidate scan (avoids copy/paste branching bugs).
  • Added a unit test that sets en-GB culture and asserts the day selector is focused on initialization.
  • Introduced a focus-enabled TestServices configuration for the new test.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/Avalonia.Controls.UnitTests/DatePickerTests.cs Adds a regression test validating initial focus for day-first locales and introduces focus-capable test services.
src/Avalonia.Controls/DateTimePickers/DatePickerPresenter.cs Refactors initial focus selection to consistently focus the leftmost visible selector.

@MrJul MrJul added bug backport-candidate-12.0.x Consider this PR for backporting to 12.0 branch labels Apr 9, 2026

@MrJul MrJul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.1.999-cibuild0064641-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@MrJul MrJul added this pull request to the merge queue Apr 9, 2026
Merged via the queue into AvaloniaUI:master with commit 5cde1a5 Apr 9, 2026
11 checks passed
@NathanDrake2406 NathanDrake2406 deleted the fix/datepicker-initial-focus branch April 10, 2026 05:08
MrJul added a commit to MrJul/Avalonia that referenced this pull request Apr 13, 2026
…AvaloniaUI#21116)

* fix: focus correct date selector for day-first locales in DatePickerPresenter

SetInitialFocus had a copy-paste bug where the day-first branch
focused _monthSelector instead of _daySelector. This caused every
locale that puts day first (UK, Europe, Australia, India — dd/MM/yyyy)
to focus the wrong selector when the picker opened.

Refactored the three near-identical if/else branches into a
data-driven loop that pairs each host panel with its selector,
making this class of copy-paste bug structurally impossible.

* chore: retrigger CI

* chore: retrigger CI

---------

Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
@MrJul MrJul added backported-12.0.x and removed backport-candidate-12.0.x Consider this PR for backporting to 12.0 branch labels Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants