Skip to content

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:

powershell
# 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 -RegisterInCsproj

Template 3 creates mod_SmartChart/ with two architecture-compliant files:

  • mod_SmartChart\SmartChartModule.cs: Implements IShibaModule and registers mod_smartchart + smartchart.toggle_taskpane.
  • mod_SmartChart\SmartChartTaskPane.cs: Pre-wired with WebView2InitializationService, WebView2LifecycleService, and WebViewMessageRouter, mapped to https://smartchart.local/index.html.

2. Implementing IShibaModule & Multi-Window TaskPanes ​

csharp
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:

  1. Singleton Activation (SingleInstanceWindowHelper): Prevents duplicate windows and automatically un-collapses docked edge windows when re-activated:
    csharp
    SingleInstanceWindowHelper.ShowOrActivate(
        () => new SmartChartWindow(),
        existing => existing.RefreshSelection());
  2. 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:
    xml
    chrome:UnifiedWindowChrome.IsEnabled="True"
    chrome:UnifiedWindowChrome.TitleText="Smart Chart"
    chrome:UnifiedWindowChrome.Buttons="ThemeToggle,PinTopmost,Minimize,Close"
  3. Edge Snapping & Auto-Collapse (WindowDocking): Enable WindowDocking.IsEnabled="True" and AutoCollapse="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/) ​

  1. Declare the control in UI\Ribbon\Ribbon1.xml:
    xml
    <button id="bt_SmartChart" label="智能图表" size="large" onAction="bt_SmartChart_Click" getImage="GetRibbonImage" />
  2. Dispatch via CommandBus in the appropriate UI\Ribbon\Ribbon1.<Domain>.cs partial class:
    csharp
    // Ribbon entry - toggle Smart Chart task pane
    public void bt_SmartChart_Click(Office.IRibbonControl control)
    {
        Globals.ThisAddIn.CommandBus?.TryExecute("smartchart.toggle_taskpane");
    }
  3. Add "bt_SmartChart" and "mod_smartchart" entries in ShibaLocalizationManager.cs (zh-CN and en-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):

csharp
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 ​

  1. Run powershell -ExecutionPolicy Bypass -File scripts\build.ps1 -SkipNpmInstall and confirm [web-assets] Validation passed, [architecture] Validation passed, and 0 Error, 0 Warning from MSBuild.
  2. Confirm ThisAddIn.cs has zero references to your module namespace.
  3. Confirm all .cs and .vue files remain $\le 2,000$ lines.
  4. Toggle your new module off and back on in Plugin Settings -> Module Management to verify clean command interception.