From 92e8540cca287e1f8e794340c2efaa17b9723ba6 Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Wed, 18 Nov 2026 08:55:28 -0400 Subject: [PATCH] feat(canvas): draw NativeScript views into a canvas on Windows HTML-in-Canvas on WinUI 3. The children of a content="drawable" canvas live in a transparent XAML Canvas on top of the SwapChainPanel, each in a Grid slot that carries the drawn geometry (a RenderTransform, a clip, and hit-test visibility), so XAML's own hit testing finds the view where it was drawn. XAML input follows neither a Projection nor a 4x4 TransformMatrix, so for a matrix with perspective the host sees each pointer message before XAML (it subclasses the island's windows): the view's slot gets the matrix's tangent at the point under the pointer, exact there, for mouse, pen and a touch's first contact alike. Hosted views keep the size they were given (no layout rounding), and snapshots are resampled to the requested pixel size, since RenderTargetBitmap takes whole DIPs. The host is native (NSCDrawableHost in canvas-napi): JS makes one update() call a frame. host/windows/xaml.rs declares the WinUI 3 members it calls, from the Windows App SDK 1.6 metadata (the windows crate covers only Windows.*). WinUI 3 has no per-element invalidation and only an async snapshot, so views are re-rendered with RenderTargetBitmap on a schedule that is hot for half a second after a change or input and backs off to once a second when idle, and compared (a quarter-size probe first). The async steps are polled from update(): XAML objects are not Send. captureElementImage and cropped uploads come from the last snapshot. A press on a hosted view no longer takes pointer capture and focus for the canvas. The htmlincanvas spec suite is registered on Windows. The demo follows the system theme. --- apps/demo/nativescript.config.ts | 4 - apps/demo/src/app.scss | 34 + apps/demo/src/plugin-demos/canvas-html.xml | 4 +- crates/canvas-napi/Cargo.toml | 4 + crates/canvas-napi/src/host/windows.rs | 4 + .../canvas-napi/src/host/windows/drawable.rs | 723 ++++++++++++++++++ .../canvas-napi/src/host/windows/picking.rs | 357 +++++++++ crates/canvas-napi/src/host/windows/xaml.rs | 653 ++++++++++++++++ crates/canvas-napi/src/image_asset.rs | 10 + packages/canvas/Canvas/drawable/common.ts | 14 +- .../canvas/Canvas/drawable/index.windows.ts | 175 ++++- packages/canvas/Canvas/index.windows.ts | 11 +- packages/canvas/README.md | 4 +- .../Android/src/main/res/values/styles.xml | 4 +- tools/demo/canvas/spec/htmlincanvas.ts | 73 +- tools/demo/canvas/spec/index.ts | 6 +- 16 files changed, 2053 insertions(+), 27 deletions(-) create mode 100644 crates/canvas-napi/src/host/windows/drawable.rs create mode 100644 crates/canvas-napi/src/host/windows/picking.rs create mode 100644 crates/canvas-napi/src/host/windows/xaml.rs diff --git a/apps/demo/nativescript.config.ts b/apps/demo/nativescript.config.ts index c0001c37e..11e5fb0ea 100644 --- a/apps/demo/nativescript.config.ts +++ b/apps/demo/nativescript.config.ts @@ -8,10 +8,6 @@ export default { markingMode: 'none', discardUncaughtJsExceptions: false, }, - // Light like the Android app theme (Theme.AppCompat.Light), whatever the desktop uses - linux: { - appearance: 'light', - }, appPath: 'src', ios: { discardUncaughtJsExceptions: false, diff --git a/apps/demo/src/app.scss b/apps/demo/src/app.scss index b1163cc3b..5663d637f 100644 --- a/apps/demo/src/app.scss +++ b/apps/demo/src/app.scss @@ -1,6 +1,40 @@ @import 'nativescript-theme-core/scss/light'; @import 'nativescript-theme-core/scss/index'; +// Follow the system theme: core sets .ns-dark on the root view (theme-core's dark skin colors). +.ns-dark { + .page { + background-color: #303030; + color: #fff; + } + + .action-bar { + background-color: #303030; + color: #fff; + } +} + +// canvas-html: the card drawn into the canvas. +.drawable-card { + background-color: #fff; + border-radius: 16; + padding: 20; +} + +.drawable-card-note { + color: #666; +} + +.ns-dark { + .drawable-card { + background-color: #2b2b2b; + } + + .drawable-card-note { + color: #b3b3b3; + } +} + .gameover { font-weight: bold; text-transform: uppercase; diff --git a/apps/demo/src/plugin-demos/canvas-html.xml b/apps/demo/src/plugin-demos/canvas-html.xml index 5758b258e..2c60b9fa1 100644 --- a/apps/demo/src/plugin-demos/canvas-html.xml +++ b/apps/demo/src/plugin-demos/canvas-html.xml @@ -4,9 +4,9 @@ - +