diff --git a/Pilz.Plugins.Advanced/Extensions.cs b/Pilz.Plugins.Advanced/Extensions.cs index a125741..13b2b1c 100644 --- a/Pilz.Plugins.Advanced/Extensions.cs +++ b/Pilz.Plugins.Advanced/Extensions.cs @@ -1,6 +1,7 @@ using System.Drawing; using System.Reflection; using Telerik.WinControls; +using Telerik.WinControls.Elements; using Telerik.WinControls.UI; namespace Pilz.Plugins.Advanced @@ -50,6 +51,58 @@ namespace Pilz.Plugins.Advanced return item; } + /// + /// Inserts all items to an item collection. + /// + /// + /// Examples:
+ /// -
+ /// -
+ /// -
+ /// -
+ /// -
+ /// Will add a default click handler that executes the feature.
+ /// You usually don't set customClickHandler if you set this parameter to . + /// Adds a custom click handler. If addDefaultHandler is true, it will only work on s.
+ /// You usually don't set addDefaultHandler to true if you set this parameter to something not null. + public static void InsertItemsTo(this IEnumerable features, RadItemOwnerCollection itemsCollection, bool addDefaultHandler = false, EventHandler? customClickHandler = null, FeatureInsertMode insertMode = FeatureInsertMode.Default, int? customDefault = null, int? customTop = null, int? customBottom = null) + { + var insertDefault = customDefault ?? (insertMode.HasFlag(FeatureInsertMode.DefaultStart) ? 0 : itemsCollection.Count); + var insertTop = customTop ?? (insertMode.HasFlag(FeatureInsertMode.InsertTop) || insertMode.HasFlag(FeatureInsertMode.DefaultStart) ? 0 : insertDefault); + var insertBottom = customBottom ?? (insertMode.HasFlag(FeatureInsertMode.InsertBottom) || insertMode.HasFlag(FeatureInsertMode.DefaultEnd) ? itemsCollection.Count : insertDefault); + + foreach (var feature in features) + { + RadMenuItem item; + + if (feature is PluginFunction function) + item = function.GetAsItem(addDefaultHandler); + else if (feature is PluginModule module) + item = module.GetAsItem(addDefaultHandler); + else + item = feature.GetAsItem(null); + + switch (feature.Prioritization) + { + case FeaturePrioritization.High: + if (insertDefault >= insertTop) insertDefault++; + if (insertBottom >= insertTop) insertBottom++; + itemsCollection.Insert(insertTop++, item); + break; + case FeaturePrioritization.Default: + if (insertBottom >= insertDefault) insertBottom++; + if (insertTop >= insertDefault) insertTop++; + itemsCollection.Insert(insertDefault++, item); + break; + case FeaturePrioritization.Low: + if (insertTop >= insertBottom) insertTop++; + if (insertDefault >= insertBottom) insertDefault++; + itemsCollection.Insert(insertBottom++, item); + break; + } + } + } + private static void RadMenuItem_RMMethod_Click(object? sender, EventArgs e) { if (sender is RadMenuItem item && item.Tag is PluginModule function) diff --git a/Pilz.Plugins.Advanced/FeatureInsertMode.cs b/Pilz.Plugins.Advanced/FeatureInsertMode.cs new file mode 100644 index 0000000..3982075 --- /dev/null +++ b/Pilz.Plugins.Advanced/FeatureInsertMode.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Pilz.Plugins.Advanced +{ + [Flags] + public enum FeatureInsertMode + { + /// + /// Features will be inserted at the end of the collection. + /// + Default = 0, + /// + /// Features will be inserted at the end of the collection. + /// This is the default behavior and equals . Will only be used if not set . + /// + DefaultEnd = Default, + /// + /// Features will be inserted at the start of the collection. + /// Will only be used if not set . + /// + DefaultStart = 1, + /// + /// Features with prioritization will be inserted at the top (or left). + /// + InsertTop = 1 << 2, + /// + /// Features with prioritization will be inserted at the bottom (or right). + /// + InsertBottom = 1 << 3, + /// + /// Features with prioritization other then will be inserted at the top or bottom. + /// + InsertTopAndBottom = InsertTop | InsertBottom, + } +} diff --git a/Pilz.Plugins.Advanced/FeaturePrioritization.cs b/Pilz.Plugins.Advanced/FeaturePrioritization.cs new file mode 100644 index 0000000..a38bbc0 --- /dev/null +++ b/Pilz.Plugins.Advanced/FeaturePrioritization.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Pilz.Plugins.Advanced +{ + public enum FeaturePrioritization + { + Low = -1, + Default = 0, + High = 1, + } +} diff --git a/Pilz.Plugins.Advanced/PluginFeature.cs b/Pilz.Plugins.Advanced/PluginFeature.cs index 87ce087..9029e2e 100644 --- a/Pilz.Plugins.Advanced/PluginFeature.cs +++ b/Pilz.Plugins.Advanced/PluginFeature.cs @@ -32,6 +32,12 @@ namespace Pilz.Plugins.Advanced /// public virtual RadSvgImage? Icon { get; set; } /// + /// Sets the prioritization of the feature. + /// This will be respected on abfragen features and on inserting as items using the extension methods"/>. + /// Some applications might implement a way to regonize feature prioritization via its own way. + /// + public virtual FeaturePrioritization Prioritization { get; set; } + /// /// Defines if the feature is enabled/visible. /// public virtual bool Enabled { get; set; } = true;