using Avalonia; using Avalonia.Controls; using Avalonia.Metadata; using Avalonia.Xaml.Interactivity; using CommunityToolkit.Mvvm.Messaging; using System.Collections.Immutable; using Toolkit.Foundation; namespace Toolkit.UI.Avalonia; public class NavigateAction : AvaloniaObject, IAction { public static readonly DirectProperty ParametersProperty = AvaloniaProperty.RegisterDirect(nameof(Parameters), x => x.Parameters); public static readonly StyledProperty RegionProperty = AvaloniaProperty.Register(nameof(Region)); public static readonly StyledProperty RouteProperty = AvaloniaProperty.Register(nameof(Route)); public static readonly StyledProperty ScopeProperty = AvaloniaProperty.Register(nameof(Scope)); private ParameterCollection parameterCollection = []; public object Region { get => GetValue(RegionProperty); set => SetValue(RegionProperty, value); } [Content] public ParameterCollection Parameters => parameterCollection ??= []; public string Route { get => GetValue(RouteProperty); set => SetValue(RouteProperty, value); } public NavigateScope? Scope { get => GetValue(ScopeProperty); set => SetValue(ScopeProperty, value); } public object Execute(object? sender, object? parameter) { if (sender is Control content) { Dictionary arguments = new(StringComparer.InvariantCultureIgnoreCase); if (content.DataContext is IObservableViewModel observableViewModel) { ImmutableDictionary? parameters = Parameters is { Count: > 0 } ? Parameters.ToImmutableDictionary(x => x.Key, x => x.Value) : ImmutableDictionary.Empty; observableViewModel.Messenger.Send(new NavigateEventArgs(Route, Region == this ? content : Region, Scope ?? null, content.DataContext, parameters)); } } return true; } }