WebView2 Service Triad & Vue 3 Frontend Architecture
ShibaPPT combines a .NET VSTO backend with a Vue 3.5 + Vite 7.2 multi-page frontend hosted inside Microsoft Edge WebView2. To guarantee rock-solid stability across multi-window STA COM threads (preventing white flashes, reentrancy deadlocks, and shutdown crashes), all WebView2 hosts adhere to the Core WebView2 Service Triad.
1. The C# WebView2 Service Triad (Core/WebView/)
IMPORTANT
verify-architecture.ps1 blocks direct calls to .EnsureCoreWebView2Async, .SetVirtualHostNameToFolderMapping, and .WebMessageReceived += outside Core/WebView/. Always use the three services below.
1. Environment & Initialization: WebView2EnvironmentManager + WebView2InitializationService
- Per-STA-Thread Shared Environment:
CoreWebView2Environmenthas strict COM thread affinity.WebView2EnvironmentManagercaches one environment perThread.CurrentThread.ManagedThreadIdbacked by%LocalAppData%\ShibAppt\WebView2Data. - 30s Timeout & Self-Healing COM Retry:
WebView2InitializationService.InitializeAsync(webView, ownerName)supports both WPF and WinForms controls. If attempt 1 fails withCOMExceptionorInvalidComObjectException, it resets the cached environment and retries automatically. - Virtual Host Folder Mapping (
MapLocalFolder): Maps local bundle directories to HTTPS virtual origins (e.g.,https://design.local), enabling standard ES Modules withoutfile:///CORS restrictions.
2. Lifecycle & Live Bridges: WebView2LifecycleService
Calling WebView2LifecycleService.Attach(ownerWindowOrControl, webView, ownerName, messageHandler) automatically provides:
- Idempotent Attachment: Uses
ConditionalWeakTableto prevent duplicate event subscriptions. - Live Language Sync: Listens to
NavigationCompletedandShibaLocalizationManager.LanguageChanged, pushing{ action: "languageChanged", locale, messages }to the WebView. - Live Theme Sync: Subscribes WPF windows to
UnifiedWindowChromeManager.Instance.WindowThemeChangedand pushes{ action: "themeChanged", isDark, isDarkTheme, theme }. - Crash-Free STA Disposal (
SafeDisposeWebView2): Explicitly closes the underlying_coreWebView2Controlleron its owning STA thread and callsGC.SuppressFinalizeso MTA finalizers never crashpowerpnt.exeduring AppDomain unload.
3. Reentrancy-Safe Routing: WebViewMessageRouter
- COM Reentrancy Protection: Opening modal dialogs or child WebView2 windows synchronously inside a native
WebMessageReceivedcallback can deadlock or crash Chromium (0xff).HandleWebMessageReceivedextracts the raw JSON string synchronously, exits the native callback immediately, and queuesDispatch(rawJson)viaDispatcher.BeginInvoke. - Declarative Fluent API:csharpNote:
_router = new WebViewMessageRouter(nameof(MyTaskPane), SendJsonToWeb) .On("applyAlign", payload => { ApplyAlignment(payload["mode"]?.ToString()); }) .On("getSlideStats", payload => new { action = "slideStatsLoaded", messageId = payload["messageId"]?.ToString(), success = true, shapeCount = GetActiveSlideShapeCount() });"getLanguagePack"is pre-registered automatically in everyWebViewMessageRouterinstance.
2. Vue 3 Multi-Entry Frontend (shibappt-ui/)
shibappt-ui is a multi-entry Vite 7 project compiling 9 standalone frontend bundles alongside shared UI primitives and composables.
1. All 9 Vite Rollup Entries & Virtual Hosts
Vite --mode | Source Entry (shibappt-ui/src/...) | Built Output Directory | WebView2 Virtual Origin |
|---|---|---|---|
design | modules/design/main.js | mod_design/html/ | https://design.local/index.html |
colorpicker | modules/design/color-picker-main.js | mod_colorpicker/html/ | https://colorpicker.local/index.html |
fonts | modules/fonts/main.js | mod_Fonts/html/ | https://fonts.local/index.html |
icon-picker | components/icon-picker/main.js | shibappt-ui/dist/html-templates/circular-menu/icon-picker.html | https://circular-menu.local/html-templates/circular-menu/icon-picker.html |
command-center | components/circular-menu/command-center/main.js | shibappt-ui/dist/html-templates/circular-menu/command-center.html | https://circular-menu.local/html-templates/circular-menu/command-center.html |
vba-editor | components/vba-editor/main.js | shibappt-ui/dist/html-templates/circular-menu/vba-editor.html | https://circular-menu.local/html-templates/circular-menu/vba-editor.html |
web-image-picker | components/web-image-picker/main.js | shibappt-ui/dist/html-templates/circular-menu/web-image-picker.html | https://circular-menu.local/html-templates/circular-menu/web-image-picker.html |
custom-ribbon | modules/custom-ribbon/main.js | shibappt-ui/dist/html-templates/circular-menu/custom-ribbon.html | https://circular-menu.local/html-templates/circular-menu/custom-ribbon.html |
cross-page | modules/cross-page/main.js | shibappt-ui/dist/html-templates/circular-menu/cross-page.html | https://circular-menu.local/html-templates/circular-menu/cross-page.html |
Note: mod_paperjs/html (https://paperjs.local) and mod_vtracer (https://vtracer.local) maintain standalone JS/HTML bundles, while lightweight dialogs such as PluginSettingsWebWindow render inline HTML via NavigateToString.
2. Custom Vite Plugins (moveHtmlPlugin.js & fix-paths.cjs)
moveHtmlPlugin.js: Recursively traces staticimport, dynamicimport(), and Vite__vite__mapDepsgraphs to copy exact transitive JS/CSS chunks intomod_design/html,mod_colorpicker/html, andmod_Fonts/html, while strippingcrossoriginattributes.fix-paths.cjs: Rewrites/assets/...paths to./assets/...across.html,.js, and.cssfiles, verified afterward byverify-web-assets.ps1.
3. Shared Vue Composables (shibappt-ui/src/shared/composables/)
1. useWebView2.js — Bidirectional Messaging
import { useWebView2 } from '@/shared/composables/useWebView2.js'
const { postMessage, sendMessage, onMessage } = useWebView2()
// Fire-and-forget command to C#
postMessage({ action: 'applyAlign', mode: 'left' })
// Promise-based request/response (auto-attaches messageId with 10s timeout)
const res = await sendMessage({ action: 'getSlideStats' })
// Subscribe to C# push events (returns unsubscribe callback)
const stop = onMessage('selectionChanged', (payload) => {
console.log('Selection updated:', payload)
})2. useI18n.js — Reactive Localization
import { useI18n } from '@/shared/composables/useI18n.js'
const { t, currentLocale } = useI18n()
const saveLabel = computed(() => t('ui.common.save', '保存'))3. useTheme.js & useConfirmDialog.js
useTheme(): HTML templates pre-setdata-theme="dark"in<head>to avoid startup flash;useTheme()listens tothemeChanged/initialThemeand syncsdata-themeand.darkCSS variables (shadcn.css).useConfirmDialog(): Promise-based modal dialog (await showConfirmDialog(...)) replacing blockingwindow.confirm()calls inside WebView2.
