Skip to content

Commit

Permalink
Implement more properties in WinUI LabelHandler (#891)
Browse files Browse the repository at this point in the history
* Implement more properties in WinUI LabelHandler

* Updated WinUI LabelHandler

* Fix build error

* Updated Windows AlignmentExtensions

Co-authored-by: Rui Marinho <me@ruimarinho.net>
  • Loading branch information
jsuarezruiz and rmarinho authored May 18, 2021
1 parent 4d17f4e commit aef35c3
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/WinUI/AlignmentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Microsoft.Maui.Controls.Compatibility.Platform.UWP
{
internal static class AlignmentExtensions
{
[PortHandler]
internal static Microsoft.UI.Xaml.TextAlignment ToNativeTextAlignment(this TextAlignment alignment, EffectiveFlowDirection flowDirection = default(EffectiveFlowDirection))
{
var isLtr = flowDirection.IsLeftToRight();
Expand Down
3 changes: 3 additions & 0 deletions src/Compatibility/Core/src/WinUI/LabelRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ void UpdateTextDecorations(TextBlock textBlock)

}

[PortHandler("Partially ported")]
void UpdateAlign(TextBlock textBlock)
{
_perfectSizeValid = false;
Expand Down Expand Up @@ -274,6 +275,7 @@ void UpdateFont(TextBlock textBlock)
_fontApplied = true;
}

[PortHandler]
void UpdateLineBreakMode(TextBlock textBlock)
{
_perfectSizeValid = false;
Expand Down Expand Up @@ -385,6 +387,7 @@ void UpdateDetectReadingOrderFromContent(TextBlock textBlock)
}
}

[PortHandler]
void UpdateLineHeight(TextBlock textBlock)
{
if (textBlock == null)
Expand Down
14 changes: 6 additions & 8 deletions src/Core/src/Handlers/Label/LabelHandler.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable enable
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

Expand Down Expand Up @@ -32,11 +30,11 @@ public static void MapFont(LabelHandler handler, ILabel label)
handler.RealNativeView?.UpdateFont(label, fontManager);
}

[MissingMapper]
public static void MapHorizontalTextAlignment(LabelHandler handler, ILabel label) { }
public static void MapHorizontalTextAlignment(LabelHandler handler, ILabel label) =>
handler.RealNativeView?.UpdateHorizontalTextAlignment(label);

[MissingMapper]
public static void MapLineBreakMode(LabelHandler handler, ILabel label) { }
public static void MapLineBreakMode(LabelHandler handler, ILabel label) =>
handler.RealNativeView?.UpdateLineBreakMode(label);

public static void MapTextDecorations(LabelHandler handler, ILabel label) =>
handler.RealNativeView?.UpdateTextDecorations(label);
Expand All @@ -47,7 +45,7 @@ public static void MapMaxLines(LabelHandler handler, ILabel label) =>
public static void MapPadding(LabelHandler handler, ILabel label) =>
handler.RealNativeView?.UpdatePadding(label);

[MissingMapper]
public static void MapLineHeight(LabelHandler handler, ILabel label) { }
public static void MapLineHeight(LabelHandler handler, ILabel label) =>
handler.RealNativeView?.UpdateLineHeight(label);
}
}
23 changes: 21 additions & 2 deletions src/Core/src/Platform/Windows/AlignmentExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml;

namespace Microsoft.Maui
{
Expand All @@ -16,5 +16,24 @@ public static HorizontalAlignment ToNativeHorizontalAlignment(this TextAlignment
return HorizontalAlignment.Left;
}
}

public static UI.Xaml.TextAlignment ToNative(this TextAlignment alignment, bool isLtr = true)
{
switch (alignment)
{
case TextAlignment.Center:
return UI.Xaml.TextAlignment.Center;
case TextAlignment.End:
if (isLtr)
return UI.Xaml.TextAlignment.Right;
else
return UI.Xaml.TextAlignment.Left;
default:
if (isLtr)
return UI.Xaml.TextAlignment.Left;
else
return UI.Xaml.TextAlignment.Right;
}
}
}
}
}
57 changes: 57 additions & 0 deletions src/Core/src/Platform/Windows/TextBlockExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Documents;

Expand Down Expand Up @@ -70,5 +72,60 @@ public static void UpdateTextDecorations(this TextBlock nativeControl, ILabel la
nativeControl.Text = nativeControl.Text;
}
}

public static void UpdateLineHeight(this TextBlock nativeControl, ILabel label)
{
if (label.LineHeight >= 0)
{
nativeControl.LineHeight = label.LineHeight * nativeControl.FontSize;
}
}

public static void UpdateHorizontalTextAlignment(this TextBlock nativeControl, ILabel label)
{
// We don't have a FlowDirection yet, so there's nothing to pass in here.
// TODO: Update this when FlowDirection is available
nativeControl.TextAlignment = label.HorizontalTextAlignment.ToNative(true);
}

public static void UpdateLineBreakMode(this TextBlock nativeControl, ILabel label)
{
var lineBreakMode = label.LineBreakMode;

switch (lineBreakMode)
{
case LineBreakMode.NoWrap:
nativeControl.TextTrimming = TextTrimming.Clip;
nativeControl.TextWrapping = TextWrapping.NoWrap;
break;
case LineBreakMode.WordWrap:
nativeControl.TextTrimming = TextTrimming.None;
nativeControl.TextWrapping = TextWrapping.Wrap;
break;
case LineBreakMode.CharacterWrap:
nativeControl.TextTrimming = TextTrimming.WordEllipsis;
nativeControl.TextWrapping = TextWrapping.Wrap;
break;
case LineBreakMode.HeadTruncation:
// TODO: This truncates at the end.
nativeControl.TextTrimming = TextTrimming.WordEllipsis;
nativeControl.DetermineTruncatedTextWrapping();
break;
case LineBreakMode.TailTruncation:
nativeControl.TextTrimming = TextTrimming.CharacterEllipsis;
nativeControl.DetermineTruncatedTextWrapping();
break;
case LineBreakMode.MiddleTruncation:
// TODO: This truncates at the end.
nativeControl.TextTrimming = TextTrimming.WordEllipsis;
nativeControl.DetermineTruncatedTextWrapping();
break;
default:
throw new ArgumentOutOfRangeException();
}
}

internal static void DetermineTruncatedTextWrapping(this TextBlock textBlock) =>
textBlock.TextWrapping = textBlock.MaxLines > 1 ? TextWrapping.Wrap : TextWrapping.NoWrap;
}
}

0 comments on commit aef35c3

Please sign in to comment.