Skip to content

Commit

Permalink
Add workaround for MudTheme
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarletKuro committed Sep 19, 2024
1 parent 855c174 commit e4b16d8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 30 deletions.
96 changes: 73 additions & 23 deletions src/MudBlazor.ThemeManager/Extensions/Extension.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
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)
{
var serializeStr = JsonSerializer.Serialize(source, JsonOptions);
var copyObj = JsonSerializer.Deserialize<T>(serializeStr, JsonOptions);
return copyObj;
}

return default;
}
// 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);

// TODO: Needs this to be done https://github.com/MudBlazor/MudBlazor/pull/9434
//private static readonly ThemeSerializerContext ThemeSerializerContext = new();
//return copyObj;

//public static MudTheme? DeepClone(this MudTheme source)
//{
// var themeType = typeof(MudTheme);
// var serializeStr = JsonSerializer.Serialize(source, themeType, ThemeSerializerContext);
// var copyObj = (MudTheme?)JsonSerializer.Deserialize(serializeStr, themeType, ThemeSerializerContext);
// Code below is a workaround for the above issue

// return copyObj;
//}
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);

Expand All @@ -45,4 +60,39 @@ internal static class Extension

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)
{
Array.Copy(baseTypography.FontFamily, fontFamilyCloned, baseTypography.FontFamily.Length);
}


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
Expand Up @@ -5,6 +5,4 @@ namespace MudBlazor.ThemeManager.Extensions;
[JsonSerializable(typeof(Palette))]
[JsonSerializable(typeof(PaletteDark))]
[JsonSerializable(typeof(PaletteLight))]
internal sealed partial class PaletteSerializerContext : JsonSerializerContext
{
}
internal sealed partial class PaletteSerializerContext : JsonSerializerContext;
10 changes: 6 additions & 4 deletions src/MudBlazor.ThemeManager/Extensions/ThemeSerializerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace MudBlazor.ThemeManager.Extensions;

//[JsonSerializable(typeof(MudTheme))]

//internal sealed partial class ThemeSerializerContext : JsonSerializerContext
//{
//}
//[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

0 comments on commit e4b16d8

Please sign in to comment.