feat: focus the first editable text box when a dialog opens

This commit is contained in:
doyaGu committed 2026-10-03 07:41:19 -04:00
1 parent 6ea0df43e3
commit bd8472f232
1 file changed
+22
+22
View File
@@ -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;
/// <see cref="DialogViewModelBase"/> 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.
/// </summary>
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<TextBox>()
.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);