Skip to content

Commit

Permalink
Some cleanup and newer lang constructs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper committed Nov 6, 2024
1 parent 97e0613 commit 3b6e4ee
Show file tree
Hide file tree
Showing 44 changed files with 126 additions and 228 deletions.
6 changes: 3 additions & 3 deletions samples/BlazorBindingsXaminals/Data/BearData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace BlazorBindingsXaminals.Data;

public static class BearData
{
public static IList<Animal> Bears { get; } = new List<Animal>
{
public static IList<Animal> Bears { get; } =
[
new Animal
{
Name = "American Black Bear",
Expand Down Expand Up @@ -104,5 +104,5 @@ public static class BearData
Details = "The California grizzly bear is an extinct subspecies of the grizzly bear, the very large North American brown bear. Grizzly could have meant grizzled (that is, with golden and grey tips of the hair) or fear-inspiring. Nonetheless, after careful study, naturalist George Ord formally classified it in 1815 – not for its hair, but for its character – as Ursus horribilis (terrifying bear). Genetically, North American grizzlies are closely related; in size and coloring, the California grizzly bear was much like the grizzly bear of the southern coast of Alaska. In California, it was particularly admired for its beauty, size and strength. The grizzly became a symbol of the Bear Flag Republic, a moniker that was attached to the short-lived attempt by a group of American settlers to break away from Mexico in 1846. Later, this rebel flag became the basis for the state flag of California, and then California was known as the Bear State.",
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/d/de/Monarch_the_bear.jpg"
}
};
];
}
6 changes: 3 additions & 3 deletions samples/BlazorBindingsXaminals/Data/CatData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace BlazorBindingsXaminals.Data;

public static class CatData
{
public static IList<Animal> Cats { get; } = new List<Animal>
{
public static IList<Animal> Cats { get; } =
[
new Animal
{
Name = "Abyssinian",
Expand Down Expand Up @@ -96,5 +96,5 @@ public static class CatData
Details = "The Sphynx cat is a breed of cat known for its lack of coat (fur). It was developed through selective breeding, starting in the 1960s. The skin should have the texture of chamois, as it has fine hairs, or they may be completely hairless. Whiskers may be present, either whole or broken, or may be totally absent. They also have a narrow, long head, and webbed feet. Their skin is the color that their fur would be, and all the usual cat markings (solid, point, van, tabby, tortie, etc.) may be found on the Sphynx cat's skin. Because they have no coat, they lose more body heat than coated cats. This makes them warm to the touch as well as heat-seeking.",
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Sphinx2_July_2006.jpg/180px-Sphinx2_July_2006.jpg"
}
};
];
}
6 changes: 3 additions & 3 deletions samples/BlazorBindingsXaminals/Data/DogData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace BlazorBindingsXaminals.Data;

public static class DogData
{
public static IList<Animal> Dogs { get; } = new List<Animal>
{
public static IList<Animal> Dogs { get; } =
[
new Animal
{
Name = "Afghan Hound",
Expand Down Expand Up @@ -104,5 +104,5 @@ public static class DogData
Details = "The St. Bernard or St Bernard is a breed of very large working dog from the western Alps in Italy and Switzerland. They were originally bred for rescue by the hospice of the Great St Bernard Pass on the Italian-Swiss border. The hospice, built by and named after Italian monk Bernard of Menthon, acquired its first dogs between 1660 and 1670. The breed has become famous through tales of alpine rescues, as well as for its enormous size.",
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Hummel_Vedor_vd_Robandahoeve.jpg/320px-Hummel_Vedor_vd_Robandahoeve.jpg"
}
};
];
}
6 changes: 3 additions & 3 deletions samples/BlazorBindingsXaminals/Data/ElephantData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace BlazorBindingsXaminals.Data;

public static class ElephantData
{
public static IList<Animal> Elephants { get; } = new List<Animal>
{
public static IList<Animal> Elephants { get; } =
[
new Animal
{
Name = "African Bush Elephant",
Expand Down Expand Up @@ -104,5 +104,5 @@ public static class ElephantData
Details = "The pygmy mammoth or Channel Islands mammoth is an extinct species of dwarf elephant descended from the Columbian mammoth of mainland North America. This species became extinct during the Quaternary extinction event in which many megafauna species became extinct due to changing conditions to which the species could not adapt. A case of island or insular dwarfism, from a recent analysis in 2010 it was determined that M. exilis was on average, 1.72 m (5.6 ft) tall at the shoulders and 760 kg (1,680 lb) in weight, in stark contrast to its 4.3 m (14 ft) tall, 9,070 kg (20,000 lb) ancestor. Another estimate gives a shoulder height of 2.02 m (6.6 ft) and a weight of 1,350 kg (2,980 lb).",
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/f/f6/Mammuthus_exilis.jpg"
}
};
];
}
6 changes: 3 additions & 3 deletions samples/BlazorBindingsXaminals/Data/MonkeyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace BlazorBindingsXaminals.Data;

