127 lines
4.8 KiB
C#
127 lines
4.8 KiB
C#
using Gtk;
|
|
|
|
namespace Pilz.Plugins.Advanced.UI.Gtk;
|
|
|
|
public static class Extensions
|
|
{
|
|
private class PluginFunctionEventArgs(EventArgs original, PluginFunction function) : EventArgs
|
|
{
|
|
public EventArgs Original { get; } = original;
|
|
public PluginFunction Function { get; } = function;
|
|
}
|
|
|
|
private class PluginModuleEventArgs(EventArgs original, PluginModuleBase function) : EventArgs
|
|
{
|
|
public EventArgs Original { get; } = original;
|
|
public PluginModuleBase Function { get; } = function;
|
|
}
|
|
|
|
public static MenuItem GetAsItem(this PluginModuleBase module)
|
|
{
|
|
return module.GetAsItem(true);
|
|
}
|
|
|
|
public static MenuItem GetAsItem(this PluginModuleBase module, bool addDefaultHandler)
|
|
{
|
|
return module.GetAsItem(addDefaultHandler ? (sender, e) => RadMenuItem_RMMethod_Click(sender, new PluginModuleEventArgs(e, module)) : null);
|
|
}
|
|
|
|
public static MenuItem GetAsItem(this PluginFunction function)
|
|
{
|
|
return function.GetAsItem(true);
|
|
}
|
|
|
|
public static MenuItem GetAsItem(this PluginFunction function, bool addDefaultHandler)
|
|
{
|
|
return function.GetAsItem(addDefaultHandler ? (sender, e) => RadMenuItem_RMFunction_Click(sender, new PluginFunctionEventArgs(e, function)) : null);
|
|
}
|
|
|
|
public static MenuItem GetAsItem(this PluginFeature module, EventHandler? clickHandler)
|
|
{
|
|
var item = new MenuItem(module.Name)
|
|
{
|
|
Visible = module.Enabled,
|
|
TooltipText = module.Description ?? module.Name
|
|
};
|
|
|
|
if (clickHandler is not null)
|
|
item.Activated += clickHandler;
|
|
|
|
return item;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Inserts all items to a menu item.
|
|
/// </summary>
|
|
/// <param name="parentMenuItem"></param>
|
|
/// <param name="features"></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="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>
|
|
public static IEnumerable<KeyValuePair<MenuItem, PluginFeature>> InsertItemsTo(this IEnumerable<PluginFeature> features,
|
|
MenuItem parentMenuItem,
|
|
bool addDefaultHandler = false,
|
|
EventHandler? customClickHandler = null,
|
|
int? customDefault = null,
|
|
int? customTop = null,
|
|
int? customBottom = null,
|
|
bool insertPrioSplitters = false)
|
|
{
|
|
var insertedItems = new List<KeyValuePair<MenuItem, PluginFeature>>();
|
|
FeaturePrioritization? prevPrio = null;
|
|
|
|
// Oder by priorization
|
|
features = features.OrderByDescending(n => n.Prioritization);
|
|
|
|
foreach (var feature in features)
|
|
{
|
|
MenuItem item;
|
|
|
|
if (feature is PluginFunction function)
|
|
item = function.GetAsItem(addDefaultHandler);
|
|
else if (feature is PluginModuleBase module)
|
|
item = module.GetAsItem(addDefaultHandler);
|
|
else
|
|
item = feature.GetAsItem(null);
|
|
|
|
if (!addDefaultHandler && customClickHandler != null)
|
|
item.Activated += customClickHandler;
|
|
|
|
if (insertPrioSplitters && (prevPrio == null || prevPrio > feature.Prioritization))
|
|
{
|
|
insertItem(new SeparatorMenuItem());
|
|
prevPrio = feature.Prioritization;
|
|
}
|
|
|
|
insertItem(item);
|
|
|
|
void insertItem(MenuItem item)
|
|
{
|
|
if (parentMenuItem.Submenu is Menu subMenu)
|
|
subMenu.Append(item);
|
|
}
|
|
|
|
if (item.Parent != null)
|
|
insertedItems.Add(new(item, feature));
|
|
}
|
|
|
|
return insertedItems;
|
|
}
|
|
|
|
private static void RadMenuItem_RMMethod_Click(object? sender, EventArgs e)
|
|
{
|
|
if (sender is MenuItem item && item.Sensitive && e is PluginFunctionEventArgs args && args.Function.Enabled)
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
private static void RadMenuItem_RMFunction_Click(object? sender, EventArgs e)
|
|
{
|
|
if (sender is MenuItem item && item.Sensitive && e is PluginFunctionEventArgs args && args.Function.Enabled)
|
|
args.Function.Execute();
|
|
}
|
|
} |