add Pilz.Plugins.Advanced.UI.Gtk
This commit is contained in:
127
Pilz.Plugins.Advanced.UI.Gtk/Extensions.cs
Normal file
127
Pilz.Plugins.Advanced.UI.Gtk/Extensions.cs
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<Version>1.0.0</Version>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="GtkSharp" Version="3.24.24.95" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Pilz.Plugins.Advanced\Pilz.Plugins.Advanced.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
</Project>
|
||||||
10
Pilz.sln
10
Pilz.sln
@@ -51,6 +51,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pilz.UI.Gtk", "Pilz.UI.Gtk\
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pilz.UI", "Pilz.UI\Pilz.UI.csproj", "{E75FBCEF-B971-4036-85D3-27D4ACA77156}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pilz.UI", "Pilz.UI\Pilz.UI.csproj", "{E75FBCEF-B971-4036-85D3-27D4ACA77156}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pilz.Plugins.Advanced.UI.Gtk", "Pilz.Plugins.Advanced.UI.Gtk\Pilz.Plugins.Advanced.UI.Gtk.csproj", "{F9CA6B5C-F14F-418D-9617-6007ED75E82F}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -251,6 +253,14 @@ Global
|
|||||||
{E75FBCEF-B971-4036-85D3-27D4ACA77156}.Release|Any CPU.Build.0 = Release|Any CPU
|
{E75FBCEF-B971-4036-85D3-27D4ACA77156}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{E75FBCEF-B971-4036-85D3-27D4ACA77156}.Release|x86.ActiveCfg = Release|Any CPU
|
{E75FBCEF-B971-4036-85D3-27D4ACA77156}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{E75FBCEF-B971-4036-85D3-27D4ACA77156}.Release|x86.Build.0 = Release|Any CPU
|
{E75FBCEF-B971-4036-85D3-27D4ACA77156}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{F9CA6B5C-F14F-418D-9617-6007ED75E82F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{F9CA6B5C-F14F-418D-9617-6007ED75E82F}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{F9CA6B5C-F14F-418D-9617-6007ED75E82F}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{F9CA6B5C-F14F-418D-9617-6007ED75E82F}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{F9CA6B5C-F14F-418D-9617-6007ED75E82F}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{F9CA6B5C-F14F-418D-9617-6007ED75E82F}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{F9CA6B5C-F14F-418D-9617-6007ED75E82F}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{F9CA6B5C-F14F-418D-9617-6007ED75E82F}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
Reference in New Issue
Block a user