public static class MonkeyData
{
public static IList<Animal> Monkeys { get; } = new List<Animal>
{
public static IList<Animal> Monkeys { get; } =
[
new Animal
{
Name = "Baboon",
Expand Down Expand Up @@ -144,5 +144,5 @@ public static class MonkeyData
Details = "The gelada, sometimes called the bleeding-heart monkey or the gelada baboon, is a species of Old World monkey found only in the Ethiopian Highlands, with large populations in the Semien Mountains. Theropithecus is derived from the Greek root words for \"beast-ape.\" Like its close relatives the baboons, it is largely terrestrial, spending much of its time foraging in grasslands.",
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/1/13/Gelada-Pavian.jpg/320px-Gelada-Pavian.jpg"
}
};
];
}
4 changes: 1 addition & 3 deletions samples/ControlGallery/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ public static MauiApp CreateMauiApp()
}
}

public class App : BlazorBindingsApplication<AppShell>
public class App(IServiceProvider services) : BlazorBindingsApplication<AppShell>(services)
{
public App(IServiceProvider services) : base(services) { }

public override Type WrapperComponentType => typeof(Root);
}
10 changes: 1 addition & 9 deletions samples/FluxorSample/Store/FetchDataResultAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,4 @@

namespace FluxorSample.Store;

public class FetchDataResultAction
{
public IEnumerable<WeatherForecast> Forecasts { get; }

public FetchDataResultAction(IEnumerable<WeatherForecast> forecasts)
{
Forecasts = forecasts;
}
}
public record FetchDataResultAction(IEnumerable<WeatherForecast> Forecasts);
9 changes: 1 addition & 8 deletions samples/FluxorSample/Store/WeatherUseCase/Effects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@

namespace FluxorSample.Store.WeatherUseCase;

