feat: add DialogFocusHint attached property.
This commit is contained in:
@@ -17,7 +17,10 @@
|
||||
<GradientStop Offset="0.9" Color="{DynamicResource SemiLightBlue1Color}" />
|
||||
</LinearGradientBrush>
|
||||
</UserControl.Background>
|
||||
<Grid Margin="24" RowDefinitions="Auto, *, Auto" MinWidth="400">
|
||||
<Grid
|
||||
MinWidth="400"
|
||||
Margin="24"
|
||||
RowDefinitions="Auto, *, Auto">
|
||||
<TextBlock
|
||||
Grid.Row="0"
|
||||
Margin="8"
|
||||
@@ -45,7 +48,10 @@
|
||||
Grid.Column="1"
|
||||
Margin="32,8,0,8"
|
||||
Label="Owner">
|
||||
<TextBox u:FormItem.Label="Owner" Text="{Binding Owner}" />
|
||||
<TextBox
|
||||
u:FocusHelper.DialogFocusHint="True"
|
||||
u:FormItem.Label="Owner"
|
||||
Text="{Binding Owner}" />
|
||||
</u:FormItem>
|
||||
<u:FormItem
|
||||
Grid.Row="1"
|
||||
@@ -69,9 +75,20 @@
|
||||
HorizontalAlignment="Right"
|
||||
Orientation="Horizontal"
|
||||
Spacing="8">
|
||||
<Button Command="{Binding DialogCommand}" Content="Dialog" Theme="{DynamicResource SolidButton}" />
|
||||
<Button Command="{Binding OKCommand}" Content="OK" Theme="{DynamicResource SolidButton}" Classes="Tertiary"/>
|
||||
<Button Command="{Binding CancelCommand}" Content="Cancel" Theme="{DynamicResource SolidButton}" Classes="Tertiary"/>
|
||||
<Button
|
||||
Command="{Binding DialogCommand}"
|
||||
Content="Dialog"
|
||||
Theme="{DynamicResource SolidButton}" />
|
||||
<Button
|
||||
Classes="Tertiary"
|
||||
Command="{Binding OKCommand}"
|
||||
Content="OK"
|
||||
Theme="{DynamicResource SolidButton}" />
|
||||
<Button
|
||||
Classes="Tertiary"
|
||||
Command="{Binding CancelCommand}"
|
||||
Content="Cancel"
|
||||
Theme="{DynamicResource SolidButton}" />
|
||||
<ComboBox>
|
||||
<ComboBoxItem>A</ComboBoxItem>
|
||||
<ComboBoxItem>B</ComboBoxItem>
|
||||
|
||||
@@ -4,3 +4,4 @@ using Avalonia.Metadata;
|
||||
[assembly:XmlnsDefinition("https://irihi.tech/ursa", "Ursa")]
|
||||
[assembly:XmlnsDefinition("https://irihi.tech/ursa", "Ursa.Controls")]
|
||||
[assembly:XmlnsDefinition("https://irihi.tech/ursa", "Ursa.Controls.Shapes")]
|
||||
[assembly:XmlnsDefinition("https://irihi.tech/ursa", "Ursa.Helpers")]
|
||||
@@ -4,6 +4,7 @@ using Avalonia.VisualTree;
|
||||
using Irihi.Avalonia.Shared.Helpers;
|
||||
using Irihi.Avalonia.Shared.Shapes;
|
||||
using Ursa.Controls.OverlayShared;
|
||||
using Ursa.Helpers;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
@@ -113,12 +114,17 @@ public partial class OverlayDialogHost
|
||||
control.AddHandler(DialogControlBase.LayerChangedEvent, OnDialogLayerChanged);
|
||||
if (!IsAnimationDisabled) MaskAppearAnimation.RunAsync(mask);
|
||||
|
||||
var element = control.GetVisualDescendants().OfType<InputElement>().FirstOrDefault(a => a.Focusable);
|
||||
var element = control.GetVisualDescendants().OfType<InputElement>()
|
||||
.FirstOrDefault(FocusHelper.GetDialogFocusHint);
|
||||
if (element is null)
|
||||
{
|
||||
element = control.GetVisualDescendants().OfType<InputElement>().FirstOrDefault(a => a.Focusable);
|
||||
}
|
||||
element?.Focus();
|
||||
_modalCount++;
|
||||
IsInModalStatus = _modalCount > 0;
|
||||
control.IsClosed = false;
|
||||
control.Focus();
|
||||
// control.Focus();
|
||||
}
|
||||
|
||||
// Handle dialog layer change event
|
||||
|
||||
@@ -9,6 +9,7 @@ using Irihi.Avalonia.Shared.Shapes;
|
||||
using Ursa.Common;
|
||||
using Ursa.Controls.OverlayShared;
|
||||
using Ursa.EventArgs;
|
||||
using Ursa.Helpers;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
@@ -69,7 +70,13 @@ public partial class OverlayDialogHost
|
||||
{
|
||||
await Task.WhenAll(animation.RunAsync(control), MaskAppearAnimation.RunAsync(mask));
|
||||
}
|
||||
var element = control.GetVisualDescendants().OfType<InputElement>().FirstOrDefault(a => a.Focusable);
|
||||
|
||||
var element = control.GetVisualDescendants().OfType<InputElement>()
|
||||
.FirstOrDefault(FocusHelper.GetDialogFocusHint);
|
||||
if (element is null)
|
||||
{
|
||||
element = control.GetVisualDescendants().OfType<InputElement>().FirstOrDefault(a => a.Focusable);
|
||||
}
|
||||
element?.Focus();
|
||||
}
|
||||
|
||||
|
||||
13
src/Ursa/Helpers/FocusHelper.cs
Normal file
13
src/Ursa/Helpers/FocusHelper.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Input;
|
||||
|
||||
namespace Ursa.Helpers;
|
||||
|
||||
public class FocusHelper
|
||||
{
|
||||
public static readonly AttachedProperty<bool> DialogFocusHintProperty =
|
||||
AvaloniaProperty.RegisterAttached<FocusHelper, InputElement, bool>("DialogFocusHint");
|
||||
|
||||
public static void SetDialogFocusHint(InputElement obj, bool value) => obj.SetValue(DialogFocusHintProperty, value);
|
||||
public static bool GetDialogFocusHint(InputElement obj) => obj.GetValue(DialogFocusHintProperty);
|
||||
}
|
||||
Reference in New Issue
Block a user