|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.Specialized; |
| 7 | +using System.Text; |
| 8 | +using Windows.UI.Xaml; |
| 9 | +using Windows.UI.Xaml.Automation; |
| 10 | +using Windows.UI.Xaml.Automation.Peers; |
| 11 | +using Windows.UI.Xaml.Controls; |
| 12 | +using Windows.UI.Xaml.Documents; |
| 13 | + |
| 14 | +namespace Microsoft.Toolkit.Uwp.UI.Controls |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Display <see cref="MetadataItem"/>s separated by bullets. |
| 18 | + /// </summary> |
| 19 | + [TemplatePart(Name = TextContainerPart, Type = typeof(TextBlock))] |
| 20 | + public sealed class MetadataControl : Control |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// The DP to store the <see cref="Separator"/> property value. |
| 24 | + /// </summary> |
| 25 | + public static readonly DependencyProperty SeparatorProperty = DependencyProperty.Register( |
| 26 | + nameof(Separator), |
| 27 | + typeof(string), |
| 28 | + typeof(MetadataControl), |
| 29 | + new PropertyMetadata(" • ", OnPropertyChanged)); |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// The DP to store the <see cref="AccessibleSeparator"/> property value. |
| 33 | + /// </summary> |
| 34 | + public static readonly DependencyProperty AccessibleSeparatorProperty = DependencyProperty.Register( |
| 35 | + nameof(AccessibleSeparator), |
| 36 | + typeof(string), |
| 37 | + typeof(MetadataControl), |
| 38 | + new PropertyMetadata(", ", OnPropertyChanged)); |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// The DP to store the <see cref="Items"/> property value. |
| 42 | + /// </summary> |
| 43 | + public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register( |
| 44 | + nameof(Items), |
| 45 | + typeof(IEnumerable<MetadataItem>), |
| 46 | + typeof(MetadataControl), |
| 47 | + new PropertyMetadata(null, OnMetadataItemsChanged)); |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// The DP to store the TextBlockStyle value. |
| 51 | + /// </summary> |
| 52 | + public static readonly DependencyProperty TextBlockStyleProperty = DependencyProperty.Register( |
| 53 | + nameof(TextBlockStyle), |
| 54 | + typeof(Style), |
| 55 | + typeof(MetadataControl), |
| 56 | + new PropertyMetadata(null)); |
| 57 | + |
| 58 | + private const string TextContainerPart = "TextContainer"; |
| 59 | + |
| 60 | + private TextBlock _textContainer; |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Initializes a new instance of the <see cref="MetadataControl"/> class. |
| 64 | + /// </summary> |
| 65 | + public MetadataControl() |
| 66 | + { |
| 67 | + DefaultStyleKey = typeof(MetadataControl); |
| 68 | + ActualThemeChanged += OnActualThemeChanged; |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets or sets the separator to display between the <see cref="MetadataItem"/>. |
| 73 | + /// </summary> |
| 74 | + public string Separator |
| 75 | + { |
| 76 | + get => (string)GetValue(SeparatorProperty); |
| 77 | + set => SetValue(SeparatorProperty, value); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Gets or sets the separator that will be used to generate the accessible string representing the control content. |
| 82 | + /// </summary> |
| 83 | + public string AccessibleSeparator |
| 84 | + { |
| 85 | + get => (string)GetValue(AccessibleSeparatorProperty); |
| 86 | + set => SetValue(AccessibleSeparatorProperty, value); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Gets or sets the <see cref="MetadataItem"/> to display in the control. |
| 91 | + /// If it implements <see cref="INotifyCollectionChanged"/>, the control will automatically update itself. |
| 92 | + /// </summary> |
| 93 | + public IEnumerable<MetadataItem> Items |
| 94 | + { |
| 95 | + get => (IEnumerable<MetadataItem>)GetValue(ItemsProperty); |
| 96 | + set => SetValue(ItemsProperty, value); |
| 97 | + } |
| 98 | + |
| 99 | + /// <summary> |
| 100 | + /// Gets or sets the <see cref="Style"/> to use on the inner <see cref="TextBlock"/> control. |
| 101 | + /// </summary> |
| 102 | + public Style TextBlockStyle |
| 103 | + { |
| 104 | + get => (Style)GetValue(TextBlockStyleProperty); |
| 105 | + set => SetValue(TextBlockStyleProperty, value); |
| 106 | + } |
| 107 | + |
| 108 | + /// <inheritdoc/> |
| 109 | + protected override void OnApplyTemplate() |
| 110 | + { |
| 111 | + _textContainer = GetTemplateChild(TextContainerPart) as TextBlock; |
| 112 | + Update(); |
| 113 | + } |
| 114 | + |
| 115 | + private static void OnMetadataItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 116 | + { |
| 117 | + var control = (MetadataControl)d; |
| 118 | + void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args) => control.Update(); |
| 119 | + |
| 120 | + if (e.OldValue is INotifyCollectionChanged oldNcc) |
| 121 | + { |
| 122 | + oldNcc.CollectionChanged -= OnCollectionChanged; |
| 123 | + } |
| 124 | + |
| 125 | + if (e.NewValue is INotifyCollectionChanged newNcc) |
| 126 | + { |
| 127 | + newNcc.CollectionChanged += OnCollectionChanged; |
| 128 | + } |
| 129 | + |
| 130 | + control.Update(); |
| 131 | + } |
| 132 | + |
| 133 | + private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 134 | + => ((MetadataControl)d).Update(); |
| 135 | + |
| 136 | + private void OnActualThemeChanged(FrameworkElement sender, object args) => Update(); |
| 137 | + |
| 138 | + private void Update() |
| 139 | + { |
| 140 | + if (_textContainer is null) |
| 141 | + { |
| 142 | + // The template is not ready yet. |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + _textContainer.Inlines.Clear(); |
| 147 | + |
| 148 | + if (Items is null) |
| 149 | + { |
| 150 | + AutomationProperties.SetName(_textContainer, string.Empty); |
| 151 | + NotifyLiveRegionChanged(); |
| 152 | + return; |
| 153 | + } |
| 154 | + |
| 155 | + Inline unitToAppend; |
| 156 | + var accessibleString = new StringBuilder(); |
| 157 | + foreach (var unit in Items) |
| 158 | + { |
| 159 | + if (_textContainer.Inlines.Count > 0) |
| 160 | + { |
| 161 | + _textContainer.Inlines.Add(new Run { Text = Separator }); |
| 162 | + accessibleString.Append(AccessibleSeparator ?? Separator); |
| 163 | + } |
| 164 | + |
| 165 | + unitToAppend = new Run |
| 166 | + { |
| 167 | + Text = unit.Label, |
| 168 | + }; |
| 169 | + |
| 170 | + if (unit.Command != null) |
| 171 | + { |
| 172 | + var hyperLink = new Hyperlink |
| 173 | + { |
| 174 | + UnderlineStyle = UnderlineStyle.None, |
| 175 | + Foreground = _textContainer.Foreground, |
| 176 | + }; |
| 177 | + hyperLink.Inlines.Add(unitToAppend); |
| 178 | + |
| 179 | + void OnHyperlinkClicked(Hyperlink sender, HyperlinkClickEventArgs args) |
| 180 | + { |
| 181 | + if (unit.Command.CanExecute(unit.CommandParameter)) |
| 182 | + { |
| 183 | + unit.Command.Execute(unit.CommandParameter); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + hyperLink.Click += OnHyperlinkClicked; |
| 188 | + |
| 189 | + unitToAppend = hyperLink; |
| 190 | + } |
| 191 | + |
| 192 | + var unitAccessibleLabel = unit.AccessibleLabel ?? unit.Label; |
| 193 | + AutomationProperties.SetName(unitToAppend, unitAccessibleLabel); |
| 194 | + accessibleString.Append(unitAccessibleLabel); |
| 195 | + |
| 196 | + _textContainer.Inlines.Add(unitToAppend); |
| 197 | + } |
| 198 | + |
| 199 | + AutomationProperties.SetName(_textContainer, accessibleString.ToString()); |
| 200 | + NotifyLiveRegionChanged(); |
| 201 | + } |
| 202 | + |
| 203 | + private void NotifyLiveRegionChanged() |
| 204 | + { |
| 205 | + if (AutomationPeer.ListenerExists(AutomationEvents.LiveRegionChanged)) |
| 206 | + { |
| 207 | + var peer = FrameworkElementAutomationPeer.FromElement(this); |
| 208 | + peer?.RaiseAutomationEvent(AutomationEvents.LiveRegionChanged); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | +} |
0 commit comments