feat(ui): clear focus from textboxes on Escape in the main window
Mirrors the existing click-outside behavior. Scoped to MainWindow only (not a generic TopLevel handler) so modal Escape-to-close bindings and Mission Control's ConPTY tiles are unaffected.
This commit is contained in:
@@ -3,12 +3,16 @@ using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.VisualTree;
|
||||
using ClaudeDo.Ui.Views;
|
||||
|
||||
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.
|
||||
/// Escape does the same, but only in the main window: modal windows bind Escape to closing
|
||||
/// themselves, and Mission Control's ConPTY tiles need Escape to reach the terminal, so this
|
||||
/// intentionally does not use a generic TopLevel handler.
|
||||
/// </summary>
|
||||
public static class FocusClearing
|
||||
{
|
||||
@@ -16,6 +20,9 @@ public static class FocusClearing
|
||||
{
|
||||
InputElement.PointerPressedEvent.AddClassHandler<TopLevel>(
|
||||
OnPointerPressed, RoutingStrategies.Tunnel, handledEventsToo: true);
|
||||
|
||||
InputElement.KeyDownEvent.AddClassHandler<MainWindow>(
|
||||
OnKeyDown, RoutingStrategies.Tunnel);
|
||||
}
|
||||
|
||||
private static void OnPointerPressed(TopLevel topLevel, PointerPressedEventArgs e)
|
||||
@@ -31,4 +38,19 @@ public static class FocusClearing
|
||||
|
||||
focusManager.Focus(null);
|
||||
}
|
||||
|
||||
private static void OnKeyDown(MainWindow window, KeyEventArgs e)
|
||||
{
|
||||
if (e.Key != Key.Escape)
|
||||
return;
|
||||
|
||||
if (window.FocusManager is not { } focusManager)
|
||||
return;
|
||||
|
||||
if (focusManager.GetFocusedElement() is not TextBox)
|
||||
return;
|
||||
|
||||
focusManager.Focus(null);
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user