Skip to content

Commit

Permalink
Move MauiBlazorBindingsElementManager to BlazorBindings.Core
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper committed Aug 29, 2023
1 parent 84011a4 commit 746f597
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 60 deletions.
46 changes: 43 additions & 3 deletions src/BlazorBindings.Core/ElementManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,48 @@ namespace BlazorBindings.Core;
/// parent/child relationships, so each must implement this given the constraints
/// and requirements of their systems.
/// </summary>
public abstract class ElementManager
public class ElementManager
{
public abstract void AddChildElement(IElementHandler parentHandler, IElementHandler childHandler, int physicalSiblingIndex);
public abstract void RemoveChildElement(IElementHandler parentHandler, IElementHandler childHandler, int physicalSiblingIndex);
public virtual void AddChildElement(
IElementHandler parentHandler,
IElementHandler childHandler,
int physicalSiblingIndex)
{
if (childHandler is INonPhysicalChild nonPhysicalChild)
{
// If the child is a non-child container then we shouldn't try to add it to a parent.
// This is used in cases such as ModalContainer, which exists for the purposes of Blazor
// markup and is not represented in the Xamarin.Forms control hierarchy.

nonPhysicalChild.SetParent(parentHandler.TargetElement);
return;
}

if (parentHandler is not IContainerElementHandler parent)
{
throw new NotSupportedException($"Handler of type '{parentHandler.GetType().FullName}' representing element type " +
$"'{parentHandler.TargetElement?.GetType().FullName ?? "<null>"}' doesn't support adding a child " +
$"(child type is '{childHandler.TargetElement?.GetType().FullName}').");
}

parent.AddChild(childHandler.TargetElement, physicalSiblingIndex);
}

public virtual void RemoveChildElement(IElementHandler parentHandler, IElementHandler childHandler, int physicalSiblingIndex)
{
if (childHandler is INonPhysicalChild nonPhysicalChild)
{
nonPhysicalChild.RemoveFromParent(parentHandler.TargetElement);
}
else if (parentHandler is IContainerElementHandler parent)
{
parent.RemoveChild(childHandler.TargetElement, physicalSiblingIndex);
}
else
{
throw new NotSupportedException($"Handler of type '{parentHandler.GetType().FullName}' representing element type " +
$"'{parentHandler.TargetElement?.GetType().FullName ?? "<null>"}' doesn't support removing a child " +
$"(child type is '{childHandler.TargetElement?.GetType().FullName}').");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

namespace BlazorBindings.Maui;
namespace BlazorBindings.Core;

public interface IContainerElementHandler : IElementHandler
{
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorBindings.Core/NativeComponentRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public NativeComponentRenderer(IServiceProvider serviceProvider, ILoggerFactory
{
}

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

internal ElementManager ElementManager => _elementManager ??= CreateNativeControlManager();

Expand Down
50 changes: 0 additions & 50 deletions src/BlazorBindings.Maui/MauiBlazorBindingsElementManager.cs

This file was deleted.

5 changes: 0 additions & 5 deletions src/BlazorBindings.Maui/MauiBlazorBindingsRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ protected override void HandleException(Exception exception)
ExceptionDispatchInfo.Throw(exception);
}

protected override ElementManager CreateNativeControlManager()
{
return new MauiBlazorBindingsElementManager();
}

// It tries to return the Element as soon as it is available, therefore Component task might still be in progress.
internal async Task<(object Element, Task<IComponent> Component)> GetElementFromRenderedComponent(Type componentType, Dictionary<string, object> parameters = null)
{
Expand Down

0 comments on commit 746f597

Please sign in to comment.