Skip to content

Add HtmlRenderer.WinUI adapter for WinUI 3 / Win2D - #268

Open
jhaygood86 wants to merge 2 commits into
ArthurHub:masterfrom
jhaygood86:feature/winui-adapter
Open

Add HtmlRenderer.WinUI adapter for WinUI 3 / Win2D#268
jhaygood86 wants to merge 2 commits into
ArthurHub:masterfrom
jhaygood86:feature/winui-adapter

Conversation

@jhaygood86

Copy link
Copy Markdown
Contributor

Summary

  • New HtmlRenderer.WinUI adapter project, living beside HtmlRenderer.WinForms and HtmlRenderer.WPF, targeting WinUI 3 (Windows App SDK 2.4.0) with Win2D as the 2D rendering backend.
  • Implements the full adapter surface (RAdapter/RGraphics/RFont/RFontFamily/RImage/RBrush/RPen/RGraphicsPath/RControl/RContextMenu) and an HtmlControl/HtmlPanel/HtmlLabel/HtmlRender control layer mirroring the WPF adapter's shape (shared HtmlControl base, DependencyPropertys), with plain EventHandler<T> events instead of WPF's custom RoutedEvent wrapper (WinUI 3 has no equivalent for user-defined routed events).
  • Dark-mode support via UISettings.ColorValuesChanged, CF_HTML clipboard copy/paste, @font-face loading via CanvasFontSet.
  • New HtmlRenderer.Demo.WinUI demo app with the same sample-browser experience (TreeView + SamplesLoader/HtmlSample from HtmlRenderer.Demo.Common) as the WinForms/WPF demos.
  • Solution/csproj wiring: both new projects added to HtmlRenderer.sln; InternalsVisibleTo added to the core HtmlRenderer.csproj for the new adapter's test-seam access (SystemColorSchemeOverride).

Notable implementation notes

  • Win2D's CanvasTextLayout.LayoutBounds excludes trailing-whitespace advance width entirely, so measuring a single space came back zero — GraphicsAdapter.MeasureString falls back to an append-a-sentinel-glyph trick to recover the true advance width when this happens.
  • CssBox.PaintBackground (shared Core code) unconditionally disposes the brush it paints with after use; this is harmless for WPF/WinForms (their brush Dispose() is a no-op) but was fatal for Win2D's real ICanvasBrush resources, since RAdapter.GetSolidBrush is a process-lifetime cache — BrushAdapter.Dispose() is a no-op here too, matching the WPF adapter's own pattern.
  • A standalone Microsoft.UI.Xaml.Controls.Primitives.ScrollBar (used outside a ScrollViewer) needs IndicatorMode set explicitly or it renders nothing regardless of Visibility.
  • Manual TreeView.RootNodes construction displays TreeViewNode.Content directly for plain strings — no ItemTemplate/{Binding} needed for that case (used in the demo's sample picker).

Test plan

  • dotnet build on the full solution succeeds across all TFMs, including the new WinUI/Win2D targets.
  • Existing HtmlRenderer.Test/HtmlRenderer.IntegrationTest/HtmlRenderer.PdfSharp.Test suites pass unchanged (core library and existing WinForms/WPF/PdfSharp adapters untouched).
  • Verified the paint pipeline directly (off-screen CanvasRenderTarget renders of real sample HTML) for the whitespace-measurement and brush-disposal fixes, including a real double-paint reproduction of the brush-disposal bug before/after the fix.
  • Manually exercised the demo app: sample browsing, text selection, dark/light theme toggling, window resizing, and scrolling.

New adapter beside HtmlRenderer.WinForms and HtmlRenderer.WPF, targeting
WinUI 3 (Windows App SDK) via Win2D for 2D rendering. Implements the full
RAdapter/RGraphics/RFont/RImage/RBrush/RPen/RGraphicsPath/RControl/
RContextMenu adapter surface, an HtmlControl/HtmlPanel/HtmlLabel/HtmlRender
control layer mirroring the WPF adapter's shape, dark-mode support via
UISettings, CF_HTML clipboard copy/paste, and a minimal demo app with the
same sample-browser experience as the WinForms/WPF demos.

Solution/csproj wiring: new HtmlRenderer.WinUI and HtmlRenderer.Demo.WinUI
projects added to HtmlRenderer.sln; InternalsVisibleTo added to the core
HtmlRenderer.csproj for the new adapter's test-seam access.
HtmlRenderer.WinUI/HtmlRenderer.Demo.WinUI depend on the Windows App SDK's
MSIX PRI-generation step, which shells out to native Windows x86 binaries
(makepri.exe/makeappx.exe) - unlike the WinForms/WPF adapters, which
cross-compile cleanly everywhere via EnableWindowsTargeting's reference-
assembly story, there is no non-Windows equivalent for those native tools
at all ("Exec format error" on Ubuntu/macOS).

Adds Source/HtmlRenderer.CrossPlatform.slnf, excluding just those two
projects, and has the workflow build the full .sln on Windows but the
filtered one everywhere else - every other project (including the WinUI
adapter's own test-relevant siblings) still builds and tests identically
on all three OSes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants