Skip to content

Commit

Permalink
choe: refactor mutable interface (#2340)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeylonSantana authored Jul 24, 2024
1 parent e492c22 commit 09bf475
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
9 changes: 3 additions & 6 deletions Intersect.Client.Framework/Interface/IMutableInterface.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using Intersect.Client.Framework.Gwen.Control;
using Intersect.Client.Framework.Gwen.Control;

namespace Intersect.Client.Interface;


public interface IMutableInterface
{

List<Base> Children { get; }

TElement Create<TElement>(params object[] parameters) where TElement : Base;

TElement Find<TElement>(string name = null, bool recurse = false) where TElement : Base;
TElement? Find<TElement>(string? name = null, bool recurse = false) where TElement : Base;

IEnumerable<TElement> FindAll<TElement>(bool recurse = false) where TElement : Base;
IEnumerable<TElement?> FindAll<TElement>(bool recurse = false) where TElement : Base;

void Remove<TElement>(TElement element, bool dispose = false) where TElement : Base;

}
60 changes: 30 additions & 30 deletions Intersect.Client/Interface/MutableInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace Intersect.Client.Interface;


public abstract partial class MutableInterface : IMutableInterface
{

private static DebugWindow _debugWindow;
private static DebugWindow? _debugWindow;

public static void DetachDebugWindow()
{
Expand All @@ -18,7 +16,10 @@ public static void DetachDebugWindow()
}
}

internal static void DisposeDebugWindow() => _debugWindow?.Dispose();
internal static void DisposeDebugWindow()
{
_debugWindow?.Dispose();
}

private static void EnsureDebugWindowInitialized(Base parent)
{
Expand All @@ -33,19 +34,18 @@ private static void EnsureDebugWindowInitialized(Base parent)
protected internal MutableInterface(Base root)
{
Root = root;

EnsureDebugWindowInitialized(root);
}

internal Base Root { get; }

/// <inheritdoc />
public List<Base> Children => Root.Children ?? new List<Base>();
public List<Base> Children => Root.Children ?? [];

/// <inheritdoc />
public TElement Create<TElement>(params object[] parameters) where TElement : Base
{
var fullParameterList = new List<object?> {Root};
var fullParameterList = new List<object?> { Root };
fullParameterList.AddRange(parameters);

var fullParameters = fullParameterList.ToArray();
Expand All @@ -56,46 +56,46 @@ public TElement Create<TElement>(params object[] parameters) where TElement : Ba
var constructorParameters = constructor.GetParameters();
if (fullParameters.Length != constructorParameters.Length)
{
fullParameters = fullParameters.Concat(
Enumerable.Range(0, constructorParameters.Length - fullParameters.Length)
.Select(_ => default(object))
)
.ToArray();
}

if (constructor.Invoke(fullParameters) is not TElement constructedElement)
{
throw new NullReferenceException("Failed to invoke constructor that matches parameters.");
fullParameters =
[
.. fullParameters,
.. Enumerable.Range(0, constructorParameters.Length - fullParameters.Length)
.Select(_ => default(object))
,
];
}

return constructedElement;
return constructor.Invoke(fullParameters) is not TElement constructedElement
? throw new NullReferenceException("Failed to invoke constructor that matches parameters.")
: constructedElement;
}

throw new NullReferenceException("Failed to find constructor that matches parameters.");
}

/// <inheritdoc />
public TElement Find<TElement>(string name = null, bool recurse = false) where TElement : Base
public TElement? Find<TElement>(string? name = null, bool recurse = false) where TElement : Base
{
if (!string.IsNullOrWhiteSpace(name))
{
return Root.FindChildByName(name, recurse) as TElement;
}

return Root.Find(child => child is TElement) as TElement;
return !string.IsNullOrWhiteSpace(name)
? Root.FindChildByName(name, recurse) as TElement
: Root.Find(child => child is TElement) as TElement;
}

/// <inheritdoc />
public IEnumerable<TElement> FindAll<TElement>(bool recurse = false) where TElement : Base =>
Root.FindAll(child => child is TElement, recurse).Select(child => child as TElement);
public IEnumerable<TElement?> FindAll<TElement>(bool recurse = false) where TElement : Base
{
return Root.FindAll(child => child is TElement, recurse).Select(child => child as TElement);
}

/// <inheritdoc />
public void Remove<TElement>(TElement element, bool dispose = false) where TElement : Base =>
public void Remove<TElement>(TElement element, bool dispose = false) where TElement : Base
{
Root.RemoveChild(element, dispose);
}

public static bool ToggleDebug()
{
_debugWindow.ToggleHidden();
return _debugWindow.IsVisible;
_debugWindow?.ToggleHidden();
return _debugWindow?.IsVisible ?? false;
}
}

0 comments on commit 09bf475

Please sign in to comment.