Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 89 additions & 30 deletions src/Wpf.Ui/Controls/TextBlock/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,124 @@ namespace Wpf.Ui.Controls;
/// </summary>
public class TextBlock : System.Windows.Controls.TextBlock
{
/// <summary>Identifies the <see cref="FontTypography"/> dependency property.</summary>
/// <summary>Identifies the <see cref="FontTypographyStyle" /> dependency property.</summary>
internal static readonly DependencyProperty FontTypographyStyleProperty = DependencyProperty.Register(
nameof(FontTypographyStyle),
typeof(Style),
typeof(System.Windows.Controls.TextBlock),
new PropertyMetadata(default(Style))
);

/// <summary>Identifies the <see cref="AppearanceForeground" /> dependency property.</summary>
internal static readonly DependencyProperty AppearanceForegroundProperty = DependencyProperty.Register(
nameof(AppearanceForeground),
typeof(Brush),
typeof(System.Windows.Controls.TextBlock),
new PropertyMetadata(default(Brush))
);

/// <summary>Identifies the <see cref="FontTypography" /> dependency property.</summary>
public static readonly DependencyProperty FontTypographyProperty = DependencyProperty.Register(
nameof(FontTypography),
typeof(FontTypography),
typeof(TextBlock),
new PropertyMetadata(
FontTypography.Body,
static (o, args) =>
{
((TextBlock)o).OnFontTypographyChanged((FontTypography)args.NewValue);
}
typeof(FontTypography?),
typeof(System.Windows.Controls.TextBlock),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.AffectsMeasure
| FrameworkPropertyMetadataOptions.AffectsRender
| FrameworkPropertyMetadataOptions.Inherits,
OnFontTypographyChanged
)
);

/// <summary>Identifies the <see cref="Appearance"/> dependency property.</summary>
/// <summary>Identifies the <see cref="Appearance" /> dependency property.</summary>
public static readonly DependencyProperty AppearanceProperty = DependencyProperty.Register(
nameof(Appearance),
typeof(TextColor),
typeof(TextBlock),
new PropertyMetadata(
TextColor.Primary,
static (o, args) =>
{
((TextBlock)o).OnAppearanceChanged((TextColor)args.NewValue);
}
typeof(TextColor?),
typeof(System.Windows.Controls.TextBlock),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.Inherits,
OnAppearanceChanged
)
);

public TextBlock()
static TextBlock() => TextBlockMetadata.Initialize();

/// <summary>
/// Gets or sets the <see cref="AppearanceForeground" /> of the text.
/// </summary>
internal Brush? AppearanceForeground
{
get { return (Brush?)GetValue(AppearanceForegroundProperty); }
set { SetValue(AppearanceForegroundProperty, value); }
}

/// <summary>
/// Gets or sets the <see cref="FontTypographyStyle" /> of the text.
/// </summary>
internal Style? FontTypographyStyle
{
var defaultFontTypography = (FontTypography)FontTypographyProperty.DefaultMetadata.DefaultValue;
SetResourceReference(StyleProperty, defaultFontTypography.ToResourceValue());
get { return (Style?)GetValue(FontTypographyStyleProperty); }
set { SetValue(FontTypographyStyleProperty, value); }
}

/// <summary>
/// Gets or sets the <see cref="FontTypography"/> of the text.
/// Gets or sets the <see cref="FontTypography" /> of the text.
/// </summary>
public FontTypography FontTypography
public FontTypography? FontTypography
{
get => (FontTypography)GetValue(FontTypographyProperty);
get => (FontTypography?)GetValue(FontTypographyProperty);
set => SetValue(FontTypographyProperty, value);
}

/// <summary>
/// Gets or sets the color of the text.
/// </summary>
public TextColor Appearance
public TextColor? Appearance
{
get => (TextColor)GetValue(AppearanceProperty);
get => (TextColor?)GetValue(AppearanceProperty);
set => SetValue(AppearanceProperty, value);
}

private void OnFontTypographyChanged(FontTypography newTypography)
private static void OnFontTypographyChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
{
SetResourceReference(StyleProperty, newTypography.ToResourceValue());
if (o is System.Windows.Controls.TextBlock tb)
{
if (args.NewValue is FontTypography fontTypography)
{
tb.SetCurrentValue(
FontTypographyStyleProperty,
tb.TryFindResource(fontTypography.ToResourceValue())
);
}
else
{
tb.ClearValue(FontTypographyStyleProperty);
}

tb.CoerceValue(FontSizeProperty);
tb.CoerceValue(FontWeightProperty);
}
}

private void OnAppearanceChanged(TextColor textColor)
private static void OnAppearanceChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
{
SetResourceReference(ForegroundProperty, textColor.ToResourceValue());
if (o is System.Windows.Controls.TextBlock tb)
{
if (args.NewValue is TextColor textColor)
{
tb.SetCurrentValue(
AppearanceForegroundProperty,
tb.TryFindResource(textColor.ToResourceValue())
);
}
else
{
tb.ClearValue(AppearanceForegroundProperty);
}

tb.CoerceValue(ForegroundProperty);
}
}
}
15 changes: 1 addition & 14 deletions src/Wpf.Ui/Controls/TextBlock/TextBlock.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style TargetType="{x:Type TextBlock}">

