add control manager for plugins

This commit is contained in:
Pilzinsel64
2024-11-15 11:03:29 +01:00
parent b7acc6f0a4
commit 6abaa8615a
7 changed files with 145 additions and 54 deletions

View File

@@ -0,0 +1,10 @@
namespace Pilz.Plugins.Advanced.UI;
public enum ControlExecuteReason
{
None,
Init,
Dispose,
Add,
Remove,
}

View File

@@ -2,6 +2,8 @@
public abstract class ControlFeature : PluginFeature
{
public bool ControlNameRegEx { get; }
public string? ControlName { get; }
public ControlFeature(string? controlName, string type, string identifier) : base(type, identifier)
@@ -14,5 +16,5 @@ public abstract class ControlFeature : PluginFeature
ControlName = controlName;
}
public abstract void Execute(Control control, PluginFunctionParameter? parameter);
public abstract void Execute(Control control, ControlExecuteReason reason, PluginFunctionParameter? parameter);
}

View File

@@ -1,17 +1,110 @@
namespace Pilz.Plugins.Advanced.UI;
using Pilz.Plugins.Advanced.UI.Extensions;
using System.Text.RegularExpressions;
public class ControlManager(PluginFeatureController controller, bool autoTrack)
namespace Pilz.Plugins.Advanced.UI;
internal class ControlManager(IEnumerable<ControlFeature> features, Control control, bool recursive) : IControlManager
{
public PluginFeatureController Controller => controller;
public bool AutoTrack => autoTrack;
private bool isTracking;
public void RegisterControl(Control control)
public bool Recursive => recursive;
public Control Control => control;
public IEnumerable<ControlFeature> Features => features;
internal void Track()
{
if (isTracking)
return;
Track(control);
isTracking = true;
}
public void Execute(object mode)
protected void Track(Control control)
{
control.Disposed += Control_Disposed;
control.ControlAdded += Control_ControlAdded;
control.ControlRemoved += Control_ControlRemoved;
if (!recursive)
return;
foreach (var child in control.Controls)
{
if (child is Control childControl)
Track(childControl);
}
}
private void Control_Disposed(object? sender, EventArgs e)
{
if (sender is not Control control)
return;
Execute(control, ControlExecuteReason.Dispose, null, false);
control.Disposed -= Control_Disposed;
control.ControlAdded -= Control_ControlAdded;
control.ControlRemoved -= Control_ControlRemoved;
}
private void Control_ControlAdded(object? sender, ControlEventArgs e)
{
if (sender is not Control control)
return;
if (e.Control is not null)
Track(e.Control);
Execute(control, ControlExecuteReason.Add, new ControlParameters(e.Control), false);
}
private void Control_ControlRemoved(object? sender, ControlEventArgs e)
{
if (sender is not Control control)
return;
Execute(control, ControlExecuteReason.Remove, new ControlParameters(e.Control), false);
}
protected internal void Initialize(PluginFunctionParameter? parameters)
{
Execute(ControlExecuteReason.Init, parameters);
}
public void Execute(PluginFunctionParameter? parameters)
{
Execute(ControlExecuteReason.None, parameters);
}
protected void Execute(ControlExecuteReason reason, PluginFunctionParameter? parameter)
{
Execute(control, reason, parameter, true);
}
protected void Execute(Control control, ControlExecuteReason reason, PluginFunctionParameter? parameter, bool recursive)
{
execute(control);
void execute(Control control)
{
if (control.Name is string name)
{
foreach (var feature in features)
{
if (feature.ControlName is null || feature.ControlNameRegEx && Regex.IsMatch(control.Name, feature.ControlName) || feature.ControlName == control.Name)
feature.Execute(control, reason, parameter);
}
}
if (!recursive)
return;
foreach (var child in control.Controls)
{
if (child is Control childControl)
execute(childControl);
}
}
}
}

View File

@@ -0,0 +1,6 @@
namespace Pilz.Plugins.Advanced.UI;
public class ControlParameters(Control? control) : PluginFunctionParameter
{
public Control? Control { get; } = control;
}

View File

@@ -1,27 +0,0 @@
using System.Windows.Forms;
namespace Pilz.Plugins.Advanced.UI.Extensions;
public static class ControlFeatureCollectoinExtensions
{
public static void ExecuteControlFeatures(this IEnumerable<ControlFeature> @this, Control control, bool recursive, PluginFunctionParameter? parameter)
{
if (control.Name is string name)
{
foreach (var feature in @this)
{
if (feature.ControlName is null || feature.ControlName.Equals(control.Name))
feature.Execute(control, parameter);
}
}
if (recursive)
{
foreach (var child in control.Controls)
{
if (child is Control childControl)
ExecuteControlFeatures(@this, childControl, true, parameter);
}
}
}
}

View File

@@ -2,29 +2,30 @@
public static class PluginFeatureControllerExtensions
{
public static ControlManager ApplyControlManager(this PluginFeatureController @this, Control control)
public static IControlManager ApplyControlManager(this PluginFeatureController @this, string featureType, Control control)
{
return ApplyControlManager(@this, control, true, true);
return ApplyControlManager(@this, featureType, control, true, true);
}
public static ControlManager ApplyControlManager(this PluginFeatureController @this, Control control, bool recursive, bool autoTrack)
public static IControlManager ApplyControlManager(this PluginFeatureController @this, string featureType, Control control, PluginFunctionParameter? parameter)
{
var manager = new ControlManager(@this, autoTrack);
goThrow(control);
return ApplyControlManager(@this, featureType, control, true, true, parameter);
}
public static IControlManager ApplyControlManager(this PluginFeatureController @this, string featureType, Control control, bool recursive, bool autoTrack)
{
return ApplyControlManager(@this, featureType, control, recursive, autoTrack, null);
}
public static IControlManager ApplyControlManager(this PluginFeatureController @this, string featureType, Control control, bool recursive, bool autoTrack, PluginFunctionParameter? parameter)
{
var manager = new ControlManager(@this.Features.Get(featureType).OfType<ControlFeature>(), control, recursive);
if (autoTrack)
manager.Track();
manager.Initialize(parameter);
return manager;
void goThrow(Control control)
{
manager.RegisterControl(control);
if (recursive)
{
foreach (var child in control.Controls)
{
if (child is Control childControl)
goThrow(childControl);
}
}
}
}
}

View File

@@ -0,0 +1,6 @@
namespace Pilz.Plugins.Advanced.UI;
public interface IControlManager
{
void Execute(PluginFunctionParameter? parameter);
}