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);