Skip to content

Commit

Permalink
fix: Don't underline HyperlinkButton if not ImplicitTextBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 authored and MartinZikmund committed Sep 22, 2021
1 parent 5209b3a commit c1ef8c1
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Private.Infrastructure;
using Windows.UI.Text;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls.HyperlinkButtonTests
{
[TestClass]
public class Given_HyperlinkButton
{
[TestMethod]
[RunsOnUIThread]
public async Task When_HyperlinkButton_With_Implicit_Content_Should_Be_UnderlinedAsync()
{
var SUT = new HyperlinkButtonPage();
TestServices.WindowHelper.WindowContent = SUT;
await TestServices.WindowHelper.WaitForIdle();

var underlinedImplicitTextBlock = VisualTreeHelper.GetChild(SUT.ShouldBeUnderlinedHyperlinkButton.GetTemplateChild("ContentPresenter"), 0) as TextBlock;
Assert.IsInstanceOfType(underlinedImplicitTextBlock, typeof(ImplicitTextBlock));
Assert.AreEqual(TextDecorations.Underline, underlinedImplicitTextBlock.TextDecorations);

var notUnderlinedTextBlock = VisualTreeHelper.GetChild(SUT.ShouldNotBeUnderlinedHyperlinkButton.GetTemplateChild("ContentPresenter"), 0) as TextBlock;
Assert.IsNotInstanceOfType(notUnderlinedTextBlock, typeof(ImplicitTextBlock));
Assert.AreEqual(TextDecorations.None, notUnderlinedTextBlock.TextDecorations);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Page
x:Class="Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls.HyperlinkButtonTests.HyperlinkButtonPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls.HyperlinkButtonTests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<StackPanel>
<HyperlinkButton x:Name="ShouldBeUnderlined_HyperlinkButton">Should be underlined text</HyperlinkButton>
<HyperlinkButton x:Name="ShouldNotBeUnderlined_HyperlinkButton">
<TextBlock x:Name="ShouldNotBeUnderlined_TextBlock">Shouldn't be underlined text</TextBlock>
</HyperlinkButton>
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls.HyperlinkButtonTests
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class HyperlinkButtonPage : Page
{
public HyperlinkButton ShouldBeUnderlinedHyperlinkButton => ShouldBeUnderlined_HyperlinkButton;
public HyperlinkButton ShouldNotBeUnderlinedHyperlinkButton => ShouldNotBeUnderlined_HyperlinkButton;
public TextBlock ShouldNotBeUnderlinedTextBlock => ShouldNotBeUnderlined_TextBlock;

public HyperlinkButtonPage()
{
this.InitializeComponent();
}
}
}
8 changes: 8 additions & 0 deletions src/Uno.UI.RuntimeTests/Uno.UI.RuntimeTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@
</ProjectReference>
</ItemGroup>

<ItemGroup>
<None Remove="Tests\Windows_UI_Xaml_Controls\HyperlinkButtonTests\HyperlinkButtonPage.xaml" />
</ItemGroup>

<ItemGroup>
<UpToDateCheckInput Remove="Tests\Windows_UI_Xaml_Controls\HyperlinkButtonTests\HyperlinkButtonPage.xaml" />
</ItemGroup>

<ItemGroup>
<Compile Include="Tests\Windows_UI_Xaml_Controls\HtmlElementAttributeTests\Given_HtmlElementAttribute.Wasm.cs" />
</ItemGroup>
Expand Down
23 changes: 20 additions & 3 deletions src/Uno.UI/UI/Xaml/Controls/Button/HyperlinkButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,26 @@ public HyperlinkButton()
DefaultStyleKey = typeof(HyperlinkButton);
}

/// <summary>
/// Gets or sets the Uniform Resource Identifier (URI) to navigate to when the HyperlinkButton is clicked.
/// </summary>
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

// This differs from UWP, where it looks for a template child named "ContentPresenter",
// but ultimately sets the underline on the TextBlock of the first ContentPresenter with a {TemplateBinding Content}.
// UWP also doesn't seem to do this in OnApplyTemplate (it's done between the first Measure and the first Arrange).
if (GetTemplateChild("ContentPresenter") is ContentPresenter contentPresenter)
{
// Forces ContentPresenter to materialize its template.
contentPresenter.Measure(new Size(0, 0));
if (VisualTreeHelper.GetChildrenCount(contentPresenter) == 1 && VisualTreeHelper.GetChild(contentPresenter, 0) is ImplicitTextBlock textBlock)
{
textBlock.TextDecorations = Windows.UI.Text.TextDecorations.Underline;
}
}
}

#region NavigateUri

public Uri NavigateUri
{
get => (Uri)GetValue(NavigateUriProperty);
Expand Down

0 comments on commit c1ef8c1

Please sign in to comment.