allow creating RadButtonItem instead of RadMenuItem

This commit is contained in:
2024-10-15 11:36:14 +02:00
parent 64dc7c776a
commit d61e47f900
2 changed files with 43 additions and 25 deletions

View File

@@ -12,37 +12,41 @@ public static class Extensions
return null; return null;
} }
public static RadMenuItem GetAsItem(this PluginModuleBase module) public static RadButtonItem GetAsItem(this PluginModuleBase module, PluginButtonType buttonType = PluginButtonType.RadMenuItem)
{ {
return GetAsItem(module, true); return GetAsItem(module, true, buttonType: buttonType);
} }
public static RadMenuItem GetAsItem(this PluginModuleBase module, bool addDefaultHandler) public static RadButtonItem GetAsItem(this PluginModuleBase module, bool addDefaultHandler, PluginButtonType buttonType = PluginButtonType.RadMenuItem)
{ {
return GetAsItem(module, addDefaultHandler ? RadMenuItem_RMMethod_Click : null); return GetAsItem(module, addDefaultHandler ? RadMenuItem_RMMethod_Click : null, buttonType: buttonType);
} }
public static RadMenuItem GetAsItem(this PluginFunction function) public static RadButtonItem GetAsItem(this PluginFunction function, PluginButtonType buttonType = PluginButtonType.RadMenuItem)
{ {
return GetAsItem(function, true); return GetAsItem(function, true, buttonType: buttonType);
} }
public static RadMenuItem GetAsItem(this PluginFunction function, bool addDefaultHandler) public static RadButtonItem GetAsItem(this PluginFunction function, bool addDefaultHandler, PluginButtonType buttonType = PluginButtonType.RadMenuItem)
{ {
return GetAsItem(function, addDefaultHandler ? RadMenuItem_RMFunction_Click : null); return GetAsItem(function, addDefaultHandler ? RadMenuItem_RMFunction_Click : null, buttonType: buttonType);
} }
public static RadMenuItem GetAsItem(this PluginFeature module, EventHandler? clickHandler) public static RadButtonItem GetAsItem(this PluginFeature module, EventHandler? clickHandler, PluginButtonType buttonType = PluginButtonType.RadMenuItem)
{ {
var item = new RadMenuItem var item = buttonType switch
{ {
Text = module.Name, PluginButtonType.RadMenuItem => new RadMenuItem(),
Image = module.Icon as Image, PluginButtonType.RadButtonItem => new RadButtonItem(),
SvgImage = module.Icon as RadSvgImage, _ => throw new NotSupportedException(),
Tag = module,
Visibility = module.Enabled ? ElementVisibility.Visible : ElementVisibility.Collapsed
}; };
item.Text = module.Name;
item.Image = module.Icon as Image;
item.SvgImage = module.Icon as RadSvgImage;
item.Tag = module;
item.Visibility = module.Enabled ? ElementVisibility.Visible : ElementVisibility.Collapsed;
if (clickHandler is not null) if (clickHandler is not null)
item.Click += clickHandler; item.Click += clickHandler;
@@ -60,10 +64,16 @@ public static class Extensions
/// - <see cref="RadContextMenu.Items"/><br/> /// - <see cref="RadContextMenu.Items"/><br/>
/// - <see cref="RadRibbonBarGroup.Items"/><br/></param> /// - <see cref="RadRibbonBarGroup.Items"/><br/></param>
/// <param name="addDefaultHandler">Will add a default click handler that executes the feature.<br/> /// <param name="addDefaultHandler">Will add a default click handler that executes the feature.<br/>
/// You usually don't set customClickHandler if you set this parameter to <see cref="true"/>.</param> /// You usually don't set customClickHandler if you set this parameter to true.</param>
/// <param name="customClickHandler">Adds a custom click handler. If addDefaultHandler is true, it will only work on <see cref="PluginFeature"/>s.<br/> /// <param name="customClickHandler">Adds a custom click handler. If addDefaultHandler is true, it will only work on <see cref="PluginFeature"/>s.<br/>
/// You usually don't set addDefaultHandler to true if you set this parameter to something not null.</param> /// You usually don't set addDefaultHandler to true if you set this parameter to something not null.</param>
public static IEnumerable<RadMenuItem> InsertItemsTo(this IEnumerable<PluginFeature> features, /// <param name="insertMode">Defines how and where the items will be inserted.</param>
/// <param name="customDefault">Defines a custom default position (index).</param>
/// <param name="customTop">Defines a custom top position (index).</param>
/// <param name="customBottom">Defines a custom bottom position (index).</param>
/// <param name="insertSplitter">Defines if splitters should be inserted to seperate the new items from the existing items.</param>
/// <param name="buttonType">Defines what type of button should be created.</param>
public static IEnumerable<RadButtonItem> InsertItemsTo(this IEnumerable<PluginFeature> features,
RadItemOwnerCollection itemsCollection, RadItemOwnerCollection itemsCollection,
bool addDefaultHandler = false, bool addDefaultHandler = false,
EventHandler? customClickHandler = null, EventHandler? customClickHandler = null,
@@ -71,23 +81,24 @@ public static class Extensions
int? customDefault = null, int? customDefault = null,
int? customTop = null, int? customTop = null,
int? customBottom = null, int? customBottom = null,
FeatureInsertPosition insertSplitter = FeatureInsertPosition.None) FeatureInsertPosition insertSplitter = FeatureInsertPosition.None,
PluginButtonType buttonType = PluginButtonType.RadMenuItem)
{ {
var insertDefault = customDefault ?? (insertMode.HasFlag(FeatureInsertMode.DefaultStart) ? 0 : itemsCollection.Count); var insertDefault = customDefault ?? (insertMode.HasFlag(FeatureInsertMode.DefaultStart) ? 0 : itemsCollection.Count);
var insertTop = customTop ?? (insertMode.HasFlag(FeatureInsertMode.InsertTop) || insertMode.HasFlag(FeatureInsertMode.DefaultStart) ? 0 : insertDefault); 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); var insertBottom = customBottom ?? (insertMode.HasFlag(FeatureInsertMode.InsertBottom) || insertMode.HasFlag(FeatureInsertMode.DefaultEnd) ? itemsCollection.Count : insertDefault);
var insertedItems = new List<RadMenuItem>(); var insertedItems = new List<RadButtonItem>();
foreach (var feature in features) foreach (var feature in features)
{ {
RadMenuItem item; RadButtonItem item;
if (feature is PluginFunction function) if (feature is PluginFunction function)
item = function.GetAsItem(addDefaultHandler); item = function.GetAsItem(addDefaultHandler, buttonType);
else if (feature is PluginModuleBase module) else if (feature is PluginModuleBase module)
item = module.GetAsItem(addDefaultHandler); item = module.GetAsItem(addDefaultHandler, buttonType);
else else
item = feature.GetAsItem(null); item = feature.GetAsItem(null, buttonType);
if (!addDefaultHandler && customClickHandler != null) if (!addDefaultHandler && customClickHandler != null)
item.Click += customClickHandler; item.Click += customClickHandler;
@@ -123,7 +134,7 @@ public static class Extensions
private static void RadMenuItem_RMMethod_Click(object? sender, EventArgs e) private static void RadMenuItem_RMMethod_Click(object? sender, EventArgs e)
{ {
if (sender is RadMenuItem item && item.Enabled) if (sender is RadButtonItem item && item.Enabled)
{ {
if (item.Tag is RadPluginModule radmodule) if (item.Tag is RadPluginModule radmodule)
radmodule.ShowUI(); radmodule.ShowUI();
@@ -134,7 +145,7 @@ public static class Extensions
private static void RadMenuItem_RMFunction_Click(object? sender, EventArgs e) private static void RadMenuItem_RMFunction_Click(object? sender, EventArgs e)
{ {
if (sender is RadMenuItem item && item.Tag is PluginFunction function && function.Enabled) if (sender is RadButtonItem item && item.Tag is PluginFunction function && function.Enabled)
function.Execute(); function.Execute();
} }
} }

View File

@@ -0,0 +1,7 @@
namespace Pilz.Plugins.Advanced.UI.Telerik;
public enum PluginButtonType
{
RadMenuItem,
RadButtonItem,
}