public class Effects
public class Effects(HttpClient http)
{
private readonly HttpClient http;

public Effects(HttpClient http)
{
this.http = http;
}

[EffectMethod]
public async Task HandleFetchDataAction(FetchDataAction action, IDispatcher dispatcher)
{
Expand Down
5 changes: 1 addition & 4 deletions samples/HybridSample/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

namespace HybridSample;

public class App : BlazorBindingsApplication<MainPage>
public class App(IServiceProvider services) : BlazorBindingsApplication<MainPage>(services)
{
public App(IServiceProvider services) : base(services)
{
}
}
6 changes: 1 addition & 5 deletions samples/ThirdPartyControlsSample/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@

namespace ThirdPartyControlsSample;

public class App : BlazorBindingsApplication<AppShell>
public class App(IServiceProvider services) : BlazorBindingsApplication<AppShell>(services)
{
public App(IServiceProvider services) : base(services)
{
}

protected override void Configure()
{
Resources.Add(new MaterialStyles());
Expand Down
2 changes: 1 addition & 1 deletion samples/ThirdPartyControlsSample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static MauiApp CreateMauiApp()
.UseMauiApp<App>()
.UseMauiBlazorBindings()
.UseMauiCommunityToolkit()
.UseMaterialComponents(new() { "OpenSans-Regular.ttf" })
.UseMaterialComponents(["OpenSans-Regular.ttf"])
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
Expand Down
5 changes: 1 addition & 4 deletions samples/ValidationsSample/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace ValidationsSample;

public class App : BlazorBindingsApplication<LoginPage>
public class App(IServiceProvider services) : BlazorBindingsApplication<LoginPage>(services)
{
public App(IServiceProvider services) : base(services)
{
}
}
27 changes: 11 additions & 16 deletions src/BlazorBindings.Core/NativeComponentAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ namespace BlazorBindings.Core;
/// Represents a "shadow" item that Blazor uses to map changes into the live native UI tree.
/// </summary>
[DebuggerDisplay("{DebugName}")]
internal sealed class NativeComponentAdapter : IDisposable
internal sealed class NativeComponentAdapter(
NativeComponentRenderer renderer,
NativeComponentAdapter closestPhysicalParent,
IElementHandler knownTargetElement = null)
: IDisposable
{
public NativeComponentAdapter(NativeComponentRenderer renderer, NativeComponentAdapter closestPhysicalParent, IElementHandler knownTargetElement = null)
{
Renderer = renderer ?? throw new ArgumentNullException(nameof(renderer));
_closestPhysicalParent = closestPhysicalParent;
_targetElement = knownTargetElement;
}


/// <summary>
/// Used for debugging purposes.
/// </summary>
Expand All @@ -45,14 +41,13 @@ private string Text
public int DeepLevel { get; init; }

public NativeComponentAdapter Parent { get; private set; }
public List<NativeComponentAdapter> Children { get; } = new();
public List<NativeComponentAdapter> Children { get; } = [];

private readonly NativeComponentAdapter _closestPhysicalParent;
private IElementHandler _targetElement;
private IElementHandler _targetElement = knownTargetElement;

private NativeComponentAdapter PhysicalTarget => _targetElement != null ? this : _closestPhysicalParent;
private NativeComponentAdapter PhysicalTarget => _targetElement != null ? this : closestPhysicalParent;

public NativeComponentRenderer Renderer { get; }
public NativeComponentRenderer Renderer { get; } = renderer ?? throw new ArgumentNullException(nameof(renderer));

private List<PendingEdit> _pendingEdits;

Expand Down Expand Up @@ -146,7 +141,7 @@ public void ApplyPendingEdits()

private void AddPendingRemoval(NativeComponentAdapter childToRemove, int index, HashSet<NativeComponentAdapter> adaptersWithPendingEdits)
{
var targetEdits = PhysicalTarget._pendingEdits ??= new();
var targetEdits = PhysicalTarget._pendingEdits ??= [];
adaptersWithPendingEdits.Add(PhysicalTarget);

if (targetEdits.Count == 0)
Expand Down Expand Up @@ -192,7 +187,7 @@ private void AddPendingRemoval(NativeComponentAdapter childToRemove, int index,

private void AddPendingAddition(NativeComponentAdapter childToAdd, int index, HashSet<NativeComponentAdapter> adaptersWithPendingEdits)
{
var targetEdits = PhysicalTarget._pendingEdits ??= new();
var targetEdits = PhysicalTarget._pendingEdits ??= [];
targetEdits.Add(new(EditType.Add, index, childToAdd));
adaptersWithPendingEdits.Add(PhysicalTarget);
}
Expand Down
17 changes: 7 additions & 10 deletions src/BlazorBindings.Core/NativeComponentRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@

namespace BlazorBindings.Core;

public abstract class NativeComponentRenderer : Renderer
public abstract class NativeComponentRenderer
(IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
: Renderer(serviceProvider, loggerFactory)
{
private readonly Dictionary<int, NativeComponentAdapter> _componentIdToAdapter = new();
private readonly List<(int Id, IComponent Component)> _rootComponents = new();
private readonly Dictionary<int, NativeComponentAdapter> _componentIdToAdapter = [];
private readonly List<(int Id, IComponent Component)> _rootComponents = [];
private ElementManager _elementManager;

public NativeComponentRenderer(IServiceProvider serviceProvider, ILoggerFactory loggerFactory)
: base(serviceProvider, loggerFactory)
{
}

protected virtual ElementManager CreateNativeControlManager() => new();

internal ElementManager ElementManager => _elementManager ??= CreateNativeControlManager();
Expand Down Expand Up @@ -47,7 +44,7 @@ public NativeComponentRenderer(IServiceProvider serviceProvider, ILoggerFactory
/// <param name="parameters"></param>
/// <returns></returns>
public async Task<IComponent> AddComponent(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type componentType,
IElementHandler parent,
Dictionary<string, object> parameters = null)
{
Expand Down Expand Up @@ -91,7 +88,7 @@ public void RemoveRootComponent(IComponent component)

protected override Task UpdateDisplayAsync(in RenderBatch renderBatch)
{
HashSet<NativeComponentAdapter> adaptersWithPendingEdits = new();
HashSet<NativeComponentAdapter> adaptersWithPendingEdits = [];

var numUpdatedComponents = renderBatch.UpdatedComponents.Count;
for (var componentIndex = 0; componentIndex < numUpdatedComponents; componentIndex++)
Expand Down
16 changes: 4 additions & 12 deletions src/BlazorBindings.Core/TextSpanContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,11 @@ namespace BlazorBindings.Core;
/// Helper class for types that accept inline text spans. This type collects text spans
/// and returns the string represented by the contained text spans.
/// </summary>
public class TextSpanContainer
public class TextSpanContainer(bool trimWhitespace = true)
{
private readonly List<string> _textSpans = new();
private readonly List<string> _textSpans = [];

public TextSpanContainer(bool trimWhitespace = true)
{
TrimWhitespace = trimWhitespace;
}

public bool TrimWhitespace { get; }
public bool TrimWhitespace { get; } = trimWhitespace;

/// <summary>
/// Updates the text spans with the new text at the new index and returns the new
Expand All @@ -27,10 +22,7 @@ public TextSpanContainer(bool trimWhitespace = true)
/// <returns></returns>
public string GetUpdatedText(int index, string text)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException(nameof(index));
}
ArgumentOutOfRangeException.ThrowIfNegative(index);

if (index >= _textSpans.Count)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ public class GenerateComponentSettings
public string FileHeader { get; set; }
public string TypeAlias { get; set; }
public INamedTypeSymbol TypeSymbol { get; set; }
public HashSet<string> Exclude { get; set; } = new();
public HashSet<string> Include { get; set; } = new();
public HashSet<string> ContentProperties { get; set; } = new();
public HashSet<string> NonContentProperties { get; set; } = new();
public string[] PropertyChangedEvents { get; set; } = Array.Empty<string>();
public Dictionary<string, INamedTypeSymbol> GenericProperties { get; set; } = new();
public Dictionary<string, string> Aliases { get; set; } = new();
public HashSet<string> Exclude { get; set; } = [];
public HashSet<string> Include { get; set; } = [];
public HashSet<string> ContentProperties { get; set; } = [];
public HashSet<string> NonContentProperties { get; set; } = [];
public string[] PropertyChangedEvents { get; set; } = [];
public Dictionary<string, INamedTypeSymbol> GenericProperties { get; set; } = [];
public Dictionary<string, string> Aliases { get; set; } = [];
public GenerateComponentSettings BaseTypeInfo { get; set; }
public bool IsBaseTypeGeneric => BaseTypeInfo?.IsGeneric ?? false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ private static bool IsObsolete(ISymbol symbol)
return symbol.GetAttributes().Any(a => a.AttributeClass.Name == nameof(ObsoleteAttribute));
}

private static readonly List<string> DisallowedComponentTypes = new()
{
private static readonly List<string> DisallowedComponentTypes =
[
"Microsoft.Maui.Controls.Button.ButtonContentLayout", // TODO: This is temporary; should be possible to add support later
"Microsoft.Maui.Controls.ColumnDefinitionCollection",
"Microsoft.Maui.Controls.PointCollection",
Expand All @@ -289,7 +289,7 @@ private static bool IsObsolete(ISymbol symbol)
"Microsoft.Maui.Graphics.IShape",
"Microsoft.Maui.IView",
"Microsoft.Maui.IViewHandler"
};
];

private string GetTypeNameAndAddNamespace(ITypeSymbol type)
{
Expand Down
27 changes: 7 additions & 20 deletions src/BlazorBindings.Maui.ComponentGenerator/GeneratedTypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@

namespace BlazorBindings.Maui.ComponentGenerator;

public class GeneratedTypeInfo
public record GeneratedTypeInfo(
Compilation Compilation,
GenerateComponentSettings Settings,
string TypeName,
string BaseTypeName,
ITypeSymbol MauiType,
IList<UsingStatement> Usings)
{
public GeneratedTypeInfo(Compilation compilation, GenerateComponentSettings settings, string typeName, string baseTypeName, ITypeSymbol mauiType, IList<UsingStatement> usings)
{
TypeName = typeName;
BaseTypeName = baseTypeName;
IsGeneric = settings.IsGeneric;
MauiType = mauiType;
Usings = usings;
Compilation = compilation;
Settings = settings;
}

public string TypeName { get; }
public string BaseTypeName { get; }
public bool IsGeneric { get; }
public Compilation Compilation { get; }
public GenerateComponentSettings Settings { get; }
public ITypeSymbol MauiType { get; }
public IList<UsingStatement> Usings { get; }

public string GetTypeNameAndAddNamespace(string @namespace, string typeName)
{
// Adding random usings might cause conflicts with global usings.
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorBindings.Maui/AttachedPropertyRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace BlazorBindings.Maui;
[Experimental("MBB001")]
public static class AttachedPropertyRegistry
{
internal static readonly Dictionary<string, Action<BindableObject, object>> AttachedPropertyHandlers = new();
internal static readonly Dictionary<string, Action<BindableObject, object>> AttachedPropertyHandlers = [];

public static void RegisterAttachedPropertyHandler(string propertyName, Action<BindableObject, object> handler)
{
Expand Down
Loading

0 comments on commit 3b6e4ee

Please sign in to comment.