<!--<Setter Property="Foreground" Value="{DynamicResource TextFillColorPrimaryBrush}"/>-->

<!-- The Display option causes a large aliasing effect -->
<!--<Setter Property="TextOptions.TextFormattingMode" Value="Ideal" />-->
<Setter Property="Background" Value="Transparent" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="Focusable" Value="False" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="OverridesDefaultStyle" Value="True" />
</Style>
<Style TargetType="{x:Type TextBlock}" />

</ResourceDictionary>
83 changes: 83 additions & 0 deletions src/Wpf.Ui/Controls/TextBlock/TextBlockMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

namespace Wpf.Ui.Controls;

internal static class TextBlockMetadata
{
static TextBlockMetadata()
{
System.Windows.Controls.TextBlock.FontSizeProperty.OverrideMetadata(
typeof(System.Windows.Controls.TextBlock),
new FrameworkPropertyMetadata(
14d,
null,
static (d, value) =>
{
if (d.GetValue(TextBlock.FontTypographyStyleProperty) is Style style)
{
foreach (SetterBase setterBase in style.Setters)
{
if (
setterBase is Setter setter
&& setter.Property == System.Windows.Controls.TextBlock.FontSizeProperty
)
{
return setter.Value;
}
}
}

return value;
}
)
);

System.Windows.Controls.TextBlock.FontWeightProperty.OverrideMetadata(
typeof(System.Windows.Controls.TextBlock),
new FrameworkPropertyMetadata(
FontWeights.Regular,
null,
static (d, value) =>
{
if (d.GetValue(TextBlock.FontTypographyStyleProperty) is Style style)
{
foreach (SetterBase setterBase in style.Setters)
{
if (
setterBase is Setter setter
&& setter.Property == System.Windows.Controls.TextBlock.FontWeightProperty
)
{
return setter.Value;
}
}
}

return value;
}
)
);

System.Windows.Controls.TextBlock.ForegroundProperty.OverrideMetadata(
typeof(System.Windows.Controls.TextBlock),
new FrameworkPropertyMetadata(
Brushes.Black,
null,
static (d, value) =>
{
if (d.GetValue(TextBlock.AppearanceForegroundProperty) is Brush brush)
{
return brush;
}

return value is Brush ? value : Brushes.Black;
}
)
);
}

public static void Initialize() { }
}
2 changes: 2 additions & 0 deletions src/Wpf.Ui/Markup/ControlsDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// All Rights Reserved.

using System.Windows.Markup;
using Wpf.Ui.Controls;

namespace Wpf.Ui.Markup;

Expand Down Expand Up @@ -38,5 +39,6 @@ public class ControlsDictionary : ResourceDictionary
public ControlsDictionary()
{
Source = new Uri(DictionaryUri, UriKind.Absolute);
TextBlockMetadata.Initialize();
}
}
8 changes: 0 additions & 8 deletions src/Wpf.Ui/Resources/Typography.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="14" />
<Setter Property="LineHeight" Value="20" />
<Setter Property="FontWeight" Value="Regular" />
</Style>

Expand All @@ -11,7 +10,6 @@
BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="12" />
<Setter Property="LineHeight" Value="16" />
<Setter Property="FontWeight" Value="Regular" />
</Style>

Expand All @@ -20,7 +18,6 @@
BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="14" />
<Setter Property="LineHeight" Value="20" />
<Setter Property="FontWeight" Value="Regular" />
</Style>

Expand All @@ -29,7 +26,6 @@
BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="14" />
<Setter Property="LineHeight" Value="20" />
<Setter Property="FontWeight" Value="SemiBold" />
</Style>

Expand All @@ -38,7 +34,6 @@
BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="20" />
<Setter Property="LineHeight" Value="28" />
<Setter Property="FontWeight" Value="SemiBold" />
</Style>

Expand All @@ -47,7 +42,6 @@
BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="28" />
<Setter Property="LineHeight" Value="36" />
<Setter Property="FontWeight" Value="SemiBold" />
</Style>

Expand All @@ -56,7 +50,6 @@
BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="40" />
<Setter Property="LineHeight" Value="52" />
<Setter Property="FontWeight" Value="SemiBold" />
</Style>

Expand All @@ -65,7 +58,6 @@
BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="68" />
<Setter Property="LineHeight" Value="92" />
<Setter Property="FontWeight" Value="SemiBold" />
</Style>

Expand Down
Loading