From bd8472f2328409ec8c10ba6fe2f1bfa4e7894ee1 Mon Sep 17 00:00:00 2001 From: doyaGu Date: Sat, 3 Oct 2026 07:41:19 -0400 Subject: [PATCH] feat: focus the first editable text box when a dialog opens --- YKanBan/Views/Dialogs/DialogWindow.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/YKanBan/Views/Dialogs/DialogWindow.cs b/YKanBan/Views/Dialogs/DialogWindow.cs index eca3d44..ef19c18 100644 --- a/YKanBan/Views/Dialogs/DialogWindow.cs +++ b/YKanBan/Views/Dialogs/DialogWindow.cs @@ -1,4 +1,6 @@ using Avalonia.Controls; +using Avalonia.Threading; +using Avalonia.VisualTree; using YKanBan.ViewModels.Dialogs; namespace YKanBan.Views.Dialogs; @@ -9,6 +11,8 @@ namespace YKanBan.Views.Dialogs; /// asks, with the result as the dialog result. /// The title bar close button is routed to the view model's cancel path, so a /// dialog with unsaved changes can ask before discarding them. +/// On opening, the first visible, enabled and editable text box takes the +/// focus with its text selected; dialogs without one leave the focus alone. /// public class DialogWindow : Window { @@ -41,6 +45,24 @@ public class DialogWindow : Window } } + protected override void OnOpened(EventArgs e) + { + base.OnOpened(e); + + // Wait for the first layout pass so visibility and enabled state are settled. + Dispatcher.UIThread.Post(FocusFirstTextBox, DispatcherPriority.Loaded); + } + + private void FocusFirstTextBox() + { + TextBox? target = this.GetVisualDescendants().OfType() + .FirstOrDefault(box => box.IsEffectivelyVisible && box.IsEffectivelyEnabled && box.Focusable && !box.IsReadOnly); + if (target is not null && target.Focus()) + { + target.SelectAll(); + } + } + protected override async void OnClosing(WindowClosingEventArgs e) { base.OnClosing(e);