feat(ui): clear textbox focus on click outside any text box

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
mika kuns
2026-06-04 08:42:49 +02:00
parent 9470c5b10b
commit fab17720cc
2 changed files with 36 additions and 0 deletions

View File

@@ -21,6 +21,8 @@ public partial class App : Application
{ {
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{ {
FocusClearing.Install();
desktop.MainWindow = new MainWindow desktop.MainWindow = new MainWindow
{ {
DataContext = Services.GetRequiredService<IslandsShellViewModel>(), DataContext = Services.GetRequiredService<IslandsShellViewModel>(),

View File

@@ -0,0 +1,34 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.VisualTree;
namespace ClaudeDo.Ui.Services;
/// <summary>
/// Clears focus from a TextBox when the user clicks outside of any text box, so input
/// fields behave like the user expects. Registered once for every window in the app.
/// </summary>
public static class FocusClearing
{
public static void Install()
{
InputElement.PointerPressedEvent.AddClassHandler<TopLevel>(
OnPointerPressed, RoutingStrategies.Tunnel, handledEventsToo: true);
}
private static void OnPointerPressed(TopLevel topLevel, PointerPressedEventArgs e)
{
if (topLevel.FocusManager is not { } focusManager)
return;
if (focusManager.GetFocusedElement() is not TextBox)
return;
if (e.Source is Visual v && v.FindAncestorOfType<TextBox>(includeSelf: true) is not null)
return;
focusManager.Focus(null);
}
}