Step-by-Step Module Extension Tutorial
With ShibaModuleHost reflection auto-discovery and the scripts\new-module.ps1 scaffolding script, adding a new feature module to ShibaPPT requires zero edits to ThisAddIn.cs. This tutorial walks through scaffolding a module, managing multi-window TaskPanes, and wiring up Ribbon buttons, CircularMenu sectors, and license gates.
1. Scaffold a Module in One Command (scripts\new-module.ps1)
Run scripts\new-module.ps1 from the repository root. It supports 3 templates and automatically registers the generated files in shibappt.csproj when -RegisterInCsproj is passed:
# Template 1: Standalone window, dialog, or background command module
powershell -ExecutionPolicy Bypass -File scripts\new-module.ps1 `
-Name SmartChart -DisplayName "Smart Chart" -RegisterInCsproj
# Template 2: Module with a docked WinForms UserControl TaskPane
powershell -ExecutionPolicy Bypass -File scripts\new-module.ps1 `
-Name SmartChart -DisplayName "Smart Chart" -TaskPaneWidth 360 -WithTaskPane -RegisterInCsproj
# Template 3: Full-stack WebView2 + WebViewMessageRouter TaskPane module (Recommended)
powershell -ExecutionPolicy Bypass -File scripts\new-module.ps1 `
-Name SmartChart -DisplayName "Smart Chart" -TaskPaneWidth 360 -WithTaskPane -WithWebView -RegisterInCsprojTemplate 3 creates mod_SmartChart/ with two architecture-compliant files:
mod_SmartChart\SmartChartModule.cs: ImplementsIShibaModuleand registersmod_smartchart+smartchart.toggle_taskpane.mod_SmartChart\SmartChartTaskPane.cs: Pre-wired withWebView2InitializationService,WebView2LifecycleService, andWebViewMessageRouter, mapped tohttps://smartchart.local/index.html.
2. Implementing IShibaModule & Multi-Window TaskPanes
using shibappt.Core.Modularity;
namespace shibappt.mod_SmartChart
{
// Smart Chart module - auto-discovered and initialized by ShibaModuleHost at startup
public sealed class SmartChartModule : IShibaModule
{
public const string TaskPaneKey = "mod_smartchart";
public const string ToggleCommandId = "smartchart.toggle_taskpane";
public string ModuleId => TaskPaneKey;
public string DisplayName => "智能图表";
// Lifecycle - register multi-window TaskPane factory and CommandBus handlers
public void Initialize(ShibaModuleContext context)
{
context.TaskPanes.Register(
TaskPaneKey,
DisplayName,
360,
window =>
{
var control = new SmartChartTaskPane();
control.InitializePowerPoint(context.Application, window.Presentation);
return control;
},
initialVisible: false);
// Parameterless command
context.Commands.Register(ToggleCommandId, () => context.TaskPanes.Toggle(TaskPaneKey));
// Parameterized command example
context.Commands.Register("smartchart.apply_preset", param =>
{
string presetId = param as string ?? "default";
// Apply preset logic...
});
}
// Lifecycle - clean up hooks or unmanaged resources on PowerPoint exit
public void Shutdown()
{
}
}
}Why CustomTaskPaneManager Matters
PowerPoint creates a separate DocumentWindow for every open .pptx file. CustomTaskPaneManager maintains a nested dictionary (_panesByModule[key][DocumentWindow]) so task panes are lazily instantiated only for windows where the user opens them, and batch-hidden across all windows if the user disables the module in settings.
3. Building Standalone Floating WPF Windows
If your module opens a floating tool window (like ColorPicker, Paper.js, or AI Suite) rather than a docked TaskPane, leverage these built-in window utilities:
- Singleton Activation (
SingleInstanceWindowHelper): Prevents duplicate windows and automatically un-collapses docked edge windows when re-activated:csharpSingleInstanceWindowHelper.ShowOrActivate( () => new SmartChartWindow(), existing => existing.RefreshSelection()); - Borderless Chrome & Theme Sync (
UnifiedWindowChrome): Attach in XAML to get unified dark/light toggle, pin-topmost, minimize, and close buttons with automatic WebView2 theme synchronization:xmlchrome:UnifiedWindowChrome.IsEnabled="True" chrome:UnifiedWindowChrome.TitleText="Smart Chart" chrome:UnifiedWindowChrome.Buttons="ThemeToggle,PinTopmost,Minimize,Close" - Edge Snapping & Auto-Collapse (
WindowDocking): EnableWindowDocking.IsEnabled="True"andAutoCollapse="True"on floating toolbars for 1px screen-edge docking with hover slide-out.
4. Wiring Entry Points (Ribbon, CircularMenu, Licensing)
Once registered on context.Commands, your command ID works everywhere with zero class coupling:
1. PowerPoint Ribbon (UI/Ribbon/)
- Declare the control in
UI\Ribbon\Ribbon1.xml:xml<button id="bt_SmartChart" label="智能图表" size="large" onAction="bt_SmartChart_Click" getImage="GetRibbonImage" /> - Dispatch via
CommandBusin the appropriateUI\Ribbon\Ribbon1.<Domain>.cspartial class:csharp// Ribbon entry - toggle Smart Chart task pane public void bt_SmartChart_Click(Office.IRibbonControl control) { Globals.ThisAddIn.CommandBus?.TryExecute("smartchart.toggle_taskpane"); } - Add
"bt_SmartChart"and"mod_smartchart"entries inShibaLocalizationManager.cs(zh-CNanden-US) for live Ribbon localization.
2. CircularMenu & Custom Ribbon
Users can bind shiba:smartchart.toggle_taskpane (accepts shiba:, cmd:, or the bare command ID) directly to any radial sector in the Circular Menu Editor or any custom button in the Custom Ribbon Designer.
3. Optional Commercial License Gate (AddInLicense)
To gate a premium capability behind a license feature (svg_tools, image_ai, custom_ribbon, dev_tools):
if (!AddInLicense.EnsureFeature(LicenseFeatures.SvgTools, "Smart Chart"))
return;If unlicensed, AddInLicense automatically opens the License Center tab on the UI thread and aborts cleanly.
5. Pre-Commit Verification Checklist
- Run
powershell -ExecutionPolicy Bypass -File scripts\build.ps1 -SkipNpmInstalland confirm[web-assets] Validation passed,[architecture] Validation passed, and 0 Error, 0 Warning from MSBuild. - Confirm
ThisAddIn.cshas zero references to your module namespace. - Confirm all
.csand.vuefiles remain $\le 2,000$ lines. - Toggle your new module off and back on in
Plugin Settings -> Module Managementto verify clean command interception.
