|
| 1 | +using Avalonia; |
| 2 | +using Avalonia.Controls; |
| 3 | +using Avalonia.Controls.Primitives; |
| 4 | +using NexusMods.Icons; |
| 5 | + |
| 6 | +namespace NexusMods.App.UI.Controls.PageHeader; |
| 7 | + |
| 8 | +public class PageHeader : ContentControl |
| 9 | +{ |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Defines the Title property of the <see cref="PageHeader"/>. |
| 13 | + /// </summary> |
| 14 | + public static readonly StyledProperty<string?> TitleProperty = AvaloniaProperty.Register<PageHeader, string?>(nameof(Title), defaultValue: "Default Title"); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Defines the Title property of the <see cref="PageHeader"/>. |
| 18 | + /// </summary> |
| 19 | + public static readonly StyledProperty<string?> DescriptionProperty = AvaloniaProperty.Register<PageHeader, string?>(nameof(Description), defaultValue: "This is the default description of the page."); |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Defines the Title property of the <see cref="PageHeader"/>. |
| 23 | + /// </summary> |
| 24 | + public static readonly StyledProperty<IconValue?> IconProperty = AvaloniaProperty.Register<PageHeader, IconValue?>(nameof(Icon), defaultValue: IconValues.PictogramBox2); |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Gets or sets the title text of the <see cref="PageHeader"/>. |
| 28 | + /// </summary> |
| 29 | + public string? Title |
| 30 | + { |
| 31 | + get => GetValue(TitleProperty); |
| 32 | + set => SetValue(TitleProperty, value); |
| 33 | + } |
| 34 | + /// <summary> |
| 35 | + /// Gets or sets the description text of the <see cref="PageHeader"/>. |
| 36 | + /// </summary> |
| 37 | + public string? Description |
| 38 | + { |
| 39 | + get => GetValue(DescriptionProperty); |
| 40 | + set => SetValue(DescriptionProperty, value); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Gets or sets the icon of the <see cref="PageHeader"/>. |
| 45 | + /// </summary> |
| 46 | + public IconValue? Icon |
| 47 | + { |
| 48 | + get => GetValue(IconProperty); |
| 49 | + set => SetValue(IconProperty, value); |
| 50 | + } |
| 51 | + |
| 52 | + private TextBlock? _titleText; |
| 53 | + private TextBlock? _descriptionText; |
| 54 | + private UnifiedIcon? _unifiedIcon; |
| 55 | + |
| 56 | + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) |
| 57 | + { |
| 58 | + base.OnPropertyChanged(change); |
| 59 | + |
| 60 | + if (change.Property == TitleProperty) |
| 61 | + { |
| 62 | + UpdateTitle(change.GetNewValue<string?>()); |
| 63 | + } |
| 64 | + else if (change.Property == DescriptionProperty) |
| 65 | + { |
| 66 | + UpdateDescription(change.GetNewValue<string?>()); |
| 67 | + } |
| 68 | + else if (change.Property == IconProperty) |
| 69 | + { |
| 70 | + UpdateIcon(change.GetNewValue<IconValue?>()); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + protected override void OnApplyTemplate(TemplateAppliedEventArgs e) |
| 75 | + { |
| 76 | + _titleText = e.NameScope.Find<TextBlock>("TitleTextBlock"); |
| 77 | + if (_titleText != null) |
| 78 | + UpdateTitle(Title); |
| 79 | + |
| 80 | + _descriptionText = e.NameScope.Find<TextBlock>("DescriptionTextBlock"); |
| 81 | + if (_descriptionText != null) |
| 82 | + UpdateDescription(Description); |
| 83 | + |
| 84 | + _unifiedIcon = e.NameScope.Find<UnifiedIcon>("Icon"); |
| 85 | + if (_unifiedIcon != null) |
| 86 | + UpdateIcon(Icon); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Updates the visual title text of the <see cref="PageHeader"/>. |
| 92 | + /// </summary> |
| 93 | + /// <param name="newTitle">The new title text</param> |
| 94 | + private void UpdateTitle(string? newTitle) |
| 95 | + { |
| 96 | + if (_titleText != null) |
| 97 | + _titleText.Text = newTitle; |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Updates the visual title text of the <see cref="PageHeader"/>. |
| 102 | + /// </summary> |
| 103 | + /// <param name="newDescription">The new description text</param> |
| 104 | + private void UpdateDescription(string? newDescription) |
| 105 | + { |
| 106 | + if (_descriptionText != null) |
| 107 | + _descriptionText.Text = newDescription; |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Updates the visual icon of the <see cref="PageHeader"/>. |
| 112 | + /// </summary> |
| 113 | + /// <param name="newIcon">The new icon</param> |
| 114 | + private void UpdateIcon(IconValue? newIcon) |
| 115 | + { |
| 116 | + if (_unifiedIcon != null) |
| 117 | + _unifiedIcon.Value = newIcon; |
| 118 | + } |
| 119 | +} |
0 commit comments