Skip to content

Commit

Permalink
Better handling of attached properties, Can now pass a string and it …
Browse files Browse the repository at this point in the history
…will be parsed to an int or double.
  • Loading branch information
Epictek committed Nov 1, 2024
1 parent d7eb1a4 commit 939faf6
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 151 deletions.
75 changes: 60 additions & 15 deletions src/BlazorBindingsAvalonia.ComponentGenerator/GeneratedFieldInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,66 @@ public string GetRegisterAttachedPropertyDeclaration()
var effectiveBindingHostType = HostType.GetFullName();

var effectiveAvaloniaFieldName = AvaloniaFieldName.Replace("Property", "");

return $$"""
{{indent}} AttachedPropertyRegistry.RegisterAttachedPropertyHandler("{{ComponentName}}.{{ComponentFieldName}}",
{{indent}} (element, value) =>
{{indent}} {
{{indent}} if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{{indent}} {
{{indent}} element.ClearValue({{AvaloniaContainingTypeName}}.{{AvaloniaFieldName}});
{{indent}} }
{{indent}} else
{{indent}} {
{{indent}} {{AvaloniaContainingTypeName}}.Set{{effectiveAvaloniaFieldName}}(({{effectiveBindingHostType}})element, ({{GetOriginalAttachedPropertyType()}})value);
{{indent}} }
{{indent}} });
""";
var originalAttachedPropertyType = GetOriginalAttachedPropertyType();
return originalAttachedPropertyType switch
{
"int" => $$"""
{{indent}}AttachedPropertyRegistry.RegisterAttachedPropertyHandler("{{ComponentName}}.{{ComponentFieldName}}",
{{indent}} (element, value) =>
{{indent}} {
{{indent}} if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{{indent}} {
{{indent}} element.ClearValue({{AvaloniaContainingTypeName}}.{{AvaloniaFieldName}});
{{indent}} }
{{indent}} else
{{indent}} {
{{indent}} if (value is string s)
{{indent}} {
{{indent}} {{AvaloniaContainingTypeName}}.Set{{effectiveAvaloniaFieldName}}(({{effectiveBindingHostType}})element, int.Parse(s));
{{indent}} }
{{indent}} else
{{indent}} {
{{indent}} {{AvaloniaContainingTypeName}}.Set{{effectiveAvaloniaFieldName}}(({{effectiveBindingHostType}})element, ({{GetOriginalAttachedPropertyType()}})value);
{{indent}} }
{{indent}} }
{{indent}} });
""",
"double" => $$"""
{{indent}}AttachedPropertyRegistry.RegisterAttachedPropertyHandler("{{ComponentName}}.{{ComponentFieldName}}",
{{indent}} (element, value) =>
{{indent}} {
{{indent}} if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{{indent}} {
{{indent}} element.ClearValue({{AvaloniaContainingTypeName}}.{{AvaloniaFieldName}});
{{indent}} }
{{indent}} else
{{indent}} {
{{indent}} if (value is string s)
{{indent}} {
{{indent}} {{AvaloniaContainingTypeName}}.Set{{effectiveAvaloniaFieldName}}(({{effectiveBindingHostType}})element, double.Parse(s));
{{indent}} }
{{indent}} else
{{indent}} {
{{indent}} {{AvaloniaContainingTypeName}}.Set{{effectiveAvaloniaFieldName}}(({{effectiveBindingHostType}})element, ({{GetOriginalAttachedPropertyType()}})value);
{{indent}} }
{{indent}} }
{{indent}} });
""",
_ => $$"""
{{indent}} AttachedPropertyRegistry.RegisterAttachedPropertyHandler("{{ComponentName}}.{{ComponentFieldName}}",
{{indent}} (element, value) =>
{{indent}} {
{{indent}} if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{{indent}} {
{{indent}} element.ClearValue({{AvaloniaContainingTypeName}}.{{AvaloniaFieldName}});
{{indent}} }
{{indent}} else
{{indent}} {
{{indent}} {{AvaloniaContainingTypeName}}.Set{{effectiveAvaloniaFieldName}}(({{effectiveBindingHostType}})element, ({{GetOriginalAttachedPropertyType()}})value);
{{indent}} }
{{indent}} });
"""
};
}

