diff --git a/Pilz.Plugins.Advanced.UI/ControlListener.cs b/Pilz.Plugins.Advanced.UI/ControlListener.cs index 9c0e76f..f9673ae 100644 --- a/Pilz.Plugins.Advanced.UI/ControlListener.cs +++ b/Pilz.Plugins.Advanced.UI/ControlListener.cs @@ -4,15 +4,15 @@ namespace Pilz.Plugins.Advanced.UI; internal class ControlListener(IEnumerable features, Control control, bool recursive) : IControlListener { - private bool isTracking; + protected bool isTracking; - public bool Recursive => recursive; + public virtual bool Recursive => recursive; - public Control Control => control; + public virtual Control Control => control; - public IEnumerable Features => features; + public virtual IEnumerable Features => features; - internal void Track() + internal virtual void Track() { if (isTracking) return; @@ -20,7 +20,7 @@ internal class ControlListener(IEnumerable features, Con isTracking = true; } - protected void Track(Control control) + protected virtual void Track(Control control) { control.Disposed += Control_Disposed; control.ControlAdded += Control_ControlAdded; @@ -36,7 +36,7 @@ internal class ControlListener(IEnumerable features, Con } } - private void Control_Disposed(object? sender, EventArgs e) + protected virtual void Control_Disposed(object? sender, EventArgs e) { if (sender is not Control control) return; @@ -48,7 +48,7 @@ internal class ControlListener(IEnumerable features, Con control.ControlRemoved -= Control_ControlRemoved; } - private void Control_ControlAdded(object? sender, ControlEventArgs e) + protected virtual void Control_ControlAdded(object? sender, ControlEventArgs e) { if (sender is not Control control) return; @@ -59,7 +59,7 @@ internal class ControlListener(IEnumerable features, Con Execute(control, ControlExecuteReason.Add, new ControlParameters(e.Control), false); } - private void Control_ControlRemoved(object? sender, ControlEventArgs e) + protected virtual void Control_ControlRemoved(object? sender, ControlEventArgs e) { if (sender is not Control control) return; @@ -67,22 +67,22 @@ internal class ControlListener(IEnumerable features, Con Execute(control, ControlExecuteReason.Remove, new ControlParameters(e.Control), false); } - protected internal void Initialize(PluginFunctionParameter? parameters) + protected internal virtual void Initialize(PluginFunctionParameter? parameters) { Execute(ControlExecuteReason.Init, parameters); } - public void Execute(PluginFunctionParameter? parameters) + public virtual void Execute(PluginFunctionParameter? parameters) { Execute(ControlExecuteReason.None, parameters); } - protected void Execute(ControlExecuteReason reason, PluginFunctionParameter? parameter) + protected virtual void Execute(ControlExecuteReason reason, PluginFunctionParameter? parameter) { Execute(control, reason, parameter, recursive); } - protected void Execute(Control control, ControlExecuteReason reason, PluginFunctionParameter? parameter, bool recursive) + protected virtual void Execute(Control control, ControlExecuteReason reason, PluginFunctionParameter? parameter, bool recursive) { execute(control); void execute(Control control) diff --git a/Pilz.Plugins.Advanced.UI/ControlListenerDummy.cs b/Pilz.Plugins.Advanced.UI/ControlListenerDummy.cs new file mode 100644 index 0000000..c08b7db --- /dev/null +++ b/Pilz.Plugins.Advanced.UI/ControlListenerDummy.cs @@ -0,0 +1,12 @@ +namespace Pilz.Plugins.Advanced.UI; + +internal class ControlListenerDummy(IEnumerable features, Control control, bool recursive) : ControlListener(features, control, recursive) +{ + protected override void Track(Control control) + { + } + + protected override void Execute(Control control, ControlExecuteReason reason, PluginFunctionParameter? parameter, bool recursive) + { + } +} diff --git a/Pilz.Plugins.Advanced.UI/Extensions/PluginFeatureControllerExtensions.cs b/Pilz.Plugins.Advanced.UI/Extensions/PluginFeatureControllerExtensions.cs index cedd027..0f69e73 100644 --- a/Pilz.Plugins.Advanced.UI/Extensions/PluginFeatureControllerExtensions.cs +++ b/Pilz.Plugins.Advanced.UI/Extensions/PluginFeatureControllerExtensions.cs @@ -19,7 +19,12 @@ public static class PluginFeatureControllerExtensions public static IControlListener ApplyControlManager(this PluginFeatureController @this, string featureType, Control control, bool recursive, bool autoTrack, PluginFunctionParameter? parameter) { - var manager = new ControlListener(@this.Features.Get(featureType).OfType(), control, recursive); + var features = @this.Features.Get(featureType).OfType(); + + if (!features.Any()) + return new ControlListenerDummy([], control, recursive); + + var manager = new ControlListener(features, control, recursive); if (autoTrack) manager.Track();