FocusManager and KeyboardDevice should stay in sync (original) (raw)

Describe the bug

FocusManager.Focus() ultimately calls KeyboardDevice.SetFocusedElement.

However, since #18373, a focus change can be cancelled. If it is, that information is not propagated back to the FocusManager, which incorrectly sets the wrong FocusedElement to the current focus scope.

To Reproduce

  1. Call FocusManager.Focus
  2. Handle FocusChanging and call TryCancel on the event arguments.
  3. Notice that FocusManager.FocusedElement on the current scope has still be changed.

Expected behavior

When FocusManager.Focus() internally calls KeyboardDevice.SetFocusedElement(), it should verify that the focused element effectively changed. If it didn't or was redirected, the focused element should be updated to the current element, and Focus() should return false.

Avalonia version

12.0.0-rc2, master

OS

No response

Additional context

No response