Skip to content

Commit

Permalink
perf: Use GeneratedDependencyProperty for commonly read properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Feb 13, 2024
1 parent 99a4fb2 commit 99c97ba
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private record GenerationCandidateData
public bool IsCallbackWithDPChangedArgs { get; }
public bool IsCallbackWithDPChangedArgsOnly { get; }
public bool CallBackWithOldAndNew { get; }
public bool IsParameterlessCallback { get; }
public bool IsInvalidChangedCallbackName { get; }

public string? MemberSymbolNodeContent { get; }
Expand Down Expand Up @@ -163,6 +164,10 @@ public GenerationCandidateData(ISymbol dpSymbol, AttributeData attribute)
{
CallBackWithOldAndNew = true;
}
else if (propertyChangedMethods.Any(m => m.Parameters.Length == 0))
{
IsParameterlessCallback = true;
}
else
{
IsInvalidChangedCallbackName = true;
Expand Down Expand Up @@ -387,6 +392,10 @@ private static void GenerateAttachedProperty(IndentedStringBuilder builder, Gene
{
builder.AppendLineIndented($"\t\t, propertyChangedCallback: (instance, args) => {changedCallbackName}(({propertyTypeName})args.OldValue, ({propertyTypeName})args.NewValue)");
}
else if (data.IsParameterlessCallback)
{
builder.AppendLineIndented($"\t\t, propertyChangedCallback: (instance, args) => {changedCallbackName}()");
}
else if (data.IsInvalidChangedCallbackName)
{
builder.AppendLineIndented($"#error Valid {changedCallbackName} not found. Must be {changedCallbackName}(DependencyPropertyChangedEventArgs), {changedCallbackName}(Instance, DependencyPropertyChangedEventArgs) or {changedCallbackName}(oldValue, newValue)");
Expand Down Expand Up @@ -486,6 +495,10 @@ private static void GenerateProperty(IndentedStringBuilder builder, GenerationCa
{
builder.AppendLineIndented($"\t\t, propertyChangedCallback: (instance, args) => (({containingTypeName})instance).{changedCallbackName}(({propertyTypeName})args.OldValue, ({propertyTypeName})args.NewValue)");
}
else if (data.IsParameterlessCallback)
{
builder.AppendLineIndented($"\t\t, propertyChangedCallback: (instance, args) => (({containingTypeName})instance).{changedCallbackName}()");
}
else if (data.IsInvalidChangedCallbackName)
{
builder.AppendLineIndented($"#error Valid {changedCallbackName} not found. Must be {changedCallbackName}(DependencyPropertyChangedEventArgs), {changedCallbackName}(Instance, DependencyPropertyChangedEventArgs) or {changedCallbackName}(oldValue, newValue)");
Expand Down
17 changes: 5 additions & 12 deletions src/Uno.UI/UI/Xaml/Controls/TextBlock/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

using RadialGradientBrush = Microsoft/* UWP don't rename */.UI.Xaml.Media.RadialGradientBrush;
using Uno.UI.Helpers;
using Uno.UI.Xaml;

#if __IOS__
using UIKit;
Expand Down Expand Up @@ -515,20 +516,12 @@ public bool IsTextSelectionEnabled

public new TextAlignment TextAlignment
{
get => (TextAlignment)GetValue(TextAlignmentProperty);
set => SetValue(TextAlignmentProperty, value);
get => GetTextAlignmentValue();
set => SetTextAlignmentValue(value);
}

public static DependencyProperty TextAlignmentProperty { get; } =
DependencyProperty.Register(
"TextAlignment",
typeof(TextAlignment),
typeof(TextBlock),
new FrameworkPropertyMetadata(
defaultValue: TextAlignment.Left,
propertyChangedCallback: (s, e) => ((TextBlock)s).OnTextAlignmentChanged()
)
);
[GeneratedDependencyProperty(DefaultValue = TextAlignment.Left, ChangedCallback = true, ChangedCallbackName = nameof(OnTextAlignmentChanged))]
public static DependencyProperty TextAlignmentProperty { get; } = CreateTextAlignmentProperty();

private void OnTextAlignmentChanged()
{
Expand Down
36 changes: 10 additions & 26 deletions src/Uno.UI/UI/Xaml/Documents/TextElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,29 +141,22 @@ protected virtual void OnFontSizeChanged()

public Brush Foreground
{
get { return (Brush)this.GetValue(ForegroundProperty); }
get => GetForegroundValue();
set
{
if (value != null && !(value is SolidColorBrush))
{
throw new InvalidOperationException("Specified brush is not a SolidColorBrush");
}

this.SetValue(ForegroundProperty, value);
SetForegroundValue(value);
}
}

public static DependencyProperty ForegroundProperty { get; } =
DependencyProperty.Register(
"Foreground",
typeof(Brush),
typeof(TextElement),
new FrameworkPropertyMetadata(
defaultValue: SolidColorBrushHelper.Black,
options: FrameworkPropertyMetadataOptions.Inherits,
propertyChangedCallback: (s, e) => ((TextElement)s).OnForegroundChanged()
)
);
[GeneratedDependencyProperty(Options = FrameworkPropertyMetadataOptions.Inherits, ChangedCallback = true, ChangedCallbackName = nameof(OnForegroundChanged))]
public static DependencyProperty ForegroundProperty { get; } = CreateForegroundProperty();

private static Brush GetForegroundDefaultValue() => SolidColorBrushHelper.Black;

protected virtual void OnForegroundChanged()
{
Expand Down Expand Up @@ -237,21 +230,12 @@ protected virtual void OnCharacterSpacingChanged()

public TextDecorations TextDecorations
{
get { return (TextDecorations)GetValue(TextDecorationsProperty); }
set { SetValue(TextDecorationsProperty, value); }
get => GetTextDecorationsValue();
set => SetTextDecorationsValue(value);
}

public static DependencyProperty TextDecorationsProperty { get; } =
DependencyProperty.Register(
nameof(TextDecorations),
typeof(TextDecorations),
typeof(TextElement),
new FrameworkPropertyMetadata(
defaultValue: TextDecorations.None,
options: FrameworkPropertyMetadataOptions.Inherits,
propertyChangedCallback: (s, e) => ((TextElement)s).OnTextDecorationsChanged()
)
);
[GeneratedDependencyProperty(DefaultValue = TextDecorations.None, Options = FrameworkPropertyMetadataOptions.Inherits, ChangedCallback = true, ChangedCallbackName = nameof(OnTextDecorationsChanged))]
public static DependencyProperty TextDecorationsProperty { get; } = CreateTextDecorationsProperty();

protected virtual void OnTextDecorationsChanged()
{
Expand Down
20 changes: 7 additions & 13 deletions src/Uno.UI/UI/Xaml/Media/SolidColorBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Text;
using Uno.Extensions;
using Uno.UI.Xaml;
using Windows.UI;

#if __ANDROID__
Expand Down Expand Up @@ -79,21 +80,14 @@ protected override void OnOpacityChanged(double oldValue, double newValue)

public Windows.UI.Color Color
{
get { return (Windows.UI.Color)this.GetValue(ColorProperty); }
set { this.SetValue(ColorProperty, value); }
get => GetColorValue();
set => SetColorValue(value);
}

// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static DependencyProperty ColorProperty { get; } =
DependencyProperty.Register(
"Color",
typeof(Windows.UI.Color),
typeof(SolidColorBrush),
new FrameworkPropertyMetadata(
Colors.Transparent,
(s, e) => ((SolidColorBrush)s).OnColorChanged((Windows.UI.Color)e.OldValue, (Windows.UI.Color)e.NewValue)
)
);
[GeneratedDependencyProperty(ChangedCallback = true, ChangedCallbackName = nameof(OnColorChanged))]
public static DependencyProperty ColorProperty { get; } = CreateColorProperty();

private static Windows.UI.Color GetColorDefaultValue() => Colors.Transparent;

partial void OnColorChanged(Windows.UI.Color oldValue, Windows.UI.Color newValue);

Expand Down

0 comments on commit 99c97ba

Please sign in to comment.