add a uniquie event for plugin function execution
This commit is contained in:
@@ -1,16 +1,7 @@
|
||||
using System.Reflection.Metadata;
|
||||
|
||||
namespace Pilz.Plugins.Advanced.UI;
|
||||
namespace Pilz.Plugins.Advanced.UI;
|
||||
|
||||
public abstract class PluginModule<TPluginModuleUI> : PluginModuleBase where TPluginModuleUI : Control
|
||||
{
|
||||
public delegate void PluginModuleUIEventHandler(PluginModuleBase module, TPluginModuleUI ui);
|
||||
|
||||
/// <summary>
|
||||
/// Fires when a <see cref="PluginModuleUI"/> instance has been created.
|
||||
/// </summary>
|
||||
public static event PluginModuleUIEventHandler? OnUICreated;
|
||||
|
||||
public bool Visible { get; set; } = true;
|
||||
public bool AllowEmbedding { get; set; } = true;
|
||||
|
||||
@@ -40,17 +31,22 @@ public abstract class PluginModule<TPluginModuleUI> : PluginModuleBase where TPl
|
||||
{
|
||||
}
|
||||
|
||||
public virtual TPluginModuleUI CreateUI()
|
||||
public virtual TPluginModuleUI? CreateUI()
|
||||
{
|
||||
return CreateUI(null);
|
||||
}
|
||||
|
||||
public virtual TPluginModuleUI CreateUI(PluginFunctionParameter? @params)
|
||||
public virtual TPluginModuleUI? CreateUI(PluginFunctionParameter? @params)
|
||||
{
|
||||
var ui = CreateNewUI(@params);
|
||||
OnUICreated?.Invoke(this, ui);
|
||||
return ui;
|
||||
object? ui = default;
|
||||
|
||||
if (!OnPreExecute(@params, ref ui))
|
||||
ui = CreateNewUI(@params);
|
||||
|
||||
OnPostExecute(@params, ref ui);
|
||||
|
||||
return ui as TPluginModuleUI;
|
||||
}
|
||||
|
||||
protected abstract TPluginModuleUI CreateNewUI(PluginFunctionParameter? @params);
|
||||
protected abstract TPluginModuleUI? CreateNewUI(PluginFunctionParameter? @params);
|
||||
}
|
||||
3
Pilz.Plugins.Advanced/Delegates.cs
Normal file
3
Pilz.Plugins.Advanced/Delegates.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
public delegate void PluginFeatureExecuteEventHandler(object sender, PluginFeatureExecuteEventArgs e);
|
||||
@@ -1,7 +1,17 @@
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
using System.Reflection.Metadata;
|
||||
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
public abstract class PluginFeature
|
||||
{
|
||||
/// <summary>
|
||||
/// Fires when the plugin feature gets used.
|
||||
/// <br/>- For <see cref="PluginFunction"/> this fires on <see cref="PluginFunction.ExecuteFunction(PluginFunctionParameter?)"/>.
|
||||
/// <br/>- For <see cref="T:PluginModule"/> this fires on <see cref="T:PluginModule.CreateNewUI(PluginFunctionParameter?)"/>.
|
||||
/// <br/>- For any other custom feature implementation this may variate.
|
||||
/// </summary>
|
||||
public static event PluginFeatureExecuteEventHandler? OnExecute;
|
||||
|
||||
/// <summary>
|
||||
/// The type of the feature defines where the feature get integrated.
|
||||
/// </summary>
|
||||
@@ -50,4 +60,49 @@ public abstract class PluginFeature
|
||||
{
|
||||
return $"{featureType}:{identifier}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires the <see cref="OnExecute"/> event with the status <see cref="PluginFeatureExecuteEventType.PostEvent"/>.
|
||||
/// </summary>
|
||||
/// <param name="params">The present <see cref="PluginFunctionParameter"/>.</param>
|
||||
/// <param name="result">The resulting object that will be returned.</param>
|
||||
/// <returns>Returns true if the original function should be executed.</returns>
|
||||
protected bool OnPreExecute(PluginFunctionParameter? @params, ref object? result)
|
||||
{
|
||||
if (OnExecute != null)
|
||||
{
|
||||
var args = new PluginFeatureExecuteEventArgs(PluginFeatureExecuteEventType.PreEvent, @params);
|
||||
|
||||
OnExecute.Invoke(this, args);
|
||||
|
||||
if (args.Handled)
|
||||
{
|
||||
result = args.Result;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fires the <see cref="OnExecute"/> event with the status <see cref="PluginFeatureExecuteEventType.PostEvent"/>.
|
||||
/// </summary>
|
||||
/// <param name="params">The present <see cref="PluginFunctionParameter"/>.</param>
|
||||
/// <param name="result">The resulting object that will be returned.</param>
|
||||
protected void OnPostExecute(PluginFunctionParameter? @params, ref object? result)
|
||||
{
|
||||
if (OnExecute != null)
|
||||
{
|
||||
var args = new PluginFeatureExecuteEventArgs(PluginFeatureExecuteEventType.PostEvent, @params)
|
||||
{
|
||||
Result = result
|
||||
};
|
||||
|
||||
OnExecute.Invoke(this, args);
|
||||
|
||||
if (args.Handled)
|
||||
result = args.Result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
9
Pilz.Plugins.Advanced/PluginFeatureExecuteEventArgs.cs
Normal file
9
Pilz.Plugins.Advanced/PluginFeatureExecuteEventArgs.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
public class PluginFeatureExecuteEventArgs(PluginFeatureExecuteEventType eventType, PluginFunctionParameter? parameters) : EventArgs
|
||||
{
|
||||
public PluginFeatureExecuteEventType EventType { get; } = eventType;
|
||||
public PluginFunctionParameter? Parameters { get; } = parameters;
|
||||
public bool Handled { get; set; }
|
||||
public object? Result { get; set; }
|
||||
}
|
||||
7
Pilz.Plugins.Advanced/PluginFeatureExecuteEventType.cs
Normal file
7
Pilz.Plugins.Advanced/PluginFeatureExecuteEventType.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Pilz.Plugins.Advanced;
|
||||
|
||||
public enum PluginFeatureExecuteEventType
|
||||
{
|
||||
PreEvent,
|
||||
PostEvent,
|
||||
}
|
||||
@@ -34,7 +34,14 @@ public abstract class PluginFunction : PluginFeature
|
||||
|
||||
public virtual object? Execute(PluginFunctionParameter? @params)
|
||||
{
|
||||
return ExecuteFunction(@params);
|
||||
object? result = default;
|
||||
|
||||
if (!OnPreExecute(@params, ref result))
|
||||
result = ExecuteFunction(@params);
|
||||
|
||||
OnPostExecute(@params, ref result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected abstract object? ExecuteFunction(PluginFunctionParameter? @params);
|
||||
|
||||
Reference in New Issue
Block a user