change UI to UI.WinForms
This commit is contained in:
168
Pilz.Plugins.Advanced.UI.WinForms.Telerik/Extensions.cs
Normal file
168
Pilz.Plugins.Advanced.UI.WinForms.Telerik/Extensions.cs
Normal file
@@ -0,0 +1,168 @@
|
||||
using Telerik.WinControls;
|
||||
using Telerik.WinControls.UI;
|
||||
|
||||
namespace Pilz.Plugins.Advanced.UI.WinForms.Telerik;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
public static Icon? ToIcon(this Image image)
|
||||
{
|
||||
if (image is Bitmap bitmap)
|
||||
return Icon.FromHandle(bitmap.GetHicon());
|
||||
return null;
|
||||
}
|
||||
|
||||
public static RadButtonItem GetAsItem(this PluginModuleBase module, PluginButtonType buttonType = PluginButtonType.RadMenuItem)
|
||||
{
|
||||
return module.GetAsItem(true, buttonType: buttonType);
|
||||
}
|
||||
|
||||
public static RadButtonItem GetAsItem(this PluginModuleBase module, bool addDefaultHandler, PluginButtonType buttonType = PluginButtonType.RadMenuItem)
|
||||
{
|
||||
return module.GetAsItem(addDefaultHandler ? RadMenuItem_RMMethod_Click : null, buttonType: buttonType);
|
||||
}
|
||||
|
||||
public static RadButtonItem GetAsItem(this PluginFunction function, PluginButtonType buttonType = PluginButtonType.RadMenuItem)
|
||||
{
|
||||
return function.GetAsItem(true, buttonType: buttonType);
|
||||
}
|
||||
|
||||
public static RadButtonItem GetAsItem(this PluginFunction function, bool addDefaultHandler, PluginButtonType buttonType = PluginButtonType.RadMenuItem)
|
||||
{
|
||||
return function.GetAsItem(addDefaultHandler ? RadMenuItem_RMFunction_Click : null, buttonType: buttonType);
|
||||
}
|
||||
|
||||
public static RadButtonItem GetAsItem(this PluginFeature module, EventHandler? clickHandler, PluginButtonType buttonType = PluginButtonType.RadMenuItem)
|
||||
{
|
||||
var item = buttonType switch
|
||||
{
|
||||
PluginButtonType.RadMenuItem => new RadMenuItem(),
|
||||
PluginButtonType.RadButtonElement => new RadButtonElement(),
|
||||
PluginButtonType.RadButtonItem => new RadButtonItem(),
|
||||
_ => throw new NotSupportedException(),
|
||||
};
|
||||
|
||||
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;
|
||||
item.ToolTipText = module.Description ?? module.Name;
|
||||
|
||||
if (clickHandler is not null)
|
||||
item.Click += clickHandler;
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts all items to an item collection.
|
||||
/// </summary>
|
||||
/// <param name="features"></param>
|
||||
/// <param name="itemsCollection">Examples:<br/>
|
||||
/// - <see cref="RadMenuItem.Items"/><br/>
|
||||
/// - <see cref="RadSplitButtonElement.Items"/><br/>
|
||||
/// - <see cref="RadDropDownButtonElement.Items"/><br/>
|
||||
/// - <see cref="RadContextMenu.Items"/><br/>
|
||||
/// - <see cref="RadRibbonBarGroup.Items"/><br/></param>
|
||||
/// <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 true.</param>
|
||||
/// <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>
|
||||
/// <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="insertPrioSplitters">Defines if splitters should be inserted to seperate the new items by priorization.</param>
|
||||
/// <param name="buttonType">Defines what type of button should be created.</param>
|
||||
public static IEnumerable<RadButtonItem> InsertItemsTo(this IEnumerable<PluginFeature> features,
|
||||
RadItemOwnerCollection itemsCollection,
|
||||
bool addDefaultHandler = false,
|
||||
EventHandler? customClickHandler = null,
|
||||
FeatureInsertMode insertMode = FeatureInsertMode.Default,
|
||||
int? customDefault = null,
|
||||
int? customTop = null,
|
||||
int? customBottom = null,
|
||||
bool insertPrioSplitters = false,
|
||||
PluginButtonType buttonType = PluginButtonType.RadMenuItem)
|
||||
{
|
||||
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);
|
||||
var insertedItems = new List<RadButtonItem>();
|
||||
FeaturePrioritization? prevPrio = null;
|
||||
|
||||
// Oder by priorization
|
||||
features = features.OrderByDescending(n => n.Prioritization);
|
||||
|
||||
foreach (var feature in features)
|
||||
{
|
||||
RadButtonItem item;
|
||||
|
||||
if (feature is PluginFunction function)
|
||||
item = function.GetAsItem(addDefaultHandler, buttonType);
|
||||
else if (feature is PluginModuleBase module)
|
||||
item = module.GetAsItem(addDefaultHandler, buttonType);
|
||||
else
|
||||
item = feature.GetAsItem(null, buttonType);
|
||||
|
||||
if (!addDefaultHandler && customClickHandler != null)
|
||||
item.Click += customClickHandler;
|
||||
|
||||
if (insertPrioSplitters && (prevPrio == null || prevPrio > feature.Prioritization))
|
||||
{
|
||||
insertItem(new RadMenuSeparatorItem());
|
||||
prevPrio = feature.Prioritization;
|
||||
}
|
||||
|
||||
insertItem(item);
|
||||
|
||||
void insertItem(RadButtonItem item)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if (item.Parent != null)
|
||||
insertedItems.Add(item);
|
||||
}
|
||||
|
||||
return insertedItems;
|
||||
}
|
||||
|
||||
private static void RadMenuItem_RMMethod_Click(object? sender, EventArgs e)
|
||||
{
|
||||
if (sender is RadButtonItem item && item.Enabled)
|
||||
{
|
||||
if (item.Tag is RadPluginModule radmodule)
|
||||
radmodule.ShowUI();
|
||||
else if (item.Tag is PluginModule module)
|
||||
module.ShowUI();
|
||||
}
|
||||
}
|
||||
|
||||
private static void RadMenuItem_RMFunction_Click(object? sender, EventArgs e)
|
||||
{
|
||||
if (sender is RadButtonItem item && item.Tag is PluginFunction function && function.Enabled)
|
||||
function.Execute();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user