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:
@@ -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>(),
|
||||||
|
|||||||
34
src/ClaudeDo.Ui/Services/FocusClearing.cs
Normal file
34
src/ClaudeDo.Ui/Services/FocusClearing.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user