Skip to content

Commit

Permalink
set focus on dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlm committed Dec 13, 2024
1 parent ad5332b commit 6872f53
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/Consolonia.Gallery/View/XamlDialogWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Linq;
using Avalonia;
using Avalonia.Input;
using Consolonia.Core.Controls;

namespace Consolonia.Gallery.View
Expand All @@ -8,19 +10,25 @@ public partial class XamlDialogWindow : DialogWindow
public XamlDialogWindow()
{
InitializeComponent();
AttachedToVisualTree += OnShowDialog;

AttachedToVisualTree += DialogWindowAttachedToVisualTree;
}

private void OnShowDialog(object sender, Avalonia.VisualTreeAttachmentEventArgs e)
private void DialogWindowAttachedToVisualTree(object sender, Avalonia.VisualTreeAttachmentEventArgs e)
{
AttachedToVisualTree -= OnShowDialog;
Xaml.AttachedToVisualTree += OnButtonAttached;
AttachedToVisualTree -= DialogWindowAttachedToVisualTree;

var child = (InputElement)this.LogicalChildren.FirstOrDefault();
if (child != null)
child.AttachedToVisualTree += OnChildAttachedToVisualTree;
}

private void OnButtonAttached(object sender, VisualTreeAttachmentEventArgs e)
private void OnChildAttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs e)
{
Xaml.AttachedToVisualTree -= OnButtonAttached;
Xaml.Focus();
var child = (InputElement)sender;
child.AttachedToVisualTree -= OnChildAttachedToVisualTree;
// Set focus to the first focusable element
child?.Focus();
}
}
}

0 comments on commit 6872f53

Please sign in to comment.