using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Primitives; using Avalonia.Controls.Metadata; namespace ControlCatalog.Controls { /// /// Framing options for a stage. /// public enum SampleStage { Padded, Flush, None } /// /// One example on a : a card with a title (), /// a description, the live example as content, and an optional options panel that sits beside the example /// on wide layouts and below it on narrow ones. /// [PseudoClasses(pcHasOptions, pcHasCode, pcNarrow, pcStageFlush, pcStageNone, pcFixedStage)] public class SampleSection : HeaderedContentControl { private const string pcHasOptions = ":has-options"; private const string pcHasCode = ":has-code"; private const string pcNarrow = ":narrow"; private const string pcStageFlush = ":stage-flush"; private const string pcStageNone = ":stage-none"; private const string pcFixedStage = ":fixed-stage"; /// /// Below this width the options panel moves under the example instead of beside it. /// private const double NarrowThreshold = 620; public static readonly StyledProperty DescriptionProperty = AvaloniaProperty.Register(nameof(Description)); public static readonly StyledProperty OptionsProperty = AvaloniaProperty.Register(nameof(Options)); public static readonly StyledProperty CodeProperty = AvaloniaProperty.Register(nameof(Code)); public static readonly StyledProperty CodeLanguageProperty = AvaloniaProperty.Register(nameof(CodeLanguage)); public static readonly StyledProperty StageProperty = AvaloniaProperty.Register(nameof(Stage), SampleStage.Padded); public static readonly StyledProperty StageMinHeightProperty = AvaloniaProperty.Register(nameof(StageMinHeight), 72); public static readonly StyledProperty StageHeightProperty = AvaloniaProperty.Register(nameof(StageHeight), double.NaN); /// /// What this example shows, in one sentence. /// public string? Description { get => GetValue(DescriptionProperty); set => SetValue(DescriptionProperty, value); } /// /// Controls that configure the example. Leave unset when the example has nothing to configure. /// public object? Options { get => GetValue(OptionsProperty); set => SetValue(OptionsProperty, value); } /// /// A short snippet shown under the example, collapsed until the reader opens it. Keep it to the /// lines that matter rather than reproducing the whole sample. /// public string? Code { get => GetValue(CodeProperty); set => SetValue(CodeProperty, value); } /// /// The language is highlighted as. Defaults to XAML. /// public CodeLanguage CodeLanguage { get => GetValue(CodeLanguageProperty); set => SetValue(CodeLanguageProperty, value); } /// /// How the example is framed. is the default card stage; use /// for content that paints its own edges and /// for content that must sit directly on the card. /// public SampleStage Stage { get => GetValue(StageProperty); set => SetValue(StageProperty, value); } /// /// Minimum height of the stage, for examples that need room to be understood. /// public double StageMinHeight { get => GetValue(StageMinHeightProperty); set => SetValue(StageMinHeightProperty, value); } /// /// Fixed stage height, for content with no natural height: a demo app, a carousel, a GL surface. /// public double StageHeight { get => GetValue(StageHeightProperty); set => SetValue(StageHeightProperty, value); } protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { base.OnPropertyChanged(change); if (change.Property == OptionsProperty) { PseudoClasses.Set(pcHasOptions, change.NewValue is not null); } else if (change.Property == CodeProperty) { PseudoClasses.Set(pcHasCode, !string.IsNullOrWhiteSpace(change.NewValue as string)); } else if (change.Property == StageProperty) { var stage = change.GetNewValue(); PseudoClasses.Set(pcStageFlush, stage == SampleStage.Flush); PseudoClasses.Set(pcStageNone, stage == SampleStage.None); } else if (change.Property == StageHeightProperty) { PseudoClasses.Set(pcFixedStage, !double.IsNaN(change.GetNewValue())); } } protected override void OnSizeChanged(SizeChangedEventArgs e) { base.OnSizeChanged(e); if (e.WidthChanged) { PseudoClasses.Set(pcNarrow, e.NewSize.Width < NarrowThreshold); } } } }