Plugin System
Extend Nimbus with powerful built-in plugins or create your own using C# and XML.
1. Built-in Plugins
Nimbus comes with a rich set of plugins ready to use. These cover everything from math to network requests.
2. How to Use Plugins
There are two ways to use plugins: calling functions that return values, or executing commands.
Calling Functions
Use the <Plugin> tag to execute a method and store the result in a variable.
<!-- Calculate Square Root -->
<Plugin Name="MathPlugin" Method="sqrt" Params="16" ToState="result"/>
<!-- Get String Length -->
<Plugin Name="StringPlugin" Method="length" Params="Hello" ToState="len"/>
<!-- Generate GUID -->
<Plugin Name="CryptoPlugin" Method="guid" ToState="myId"/>
Executing Commands
Some plugins register custom XML commands. For example, FilePlugin adds <file.write>.
<!-- Using Command -->
<file.write Path="log.txt" Content="System started"/>
<!-- Using Dialog -->
<dialog.input Title="Enter Name" Message="Who are you?" ToState="username"/>
<!-- Using UI Helper -->
<ui.toast Message="Operation Complete"/>
3. Creating C# Plugins
You can create powerful plugins by writing a C# class that implements INimbusPlugin. Save your file in the `plugins/` folder.
Example: Greeting Plugin
Create `plugins/MyPlugin.cs`:
using System;
using System.Xml;
using Nimbus.WPF; // Core framework namespace
public class MyPlugin : INimbusPlugin
{
// Metadata
public string Name { get { return "MyPlugin"; } }
public string Version { get { return "1.0"; } }
public string Description { get { return "A custom greeting plugin"; } }
public void OnLoad(WpfEngine engine)
{
// 1. Register a Function (returns string)
engine.RegisterFunction("myplugin.greet", delegate(string args)
{
return "Hello, " + args + "!";
});
// 2. Register a Command (performs action)
engine.RegisterCommand("myplugin.log", delegate(XmlNode node, object sender)
{
string msg = node.Attributes["Message"].Value;
engine.Log("MY_PLUGIN", msg);
return true;
});
}
public void OnUnload(WpfEngine engine)
{
// Cleanup logic if needed
}
public void OnEvent(WpfEngine engine, string eventName, object data)
{
// Listen to engine events
}
}
Using Your C# Plugin
<!-- Call Function -->
<Plugin Name="MyPlugin" Method="greet" Params="World" ToState="greeting"/>
<!-- Call Command -->
<myplugin.log Message="Custom log message"/>
4. Creating XML Plugins
For simpler logic, you can create "Logic Plugins" using purely XML. This is great for reusable handler libraries.
Example: Auth Plugin
Create `plugins/Auth.xml`:
<NimbusPlugin Name="AuthPlugin" Version="1.0">
<Handlers>
<Handler Name="Login">
<!-- Simulated login logic -->
<If Condition="{username} == 'admin' AND {password} == '123'">
<Set Var="isLoggedIn" Value="true"/>
<Toast Message="Login Successful!" Type="Success"/>
<Else>
<Set Var="isLoggedIn" Value="false"/>
<Alert Message="Invalid credentials"/>
</Else>
</If>
</Handler>
<Handler Name="Logout">
<Set Var="isLoggedIn" Value="false"/>
<Toast Message="Logged out"/>
</Handler>
</Handlers>
<!-- Expose commands to App.xml -->
<Commands>
<Command Name="auth.login" Handler="Login"/>
<Command Name="auth.logout" Handler="Logout"/>
</Commands>
</NimbusPlugin>
Using Your XML Plugin
Nimbus automatically loads `.xml` files in the `plugins/` folder.
<!-- Use the custom command directly -->
<auth.login/>
<!-- Or call via Plugin tag -->
<Plugin Name="AuthPlugin" Method="Logout"/>
5. Plugin API Reference
Methods available to C# plugins via the `WpfEngine` instance.
| Method | Description |
|---|---|
engine.RegisterFunction(name, func) | Register a string-returning function (e.g., `math.abs`). |
engine.RegisterCommand(name, handler) | Register a void-returning command (e.g., `file.write`). |
engine.SetVariable(name, value) | Set a state variable. |
engine.GetVariable(name) | Get a state variable value. |
engine.GetControl(name) | Get a WPF control reference. |
engine.Log(level, message) | Write to DevTools log. |
engine.ExecuteHandler(name) | Run another handler by name. |