Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeepClone: Use sourcegenerator to fix trimming issues #32

Merged
merged 2 commits into from
Sep 19, 2024
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Components;
using MudBlazor.State;
using MudBlazor.ThemeManager.Extensions;
using System.Diagnostics.CodeAnalysis;

namespace MudBlazor.ThemeManager;

Expand Down Expand Up @@ -49,7 +48,6 @@ public MudThemeManager()
[Parameter]
public EventCallback<ThemeManagerTheme> ThemeChanged { get; set; }

[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
protected override void OnInitialized()
{
base.OnInitialized();
Expand Down
96 changes: 86 additions & 10 deletions src/MudBlazor.ThemeManager/Extensions/Extension.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,98 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json;

namespace MudBlazor.ThemeManager.Extensions;

internal static class Extension
{
private static readonly JsonSerializerOptions JsonOptions = new();
private static readonly PaletteSerializerContext PaletteSerializerContext = new();
private static readonly ThemeSerializerContext ThemeSerializerContext = new();

[RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
public static T? DeepClone<T>(this T source)
public static MudTheme DeepClone(this MudTheme source)
{
if (source is not null)
// TODO: Needs this to be done https://github.com/MudBlazor/MudBlazor/pull/9434
//var themeType = typeof(MudTheme);
//var serializeStr = JsonSerializer.Serialize(source, themeType, ThemeSerializerContext);
//var copyObj = (MudTheme?)JsonSerializer.Deserialize(serializeStr, themeType, ThemeSerializerContext);

//return copyObj;

// Code below is a workaround for the above issue

return new MudTheme
{
PaletteDark = source.PaletteDark.DeepClone() ?? new PaletteDark(),
PaletteLight = source.PaletteLight.DeepClone() ?? new PaletteLight(),
Shadows = DeepCloneTheme(source.Shadows) ?? new Shadow(),
LayoutProperties = DeepCloneTheme(source.LayoutProperties) ?? new LayoutProperties(),
ZIndex = DeepCloneTheme(source.ZIndex) ?? new ZIndex(),
PseudoCss = DeepCloneTheme(source.PseudoCss) ?? new PseudoCss(),
// Exception case
Typography = new Typography
{
Default = DeepCloneBaseTypography(source.Typography.Default),
H1 = DeepCloneBaseTypography(source.Typography.H1),
H2 = DeepCloneBaseTypography(source.Typography.H2),
H3 = DeepCloneBaseTypography(source.Typography.H3),
H4 = DeepCloneBaseTypography(source.Typography.H4),
H5 = DeepCloneBaseTypography(source.Typography.H5),
H6 = DeepCloneBaseTypography(source.Typography.H6),
Subtitle1 = DeepCloneBaseTypography(source.Typography.Subtitle1),
Subtitle2 = DeepCloneBaseTypography(source.Typography.Subtitle2),
Body1 = DeepCloneBaseTypography(source.Typography.Body1),
Body2 = DeepCloneBaseTypography(source.Typography.Body2),
Input = DeepCloneBaseTypography(source.Typography.Input),
Button = DeepCloneBaseTypography(source.Typography.Button),
Caption = DeepCloneBaseTypography(source.Typography.Caption),
Overline = DeepCloneBaseTypography(source.Typography.Overline)
}
};
}

public static PaletteDark? DeepClone(this PaletteDark source) => DeepClonePalette(source);

public static PaletteLight? DeepClone(this PaletteLight source) => DeepClonePalette(source);

private static T? DeepClonePalette<T>(T source) where T : Palette
{
var paletteType = typeof(T);
var serializeStr = JsonSerializer.Serialize(source, paletteType, PaletteSerializerContext);
var copyObj = (T?)JsonSerializer.Deserialize(serializeStr, paletteType, PaletteSerializerContext);

return copyObj;
}

private static T? DeepCloneTheme<T>(T source) where T : class
{
var paletteType = typeof(T);
var serializeStr = JsonSerializer.Serialize(source, paletteType, ThemeSerializerContext);
var copyObj = (T?)JsonSerializer.Deserialize(serializeStr, paletteType, ThemeSerializerContext);

return copyObj;
}

private static T DeepCloneBaseTypography<T>(T baseTypography) where T : BaseTypography, new()
{
string[] fontFamilyCloned = new string[baseTypography.FontFamily?.Length ?? 0];
if (baseTypography.FontFamily is not null)
{
var serializeStr = JsonSerializer.Serialize(source, JsonOptions);
var copyObj = JsonSerializer.Deserialize<T>(serializeStr, JsonOptions);
return copyObj;
Array.Copy(baseTypography.FontFamily, fontFamilyCloned, baseTypography.FontFamily.Length);
}

return default;

var fontWeightCloned = baseTypography.FontWeight;
var fontSizeCloned = baseTypography.FontSize;
var lineHeightCloned = baseTypography.LineHeight;
var letterSpacingCloned = baseTypography.LetterSpacing;
var textTransformCloned = baseTypography.TextTransform;

return new T
{
FontWeight = fontWeightCloned,
FontFamily = fontFamilyCloned,
FontSize = fontSizeCloned,
LineHeight = lineHeightCloned,
LetterSpacing = letterSpacingCloned,
TextTransform = textTransformCloned,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Text.Json.Serialization;

namespace MudBlazor.ThemeManager.Extensions;

[JsonSerializable(typeof(Palette))]
[JsonSerializable(typeof(PaletteDark))]
[JsonSerializable(typeof(PaletteLight))]
internal sealed partial class PaletteSerializerContext : JsonSerializerContext;
11 changes: 11 additions & 0 deletions src/MudBlazor.ThemeManager/Extensions/ThemeSerializerContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;

namespace MudBlazor.ThemeManager.Extensions;


//[JsonSerializable(typeof(MudTheme))] TODO: Needs this to be done https://github.com/MudBlazor/MudBlazor/pull/9434 rest can be removed after
[JsonSerializable(typeof(Shadow))]
[JsonSerializable(typeof(LayoutProperties))]
[JsonSerializable(typeof(ZIndex))]
[JsonSerializable(typeof(PseudoCss))]
internal sealed partial class ThemeSerializerContext : JsonSerializerContext;
1 change: 1 addition & 0 deletions src/MudBlazor.ThemeManager/MudBlazor.ThemeManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsTrimmable>true</IsTrimmable>
Expand Down
Loading