| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | |
| 3 | using Microsoft.UI.Xaml; |
| 4 | using Microsoft.UI.Xaml.Controls; |
| 5 | |
| 6 | namespace WslSettings.Controls; |
| 7 | |
| 8 | /// <summary> |
| 9 | /// Custom control for a text block that contains a hyperlink in the text. |
| 10 | /// </summary> |
| 11 | /// <remarks> |
| 12 | /// The <see cref="Text"/> property must contain a substring enclosed in square |
| 13 | /// brackets ('[' and ']'). When displaying the text block, the brackets are |
| 14 | /// removed and the text inside is made into a link that points to <see cref="NavigateUri"/> |
| 15 | /// </remarks> |
| 16 | public sealed partial class HyperlinkTextBlock : UserControl |
| 17 | { |
| 18 | /// <summary> |
| 19 | /// Gets or sets the text for the text block. This must contain |
| 20 | /// a substring contained within square brackets ('[', ']') |
| 21 | /// </summary> |
| 22 | public string Text |
| 23 | { |
| 24 | get => (string)GetValue(TextProperty); |
| 25 | set |
| 26 | { |
| 27 | SetValue(TextProperty, value); |
| 28 | |
| 29 | var openingBracketIndex = value.IndexOf('['); |
| 30 | var closingBracketIndex = value.IndexOf(']'); |
| 31 | |
| 32 | if (openingBracketIndex == -1 || closingBracketIndex == -1 |
| 33 | || openingBracketIndex > closingBracketIndex) |
| 34 | { |
| 35 | // If there is not string contained between brackets, show the text as is |
| 36 | TextBeforeHyperlink = value; |
| 37 | HyperLinkText = string.Empty; |
| 38 | TextAfterHyperlink = string.Empty; |
| 39 | } |
| 40 | else |
| 41 | { |
| 42 | TextBeforeHyperlink = value.Substring(0, openingBracketIndex); |
| 43 | HyperLinkText = value.Substring(openingBracketIndex + 1, closingBracketIndex - openingBracketIndex - 1); |
| 44 | TextAfterHyperlink = value.Substring(closingBracketIndex + 1); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | public string NavigateUri |
| 50 | { |
| 51 | get => (string)GetValue(NavigateUriProperty); |
| 52 | set => SetValue(NavigateUriProperty, value); |
| 53 | } |
| 54 | |
| 55 | internal string TextBeforeHyperlink { get; private set; } = string.Empty; |
| 56 | |
| 57 | internal string HyperLinkText { get; private set; } = string.Empty; |
| 58 | |
| 59 | internal string TextAfterHyperlink { get; private set; } = string.Empty; |
| 60 | |
| 61 | public HyperlinkTextBlock() |
| 62 | { |
| 63 | InitializeComponent(); |
| 64 | } |
| 65 | |
| 66 | public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(HyperlinkTextBlock), new PropertyMetadata(string.Empty)); |
| 67 | public static readonly DependencyProperty NavigateUriProperty = DependencyProperty.Register(nameof(NavigateUri), typeof(string), typeof(HyperlinkTextBlock), new PropertyMetadata(string.Empty)); |
| 68 | } |