public string GetExtensionMethodDeclaration()
Expand Down
68 changes: 48 additions & 20 deletions src/BlazorBindingsAvalonia/Elements/Canvas.generated.attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,82 @@ internal static class CanvasInitializer
[System.Runtime.CompilerServices.ModuleInitializer]
internal static void RegisterAdditionalHandlers()
{
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Canvas.Bottom",
(element, value) =>
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Canvas.Bottom",
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
element.ClearValue(AC.Canvas.BottomProperty);
}
else
{
if (value is string s)
{
element.ClearValue(AC.Canvas.BottomProperty);
AC.Canvas.SetBottom((Avalonia.AvaloniaObject)element, double.Parse(s));
}
else
{
AC.Canvas.SetBottom((Avalonia.AvaloniaObject)element, (double)value);
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Canvas.Left",
(element, value) =>
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Canvas.Left",
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
element.ClearValue(AC.Canvas.LeftProperty);
}
else
{
if (value is string s)
{
element.ClearValue(AC.Canvas.LeftProperty);
AC.Canvas.SetLeft((Avalonia.AvaloniaObject)element, double.Parse(s));
}
else
{
AC.Canvas.SetLeft((Avalonia.AvaloniaObject)element, (double)value);
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Canvas.Right",
(element, value) =>
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Canvas.Right",
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
element.ClearValue(AC.Canvas.RightProperty);
}
else
{
if (value is string s)
{
element.ClearValue(AC.Canvas.RightProperty);
AC.Canvas.SetRight((Avalonia.AvaloniaObject)element, double.Parse(s));
}
else
{
AC.Canvas.SetRight((Avalonia.AvaloniaObject)element, (double)value);
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Canvas.Top",
(element, value) =>
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Canvas.Top",
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
element.ClearValue(AC.Canvas.TopProperty);
}
else
{
if (value is string s)
{
element.ClearValue(AC.Canvas.TopProperty);
AC.Canvas.SetTop((Avalonia.AvaloniaObject)element, double.Parse(s));
}
else
{
AC.Canvas.SetTop((Avalonia.AvaloniaObject)element, (double)value);
}
});
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class DockPanelInitializer
internal static void RegisterAdditionalHandlers()
{
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("DockPanel.Dock",
(element, value) =>
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static class TextElementInitializer
internal static void RegisterAdditionalHandlers()
{
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("TextElement.FontFamily",
(element, value) =>
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
Expand All @@ -22,7 +22,7 @@ internal static void RegisterAdditionalHandlers()
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("TextElement.FontFeatures",
(element, value) =>
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
Expand All @@ -33,20 +33,27 @@ internal static void RegisterAdditionalHandlers()
ACD.TextElement.SetFontFeatures((Avalonia.Controls.Control)element, (global::Avalonia.Media.FontFeatureCollection)value);
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("TextElement.FontSize",
(element, value) =>
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("TextElement.FontSize",
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
element.ClearValue(ACD.TextElement.FontSizeProperty);
}
else
{
if (value is string s)
{
element.ClearValue(ACD.TextElement.FontSizeProperty);
ACD.TextElement.SetFontSize((Avalonia.Controls.Control)element, double.Parse(s));
}
else
{
ACD.TextElement.SetFontSize((Avalonia.Controls.Control)element, (double)value);
}
});
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("TextElement.FontStretch",
(element, value) =>
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
Expand All @@ -58,7 +65,7 @@ internal static void RegisterAdditionalHandlers()
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("TextElement.FontStyle",
(element, value) =>
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
Expand All @@ -70,7 +77,7 @@ internal static void RegisterAdditionalHandlers()
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("TextElement.FontWeight",
(element, value) =>
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
Expand All @@ -82,7 +89,7 @@ internal static void RegisterAdditionalHandlers()
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("TextElement.Foreground",
(element, value) =>
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
Expand Down
70 changes: 49 additions & 21 deletions src/BlazorBindingsAvalonia/Elements/Grid.generated.attachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,46 @@ internal static class GridInitializer
[System.Runtime.CompilerServices.ModuleInitializer]
internal static void RegisterAdditionalHandlers()
{
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Grid.Column",
(element, value) =>
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Grid.Column",
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
element.ClearValue(AC.Grid.ColumnProperty);
}
else
{
if (value is string s)
{
element.ClearValue(AC.Grid.ColumnProperty);
AC.Grid.SetColumn((Avalonia.Controls.Control)element, int.Parse(s));
}
else
{
AC.Grid.SetColumn((Avalonia.Controls.Control)element, (int)value);
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Grid.ColumnSpan",
(element, value) =>
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Grid.ColumnSpan",
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
element.ClearValue(AC.Grid.ColumnSpanProperty);
}
else
{
if (value is string s)
{
element.ClearValue(AC.Grid.ColumnSpanProperty);
AC.Grid.SetColumnSpan((Avalonia.Controls.Control)element, int.Parse(s));
}
else
{
AC.Grid.SetColumnSpan((Avalonia.Controls.Control)element, (int)value);
}
});
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Grid.IsSharedSizeScope",
(element, value) =>
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
Expand All @@ -44,30 +58,44 @@ internal static void RegisterAdditionalHandlers()
AC.Grid.SetIsSharedSizeScope((Avalonia.Controls.Control)element, (bool)value);
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Grid.Row",
(element, value) =>
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Grid.Row",
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
element.ClearValue(AC.Grid.RowProperty);
}
else
{
if (value is string s)
{
element.ClearValue(AC.Grid.RowProperty);
AC.Grid.SetRow((Avalonia.Controls.Control)element, int.Parse(s));
}
else
{
AC.Grid.SetRow((Avalonia.Controls.Control)element, (int)value);
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Grid.RowSpan",
(element, value) =>
}
});
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Grid.RowSpan",
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
element.ClearValue(AC.Grid.RowSpanProperty);
}
else
{
if (value is string s)
{
element.ClearValue(AC.Grid.RowSpanProperty);
AC.Grid.SetRowSpan((Avalonia.Controls.Control)element, int.Parse(s));
}
else
{
AC.Grid.SetRowSpan((Avalonia.Controls.Control)element, (int)value);
}
});
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class NativeMenuInitializer
internal static void RegisterAdditionalHandlers()
{
AttachedPropertyRegistry.RegisterAttachedPropertyHandler("NativeMenu.Menu",
(element, value) =>
(element, value) =>
{
if (value?.Equals(AvaloniaProperty.UnsetValue) == true)
{
Expand Down
Loading

0 comments on commit 939faf6

Please sign in to comment.