using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Interactivity; using Avalonia.Media; using Avalonia.Media.Immutable; using Avalonia.Platform; using Avalonia.Styling; using Avalonia.Threading; using AvaloniaEdit; using AvaloniaEdit.Highlighting; using AvaloniaEdit.Indentation.CSharp; using FluentAvalonia.Core; using FluentAvalonia.Styling; using FluentAvalonia.UI.Controls; using FluentAvaloniaSamples.Services; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace FluentAvaloniaSamples.Controls; // From WinUI XamlControlsGallery public class ControlExampleSubstitution : AvaloniaObject { public static readonly DirectProperty IsEnabledProperty = AvaloniaProperty.RegisterDirect(nameof(IsEnabled), x => x.IsEnabled, (x, v) => x.IsEnabled = v); public static readonly DirectProperty ValueProperty = AvaloniaProperty.RegisterDirect(nameof(Value), x => x.Value, (x, v) => x.Value = v); public string Key { get; set; } private object _value = null; public object Value { get => _value; set { SetAndRaise(ValueProperty, ref _value, value); ValueChanged?.Invoke(this, null); } } private bool _enabled = true; public bool IsEnabled { get => _enabled; set { SetAndRaise(IsEnabledProperty, ref _enabled, value); ValueChanged?.Invoke(this, null); } } public event TypedEventHandler ValueChanged; public string ValueAsString() { if (!IsEnabled) { return string.Empty; } object value = Value; // For solid color brushes, use the underlying color. if (value is SolidColorBrush) { value = ((SolidColorBrush)value).Color; } if (value == null) { return string.Empty; } return value.ToString(); } } public class ControlExample : HeaderedContentControl { public ControlExample() { _substitutions = new List(); _flyoutOptions = new List(); ResourcesChanged += OnResourcesChanged; PseudoClasses.Add(":optionsfull"); } public static readonly StyledProperty TargetTypeProperty = AvaloniaProperty.Register(nameof(TargetType)); public static readonly StyledProperty XamlSourceProperty = AvaloniaProperty.Register(nameof(XamlSource)); public static readonly StyledProperty CSharpSourceProperty = AvaloniaProperty.Register(nameof(CSharpSource)); public static readonly StyledProperty UsageNotesProperty = AvaloniaProperty.Register(nameof(UsageNotes)); public static readonly StyledProperty OptionsProperty = AvaloniaProperty.Register(nameof(Options)); public static readonly DirectProperty> SubstitutionsProperty = AvaloniaProperty.RegisterDirect>(nameof(Substitutions), x => x.Substitutions); public static readonly StyledProperty IsOptionsExpandedProperty = AvaloniaProperty.Register(nameof(IsOptionsExpanded), true); public Type TargetType { get => GetValue(TargetTypeProperty); set => SetValue(TargetTypeProperty, value); } public string XamlSource { get => GetValue(XamlSourceProperty); set => SetValue(XamlSourceProperty, value); } public string CSharpSource { get => GetValue(CSharpSourceProperty); set => SetValue(CSharpSourceProperty, value); } public Control Options { get => GetValue(OptionsProperty); set => SetValue(OptionsProperty, value); } public string UsageNotes { get => GetValue(UsageNotesProperty); set => SetValue(UsageNotesProperty, value); } public IList Substitutions { get => _substitutions; } public bool IsOptionsExpanded { get => GetValue(IsOptionsExpandedProperty); set => SetValue(IsOptionsExpandedProperty, value); } public bool EnableShowDocsLink { get; set; } public bool EnableShowDefinitionLink { get; set; } public string DocsLinkHeader { get; set; } = "Avalonia Docs"; private static Regex SubstitutionPattern = new Regex(@"\$\(([^\)]+)\)"); protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { if (_copyXamlButton != null) _copyXamlButton.Click -= OnCopyXamlClick; if (_copyCSharpButton != null) _copyCSharpButton.Click -= OnCopyCSharpClick; if (_expandOptionsButton != null) _expandOptionsButton.Click -= OnExpandOptionsClick; base.OnApplyTemplate(e); _exampleThemeScopeProvider = e.NameScope.Find("ThemeScopeProvider"); _expandOptionsButton = e.NameScope.Find